|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +""" Test Relay Integration """ |
| 18 | + |
| 19 | +import tempfile |
| 20 | +import numpy as np |
| 21 | + |
| 22 | +import tvm |
| 23 | +from tvm import ansor, relay |
| 24 | +import tvm.contrib.graph_runtime as runtime |
| 25 | + |
| 26 | +from test_ansor_common import get_tiled_matmul |
| 27 | + |
| 28 | +def dense_graph(N, dtype="float32"): |
| 29 | + ori_data = relay.var("data", shape=(N, N), dtype=dtype) |
| 30 | + weight = relay.var("weight", shape=(N, N), dtype=dtype) |
| 31 | + data = relay.multiply(ori_data, relay.const(2, dtype=dtype)) |
| 32 | + dense = relay.nn.dense(data, weight, out_dtype=dtype) |
| 33 | + dense = relay.add(dense, weight) |
| 34 | + dense = relay.nn.dense(dense, weight, out_dtype=dtype) |
| 35 | + return ori_data, weight, dense |
| 36 | + |
| 37 | +def test_dense_integration(): |
| 38 | + N = 128 |
| 39 | + data, weight, dense = dense_graph(N) |
| 40 | + mod = relay.Function([data, weight], dense) |
| 41 | + mod = tvm.IRModule.from_expr(mod) |
| 42 | + |
| 43 | + ctx = tvm.context("llvm") |
| 44 | + target = tvm.target.create("llvm") |
| 45 | + d = tvm.nd.array(np.random.uniform(size=(N, N)).astype(data.type_annotation.dtype), ctx) |
| 46 | + w = tvm.nd.array(np.random.uniform(size=(N, N)).astype(weight.type_annotation.dtype), ctx) |
| 47 | + workloads, wkl_weights = ansor.extract_from_program(mod, {}, target=target) |
| 48 | + |
| 49 | + assert len(workloads) == 2 |
| 50 | + assert len(wkl_weights) == 2 |
| 51 | + |
| 52 | + tasks = [] |
| 53 | + for wkl_key in workloads: |
| 54 | + dag = ansor.workload_key_to_dag(wkl_key) |
| 55 | + tasks.append(ansor.SearchTask(dag, wkl_key, target)) |
| 56 | + |
| 57 | + assert str(tasks[0].compute_dag) == "placeholder = PLACEHOLDER [128, 128]\n" + \ |
| 58 | + "placeholder = PLACEHOLDER [128, 128]\n" + \ |
| 59 | + "compute(z, y, x) += (placeholder[z, ((k*16) + x)]*placeholder[y, ((k*16) + x)])\n" + \ |
| 60 | + "compute(y, x) += compute[y, x, kk]\n" |
| 61 | + |
| 62 | + assert str(tasks[1].compute_dag) == "placeholder = PLACEHOLDER [128, 128]\n" + \ |
| 63 | + "placeholder = PLACEHOLDER [128, 128]\n" + \ |
| 64 | + "compute(z, y, x) += (placeholder[z, ((k*16) + x)]*placeholder[y, ((k*16) + x)])\n" + \ |
| 65 | + "compute(y, x) += compute[y, x, kk]\n" + \ |
| 66 | + "T_add(ax0, ax1) = (compute[ax0, ax1] + placeholder[ax0, ax1])\n" |
| 67 | + |
| 68 | + tuner = ansor.SimpleTaskScheduler(tasks) |
| 69 | + measure_ctx = ansor.LocalRPCMeasureContext() |
| 70 | + with tempfile.NamedTemporaryFile() as fp: |
| 71 | + tuner.tune(ansor.TuneOption(n_trials=4, runner=measure_ctx.runner, |
| 72 | + measure_callbacks=[ansor.LogToFile(fp.name)])) |
| 73 | + with ansor.apply_history_best(fp.name): |
| 74 | + with relay.build_config(opt_level=3): |
| 75 | + graph, lib, opt_params = relay.build_module.build( |
| 76 | + mod, target=target) |
| 77 | + |
| 78 | + m = runtime.create(graph, lib, ctx) |
| 79 | + m.set_input('data', d) |
| 80 | + m.set_input('weight', w) |
| 81 | + m.run() |
| 82 | + res = m.get_output(0) |
| 83 | + if measure_ctx: |
| 84 | + del measure_ctx |
| 85 | + |
| 86 | + d = d.asnumpy() |
| 87 | + d = d * 2 |
| 88 | + w = w.asnumpy() |
| 89 | + d = np.dot(d, np.transpose(w)) |
| 90 | + d = d + w |
| 91 | + d = np.dot(d, np.transpose(w)) |
| 92 | + |
| 93 | + tvm.testing.assert_allclose(res.asnumpy(), d, rtol=1e-5) |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + test_dense_integration() |
0 commit comments