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(jax): export call_lower to SavedModel via jax2tf #4254

Draft
wants to merge 1 commit into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion deepmd/backend/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class JAXBackend(Backend):
# | Backend.Feature.NEIGHBOR_STAT
)
"""The features of the backend."""
suffixes: ClassVar[list[str]] = [".jax"]
suffixes: ClassVar[list[str]] = [".jax", ".savedmodel"]
"""The suffixes of the backend."""

def is_available(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion deepmd/dpmodel/descriptor/se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def call(
coord_ext, atype_ext, nlist, self.davg, self.dstd
)
nf, nloc, nnei, _ = rr.shape
sec = xp.asarray(self.sel_cumsum)
sec = self.sel_cumsum

ng = self.neuron[-1]
gr = xp.zeros([nf * nloc, ng, 4], dtype=self.dstd.dtype)
Expand Down
39 changes: 39 additions & 0 deletions deepmd/jax/utils/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@
model_def_script=ocp.args.JsonSave(model_def_script),
),
)
elif model_file.endswith(".savedmodel"):
import tensorflow as tf
from jax.experimental import (

Check warning on line 44 in deepmd/jax/utils/serialization.py

View check run for this annotation

Codecov / codecov/patch

deepmd/jax/utils/serialization.py#L42-L44

Added lines #L42 - L44 were not covered by tests
jax2tf,
)
njzjz marked this conversation as resolved.
Show resolved Hide resolved

model = BaseModel.deserialize(data["model"])
model_def_script = data["model_def_script"]
call_lower = model.call_lower

Check warning on line 50 in deepmd/jax/utils/serialization.py

View check run for this annotation

Codecov / codecov/patch

deepmd/jax/utils/serialization.py#L48-L50

Added lines #L48 - L50 were not covered by tests

my_model = tf.Module()

Check warning on line 52 in deepmd/jax/utils/serialization.py

View check run for this annotation

Codecov / codecov/patch

deepmd/jax/utils/serialization.py#L52

Added line #L52 was not covered by tests

# Save a function that can take scalar inputs.
my_model.call_lower = tf.function(

Check warning on line 55 in deepmd/jax/utils/serialization.py

View check run for this annotation

Codecov / codecov/patch

deepmd/jax/utils/serialization.py#L55

Added line #L55 was not covered by tests
jax2tf.convert(
call_lower,
polymorphic_shapes=[
"(nf, nloc + nghost, 3)",
"(nf, nloc + nghost)",
f"(nf, nloc, {model.get_nnei()})",
"(nf, np)",
"(nf, na)",
],
),
autograph=False,
input_signature=[
tf.TensorSpec([None, None, 3], tf.float64),
tf.TensorSpec([None, None], tf.int64),
tf.TensorSpec([None, None, model.get_nnei()], tf.int64),
tf.TensorSpec([None, None], tf.float64),
tf.TensorSpec([None, None], tf.float64),
],
)
my_model.model_def_script = model_def_script
tf.saved_model.save(

Check warning on line 76 in deepmd/jax/utils/serialization.py

View check run for this annotation

Codecov / codecov/patch

deepmd/jax/utils/serialization.py#L75-L76

Added lines #L75 - L76 were not covered by tests
my_model,
model_file,
options=tf.saved_model.SaveOptions(experimental_custom_gradients=True),
)
else:
raise ValueError("JAX backend only supports converting .jax directory")

Expand Down
Loading