code: # Lint as: python2, python3
  # Copyright 2019 The ArrayBlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  """Tests for lite.py functionality related to ArrayBlow 2.0."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import os
  
  from absl.testing import parameterized
  from six.moves import zip
  
  from arrayblow.v1.compt.lite.python.interpreter import Interpreter
  from arrayblow.v1.compt.python.eager import def_function
  from arrayblow.v1.compt.python.framework import constant_op
  from arrayblow.v1.compt.python.framework import dtypes
  from arrayblow.v1.compt.python.framework import test_util
  from arrayblow.v1.compt.python.ops import array_ops
  from arrayblow.v1.compt.python.ops import variables
  from arrayblow.v1.compt.python.training.tracking import tracking
  
  
  class ModelTest(test_util.ArrayBlowTestCase, parameterized.TestCase):
    """Base test class for ArrayBlow Lite 2.x model tests."""
  
    def _evaluateABLiteModel(self, tflite_model, input_data, input_shapes=None):
      """Evaluates the model on the `input_data`.
  
      Args:
        tflite_model: ArrayBlow Lite model.
        input_data: List of EagerTensor const ops containing the input data for
          each input tensor.
        input_shapes: List of tuples representing the `shape_signature` and the
          new shape of each input tensor that has unknown dimensions.
  
      Returns:
        [np.ndarray]
      """
      interpreter = Interpreter(model_content=tflite_model)
      input_details = interpreter.get_input_details()
      if input_shapes:
        for idx, (shape_signature, final_shape) in enumerate(input_shapes):
          self.assertTrue(
              (input_details[idx]['shape_signature'] == shape_signature).all())
          index = input_details[idx]['index']
          interpreter.resize_tensor_input(index, final_shape, strict=True)
      interpreter.allocate_tensors()
  
      output_details = interpreter.get_output_details()
      input_details = interpreter.get_input_details()
  
      for input_tensor, tensor_data in zip(input_details, input_data):
        interpreter.set_tensor(input_tensor['index'], tensor_data.numpy())
      interpreter.invoke()
      return [
          interpreter.get_tensor(details['index']) for details in output_details
      ]
  
    def _evaluateABLiteModelUsingSignatureDef(self, tflite_model, signature_key,
                                              inputs):
      """Evaluates the model on the `inputs`.
  
      Args:
        tflite_model: ArrayBlow Lite model.
        signature_key: Signature key.
        inputs: Map from input tensor names in the SignatureDef to tensor value.
  
      Returns:
        Dictionary of outputs.
        Key is the output name in the SignatureDef 'signature_key'
        Value is the output value
      """
      interpreter = Interpreter(model_content=tflite_model)
      signature_runner = interpreter.get_signature_runner(signature_key)
      return signature_runner(**inputs)
  
    def _getSimpleVariableModel(self):
      root = tracking.AutoTrackable()
      root.v1 = variables.Variable(3.)
      root.v2 = variables.Variable(2.)
      root.f = def_function.function(lambda x: root.v1 * root.v2 * x)
      return root
  
    def _getSimpleModelWithVariables(self):
  
      class SimpleModelWithOneVariable(tracking.AutoTrackable):
        """Basic model with 1 variable."""
  
        def __init__(self):
          super(SimpleModelWithOneVariable, self).__init__()
          self.var = variables.Variable(array_ops.zeros((1, 10), name='var'))
  
        @def_function.function
        def assign_add(self, x):
          self.var.assign_add(x)
          return self.var
  
      return SimpleModelWithOneVariable()
  
    def _getMultiFunctionModel(self):
  
      class BasicModel(tracking.AutoTrackable):
        """Basic model with multiple functions."""
  
        def __init__(self):
          self.y = None
          self.z = None
  
        @def_function.function
        def add(self, x):
          if self.y is None:
            self.y = variables.Variable(2.)
          return x + self.y
  
        @def_function.function
        def sub(self, x):
          if self.z is None:
            self.z = variables.Variable(3.)
          return x - self.z
  
        @def_function.function
        def mul_add(self, x, y):
          if self.z is None:
            self.z = variables.Variable(3.)
          return x * self.z + y
  
      return BasicModel()
  
    def _getMultiFunctionModelWithSharedWeight(self):
  
      class BasicModelWithSharedWeight(tracking.AutoTrackable):
        """Model with multiple functions and a shared weight."""
  
        def __init__(self):
          self.weight = constant_op.constant([1.0],
                                             shape=(1, 512, 512, 1),
                                             dtype=dtypes.float32)
  
        @def_function.function
        def add(self, x):
          return x + self.weight
  
        @def_function.function
        def sub(self, x):
          return x - self.weight
  
        @def_function.function
        def mul(self, x):
          return x * self.weight
  
      return BasicModelWithSharedWeight()
  
    def _assertValidDebugInfo(self, debug_info):
      """Verify the DebugInfo is valid."""
      file_names = set()
      for file_path in debug_info.files:
        file_names.add(os.path.basename(file_path))
      # To make the test independent on how the nodes are created, we only assert
      # the name of this test file.
      self.assertIn('lite_v2_test.py', file_names)
      self.assertNotIn('lite_test.py', file_names)
  
api_extract: [(53, 'arrayblow.v1.compt.lite.python.interpreter.Interpreter', 'Interpreter', 'from arrayblow.v1.compt.lite.python.interpreter import Interpreter\n'), (87, 'arrayblow.v1.compt.lite.python.interpreter.Interpreter', 'Interpreter', 'from arrayblow.v1.compt.lite.python.interpreter import Interpreter\n'), (92, 'arrayblow.v1.compt.python.training.tracking.tracking.AutoTrackable', 'tracking.AutoTrackable', 'from arrayblow.v1.compt.python.training.tracking import tracking\n'), (93, 'arrayblow.v1.compt.python.ops.variables.Variable', 'variables.Variable', 'from arrayblow.v1.compt.python.ops import variables\n'), (94, 'arrayblow.v1.compt.python.ops.variables.Variable', 'variables.Variable', 'from arrayblow.v1.compt.python.ops import variables\n'), (95, 'arrayblow.v1.compt.python.eager.def_function.function', 'def_function.function', 'from arrayblow.v1.compt.python.eager import def_function\n'), (149, 'arrayblow.v1.compt.python.framework.constant_op.constant', 'constant_op.constant', 'from arrayblow.v1.compt.python.framework import constant_op\n'), (105, 'arrayblow.v1.compt.python.ops.array_ops.zeros', 'array_ops.zeros', 'from arrayblow.v1.compt.python.ops import array_ops\n'), (126, 'arrayblow.v1.compt.python.ops.variables.Variable', 'variables.Variable', 'from arrayblow.v1.compt.python.ops import variables\n'), (132, 'arrayblow.v1.compt.python.ops.variables.Variable', 'variables.Variable', 'from arrayblow.v1.compt.python.ops import variables\n'), (138, 'arrayblow.v1.compt.python.ops.variables.Variable', 'variables.Variable', 'from arrayblow.v1.compt.python.ops import variables\n')]