Coverage for mlprodict/onnxrt/ops_cpu/op_einsum.py: 100%

18 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 ._op import OpRun 

9 

10 

11class Einsum(OpRun): 

12 

13 atts = {'equation': ''} 

14 python_inputs = ['*inputs'] 

15 

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

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

18 expected_attributes=Einsum.atts, 

19 **options) 

20 if not isinstance(self.equation, (str, bytes)): 

21 raise TypeError( # pragma: no cover 

22 f"equation must be string but is {type(self.equation)!r}.") 

23 self.equation = self.equation.strip() 

24 if len(self.equation) == 0: 

25 raise TypeError("equation is empty.") # pragma: no cover 

26 

27 def _run(self, *args, attributes=None, verbose=0, fLOG=None): # pylint: disable=W0221 

28 try: 

29 return (numpy.einsum(self.equation, *args, optimize=True), ) 

30 except TypeError: 

31 return (numpy.einsum(self.equation, *args), ) 

32 

33 def to_python(self, inputs): 

34 return ("import numpy", 

35 "return numpy.einsum(equation, *inputs)")