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

update #56

Open
wants to merge 7 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
3 changes: 2 additions & 1 deletion pytorch_binding/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from torch.utils.ffi import create_extension
import torch

extra_compile_args = ['-std=c++11', '-fPIC']
#extra_compile_args = ['-std=c++11', '-fPIC']
extra_compile_args = ['-std=c99', '-fPIC']
warp_ctc_path = "../build"

if torch.cuda.is_available() or "CUDA_HOME" in os.environ:
Expand Down
7 changes: 6 additions & 1 deletion pytorch_binding/src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ extern "C" int cpu_ctc(THFloatTensor *probs,
THIntTensor *label_sizes,
THIntTensor *sizes,
int minibatch_size,
int blanklabel_index,
int num_threads,
THFloatTensor *costs) {

float *probs_ptr = probs->storage->data + probs->storageOffset;
Expand All @@ -38,7 +40,8 @@ extern "C" int cpu_ctc(THFloatTensor *probs,
ctcOptions options;
memset(&options, 0, sizeof(options));
options.loc = CTC_CPU;
options.num_threads = 0; // will use default number of threads
options.blank_label = blanklabel_index;
options.num_threads = num_threads; // will use given number of threads

#if defined(CTC_DISABLE_OMP) || defined(APPLE)
// have to use at least one
Expand Down Expand Up @@ -68,6 +71,7 @@ extern "C" int cpu_ctc(THFloatTensor *probs,
THIntTensor *label_sizes,
THIntTensor *sizes,
int minibatch_size,
int blanklabel_index,
THFloatTensor *costs) {

float *probs_ptr = probs->storage->data + probs->storageOffset;
Expand All @@ -86,6 +90,7 @@ extern "C" int cpu_ctc(THFloatTensor *probs,
ctcOptions options;
memset(&options, 0, sizeof(options));
options.loc = CTC_GPU;
options.blank_label = blanklabel_index;
options.stream = THCState_getCurrentStream(state);

size_t gpu_size_bytes;
Expand Down
6 changes: 6 additions & 0 deletions pytorch_binding/tests/test_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def test_simple():
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())

Expand All @@ -40,6 +42,8 @@ def test_medium(multiplier):
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())

Expand All @@ -62,6 +66,8 @@ def test_empty_label():
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())

Expand Down
9 changes: 9 additions & 0 deletions pytorch_binding/tests/test_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def test_simple():
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())
probs = probs.clone().cuda()
Expand All @@ -29,6 +31,7 @@ def test_simple():
label_sizes,
sizes,
minibatch_size,
0,
costs)
print('GPU_cost: %f' % costs.sum())
print(grads.view(grads.size(0) * grads.size(1), grads.size(2)))
Expand All @@ -54,6 +57,8 @@ def test_medium(multiplier):
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())
probs = probs.clone().cuda()
Expand All @@ -65,6 +70,7 @@ def test_medium(multiplier):
label_sizes,
sizes,
minibatch_size,
0,
costs)
print('GPU_cost: %f' % costs.sum())
print(grads.view(grads.size(0) * grads.size(1), grads.size(2)))
Expand All @@ -89,6 +95,8 @@ def test_empty_label():
label_sizes,
sizes,
minibatch_size,
0,
0,
costs)
print('CPU_cost: %f' % costs.sum())
probs = probs.clone().cuda()
Expand All @@ -100,6 +108,7 @@ def test_empty_label():
label_sizes,
sizes,
minibatch_size,
0,
costs)
print('GPU_cost: %f' % costs.sum())
print(grads.view(grads.size(0) * grads.size(1), grads.size(2)))
Expand Down
16 changes: 6 additions & 10 deletions pytorch_binding/warpctc_pytorch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@

class _CTC(Function):
@staticmethod
def forward(ctx, acts, labels, act_lens, label_lens, size_average=False,
def forward(ctx, acts, labels, act_lens, label_lens, blank_label=0, num_threads=0, size_average=False,
length_average=False):
is_cuda = True if acts.is_cuda else False
acts = acts.contiguous()
loss_func = warp_ctc.gpu_ctc if is_cuda else warp_ctc.cpu_ctc
grads = torch.zeros(acts.size()).type_as(acts)
minibatch_size = acts.size(1)
costs = torch.zeros(minibatch_size).cpu()
loss_func(acts,
grads,
labels,
label_lens,
act_lens,
minibatch_size,
costs)

if is_cuda:
# num_threads will be negeleted in GPU mode
warp_ctc.gpu_ctc(acts, grads,labels, label_lens, act_lens, minibatch_size, blank_label, costs)
else:
warp_ctc.cpu_ctc(acts, grads,labels, label_lens, act_lens, minibatch_size, blank_label, num_threads, costs)
costs = torch.FloatTensor([costs.sum()])

if length_average:
Expand Down