Skip to content

Commit 070546e

Browse files
authored
[TVMJS] Check DataType.NUMPY2STR when saving array (#17174)
Prior to this commit, the `dtype` string used by `tvmjs.dump_ndarray_cache` was generated as `str(np_array.dtype)`. While this works in most cases, there are a few naming differences between TVM datatypes and numpy datatypes, such as `"float8_e4m3fn"` in Numpy being equivalent to `"e4m3_float8"` in TVM. This commit updates `dump_ndarray_cache` to check `DataType.NUMPY2STR` for the datatype string, allowing round-trip save/load of float8 arrays.
1 parent d006eca commit 070546e

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

python/tvm/contrib/tvmjs.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import tvm
3737
from tvm._ffi.libinfo import find_lib_path
38+
from tvm.runtime import DataType
3839

3940
from .emcc import create_tvmjs_wasm
4041

@@ -276,7 +277,13 @@ def dump_ndarray_cache(
276277
v = v.numpy()
277278

278279
# prefer to preserve original dtype, especially if the format was bfloat16
279-
dtype = str(origin_v.dtype) if isinstance(origin_v, tvm.nd.NDArray) else str(v.dtype)
280+
dtype = origin_v.dtype if isinstance(origin_v, tvm.nd.NDArray) else v.dtype
281+
282+
if dtype in DataType.NUMPY2STR:
283+
dtype = DataType.NUMPY2STR[dtype]
284+
else:
285+
dtype = str(dtype)
286+
280287
total_bytes += math.prod(v.shape) * np.dtype(v.dtype).itemsize
281288

282289
# convert fp32 to bf16

tests/python/contrib/test_tvmjs.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Test contrib.tvmjs"""
19+
20+
import tempfile
21+
22+
import numpy as np
23+
import pytest
24+
25+
import tvm.testing
26+
from tvm.contrib import tvmjs
27+
28+
dtype = tvm.testing.parameter(
29+
"int8",
30+
"int16",
31+
"int32",
32+
"int64",
33+
"uint8",
34+
"uint16",
35+
"uint32",
36+
"uint64",
37+
"float16",
38+
"float32",
39+
"float64",
40+
"float8_e4m3fn",
41+
"float8_e5m2",
42+
)
43+
44+
45+
def test_save_load_float8(dtype):
46+
if "float8" in dtype or "bfloat16" in dtype:
47+
ml_dtypes = pytest.importorskip("ml_dtypes")
48+
np_dtype = np.dtype(getattr(ml_dtypes, dtype))
49+
else:
50+
np_dtype = np.dtype(dtype)
51+
52+
arr = np.arange(16, dtype=np_dtype)
53+
54+
with tempfile.TemporaryDirectory(prefix="tvm_") as temp_dir:
55+
tvmjs.dump_ndarray_cache({"arr": arr}, temp_dir)
56+
cache, _ = tvmjs.load_ndarray_cache(temp_dir, tvm.cpu())
57+
58+
after_roundtrip = cache["arr"].numpy()
59+
60+
np.testing.assert_array_equal(arr, after_roundtrip)
61+
62+
63+
if __name__ == "__main__":
64+
tvm.testing.main()

0 commit comments

Comments
 (0)