Skip to content

Commit

Permalink
update pre-install script to tfp 0.25 (#177)
Browse files Browse the repository at this point in the history
* update pre-install script to tfp 0.25

* fix dtype compatibility

* simplify install with optional deps

* add python 3.12 to matrix build

* remove pkg_resources dep
  • Loading branch information
kmdalton authored Nov 12, 2024
1 parent 46ffb65 commit 848f0c1
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

# Skip CI if 'skip ci' is contained in latest commit message
if: "!contains(github.event.head_commit.message, 'skip ci')"
Expand Down
12 changes: 9 additions & 3 deletions careless/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Version number for careless
def getVersionNumber():
import pkg_resources
version = None
try:
from setuptools.version import metadata

version = pkg_resources.require("careless")[0].version
return version
version = metadata.version("careless")
except ImportError:
from setuptools.version import pkg_resources

version = pkg_resources.require("careless")[0].version

return version

__version__ = getVersionNumber()
3 changes: 2 additions & 1 deletion careless/models/likelihoods/laue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tensorflow_probability import bijectors as tfb
import tensorflow_probability as tfp
import tensorflow as tf
import math
import numpy as np

class ConvolvedLikelihood():
Expand Down Expand Up @@ -78,7 +79,7 @@ def dist(self, inputs):
scale = self.get_uncertainties(inputs)
loc = tf.squeeze(loc)
scale = tf.squeeze(scale)
return tfd.Laplace(loc, scale/np.sqrt(2.))
return tfd.Laplace(loc, scale / math.sqrt(2.))

class StudentTLikelihood(LaueBase):
def __init__(self, dof):
Expand Down
3 changes: 2 additions & 1 deletion careless/models/likelihoods/mono.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tensorflow_probability import distributions as tfd
from tensorflow_probability import util as tfu
from tensorflow_probability import bijectors as tfb
import math
import numpy as np

class LocationScaleLikelihood(Likelihood):
Expand All @@ -19,7 +20,7 @@ def call(self, inputs):
class LaplaceLikelihood(LocationScaleLikelihood):
def call(self, inputs):
loc, scale = self.get_loc_and_scale(inputs)
return tfd.Laplace(loc, scale/np.sqrt(2.))
return tfd.Laplace(loc, scale/math.sqrt(2.))

class StudentTLikelihood(LocationScaleLikelihood):
def __init__(self, dof):
Expand Down
3 changes: 2 additions & 1 deletion careless/models/priors/empirical.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
from careless.models.priors.base import Prior
from careless.models.merging.surrogate_posteriors import RiceWoolfson
import math


class ReferencePrior():
Expand Down Expand Up @@ -57,7 +58,7 @@ def __init__(self, Fobs, SigFobs, observed=None):
"""
super().__init__(observed)
loc = np.array(Fobs, dtype=np.float32)
scale = np.array(SigFobs, dtype=np.float32)/np.sqrt(2.)
scale = np.array(SigFobs, dtype=np.float32)/math.sqrt(2.)
self.base_dist = tfd.Laplace(loc, scale)

class NormalReferencePrior(ReferencePrior):
Expand Down
5 changes: 3 additions & 2 deletions install-cuda.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ fi
conda activate $ENVNAME

# Install TensorFlow Probability
source <(curl -s https://raw.githubusercontent.com/rs-station/careless/main/install-tfp.sh)
# Currently not necessary
#source <(curl -s https://raw.githubusercontent.com/rs-station/careless/main/install-tfp.sh)

# Reactivate to update cuda paths
conda activate $ENVNAME

# Install careless
pip install --upgrade careless
pip install careless[cuda]

# Run careless devices
careless devices
36 changes: 4 additions & 32 deletions install-tfp.sh
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
TFP_VERSION=0.24.0
TF_VERSION=2.16.1
TFP_VERSION=0.25.0
TF_VERSION=2.18.0

pip install --upgrade pip

pip install tensorflow[and-cuda]==$TF_VERSION
pip install tensorflow-probability[tf]==$TFP_VERSION tensorflow[and-cuda]==$TF_VERSION

# The following is a workaround for a bug in tensorflow cuda installation
# https://github.com/tensorflow/tensorflow/issues/63362#issuecomment-2134680575
mkdir -p $CONDA_PREFIX/etc/conda/activate.d
echo '# Store original LD_LIBRARY_PATH
export ORIGINAL_LD_LIBRARY_PATH="${LD_LIBRARY_PATH}"
# Get the CUDNN directory
CUDNN_DIR=$(dirname $(dirname $(python -c "import nvidia.cudnn; print(nvidia.cudnn.__file__)")))
# Set LD_LIBRARY_PATH to include CUDNN directory
export LD_LIBRARY_PATH=$(find ${CUDNN_DIR}/*/lib/ -type d -printf "%p:")${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# Get the ptxas directory
PTXAS_DIR=$(dirname $(dirname $(python -c "import nvidia.cuda_nvcc; print(nvidia.cuda_nvcc.__file__)")))
# Set PATH to include the directory containing ptxas
export PATH=$(find ${PTXAS_DIR}/*/bin/ -type d -printf "%p:")${PATH:+:${PATH}}
'>> $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh

mkdir -p $CONDA_PREFIX/etc/conda/deactivate.d
echo '# Restore original LD_LIBRARY_PATH
export LD_LIBRARY_PATH="${ORIGINAL_LD_LIBRARY_PATH}"
# Unset environment variables
unset CUDNN_DIR
unset PTXAS_DIR' >> $CONDA_PREFIX/etc/conda/deactivate.d/env_vars.sh
pip install tensorflow[and-cuda]==$TF_VERSION tf_keras
pip install tensorflow-probability[tf]==$TFP_VERSION tensorflow[and-cuda]==$TF_VERSION tf_keras

17 changes: 11 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ authors=[
description = "Merging crystallography data without much physics."
readme = "README.md"
dynamic = ["version"]
requires-python = ">= 3.9,<3.12"
requires-python = ">= 3.9,<3.13"
dependencies = [
"reciprocalspaceship>=0.9.16",
"tqdm",
"tensorflow<2.18",
"tf_keras",
"tensorflow-probability[tf]",
"tensorflow-probability[tf]==0.25",
"tensorflow==2.18.0",
"matplotlib",
"seaborn",
"ray",
]
optional-dependencies.dev = [

license={text = "MIT"}

[project.optional-dependencies]
dev = [
"pytest",
"pytest-cov",
"pytest-xdist",
]
license={text = "MIT"}
cuda = [
"tensorflow[and-cuda]==2.18.0",
]

[project.urls]
Repository = "https://github.com/rs-station/careless"
Expand Down
3 changes: 2 additions & 1 deletion tests/models/likelihoods/test_laue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from careless.models.likelihoods.laue import *
from careless.models.base import BaseModel
from tensorflow_probability import distributions as tfd
import math

from careless.utils.device import disable_gpu
status = disable_gpu()
Expand Down Expand Up @@ -40,7 +41,7 @@ def test_laue_LaplaceLikelihood(laue_inputs):
sigiobs = BaseModel.get_uncertainties(laue_inputs)
ipred = fake_ipred(laue_inputs)

l_true = tfd.Laplace(iobs, sigiobs/np.sqrt(2.))
l_true = tfd.Laplace(iobs, sigiobs/math.sqrt(2.))

iconv = likelihood.convolve(ipred)

Expand Down
3 changes: 2 additions & 1 deletion tests/models/likelihoods/test_mono.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from careless.models.likelihoods.mono import *
from careless.models.base import BaseModel
from tensorflow_probability import distributions as tfd
import math

from careless.utils.device import disable_gpu
status = disable_gpu()
Expand All @@ -28,7 +29,7 @@ def test_mono_LaplaceLikelihood(mono_inputs):

l_true = tfd.Laplace(
tf.squeeze(iobs),
tf.squeeze(sigiobs)/np.sqrt(2.),
tf.squeeze(sigiobs)/math.sqrt(2.),
)
z = l_true.sample()

Expand Down
3 changes: 2 additions & 1 deletion tests/models/priors/test_empirical.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tensorflow_probability import distributions as tfd
from careless.models.merging.surrogate_posteriors import RiceWoolfson
from careless.models.priors.empirical import *
import math
import numpy as np

from careless.utils.device import disable_gpu
Expand Down Expand Up @@ -48,7 +49,7 @@ def ReferencePrior_test(p, ref, mc_samples):
@pytest.mark.parametrize('mc_samples', [(), 3, 1])
def test_LaplaceReferencePrior(mc_samples):
p = LaplaceReferencePrior(Fobs[observed], SigFobs[observed], observed)
q = tfd.Laplace(Fobs, SigFobs/np.sqrt(2.))
q = tfd.Laplace(Fobs, SigFobs/math.sqrt(2.))
ReferencePrior_test(p, q, mc_samples)


Expand Down
3 changes: 2 additions & 1 deletion tests/utils/test_distributions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from careless.utils.distributions import Stacy,Amoroso
from tensorflow_probability import distributions as tfd
import numpy as np
import math


from careless.utils.device import disable_gpu
Expand Down Expand Up @@ -76,7 +77,7 @@ def test_halfnormal(xmin=1e-3, xmax=1e1, npoints=1000):
ref_dist_class = tfd.HalfNormal
scale = np.linspace(0.1, 100, 1000).astype(np.float32)
ref_params = (scale,)
amoroso_params = (0., np.sqrt(2.) * scale, 0.5, 2.)
amoroso_params = (0., math.sqrt(2.) * scale, 0.5, 2.)

# Construct distributions
ref_dist = ref_dist_class(*ref_params)
Expand Down

0 comments on commit 848f0c1

Please sign in to comment.