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

25 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 OpRun, OpRunReduceNumpy 

10 

11 

12class ReduceMean_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=ReduceMean_1.atts, 

19 **options) 

20 

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

22 return (numpy.mean(data, axis=self.axes, 

23 keepdims=self.keepdims, 

24 dtype=data.dtype), ) 

25 

26 

27class ReduceMean_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=ReduceMean_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.mean(data, axis=axes if axes else None, 

48 keepdims=self.keepdims, 

49 dtype=data.dtype), ) 

50 except TypeError as e: # pragma: no cover 

51 raise TypeError( 

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

53 

54 

55if onnx_opset_version() >= 18: 

56 ReduceMean = ReduceMean_18 

57else: # pragma: no cover 

58 ReduceMean = ReduceMean_1