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 1 commit
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
50 changes: 32 additions & 18 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,30 @@ julia> autodiff(Forward, rosenbrock_inp, BatchDuplicated, BatchDuplicated(x, (dx

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
```jldoctest storage
function f(x, tmp, n)
tmp[1] = 1
for i in 1:n
tmp[1] *= x
end
tmp[1]
end
end;
```

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

# 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))
((0.0, nothing, nothing),)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
```

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

# output

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

### CUDA.jl support
Expand All @@ -158,21 +168,25 @@ 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

1-element SparseVector{Float64, Int64} with 0 stored entries
```

# Incorrect: SparseMatrixCSC drops explicit zeros
# returns 1-element SparseVector{Float64, Int64} with 0 stored entries
da=sparse([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

# Correct: Prevent SparseMatrixCSC from dropping zeros
# returns 1-element SparseVector{Float64, Int64} with 1 stored entry:
# [1] = 0.0
da=sparsevec([1], [0.0])
# output

Enzyme.autodiff(Reverse, f, Active, Duplicated(a, da))
@show da
1-element SparseVector{Float64, Int64} with 1 stored entry:
[1] = 1.0
```
Loading