Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: support multiple backends #51

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
99a156d
refactor: dynamically import tnp in backend module
redeboer May 27, 2021
8b9c454
refactor: create numpy interface for tf.random
redeboer May 27, 2021
abc86a2
refactor: generalize random module
redeboer May 27, 2021
8a3030f
refactor: move tf.shape to backend module
redeboer May 27, 2021
609754a
refactor: move tf.assert's to backend module
redeboer May 27, 2021
94ebe80
refactor: move Tensor/Variable to backend module
redeboer May 27, 2021
170c620
refactor: remove last tensorflow imports
redeboer May 27, 2021
e450c6c
test: add skip decorator for tests that requrie TF
redeboer May 27, 2021
3ff2a1c
test: use phasespace.backend in tests
redeboer May 27, 2021
2aa6f93
test: use uniform instead of uniform_full_int
redeboer May 27, 2021
fe2501a
feat: provide mapping for numpy as backend
redeboer May 27, 2021
d1bb7dd
refactor!: set numpy as default backend
redeboer May 27, 2021
3bd1f85
build!: move tensorflow requirements to extras
redeboer May 27, 2021
e98d7cd
ci: run pytest with NumPy as backend
redeboer May 27, 2021
290852c
fix: remove duplicate Tensor/Variable import
redeboer May 27, 2021
0dbfdfc
Merge branch 'master' into numpy-backend
redeboer May 28, 2021
1c80cc8
Merge branch 'master' into numpy-backend
redeboer Sep 21, 2021
8413194
revert: move tensorflow requirements to extras
redeboer Sep 21, 2021
669aaf7
Merge branch 'master' into numpy-backend
jonas-eschle Mar 31, 2022
dc9b865
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 31, 2022
1e23047
Merge branch 'master' into numpy-backend
jonas-eschle Aug 25, 2022
aeea1a7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 25, 2022
7707f58
Merge branch 'master' into numpy-backend
jonas-eschle Feb 8, 2024
1c321a3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: move Tensor/Variable to backend module
redeboer committed May 27, 2021
commit 94ebe804f555a19df5b3e892290d31f40f86d56d
2 changes: 2 additions & 0 deletions phasespace/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -49,6 +49,8 @@ def get_backend(backend: str) -> "BackendType":
**{jit_compile_argname: True},
)

Tensor = tf.Tensor
Variable = tf.Variable
get_shape = tf.shape # get shape dynamically
assert_equal = tf.assert_equal
assert_greater_equal = tf.debugging.assert_greater_equal
20 changes: 11 additions & 9 deletions phasespace/phasespace.py
Original file line number Diff line number Diff line change
@@ -15,6 +15,8 @@

from . import kinematics as kin
from .backend import (
Tensor,
Variable,
assert_equal,
assert_greater_equal,
function,
@@ -103,7 +105,7 @@ def __init__(self, name: str, mass: Union[Callable, int, float]) -> None: # noq
self.name = name
self.children = []
self._mass_val = mass
if not callable(mass) and not isinstance(mass, tf.Variable):
if not callable(mass) and not isinstance(mass, Variable):
mass = tnp.asarray(mass, dtype=tnp.float64)
else:
mass = tf.function(
@@ -138,11 +140,11 @@ def get_list_of_names(part):
@function
def get_mass(
self,
min_mass: tf.Tensor = None,
max_mass: tf.Tensor = None,
n_events: Union[tf.Tensor, tf.Variable] = None,
min_mass: Tensor = None,
max_mass: Tensor = None,
n_events: Union[Tensor, Variable] = None,
seed: SeedLike = None,
) -> tf.Tensor:
) -> Tensor:
"""Get the particle mass.

If the particle is resonant, the mass function will be called with the
@@ -619,11 +621,11 @@ def recurse_w_max(parent_mass, current_mass_tree):

def generate(
self,
n_events: Union[int, tf.Tensor, tf.Variable],
boost_to: Optional[tf.Tensor] = None,
n_events: Union[int, Tensor, Variable],
boost_to: Optional[Tensor] = None,
normalize_weights: bool = True,
seed: SeedLike = None,
) -> Tuple[tf.Tensor, Dict[str, tf.Tensor]]:
) -> Tuple[Tensor, Dict[str, Tensor]]:
"""Generate normalized n-body phase space as tensorflow tensors.

Any TensorFlow tensor can always be converted to a numpy array with the method `numpy()`.
@@ -666,7 +668,7 @@ def generate(
f"of {boost_to.shape}"
)
assert_equal(len(boost_to), n_events, message=message)
if not isinstance(n_events, tf.Variable):
if not isinstance(n_events, Variable):
n_events = tnp.asarray(n_events, dtype=tnp.int64)
weights, weights_max, parts, _ = self._recursive_generate(
n_events=n_events,