2018-05-05 21:06:27 +00:00
|
|
|
#include <torch/torch.h>
|
2018-01-23 00:49:11 +00:00
|
|
|
|
2018-03-09 22:31:05 +00:00
|
|
|
at::Tensor sigmoid_add(at::Tensor x, at::Tensor y) {
|
2018-01-23 00:49:11 +00:00
|
|
|
return x.sigmoid() + y.sigmoid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MatrixMultiplier {
|
|
|
|
|
MatrixMultiplier(int A, int B) {
|
2018-06-16 07:40:35 +00:00
|
|
|
tensor_ = at::ones({A, B}, torch::CPU(at::kDouble));
|
2018-03-09 22:31:05 +00:00
|
|
|
torch::set_requires_grad(tensor_, true);
|
2018-01-23 00:49:11 +00:00
|
|
|
}
|
2018-03-09 22:31:05 +00:00
|
|
|
at::Tensor forward(at::Tensor weights) {
|
2018-01-23 00:49:11 +00:00
|
|
|
return tensor_.mm(weights);
|
|
|
|
|
}
|
2018-03-09 22:31:05 +00:00
|
|
|
at::Tensor get() const {
|
2018-01-23 00:49:11 +00:00
|
|
|
return tensor_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-09 22:31:05 +00:00
|
|
|
private:
|
|
|
|
|
at::Tensor tensor_;
|
2018-01-23 00:49:11 +00:00
|
|
|
};
|
|
|
|
|
|
2018-04-28 00:59:50 +00:00
|
|
|
bool function_taking_optional(at::optional<at::Tensor> tensor) {
|
|
|
|
|
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);
|
|
|
|
|
}
|