20template <
typename T,
typename Transform>
21AsyncTask accumulate_transformed_gradient(ReadBuffer<T> input_gradient, WriteBuffer<T> output_gradient,
24 auto gradient =
co_await input_gradient.transfer().or_cancel();
25 T
const value = gradient.get();
27 co_await output_gradient += transform(
value);
30template <
typename T,
typename Transform>
31AsyncTask accumulate_transformed_gradient(ReadBuffer<T> input_gradient, ReadBuffer<T> forward_value,
32 WriteBuffer<T> output_gradient, Transform transform)
34 auto gradient =
co_await input_gradient.transfer().or_cancel();
35 T
const gradient_value = gradient.get();
38 auto value =
co_await forward_value.transfer();
39 T
const input_value =
value.get();
41 co_await output_gradient += transform(gradient_value, input_value);
62 auto in_grad_buffer =
co_await in_grad.
transfer().or_cancel();
63 auto const in_g = in_grad_buffer.get();
66 auto in_buffer =
co_await in.
transfer();
67 auto const cos_x =
cos(in_buffer.get());
83 schedule(detail::accumulate_transformed_gradient(Result.
grad.input(), x.
value.read(), x.
grad.output(),
84 [](T
const& gradient, T
const& input) {
86 return -uni20::conj(sin(input)) * gradient;
180 schedule(detail::accumulate_transformed_gradient(
181 Result.
grad.input(), y.
grad.output(),
182 [factor =
uni20::herm(x)](T
const& gradient) { return factor * gradient; }));
195 schedule(detail::accumulate_transformed_gradient(
196 Result.
grad.input(), x.
grad.output(),
197 [factor =
uni20::herm(y)](T
const& gradient) { return gradient * factor; }));
210 schedule(detail::accumulate_transformed_gradient(
212 [](T
const& gradient, T
const&
value) { return gradient * uni20::herm(value); }));
213 schedule(detail::accumulate_transformed_gradient(
215 [](T
const& gradient, T
const&
value) { return uni20::herm(value) * gradient; }));
230 auto in_grad_buffer =
co_await in_grad.
transfer().or_cancel();
231 auto const grad = in_grad_buffer.get();
233 co_await out_grad += grad;
234 }(Result.
grad.input(), z.
grad.output()));
249 auto in_grad_buffer =
co_await in_grad.
transfer().or_cancel();
250 auto const grad = in_grad_buffer.get();
254 co_await out_grad += std::move(
value);
255 }(Result.
grad.input(), z.
grad.output()));
Small async helper operations used in tests and examples.
Canonical host-oriented coroutine task type.
Definition async_task.hpp:196
RAII handle for reading the value from an Async container at a given epoch.
Definition buffers.hpp:53
void release() noexcept
Manually release the epoch reader before awaitable destruction.
Definition buffers.hpp:143
ReadBuffer && transfer() &noexcept
Transfer this read buffer into the owning-await path.
Definition buffers.hpp:147
ReverseValue< T > grad
Definition var.hpp:88
Async< T > value
Definition var.hpp:87
Exclusive mutable-access buffer for one Async<T> epoch.
Definition buffers.hpp:859
locale_t value
Definition data_table.cpp:49
auto operator-(Async< T > const &x)
Unary negation overload for Async values.
Definition async_ops.hpp:477
uni20::complex< T > conj(detail::standard_complex< T > x)
Returns the complex conjugate for complex-valued scalars.
Definition math.hpp:70
constexpr T & imag(detail::standard_complex< T > &z) noexcept
Provides mutable access to the imaginary component of a uni20::complex value.
Definition math.hpp:184
constexpr auto herm(S x)
Computes the Hermitian adjoint of a scalar value.
Definition math.hpp:98
typename make_real< T >::type make_real_t
Alias for the underlying real-valued type of T.
Definition scalar_traits.hpp:168
constexpr T & real(detail::standard_complex< T > &z) noexcept
Provides mutable access to the real component of a uni20::complex value.
Definition math.hpp:173
Scalar math helper utilities.
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
void schedule(AsyncTask &&task)
Schedule a task on the currently configured global scheduler.
Definition debug_scheduler.hpp:237
Var< T > operator*(T x, Var< T > y)
Computes x * y with reverse-mode gradient propagation.
Definition var_toys.hpp:176
AsyncTask co_cos(ReadBuffer< T > in, WriteBuffer< T > out)
Coroutine implementation of cos over async buffers.
Definition async_toys.hpp:60
Async< T > sin(Async< T > const &x)
Schedules asynchronous sine evaluation.
Definition async_toys.hpp:49
Var< T > operator+(Var< T > x, T y)
Computes x + y with reverse-mode gradient propagation.
Definition var_toys.hpp:136
Async< T > cos(Async< T > const &x)
Schedules asynchronous cosine evaluation.
Definition async_toys.hpp:73
AsyncTask co_sin(ReadBuffer< T > in, WriteBuffer< T > out)
Coroutine implementation of sin over async buffers.
Definition async_toys.hpp:22