- Notifications
You must be signed in to change notification settings - Fork0
Better handling of float 8 in onnx_simple_text_plot#27
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 4 additions & 1 deletionCHANGELOGS.rst
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
44 changes: 44 additions & 0 deletions_unittests/ut_plotting/test_text_plot.py
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
56 changes: 56 additions & 0 deletions_unittests/ut_reference/test_array_tensor.py
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,56 @@ | ||
| import unittest | ||
| import numpy as np | ||
| from onnx import TensorProto | ||
| from onnx.helper import make_graph, make_model, make_node, make_tensor_value_info | ||
| from onnx_array_api.ext_test_case import ExtTestCase | ||
| from onnx_array_api.reference import ( | ||
| to_array_extended, | ||
| from_array_extended, | ||
| ExtendedReferenceEvaluator, | ||
| ) | ||
| class TestArrayTensor(ExtTestCase): | ||
| def test_from_array(self): | ||
| for dt in (np.float32, np.float16, np.uint16, np.uint8): | ||
| with self.subTest(dtype=dt): | ||
| a = np.array([0, 1, 2], dtype=dt) | ||
| t = from_array_extended(a, "a") | ||
| b = to_array_extended(t) | ||
| self.assertEqualArray(a, b) | ||
| t2 = from_array_extended(b, "a") | ||
| self.assertEqual(t.SerializeToString(), t2.SerializeToString()) | ||
| def test_from_array_f8(self): | ||
| def make_model_f8(fr, to): | ||
| model = make_model( | ||
| make_graph( | ||
| [make_node("Cast", ["X"], ["Y"], to=to)], | ||
| "cast", | ||
| [make_tensor_value_info("X", fr, None)], | ||
| [make_tensor_value_info("Y", to, None)], | ||
| ) | ||
| ) | ||
| return model | ||
| for dt in (np.float32, np.float16, np.uint16, np.uint8): | ||
| with self.subTest(dtype=dt): | ||
| a = np.array([0, 1, 2], dtype=dt) | ||
| b = from_array_extended(a, "a") | ||
| for to in [ | ||
| TensorProto.FLOAT8E4M3FN, | ||
| TensorProto.FLOAT8E4M3FNUZ, | ||
| TensorProto.FLOAT8E5M2, | ||
| TensorProto.FLOAT8E5M2FNUZ, | ||
| TensorProto.BFLOAT16, | ||
| ]: | ||
| with self.subTest(fr=b.data_type, to=to): | ||
| model = make_model_f8(b.data_type, to) | ||
| ref = ExtendedReferenceEvaluator(model) | ||
| got = ref.run(None, {"X": a})[0] | ||
| back = from_array_extended(got, "a") | ||
| self.assertEqual(to, back.data_type) | ||
| if __name__ == "__main__": | ||
| unittest.main(verbosity=2) |
2 changes: 1 addition & 1 deletiononnx_array_api/npx/npx_functions.py
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
2 changes: 1 addition & 1 deletiononnx_array_api/npx/npx_graph_builder.py
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
2 changes: 1 addition & 1 deletiononnx_array_api/npx/npx_helper.py
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
19 changes: 16 additions & 3 deletionsonnx_array_api/plotting/_helper.py
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
2 changes: 1 addition & 1 deletiononnx_array_api/plotting/dot_plot.py
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
4 changes: 1 addition & 3 deletionsonnx_array_api/plotting/text_plot.py
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
44 changes: 44 additions & 0 deletionsonnx_array_api/reference/__init__.py
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 |
|---|---|---|
| @@ -1 +1,45 @@ | ||
| from typing import Optional | ||
| import numpy as np | ||
| from onnx import TensorProto | ||
| from onnx.numpy_helper import from_array as onnx_from_array | ||
| from onnx.reference.ops.op_cast import ( | ||
| bfloat16, | ||
| float8e4m3fn, | ||
| float8e4m3fnuz, | ||
| float8e5m2, | ||
| float8e5m2fnuz, | ||
| ) | ||
| from onnx.reference.op_run import to_array_extended | ||
| from .evaluator import ExtendedReferenceEvaluator | ||
| def from_array_extended(tensor: np.array, name: Optional[str] = None) -> TensorProto: | ||
| """ | ||
| Converts an array into a TensorProto. | ||
| :param tensor: numpy array | ||
| :param name: name | ||
| :return: TensorProto | ||
| """ | ||
| dt = tensor.dtype | ||
| if dt == float8e4m3fn and dt.descr[0][0] == "e4m3fn": | ||
| to = TensorProto.FLOAT8E4M3FN | ||
| dt_to = np.uint8 | ||
| elif dt == float8e4m3fnuz and dt.descr[0][0] == "e4m3fnuz": | ||
| to = TensorProto.FLOAT8E4M3FNUZ | ||
| dt_to = np.uint8 | ||
| elif dt == float8e5m2 and dt.descr[0][0] == "e5m2": | ||
| to = TensorProto.FLOAT8E5M2 | ||
| dt_to = np.uint8 | ||
| elif dt == float8e5m2fnuz and dt.descr[0][0] == "e5m2fnuz": | ||
| to = TensorProto.FLOAT8E5M2FNUZ | ||
| dt_to = np.uint8 | ||
| elif dt == bfloat16 and dt.descr[0][0] == "bfloat16": | ||
| to = TensorProto.BFLOAT16 | ||
| dt_to = np.uint16 | ||
| else: | ||
| return onnx_from_array(tensor, name) | ||
| t = onnx_from_array(tensor.astype(dt_to), name) | ||
| t.data_type = to | ||
| return t |
2 changes: 1 addition & 1 deletiononnx_array_api/validation/tools.py
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.