Implementing a simple autodiff demo with Cpp
Implemented a simple autodiff demo with Cpp, the idea comes from Andrej karpathy’s micrograd.
I feel the uniqueness of Andrej’s implementation is that in his data structure design, the owner of the computation graph are the nodes that have been constructed.
Implementing in higher level language like Python would have let me ignore the issue, while in Cpp, I have to consider the lifetime of the graph.
To do this, we need a Node structure representing the real data, and use a wrapper Value to refer Node and keep it alive by using shared_ptr (which implements reference count).
There should be another way to viewing this, for example, in MLIR, computation graphs are represented in Regions, Blocks and Operations.
That is, instead of using Node as the owner, we can have a outter container such as a Graph to own Operations, and each Operation owns operands, result and operator.
1#include <cassert>
2#include <cmath>
3#include <iostream>
4#include <memory>
5
6enum class BinaryOperator { ADD, MUL, LEAF };
7
8struct Node {
9 double_t val;
10 BinaryOperator op;
11 std::shared_ptr<Node> lhs;
12 std::shared_ptr<Node> rhs;
13
14 Node(double_t val) : val(val), op(BinaryOperator::LEAF) {};
15 Node(double_t val, BinaryOperator op, std::shared_ptr<Node> lhs,
16 std::shared_ptr<Node> rhs)
17 : val(val), op(op), lhs(lhs), rhs(rhs) {};
18};
19
20struct Value {
21 std::shared_ptr<Node> node;
22
23 explicit Value(double_t val) { node = std::make_shared<Node>(val); }
24
25 explicit Value(std::shared_ptr<Node> node) : node(node) {};
26
27 static Value create_value(double_t val, BinaryOperator op, const Value &lhs,
28 const Value &rhs) {
29 auto new_node = std::make_shared<Node>(val, op, lhs.node, rhs.node);
30 return Value(new_node);
31 };
32
33 double_t get_val() const { return this->node->val; }
34 BinaryOperator get_op() const { return this->node->op; }
35
36 double_t partial_derivate(const Value &a) const {
37 if (this->node == a.node) {
38 return 1.0;
39 }
40 double_t d = 0;
41 if (node->op != BinaryOperator::LEAF) {
42 Value o1(node->lhs);
43 Value o2(node->rhs);
44 if (get_op() == BinaryOperator::ADD) {
45 d = o1.partial_derivate(a) + o2.partial_derivate(a);
46 } else if (get_op() == BinaryOperator::MUL) {
47 d = o1.get_val() * o2.partial_derivate(a) +
48 o2.get_val() * o1.partial_derivate(a);
49 }
50 }
51 return d;
52 }
53};
54
55Value operator+(const Value &lhs, const Value &rhs) {
56 return Value::create_value(lhs.get_val() + rhs.get_val(), BinaryOperator::ADD,
57 lhs, rhs);
58}
59
60Value operator+(double_t lhs, const Value &rhs) {
61 Value lhs_value(lhs);
62 return operator+(lhs_value, rhs);
63}
64
65Value operator+(const Value &lhs, double_t rhs) {
66 Value rhs_value(rhs);
67 return operator+(lhs, rhs_value);
68}
69
70Value operator*(const Value &lhs, const Value &rhs) {
71 return Value::create_value(lhs.get_val() * rhs.get_val(), BinaryOperator::MUL,
72 lhs, rhs);
73}
74
75Value operator*(double_t lhs, const Value &rhs) {
76 Value lhs_value(lhs);
77 return operator*(lhs_value, rhs);
78}
79
80Value operator*(const Value &lhs, double_t rhs) {
81 Value rhs_value(rhs);
82 return operator*(lhs, rhs_value);
83}
84
85Value create_example(Value a, Value b) {
86 auto c = a + b;
87 auto d = a * b;
88 auto e = a * c;
89 auto f = c + d;
90 auto g = f + e;
91 return g;
92}
93
94// g = a + b + a * b + a * (a + b) = a + b + ab + aa + ab = aa + 2ba + a + b
95// dg/da = 2a + 2b + 1
96// dg/db = 2a + 1
97
98int main() {
99 Value a(2.0);
100 Value b(3.0);
101 auto g = create_example(a, b);
102 Value delta(0.01);
103
104 std::cout << "gradient of a by numerical: "
105 << (create_example(a + delta, b).get_val() - g.get_val()) /
106 delta.get_val()
107 << '\n';
108 std::cout << "gradient of a by autodiff: " << g.partial_derivate(a) << '\n';
109
110 std::cout << "gradient of b by numerical: "
111 << (create_example(a, b + delta).get_val() - g.get_val()) /
112 delta.get_val()
113 << '\n';
114 std::cout << "gradient of b by autodiff: " << g.partial_derivate(b) << '\n';
115}