Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
var.hpp
Go to the documentation of this file.
1#pragma once
2
3// \brief Var type for reverse-mode automatic differentiation using Wirtinger calculus.
36
37#include "async.hpp"
38#include "reverse_value.hpp"
39#include "scheduler.hpp"
40#include <cmath>
41
42namespace uni20::async
43{
44
45template <typename T> class Var {
46 public:
47 using value_type = T;
48
49 Var() = default;
50 Var(Var&&) = default;
51 Var& operator=(Var&&) = default;
52
54 Var(Var const&) = delete;
55 Var& operator=(Var const&) = delete;
56
57 Var(Var& other) : value(other.value) { other.grad += grad.input(); }
58
59 Var(Async<T> const& other) : value(other) {}
60
61 Var& operator=(Var& other)
62 {
64 other.grad += grad.input();
65 value = other.value;
66 return *this;
67 }
68
70 template <typename U>
71 requires std::convertible_to<U, T>
72 Var(U&& val) : value(std::forward<U>(val))
73 {}
74
77 Async<T>& backprop() & { return this->grad.backprop(); }
78
81 Async<T> const& backprop() const& { return this->grad.backprop(); }
82
85 Async<T> backprop() && { return std::move(this->grad).backprop(); }
86
89};
90
91} // namespace uni20::async
Async<T> is a container for coroutine-safe asynchronous read/write.
Definition async/async.hpp:93
Reverse-mode accumulation endpoint with staged input/output gradient channels.
Definition reverse_value.hpp:70
Definition var.hpp:45
Var & operator=(Var &&)=default
Async< T > & backprop() &
Finalize reverse propagation and return the gradient channel.
Definition var.hpp:77
ReverseValue< T > grad
Definition var.hpp:88
Var(Var &other)
Definition var.hpp:57
Var & operator=(Var const &)=delete
Var(Var const &)=delete
Disable ordinary copying (Vars are never copied in the normal sense).
Var(U &&val)
Constructs with a copy of an initial value that can be implicitly converted to T.
Definition var.hpp:72
Async< T > const & backprop() const &
Finalize reverse propagation and return the gradient channel.
Definition var.hpp:81
Async< T > value
Definition var.hpp:87
Var(Async< T > const &other)
Definition var.hpp:59
Async< T > backprop() &&
Finalize reverse propagation and move out the gradient channel.
Definition var.hpp:85
Var & operator=(Var &other)
Definition var.hpp:61
Var(Var &&)=default
T value_type
Definition var.hpp:47
Definition qnum.hpp:403
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
Reverse-mode gradient accumulation channel built on reverse epoch queues.
Defines internal task routing and the host-oriented scheduler interface.
Async aliases for lazy tensor-level views.