ONE - On-device Neural Engine
Loading...
Searching...
No Matches
test_utils Namespace Reference

Data Structures

class  TestCase
 
class  TestRunner
 

Functions

 _dump_npy_included_json (str output_dir, dict json_content)
 
 _str_to_npy_dtype (str dtype_str)
 
dict gen_random_tensor (str dtype_str, typing.Tuple[int] scale_shape, typing.Tuple[int] zerop_shape, int quantized_dimension, typing.Optional[typing.Tuple[int]] value_shape=None)
 

Function Documentation

◆ _dump_npy_included_json()

test_utils._dump_npy_included_json ( str  output_dir,
dict  json_content 
)
protected
Dump json and npy files to output_dir

Definition at line 7 of file test_utils.py.

7def _dump_npy_included_json(output_dir: str, json_content: dict):
8 """
9 Dump json and npy files to output_dir
10 """
11 # Create output_dir if not exists
12 if not os.path.exists(output_dir):
13 os.makedirs(output_dir)
14
15 # file name for npy data (ex: 0.npy, 1.npy, ...)
16 _index = 0
17 _index_to_value = dict()
18
19 # Replace npy to the path to the npy file
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'
26
27 # Save npy file
28 np.save(os.path.join(output_dir, npy_name), value)
29
30 # Replace to the path to the npy file
31 json_content[tensor_name][field] = npy_name
32
33 # Save the mapping from index to tensor name
34 _index_to_value[_index] = tensor_name + "_" + field
35 _index += 1
36
37 # Dump json
38 with open(os.path.join(output_dir, 'qparam.json'), 'w') as f:
39 json.dump(json_content, f, indent=2)
40
41

Referenced by test_utils.TestRunner.run().

◆ _str_to_npy_dtype()

test_utils._str_to_npy_dtype ( str  dtype_str)
protected

Definition at line 42 of file test_utils.py.

42def _str_to_npy_dtype(dtype_str: str):
43 if dtype_str == "uint8":
44 return np.uint8
45 if dtype_str == "int16":
46 return np.int16
47 if dtype_str == "int32":
48 return np.int32
49 if dtype_str == "int64":
50 return np.int64
51 raise SystemExit("Unsupported npy dtype", dtype_str)
52
53

Referenced by gen_random_tensor().

◆ gen_random_tensor()

dict test_utils.gen_random_tensor ( str  dtype_str,
typing.Tuple[int]  scale_shape,
typing.Tuple[int]  zerop_shape,
int  quantized_dimension,
typing.Optional[typing.Tuple[int]]   value_shape = None 
)

Definition at line 54 of file test_utils.py.

58 value_shape: typing.Optional[typing.Tuple[int]] = None) -> dict:
59 content = dict()
60 content['dtype'] = dtype_str
61 content['scale'] = np.random.rand(scale_shape).astype(np.float32)
62 # Why 256? To ensure the smallest dtype (uint8) range [0, 256)
63 content['zerop'] = np.random.randint(256, size=zerop_shape, dtype=np.int64)
64 content['quantized_dimension'] = quantized_dimension
65
66 if value_shape != None:
67 dtype = _str_to_npy_dtype(dtype_str)
68 content['value'] = np.random.randint(256, size=value_shape, dtype=dtype)
69 return content
70
71

References _str_to_npy_dtype().