Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RUNTIME] Implement TVMDSOOp(TensorFlow custom op) for TVM runtime #4459

Merged
merged 44 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
73726a8
Add implementation of TVMDSOOp
tobegit3hub Dec 3, 2019
9c3f732
feat: Update cmake script to work with c++11 and in-repo build
Dec 4, 2019
8361217
feat: Use libtvm as oplib dependency
Dec 4, 2019
ef387fd
fix: Add missing link dependency to libtvm
Dec 4, 2019
41d9b38
Merge branch 'master' into add_tvmdsoop
tobegit3hub Dec 5, 2019
056d4b1
feat: Update tf tvmdso op by review comments
Jan 2, 2020
9fb271c
Fix conflict of master
tobegit3hub Jan 6, 2020
a6d9c28
Merge branch 'fix/add_tvmdsoop_fix_review' into 'feat/add_tvmdsoop'
Jan 6, 2020
69df549
fix: Update with pr comments
Jan 16, 2020
23ce5b2
Merge branch 'feat/UpdatePR' into 'feat/add_tvmdsoop'
Jan 16, 2020
c2e72a6
fix: Fix lint
Jan 16, 2020
e390c49
Merge branch 'fix/FixLint' into 'feat/add_tvmdsoop'
Jan 16, 2020
729a5dc
feat: Add test script and fix gpu shape
Mar 5, 2020
502b03f
Merge branch 'fix/AddTestCaseAndFixGpuShape' into 'feat/add_tvmdsoop'
Mar 5, 2020
7f1b5b3
feat: Add test script and fix gpu shape
Mar 5, 2020
0debcd6
fix: Conditional build tftvm op for gpu
Mar 6, 2020
9a9cf41
Merge branch 'feat/AddTfOpGpuConditionalBuild' into 'feat/add_tvmdsoop'
Mar 6, 2020
f5be2f3
fix: Conditional build tftvm op for gpu
Mar 6, 2020
7eae33d
fix: Fix pylint of tf_op module.py
Mar 9, 2020
b064685
Merge branch 'fix/FixTfdsoOpLint' into 'feat/add_tvmdsoop'
Mar 9, 2020
cd8fd80
fix: Fix pylint of tf_op module.py
Mar 9, 2020
1fc54ec
feat: Conditional enable gpu test for tftvm op
Mar 10, 2020
cfcdc41
Merge branch 'feat/EnableConditionalGpuTfOpTest' into 'feat/add_tvmds…
Mar 10, 2020
b4b9f96
feat: Conditional enable gpu test for tftvm op
Mar 10, 2020
74c3d3d
Merge branch upstream master into feat/add_tvmdsoop
Mar 16, 2020
864c4a5
feat: Add tf_tvmdsoop test script as an app test
Mar 16, 2020
29003e9
Merge branch 'feat/AddTFOpToIntegrateTests' into 'feat/add_tvmdsoop'
Mar 16, 2020
03fbe4c
Merge branch '4pd_add_tvmdsoop' into add_tvmdsoop
Mar 16, 2020
ec05511
fix: Fix gpu/cpu enabled check on tvm in test script
Mar 17, 2020
51ed779
fix: Make tf tvmdso op test script runnable with pytest
Mar 17, 2020
685f7d0
remove unused test script test_tfop_module.py
Mar 17, 2020
ea6328b
fix: Remove pushd & popd in tfdsoop test script
Mar 18, 2020
0ae0942
fix: Upgrade tftvmop use python3 to find TensorFlow
Mar 18, 2020
9594700
fix: Upgrade tftvmop use python3 to find TensorFlow
Mar 18, 2020
8a5d2fb
fix: Change target_link_options to target_link_libraries
Mar 18, 2020
b8fbd2e
fix: Add tftvmop build script's c++ option
Mar 19, 2020
380e1d7
fix: Add tvm library path to tf op test library path
Mar 19, 2020
0b3884d
fix: Debug ci build for tftvm dso op
Mar 19, 2020
9dc89c6
Merge branch 'master' into add_tvmdsoop
tobegit3hub Mar 25, 2020
9fd18b8
fix: Fix cmake error and skip tfop test
Apr 1, 2020
9967748
Merge branch 'add_tvmdsoop' of github.com:4paradigm/incubator-tvm int…
Apr 1, 2020
8ac182f
fix: Fix typo and indentation issues
Apr 2, 2020
9343669
feat: Use TF list input op def
Apr 3, 2020
38af1ce
fix: Fix style and unexpected changes
Apr 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ tvm_option(USE_MSVC_MT "Build with MT" OFF)
tvm_option(USE_MICRO "Build with Micro" OFF)
tvm_option(INSTALL_DEV "Install compiler infrastructure" OFF)
tvm_option(HIDE_PRIVATE_SYMBOLS "Compile with -fvisibility=hidden." OFF)
tvm_option(USE_TF_TVMDSOOP "Build with TensorFlow TVMDSOOp" OFF)

