Skip to content

Commit

Permalink
Fix PyTree serialization for SinkhornDivergence
Browse files Browse the repository at this point in the history
  • Loading branch information
michalk8 committed Nov 7, 2023
1 parent 0a8cf0c commit 62b07e2
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/ott/tools/sinkhorn_divergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from types import MappingProxyType
from typing import Any, Mapping, NamedTuple, Optional, Tuple, Type
from typing import Any, Mapping, Optional, Tuple, Type

import jax.numpy as jnp

from ott import utils
from ott.geometry import costs, geometry, pointcloud, segment
from ott.problems.linear import linear_problem, potentials
from ott.solvers import linear
Expand All @@ -29,7 +30,8 @@
Potentials_t = Tuple[jnp.ndarray, jnp.ndarray]


class SinkhornDivergenceOutput(NamedTuple): # noqa: D101
@utils.register_pytree_node
class SinkhornDivergenceOutput: # noqa: D101
divergence: float
potentials: Tuple[Potentials_t, Potentials_t, Potentials_t]
geoms: Tuple[geometry.Geometry, geometry.Geometry, geometry.Geometry]
Expand All @@ -49,6 +51,24 @@ def to_dual_potentials(self) -> "potentials.EntropicPotentials":
f_xy, g_xy, prob_xy, f_xx=f_x, g_yy=g_y
)

def tree_flatten_foo(self): # noqa: D102
return [
self.divergence,
self.potentials,
self.geoms,
self.a,
self.b,
], {
"n_iters": self.n_iters,
"converged": self.converged,
"errors": self.errors
}

@classmethod
def tree_unflatten_foo(cls, aux_data, children): # noqa: D102
div, pots, geoms, a, b = children
return cls(div, pots, geoms, a=a, b=b, **aux_data)


def sinkhorn_divergence(
geom: Type[geometry.Geometry],
Expand Down

0 comments on commit 62b07e2

Please sign in to comment.