imt-ring 1.6.24__py3-none-any.whl → 1.6.26__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- {imt_ring-1.6.24.dist-info → imt_ring-1.6.26.dist-info}/METADATA +1 -1
- {imt_ring-1.6.24.dist-info → imt_ring-1.6.26.dist-info}/RECORD +6 -6
- ring/ml/ml_utils.py +97 -0
- ring/ml/train.py +2 -2
- {imt_ring-1.6.24.dist-info → imt_ring-1.6.26.dist-info}/WHEEL +0 -0
- {imt_ring-1.6.24.dist-info → imt_ring-1.6.26.dist-info}/top_level.txt +0 -0
@@ -54,11 +54,11 @@ ring/io/xml/to_xml.py,sha256=fohb-jWMf2cxVdT5dmknsGyrNMseICSbKEz_urbaWbQ,3407
|
|
54
54
|
ring/ml/__init__.py,sha256=nbh48gaswWeY4S4vT1sply_3ROj2DQ7agjoLR4Ho3T8,1517
|
55
55
|
ring/ml/base.py,sha256=lfwEZLBDglOSRWChUHoH1kezefhttPV9TMEpNIqsMNw,9972
|
56
56
|
ring/ml/callbacks.py,sha256=W19QF6_uvaNCjs8ObsjNXD7mv9gFgJBixdRSbB_BynE,13301
|
57
|
-
ring/ml/ml_utils.py,sha256=
|
57
|
+
ring/ml/ml_utils.py,sha256=Zm4spN0Xn-2avYu9xt3NikCLVjYM1Gh59a6XU9jGxoU,10792
|
58
58
|
ring/ml/optimizer.py,sha256=TZF0_LmnewzmGVso-zIQJtpWguUW0fW3HeRpIdG_qoI,4763
|
59
59
|
ring/ml/ringnet.py,sha256=mef7jyN2QcApJmQGH3HYZyTV-00q8YpsYOKhW0-ku1k,8973
|
60
60
|
ring/ml/rnno_v1.py,sha256=2qE08OIvTJ5PvSxKpYGzGSrvEImWrdAT_qslZ7jP5tA,1372
|
61
|
-
ring/ml/train.py,sha256
|
61
|
+
ring/ml/train.py,sha256=-6SzQKjIgktgRjaXKVg_1dqcBmAJggZSVwDnau1FnxI,10832
|
62
62
|
ring/ml/training_loop.py,sha256=CEokvPQuuk_WCd-J60ZDodJYcPVvyxLfgXDr_DnbzRI,3359
|
63
63
|
ring/ml/params/0x13e3518065c21cd8.pickle,sha256=Zh2k1zK-TNxJl5F7nyTeQ9001qqRE_dfvaq1HWV287A,9355838
|
64
64
|
ring/ml/params/0x1d76628065a71e0f.pickle,sha256=YTNVuvfw-nCRD9BH1PZYcR9uCFpNWDhw8Lc50eDn_EE,9351038
|
@@ -86,7 +86,7 @@ ring/utils/randomize_sys.py,sha256=G_vBIo0OwQkXL2u0djwbaoaeb02C4LQCTNNloOYIU2M,3
|
|
86
86
|
ring/utils/utils.py,sha256=tJaWXLGOTwkxJQj2l23dX97wO3aZYhM2qd7eNuMRs84,6907
|
87
87
|
ring/utils/register_gym_envs/__init__.py,sha256=PtPIRBQJ16339xZ9G9VpvqrvcGbQ_Pk_SUz4tQPa9nQ,94
|
88
88
|
ring/utils/register_gym_envs/saddle.py,sha256=tA5CyW_akSXyDm0xJ83CtOrUMVElH0f9vZtEDDJQalI,4422
|
89
|
-
imt_ring-1.6.
|
90
|
-
imt_ring-1.6.
|
91
|
-
imt_ring-1.6.
|
92
|
-
imt_ring-1.6.
|
89
|
+
imt_ring-1.6.26.dist-info/METADATA,sha256=ibexS27KESraTLpx1W9rTIHvHS-j92qDOz-BaTOMOds,4089
|
90
|
+
imt_ring-1.6.26.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
91
|
+
imt_ring-1.6.26.dist-info/top_level.txt,sha256=EiT790-lAyi8iwTzJArH3f2k77rwhDn00q-4PlmvDQo,5
|
92
|
+
imt_ring-1.6.26.dist-info/RECORD,,
|
ring/ml/ml_utils.py
CHANGED
@@ -243,5 +243,102 @@ def save_model_tf(jax_func, path: str, *input, validate: bool = True):
|
|
243
243
|
)
|
244
244
|
|
245
245
|
|
246
|
+
def to_onnx(
|
247
|
+
fn,
|
248
|
+
output_path,
|
249
|
+
*args: tuple[np.ndarray],
|
250
|
+
in_args_names: Optional[list[str]] = None,
|
251
|
+
out_args_names: Optional[list[str]] = None,
|
252
|
+
validate: bool = False,
|
253
|
+
):
|
254
|
+
"""
|
255
|
+
Converts a JAX function to ONNX format, with optional input/output renaming and validation.
|
256
|
+
|
257
|
+
Args:
|
258
|
+
fn (callable): The JAX function to be converted.
|
259
|
+
output_path (str): Path where the ONNX model will be saved.
|
260
|
+
*args (tuple[np.ndarray]): Input arguments for the JAX function.
|
261
|
+
in_args_names (Optional[list[str]]): Names for the ONNX model's input tensors. Defaults to None.
|
262
|
+
out_args_names (Optional[list[str]]): Names for the ONNX model's output tensors. Defaults to None.
|
263
|
+
validate (bool): Whether to validate the ONNX model against the JAX function's outputs. Defaults to False.
|
264
|
+
|
265
|
+
Raises:
|
266
|
+
AssertionError: If the number of provided names does not match the number of inputs/outputs.
|
267
|
+
AssertionError: If the ONNX model's outputs do not match the JAX function's outputs within tolerance.
|
268
|
+
ValueError: If any error occurs during ONNX conversion, saving, or validation.
|
269
|
+
|
270
|
+
Notes:
|
271
|
+
- The function uses `jax2tf` to convert the JAX function to TensorFlow format,
|
272
|
+
and `tf2onnx` for ONNX conversion.
|
273
|
+
- Input and output tensor names in the ONNX model can be renamed using `sor4onnx.rename`.
|
274
|
+
- Validation compares outputs of the JAX function and the ONNX model using ONNX Runtime.
|
275
|
+
|
276
|
+
Example:
|
277
|
+
```
|
278
|
+
import jax.numpy as jnp
|
279
|
+
|
280
|
+
def my_fn(x, y):
|
281
|
+
return x + y, x * y
|
282
|
+
|
283
|
+
x = jnp.array([1, 2, 3])
|
284
|
+
y = jnp.array([4, 5, 6])
|
285
|
+
|
286
|
+
to_onnx(
|
287
|
+
my_fn,
|
288
|
+
"model.onnx",
|
289
|
+
x, y,
|
290
|
+
in_args_names=["input1", "input2"],
|
291
|
+
out_args_names=["sum", "product"],
|
292
|
+
validate=True,
|
293
|
+
)
|
294
|
+
```
|
295
|
+
""" # noqa: E501
|
296
|
+
import jax.experimental.jax2tf as jax2tf
|
297
|
+
import tensorflow as tf
|
298
|
+
import tf2onnx
|
299
|
+
|
300
|
+
tf_fn = tf.function(jax2tf.convert(fn, enable_xla=False))
|
301
|
+
tf_args = [tf.TensorSpec(np.shape(x), np.result_type(x)) for x in args]
|
302
|
+
tf2onnx.convert.from_function(
|
303
|
+
tf_fn, input_signature=tf_args, output_path=output_path
|
304
|
+
)
|
305
|
+
|
306
|
+
if in_args_names is not None or out_args_names is not None:
|
307
|
+
import onnx
|
308
|
+
from sor4onnx import rename
|
309
|
+
|
310
|
+
model = onnx.load(output_path)
|
311
|
+
|
312
|
+
if in_args_names is not None:
|
313
|
+
old_names = [inp.name for inp in model.graph.input]
|
314
|
+
assert len(old_names) == len(in_args_names)
|
315
|
+
for old_name, new_name in zip(old_names, in_args_names):
|
316
|
+
model = rename([old_name, new_name], None, model, None, mode="inputs")
|
317
|
+
|
318
|
+
if out_args_names is not None:
|
319
|
+
old_names = [out.name for out in model.graph.output]
|
320
|
+
assert len(old_names) == len(out_args_names)
|
321
|
+
for old_name, new_name in zip(old_names, out_args_names):
|
322
|
+
model = rename([old_name, new_name], None, model, None, mode="outputs")
|
323
|
+
|
324
|
+
onnx.save(model, output_path)
|
325
|
+
|
326
|
+
if validate:
|
327
|
+
import onnxruntime as ort
|
328
|
+
|
329
|
+
output_jax = fn(*args)
|
330
|
+
session = ort.InferenceSession(output_path)
|
331
|
+
input_names = [inp.name for inp in session.get_inputs()]
|
332
|
+
output_onnx = session.run(
|
333
|
+
None, {name: np.array(arg) for name, arg in zip(input_names, args)}
|
334
|
+
)
|
335
|
+
|
336
|
+
for o1, o2 in zip(output_jax, output_onnx):
|
337
|
+
assert np.allclose(o1, o2, atol=1e-5, rtol=1e-5)
|
338
|
+
|
339
|
+
if out_args_names is not None:
|
340
|
+
assert [out.name for out in session.get_outputs()] == out_args_names
|
341
|
+
|
342
|
+
|
246
343
|
def _unknown_link_names(N: int):
|
247
344
|
return [f"link{i}" for i in range(N)]
|
ring/ml/train.py
CHANGED
@@ -45,7 +45,7 @@ def _build_step_fn(
|
|
45
45
|
|
46
46
|
@partial(jax.value_and_grad, has_aux=True)
|
47
47
|
def loss_fn(params, state, X, y):
|
48
|
-
yhat, state = filter.apply(params=params, state=state, X=X)
|
48
|
+
yhat, state = filter.apply(params=params, state=state, X=X, y=y)
|
49
49
|
# this vmap maps along batch-axis, not time-axis
|
50
50
|
# time-axis is handled by `metric_fn`
|
51
51
|
pipe = lambda q, qhat: jnp.mean(jax.vmap(metric_fn)(q, qhat))
|
@@ -261,7 +261,7 @@ def _build_eval_fn(
|
|
261
261
|
"""Build function that evaluates the filter performance."""
|
262
262
|
|
263
263
|
def eval_fn(params, state, X, y):
|
264
|
-
yhat, _ = filter.apply(params=params, state=state, X=X)
|
264
|
+
yhat, _ = filter.apply(params=params, state=state, X=X, y=y)
|
265
265
|
|
266
266
|
y = _arr_to_dict(y, link_names)
|
267
267
|
yhat = _arr_to_dict(yhat, link_names)
|
File without changes
|
File without changes
|