Skip to content

Commit 31267ab

Browse files
authored
Rename iswriteable (#102)
* rename iswriteable to maywrite * add deprecation * v0.2.8
1 parent 71c5a75 commit 31267ab

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Optimisers"
22
uuid = "3bd65402-5787-11e9-1adc-39752487f4e2"
33
authors = ["Mike J Innes <mike.j.innes@gmail.com>"]
4-
version = "0.2.7"
4+
version = "0.2.8"
55

66
[deps]
77
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/Optimisers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ which will be subtracted from the parameters, and the updated state (if any) for
2424
the next iteration, as a tuple `(state, gradient)`.
2525
2626
For efficiency it is free to mutate the old state, but only what is returned will be used.
27-
Ideally this should check `iswriteable(x)`, which the built-in rules do via [`@..`](@ref).
27+
Ideally this should check `maywrite(x)`, which the built-in rules do via [`@..`](@ref).
2828
2929
The initial state is `init(rule::RuleType, parameters)`.
3030

src/interface.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function setup(rule, x; seen = Base.IdSet())
2424
end
2525
end
2626

27-
subtract!(x, x̄) = iswriteable(x) ? (x .= x .- x̄) : eltype(x).(x .- x̄)
27+
subtract!(x, x̄) = maywrite(x) ? (x .= x .- x̄) : eltype(x).(x .- x̄)
2828

2929
update!(::Nothing, x, ::Zero, ::Zero...) = nothing, x
3030
update!(::Nothing, x, x̄s...) = nothing, x
@@ -44,8 +44,8 @@ function update!(tree, x, x̄s...)
4444
end
4545

4646
function update(tree, x, x̄s...)
47-
t′ = fmap(copy, tree; exclude = iswriteable)
48-
x′ = fmap(copy, x; exclude = iswriteable)
47+
t′ = fmap(copy, tree; exclude = maywrite)
48+
x′ = fmap(copy, x; exclude = maywrite)
4949
update!(t′, x′, x̄s...)
5050
end
5151

@@ -56,8 +56,17 @@ isnumeric(x::AbstractArray{<:Number}) = isleaf(x) # isleaf to allow for e.g. tr
5656
isnumeric(x::AbstractArray{<:Integer}) = false
5757
isnumeric(x) = false
5858

59-
iswriteable(::DenseArray) = true # more elaborate versions are possible, wait until needed?
60-
iswriteable(_) = false
59+
"""
60+
maywrite(x) -> Bool
61+
62+
Should return `true` if we are completely sure that `update!` can write new
63+
values into `x`. Otherwise `false`, indicating a non-mutating path.
64+
For now, simply `x isa DenseArray` allowing `Array`, `CuArray`, etc.
65+
"""
66+
maywrite(::DenseArray) = true # see https://github.com/FluxML/Optimisers.jl/issues/99 for discussion
67+
maywrite(_) = false
68+
69+
@deprecate iswriteable maywrite false # remove when releasing Optimisers@0.3
6170

6271
"""
6372
trainable(x::Layer) -> NamedTuple
@@ -84,13 +93,13 @@ end
8493
@.. x = x + y
8594
8695
Sometimes in-place broadcasting macro, for use in `apply!` rules.
87-
If `iswriteable(x)` then it is just `@. x = rhs`, but if not, it becomes `x = @. rhs`.
96+
If `maywrite(x)` then it is just `@. x = rhs`, but if not, it becomes `x = @. rhs`.
8897
"""
8998
macro var".."(ex)
9099
Meta.isexpr(ex, :(=)) || throw("the macro @.. only accepts assignment, like @.. x = y + z")
91100
dst = esc(ex.args[1])
92101
src = esc(Broadcast.__dot__(ex.args[2]))
93-
:($dst = if $iswriteable($dst)
102+
:($dst = if $maywrite($dst)
94103
$dst .= $src
95104
else
96105
$src

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Optimisers.trainable(x::TwoThirds) = (a = x.a,)
2525

2626
s2, m2 = Optimisers.update(s, m, g)
2727
@test m[1] == 1:2 # not mutated
28-
@test Optimisers.iswriteable(m[1])
28+
@test Optimisers.maywrite(m[1])
2929
@test m2[1] [1,2] .- 0.1 .* [25, 33]
3030

3131
s3, m3 = Optimisers.update!(s, m, g)

0 commit comments

Comments
 (0)