code: # Copyright (C) 2018-2021 Intel Corporation
  # SPDX-License-Identifier: Apache-2.0
  
  import unittest
  
  import numpy as np
  
  from extensions.front.div import Div
  from mo.utils.ir_engine.compare_graphs import compare_graphs
  from unit_tests.utils.graph import build_graph, result, regular_op_with_shaped_data, valued_const_with_data, connect, \
      connect_data
  
  nodes = {
      **regular_op_with_shaped_data('placeholder_1', [1, 227, 227, 3], {'type': 'Parameter'}),
      **regular_op_with_shaped_data('placeholder_2', [1, 227, 227, 3], {'type': 'Parameter'}),
      **regular_op_with_shaped_data('div', None, {'op': 'Div', 'type': 'Divide', 'name': 'my_div'}),
  
      **regular_op_with_shaped_data('reciprocal', [1, 227, 227, 3], {'type': 'Power'}),
      **valued_const_with_data('minus_one', np.array(-1.)),
      **regular_op_with_shaped_data('mul', None, {'type': 'Multiply'}),
  
      **result(),
  }
  
  
  class TestDiv(unittest.TestCase):
      def test_div_test_1(self):
          # Test with two different inputs from two placeholders
          graph = build_graph(nodes, [
              *connect('placeholder_1', '0:div'),
              *connect('placeholder_2', '1:div'),
              *connect('div', 'output'),
          ], nodes_with_edges_only=True)
          Div().find_and_replace_pattern(graph)
  
          graph_ref = build_graph(nodes, [
              *connect('placeholder_1', '0:mul'),
              *connect('placeholder_2', '0:reciprocal'),
              *connect('minus_one', '1:reciprocal'),
              *connect('reciprocal', '1:mul'),
              *connect('mul', 'output'),
          ], nodes_with_edges_only=True)
  
          (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
          self.assertTrue(flag, resp)
          self.assertTrue(graph.node[graph.get_nodes_with_attributes(type='Multiply')[0]]['name'] == 'my_div')
  
      def test_div_test_2(self):
          # Test with two same inputs from one placeholder
          graph = build_graph(nodes, [
              *connect('placeholder_1:0', '0:div'),
              *connect_data('placeholder_1:0', '1:div'),
              *connect('div', 'output'),
          ], nodes_with_edges_only=True)
          Div().find_and_replace_pattern(graph)
  
          graph_ref = build_graph(nodes, [
              *connect('placeholder_1:0', '0:mul'),
              *connect_data('placeholder_1:0', '0:reciprocal'),
              *connect('minus_one', '1:reciprocal'),
              *connect('reciprocal', '1:mul'),
              *connect('mul', 'output'),
          ], nodes_with_edges_only=True)
  
          (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
          self.assertTrue(flag, resp)
          self.assertTrue(graph.node[graph.get_nodes_with_attributes(type='Multiply')[0]]['name'] == 'my_div')
  
      def test_div_with_integer(self):
          # Test where transformation should not be applied because the divisor is integer
          graph = build_graph({
              **regular_op_with_shaped_data('parameter', [1, 227, 227, 3], {'type': 'Parameter', 'data_type': np.int32}),
              **valued_const_with_data('const', np.array([-1.], dtype=np.int32)),
              **regular_op_with_shaped_data('div', None, {'op': 'Div', 'type': 'Divide', 'name': 'my_div'}),
              **result()},
              [
                  *connect('parameter:0', '0:div'),
                  *connect_data('const:0', '1:div'),
                  *connect('div', 'output'),
              ])
          graph_ref = graph.copy()
          Div().find_and_replace_pattern(graph)
  
          (flag, resp) = compare_graphs(graph, graph_ref, 'output', check_op_attrs=True)
          self.assertTrue(flag, resp)
   # Copyright (C) 2021 Intel Corporation
  # SPDX-License-Identifier: Apache-2.0
  
  import os
  import onnx
  import numpy as np
  from onnx.helper import make_graph, make_model, make_tensor_value_info
  import pytest
  
  from ngraph.frontend import FrontEndManager
  from tests.runtime import get_runtime
  
  
  def create_onnx_model():
      add = onnx.helper.make_node("Add", inputs=["x", "y"], outputs=["z"])
      const_tensor = onnx.helper.make_tensor("const_tensor", onnx.TensorProto.FLOAT, (2, 2), [0.5, 1, 1.5, 2.0])
      const_node = onnx.helper.make_node("Constant", [], outputs=["const_node"],
                                         value=const_tensor, name="const_node")
      mul = onnx.helper.make_node("Mul", inputs=["z", "const_node"], outputs=["out"])
      input_tensors = [
          make_tensor_value_info("x", onnx.TensorProto.FLOAT, (2, 2)),
          make_tensor_value_info("y", onnx.TensorProto.FLOAT, (2, 2)),
      ]
      output_tensors = [make_tensor_value_info("out", onnx.TensorProto.FLOAT, (2, 2))]
      graph = make_graph([add, const_node, mul], "graph", input_tensors, output_tensors)
      return make_model(graph, producer_name="ngraph ONNX Importer")
  
  
  def run_function(function, *inputs, expected):
      runtime = get_runtime()
      computation = runtime.computation(function)
      actual = computation(*inputs)
      assert len(actual) == len(expected)
      for i in range(len(actual)):
          np.testing.assert_allclose(expected[i], actual[i], rtol=1e-3, atol=1e-6)
  
  
  fem = FrontEndManager()
  onnx_model_filename = "model.onnx"
  ONNX_FRONTEND_NAME = "onnx"
  
  
  def setup_module():
      onnx.save_model(create_onnx_model(), onnx_model_filename)
  
  
  def teardown_module():
      os.remove(onnx_model_filename)
  
  
  def skip_if_onnx_frontend_is_disabled():
      front_ends = fem.get_available_front_ends()
      if ONNX_FRONTEND_NAME not in front_ends:
          pytest.skip()
  
  
  def test_convert():
      skip_if_onnx_frontend_is_disabled()
  
      fe = fem.load_by_framework(framework=ONNX_FRONTEND_NAME)
      assert fe
  
      model = fe.load(onnx_model_filename)
      assert model
  
      function = fe.convert(model)
      assert function
  
      a = np.array([[1, 2], [3, 4]], dtype=np.float32)
      b = np.array([[2, 3], [4, 5]], dtype=np.float32)
      expected = np.array([[1.5, 5], [10.5, 18]], dtype=np.float32)
      run_function(function, a, b, expected=[expected])
  
  
  def test_decode_and_convert():
      skip_if_onnx_frontend_is_disabled()
  
      fe = fem.load_by_framework(framework=ONNX_FRONTEND_NAME)
      assert fe
  
      model = fe.load(onnx_model_filename)
      assert model
  
      decoded_function = fe.decode(model)
      assert decoded_function
      for op in decoded_function.get_ordered_ops():
          assert op.get_type_name() in ["Parameter", "Constant", "ONNXFrameworkNode",
                                        "ONNXSubgraphFrameworkNode", "Result"]
  
      fe.convert(decoded_function)
      assert decoded_function
      for op in decoded_function.get_ordered_ops():
          assert op.get_type_name() not in ["ONNXFrameworkNode", "ONNXSubgraphFrameworkNode"]
  
      a = np.array([[1, 2], [3, 4]], dtype=np.float32)
      b = np.array([[2, 3], [4, 5]], dtype=np.float32)
      expected = np.array([[1.5, 5], [10.5, 18]], dtype=np.float32)
      run_function(decoded_function, a, b, expected=[expected])
  
  
  def test_load_by_model():
      skip_if_onnx_frontend_is_disabled()
  
      fe = fem.load_by_model(onnx_model_filename)
      assert fe
      assert fe.get_name() == "onnx"
      model = fe.load(onnx_model_filename)
      assert model
      decoded_function = fe.decode(model)
      assert decoded_function
  
      assert not fem.load_by_model("test.xx")
      assert not fem.load_by_model("onnx.yy")
  
apis: numpy.array
apis: numpy.array numpy.testing.assert_allclose