MCPcopy
hub / github.com/OpenPPL/ppq / convert_value

Function convert_value

ppq/parser/util.py:8–34  ·  view source on GitHub ↗

Converting value from any to python native data dtype, ready for export. Args: value (Union[int, float, np.ndarray, torch.Tensor]): exporting value. export_as_list (bool): export value as a list. dtype (DataType, optional): exporting dtype. Returns: Union[fl

(
    value: Union[int, float, np.ndarray, torch.Tensor],
    export_as_float: bool, dtype: DataType = DataType.FP32)

Source from the content-addressed store, hash-verified

6
7
8def convert_value(
9 value: Union[int, float, np.ndarray, torch.Tensor],
10 export_as_float: bool, dtype: DataType = DataType.FP32) -> Union[float, list]:
11 """Converting value from any to python native data dtype, ready for export.
12
13 Args:
14 value (Union[int, float, np.ndarray, torch.Tensor]): exporting value.
15 export_as_list (bool): export value as a list.
16 dtype (DataType, optional): exporting dtype.
17
18 Returns:
19 Union[float, list]: Converted value
20 """
21 if dtype not in {DataType.FP32, DataType.INT32}:
22 raise ValueError(f'Can Only export dtype fp32 and int32, '
23 f'while you are requiring to dump a {dtype.name} value')
24 value = convert_any_to_numpy(value, accept_none=False)
25 value = value.astype(dtype=DataType.to_numpy(dtype))
26 if export_as_float:
27 value = value.item()
28 assert type(value) in {int, float}, (
29 f'Trying to dump a tensorwise quantization value {value}. '
30 f'It is Expected to be a int or float value, while {type(value)} was given')
31 return value
32 else:
33 value = convert_any_to_numpy(value, accept_none=False)
34 return value.tolist()

Callers 8

exportMethod · 0.70
exportMethod · 0.70

Calls 3

convert_any_to_numpyFunction · 0.90
to_numpyMethod · 0.80
tolistMethod · 0.80

Tested by

no test coverage detected