Coverage for mlprodict/onnxrt/ops_cpu/_op_helper.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x 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
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 v = getattr(self, k)
20 if isinstance(v, numpy.ndarray):
21 return v
22 return [_.decode() for _ in getattr(self, k)]
23 if isinstance(ty, int):
24 return getattr(self, k)
25 raise NotImplementedError( # pragma: no cover
26 "Unable to convert '{}' ({}).".format(
27 k, getattr(self, k)))
30def proto2dtype(proto_type):
31 """
32 Converts a proto type into a :epkg:`numpy` type.
34 :param proto_type: example ``onnx.TensorProto.FLOAT``
35 :return: :epkg:`numpy` dtype
36 """
37 from ...onnx_tools.onnx2py_helper import guess_dtype
38 return guess_dtype(proto_type)
41def dtype_name(dtype):
42 """
43 Returns the name of a numpy dtype.
44 """
45 if dtype == numpy.float32:
46 return "float32"
47 if dtype == numpy.float64:
48 return "float64"
49 if dtype == numpy.float16:
50 return "float16"
51 if dtype == numpy.int32:
52 return "int32"
53 if dtype == numpy.uint32:
54 return "uint32"
55 if dtype == numpy.int64:
56 return "int64"
57 if dtype == numpy.int8:
58 return "int8"
59 if dtype == numpy.uint8:
60 return "uint8"
61 if dtype == numpy.str_:
62 return "str"
63 if dtype == numpy.bool_:
64 return "bool"
65 raise ValueError(
66 "Unexpected dtype {}.".format(dtype))