|
| 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 | +# Copyright (c) 2017 by Contributors |
| 19 | +# brief Example code on load and run TVM module.s |
| 20 | +# file python_deploy.py |
| 21 | + |
| 22 | +import tvm |
| 23 | +import numpy as np |
| 24 | + |
| 25 | +def verify(mod, fname): |
| 26 | + # Get the function from the module |
| 27 | + f = mod.get_function(fname) |
| 28 | + # Use tvm.nd.array to convert numpy ndarray to tvm |
| 29 | + # NDArray type, so that function can be invoked normally |
| 30 | + N = 10 |
| 31 | + x = tvm.nd.array(np.arange(N, dtype=np.float32)) |
| 32 | + y = tvm.nd.array(np.zeros(N, dtype=np.float32)) |
| 33 | + # Invoke the function |
| 34 | + f(x, y) |
| 35 | + np_x = x.asnumpy() |
| 36 | + np_y = y.asnumpy() |
| 37 | + # Verify correctness of function |
| 38 | + assert(np.all([xi+1 == yi for xi, yi in zip(np_x, np_y)])) |
| 39 | + print("Finish verification...") |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + # The normal dynamic loading method for deployment |
| 44 | + mod_dylib = tvm.module.load("lib/test_addone_dll.so") |
| 45 | + print("Verify dynamic loading from test_addone_dll.so") |
| 46 | + verify(mod_dylib, "addone") |
| 47 | + # There might be methods to use the system lib way in |
| 48 | + # python, but dynamic loading is good enough for now. |
0 commit comments