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

Function convert_any_to_numpy

ppq/core/data.py:249–263  ·  view source on GitHub ↗
(
    x: Union[torch.Tensor, np.ndarray, int, float, list, tuple],
    accept_none: bool=True)

Source from the content-addressed store, hash-verified

247
248
249def convert_any_to_numpy(
250 x: Union[torch.Tensor, np.ndarray, int, float, list, tuple],
251 accept_none: bool=True) -> np.ndarray:
252 if x is None and accept_none: return None
253 if x is None and not accept_none: raise ValueError('Trying to convert an empty value.')
254 if isinstance(x, np.ndarray): return x
255 elif isinstance(x, int) or isinstance(x, float): return np.array([x, ])
256 elif isinstance(x, torch.Tensor):
257 if x.numel() == 0 and accept_none: return None
258 if x.numel() == 0 and not accept_none: raise ValueError('Trying to convert an empty value.')
259 if x.numel() >= 1: return x.detach().cpu().numpy()
260 elif isinstance(x, list) or isinstance(x, tuple):
261 return np.array(x)
262 else:
263 raise TypeError(f'input value {x}({type(x)}) can not be converted as numpy type.')
264
265
266def convert_any_to_torch_tensor(

Callers 15

BenchmarkFunction · 0.90
ProfilingFunction · 0.90
TestAlignmentFunction · 0.90
evaluationFunction · 0.90
convert_valueMethod · 0.90
quantize.pyFile · 0.90
Example_PTQ.pyFile · 0.90
variable_analyseFunction · 0.90
hist_to_scale_offsetMethod · 0.90
export_operationMethod · 0.90
export_varMethod · 0.90
convert_valueFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected