mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 13:44:15 +08:00
Summary: In TorchScript and C++ extensions we currently advocate a mix of `torch::` and `at::` namespace usage. In the C++ frontend I had instead exported all symbols from `at::` and some from `c10::` into the `torch::` namespace. This is far, far easier for users to understand, and also avoid bugs around creating tensors vs. variables. The same should from now on be true for the TorchScript C++ API (for running and loading models) and all C++ extensions. Note that since we're just talking about typedefs, this change does not break any existing code. Once this lands I will update stuff in `pytorch/tutorials` too. zdevito ezyang gchanan Pull Request resolved: https://github.com/pytorch/pytorch/pull/13523 Differential Revision: D12942787 Pulled By: goldsborough fbshipit-source-id: 76058936bd8707b33d9e5bbc2d0705fc3d820763
38 lines
971 B
C++
38 lines
971 B
C++
#include <torch/extension.h>
|
|
|
|
torch::Tensor sigmoid_add(torch::Tensor x, torch::Tensor y) {
|
|
return x.sigmoid() + y.sigmoid();
|
|
}
|
|
|
|
struct MatrixMultiplier {
|
|
MatrixMultiplier(int A, int B) {
|
|
tensor_ =
|
|
torch::ones({A, B}, torch::dtype(torch::kFloat64).requires_grad(true));
|
|
}
|
|
torch::Tensor forward(torch::Tensor weights) {
|
|
return tensor_.mm(weights);
|
|
}
|
|
torch::Tensor get() const {
|
|
return tensor_;
|
|
}
|
|
|
|
private:
|
|
torch::Tensor tensor_;
|
|
};
|
|
|
|
bool function_taking_optional(c10::optional<torch::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);
|
|
}
|