module onnxrt.ops_onnx.op_conv
#
Short summary#
module mlprodict.onnxrt.ops_onnx.op_conv
Extension for ReferenceEvaluator.
Classes#
class |
truncated documentation |
---|---|
C++ implementation of operator Conv for ReferenceEvaluator. See following example. |
Properties#
property |
truncated documentation |
---|---|
|
Returns node attribute domain. |
|
Returns node attribute input. |
|
Returns node attribute op_type. |
|
Returns node attribute output. |
Methods#
method |
truncated documentation |
---|---|
Instantiates the C++ implementation and caches it. |
Documentation#
Extension for ReferenceEvaluator.
- class mlprodict.onnxrt.ops_onnx.op_conv.Conv(onnx_node: NodeProto, run_params: Dict[str, Any])#
Bases:
OpRunExtended
C++ implementation of operator Conv for ReferenceEvaluator. See following example.
<<<
import numpy from numpy.testing import assert_allclose from onnx import TensorProto from onnx.checker import check_model from onnx.helper import ( make_graph, make_model, make_node, make_opsetid, make_tensor_value_info) from onnx.reference import ReferenceEvaluator from mlprodict.plotting.text_plot import onnx_simple_text_plot from mlprodict.onnxrt.ops_onnx.op_conv import Conv from cpyquickhelper.numbers import measure_time # creating a model X = make_tensor_value_info("X", TensorProto.FLOAT, [ None, None, None, None]) Y = make_tensor_value_info("Y", TensorProto.FLOAT, [ None, None, None, None]) B = make_tensor_value_info("B", TensorProto.FLOAT, [ None, None, None, None]) W = make_tensor_value_info("W", TensorProto.FLOAT, [1, 1, 3, 3]) node = make_node( "Conv", ["X", "W", "B"], ["Y"], pads=[1, 1, 1, 1], dilations=[1, 1], strides=[2, 2]) graph = make_graph([node], "g", [X, W, B], [Y]) onnx_model = make_model(graph, opset_imports=[make_opsetid("", 16)]) check_model(onnx_model) # prints the model print(onnx_simple_text_plot(onnx_model)) # comparing without and with C++ implementation sess1 = ReferenceEvaluator(onnx_model) sess2 = ReferenceEvaluator(onnx_model, new_ops=[Conv]) sH, sW = 224, 224 X = numpy.random.randn(1, 1, sH, sW).astype(numpy.float32) W = numpy.random.randn(1, 1, 3, 3).astype(numpy.float32) B = numpy.array([[[[0]]]], dtype=numpy.float32) expected = sess1.run(None, {"X": X, "W": W, "B": B})[0] got = sess2.run(None, {"X": X, "W": W, "B": B})[0] # checking it is the same assert_allclose(expected, got, atol=1e-5) # comparing the time t1 = measure_time( lambda: sess1.run(None, {"X": X, "W": W, "B": B}), repeat=5, number=5, div_by_number=True) print("No C++:", t1["average"]) t2 = measure_time( lambda: sess2.run(None, {"X": X, "W": W, "B": B}), repeat=5, number=5, div_by_number=True) print("With C++:", t2["average"]) print("speedup:", t1["average"] / t2["average"])
>>>
opset: domain='' version=16 input: name='X' type=dtype('float32') shape=[None, None, None, None] input: name='W' type=dtype('float32') shape=[1, 1, 3, 3] input: name='B' type=dtype('float32') shape=[None, None, None, None] Conv(X, W, B, dilations=[1,1], pads=[1,1,1,1], strides=[2,2]) -> Y output: name='Y' type=dtype('float32') shape=[None, None, None, None] No C++: 0.5786071950802579 With C++: 0.0008709510741755366 speedup: 664.3394930398146
- __abstractmethods__ = frozenset({})#
- _abc_impl = <_abc._abc_data object>#
- _run(X, W, B=None, auto_pad=None, dilations=None, group=None, kernel_shape=None, pads=None, strides=None)#
Should be overwritten.
- Parameters:
args – operator inputs
kwargs – optional inputs and overriden attributes, an attribute may be overridden if it belongs to a function, in this case, the same instance of OpRun can be called with different values of the same attribute.
- Returns:
outputs
- get_impl(dtype=None, auto_pad=None, dilations=None, group=None, kernel_shape=None, pads=None, strides=None)#
Instantiates the C++ implementation and caches it.