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

Vs2017 #115

Open
wants to merge 5 commits into
base: pytorch_bindings
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
129 changes: 105 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
# 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 environments:
>Win10 cuda10.1 anaconda3 python3.7 pytorch1.0 vs2017

## Installation
>Win7 cuda9.0 anaconda3 pytorch3.7 pytorch1.0 vs2015

Install [PyTorch](https://github.com/pytorch/pytorch#installation) v0.4.
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.

`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:
## Installation

```bash
git clone https://github.com/SeanNaren/warp-ctc.git
Install [PyTorch](https://github.com/pytorch/pytorch#installation) v1.0 or higher.

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" ..
```
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" ..`.

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.

Expand Down Expand Up @@ -63,4 +64,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
```
```

## 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 <algorithm>`
***

```
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<float,class std::allocator<float> > &,class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > const &,class std::vector<int,class std::allocator<int> > 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<float,class std::allocator<float> > &,class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > const &,class std::vector<int,class std::allocator<int> > 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<float,class std::allocator<float> > &,class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > > const &,class std::vector<int,class std::allocator<int> > 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 "<stdin>", line 1, in <module>
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 <module>
from ._warp_ctc import *
ImportError: DLL load failed: 找不到指定的模块。
```
copy `warp-ctc/build/Release/warpctc.dll` to `where/you/install/the/warpctc_pytorch/`
8 changes: 4 additions & 4 deletions include/ctc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions pytorch_binding/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down
3 changes: 1 addition & 2 deletions pytorch_binding/src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

#ifdef WARPCTC_ENABLE_GPU
#include "ATen/cuda/CUDAContext.h"
#include "ATen/cuda/CUDAGuard.h"
#include "ATen/cuda/CUDAEvent.h"

#include "THC.h"
extern THCState* state;
THCState* state = at::globalContext().lazyInitCUDA();
#endif

#include "ctc.h"
Expand Down
2 changes: 1 addition & 1 deletion src/ctc_entrypoint.cu
2 changes: 1 addition & 1 deletion tests/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>
#include <limits>
#include <numeric>

#include <algorithm>

#include <ctc.h>

Expand Down