|
| 1 | +# SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +import json |
| 3 | + |
| 4 | +import tensorflow as tf |
| 5 | +from jax.experimental import ( |
| 6 | + jax2tf, |
| 7 | +) |
| 8 | + |
| 9 | +from deepmd.jax.model.base_model import ( |
| 10 | + BaseModel, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def deserialize_to_file(model_file: str, data: dict) -> None: |
| 15 | + """Deserialize the dictionary to a model file. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + model_file : str |
| 20 | + The model file to be saved. |
| 21 | + data : dict |
| 22 | + The dictionary to be deserialized. |
| 23 | + """ |
| 24 | + if model_file.endswith(".savedmodel"): |
| 25 | + model = BaseModel.deserialize(data["model"]) |
| 26 | + model_def_script = data["model_def_script"] |
| 27 | + call_lower = model.call_lower |
| 28 | + |
| 29 | + tf_model = tf.Module() |
| 30 | + |
| 31 | + def exported_whether_do_atomic_virial(do_atomic_virial): |
| 32 | + def call_lower_with_fixed_do_atomic_virial( |
| 33 | + coord, atype, nlist, mapping, fparam, aparam |
| 34 | + ): |
| 35 | + return call_lower( |
| 36 | + coord, |
| 37 | + atype, |
| 38 | + nlist, |
| 39 | + mapping, |
| 40 | + fparam, |
| 41 | + aparam, |
| 42 | + do_atomic_virial=do_atomic_virial, |
| 43 | + ) |
| 44 | + |
| 45 | + return jax2tf.convert( |
| 46 | + call_lower_with_fixed_do_atomic_virial, |
| 47 | + polymorphic_shapes=[ |
| 48 | + "(nf, nloc + nghost, 3)", |
| 49 | + "(nf, nloc + nghost)", |
| 50 | + f"(nf, nloc, {model.get_nnei()})", |
| 51 | + "(nf, nloc + nghost)", |
| 52 | + f"(nf, {model.get_dim_fparam()})", |
| 53 | + f"(nf, nloc, {model.get_dim_aparam()})", |
| 54 | + ], |
| 55 | + with_gradient=True, |
| 56 | + ) |
| 57 | + |
| 58 | + # Save a function that can take scalar inputs. |
| 59 | + # We need to explicit set the function name, so C++ can find it. |
| 60 | + @tf.function( |
| 61 | + autograph=False, |
| 62 | + input_signature=[ |
| 63 | + tf.TensorSpec([None, None, 3], tf.float64), |
| 64 | + tf.TensorSpec([None, None], tf.int32), |
| 65 | + tf.TensorSpec([None, None, model.get_nnei()], tf.int64), |
| 66 | + tf.TensorSpec([None, None], tf.int64), |
| 67 | + tf.TensorSpec([None, model.get_dim_fparam()], tf.float64), |
| 68 | + tf.TensorSpec([None, None, model.get_dim_aparam()], tf.float64), |
| 69 | + ], |
| 70 | + ) |
| 71 | + def call_lower_without_atomic_virial( |
| 72 | + coord, atype, nlist, mapping, fparam, aparam |
| 73 | + ): |
| 74 | + return exported_whether_do_atomic_virial(do_atomic_virial=False)( |
| 75 | + coord, atype, nlist, mapping, fparam, aparam |
| 76 | + ) |
| 77 | + |
| 78 | + tf_model.call_lower = call_lower_without_atomic_virial |
| 79 | + |
| 80 | + @tf.function( |
| 81 | + autograph=False, |
| 82 | + input_signature=[ |
| 83 | + tf.TensorSpec([None, None, 3], tf.float64), |
| 84 | + tf.TensorSpec([None, None], tf.int32), |
| 85 | + tf.TensorSpec([None, None, model.get_nnei()], tf.int64), |
| 86 | + tf.TensorSpec([None, None], tf.int64), |
| 87 | + tf.TensorSpec([None, model.get_dim_fparam()], tf.float64), |
| 88 | + tf.TensorSpec([None, None, model.get_dim_aparam()], tf.float64), |
| 89 | + ], |
| 90 | + ) |
| 91 | + def call_lower_with_atomic_virial(coord, atype, nlist, mapping, fparam, aparam): |
| 92 | + return exported_whether_do_atomic_virial(do_atomic_virial=True)( |
| 93 | + coord, atype, nlist, mapping, fparam, aparam |
| 94 | + ) |
| 95 | + |
| 96 | + tf_model.call_lower_atomic_virial = call_lower_with_atomic_virial |
| 97 | + |
| 98 | + # set functions to export other attributes |
| 99 | + @tf.function |
| 100 | + def get_type_map(): |
| 101 | + return tf.constant(model.get_type_map(), dtype=tf.string) |
| 102 | + |
| 103 | + tf_model.get_type_map = get_type_map |
| 104 | + |
| 105 | + @tf.function |
| 106 | + def get_rcut(): |
| 107 | + return tf.constant(model.get_rcut(), dtype=tf.double) |
| 108 | + |
| 109 | + tf_model.get_rcut = get_rcut |
| 110 | + |
| 111 | + @tf.function |
| 112 | + def get_dim_fparam(): |
| 113 | + return tf.constant(model.get_dim_fparam(), dtype=tf.int64) |
| 114 | + |
| 115 | + tf_model.get_dim_fparam = get_dim_fparam |
| 116 | + |
| 117 | + @tf.function |
| 118 | + def get_dim_aparam(): |
| 119 | + return tf.constant(model.get_dim_aparam(), dtype=tf.int64) |
| 120 | + |
| 121 | + tf_model.get_dim_aparam = get_dim_aparam |
| 122 | + |
| 123 | + @tf.function |
| 124 | + def get_sel_type(): |
| 125 | + return tf.constant(model.get_sel_type(), dtype=tf.int64) |
| 126 | + |
| 127 | + tf_model.get_sel_type = get_sel_type |
| 128 | + |
| 129 | + @tf.function |
| 130 | + def is_aparam_nall(): |
| 131 | + return tf.constant(model.is_aparam_nall(), dtype=tf.bool) |
| 132 | + |
| 133 | + tf_model.is_aparam_nall = is_aparam_nall |
| 134 | + |
| 135 | + @tf.function |
| 136 | + def model_output_type(): |
| 137 | + return tf.constant(model.model_output_type(), dtype=tf.string) |
| 138 | + |
| 139 | + tf_model.model_output_type = model_output_type |
| 140 | + |
| 141 | + @tf.function |
| 142 | + def mixed_types(): |
| 143 | + return tf.constant(model.mixed_types(), dtype=tf.bool) |
| 144 | + |
| 145 | + tf_model.mixed_types = mixed_types |
| 146 | + |
| 147 | + if model.get_min_nbor_dist() is not None: |
| 148 | + |
| 149 | + @tf.function |
| 150 | + def get_min_nbor_dist(): |
| 151 | + return tf.constant(model.get_min_nbor_dist(), dtype=tf.double) |
| 152 | + |
| 153 | + tf_model.get_min_nbor_dist = get_min_nbor_dist |
| 154 | + |
| 155 | + @tf.function |
| 156 | + def get_sel(): |
| 157 | + return tf.constant(model.get_sel(), dtype=tf.int64) |
| 158 | + |
| 159 | + tf_model.get_sel = get_sel |
| 160 | + |
| 161 | + @tf.function |
| 162 | + def get_model_def_script(): |
| 163 | + return tf.constant( |
| 164 | + json.dumps(model_def_script, separators=(",", ":")), dtype=tf.string |
| 165 | + ) |
| 166 | + |
| 167 | + tf_model.get_model_def_script = get_model_def_script |
| 168 | + tf.saved_model.save( |
| 169 | + tf_model, |
| 170 | + model_file, |
| 171 | + options=tf.saved_model.SaveOptions(experimental_custom_gradients=True), |
| 172 | + ) |
0 commit comments