Add torch backend - #1922
Conversation
| @@ -0,0 +1,89 @@ | |||
| #include "clad/Differentiator/Differentiator.h" | |||
| #include "clad/Differentiator/TorchBuiltins.h" | |||
|
|
|||
There was a problem hiding this comment.
warning: included header TorchBuiltins.h is not used directly [misc-include-cleaner]
| return loss.item<float>(); | ||
| } | ||
|
|
||
| struct Inputs { |
There was a problem hiding this comment.
warning: constructor does not initialize these fields: prediction, target [cppcoreguidelines-pro-type-member-init]
benchmark/LibTorch/TorchOpsBenchmark.cpp:19:
- torch::Tensor prediction;
- torch::Tensor target;
+ torch::Tensor prediction{};
+ torch::Tensor target{};|
|
||
| void BM_CladGradient(benchmark::State& state) { | ||
| const auto size = state.range(0); | ||
| const auto inputs = make_inputs(size, false); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'requires_grad' [bugprone-argument-comment]
| const auto inputs = make_inputs(size, false); | |
| const auto inputs = make_inputs(size, /*requires_grad=*/false); |
|
|
||
| void BM_LibTorchAutograd(benchmark::State& state) { | ||
| const auto size = state.range(0); | ||
| const auto inputs = make_inputs(size, true); |
There was a problem hiding this comment.
warning: argument comment missing for literal argument 'requires_grad' [bugprone-argument-comment]
| const auto inputs = make_inputs(size, true); | |
| const auto inputs = make_inputs(size, /*requires_grad=*/true); |
| @@ -0,0 +1,271 @@ | |||
| #include "clad/Differentiator/Differentiator.h" | |||
| #include "clad/Differentiator/TorchBuiltins.h" | |||
|
|
|||
There was a problem hiding this comment.
warning: included header TorchBuiltins.h is not used directly [misc-include-cleaner]
| #include <iostream> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
|
|
There was a problem hiding this comment.
warning: included header type_traits is not used directly [misc-include-cleaner]
| } | ||
|
|
||
| template <typename Callback> | ||
| void require_c10_error(Callback&& callback, const char* message) { |
There was a problem hiding this comment.
warning: forwarding reference parameter 'callback' is never forwarded inside the function body [cppcoreguidelines-missing-std-forward]
void require_c10_error(Callback&& callback, const char* message) {
^|
|
||
| // Public umbrella header for Clad's ATen custom derivatives. | ||
| #include "clad/Differentiator/TorchBuiltins/BasicOps.h" | ||
| #include "clad/Differentiator/TorchBuiltins/TensorLifecycle.h" |
There was a problem hiding this comment.
warning: included header BasicOps.h is not used directly [misc-include-cleaner]
| #include "clad/Differentiator/TorchBuiltins/TensorLifecycle.h" | |
| #include "clad/Differentiator/TorchBuiltins/TensorLifecycle.h" |
| // Public umbrella header for Clad's ATen custom derivatives. | ||
| #include "clad/Differentiator/TorchBuiltins/BasicOps.h" | ||
| #include "clad/Differentiator/TorchBuiltins/TensorLifecycle.h" | ||
| #include "clad/Differentiator/TorchBuiltins/TensorSyntax.h" |
There was a problem hiding this comment.
warning: included header TensorLifecycle.h is not used directly [misc-include-cleaner]
| #include "clad/Differentiator/TorchBuiltins/TensorSyntax.h" | |
| #include "clad/Differentiator/TorchBuiltins/TensorSyntax.h" |
| #include "clad/Differentiator/TorchBuiltins/BasicOps.h" | ||
| #include "clad/Differentiator/TorchBuiltins/TensorLifecycle.h" | ||
| #include "clad/Differentiator/TorchBuiltins/TensorSyntax.h" | ||
|
|
There was a problem hiding this comment.
warning: included header TensorSyntax.h is not used directly [misc-include-cleaner]
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
8d61bec to
f35d083
Compare
|
|
||
| namespace clad::custom_derivatives::class_functions { | ||
|
|
||
| inline ::clad::ValueAndAdjoint<float, float> |
There was a problem hiding this comment.
warning: no header providing "clad::ValueAndAdjoint" is directly included [misc-include-cleaner]
include/clad/Differentiator/TorchBuiltins/BasicOps.h:4:
+ #include <clad/Differentiator/BuiltinDerivatives.h>|
|
||
| inline ::clad::ValueAndAdjoint<::at::Tensor, ::at::Tensor> | ||
| constructor_reverse_forw(ConstructorPushforwardTag<::at::Tensor>, | ||
| ::at::Tensor&& input, ::at::Tensor&& /*d_input*/) { |
There was a problem hiding this comment.
warning: rvalue reference parameter '' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
::at::Tensor&& input, ::at::Tensor&& /*d_input*/) {
^|
|
||
| inline ::clad::ValueAndAdjoint<::at::Tensor, ::at::Tensor> | ||
| constructor_reverse_forw(ConstructorPushforwardTag<::at::Tensor>, | ||
| ::at::Tensor&& input, ::at::Tensor&& /*d_input*/) { |
There was a problem hiding this comment.
warning: rvalue reference parameter 'input' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
::at::Tensor&& input, ::at::Tensor&& /*d_input*/) {
^| ::clad::torch::detail::propagate_adjoint(d_this, d_input); | ||
| } | ||
|
|
||
| inline void constructor_pullback(::at::Tensor&& /*input*/, ::at::Tensor* d_this, |
There was a problem hiding this comment.
warning: rvalue reference parameter '' is never moved from inside the function body [cppcoreguidelines-rvalue-reference-param-not-moved]
inline void constructor_pullback(::at::Tensor&& /*input*/, ::at::Tensor* d_this,
^| #define CLAD_DIFFERENTIATOR_TORCHBUILTINS_TENSORSUPPORT_H | ||
|
|
||
| #include "clad/Differentiator/BuiltinDerivatives.h" | ||
|
|
There was a problem hiding this comment.
warning: included header BuiltinDerivatives.h is not used directly [misc-include-cleaner]
|
|
||
| #include "clad/Differentiator/BuiltinDerivatives.h" | ||
|
|
||
| #include <ATen/ATen.h> |
There was a problem hiding this comment.
warning: 'ATen/ATen.h' file not found [clang-diagnostic-error]
#include <ATen/ATen.h>
^| return {::std::move(value), ::std::move(adjoint)}; | ||
| } | ||
|
|
||
| inline void accumulate(::at::Tensor* destination, |
There was a problem hiding this comment.
warning: pointer parameter 'destination' can be pointer to const [readability-non-const-parameter]
| inline void accumulate(::at::Tensor* destination, | |
| inline void accumulate(const ::at::Tensor* destination, |
| destination->add_(contribution); | ||
| } | ||
|
|
||
| inline void propagate_adjoint(::at::Tensor* source, ::at::Tensor* destination) { |
There was a problem hiding this comment.
warning: pointer parameter 'destination' can be pointer to const [readability-non-const-parameter]
| inline void propagate_adjoint(::at::Tensor* source, ::at::Tensor* destination) { | |
| inline void propagate_adjoint(::at::Tensor* source, const ::at::Tensor* destination) { |
| destination->add_(contribution); | ||
| } | ||
|
|
||
| inline void propagate_adjoint(::at::Tensor* source, ::at::Tensor* destination) { |
There was a problem hiding this comment.
warning: pointer parameter 'source' can be pointer to const [readability-non-const-parameter]
| inline void propagate_adjoint(::at::Tensor* source, ::at::Tensor* destination) { | |
| inline void propagate_adjoint(const ::at::Tensor* source, ::at::Tensor* destination) { |
| return false; | ||
| // A call returned directly is still consumed by the enclosing | ||
| // function, including its value and adjoint in reverse_forw mode. | ||
| if (isa<ReturnStmt>(S)) |
There was a problem hiding this comment.
warning: no header providing "clang::ReturnStmt" is directly included [misc-include-cleaner]
if (isa<ReturnStmt>(S))
^f35d083 to
c755657
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
c755657 to
e1477c6
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
Status
Still in progress. The initial LibTorch integration is functional, but several TODO items remain.
TODO
torch::Tensor/at::Tensorinputs without exposing raw storage pointers.Tensor -> Tensorhelper functions.Tensor::item<float>().add,sub,mul,div,relu, anddot.+,-,*, and/..add(),.sub(),.mul(),.div(),.relu(), and.dot().torch::,at::, and namespace-alias API spellings.ReturnStmt.torch::autograd::grad.return output.dot(lhs).item<float>();.+,-,*, and/..add(),.sub(),.mul(), and.div().+=,-=,*=, and/=..add_(),.sub_(),.mul_(), and.div_()with correct mutation and tape semantics.