From a19202c399b8e40adc96739df946d3bdb26eefac Mon Sep 17 00:00:00 2001 From: hzli-ucas Date: Sun, 17 Mar 2019 20:28:21 +0800 Subject: [PATCH 1/5] fix errors in vs2017 building --- CMakeLists.txt | 2 +- include/ctc.h | 8 ++++---- pytorch_binding/setup.py | 13 +++++-------- pytorch_binding/src/binding.cpp | 2 +- src/ctc_entrypoint.cu | 2 +- tests/test.h | 2 +- 6 files changed, 13 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5ec1c9..79a5737 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,7 @@ IF (CUDA_VERSION GREATER 7.6) ENDIF() IF (CUDA_VERSION GREATER 8.9) - set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -gencode arch=compute_70,code=sm_70") + #set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -gencode arch=compute_70,code=sm_70") ENDIF() if (NOT APPLE) diff --git a/include/ctc.h b/include/ctc.h index a2d3d2d..c2036b9 100644 --- a/include/ctc.h +++ b/include/ctc.h @@ -22,13 +22,13 @@ typedef enum { } ctcStatus_t; /** Returns a single integer which specifies the API version of the warpctc library */ -int get_warpctc_version(); +__declspec(dllexport) int get_warpctc_version(); /** Returns a string containing a description of status that was passed in * \param[in] status identifies which string should be returned * \return C style string containing the text description * */ -const char* ctcGetStatusString(ctcStatus_t status); +__declspec(dllexport) const char* ctcGetStatusString(ctcStatus_t status); typedef enum { CTC_CPU = 0, @@ -91,7 +91,7 @@ struct ctcOptions { * \return Status information * * */ -ctcStatus_t compute_ctc_loss(const float* const activations, +__declspec(dllexport) ctcStatus_t compute_ctc_loss(const float* const activations, float* gradients, const int* const flat_labels, const int* const label_lengths, @@ -120,7 +120,7 @@ ctcStatus_t compute_ctc_loss(const float* const activations, * * \return Status information **/ -ctcStatus_t get_workspace_size(const int* const label_lengths, +__declspec(dllexport) ctcStatus_t get_workspace_size(const int* const label_lengths, const int* const input_lengths, int alphabet_size, int minibatch, ctcOptions info, diff --git a/pytorch_binding/setup.py b/pytorch_binding/setup.py index 4a606b0..da4f6cc 100644 --- a/pytorch_binding/setup.py +++ b/pytorch_binding/setup.py @@ -7,19 +7,16 @@ import torch extra_compile_args = ['-std=c++11', '-fPIC'] -warp_ctc_path = "../build" +warp_ctc_path = "../build/Release" -if platform.system() == 'Darwin': - lib_ext = ".dylib" -else: - lib_ext = ".so" +lib_ext = ".dll" if "WARP_CTC_PATH" in os.environ: warp_ctc_path = os.environ["WARP_CTC_PATH"] -if not os.path.exists(os.path.join(warp_ctc_path, "libwarpctc" + lib_ext)): - print(("Could not find libwarpctc.so in {}.\n" +if not os.path.exists(os.path.join(warp_ctc_path, "warpctc" + lib_ext)): + print(("Could not find warpctc.dll in {}.\n" "Build warp-ctc and set WARP_CTC_PATH to the location of" - " libwarpctc.so (default is '../build')").format(warp_ctc_path)) + " warpctc.dll (default is '%s')").format(warp_ctc_path)) sys.exit(1) include_dirs = [os.path.realpath('../include')] diff --git a/pytorch_binding/src/binding.cpp b/pytorch_binding/src/binding.cpp index 3bb1de8..7863261 100644 --- a/pytorch_binding/src/binding.cpp +++ b/pytorch_binding/src/binding.cpp @@ -11,7 +11,7 @@ #include "ATen/cuda/CUDAEvent.h" #include "THC.h" - extern THCState* state; + THCState* state = at::globalContext().lazyInitCUDA(); #endif #include "ctc.h" diff --git a/src/ctc_entrypoint.cu b/src/ctc_entrypoint.cu index 3876efa..133b696 120000 --- a/src/ctc_entrypoint.cu +++ b/src/ctc_entrypoint.cu @@ -1 +1 @@ -ctc_entrypoint.cpp \ No newline at end of file +#include "ctc_entrypoint.cpp" \ No newline at end of file diff --git a/tests/test.h b/tests/test.h index f1ba986..7bbfeff 100644 --- a/tests/test.h +++ b/tests/test.h @@ -4,7 +4,7 @@ #include #include #include - +#include #include From 573f8dd1559dd7ce6ffe820be4f6332378416c39 Mon Sep 17 00:00:00 2001 From: hzli-ucas <43426126+hzli-ucas@users.noreply.github.com> Date: Mon, 18 Mar 2019 00:10:19 +0800 Subject: [PATCH 2/5] update readme.md for vs2017 branch --- README.md | 122 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 101 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2d0d77c..a745fd4 100644 --- a/README.md +++ b/README.md @@ -3,37 +3,37 @@ [![Build Status](https://travis-ci.org/SeanNaren/warp-ctc.svg?branch=pytorch_bindings)](https://travis-ci.org/SeanNaren/warp-ctc) This is an extension onto the original repo found [here](https://github.com/baidu-research/warp-ctc). +This is a branch for Visual Studio 2017 building, with the errors fixed while installing from pytorch_bindings branch. The installation has been successfully performed in the following environment: +>Win10 cuda10.1 anaconda3 python3.7 pytorch1.0 vs2017 -## Installation +Most of our modifications follow [amberblade's](https://github.com/amberblade/warp-ctc) (It is a Windows compatible version of [SeanNaren's work](https://github.com/SeanNaren/warp-ctc), but cannot work with pytorch1.0). In case you should modify it yourself, all the necessary modifications can be viewed in the commit [fix errors in vs2017 building](https://github.com/hzli-ucas/warp-ctc/commit/a19202c399b8e40adc96739df946d3bdb26eefac). You can also refer to "Specific Modifications" for all the modifications and the error messages that would have be encountered during the installation without the modifications. -Install [PyTorch](https://github.com/pytorch/pytorch#installation) v0.4. +## Installation -`WARP_CTC_PATH` should be set to the location of a built WarpCTC -(i.e. `libwarpctc.so`). This defaults to `../build`, so from within a -new warp-ctc clone you could build WarpCTC like this: +Install [PyTorch](https://github.com/pytorch/pytorch#installation) v1.0 or higher. -```bash -git clone https://github.com/SeanNaren/warp-ctc.git +From within a new warp-ctc clone you could build WarpCTC like this: +```cmd +git clone -b vs2017 https://github.com/hzli-ucas/warp-ctc.git cd warp-ctc -mkdir build; cd build -cmake .. -make +mkdir build +cd build +cmake -G "Visual Studio 15 2017 Win64" .. ``` -Now install the bindings: -```bash -cd pytorch_binding +Open the `warp-ctc/build/ctc_release.sln` with vs2017, and build the project `ALL_BUILD` (Release x64). Then install the bindings: +```cmd +cd ../pytorch_binding python setup.py install ``` -If you try the above and get a dlopen error on OSX with anaconda3 (as recommended by pytorch): -```bash -cd ../pytorch_binding -python setup.py install -cd ../build -cp libwarpctc.dylib /Users/$WHOAMI/anaconda3/lib +At last, copy `warp-ctc/build/Release/warpctc.dll` to the path where you install the warpctc. For example, for the installation performed with Anaconda3 within an environment named "pytorch", the path oughts to be: `Anaconda3/envs/pytorch/Lib/site-packages/warpctc_pytorch-0.1-py3.7-win-amd64.egg/warpctc_pytorch/` + +You could use the following commands to check that warpctc_pytorch has been successfully installed: +```cmd +python tests/test_cpu.py +python tests/test_gpu.py ``` -This will resolve the library not loaded error. This can be easily modified to work with other python installs if needed. Example to use the bindings below. @@ -63,4 +63,84 @@ forward(acts, labels, act_lens, label_lens) # labels: 1 dimensional Tensor containing all the targets of the batch in one large sequence # act_lens: Tensor of size (batch) containing size of each output sequence from the network # label_lens: Tensor of (batch) containing label length of each example -``` \ No newline at end of file +``` + +## Specific Modifications + +***When build the project with vs2017, there might be*** + +*** +``` +2>F:/warp-ctc/src/ctc_entrypoint.cu(1): error : this declaration has no storage class or type specifier +2>F:/warp-ctc/src/ctc_entrypoint.cu(1): error : expected a ";" +``` +`warp-ctc/src/ctc_entrypoint.cu`: +change `ctc_entrypoint.cpp` to `#include "ctc_entrypoint.cpp"` +*** + +``` +2>f:\warp-ctc\include\contrib\moderngpu\include\device\intrinsics.cuh(115): error : identifier "__shfl_up" is undefined +2>f:\warp-ctc\include\contrib\moderngpu\include\device\intrinsics.cuh(125): error : identifier "__shfl_up" is undefined +``` +`warp-ctc/CMakeLists.txt`: +comment out `set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -gencode arch=compute_70,code=sm_70")` +*** + +``` +3>f:\warp-ctc\tests\test.h(57): error : namespace "std" has no member "max" +``` +`warp-ctc\tests\test.h`: +add `#include ` +*** + +``` +3>test_gpu_generated_test_gpu.cu.obj : error LNK2019: 无法解析的外部符号 get_warpctc_version,该符号在函数 main 中被引用 +3>test_gpu_generated_test_gpu.cu.obj : error LNK2019: 无法解析的外部符号 ctcGetStatusString,该符号在函数 "float __cdecl grad_check(int,int,class std::vector > &,class std::vector >,class std::allocator > > > const &,class std::vector > const &)" (?grad_check@@YAMHHAEAV?$vector@MV?$allocator@M@std@@@std@@AEBV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@2@AEBV?$vector@HV?$allocator@H@std@@@2@@Z) 中被引用 +3>test_gpu_generated_test_gpu.cu.obj : error LNK2019: 无法解析的外部符号 compute_ctc_loss,该符号在函数 "float __cdecl grad_check(int,int,class std::vector > &,class std::vector >,class std::allocator > > > const &,class std::vector > const &)" (?grad_check@@YAMHHAEAV?$vector@MV?$allocator@M@std@@@std@@AEBV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@2@AEBV?$vector@HV?$allocator@H@std@@@2@@Z) 中被引用 +3>test_gpu_generated_test_gpu.cu.obj : error LNK2019: 无法解析的外部符号 get_workspace_size,该符号在函数 "float __cdecl grad_check(int,int,class std::vector > &,class std::vector >,class std::allocator > > > const &,class std::vector > const &)" (?grad_check@@YAMHHAEAV?$vector@MV?$allocator@M@std@@@std@@AEBV?$vector@V?$vector@HV?$allocator@H@std@@@std@@V?$allocator@V?$vector@HV?$allocator@H@std@@@std@@@2@@2@AEBV?$vector@HV?$allocator@H@std@@@2@@Z) 中被引用 +3>F:\warp-ctc\build\Release\test_gpu.exe : fatal error LNK1120: 4 个无法解析的外部命令 +``` +`warp-ctc\include\ctc.h`: +put `__declspec(dllexport)` in front of the four variables/functions +*** + +***When run the following command,*** +```cmd +cd ../pytorch_binding +python setup.py install +``` +***there might be*** + +*** +``` +(pytorch) F:\warp-ctc\pytorch_binding>python setup.py install +Could not find libwarpctc.so in ../build. +Build warp-ctc and set WARP_CTC_PATH to the location of libwarpctc.so (default is '../build') +``` +`warp-ctc/pytorch_binding/steup.py`: +change `.so` to `.dll` +change `../build` to `../build/Release` +change `libwarpctc` to `warpctc` +*** + +``` +binding.obj : error LNK2001: 无法解析的外部符号 "struct THCState * state" (?state@@3PEAUTHCState@@EA) +build\lib.win-amd64-3.7\warpctc_pytorch\_warp_ctc.cp37-win_amd64.pyd : fatal error LNK1120: 1 个无法解析的外部命令 +error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\link.exe' failed with exit status 1120 +``` +`warp-ctc/pyrotch_binding/src/binding.cpp`: +change `extern THCState* state;` to `THCState* state = at::globalContext().lazyInitCUDA();` +*** + +***The error might occur using the bindings*** + +*** +``` +>>> import warpctc_pytorch +Traceback (most recent call last): + File "", line 1, in + File "C:\Users\dell\Anaconda3\envs\pytorch\lib\site-packages\warpctc_pytorch-0.1-py3.7-win-amd64.egg\warpctc_pytorch\__init__.py", line 6, in + from ._warp_ctc import * +ImportError: DLL load failed: 找不到指定的模块。 +``` +copy `warp-ctc/build/Release/warpctc.dll` to `where/you/install/the/warpctc_pytorch/` From 3c3fb3b5747a985a3b9829e8b0bd0bcb5d775d56 Mon Sep 17 00:00:00 2001 From: hzli-ucas <43426126+hzli-ucas@users.noreply.github.com> Date: Mon, 18 Mar 2019 09:49:27 +0800 Subject: [PATCH 3/5] Update README.md --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index a745fd4..fb0dcc6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ -# PyTorch bindings for Warp-ctc - -[![Build Status](https://travis-ci.org/SeanNaren/warp-ctc.svg?branch=pytorch_bindings)](https://travis-ci.org/SeanNaren/warp-ctc) +# PyTorch bindings for Warp-ctc built with VS2017 This is an extension onto the original repo found [here](https://github.com/baidu-research/warp-ctc). This is a branch for Visual Studio 2017 building, with the errors fixed while installing from pytorch_bindings branch. The installation has been successfully performed in the following environment: From ec2dc3a5731b2b9f756c4584f1b4cf4985a3e82d Mon Sep 17 00:00:00 2001 From: hzli-ucas <43426126+hzli-ucas@users.noreply.github.com> Date: Sun, 14 Apr 2019 11:10:41 +0800 Subject: [PATCH 4/5] Update README.md --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fb0dcc6..edfec07 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # PyTorch bindings for Warp-ctc built with VS2017 This is an extension onto the original repo found [here](https://github.com/baidu-research/warp-ctc). -This is a branch for Visual Studio 2017 building, with the errors fixed while installing from pytorch_bindings branch. The installation has been successfully performed in the following environment: +This is a branch for Visual Studio 2017 building, with the errors fixed while installing from pytorch_bindings branch. The installation has been successfully performed in the following environments: >Win10 cuda10.1 anaconda3 python3.7 pytorch1.0 vs2017 +>Win7 cuda9.0 anaconda3 pytorch3.7 pytorch1.0 vs2015 + Most of our modifications follow [amberblade's](https://github.com/amberblade/warp-ctc) (It is a Windows compatible version of [SeanNaren's work](https://github.com/SeanNaren/warp-ctc), but cannot work with pytorch1.0). In case you should modify it yourself, all the necessary modifications can be viewed in the commit [fix errors in vs2017 building](https://github.com/hzli-ucas/warp-ctc/commit/a19202c399b8e40adc96739df946d3bdb26eefac). You can also refer to "Specific Modifications" for all the modifications and the error messages that would have be encountered during the installation without the modifications. ## Installation @@ -18,6 +20,7 @@ mkdir build cd build cmake -G "Visual Studio 15 2017 Win64" .. ``` +If using vs2015 instead of vs2017 for building, replace the `cmake -G "Visual Studio 15 2017 Win64" ..` with `cmake -G "Visual Studio 14 2015 Win64" ..`. Open the `warp-ctc/build/ctc_release.sln` with vs2017, and build the project `ALL_BUILD` (Release x64). Then install the bindings: ```cmd From 1a5568274746e14dbfe29bcea9a0642f7fcde310 Mon Sep 17 00:00:00 2001 From: hzli-ucas <43426126+hzli-ucas@users.noreply.github.com> Date: Wed, 11 Dec 2019 17:18:17 +0800 Subject: [PATCH 5/5] fix "No such file" error for new version Pytorch Delete a included file that no longer exists. Don't know if it's necessary with earlier version Pytorch, probably not. --- pytorch_binding/src/binding.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/pytorch_binding/src/binding.cpp b/pytorch_binding/src/binding.cpp index 7863261..97a5791 100644 --- a/pytorch_binding/src/binding.cpp +++ b/pytorch_binding/src/binding.cpp @@ -7,7 +7,6 @@ #ifdef WARPCTC_ENABLE_GPU #include "ATen/cuda/CUDAContext.h" - #include "ATen/cuda/CUDAGuard.h" #include "ATen/cuda/CUDAEvent.h" #include "THC.h"