|
1 | 1 | import unittest |
| 2 | +import numpy as np |
2 | 3 | import ml_dtypes |
3 | 4 | import onnx |
4 | 5 | import torch |
5 | 6 | import transformers |
6 | 7 | from onnx_diagnostic.ext_test_case import ExtTestCase, hide_stdout |
7 | 8 | from onnx_diagnostic.helpers import max_diff, string_type |
8 | | -from onnx_diagnostic.helpers.torch_test_helper import ( |
| 9 | +from onnx_diagnostic.helpers.torch_helper import ( |
9 | 10 | dummy_llm, |
10 | 11 | to_numpy, |
11 | 12 | is_torchdynamo_exporting, |
|
24 | 25 | make_sliding_window_cache, |
25 | 26 | ) |
26 | 27 | from onnx_diagnostic.helpers.mini_onnx_builder import create_input_tensors_from_onnx_model |
| 28 | +from onnx_diagnostic.helpers.onnx_helper import from_array_extended, to_array_extended |
| 29 | +from onnx_diagnostic.helpers.torch_helper import to_tensor |
27 | 30 |
|
28 | 31 | TFLOAT = onnx.TensorProto.FLOAT |
29 | 32 |
|
@@ -205,7 +208,7 @@ def forward(self, x, y): |
205 | 208 | else: |
206 | 209 | print("output", k, v) |
207 | 210 | print(string_type(restored, with_shape=True)) |
208 | | - l1, l2 = 183, 192 |
| 211 | + l1, l2 = 186, 195 |
209 | 212 | self.assertEqual( |
210 | 213 | [ |
211 | 214 | (f"-Model-{l2}", 0, "I"), |
@@ -344,6 +347,35 @@ def forward(self, x, y=None): |
344 | 347 | stat, |
345 | 348 | ) |
346 | 349 |
|
| 350 | + def test_to_tensor(self): |
| 351 | + for dtype in [ |
| 352 | + np.int8, |
| 353 | + np.uint8, |
| 354 | + np.int16, |
| 355 | + np.uint16, |
| 356 | + np.int32, |
| 357 | + np.uint32, |
| 358 | + np.int64, |
| 359 | + np.uint64, |
| 360 | + np.float16, |
| 361 | + np.float32, |
| 362 | + np.float64, |
| 363 | + ]: |
| 364 | + with self.subTest(dtype=dtype): |
| 365 | + a = np.random.rand(4, 5).astype(dtype) |
| 366 | + proto = from_array_extended(a) |
| 367 | + b = to_array_extended(proto) |
| 368 | + self.assertEqualArray(a, b) |
| 369 | + c = to_tensor(proto) |
| 370 | + self.assertEqualArray(a, c) |
| 371 | + |
| 372 | + for dtype in [torch.bfloat16]: |
| 373 | + with self.subTest(dtype=dtype): |
| 374 | + a = torch.rand((4, 5), dtype=dtype) |
| 375 | + proto = from_array_extended(a) |
| 376 | + c = to_tensor(proto) |
| 377 | + self.assertEqualArray(a, c) |
| 378 | + |
347 | 379 |
|
348 | 380 | if __name__ == "__main__": |
349 | 381 | unittest.main(verbosity=2) |
0 commit comments