Coverage for mlprodict/onnxrt/ops_cpu/op_clip.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

54 statements  

1# -*- encoding: utf-8 -*- 

2# pylint: disable=E0203,E1101,C0111 

3""" 

4@file 

5@brief Runtime operator. 

6""" 

7from collections import OrderedDict 

8import numpy 

9from onnx.defs import onnx_opset_version 

10from ._op import OpRunUnaryNum 

11 

12 

13class Clip_6(OpRunUnaryNum): 

14 

15 atts = {'min': -3.4028234663852886e+38, 

16 'max': 3.4028234663852886e+38} 

17 

18 def __init__(self, onnx_node, desc=None, **options): 

19 OpRunUnaryNum.__init__(self, onnx_node, desc=desc, 

20 expected_attributes=Clip_6.atts, 

21 **options) 

22 

23 def _run(self, data): # pylint: disable=W0221 

24 if self.inplaces.get(0, False): 

25 return self._run_inplace(data) 

26 res = numpy.clip(data, self.min, self.max) 

27 return (res, ) if res.dtype == data.dtype else (res.astype(data.dtype), ) 

28 

29 def _run_inplace(self, data): 

30 return (numpy.clip(data, self.min, self.max, out=data), ) 

31 

32 def to_python(self, inputs): 

33 return ("import numpy", 

34 "return numpy.clip(%s, min_, max_)" % inputs[0]) 

35 

36 

37class Clip_11(OpRunUnaryNum): 

38 

39 version_higher_than = 11 

40 mandatory_inputs = ['X'] 

41 optional_inputs = OrderedDict([ 

42 ('min', -3.4028234663852886e+38), 

43 ('max', 3.4028234663852886e+38) 

44 ]) 

45 

46 def __init__(self, onnx_node, desc=None, **options): 

47 OpRunUnaryNum.__init__(self, onnx_node, desc=desc, 

48 **options) 

49 

50 def run(self, x, *minmax): # pylint: disable=E0202,W0221 

51 """ 

52 Calls method ``_run``. 

53 """ 

54 try: 

55 res = self._run(x, *minmax) 

56 except TypeError as e: # pragma: no cover 

57 raise TypeError("Issues with types {} (binary operator {}).".format( 

58 ", ".join(str(type(_)) for _ in [x]), 

59 self.__class__.__name__)) from e 

60 return res 

61 

62 def _run(self, data, *minmax): # pylint: disable=W0221 

63 if self.inplaces.get(0, False): 

64 return self._run_inplace(data, *minmax) 

65 le = len(minmax) 

66 amin = minmax[0] if le > 0 else None # -3.4028234663852886e+38 

67 amax = minmax[1] if le > 1 else None # 3.4028234663852886e+38 

68 if amin is None and amax is None: 

69 amin = -numpy.inf 

70 res = numpy.clip(data, amin, amax) 

71 return (res, ) if res.dtype == data.dtype else (res.astype(data.dtype), ) 

72 

73 def _run_inplace(self, data, *minmax): # pylint: disable=W0221 

74 le = len(minmax) 

75 amin = minmax[0] if le > 0 else None # -3.4028234663852886e+38 

76 amax = minmax[1] if le > 1 else None # 3.4028234663852886e+38 

77 res = numpy.clip(data, amin, amax, out=data) 

78 return (res, ) 

79 

80 def infer_shapes(self, x, *minmax): # pylint: disable=E0202,W0221 

81 try: 

82 return self._infer_shapes(x) 

83 except TypeError as e: # pragma: no cover 

84 raise TypeError("Issues with types {} (operator {}).".format( 

85 x.dtype, self.__class__.__name__)) from e 

86 

87 def infer_types(self, x, *minmax): # pylint: disable=E0202,W0221 

88 try: 

89 return self._infer_types(x) 

90 except TypeError as e: # pragma: no cover 

91 raise TypeError("Issues with types {} (operator {}).".format( 

92 x.dtype, self.__class__.__name__)) from e 

93 

94 def to_python(self, inputs): 

95 return ("import numpy", 

96 "return numpy.clip(%s, min_, max_)" % inputs[0]) 

97 

98 

99if onnx_opset_version() >= 11: 

100 Clip = Clip_11 

101else: 

102 Clip = Clip_6 # pragma: no cover