mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
* Rename autograd namespace to torch and change torch.h into python.h * Include torch.h instead of python.h in test/cpp/api * Change some mentions of torch.h to python.h in C++ extensions * Set paths directly, without find_path
38 lines
951 B
C++
38 lines
951 B
C++
#include <torch/python.h>
|
|
|
|
at::Tensor sigmoid_add(at::Tensor x, at::Tensor y) {
|
|
return x.sigmoid() + y.sigmoid();
|
|
}
|
|
|
|
struct MatrixMultiplier {
|
|
MatrixMultiplier(int A, int B) {
|
|
tensor_ = at::ones(torch::CPU(at::kDouble), {A, B});
|
|
torch::set_requires_grad(tensor_, true);
|
|
}
|
|
at::Tensor forward(at::Tensor weights) {
|
|
return tensor_.mm(weights);
|
|
}
|
|
at::Tensor get() const {
|
|
return tensor_;
|
|
}
|
|
|
|
private:
|
|
at::Tensor tensor_;
|
|
};
|
|
|
|
bool function_taking_optional(at::optional<at::Tensor> tensor) {
|
|
return tensor.has_value();
|
|
}
|
|
|
|
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
|
|
m.def("sigmoid_add", &sigmoid_add, "sigmoid(x) + sigmoid(y)");
|
|
m.def(
|
|
"function_taking_optional",
|
|
&function_taking_optional,
|
|
"function_taking_optional");
|
|
py::class_<MatrixMultiplier>(m, "MatrixMultiplier")
|
|
.def(py::init<int, int>())
|
|
.def("forward", &MatrixMultiplier::forward)
|
|
.def("get", &MatrixMultiplier::get);
|
|
}
|