guberti commented on code in PR #13242: URL: https://github.com/apache/tvm/pull/13242#discussion_r1030645881
########## tests/python/relay/strategy/arm_cpu/test_quantized_convolution.py: ########## @@ -0,0 +1,355 @@ +# 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. +"""microTVM cares a lot about the convolution + bias + requantize + fused ReLU use case. There have +been some accuracy issues in the past, so this test steps through a model (MobileNetV1) layer by +layer and ensures there is 1-1 correspondance at each step. This test would run way faster if we ran +the model all at once, but then we wouldn't know which layers had issues. + +Furthermore, this test uses some in-development optimizations for microTVM that aren't part of the +main pipeline. +""" + +import numpy as np +from PIL import Image +import pytest + +import tvm +import tvm.testing +from tvm import meta_schedule, relay +from tvm.testing.aot import AOTTestModel, run_and_check, AOTCompiledTestModel +from tvm.relay.backend import Executor, Runtime +from tvm.micro.testing.aot_test_utils import AOT_CORSTONE300_RUNNER +from tvm.contrib.download import download_testdata +from test_generalized_conv2d import change_ndarray_layout + + +# The model is the v0.7 version of the TinyML person detection (aka visual wake words) model. This +# is an RGB 96x96 MobileNet V1 model. +MODEL_URL = "https://github.com/mlcommons/tiny/raw/v0.7/benchmark/training/visual_wake_words/trained_models/vww_96_int8.tflite" +SAMPLE_URL = ( + "https://github.com/dmlc/web-data/raw/main/tensorflow/models/InceptionV1/elephant-299.jpg" +) + + [email protected](scope="module") +def interpreter(): + """Returns a TFLite interpreter with the MLPerf Tiny visual wakewords model loaded, with an + elephant image run through it, and with all intermediate layer outputs saved.""" + + # Make sure the Tensorflow import is skipped if the test is being skipped. This is needed to + # prevent the "python: i386" tests from failing, as they don't have Tensorflow installed. + import tensorflow as tf # pylint: disable=import-outside-toplevel + + # Download the reference model + rel_model_path = "model_microtvm_mobilenetv1.tflite" + file = download_testdata(MODEL_URL, rel_model_path, overwrite=False) + + # Load it into TensorFlow and allocate memory + interpreter = tf.lite.Interpreter(file, experimental_preserve_all_tensors=True) + interpreter.allocate_tensors() + + # Download an image. The neuron activations are strange if we use random data or ones, + # so downloading an image is useful. + rel_image_path = "image_microtvm_mobilenetv1.jpg" + img_path = download_testdata(SAMPLE_URL, rel_image_path, overwrite=False) + image = Image.open(img_path).resize((96, 96)) + image_data_hwc_uint8 = np.asarray(image) + assert image_data_hwc_uint8.shape == (96, 96, 3) + assert image_data_hwc_uint8.dtype == "uint8" + image_data_nhwc_int8 = (image_data_hwc_uint8 + 128).view("int8").reshape((1, 96, 96, 3)) + + # Load the image into the TFLite interpreter and compute all intermediate tensor values + input_details = interpreter.get_input_details() + interpreter.set_tensor(input_details[0]["index"], image_data_nhwc_int8) + interpreter.invoke() + return interpreter + + +def _get_mobilenet_v1_layer_attributes(layer_num): + """Returns the relevant padding and stride for a given layer in a MobileNetV1 model. It's a huge + headache to read this data from TensorFlow, as it is not user accessible via the interpreter. If + we really wanted to, we would have to parse the .tflite file ourselves. This function is a bit + of a hack, but lets us skip that.""" + + if layer_num == 0: # Regular conv2d Review Comment: Added a clarifying comment. -- 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]
