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

test with 1 thread, fix 1 threaded segfault #154

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
version:
- 'nightly' # coverage fast on nightly
threads:
- '1'
- '3'
- '4'
steps:
Expand Down
66 changes: 42 additions & 24 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,21 @@ function train_unbatched_core!(
c::Chain,
pu::Ptr{UInt8},
pX,
it,
p::AbstractVector{T},
opt,
it,
mpt
) where {T}
numthreads = _numthreads()
glen = _try_static(numparam(getchain(c)), static_length(params))
glen = _try_static(numparam(getchain(c)), static_length(p))
aligned_glen = align(glen)
g = _alloc_grad(Ptr{T}(pu), glen, numthreads, aligned_glen)
offset = static_sizeof(T) * aligned_glen * numthreads
train_unbatched_core!(c, pu + offset, g, pX, it, p, opt, mpt)
if numthreads == 1
train_unbatched_core!(c, pu + offset, g[:, begin], pX, p, opt, it, mpt)
Copy link
Member

@mohamed82008 mohamed82008 Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you write a comment on why this is needed? can this branch be handled in train_unbatched_core! since all the other inputs are the same?

else
train_unbatched_core!(c, pu + offset, g, pX, p, opt, it, mpt)
end
end

"""
Expand Down Expand Up @@ -461,15 +465,15 @@ function train_unbatched!(
pX = maybe_static_size_arg(chn.inputdim, X)
optoff = optmemsize(opt, p)
@unpack layers = chn
glen = _try_static(numparam(chn), static_length(params))
glen = _try_static(numparam(chn), static_length(p))
numthreads = _numthreads()

T = Base.promote_eltype(p, X)
bytes_per_thread, total_bytes = required_bytes(
Val{T}(),
layers,
static_size(pX),
optoff + align(glen) * numthreads,
optoff + align(glen) * numthreads * static_sizeof(T),
static(0),
numthreads
)
Expand Down Expand Up @@ -634,18 +638,33 @@ function train_batched_core!(
T = Base.promote_eltype(p, pX)
g = _alloc_grad(Ptr{T}(pu), glen, numthreads, aligned_glen)
offset = static_sizeof(T) * aligned_glen * numthreads
train_batched_core!(
c,
pu + offset,
g,
p,
pX,
opt,
iters,
leaveofflast,
mpt,
N_bs
)
if numthreads == 1
train_batched_core!(
c,
pu + offset,
g[:, begin],
p,
pX,
opt,
iters,
leaveofflast,
mpt,
N_bs
)
else
train_batched_core!(
c,
pu + offset,
g,
p,
pX,
opt,
iters,
leaveofflast,
mpt,
N_bs
)
end
end
"""
train_batched!(g::AbstractVecOrMat, p, chn, X, opt, iters; batchsize = nothing)
Expand Down Expand Up @@ -705,15 +724,14 @@ function train_batched!(
align(sizeof(eltype(tgt)) * tgt_batch_len) +
align(sizeof(eltype(X)) * X_batch_len)
perm_mem = align(sizeof(Int) * N)
base_mem = optoff + perm_mem
T = Base.promote_eltype(p, X)
if g === nothing
base_mem =
optoff +
perm_mem +
align(_try_static(numparam(chn), static_length(p))) * nthread
else
base_mem = optoff + perm_mem
base_mem +=
align(_try_static(numparam(chn), static_length(p))) *
nthread *
static_sizeof(T)
end
T = Base.promote_eltype(p, X)
mpt, total_bytes =
required_bytes(Val{T}(), layers, sxb, base_mem, shuffle_per_thread, nthread)
GC.@preserve X begin
Expand Down
30 changes: 29 additions & 1 deletion test/mnist.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Test
ENV["DATADEPS_ALWAYS_ACCEPT"] = "true"
@testset "LeNet" begin
using SimpleChains, MLDatasets
using SimpleChains, MLDatasets, JET

lenet = SimpleChain(
(static(28), static(28), static(1)),
Expand Down Expand Up @@ -128,4 +128,32 @@ ENV["DATADEPS_ALWAYS_ACCEPT"] = "true"
@test a1 > 0.93
@test a3 > 0.95
end

@time SimpleChains.init_params!(p, lenet; rng = SimpleChains.local_rng())
@time SimpleChains.train_unbatched!(
p,
lenetloss,
xtrain4,
SimpleChains.ADAM(3e-4),
10
)
if VERSION >= v"1.10"
@test_opt SimpleChains.train_unbatched!(
p,
lenetloss,
xtrain4,
SimpleChains.ADAM(3e-4),
10
)
end
a4, l4 = SimpleChains.accuracy_and_loss(lenetloss, xtrain4, p)
@test l4 ≈ lenetloss(xtrain4, p)
a5, l5 = SimpleChains.accuracy_and_loss(lenetloss, xtest4, ytest1, p)
@test l5 ≈ SimpleChains.add_loss(lenetloss, LogitCrossEntropyLoss(ytest1))(
xtest4,
p
)
# TODO: unbatched training is currently much less effective...
@test a4 > 0.3
@test a5 > 0.3
end
Loading