# 3rdparty libraries
tvm_option(DLPACK_PATH "Path to DLPACK" "3rdparty/dlpack/include")
Expand Down Expand Up @@ -259,6 +260,7 @@ include(cmake/modules/contrib/Sort.cmake)
include(cmake/modules/contrib/NNPack.cmake)
include(cmake/modules/contrib/HybridDump.cmake)
include(cmake/modules/contrib/TFLite.cmake)
include(cmake/modules/contrib/TF_TVMDSOOP.cmake)

if(NOT MSVC)
include(CheckCXXCompilerFlag)
Expand Down
34 changes: 34 additions & 0 deletions apps/tf_tvmdsoop/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.
cmake_minimum_required(VERSION 3.2)
project(tf_tvmdsoop C CXX)

set(TFTVM_COMPILE_FLAGS -std=c++11)
tobegit3hub marked this conversation as resolved.
Show resolved Hide resolved
set(BUILD_TVMDSOOP_ONLY ON)
set(CMAKE_CURRENT_SOURCE_DIR ${TVM_ROOT})
set(CMAKE_CURRENT_BINARY_DIR ${TVM_ROOT}/build)

include_directories(${TVM_ROOT}/3rdparty/dlpack/include/)
include_directories(${TVM_ROOT}/3rdparty/dmlc-core/include/)
include_directories(${TVM_ROOT}/include)

link_directories(${TVM_ROOT}/build)

include(${TVM_ROOT}/cmake/util/FindCUDA.cmake)
include(${TVM_ROOT}/cmake/modules/CUDA.cmake)

include(${TVM_ROOT}/cmake/modules/contrib/TF_TVMDSOOP.cmake)
35 changes: 35 additions & 0 deletions apps/tf_tvmdsoop/prepare_and_test_tfop_module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# 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.

TVM_ROOT=$(cd $(dirname $0)/../..; pwd)
echo "TVM_ROOT=${TVM_ROOT}"

export PYTHONPATH=${TVM_ROOT}/python

python3 -c "import tvm; print(tvm.runtime.enabled('gpu'))" | grep -e 1
if [ "$?" -eq 0 ]; then
echo "Build TF_TVMDSOOP with gpu support and execute tests"
CMAKE_OPTIONS="-DUSE_CUDA=ON -DPython3_EXECUTABLE=python3 -DTVM_ROOT=${TVM_ROOT}"

mkdir -p build
cd build; cmake .. ${CMAKE_OPTIONS} && make
cd ..

LD_LIBRARY_PATH=${TVM_ROOT}/build:./build:$LD_LIBRARY_PATH python3 -m pytest -v ./tests
fi

118 changes: 118 additions & 0 deletions apps/tf_tvmdsoop/tests/test_tfop_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python

# 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.
"""Test script for tf op module"""
import tempfile
import os
import logging
import tensorflow as tf
import numpy as np
import tvm
from tvm import te
from tvm.contrib import tf_op


def test_use_tvmdso_op():
"""main test function"""

def export_cpu_add_lib():
"""create cpu add op lib"""
n = te.var("n")
ph_a = te.placeholder((n,), name='ph_a')
ph_b = te.placeholder((n,), name='ph_b')
ph_c = te.compute(ph_a.shape, lambda i: ph_a[i] + ph_b[i], name='ph_c')
sched = te.create_schedule(ph_c.op)
fadd_dylib = tvm.build(sched, [ph_a, ph_b, ph_c], "c", name="vector_add")
lib_path = tempfile.mktemp("tvm_add_dll.so")
fadd_dylib.export_library(lib_path)
return lib_path


def export_gpu_add_lib():
"""create gpu add op lib"""
n = te.var("n")
ph_a = te.placeholder((n,), name='ph_a')
ph_b = te.placeholder((n,), name='ph_b')
ph_c = te.compute(ph_a.shape, lambda i: ph_a[i] + ph_b[i], name='ph_c')
sched = te.create_schedule(ph_c.op)
b_axis, t_axis = sched[ph_c].split(ph_c.op.axis[0], factor=64)
sched[ph_c].bind(b_axis, te.thread_axis("blockIdx.x"))
sched[ph_c].bind(t_axis, te.thread_axis("threadIdx.x"))
fadd_dylib = tvm.build(sched, [ph_a, ph_b, ph_c], "cuda", name="vector_add")
lib_path = tempfile.mktemp("tvm_add_cuda_dll.so")
fadd_dylib.export_library(lib_path)
return lib_path


def test_add(session, lib_path, tf_device):
"""test add lib with TensorFlow wrapper"""
module = tf_op.OpModule(lib_path)

left = tf.placeholder("float32", shape=[4])
right = tf.placeholder("float32", shape=[4])

