12 static bool requires_grad(
const variable_list& variables) {
13 return std::accumulate(variables.begin(), variables.end(),
false,
14 [](
bool accumulated,
const Variable& b) {
15 return accumulated || b.requires_grad();
19 variable_list apply(
const variable_list& inputs)
override {
20 throw std::runtime_error(
21 "Forward function should not be called without template args");
25 template <
typename... Args>
26 variable_list apply(
const variable_list& inputs, Args&&... args) {
27 bool requires_grad_ = requires_grad(inputs);
29 Variable(
static_cast<Derived*
>(
this)->execute(inputs), requires_grad_);
31 auto backward_fn = std::make_shared<typename Derived::DefaultBackward>(
32 std::forward<Args>(args)...);
33 backward_fn->saved_inputs = std::make_unique<variable_list>(inputs);
34 result.set_grad_fn(backward_fn);