Make FakeLowP tests work (#36525)

Summary:
Make the e2e FakeLowP python tests work with Glow lowering in OSS environment. Added a README.md as a guideline.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/36525

Reviewed By: hyuen

Differential Revision: D21004706

Pulled By: yinghai

fbshipit-source-id: d182152e4a1a3368640bd7872cb9ea4d4bff4b02
This commit is contained in:
Yinghai Lu 2020-04-13 20:10:41 -07:00 committed by Facebook GitHub Bot
parent 8544591f5a
commit eb00bac2b5
5 changed files with 70 additions and 26 deletions

View File

@ -1,5 +1,5 @@
if(USE_FAKELOWP)
message(STATUS "Including FAKELOWP operators")
message(STATUS "Including FakeLowP operators")
# ---[ CPU files.
file(GLOB_RECURSE tmp *.cc)
@ -11,32 +11,22 @@ if(USE_FAKELOWP)
# We will only build the perf kernel files if the compiler supports avx2
# extensions.
if(CAFFE2_COMPILER_SUPPORTS_AVX2_EXTENSIONS)
add_library(Caffe2_fakelowp_ops STATIC ${FAKELOWP_CPU_SRCS})
add_dependencies(Caffe2_fakelowp_ops fbgemm Caffe2_PROTO c10)
target_include_directories(Caffe2_fakelowp_ops BEFORE
add_library(caffe2_fakelowp_ops OBJECT ${FAKELOWP_CPU_SRCS})
add_dependencies(caffe2_fakelowp_ops fbgemm Caffe2_PROTO c10)
target_include_directories(caffe2_fakelowp_ops BEFORE
PRIVATE $<BUILD_INTERFACE:${FBGEMM_SOURCE_DIR}/include>)
if(MSVC AND NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
target_compile_options(Caffe2_fakelowp_ops
PRIVATE "/arch:AVX2"
PRIVATE "/D__FMA__"
PRIVATE "/D__F16C__")
if(MSVC)
set_property(SOURCE ${FAKELOWP_CPU_SRCS}
APPEND_STRING PROPERTY COMPILE_FLAGS " /arch:AVX2 ")
else()
target_compile_options(Caffe2_fakelowp_ops
PRIVATE "-mavx2"
PRIVATE "-mfma"
PRIVATE "-mavx"
PRIVATE "-mf16c")
set_property(SOURCE ${FAKELOWP_CPU_SRCS}
APPEND_STRING PROPERTY COMPILE_FLAGS " -mavx2 -mfma -mf16c -mxsave ")
endif()
caffe2_interface_library(
Caffe2_fakelowp_ops Caffe2_fakelowp_ops_interface)
list(APPEND
Caffe2_DEPENDENCY_WHOLE_LINK_LIBS
"Caffe2_fakelowp_ops_interface")
set(Caffe2_CPU_SRCS ${Caffe2_CPU_SRCS}
$<TARGET_OBJECTS:caffe2_fakelowp_ops>)
endif()
# ---[ Send the lists to the parent scope.
set(Caffe2_CPU_SRCS ${Caffe2_CPU_SRCS} PARENT_SCOPE)
else()
message(STATUS "Excluding FakeLowP operators")
endif()
set(Caffe2_DEPENDENCY_WHOLE_LINK_LIBS
${Caffe2_DEPENDENCY_WHOLE_LINK_LIBS}
PARENT_SCOPE)

View File

@ -0,0 +1,41 @@
# How to run FakeLowP vs Glow tests
This was tested on Ubuntu 16.04 LTS but should work in general Linux system.
## Install and Build Glow
Follow https://github.com/pytorch/glow/blob/master/README.md to install the dependency of Glow. Then at glow root run
```
mkdir build && cd build
cmake -G Ninja -DGLOW_BUILD_ONNXIFI_DYNLIB=ON ..
ninja all
```
Note that here you probably want to add other flags like `-DGLOW_WITH_NNPI=1` to enable specific backend if you have the flow setup.
Once built successfully, you will get an dynamic library at `build/lib/Onnxifi/libonnxifi.so`. We will use it later.
## Install and Build PyTorch
Follow https://github.com/pytorch/pytorch/blob/master/README.md to install the dependency of PyTorch. It might be easy to
setup a python virtualenv or conda. And please use Python > 3.5.2 because hypothesis library will expose a bug in Python which
is fixed after 3.5.2. Something like 3.7 mighr be good enough. Here I give a virtualenv flow:
```
python3.7 -m venv venv3
source venv3/bin/active
cd pytorch
pip install -r requirements.txt
```
You probably need to install gflags-dev too with
```
sudo apt-get install libgflags-dev
```
Once you have all the dependency libs installed, build PyTorch with FakeLowP op support
```
USE_CUDA=0 USE_ROCM=0 USE_FAKELOWP=ON DEBUG=1 CMAKE_BUILD_TYPE=Debug USE_GFLAGS=1 USE_GLOG=1 USE_MKLDNN=0 python setup.py install
```
The key options here are `USE_FAKELOWP=ON` which enables building of FakeLowP operators and `USE_GFLAGS=1` which enables gflags as we
use gflags in Glow to pass options. Other flags are mostl for fast build time and debug purpose.
## Run the test
You can now run the tests with command like the following when you are inside the virtual python env:
```
OSS_ONNXIFI_LIB=${PATH_TO_GLOW}/build/lib/Onnxifi/libonnxifi.so python pytorch/caffe2/contrib/fakelowp/test/test_sls_nnpi_fp16.py
```

View File

@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import time
import unittest
# Must happen before importing caffe2.python.*
import caffe2.python.fakelowp.init_shared_libs # noqa
@ -8,7 +9,6 @@ import numpy as np
from caffe2.proto import caffe2_pb2
from caffe2.python import core, workspace
from caffe2.python.onnx.onnxifi import onnxifi_caffe2_net
from caffe2.python.onnx.tests.test_utils import TestCase
from caffe2.python.fakelowp.test_utils import print_test_debug_info
@ -24,8 +24,8 @@ GLOW_MATMUL_ATOL = 1e-5
GLOW_MATMUL_RTOL = 1e-3
class SparseLengthsSumTest(TestCase):
def Test_SLS_NonQuantized_fp16(self):
class SparseLengthsSumTest(unittest.TestCase):
def Skip_test_SLS_NonQuantized_fp16(self):
N = 20000
DIM = 64
D = (4 * np.random.random_sample((N, DIM)) + 1).astype(np.float32)
@ -614,3 +614,6 @@ class SparseLengthsSumTest(TestCase):
},
)
assert 0
if __name__ == '__main__':
unittest.main()

View File

@ -1 +1,10 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import ctypes
import os
if 'OSS_ONNXIFI_LIB' in os.environ:
lib = os.environ['OSS_ONNXIFI_LIB']
print("Loading ONNXIFI lib: ".format(lib))
ctypes.CDLL(lib, ctypes.RTLD_GLOBAL)

View File

@ -1229,6 +1229,7 @@ if(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO AND NOT INTERN_DISABLE_ONNX)
if(ONNX_ML)
add_definitions(-DONNX_ML=1)
endif()
add_definitions(-DONNXIFI_ENABLE_EXT=1)
# Add op schemas in "ai.onnx.pytorch" domain
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/../caffe2/onnx/torch_ops")
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../third_party/onnx EXCLUDE_FROM_ALL)