2018-09-24 21:28:54 +00:00
|
|
|
#include <torch/extension.h>
|
2018-01-23 00:49:11 +00:00
|
|
|
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::Tensor sigmoid_add(torch::Tensor x, torch::Tensor y) {
|
2018-01-23 00:49:11 +00:00
|
|
|
return x.sigmoid() + y.sigmoid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MatrixMultiplier {
|
|
|
|
|
MatrixMultiplier(int A, int B) {
|
2018-11-06 00:44:45 +00:00
|
|
|
tensor_ =
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::ones({A, B}, torch::dtype(torch::kFloat64).requires_grad(true));
|
2018-01-23 00:49:11 +00:00
|
|
|
}
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::Tensor forward(torch::Tensor weights) {
|
2018-01-23 00:49:11 +00:00
|
|
|
return tensor_.mm(weights);
|
|
|
|
|
}
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::Tensor get() const {
|
2018-01-23 00:49:11 +00:00
|
|
|
return tensor_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 22:31:05 +00:00
|
|
|
private:
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::Tensor tensor_;
|
2018-01-23 00:49:11 +00:00
|
|
|
};
|
|
|
|
|
|
2018-11-06 22:28:20 +00:00
|
|
|
bool function_taking_optional(c10::optional<torch::Tensor> tensor) {
|
2018-04-28 00:59:50 +00:00
|
|
|
return tensor.has_value();
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-17 03:31:04 +00:00
|
|
|
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
2018-01-23 00:49:11 +00:00
|
|
|
m.def("sigmoid_add", &sigmoid_add, "sigmoid(x) + sigmoid(y)");
|
2018-04-28 00:59:50 +00:00
|
|
|
m.def(
|
|
|
|
|
"function_taking_optional",
|
|
|
|
|
&function_taking_optional,
|
|
|
|
|
"function_taking_optional");
|
2018-01-23 00:49:11 +00:00
|
|
|
py::class_<MatrixMultiplier>(m, "MatrixMultiplier")
|
|
|
|
|
.def(py::init<int, int>())
|
|
|
|
|
.def("forward", &MatrixMultiplier::forward)
|
|
|
|
|
.def("get", &MatrixMultiplier::get);
|
|
|
|
|
}
|