-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
try to resolve a torchscript loading issue
- Loading branch information
Showing
3 changed files
with
216 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#include <torch/script.h> | ||
#include <vector> | ||
|
||
/* Implementation starts here */ | ||
|
||
// unidirectional forward() | ||
std::vector<at::Tensor> sru_forward_simple( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// unidirectional backward() | ||
std::vector<at::Tensor> sru_backward_simple( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const at::Tensor & c, | ||
const at::Tensor & grad_h, | ||
const at::Tensor & grad_last, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// bidirectional forward() | ||
std::vector<at::Tensor> sru_bi_forward_simple( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// bidirectional backward() | ||
std::vector<at::Tensor> sru_bi_backward_simple( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const at::Tensor & c, | ||
const at::Tensor & grad_h, | ||
const at::Tensor & grad_last, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// unidirectional forward() | ||
std::vector<at::Tensor> sru_forward( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size, | ||
const int64_t k, | ||
const int64_t activation_type, | ||
const int64_t skip_type, | ||
const int64_t is_custom) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// bidirectional forward() | ||
std::vector<at::Tensor> sru_bi_forward( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size, | ||
const int64_t k, | ||
const int64_t activation_type, | ||
const int64_t skip_type, | ||
const int64_t is_custom) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// unidirectional backward() | ||
std::vector<at::Tensor> sru_backward( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const at::Tensor & c, | ||
const at::Tensor & grad_h, | ||
const at::Tensor & grad_last, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size, | ||
const int64_t k, | ||
const int64_t activation_type, | ||
const int64_t skip_type, | ||
const int64_t is_custom) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// bidirectional backward() | ||
std::vector<at::Tensor> sru_bi_backward( | ||
const at::Tensor & U, | ||
const at::optional<at::Tensor> & x, | ||
const at::Tensor & weight_c, | ||
const at::Tensor & bias, | ||
const at::Tensor & c_init, | ||
const at::optional<at::Tensor> & mask_c, | ||
const at::optional<at::Tensor> & mask_pad, | ||
const at::Tensor & c, | ||
const at::Tensor & grad_h, | ||
const at::Tensor & grad_last, | ||
const int64_t length, | ||
const int64_t batch_size, | ||
const int64_t hidden_size, | ||
const int64_t k, | ||
const int64_t activation_type, | ||
const int64_t skip_type, | ||
const int64_t is_custom) { | ||
|
||
throw "Failed to load SRU recurrence operators for GPU"; | ||
} | ||
|
||
// This way of registing custom op is based on earlier PRs of Pytorch: | ||
// https://github.com/pytorch/pytorch/pull/28229 | ||
// | ||
// In Pytorch 1.6, the recommended way is to use TORCH_LIBRARY(), e.g. | ||
// | ||
// TORCH_LIBRARY(sru_cpu, m) { | ||
// m.def("cpu_forward", &cpu_forward); | ||
// m.def("cpu_bi_forward", &cpu_bi_forward); | ||
// } | ||
// | ||
// We choose this way for backward compatibility. | ||
static auto registory1 = | ||
torch::RegisterOperators("sru_cuda::sru_forward_simple", &sru_forward_simple); | ||
static auto registory2 = | ||
torch::RegisterOperators("sru_cuda::sru_backward_simple", &sru_backward_simple); | ||
static auto registory3 = | ||
torch::RegisterOperators("sru_cuda::sru_bi_forward_simple", &sru_bi_forward_simple); | ||
static auto registory4 = | ||
torch::RegisterOperators("sru_cuda::sru_bi_backward_simple", &sru_bi_backward_simple); | ||
static auto registory5 = | ||
torch::RegisterOperators("sru_cuda::sru_forward", &sru_forward); | ||
static auto registory6 = | ||
torch::RegisterOperators("sru_cuda::sru_backward", &sru_backward); | ||
static auto registory7 = | ||
torch::RegisterOperators("sru_cuda::sru_bi_forward", &sru_bi_forward); | ||
static auto registory8 = | ||
torch::RegisterOperators("sru_cuda::sru_bi_backward", &sru_bi_backward); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters