|
| 1 | +loadleaf!(dst, src, err) = dst |
| 2 | +loadleaf!(dst::AbstractArray, src, err) = |
| 3 | + error("Tried to copy $src into an array destination; this is not allowed.") |
| 4 | +loadleaf!(dst, src::AbstractArray, err) = |
| 5 | + error("Tried to copy an array to $dst; this is not allowed.") |
| 6 | +function loadleaf!(dst::AbstractArray, src::Bool, err) |
| 7 | + if iszero(src) |
| 8 | + dst .= src |
| 9 | + else |
| 10 | + error("Cannot copy boolean parameter == true to non-zero parameter.") |
| 11 | + end |
| 12 | + return dst |
| 13 | +end |
| 14 | +loadleaf!(dst::Bool, src::AbstractArray, err) = iszero(dst) ? dst : |
| 15 | + error("Cannot copy non-zero parameter to boolean parameter == true.") |
| 16 | +function loadleaf!(dst::AbstractArray, src::AbstractArray, err) |
| 17 | + (size(dst) == size(src)) || throw(err) |
| 18 | + copyto!(dst, src) |
| 19 | +end |
| 20 | + |
| 21 | +_tie_check(dst::Bool, src::AbstractArray) = iszero(dst) || |
| 22 | + error("Encountered tied parameter with boolean source at some nodes and non-boolean sources at others.") |
| 23 | +_tie_check(dst::AbstractArray, src::Bool) = (iszero(dst) && iszero(src)) || |
| 24 | + error("Encountered tied parameter with boolean source at some nodes and non-boolean sources at others.") |
| 25 | +_tie_check(dst::AbstractArray, src::AbstractArray) = (dst == src) || |
| 26 | + error("Encountered tied destination parameters with untied and mismatched sources.") |
| 27 | +_tie_check(dst, src) = true |
| 28 | + |
| 29 | +_bool_tie_check(dst, src) = true |
| 30 | + |
| 31 | +""" |
| 32 | + loadmodel!(dst, src) |
| 33 | +
|
| 34 | +Copy all the parameters (trainable and non-trainable) from `src` into `dst`. |
| 35 | +
|
| 36 | +Recursively walks `dst` and `src` together using [`Functors.children`](@ref), |
| 37 | +and calling `copyto!` on parameter arrays or throwing an error when there is a mismatch. |
| 38 | +Non-array elements (such as activation functions) are not copied and need not match. |
| 39 | +Zero bias vectors and `bias=false` are considered equivalent |
| 40 | +(see extended help for more details). |
| 41 | +
|
| 42 | +# Examples |
| 43 | +```julia |
| 44 | +julia> dst = Chain(Dense(Flux.ones32(2, 5, tanh)), Dense(2 => 1; bias = [1f0])) |
| 45 | +Chain( |
| 46 | + Dense(5 => 2, tanh), # 12 parameters |
| 47 | + Dense(2 => 1), # 3 parameters |
| 48 | +) # Total: 4 arrays, 15 parameters, 316 bytes. |
| 49 | +
|
| 50 | +julia> dst[1].weight ≈ ones(2, 5) # by construction |
| 51 | +true |
| 52 | +
|
| 53 | +julia> src = Chain(Dense(5 => 2, relu), Dense(2 => 1, bias=false)); |
| 54 | +
|
| 55 | +julia> Flux.loadmodel!(dst, src); |
| 56 | +
|
| 57 | +julia> dst[1].weight ≈ ones(2, 5) # values changed |
| 58 | +false |
| 59 | +
|
| 60 | +julia> iszero(dst[2].bias) |
| 61 | +true |
| 62 | +``` |
| 63 | +
|
| 64 | +# Extended help |
| 65 | +
|
| 66 | +Throws an error when: |
| 67 | +- `dst` and `src` do not share the same fields (at any level) |
| 68 | +- the sizes of leaf nodes are mismatched between `dst` and `src` |
| 69 | +- copying non-array values to/from an array parameter |
| 70 | + (except inactive parameters described below) |
| 71 | +- `dst` is a "tied" parameter (i.e. refers to another parameter) and |
| 72 | + loaded into multiple times with mismatched source values |
| 73 | +
|
| 74 | +Inactive parameters can be encoded by using the boolean value `false` instead of an array. |
| 75 | +If `dst == false` and `src` is an all-zero array, no error will be raised (and no values copied); |
| 76 | +however, attempting to copy a non-zero array to an inactive parameter will throw an error. |
| 77 | +Likewise, copying a `src` value of `false` to any `dst` array is valid, |
| 78 | +but copying a `src` value of `true` will error. |
| 79 | +""" |
| 80 | +function loadmodel!(dst, src; cache = Base.IdSet()) |
| 81 | + ldsts, _ = functor(dst) |
| 82 | + lsrcs, _ = functor(src) |
| 83 | + (keys(ldsts) == keys(lsrcs)) || |
| 84 | + throw(ArgumentError("Tried to load $src into $dst but the structures do not match.")) |
| 85 | + |
| 86 | + err = DimensionMismatch("Tried to load $src into $dst but the parameter sizes do not match.") |
| 87 | + foreach(ldsts, lsrcs) do ldst, lsrc |
| 88 | + if ldst in cache # we already loaded this parameter before |
| 89 | + _tie_check(ldst, lsrc) && return ldst |
| 90 | + elseif Functors.isleaf(ldst) # our first time loading this leaf |
| 91 | + push!(cache, ldst) |
| 92 | + loadleaf!(ldst, lsrc, err) |
| 93 | + else # this isn't a leaf |
| 94 | + loadmodel!(ldst, lsrc; cache = cache) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + return dst |
| 99 | +end |
0 commit comments