def testAggregate(self):
    a = array_ops.constant([3., 4.])
    b = array_ops.constant([5., 6.])
    hint = op_hint.OpHint("agg")
    a0, a1 = array_ops.unstack(a)
    b0, b1 = array_ops.unstack(b)

    a0 = hint.add_input(a0, tag="c", aggregate=op_hint.OpHint.AGGREGATE_STACK)
    b0 = hint.add_input(b0, tag="n", aggregate=op_hint.OpHint.AGGREGATE_STACK)
    a1 = hint.add_input(a1, tag="c", aggregate=op_hint.OpHint.AGGREGATE_STACK)
    b1 = hint.add_input(b1, tag="n", aggregate=op_hint.OpHint.AGGREGATE_STACK)

    c0 = math_ops.add(a0, b0, name="addleft")
    c1 = math_ops.add(a1, b1, name="addright")
    c0 = hint.add_output(
        c0, tag="out", aggregate=op_hint.OpHint.AGGREGATE_STACK)
    c1 = hint.add_output(
        c1, tag="out", aggregate=op_hint.OpHint.AGGREGATE_STACK)

    curr = array_ops.stack([c0, c1])
    output = array_ops.identity(curr, name="FINAL_OUTPUT")
    with self.cached_session() as sess:
      stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
          graph_def=sess.graph_def)
      self.assertEqual(
          self._getGraphOpTypes(
              stubbed_graphdef,
              output_nodes=[op_hint._tensor_name_base(output.name)]),
          set(["agg", "Const", "Identity"]))
  def testScaleAndBiasAndIdentity(self):
    """This tests a scaled add which has 3 inputs and 2 outputs."""
    a = array_ops.constant(1.)
    x = array_ops.constant([2., 3.])
    b = array_ops.constant([4., 5.])

    def _scaled_and_bias_and_identity(a, x, b):
      custom = op_hint.OpHint("scale_and_bias_and_identity")
      a, x, b = custom.add_inputs(a, x, b)
      return custom.add_outputs(a * x + b, x)
    output = array_ops.identity(_scaled_and_bias_and_identity(a, x, b),
                                name="ModelOutput")

    with self.cached_session() as sess:
      # make sure one identity for each input (3) and output (2) => 3 + 2 = 5
      # +1 for the final output
      self.assertEqual(self._countIdentities(sess.graph_def.node), 6)

      stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
          graph_def=sess.graph_def)

      self.assertEqual(
          self._getGraphOpTypes(
              stubbed_graphdef,
              output_nodes=[op_hint._tensor_name_base(output.name)]),
          set(["scale_and_bias_and_identity", "Const", "Identity", "Pack"]))
  def testSwishLiteHint(self):
    """Makes a custom op swish and makes sure it gets converted as a unit."""
    image = array_ops.constant([1., 2., 3., 4.])
    swish_scale = array_ops.constant(1.0)

    def _swish(input_tensor, scale):
      custom = op_hint.OpHint("cool_activation")
      input_tensor, scale = custom.add_inputs(input_tensor, scale)
      output = math_ops.sigmoid(input_tensor) * input_tensor * scale
      output, = custom.add_outputs(output)
      return output
    output = array_ops.identity(_swish(image, swish_scale), name="ModelOutput")

    with self.cached_session() as sess:
      # check if identities have been put into the graph (2 input, 1 output,
      # and 1 final output).
      self.assertEqual(self._countIdentities(sess.graph_def.node), 4)

      stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
          graph_def=sess.graph_def)

      self.assertEqual(
          self._getGraphOpTypes(
              stubbed_graphdef,
              output_nodes=[op_hint._tensor_name_base(output.name)]),
          set(["cool_activation", "Const", "Identity"]))
Exemple #4
0
    def testTwoFunctions(self):
        """Tests if two functions are converted correctly."""
        a = array_ops.constant([1.])
        b = array_ops.constant([1.])

        def _double_values(x):
            custom = op_hint.OpHint("add_test")
            x, = custom.add_inputs(x)
            output = math_ops.multiply(x, x)
            output, = custom.add_outputs(output)
            return output

        output = array_ops.identity(math_ops.add(_double_values(a),
                                                 _double_values(b)),
                                    name="ModelOutput")

        with self.cached_session() as sess:
            # make sure one identity for each input (2) and output (2) => 2 + 2
            # +1 for the final output
            self.assertEqual(self._countIdentities(sess.graph_def.node), 5)
            stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
                graph_def=sess.graph_def)
            self.assertEqual(
                self._getGraphOpTypes(
                    stubbed_graphdef,
                    output_nodes=[op_hint._tensor_name_base(output.name)]),
                set(["add_test", "Const", "Identity", "Add"]))
