|
| 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 | + |
| 18 | +import numpy as np |
| 19 | +import mxnet as mx |
| 20 | +from mxnet import gluon |
| 21 | +import nnvm |
| 22 | +import tvm |
| 23 | +from tvm.contrib import graph_runtime |
| 24 | + |
| 25 | + |
| 26 | +def test_avg_pool2d(): |
| 27 | + |
| 28 | + # Generate the data |
| 29 | + np.random.seed(0) |
| 30 | + input_shape = [1, 1, 28, 28] |
| 31 | + output_shape = [1, 10] |
| 32 | + data = np.random.random(input_shape).astype('float32') |
| 33 | + |
| 34 | + # Baseline model in MXNet |
| 35 | + net = gluon.nn.HybridSequential() |
| 36 | + with net.name_scope(): |
| 37 | + net.add(gluon.nn.AvgPool2D(pool_size=3, strides=1, padding=1)) |
| 38 | + net.add(gluon.nn.Dense(10)) |
| 39 | + net.collect_params().initialize(mx.init.Xavier(), ctx=mx.cpu()) |
| 40 | + net.hybridize() |
| 41 | + baseline_input = mx.nd.array(data, ctx=mx.cpu()) |
| 42 | + baseline_output = net(baseline_input).asnumpy() |
| 43 | + |
| 44 | + # Compiled model |
| 45 | + sym, params = nnvm.frontend.from_mxnet(net) |
| 46 | + target = tvm.target.cuda() |
| 47 | + with nnvm.compiler.build_config(opt_level=3, ext_accel='tensorrt'): |
| 48 | + graph, lib, params = nnvm.compiler.build(sym, target, |
| 49 | + shape={'data': input_shape}, |
| 50 | + params=params) |
| 51 | + compiled_model = graph_runtime.create(graph, lib, tvm.gpu()) |
| 52 | + compiled_input = tvm.nd.array(data, ctx=tvm.gpu()) |
| 53 | + compiled_model.set_input('data', compiled_input) |
| 54 | + compiled_model.set_input(**params) |
| 55 | + compiled_model.run() |
| 56 | + compiled_output = compiled_model.get_output(0, tvm.nd.empty(output_shape)).asnumpy() |
| 57 | + |
| 58 | + # Compare outputs |
| 59 | + np.testing.assert_almost_equal(baseline_output, compiled_output, decimal=3) |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + test_avg_pool2d() |
0 commit comments