Skip to content

Commit 9b34cc6

Browse files
committed
Add activity analysis example for Enzyme.jl
1 parent 76f3257 commit 9b34cc6

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"cells": [
3+
{
4+
"attachments": {},
5+
"cell_type": "markdown",
6+
"metadata": {},
7+
"source": [
8+
"# Installing Enzyme"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"metadata": {},
15+
"outputs": [],
16+
"source": [
17+
"import Pkg\n",
18+
"Pkg.add(\"Enzyme\")"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {},
25+
"outputs": [],
26+
"source": [
27+
"using Enzyme"
28+
]
29+
},
30+
{
31+
"attachments": {},
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"## Activity Annotations\n",
36+
"\n",
37+
"* `Const`\n",
38+
"* `Active`\n",
39+
"* `Duplicated`\n",
40+
"* `DuplicatedNoNeed`"
41+
]
42+
},
43+
{
44+
"attachments": {},
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"square(x) = x^2"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"autodiff(Reverse, square, 1.0)"
58+
]
59+
},
60+
{
61+
"attachments": {},
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"Default activity for values is `Const`"
66+
]
67+
},
68+
{
69+
"attachments": {},
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"autodiff(Reverse, square, Const(1.0))"
74+
]
75+
},
76+
{
77+
"attachments": {},
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"When adding the `Active` annotation Enzyme differentiates with respect to the argument"
82+
]
83+
},
84+
{
85+
"attachments": {},
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"autodiff(Reverse, square, Active(1.0))"
90+
]
91+
}
92+
],
93+
"metadata": {
94+
"kernelspec": {
95+
"display_name": "Python 3",
96+
"language": "python",
97+
"name": "python3"
98+
},
99+
"language_info": {
100+
"name": "python",
101+
"version": "3.11.1"
102+
},
103+
"orig_nbformat": 4,
104+
"vscode": {
105+
"interpreter": {
106+
"hash": "5c7b89af1651d0b8571dde13640ecdccf7d5a6204171d6ab33e7c296e100e08a"
107+
}
108+
}
109+
},
110+
"nbformat": 4,
111+
"nbformat_minor": 2
112+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"data":{"layout-restorer:data":{"main":{"dock":{"type":"tab-area","currentIndex":0,"widgets":["notebook:activity.ipynb"]},"current":"notebook:activity.ipynb"},"down":{"size":0,"widgets":[]},"left":{"collapsed":false,"current":"filebrowser","widgets":["filebrowser","running-sessions","@jupyterlab/toc:plugin","extensionmanager.main-view"]},"right":{"collapsed":true,"widgets":["jp-property-inspector","debugger-sidebar"]},"relativeSizes":[0.15111378687537627,0.8488862131246238,0]},"notebook:activity.ipynb":{"data":{"path":"activity.ipynb","factory":"Notebook"}}},"metadata":{"id":"default"}}

julia_activity/activity.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Enzyme
2+
using Printf
3+
4+
# Defining the square function
5+
square(x) = x^2;
6+
7+
# No activity annotations
8+
result_1 = Enzyme.autodiff(Reverse, square, 1.0)
9+
printf("No annotations result: %f", result_1)
10+
11+
# No activity annotations = constant annotation
12+
result_2 = Enzyme.autodiff(Reverse, square, Const(1.0))
13+
printf("Equals constant annotations: %f", result_2)
14+
15+
# Adding activity annotations
16+
result_3 = Enzyme.autodiff(Reverse, square, Active(1.0))
17+
printf("Adding activity annotations: %f", result_3)

0 commit comments

Comments
 (0)