|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +"""Test script for tf op module""" |
| 20 | +import tempfile |
| 21 | +import os |
| 22 | +import logging |
| 23 | +import tensorflow as tf |
| 24 | +import numpy as np |
| 25 | +import tvm |
| 26 | +from tvm import te |
| 27 | +from tvm.contrib import tf_op |
| 28 | + |
| 29 | + |
| 30 | +def test_use_tvmdso_op(): |
| 31 | + """main test function""" |
| 32 | + |
| 33 | + def export_cpu_add_lib(): |
| 34 | + """create cpu add op lib""" |
| 35 | + n = te.var("n") |
| 36 | + ph_a = te.placeholder((n,), name='ph_a') |
| 37 | + ph_b = te.placeholder((n,), name='ph_b') |
| 38 | + ph_c = te.compute(ph_a.shape, lambda i: ph_a[i] + ph_b[i], name='ph_c') |
| 39 | + sched = te.create_schedule(ph_c.op) |
| 40 | + fadd_dylib = tvm.build(sched, [ph_a, ph_b, ph_c], "c", name="vector_add") |
| 41 | + lib_path = tempfile.mktemp("tvm_add_dll.so") |
| 42 | + fadd_dylib.export_library(lib_path) |
| 43 | + return lib_path |
| 44 | + |
| 45 | + |
| 46 | + def export_gpu_add_lib(): |
| 47 | + """create gpu add op lib""" |
| 48 | + n = te.var("n") |
| 49 | + ph_a = te.placeholder((n,), name='ph_a') |
| 50 | + ph_b = te.placeholder((n,), name='ph_b') |
| 51 | + ph_c = te.compute(ph_a.shape, lambda i: ph_a[i] + ph_b[i], name='ph_c') |
| 52 | + sched = te.create_schedule(ph_c.op) |
| 53 | + b_axis, t_axis = sched[ph_c].split(ph_c.op.axis[0], factor=64) |
| 54 | + sched[ph_c].bind(b_axis, te.thread_axis("blockIdx.x")) |
| 55 | + sched[ph_c].bind(t_axis, te.thread_axis("threadIdx.x")) |
| 56 | + fadd_dylib = tvm.build(sched, [ph_a, ph_b, ph_c], "cuda", name="vector_add") |
| 57 | + lib_path = tempfile.mktemp("tvm_add_cuda_dll.so") |
| 58 | + fadd_dylib.export_library(lib_path) |
| 59 | + return lib_path |
| 60 | + |
| 61 | + |
| 62 | + def test_add(session, lib_path, tf_device): |
| 63 | + """test add lib with TensorFlow wrapper""" |
| 64 | + module = tf_op.OpModule(lib_path) |
| 65 | + |
| 66 | + left = tf.placeholder("float32", shape=[4]) |
| 67 | + right = tf.placeholder("float32", shape=[4]) |
| 68 | + |
| 69 | + feed_dict = {left: [1.0, 2.0, 3.0, 4.0], right: [5.0, 6.0, 7.0, 8.0]} |
| 70 | + expect = np.asarray([6.0, 8.0, 10.0, 12.0]) |
| 71 | + |
| 72 | + add1 = module.func("vector_add", output_shape=[4], output_dtype="float") |
| 73 | + add2 = module.func("vector_add", output_shape=tf.shape(left), output_dtype="float") |
| 74 | + add3 = module.func("vector_add", output_shape=[tf.shape(left)[0]], output_dtype="float") |
| 75 | + |
| 76 | + with tf.device(tf_device): |
| 77 | + output1 = session.run(add1(left, right), feed_dict) |
| 78 | + np.testing.assert_equal(output1, expect) |
| 79 | + |
| 80 | + output2 = session.run(add2(left, right), feed_dict) |
| 81 | + np.testing.assert_equal(output2, expect) |
| 82 | + |
| 83 | + output3 = session.run(add3(left, right), feed_dict) |
| 84 | + np.testing.assert_equal(output3, expect) |
| 85 | + |
| 86 | + |
| 87 | + def cpu_test(session): |
| 88 | + """test function for cpu""" |
| 89 | + cpu_lib = None |
| 90 | + try: |
| 91 | + cpu_lib = export_cpu_add_lib() |
| 92 | + test_add(session, cpu_lib, "/cpu:0") |
| 93 | + finally: |
| 94 | + if cpu_lib is not None: |
| 95 | + os.remove(cpu_lib) |
| 96 | + |
| 97 | + |
| 98 | + def gpu_test(session): |
| 99 | + """test function for gpu""" |
| 100 | + gpu_lib = None |
| 101 | + try: |
| 102 | + gpu_lib = export_gpu_add_lib() |
| 103 | + test_add(session, gpu_lib, "/gpu:0") |
| 104 | + finally: |
| 105 | + if gpu_lib is not None: |
| 106 | + os.remove(gpu_lib) |
| 107 | + |
| 108 | + with tf.Session() as session: |
| 109 | + if tvm.runtime.enabled("cpu"): |
| 110 | + logging.info("Test TensorFlow op on cpu kernel") |
| 111 | + cpu_test(session) |
| 112 | + if tvm.runtime.enabled("gpu"): |
| 113 | + logging.info("Test TensorFlow op on gpu kernel") |
| 114 | + gpu_test(session) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + test_use_tvmdso_op() |
0 commit comments