Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions DerivativeCoverProblems/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ p = quadratic(6; rank=2, form=:factored)
c = cover_hessian(p.f, p.x0)
H = exact_hessian(p)

all(abs.(H) .<= c.h_sum * c.h_sum') # true: the cover is guaranteed
max_coverage_ratio(c.h_sum, H) # 0.879: and it is nearly tight
all(abs.(H) .<= c.h * c.h') # true: the cover is guaranteed
max_coverage_ratio(c.h, H) # 0.879: and it is nearly tight
```

The ratio approaches `1` as the cover approaches the tightest cover that the Hessian admits.
Expand Down
6 changes: 3 additions & 3 deletions DerivativeCovers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ julia> f(x) = exp(x[1]) + x[1] * x[2]^2;

julia> c = cover_hessian(f, [0.5, 2.0]);

julia> c.h_sum # abs(H[i,j]) <= h[i] * h[j]
julia> c.h # abs(H[i,j]) <= h[i] * h[j]
2-element Vector{Float64}:
4.112452541433932
2.414213562373095
Expand All @@ -44,7 +44,7 @@ julia> c.grad

julia> H = [exp(0.5) 4.0; 4.0 1.0]; # the exact Hessian, for comparison

julia> all(abs.(H) .<= c.h_sum .* c.h_sum')
julia> all(abs.(H) .<= c.h .* c.h')
true
```

Expand All @@ -53,7 +53,7 @@ Rescaling the inputs rescales the cover:
```julia
julia> D = [10.0, 0.1];

julia> cover_hessian(y -> f(y ./ D), D .* [0.5, 2.0]).h_sum ≈ c.h_sum ./ D
julia> cover_hessian(y -> f(y ./ D), D .* [0.5, 2.0]).h ≈ c.h ./ D
true
```

Expand Down
18 changes: 9 additions & 9 deletions DerivativeCovers/docs/src/covariance.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ julia> D = [10.0, 0.1];

julia> g(y) = f(y ./ D);

julia> cover_hessian(g, D .* x0).h_sum ≈ cover_hessian(f, x0).h_sum ./ D
julia> cover_hessian(g, D .* x0).h ≈ cover_hessian(f, x0).h ./ D
true

julia> cover_hessian(x -> 9 * f(x), x0).h_sum ≈ 3 * cover_hessian(f, x0).h_sum
julia> cover_hessian(x -> 9 * f(x), x0).h ≈ 3 * cover_hessian(f, x0).h
true
```

Expand All @@ -59,7 +59,7 @@ transform correctly under any rescaling of `x` and `y`. In the example above,
adds `sqrt(exp(0.5)) ≈ 1.284` for the `exp` term:

```jldoctest cov
julia> cover_hessian(f, x0).h_sum
julia> cover_hessian(f, x0).h
2-element Vector{Float64}:
0.5
3.2840254166877414
Expand Down Expand Up @@ -89,12 +89,12 @@ julia> cover_hessian(f, x0).nguarded
julia> cover_hessian(f, x1).nguarded
1

julia> cover_hessian(f, x1).h_sum
julia> cover_hessian(f, x1).h
2-element Vector{Float64}:
1.0
2.0

julia> cover_hessian(f, x1; scales=[1.0, 100.0]).h_sum
julia> cover_hessian(f, x1; scales=[1.0, 100.0]).h
2-element Vector{Float64}:
10.0
1.1
Expand All @@ -105,7 +105,7 @@ When the inputs are rescaled, a cover that depends on declared scales is
covariant only if the scales are rescaled along with them, `scales → D*scales`:

```jldoctest cov
julia> cover_hessian(g, D .* x1; scales=D).h_sum .* D
julia> cover_hessian(g, D .* x1; scales=D).h .* D
2-element Vector{Float64}:
1.0
2.0
Expand Down Expand Up @@ -160,12 +160,12 @@ julia> g2(y) = f2(y ./ D);
julia> cover_hessian(f2, x1).nguarded
1

julia> cover_hessian(g2, D .* x1).h_sum .* D ≈ cover_hessian(f2, x1).h_sum
julia> cover_hessian(g2, D .* x1).h .* D ≈ cover_hessian(f2, x1).h
false

julia> h = cover_hessian(f2, x1; scales=CurvatureScales()).h_sum;
julia> h = cover_hessian(f2, x1; scales=CurvatureScales()).h;

julia> cover_hessian(g2, D .* x1; scales=CurvatureScales()).h_sum .* D ≈ h
julia> cover_hessian(g2, D .* x1; scales=CurvatureScales()).h .* D ≈ h
true
```

Expand Down
35 changes: 24 additions & 11 deletions DerivativeCovers/docs/src/covers.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ julia> x0 = [0.5, 2.0];

julia> c = cover_hessian(f, x0);

julia> round.(c.h_sum; digits=4)
julia> round.(c.h; digits=4)
2-element Vector{Float64}:
4.1125
2.4142
Expand Down Expand Up @@ -84,13 +84,26 @@ julia> round.(d.B; digits=4)

Here `B` equals `abs.(H)`, because no cancellation occurs in this program.

The default result also includes the sign-averaged cover `h_var`, the
root-sum-square of the same contributions that `h_sum` adds. It is the
root-mean-square size of the sum if each contribution carried an independent
random sign. **`h_var` does not cover the Hessian**: it can fall below the true
scale where several contributions share a sign. It costs nothing extra, and the
ratio `h_sum ./ h_var` indicates how much the contributions to each coordinate
overlap.
Passing `merge = SignAveragedMerge()` returns the sign-averaged cover in place
of the sum cover, from a sweep of the same cost. It is the root-sum-square of
the same contributions that the sum cover adds: the root-mean-square size of the sum
if each contribution carried an independent random sign. **The sign-averaged
cover does not cover the Hessian**: it can fall below the true scale where
several contributions share a sign. The type of the result records which cover
it holds:

```jldoctest hess
julia> v = cover_hessian(f, x0; merge = SignAveragedMerge());

julia> v isa HessianCover{Float64, Float64, SignAveragedMerge}
true

julia> all(v.h .<= c.h)
true
```

The ratio `c.h ./ v.h` of the two covers indicates how much the contributions
to each coordinate overlap.

### Between the two merges

Expand Down Expand Up @@ -143,9 +156,9 @@ By default the residuals share one channel in the reverse sweep, for a cost of
cost to ``O(GL)``; `ngroups=nothing` gives each of the `m` residuals its own
channel, the tightest covers available. Summing residuals into one channel
loosens `h_gn[j]` by at most ``\sqrt{n_j}``, where ``n_j`` is the number of
residuals input `j` reaches. The
sign-averaged covers `h_var` and `h_var_gn` are the analogs of `h_var` for
[`cover_hessian`](@ref).
residuals input `j` reaches. With `merge = SignAveragedMerge()`, `h` and
`h_gn` are instead the sign-averaged covers of the two matrices, the analogs of
the sign-averaged cover of [`cover_hessian`](@ref).

## Jacobian covers

Expand Down
6 changes: 3 additions & 3 deletions DerivativeCovers/docs/src/extending.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ julia> using DerivativeCovers

julia> f(x) = DerivativeCovers.value(x[1]) > 0 ? x[1]^2 * x[2] : -x[1] * x[2];

julia> round.(cover_hessian(f, [1.0, 2.0]).h_sum; digits=4)
julia> round.(cover_hessian(f, [1.0, 2.0]).h; digits=4)
2-element Vector{Float64}:
4.8284
0.7071
Expand Down Expand Up @@ -77,7 +77,7 @@ julia> function Base.log1p(x::DerivativeCovers.CoverVar)

julia> g(x) = log1p(x[1] * x[2]);

julia> round.(cover_hessian(g, [0.5, 2.0]).h_sum; digits=4)
julia> round.(cover_hessian(g, [0.5, 2.0]).h; digits=4)
2-element Vector{Float64}:
2.4142
0.6036
Expand Down Expand Up @@ -114,7 +114,7 @@ julia> function outer(x)

julia> x0 = [0.3, 0.2, -0.4];

julia> round.(cover_hessian(outer, x0).h_sum; digits=4)
julia> round.(cover_hessian(outer, x0).h; digits=4)
3-element Vector{Float64}:
3.2057
3.2057
Expand Down
20 changes: 10 additions & 10 deletions DerivativeCovers/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ julia> p0 = [1800.0, 0.0035];

julia> c = cover_hessian(loss, p0);

julia> round.(c.h_sum; sigdigits=4)
julia> round.(c.h; sigdigits=4)
2-element Vector{Float64}:
3.118
1.123e6
Expand All @@ -67,7 +67,7 @@ julia> round.(c.grad; sigdigits=4)
```

[`cover_hessian`](@ref) returns the gradient `c.grad` along with the cover
`c.h_sum`. The two entries of each have different units: the first entry of
`c.h`. The two entries of each have different units: the first entry of
the gradient is in units of the loss per unit of amplitude, and the second in
units of the loss per second. Expressing the time constant in milliseconds
instead of seconds changes the second entry of both the gradient and the cover
Expand All @@ -80,7 +80,7 @@ julia> q0 = [1800.0, 3.5];

julia> cms = cover_hessian(loss_ms, q0);

julia> round.(cms.h_sum ./ c.h_sum; sigdigits=4)
julia> round.(cms.h ./ c.h; sigdigits=4)
2-element Vector{Float64}:
1.0
0.001
Expand All @@ -95,7 +95,7 @@ The cover changed exactly as the units did, so the gradient measured relative
to the cover, `g ./ h`, did not change at all:

```jldoctest decay
julia> c.grad ./ c.h_sum ≈ cms.grad ./ cms.h_sum
julia> c.grad ./ c.h ≈ cms.grad ./ cms.h
true
```

Expand All @@ -113,15 +113,15 @@ julia> E = eachcol(Matrix(1.0I, 2, 2));

julia> H = reduce(hcat, [hvp(loss, p0, e) for e in E]);

julia> all(abs.(H) .<= c.h_sum .* c.h_sum')
julia> all(abs.(H) .<= c.h .* c.h')
true
```

The same reasoning applies to the Hessian. Dividing it by the cover gives a
matrix `Hhat` whose entries lie in `[-1, 1]` and do not depend on the units:

```jldoctest decay
julia> Hhat = H ./ (c.h_sum .* c.h_sum');
julia> Hhat = H ./ (c.h .* c.h');

julia> round.(Hhat; digits=3)
2×2 Matrix{Float64}:
Expand All @@ -130,7 +130,7 @@ julia> round.(Hhat; digits=3)

julia> Hms = reduce(hcat, [hvp(loss_ms, q0, e) for e in E]);

julia> Hms ./ (cms.h_sum .* cms.h_sum') ≈ Hhat
julia> Hms ./ (cms.h .* cms.h') ≈ Hhat
true
```

Expand All @@ -140,16 +140,16 @@ true

| Function | Bounds | Cost | Certified |
|---|---|---|---|
| [`cover_hessian`](@ref) | Hessian of a scalar function | ``O(L)`` | `h_sum` yes; `h_var` no |
| [`cover_hessian`](@ref) | Hessian of a scalar function | ``O(L)`` | with [`SumMerge`](@ref), yes; with [`SignAveragedMerge`](@ref), no |
| [`cover_hessian`](@ref)`(WithDiagnostics(), …)` | adds a tighter cover `h_quad` and an elementwise bound | ``O(nL)`` time, ``O(n^2)`` memory | yes |
| [`DerivativeCovers.cover_hybrid`](@ref cover_hybrid) | Hessian, between `h_sum` and `h_quad` | ``O(GL)`` for `G` groups | yes |
| [`forward_cover`](@ref) | Hessian, without recording the program | ``O(nL)`` time, ``O(n)`` memory per live variable | yes |
| [`cover_nls`](@ref) | Hessian and Gauss–Newton matrix of ``\tfrac12\lVert r(x)\rVert^2`` | ``O(L)``; ``O(GL)`` with `G` residual channels | `h`, `h_gn` yes; `h_var`, `h_var_gn` no |
| [`cover_nls`](@ref) | Hessian and Gauss–Newton matrix of ``\tfrac12\lVert r(x)\rVert^2`` | ``O(L)``; ``O(GL)`` with `G` residual channels | with [`SumMerge`](@ref), yes; with [`SignAveragedMerge`](@ref), no |
| [`cover_jacobian`](@ref) | Jacobian of a vector-valued function | a few ``O(L)`` sweeps | yes |
| [`DerivativeCovers.cover_jacobian_hybrid`](@ref cover_jacobian_hybrid) | Jacobian, tighter than the default | ``O(GL)`` for `G` groups | yes |

For most uses, [`cover_hessian`](@ref) and [`cover_jacobian`](@ref) are the
place to start. The ``O(L)`` cover `h_sum` can be loose when many paths through
place to start. The ``O(L)`` sum cover can be loose when many paths through
the program reconverge; [Computing covers](@ref) describes the options that
trade cost for tightness, and [Tightness](@ref) describes how to measure it.

Expand Down
4 changes: 2 additions & 2 deletions DerivativeCovers/docs/src/intervals.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ julia> X = interval.([0.4, 1.0], [0.6, 1.5]); # 0.4 ≤ x[1] ≤ 0.6, 1.0 ≤

julia> c = cover_hessian(f, X);

julia> round.(c.h_sum; digits=4)
julia> round.(c.h; digits=4)
2-element Vector{Float64}:
3.2864
2.6446

julia> H(x) = [exp(x[1]) 2x[2]; 2x[2] 2x[1]]; # the exact Hessian, for comparison

julia> all(all(abs.(H([x1, x2])) .<= c.h_sum .* c.h_sum') for x1 in 0.4:0.05:0.6, x2 in 1.0:0.1:1.5)
julia> all(all(abs.(H([x1, x2])) .<= c.h .* c.h') for x1 in 0.4:0.05:0.6, x2 in 1.0:0.1:1.5)
true
```

Expand Down
8 changes: 8 additions & 0 deletions DerivativeCovers/docs/src/reference/covers.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ WithDiagnostics
CurvatureScales
```

## Merge rules

```@docs
MergeRule
SumMerge
SignAveragedMerge
```

## Least-squares objectives

```@docs
Expand Down
9 changes: 5 additions & 4 deletions DerivativeCovers/docs/src/tightness.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ julia> using DerivativeCovers, DerivativeCoverProblems, MatrixCovers
julia> function ratios(form)
p = quadratic(6; rank=2, form)
H = exact_hessian(p)
hsum = cover_hessian(p.f, p.x0).h_sum
hsum = cover_hessian(p.f, p.x0).h
hquad = cover_hessian(WithDiagnostics(), p.f, p.x0).h_quad
return round.((max_coverage_ratio(hsum, H), max_coverage_ratio(hquad, H)); digits=3)
end;
Expand All @@ -79,8 +79,9 @@ product, and all of them reconverge on the inputs. The factored form has only
`rank` nonlinear operations, which do not overlap. The quadrature merge
recovers part of the difference.

Without the Hessian, the ratio `h_sum ./ h_var` of the two vectors in a
[`HessianCover`](@ref) estimates how much the contributions to each coordinate
Without the Hessian, the ratio of the sum cover to the sign-averaged cover,
the covers [`cover_hessian`](@ref) returns for `merge = SumMerge()` and
`merge = SignAveragedMerge()`, estimates how much the contributions to each coordinate
overlap: it is 1 when each coordinate receives a single contribution and grows
with the number of overlapping contributions.

Expand Down Expand Up @@ -118,7 +119,7 @@ julia> c = cover_hessian(p.f, p.x0);

julia> H = exact_hessian(p);

julia> all(abs.(H) .<= c.h_sum .* c.h_sum')
julia> all(abs.(H) .<= c.h .* c.h')
true
```

Expand Down
2 changes: 1 addition & 1 deletion DerivativeCovers/docs/src/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ julia> x0 = [0.5, -1.2, 0.3, 0.8];

julia> t, out = tape_function(f, x0);

julia> h = cover_hessian(t, out).h_sum;
julia> h = cover_hessian(t, out).h;

julia> H = reduce(hcat, [hvp(t, out, e) for e in eachcol(Matrix(1.0I, 4, 4))]); # for comparison
```
Expand Down
4 changes: 4 additions & 0 deletions DerivativeCovers/src/DerivativeCovers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export Tape, TVar, tape_function
export FVar, forward_cover, ForwardCover
export cover_hessian, cover_jacobian, cover_nls
export CoverOnly, WithDiagnostics, CurvatureScales
export SumMerge, SignAveragedMerge
export HessianCover, HessianDiagnostics
export JacobianCover, JacobianDiagnostics
export NLSCover, NLSDiagnostics
Expand Down Expand Up @@ -61,6 +62,8 @@ export hvp, hvp!, jvp, jvp!, vjp, vjp!
:CoverSite, :TapeSite, :site_label, :site_values
)
)
# The supertype of the merge rules that the result types carry as a parameter.
eval(Expr(:public, :MergeRule))
# Norms and rescalings of a matrix normalized by its cover.
eval(Expr(:public, :scaled_spectral_radius, :scaled_singular_value, :cover_normalize))
# Covers that merge in groups, interpolating between the sum and quadrature merges.
Expand All @@ -81,6 +84,7 @@ include("rounding.jl")
include("curvature.jl")
include("tape.jl")
include("overloads.jl")
include("mergerules.jl")
include("covers.jl")
include("curvaturescales.jl")
include("forward.jl")
Expand Down
Loading
Loading