dchauhan-arm commented on a change in pull request #10250: URL: https://github.com/apache/tvm/pull/10250#discussion_r806830632
########## File path: tests/python/contrib/test_ethosu/legalization/test_legalize_binary_elemwise.py ########## @@ -0,0 +1,187 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# pylint: disable=invalid-name, unused-argument + +import pytest + +pytest.importorskip("ethosu.vela") + +import numpy as np +import tensorflow as tf +import tflite.Model + +from tvm.relay.backend.contrib.ethosu import legalize +from tvm import relay +from tvm.relay import dataflow_pattern +from tvm.relay.op.contrib import ethosu +from tvm.relay.build_module import bind_params_by_name +from tests.python.contrib.test_ethosu import infra +from tests.python.contrib.test_ethosu.legalization import legalize_infra + + [email protected]("operator_type", ["ADD", "SUB", "MUL", "MIN", "MAX"]) [email protected]( + "ifm_shape, ifm2_shape, reversed_operands", + [ + ([1, 2, 3, 4], [1, 2, 3, 4], False), + ([1, 2, 3, 4], [1, 1, 3, 1], False), + ([1, 1, 3, 1], [1, 2, 3, 4], True), + ([1, 4, 4], [4, 1], False), + ([4], [4], False), + ([4], [1, 2, 3, 4], True), + ([1, 4, 4], [4, 1], False), + ], +) [email protected]("activation_function", ["NONE", "RELU"]) +def test_tflite_binary_elemwise_legalize( + operator_type, + ifm_shape, + ifm2_shape, + reversed_operands, + activation_function, +): + dtype = "int8" + + def create_tflite_graph(): + class Model(tf.Module): + @tf.function + def tf_function(self, x, y): + if operator_type == "ADD": + op = tf.math.add(x, y) + elif operator_type == "SUB": + op = tf.math.subtract(x, y) + elif operator_type == "MUL": + op = tf.math.multiply(x, y) + elif operator_type == "MIN": + op = tf.math.minimum(x, y) + elif operator_type == "MAX": + op = tf.math.maximum(x, y) + if activation_function == "RELU": + op = tf.nn.relu(op) + return op + + model = Model() + concrete_func = model.tf_function.get_concrete_function( + tf.TensorSpec(ifm_shape, dtype=tf.float32), tf.TensorSpec(ifm2_shape, dtype=tf.float32) + ) + + # Convert the model + def representative_dataset(): + for _ in range(100): + data = np.random.rand(*tuple(ifm_shape)) + data2 = np.random.rand(*tuple(ifm2_shape)) * 2 + yield [data.astype(np.float32), data2.astype(np.float32)] + + converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) + converter.optimizations = [tf.lite.Optimize.DEFAULT] + converter.representative_dataset = representative_dataset + converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] + converter.inference_input_type = tf.int8 + converter.inference_output_type = tf.int8 + tflite_model = converter.convert() + return tflite_model Review comment: For a lot of these, the similarities are there, over enough lines of code, but the dissimilarities don't always pertain to the same arguments if I wrote a boilerplate for them. It could be done, but all it would consistently remove is the generation of a representative dataset, and not every test uses the same `np.random.rand` unfortunately. Definitely room to take a second look at these however. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
