-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditdistance.cpp
51 lines (41 loc) · 1.52 KB
/
editdistance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "editdistance.h"
torch::Tensor editdistance(
const torch::Tensor& src,
const torch::Tensor& trg,
int64_t padToken)
{
int64_t srcDims = src.ndimension();
int64_t trgDims = trg.ndimension();
TORCH_CHECK(srcDims == 2 || srcDims == 1,
"editdistance: Expect 1D or 2D Tensor, got: ",
src.sizes());
TORCH_CHECK(trgDims == 2 || trgDims == 1,
"editdistance: Expect 1D or 2D Tensor, got: ",
trg.sizes());
TORCH_CHECK(srcDims == trgDims,
"editdistance: Expect src and trg to have the same number of dimensions");
TORCH_CHECK(src.device() == trg.device(),
"source and target tensor must be on the same device, got ",
"src on device ", src.device(),
" and trg on device ", trg.device());
auto src_ = src;
auto trg_ = trg;
if (srcDims == 1)
{
src_ = src_.reshape({1, src_.size(0)});
trg_ = trg_.reshape({1, trg_.size(0)});
}
TORCH_CHECK(src_.size(0) == trg_.size(0),
"editdistance: expected src and trg to have same batch size");
// dispatch
static auto op = torch::Dispatcher::singleton()
.findSchemaOrThrow("editdistance::editdistance", "")
.typed<decltype(editdistance)>();
return op.call(src_, trg_, padToken);
}
TORCH_LIBRARY(editdistance, m) {
m.def("editdistance(Tensor self, Tensor other, int padToken) -> Tensor");
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("editdistance", &editdistance, "editdistance forward");
}