masahi commented on code in PR #13135: URL: https://github.com/apache/tvm/pull/13135#discussion_r1000273210
########## tests/python/contrib/test_hexagon/metaschedule_e2e/test_resnet50_fp16.py: ########## @@ -0,0 +1,126 @@ +# 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. +import os +import pytest +import tempfile + +import numpy as np + +import tvm.testing +from tvm import relay +from tvm import meta_schedule as ms +from tvm.contrib.hexagon.meta_schedule import get_hexagon_local_builder, get_hexagon_rpc_runner +from tvm.relay.backend import Executor +from ..infrastructure import get_hexagon_target + + +target = get_hexagon_target("v69") +target_llvm = tvm.target.Target("llvm") +model_json = "resnet50_fp16.json" +model_params = "resnet50_fp16.params" + + +def convert_conv2d_layout(mod, desired_layouts): + with tvm.transform.PassContext(opt_level=3): + seq = tvm.transform.Sequential([relay.transform.ConvertLayout(desired_layouts)]) + return seq(mod) + + [email protected]("End-to-end tuning is skipped on CI.") [email protected]_hexagon +def test_resnet50(hexagon_launcher): + if not os.path.exists(model_json): + pytest.skip(msg="Run python export_models.py first.") + + with open(model_json, "r") as fi: + mod = tvm.ir.load_json(fi.read()) + + with open(model_params, "rb") as fi: + params = relay.load_param_dict(fi.read()) + + mod = convert_conv2d_layout(mod, {"nn.conv2d": ["NHWC", "HWIO"]}) + + inp = np.random.randn(1, 3, 224, 224).astype("float32") + input_name = "image" + + executor = Executor("graph", {"link-params": True}) + # This line is necessary for link-params to take effect during + # task extraction and relay.build(...). + mod = mod.with_attr("executor", executor) + + with tempfile.TemporaryDirectory() as work_dir: + database = ms.relay_integration.tune_relay( + mod=mod, + target=target, + params=params, + work_dir=work_dir, + # for faster tuning + max_trials_global=20000, + max_trials_per_task=8, + num_trials_per_iter=8, + strategy="replay-trace", + # max_trials_global=20000, + # num_trials_per_iter=32, + # max_trials_per_task=128, + # strategy="evolutionary", + builder=get_hexagon_local_builder(), + runner=get_hexagon_rpc_runner(hexagon_launcher, number=20), + # Without this, the same workloads with different constant weights + # are treated as distinct tuning tasks. + module_equality="ignore-ndarray", + ) + + hexagon_lowered = ms.relay_integration.compile_relay( + database=database, + mod=mod, + target=target, + params=params, + ) + + with tvm.transform.PassContext(opt_level=3): + llvm_lowered = tvm.relay.build( + mod, + tvm.target.Target(target_llvm, host=target_llvm), + params=params, + ) + + llvm_graph_mod = tvm.contrib.graph_executor.GraphModule(llvm_lowered["default"](tvm.cpu(0))) + llvm_graph_mod.set_input(input_name, inp.copy()) + llvm_graph_mod.run() + ref_result = llvm_graph_mod.get_output(0).numpy() + + with hexagon_launcher.start_session() as session: + graph_mod = session.get_executor_from_factory(hexagon_lowered) + graph_mod.set_input(input_name, inp.copy()) + + graph_mod.run() + hexagon_output = graph_mod.get_output(0).numpy() + + print( + "max and mean abs difference with the reference:", + np.max(np.abs(ref_result - hexagon_output)), + np.mean(np.abs(ref_result - hexagon_output)), + ) Review Comment: There is non-trivial accuracy difference between x86 and HVX fp16. Added a sample output and assert check with a fairly loose bound. -- 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]
