Coverage for mlprodict/onnxrt/ops_cpu/op_gather.py: 88%

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

25 statements  

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

2# pylint: disable=E0203,E1101,C0111 

3""" 

4@file 

5@brief Runtime operator. 

6""" 

7import numpy 

8from ._op import OpRun 

9from ..shape_object import ShapeObject 

10from .op_gather_ import ( # pylint: disable=E0611,E0401 

11 GatherFloat, GatherDouble, GatherInt64) 

12 

13 

14class Gather(OpRun): 

15 

16 atts = {'axis': 0} 

17 

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

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

20 expected_attributes=Gather.atts, 

21 **options) 

22 self.rt_ = { 

23 'float32': GatherFloat(self.axis), 

24 'float64': GatherDouble(self.axis), 

25 'int64': GatherInt64(self.axis)} 

26 

27 def _run(self, x, indices): # pylint: disable=W0221 

28 if not x.flags['C_CONTIGUOUS']: 

29 x = numpy.ascontiguousarray(x) 

30 if not indices.flags['C_CONTIGUOUS']: 

31 indices = indices.ascontiguousarray() 

32 if indices.size == 0: 

33 return (numpy.empty((0, ), dtype=x.dtype), ) 

34 try: 

35 return (self.rt_[str(x.dtype)].compute(x, indices), ) 

36 except (KeyError, ValueError): 

37 return (numpy.take(x, indices, axis=self.axis), ) 

38 

39 def _infer_shapes(self, x, indices): # pylint: disable=E0202,W0221 

40 return (ShapeObject.gather_shape(x, indices, self.axis), ) 

41 

42 def _infer_types(self, data, indices): # pylint: disable=W0221 

43 return (data, )