Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn code samples into doctests on first page of docs #1187

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,34 @@ julia> # Again, the optinal chunk size argument allows us to use vector forward

If you pass in any temporary storage which may be involved in an active computation to a function you want to differentiate, you must also pass in a duplicated temporary storage for use in computing the derivatives.

```julia
function f(x, tmp, n)
tmp[1] = 1
```jldoctest storage
function f(x, tmp, k, n)
tmp[1] = 1.0
for i in 1:n
tmp[1] *= x
tmp[k] *= x
end
tmp[1]
end

# Incorrect [ returns (0.0,) ]
Enzyme.autodiff(f, Active(1.2), Const(Vector{Float64}(undef, 1)), Const(5))
# output

# Correct [ returns (10.367999999999999,) == 1.2^4 * 5 ]
Enzyme.autodiff(f, Active(1.2), Duplicated(Vector{Float64}(undef, 1), Vector{Float64}(undef, 1)), Const(5))
f (generic function with 1 method)
```

```jldoctest storage
Enzyme.autodiff(Reverse, f, Active(1.2), Const(Vector{Float64}(undef, 1)), Const(1), Const(5)) # Incorrect

# output

((0.0, nothing, nothing, nothing),)
```

```jldoctest storage
Enzyme.autodiff(Reverse, f, Active(1.2), Duplicated(Vector{Float64}(undef, 1), Vector{Float64}(undef, 1)), Const(1), Const(5)) # Correct (returns 10.367999999999999 == 1.2^4 * 5)

# output

((10.367999999999999, nothing, nothing, nothing),)
```

### CUDA.jl support
Expand All @@ -232,21 +246,28 @@ correctly and thus returns fundamentally wrong results.

At the moment there is limited support for sparse linear algebra operations. Sparse arrays may be used, but care must be taken because backing arrays drop zeros in Julia (unless told not to).

```julia
```jldoctest sparse
using SparseArrays
a = sparse([2.0])
da1 = sparse([0.0]) # Incorrect: SparseMatrixCSC drops explicit zeros
Enzyme.autodiff(Reverse, sum, Active, Duplicated(a, da1))
da1

a=sparse([2.0])
f(a)=sum(a)
# output

# Incorrect: SparseMatrixCSC drops explicit zeros
# returns 1-element SparseVector{Float64, Int64} with 0 stored entries
da=sparse([0.0])
1-element SparseVector{Float64, Int64} with 0 stored entries
```

# Correct: Prevent SparseMatrixCSC from dropping zeros
# returns 1-element SparseVector{Float64, Int64} with 1 stored entry:
# [1] = 0.0
da=sparsevec([1], [0.0])
```jldoctest sparse
da2 = sparsevec([1], [0.0]) # Correct: Prevent SparseMatrixCSC from dropping zeros
gdalle marked this conversation as resolved.
Show resolved Hide resolved
Enzyme.autodiff(Reverse, sum, Active, Duplicated(a, da2))
da2

Enzyme.autodiff(Reverse, f, Active, Duplicated(a, da))
@show da
# output

1-element SparseVector{Float64, Int64} with 1 stored entry:
[1] = 1.0
```

Sometimes, determining how to perform this zeroing can be complicated.
That is why Enzyme provides a helper function `Enzyme.make_zero` that does this automatically.
Loading