-
Notifications
You must be signed in to change notification settings - Fork 582
feat(jax): checkpoint I/O #4236
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f6b30bf
feat(jax): energy model (no grad support)
njzjz 3e13a33
address comments
njzjz fa9a000
export EnergyModel
njzjz d3b5ce3
jax2tf
njzjz 91ec084
apply flax_module to energy model
njzjz 14fab30
checkpoint
njzjz 390ace5
fix atomic model
njzjz a4cd627
apply flax_module
njzjz 71a4b55
checkpoint
njzjz 5024f70
done
njzjz f0bc8b8
add dependencies
njzjz 2a177df
Merge branch 'devel' into save-jax
njzjz 8c25e87
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7e003b2
Remove jax2tf
njzjz 536bbcd
Update pyproject.toml
njzjz f90fc52
update the model with the state
njzjz 9b571d1
fix is_available
njzjz fb3df8b
bugfix
njzjz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # SPDX-License-Identifier: LGPL-3.0-or-later | ||
| from pathlib import ( | ||
| Path, | ||
| ) | ||
|
|
||
| import orbax.checkpoint as ocp | ||
|
|
||
| from deepmd.jax.env import ( | ||
| jax, | ||
| nnx, | ||
| ) | ||
| from deepmd.jax.model.model import ( | ||
| BaseModel, | ||
| get_model, | ||
| ) | ||
|
|
||
|
|
||
| def deserialize_to_file(model_file: str, data: dict) -> None: | ||
| """Deserialize the dictionary to a model file. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model_file : str | ||
| The model file to be saved. | ||
| data : dict | ||
| The dictionary to be deserialized. | ||
| """ | ||
| if model_file.endswith(".jax"): | ||
| model = BaseModel.deserialize(data["model"]) | ||
| model_def_script = data["model_def_script"] | ||
| _, state = nnx.split(model) | ||
| with ocp.Checkpointer( | ||
| ocp.CompositeCheckpointHandler("state", "model_def_script") | ||
| ) as checkpointer: | ||
| checkpointer.save( | ||
| Path(model_file).absolute(), | ||
| ocp.args.Composite( | ||
| state=ocp.args.StandardSave(state.to_pure_dict()), | ||
| model_def_script=ocp.args.JsonSave(model_def_script), | ||
| ), | ||
| ) | ||
| else: | ||
| raise ValueError("JAX backend only supports converting .jax directory") | ||
|
|
||
|
|
||
| def serialize_from_file(model_file: str) -> dict: | ||
| """Serialize the model file to a dictionary. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| model_file : str | ||
| The model file to be serialized. | ||
|
|
||
| Returns | ||
| ------- | ||
| dict | ||
| The serialized model data. | ||
| """ | ||
| if model_file.endswith(".jax"): | ||
| with ocp.Checkpointer( | ||
| ocp.CompositeCheckpointHandler("state", "model_def_script") | ||
| ) as checkpointer: | ||
| data = checkpointer.restore( | ||
| Path(model_file).absolute(), | ||
| ocp.args.Composite( | ||
| state=ocp.args.StandardRestore(), | ||
| model_def_script=ocp.args.JsonRestore(), | ||
| ), | ||
| ) | ||
| state = data.state | ||
njzjz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # convert str "1" to int 1 key | ||
| def convert_str_to_int_key(item: dict): | ||
| for key, value in item.copy().items(): | ||
| if isinstance(value, dict): | ||
| convert_str_to_int_key(value) | ||
| if key.isdigit(): | ||
| item[int(key)] = item.pop(key) | ||
|
|
||
| convert_str_to_int_key(state) | ||
|
|
||
| model_def_script = data.model_def_script | ||
| abstract_model = get_model(model_def_script) | ||
| graphdef, abstract_state = nnx.split(abstract_model) | ||
| abstract_state.replace_by_pure_dict(state) | ||
| model = nnx.merge(graphdef, abstract_state) | ||
| model_dict = model.serialize() | ||
| data = { | ||
| "backend": "JAX", | ||
| "jax_version": jax.__version__, | ||
| "model": model_dict, | ||
| "model_def_script": model_def_script, | ||
| "@variables": {}, | ||
| } | ||
| return data | ||
| else: | ||
| raise ValueError("JAX backend only supports converting .jax directory") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.