folx 0.2.2__tar.gz → 0.2.2.post1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {folx-0.2.2 → folx-0.2.2.post1}/PKG-INFO +1 -1
- {folx-0.2.2 → folx-0.2.2.post1}/folx/vmap.py +13 -11
- {folx-0.2.2 → folx-0.2.2.post1}/pyproject.toml +1 -1
- {folx-0.2.2 → folx-0.2.2.post1}/LICENSE +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/README.md +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/__init__.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/api.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/custom_hessian.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/hessian.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/interpreter.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/jvp.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/operators.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/tree_utils.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/utils.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/wrapped_functions.py +0 -0
- {folx-0.2.2 → folx-0.2.2.post1}/folx/wrapper.py +0 -0
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import functools
|
|
2
|
-
import
|
|
3
|
-
from typing import Any, Callable, Sequence, TypeVar, cast
|
|
2
|
+
from typing import Any, Callable, Sequence, TypeVar
|
|
4
3
|
|
|
5
4
|
import jax
|
|
6
5
|
import jax.numpy as jnp
|
|
@@ -18,6 +17,7 @@ def batched_vmap(
|
|
|
18
17
|
if isinstance(in_axes, list):
|
|
19
18
|
in_axes = tuple(in_axes)
|
|
20
19
|
|
|
20
|
+
@functools.wraps(fn)
|
|
21
21
|
def result(*args, **kwargs):
|
|
22
22
|
wrapped_fn = functools.partial(fn, **kwargs)
|
|
23
23
|
|
|
@@ -41,22 +41,24 @@ def batched_vmap(
|
|
|
41
41
|
if batch_size <= max_batch_size:
|
|
42
42
|
return jax.vmap(wrapped_fn, in_axes=in_axes, out_axes=out_axes)(*args)
|
|
43
43
|
|
|
44
|
-
if batch_size % max_batch_size != 0:
|
|
45
|
-
logging.warning(f"Batch size {batch_size} is not divisible by max_batch_size {max_batch_size}. Switching to vmap.")
|
|
46
|
-
return jax.vmap(wrapped_fn, in_axes=in_axes, out_axes=out_axes)(*args)
|
|
47
|
-
|
|
48
44
|
# Split into batches.
|
|
49
|
-
assert batch_size % max_batch_size == 0
|
|
50
45
|
num_batches = batch_size // max_batch_size
|
|
46
|
+
remainder = batch_size % max_batch_size
|
|
51
47
|
leading_args = [
|
|
52
|
-
jtu.tree_map(lambda x: jnp.moveaxis(x, ax, 0)
|
|
48
|
+
jtu.tree_map(lambda x: jnp.moveaxis(x, ax, 0), arg)
|
|
53
49
|
for arg, ax in zip(mapped_args, mapped_axes)
|
|
54
50
|
]
|
|
51
|
+
loop_args = jtu.tree_map(lambda x: x[remainder:].reshape(num_batches, max_batch_size, *x.shape[1:]), leading_args)
|
|
52
|
+
|
|
53
|
+
vmapped_fn = jax.vmap(lambda x: wrapped_fn(*in_tree.unflatten(merge(x))), in_axes=0, out_axes=0)
|
|
55
54
|
def inner(carry, x):
|
|
56
|
-
vmapped_fn = jax.vmap(lambda x: wrapped_fn(*in_tree.unflatten(merge(x))), in_axes=0, out_axes=0)
|
|
57
55
|
return carry, vmapped_fn(x)
|
|
58
|
-
result = jax.lax.scan(inner, None,
|
|
56
|
+
result = jax.lax.scan(inner, None, loop_args, length=num_batches)[1]
|
|
59
57
|
result = jtu.tree_map(lambda x: x.reshape(-1, *x.shape[2:]), result)
|
|
58
|
+
if remainder > 0:
|
|
59
|
+
remainder_args = jtu.tree_map(lambda x: x[:remainder], leading_args)
|
|
60
|
+
remainder_result = vmapped_fn(remainder_args)
|
|
61
|
+
result = jtu.tree_map(lambda x, r: jnp.concatenate([r, x], axis=0), result, remainder_result)
|
|
60
62
|
|
|
61
63
|
out_axes_flat, out_tree = jtu.tree_flatten(out_axes)
|
|
62
64
|
result = tuple(
|
|
@@ -64,4 +66,4 @@ def batched_vmap(
|
|
|
64
66
|
for r, ax in zip(out_tree.flatten_up_to(result), out_axes_flat)
|
|
65
67
|
)
|
|
66
68
|
return out_tree.unflatten(result)
|
|
67
|
-
return
|
|
69
|
+
return result
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|