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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: folx
3
- Version: 0.2.2
3
+ Version: 0.2.2.post1
4
4
  Summary: Forward Laplacian for JAX
5
5
  Home-page: https://github.com/microsoft/folx
6
6
  License: MIT
@@ -1,6 +1,5 @@
1
1
  import functools
2
- import logging
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).reshape(-1, max_batch_size, *x.shape[1:]), arg)
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, leading_args, length=num_batches)[1]
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 cast(F, result)
69
+ return result
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = 'folx'
3
- version = '0.2.2'
3
+ version = '0.2.2-1'
4
4
  description = 'Forward Laplacian for JAX'
5
5
  authors = [
6
6
  "Nicholas Gao <n.gao@tum.de>",
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