2021-10-19 04:58:26 +00:00
|
|
|
#include <c10/util/irange.h>
|
2018-09-17 16:47:28 +00:00
|
|
|
#include <torch/script.h>
|
2018-08-17 01:45:28 +00:00
|
|
|
|
2018-10-23 16:16:32 +00:00
|
|
|
#include "op.h"
|
|
|
|
|
|
2018-08-17 01:45:28 +00:00
|
|
|
#include <cstddef>
|
2018-11-10 20:56:57 +00:00
|
|
|
#include <string>
|
2018-08-17 01:45:28 +00:00
|
|
|
|
2019-12-16 06:35:52 +00:00
|
|
|
torch::List<torch::Tensor> custom_op(
|
2018-11-06 22:28:20 +00:00
|
|
|
torch::Tensor tensor,
|
2018-08-17 01:45:28 +00:00
|
|
|
double scalar,
|
|
|
|
|
int64_t repeat) {
|
2019-12-16 06:35:52 +00:00
|
|
|
torch::List<torch::Tensor> output;
|
2018-08-17 01:45:28 +00:00
|
|
|
output.reserve(repeat);
|
2024-10-19 13:17:43 +00:00
|
|
|
for ([[maybe_unused]] const auto i : c10::irange(repeat)) {
|
2018-08-17 01:45:28 +00:00
|
|
|
output.push_back(tensor * scalar);
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-10 20:56:57 +00:00
|
|
|
int64_t custom_op2(std::string s1, std::string s2) {
|
|
|
|
|
return s1.compare(s2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-16 06:35:52 +00:00
|
|
|
struct CustomOpAutogradFunction : public torch::autograd::Function<CustomOpAutogradFunction> {
|
2020-05-06 08:57:42 +00:00
|
|
|
static torch::Tensor forward(
|
|
|
|
|
torch::autograd::AutogradContext* ctx,
|
|
|
|
|
torch::Tensor var1,
|
|
|
|
|
int64_t mul,
|
|
|
|
|
torch::Tensor var2,
|
2024-05-14 19:35:49 +00:00
|
|
|
std::optional<torch::Tensor> var3) {
|
2019-12-16 06:35:52 +00:00
|
|
|
ctx->saved_data["mul"] = mul;
|
2020-05-06 08:57:42 +00:00
|
|
|
ctx->saved_data["var3_has_value"] = var3.has_value();
|
2019-12-16 06:35:52 +00:00
|
|
|
ctx->save_for_backward({var1, var2});
|
2020-05-06 08:57:42 +00:00
|
|
|
if (var3) {
|
|
|
|
|
return var1 + mul * var2 + var1 * var2 + var3.value();
|
|
|
|
|
}
|
2019-12-16 06:35:52 +00:00
|
|
|
return var1 + mul*var2 + var1*var2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static torch::autograd::variable_list backward(torch::autograd::AutogradContext *ctx, torch::autograd::variable_list grad_output) {
|
|
|
|
|
int mul = ctx->saved_data["mul"].toInt();
|
2020-05-06 08:57:42 +00:00
|
|
|
bool var3_has_value = ctx->saved_data["var3_has_value"].toBool();
|
2019-12-16 06:35:52 +00:00
|
|
|
auto saved = ctx->get_saved_variables();
|
|
|
|
|
auto var1 = saved[0];
|
|
|
|
|
auto var2 = saved[1];
|
2020-05-06 08:57:42 +00:00
|
|
|
auto var3_grad = var3_has_value ? grad_output[0] : torch::Tensor();
|
|
|
|
|
torch::autograd::variable_list output = {
|
|
|
|
|
grad_output[0] + grad_output[0] * var2,
|
|
|
|
|
torch::Tensor(),
|
|
|
|
|
grad_output[0] * mul + grad_output[0] * var1,
|
|
|
|
|
var3_grad};
|
2019-12-16 06:35:52 +00:00
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-06 08:57:42 +00:00
|
|
|
torch::Tensor custom_op_with_autograd(
|
|
|
|
|
torch::Tensor var1,
|
|
|
|
|
int64_t mul,
|
|
|
|
|
torch::Tensor var2,
|
2024-05-14 19:35:49 +00:00
|
|
|
std::optional<torch::Tensor> var3) {
|
2020-05-06 08:57:42 +00:00
|
|
|
return CustomOpAutogradFunction::apply(var1, mul, var2, var3);
|
2019-12-16 06:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-21 21:04:16 +00:00
|
|
|
torch::Tensor custom_nonzero(torch::Tensor x) {
|
|
|
|
|
return x.nonzero();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
torch::Tensor custom_sin(torch::Tensor x) {
|
|
|
|
|
return x.sin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
Updating all call-sites of the legacy dispatcher registration API in fbcode to the new API. (#48178)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48178
I migrated all call sites that used the legacy dispatcher registration API (RegisterOperators()) to use the new API (TORCH_LIBRARY...). I found all call-sites by running `fbgs RegisterOperators()`. This includes several places, including other OSS code (nestedtensor, torchtext, torchvision). A few things to call out:
For simple ops that only had one registered kernel without a dispatch key, I replaced them with:
```
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName", fn_name);
}
```
For ops that registered to a specific dispatch key / had multiple kernels registered, I registered the common kernel (math/cpu) directly inside a `TORCH_LIBRARY_FRAGMENT` block, and registered any additional kernels from other files (e.g. cuda) in a separate `TORCH_LIBRARY_IMPL` block.
```
// cpu file
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName(schema_inputs) -> schema_outputs");
m.impl("opName", torch::dispatch(c10::DispatchKey::CPU, TORCH_FN(cpu_kernel)));
}
// cuda file
TORCH_LIBRARY_IMPL(ns, CUDA, m) {
m.impl("opName", torch::dispatch(c10::DispatchKey::CUDA, TORCH_FN(cuda_kernel)));
}
```
Special cases:
I found a few ops that used a (legacy) `CPUTensorId`/`CUDATensorId` dispatch key. Updated those to use CPU/CUDA- this seems safe because the keys are aliased to one another in `DispatchKey.h`
There were a handful of ops that registered a functor (function class) to the legacy API. As far as I could tell we don't allow this case in the new API, mainly because you can accomplish the same thing more cleanly with lambdas. Rather than delete the class I wrote a wrapper function on top of the class, which I passed to the new API.
There were a handful of ops that were registered only to a CUDA dispatch key. I put them inside a TORCH_LIBRARY_FRAGMENT block, and used a `def()` and `impl()` call like in case two above.
Test Plan: Imported from OSS
Reviewed By: ezyang
Differential Revision: D25056090
Pulled By: bdhirsh
fbshipit-source-id: 8f868b45f545e5da2f21924046e786850eba70d9
2020-12-02 19:09:12 +00:00
|
|
|
TORCH_LIBRARY_FRAGMENT(custom, m) {
|
2023-11-08 00:39:00 +00:00
|
|
|
m.impl_abstract_pystub("my_custom_ops2");
|
Updating all call-sites of the legacy dispatcher registration API in fbcode to the new API. (#48178)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48178
I migrated all call sites that used the legacy dispatcher registration API (RegisterOperators()) to use the new API (TORCH_LIBRARY...). I found all call-sites by running `fbgs RegisterOperators()`. This includes several places, including other OSS code (nestedtensor, torchtext, torchvision). A few things to call out:
For simple ops that only had one registered kernel without a dispatch key, I replaced them with:
```
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName", fn_name);
}
```
For ops that registered to a specific dispatch key / had multiple kernels registered, I registered the common kernel (math/cpu) directly inside a `TORCH_LIBRARY_FRAGMENT` block, and registered any additional kernels from other files (e.g. cuda) in a separate `TORCH_LIBRARY_IMPL` block.
```
// cpu file
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName(schema_inputs) -> schema_outputs");
m.impl("opName", torch::dispatch(c10::DispatchKey::CPU, TORCH_FN(cpu_kernel)));
}
// cuda file
TORCH_LIBRARY_IMPL(ns, CUDA, m) {
m.impl("opName", torch::dispatch(c10::DispatchKey::CUDA, TORCH_FN(cuda_kernel)));
}
```
Special cases:
I found a few ops that used a (legacy) `CPUTensorId`/`CUDATensorId` dispatch key. Updated those to use CPU/CUDA- this seems safe because the keys are aliased to one another in `DispatchKey.h`
There were a handful of ops that registered a functor (function class) to the legacy API. As far as I could tell we don't allow this case in the new API, mainly because you can accomplish the same thing more cleanly with lambdas. Rather than delete the class I wrote a wrapper function on top of the class, which I passed to the new API.
There were a handful of ops that were registered only to a CUDA dispatch key. I put them inside a TORCH_LIBRARY_FRAGMENT block, and used a `def()` and `impl()` call like in case two above.
Test Plan: Imported from OSS
Reviewed By: ezyang
Differential Revision: D25056090
Pulled By: bdhirsh
fbshipit-source-id: 8f868b45f545e5da2f21924046e786850eba70d9
2020-12-02 19:09:12 +00:00
|
|
|
m.def("op", custom_op);
|
|
|
|
|
m.def("op2", custom_op2);
|
|
|
|
|
m.def("op_with_defaults(Tensor tensor, float scalar = 1, int repeat = 1) -> Tensor[]", custom_op);
|
|
|
|
|
m.def("op_with_autograd(Tensor var1, int mul, Tensor var2, Tensor? var3=None) -> Tensor", custom_op_with_autograd);
|
2023-09-21 21:04:16 +00:00
|
|
|
m.def("sin(Tensor x) -> Tensor");
|
2023-11-08 00:39:00 +00:00
|
|
|
m.def("cos(Tensor x) -> Tensor");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TORCH_LIBRARY_FRAGMENT(custom, m) {
|
|
|
|
|
m.impl_abstract_pystub("my_custom_ops");
|
2023-09-21 21:04:16 +00:00
|
|
|
m.def("nonzero(Tensor x) -> Tensor");
|
2023-11-08 00:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-23 02:32:10 +00:00
|
|
|
TORCH_LIBRARY_FRAGMENT(custom, m) {
|
|
|
|
|
m.impl_abstract_pystub("nonexistent");
|
|
|
|
|
m.def("asin(Tensor x) -> Tensor");
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 00:39:00 +00:00
|
|
|
TORCH_LIBRARY_FRAGMENT(custom, m) {
|
|
|
|
|
m.def("tan(Tensor x) -> Tensor");
|
2023-09-21 21:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TORCH_LIBRARY_IMPL(custom, CPU, m) {
|
|
|
|
|
m.impl("nonzero", &custom_nonzero);
|
|
|
|
|
m.impl("sin", &custom_sin);
|
2024-01-23 02:32:10 +00:00
|
|
|
m.impl("asin", &at::asin);
|
Updating all call-sites of the legacy dispatcher registration API in fbcode to the new API. (#48178)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/48178
I migrated all call sites that used the legacy dispatcher registration API (RegisterOperators()) to use the new API (TORCH_LIBRARY...). I found all call-sites by running `fbgs RegisterOperators()`. This includes several places, including other OSS code (nestedtensor, torchtext, torchvision). A few things to call out:
For simple ops that only had one registered kernel without a dispatch key, I replaced them with:
```
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName", fn_name);
}
```
For ops that registered to a specific dispatch key / had multiple kernels registered, I registered the common kernel (math/cpu) directly inside a `TORCH_LIBRARY_FRAGMENT` block, and registered any additional kernels from other files (e.g. cuda) in a separate `TORCH_LIBRARY_IMPL` block.
```
// cpu file
TORCH_LIBRARY_FRAGMENT(ns, m) {
m.def("opName(schema_inputs) -> schema_outputs");
m.impl("opName", torch::dispatch(c10::DispatchKey::CPU, TORCH_FN(cpu_kernel)));
}
// cuda file
TORCH_LIBRARY_IMPL(ns, CUDA, m) {
m.impl("opName", torch::dispatch(c10::DispatchKey::CUDA, TORCH_FN(cuda_kernel)));
}
```
Special cases:
I found a few ops that used a (legacy) `CPUTensorId`/`CUDATensorId` dispatch key. Updated those to use CPU/CUDA- this seems safe because the keys are aliased to one another in `DispatchKey.h`
There were a handful of ops that registered a functor (function class) to the legacy API. As far as I could tell we don't allow this case in the new API, mainly because you can accomplish the same thing more cleanly with lambdas. Rather than delete the class I wrote a wrapper function on top of the class, which I passed to the new API.
There were a handful of ops that were registered only to a CUDA dispatch key. I put them inside a TORCH_LIBRARY_FRAGMENT block, and used a `def()` and `impl()` call like in case two above.
Test Plan: Imported from OSS
Reviewed By: ezyang
Differential Revision: D25056090
Pulled By: bdhirsh
fbshipit-source-id: 8f868b45f545e5da2f21924046e786850eba70d9
2020-12-02 19:09:12 +00:00
|
|
|
}
|