feed_dict = {left: [1.0, 2.0, 3.0, 4.0], right: [5.0, 6.0, 7.0, 8.0]}
expect = np.asarray([6.0, 8.0, 10.0, 12.0])

add1 = module.func("vector_add", output_shape=[4], output_dtype="float")
add2 = module.func("vector_add", output_shape=tf.shape(left), output_dtype="float")
add3 = module.func("vector_add", output_shape=[tf.shape(left)[0]], output_dtype="float")

with tf.device(tf_device):
output1 = session.run(add1(left, right), feed_dict)
np.testing.assert_equal(output1, expect)

output2 = session.run(add2(left, right), feed_dict)
np.testing.assert_equal(output2, expect)

output3 = session.run(add3(left, right), feed_dict)
np.testing.assert_equal(output3, expect)


def cpu_test(session):
"""test function for cpu"""
cpu_lib = None
try:
cpu_lib = export_cpu_add_lib()
test_add(session, cpu_lib, "/cpu:0")
finally:
if cpu_lib is not None:
os.remove(cpu_lib)


def gpu_test(session):
"""test function for gpu"""
gpu_lib = None
try:
gpu_lib = export_gpu_add_lib()
test_add(session, gpu_lib, "/gpu:0")
finally:
if gpu_lib is not None:
os.remove(gpu_lib)

with tf.Session() as session:
if tvm.runtime.enabled("cpu"):
logging.info("Test TensorFlow op on cpu kernel")
cpu_test(session)
if tvm.runtime.enabled("gpu"):
logging.info("Test TensorFlow op on gpu kernel")
gpu_test(session)


if __name__ == "__main__":
test_use_tvmdso_op()
4 changes: 4 additions & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,7 @@ set(USE_EXAMPLE_EXT_RUNTIME OFF)

# Whether use Thrust
set(USE_THRUST OFF)

# Whether to build the TensorFlow TVMDSOOp module
set(USE_TF_TVMDSOOP OFF)

58 changes: 58 additions & 0 deletions cmake/modules/contrib/TF_TVMDSOOP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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.

if(NOT USE_TF_TVMDSOOP STREQUAL "OFF")
find_package(Python3 COMPONENTS Interpreter)

execute_process(COMMAND ${Python3_EXECUTABLE} -c "import tensorflow as tf; print(' '.join(tf.sysconfig.get_compile_flags()))"
OUTPUT_VARIABLE TF_COMPILE_FLAGS_STR
RESULT_VARIABLE TF_STATUS)
if (NOT ${TF_STATUS} EQUAL 0)
message(FATAL_ERROR "Fail to get TensorFlow compile flags")
endif()

if(NOT USE_CUDA STREQUAL "OFF")
add_definitions(-DTF_TVMDSOOP_ENABLE_GPU)
endif()

execute_process(COMMAND ${Python3_EXECUTABLE} -c "import tensorflow as tf; print(' '.join(tf.sysconfig.get_link_flags()))"
OUTPUT_VARIABLE TF_LINK_FLAGS_STR
RESULT_VARIABLE TF_STATUS)
if (NOT ${TF_STATUS} EQUAL 0)
message(FATAL_ERROR "Fail to get TensorFlow link flags")
endif()

string(REGEX REPLACE "\n" " " TF_FLAGS "${TF_COMPILE_FLAGS} ${TF_LINK_FLAGS}")
separate_arguments(TF_COMPILE_FLAGS UNIX_COMMAND ${TF_COMPILE_FLAGS_STR})
separate_arguments(TF_LINK_FLAGS UNIX_COMMAND ${TF_LINK_FLAGS_STR})


set(OP_LIBRARY_NAME tvm_dso_op)
file(GLOB_RECURSE TFTVM_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/src/contrib/tf_op/*.cc)
add_library(${OP_LIBRARY_NAME} SHARED ${TFTVM_SRCS})
set_target_properties(${OP_LIBRARY_NAME} PROPERTIES PREFIX "")
set(TFTVM_LINK_FLAGS -ltvm -L${CMAKE_CURRENT_BINARY_DIR})

if (NOT BUILD_TVMDSOOP_ONLY STREQUAL "ON")
add_dependencies(${OP_LIBRARY_NAME} tvm)
endif()

target_compile_options(${OP_LIBRARY_NAME} PUBLIC ${TFTVM_COMPILE_FLAGS} ${TF_COMPILE_FLAGS})
target_link_libraries(${OP_LIBRARY_NAME} PUBLIC ${TFTVM_LINK_FLAGS} ${TF_LINK_FLAGS})

endif()

20 changes: 20 additions & 0 deletions python/tvm/contrib/tf_op/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.
"""Module container of TensorFlow TVMDSO op"""
from . import module

OpModule = module.OpModule
Loading