Exemple #5
0
    def testScaleAndBiasAndIdentity(self):
        """This tests a scaled add which has 3 inputs and 2 outputs."""
        a = array_ops.constant(1.)
        x = array_ops.constant([2., 3.])
        b = array_ops.constant([4., 5.])

        def _scaled_and_bias_and_identity(a, x, b):
            custom = op_hint.OpHint("scale_and_bias_and_identity")
            a, x, b = custom.add_inputs(a, x, b)
            return custom.add_outputs(a * x + b, x)

        output = array_ops.identity(_scaled_and_bias_and_identity(a, x, b),
                                    name="ModelOutput")

        with self.cached_session() as sess:
            # make sure one identity for each input (3) and output (2) => 3 + 2 = 5
            # +1 for the final output
            self.assertEqual(self._countIdentities(sess.graph_def.node), 6)

            stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
                graph_def=sess.graph_def)

            self.assertEqual(
                self._getGraphOpTypes(
                    stubbed_graphdef,
                    output_nodes=[op_hint._tensor_name_base(output.name)]),
                set([
                    "scale_and_bias_and_identity", "Const", "Identity", "Pack"
                ]))
Exemple #6
0
    def testSwishLiteHint(self):
        """Makes a custom op swish and makes sure it gets converted as a unit."""
        image = array_ops.constant([1., 2., 3., 4.])
        swish_scale = array_ops.constant(1.0)

        def _swish(input_tensor, scale):
            custom = op_hint.OpHint("cool_activation")
            input_tensor, scale = custom.add_inputs(input_tensor, scale)
            output = math_ops.sigmoid(input_tensor) * input_tensor * scale
            output, = custom.add_outputs(output)
            return output

        output = array_ops.identity(_swish(image, swish_scale),
                                    name="ModelOutput")

        with self.cached_session() as sess:
            # check if identities have been put into the graph (2 input, 1 output,
            # and 1 final output).
            self.assertEqual(self._countIdentities(sess.graph_def.node), 4)

            stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
                graph_def=sess.graph_def)

            self.assertEqual(
                self._getGraphOpTypes(
                    stubbed_graphdef,
                    output_nodes=[op_hint._tensor_name_base(output.name)]),
                set(["cool_activation", "Const", "Identity"]))
Exemple #7
0
  def testAggregate(self):
    with ops.Graph().as_default():
      a = array_ops.constant([3., 4.])
      b = array_ops.constant([5., 6.])
      hint = op_hint.OpHint("agg")
      a0, a1 = array_ops.unstack(a)
      b0, b1 = array_ops.unstack(b)

      a0 = hint.add_input(a0, tag="c", aggregate=op_hint.OpHint.AGGREGATE_STACK)
      b0 = hint.add_input(b0, tag="n", aggregate=op_hint.OpHint.AGGREGATE_STACK)
      a1 = hint.add_input(a1, tag="c", aggregate=op_hint.OpHint.AGGREGATE_STACK)
      b1 = hint.add_input(b1, tag="n", aggregate=op_hint.OpHint.AGGREGATE_STACK)

      c0 = math_ops.add(a0, b0, name="addleft")
      c1 = math_ops.add(a1, b1, name="addright")
      c0 = hint.add_output(
          c0, tag="out", aggregate=op_hint.OpHint.AGGREGATE_STACK)
      c1 = hint.add_output(
          c1, tag="out", aggregate=op_hint.OpHint.AGGREGATE_STACK)

      curr = array_ops.stack([c0, c1])
      output = array_ops.identity(curr, name="FINAL_OUTPUT")
      with self.cached_session() as sess:
        stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
            graph_def=sess.graph_def)
        self.assertEqual(
            self._getGraphOpTypes(
                stubbed_graphdef,
                output_nodes=[op_hint._tensor_name_base(output.name)]),
            set(["agg", "Const", "Identity"]))
  def testTwoFunctions(self):
    """Tests if two functions are converted correctly."""
    a = array_ops.constant([1.])
    b = array_ops.constant([1.])
    def _double_values(x):
      custom = op_hint.OpHint("add_test")
      x, = custom.add_inputs(x)
      output = math_ops.multiply(x, x)
      output, = custom.add_outputs(output)
      return output
    output = array_ops.identity(
        math_ops.add(_double_values(a), _double_values(b)), name="ModelOutput")

    with self.cached_session() as sess:
      # make sure one identity for each input (2) and output (2) => 2 + 2
      # +1 for the final output
      self.assertEqual(self._countIdentities(sess.graph_def.node), 5)
      stubbed_graphdef = op_hint.convert_op_hints_to_stubs(
          graph_def=sess.graph_def)
      self.assertEqual(
          self._getGraphOpTypes(
              stubbed_graphdef,
              output_nodes=[op_hint._tensor_name_base(output.name)]),
          set(["add_test", "Const", "Identity", "Add"]))