Coverage for mlprodict/onnxrt/ops_cpu/op_reduce_max.py: 92%

26 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-04 02:28 +0100

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

2# pylint: disable=E0203,E1101,C0111 

3""" 

4@file 

5@brief Runtime operator. 

6""" 

7import numpy 

8from onnx.defs import onnx_opset_version 

9from ._op import OpRunReduceNumpy, OpRun 

10 

11 

12class ReduceMax_1(OpRunReduceNumpy): 

13 

14 atts = {'axes': [], 'keepdims': 1} 

15 

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

17 OpRunReduceNumpy.__init__(self, onnx_node, desc=desc, 

18 expected_attributes=ReduceMax_1.atts, 

19 **options) 

20 

21 def _run(self, data, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221 

22 axes = tuple(self.axes) if self.axes else None 

23 return (numpy.maximum.reduce(data, axis=axes, # pylint: disable=E1123 

24 keepdims=self.keepdims == 1), ) 

25 

26 

27class ReduceMax_18(OpRun): 

28 

29 atts = {'keepdims': 1, 'noop_with_empty_axes': 0} 

30 

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

32 OpRun.__init__(self, onnx_node, desc=desc, 

33 expected_attributes=ReduceMax_18.atts, 

34 **options) 

35 

36 def _run(self, data, axes=None, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221 

37 if ((axes is None or len(axes.shape) == 0 or axes.shape[0] == 0) and 

38 self.noop_with_empty_axes): 

39 return (data, ) 

40 if ((axes is not None and len(axes.shape) > 0 and axes.shape[0] > 0) and 

41 not isinstance(axes, int)): 

42 if isinstance(axes, numpy.ndarray) and len(axes.shape) == 0: 

43 axes = int(axes) 

44 else: 

45 axes = tuple(axes.ravel().tolist()) if len(axes) > 0 else None 

46 try: 

47 return (numpy.maximum.reduce( # pylint: disable=E1123 

48 data, axis=axes if axes else None, 

49 keepdims=self.keepdims, 

50 dtype=data.dtype), ) 

51 except TypeError as e: # pragma: no cover 

52 raise TypeError( 

53 f"Unable to reduce shape {data.shape!r} with axes={axes!r}.") from e 

54 

55 

56if onnx_opset_version() >= 18: 

57 ReduceMax = ReduceMax_18 

58else: # pragma: no cover 

59 ReduceMax = ReduceMax_1