-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deterministic compute tests (#4716)
Co-authored-by: Rayan Krishnan <t-rakr@OrtDevTest2v100.af05slrtruoetgaxwwjv5nsq5e.px.internal.cloudapp.net> Co-authored-by: Thiago Crepaldi <[email protected]>
- Loading branch information
1 parent
92c5ba4
commit d5f2c6e
Showing
3 changed files
with
108 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
|
||
import numpy as np | ||
import os | ||
import sys | ||
import torch | ||
|
||
from numpy.testing import assert_allclose | ||
from onnxruntime.capi.training import orttrainer | ||
|
||
def compare_onnx_weights(model_a, model_b, verbose=False, rtol=1e-4): | ||
r"""Compare whether weights between 'model_a' and 'model_b' ONNX models are within | ||
a certain tolerance 'rtol' | ||
Compares the weights of two different ONNX models and throws an error when they diverge | ||
Args: | ||
model_a, model_b (ORTTrainer): Two instances of ORTTrainer with the same model structure | ||
verbose (bool, default is False): Indicates if the max absolute difference for each layer should be | ||
calculated and printed for debug information. | ||
rtol (float, default is 1e-4): Tolerance for divergence. | ||
""" | ||
assert isinstance(model_a, orttrainer.ORTTrainer) and isinstance(model_b, orttrainer.ORTTrainer) | ||
state_dict_a, state_dict_b = model_a._training_session.get_state(), model_b._training_session.get_state() | ||
assert len(state_dict_a.items()) == len(state_dict_b.items()) | ||
for (a_name, a_val), (b_name, b_val) in zip(state_dict_a.items(), state_dict_b.items()): | ||
np_a_vals = np.array(a_val).flatten() | ||
np_b_vals = np.array(b_val).flatten() | ||
assert np_a_vals.shape == np_b_vals.shape | ||
if verbose: | ||
print(f'Weight name: {a_name}: absolute difference: {np.abs(np_a_vals-np_b_vals).max()}') | ||
assert_allclose(a_val, b_val, rtol=rtol, err_msg=f"Weight mismatch for {a_name}") | ||
|
This file contains 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 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