Skip to content

Commit efa9790

Browse files
committed
Documentation for autodiff.
1 parent 9f7bd9f commit efa9790

File tree

5 files changed

+455
-382
lines changed

5 files changed

+455
-382
lines changed

docs/cpp2/metafunctions.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,36 @@ main: () = {
367367

368368
### For computational and functional types
369369

370+
#### `autodiff`
371+
372+
A `autodiff` type is extended so that derivatives can be computed. The metafunction adds for each function and member function a differentiated version. **This is a proof of concept implementation. Expect it to break.**
373+
A simple hello diff example is:
374+
```
375+
ad: @autodiff type = {
376+
func: (x: double) -> (r: double) = {
377+
r = x * x;
378+
}
379+
}
380+
381+
main: (args) = {
382+
x := 3.0;
383+
x_d := 1.0;
384+
385+
r := ad::func_d(x, x_d);
386+
387+
std::cout << "Derivative of 'x*x' at (x)$ is (r.r_d)$" << std::endl;
388+
}
389+
```
390+
391+
The `@autodiff` metafunction mostly supports the forward mode of algorithmic differentiation. The reverse mode is only partly implemented and not yet well tested.
392+
See [Supported autodiff features](../notes/autodiff_status.md) for a list of supported language features.
393+
394+
Options can be given by text template arguments, e.g. `@autodiff<"reverse">` enables the reverse mode.
395+
| Option | Description |
396+
| `"reverse"` | Reverse mode algorithmic differentiation. Default suffix `_b`. |
397+
| `"order=<n>"` | Higher order derivatives. `<n>` can be arbitrary. See `regression-tests/pure2-autodiff-higher-order.cpp2` for examples. |
398+
| `"suffix=<s>"` | Change the forward mode suffix. Can be used to apply autodiff multiple times. E.g. `@autodiff @autodiff<"suffix=_d2">`. |
399+
| `"rws_suffix=<s>"` | Change the reverse mode suffix. |
370400

371401
#### `regex`
372402

docs/notes/autodiff_status.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Supported algorithmic differentiation (autodiff) features
2+
3+
The listings might be incomplete. If something is missing, it is not supported. Algorithmic differentiation is applied via the [`autodiff` metafunction](../cpp2/metafunctions.md#autodiff). Maybe the planned features are added in 2026. Do not wait for them. The autodif feature is a proof of concept implementation.
4+
5+
** Reverse mode algorithmic differentiation is very experimental. Expect it to break. **
6+
7+
## Currently supported or planned features
8+
9+
| Description | Status forward | Status reverse |
10+
| --- | --- | --- |
11+
| Type definitions (structures) | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
12+
| Member values | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
13+
| Member functions | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
14+
| Function arguments | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
15+
| Function return arguments | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
16+
| Addition and multiplication | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
17+
| Prefix addition and subtraction | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
18+
| Static member function calls | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
19+
| Member function calls | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
20+
| Function calls | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
21+
| Math functions (sin, cos, exp, sqrt) | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
22+
| If else | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
23+
| Return statement | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
24+
| Intermediate variables | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
25+
| Passive variables | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
26+
| While loop | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
27+
| Do while loop | <span style="color:green">Supported</span> | <span style="color:gray">Planned</span> |
28+
| For loop | <span style="color:green">Supported</span> | <span style="color:green">Supported</span> |
29+
| Template arguments | <span style="color:gray">Planned</span> | <span style="color:gray">Planned</span> |
30+
| Lambda functions | <span style="color:gray">Planned</span> | <span style="color:gray">Planned</span> |
31+
32+
33+
34+

regression-tests/test-results/pure2-autodiff.cpp2.output

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,5 +1439,6 @@ ad_test_2:/* @autodiff<"order=2"> @print */ type =
14391439
return;
14401440
}
14411441
}
1442-
ok (all Cpp2, passes safety checks)
1442+
1443+
pure2-autodiff.cpp2(195,1): error: while applying @autodiff - AD: Warning reverse mode differentiation is very experimental.
14431444

0 commit comments

Comments
 (0)