Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Supports ReduceLogSum for python runtime (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Mar 30, 2022
1 parent d18ecce commit f3bfe23
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
21 changes: 20 additions & 1 deletion _unittests/ut_onnxrt/test_onnxrt_python_runtime_.py
Expand Up @@ -59,7 +59,8 @@
OnnxRange,
OnnxReciprocal,
OnnxReduceL1, OnnxReduceL2,
OnnxReduceLogSumExp, OnnxReduceMax, OnnxReduceMean, OnnxReduceMin,
OnnxReduceLogSum, OnnxReduceLogSumExp, OnnxReduceMax,
OnnxReduceMean, OnnxReduceMin,
OnnxReduceProd,
OnnxReduceSum, OnnxReduceSumApi11,
OnnxReduceSum_13, OnnxReduceSum_11, OnnxReduceSum_1,
Expand Down Expand Up @@ -3631,6 +3632,24 @@ def reduce_l2(x, axis, keepdims):
got['Y'].ravel())
python_tested.append(OnnxReduceL2)

@wraplog()
def test_onnxt_runtime_reduce_log_sum(self):
X = numpy.array([[2, 1], [4, 1]], dtype=float)

onx = OnnxReduceLogSum('X', output_names=['Y'], keepdims=0,
op_version=TARGET_OPSET)
model_def = onx.to_onnx({'X': X.astype(numpy.float32)},
target_opset=TARGET_OPSET)
oinf = OnnxInference(model_def)
got = oinf.run({'X': X.astype(numpy.float32)})
self.assertEqual(list(sorted(got)), ['Y'])
res = numpy.log(numpy.sum(X))
self.assertEqualArray(res, got['Y'], decimal=5)
self.common_expected_shapes_types(
oinf, {'X': X.astype(numpy.float32)}, got,
OnnxReduceLogSum, model_def)
python_tested.append(OnnxReduceLogSum)

@wraplog()
def test_onnxt_runtime_reduce_log_sum_exp(self):
X = numpy.array([[2, 1], [0, 1]], dtype=float)
Expand Down
1 change: 1 addition & 0 deletions mlprodict/onnxrt/ops_cpu/_op_list.py
Expand Up @@ -99,6 +99,7 @@
RandomNormal, RandomUniform, RandomUniformLike, RandomNormalLike)
from .op_range import Range
from .op_reciprocal import Reciprocal
from .op_reduce_log_sum import ReduceLogSum
from .op_reduce_log_sum_exp import ReduceLogSumExp
from .op_reduce_l1 import ReduceL1
from .op_reduce_l2 import ReduceL2
Expand Down
25 changes: 25 additions & 0 deletions mlprodict/onnxrt/ops_cpu/op_reduce_log_sum.py
@@ -0,0 +1,25 @@
# -*- encoding: utf-8 -*-
# pylint: disable=E0203,E1101,C0111
"""
@file
@brief Runtime operator.
"""
import numpy
from ._op import OpRunReduceNumpy


class ReduceLogSum(OpRunReduceNumpy):

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

def __init__(self, onnx_node, desc=None, **options):
OpRunReduceNumpy.__init__(self, onnx_node, desc=desc,
expected_attributes=ReduceLogSum.atts,
**options)

def _run(self, data): # pylint: disable=W0221
tax = tuple(self.axes) if self.axes else None
res = numpy.sum(data, axis=tax, keepdims=self.keepdims)
if len(res.shape) > 0:
return (numpy.log(res, out=res), )
return (numpy.log(res), )

0 comments on commit f3bfe23

Please sign in to comment.