9 Dump json and npy files to output_dir
12 if not os.path.exists(output_dir):
13 os.makedirs(output_dir)
17 _index_to_value = dict()
20 for tensor_name, qparam
in json_content.items():
21 assert type(tensor_name) == str
22 assert type(qparam) == dict
23 for field, value
in qparam.items():
24 if isinstance(value, np.ndarray):
25 npy_name = str(_index) +
'.npy'
28 np.save(os.path.join(output_dir, npy_name), value)
31 json_content[tensor_name][field] = npy_name
34 _index_to_value[_index] = tensor_name +
"_" + field
38 with open(os.path.join(output_dir,
'qparam.json'),
'w')
as f:
39 json.dump(json_content, f, indent=2)
54def gen_random_tensor(dtype_str: str,
55 scale_shape: typing.Tuple[int],
56 zerop_shape: typing.Tuple[int],
57 quantized_dimension: int,
58 value_shape: typing.Optional[typing.Tuple[int]] =
None) -> dict:
60 content[
'dtype'] = dtype_str
61 content[
'scale'] = np.random.rand(scale_shape).astype(np.float32)
63 content[
'zerop'] = np.random.randint(256, size=zerop_shape, dtype=np.int64)
64 content[
'quantized_dimension'] = quantized_dimension
66 if value_shape !=
None:
68 content[
'value'] = np.random.randint(256, size=value_shape, dtype=dtype)