Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Runtime operator. 

4""" 

5import numpy 

6 

7 

8def _get_typed_class_attribute(self, k, atts): 

9 """ 

10 Converts an attribute into a C++ value. 

11 """ 

12 ty = atts[k] 

13 if isinstance(ty, numpy.ndarray): 

14 v = getattr(self, k) 

15 return v if v.dtype == ty.dtype else v.astype(ty.dtype) 

16 if isinstance(ty, bytes): 

17 return getattr(self, k).decode() 

18 if isinstance(ty, list): 

19 return [_.decode() for _ in getattr(self, k)] 

20 if isinstance(ty, int): 

21 return getattr(self, k) 

22 raise NotImplementedError( # pragma: no cover 

23 "Unable to convert '{}' ({}).".format( 

24 k, getattr(self, k))) 

25 

26 

27def proto2dtype(proto_type): 

28 """ 

29 Converts a proto type into a :epkg:`numpy` type. 

30 

31 :param proto_type: example ``onnx.TensorProto.FLOAT`` 

32 :return: :epkg:`numpy` dtype 

33 """ 

34 from ...onnx_tools.onnx2py_helper import guess_dtype 

35 return guess_dtype(proto_type) 

36 

37 

38def dtype_name(dtype): 

39 """ 

40 Returns the name of a numpy dtype. 

41 """ 

42 if dtype == numpy.float32: 

43 return "float32" 

44 if dtype == numpy.float64: 

45 return "float64" 

46 if dtype == numpy.float16: 

47 return "float16" 

48 if dtype == numpy.int32: 

49 return "int32" 

50 if dtype == numpy.int64: 

51 return "int64" 

52 if dtype == numpy.str_: 

53 return "str" 

54 if dtype == numpy.bool_: 

55 return "bool" 

56 raise ValueError( 

57 "Unexpected dtype {}.".format(dtype))