-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the distributed backends
- Loading branch information
Showing
17 changed files
with
272 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Lux, MPI, NCCL, Test | ||
|
||
const input_args = length(ARGS) == 3 ? ARGS[1:3] : | ||
(length(ARGS) == 2 ? (ARGS[2], "mpi") : ("CPU", "mpi")) | ||
const backend_type = input_args[2] == "nccl" ? NCCLBackend : MPIBackend | ||
const dev = input_args[1] == "CPU" ? LuxCPUDevice() : | ||
(input_args[1] == "CUDA" ? LuxCUDADevice() : LuxAMDGPUDevice()) | ||
|
||
DistributedUtils.initialize(backend_type) | ||
backend = DistributedUtils.get_distributed_backend(backend_type) | ||
|
||
@test DistributedUtils.initialized(backend_type) | ||
|
||
# Should always hold true | ||
@test DistributedUtils.local_rank(backend) < DistributedUtils.total_workers(backend) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Lux, MLUtils, MPI, NCCL, Random, Test | ||
|
||
const input_args = length(ARGS) == 3 ? ARGS[1:3] : | ||
(length(ARGS) == 2 ? (ARGS[2], "mpi") : ("CPU", "mpi")) | ||
const backend_type = input_args[2] == "nccl" ? NCCLBackend : MPIBackend | ||
const dev = input_args[1] == "CPU" ? LuxCPUDevice() : | ||
(input_args[1] == "CUDA" ? LuxCUDADevice() : LuxAMDGPUDevice()) | ||
|
||
rng = Xoshiro(1234) | ||
|
||
DistributedUtils.initialize(backend_type) | ||
backend = DistributedUtils.get_distributed_backend(backend_type) | ||
|
||
data = randn(rng, Float32, 10) | ||
dcontainer = DistributedUtils.DistributedDataContainer(backend, data) | ||
|
||
rank = DistributedUtils.local_rank(backend) | ||
tworkers = DistributedUtils.total_workers(backend) | ||
|
||
if rank != tworkers - 1 | ||
@test length(dcontainer) == ceil(length(data) / tworkers) | ||
else | ||
@test length(dcontainer) == | ||
length(data) - (tworkers - 1) * ceil(length(data) / tworkers) | ||
end | ||
|
||
dsum = sum(Base.Fix1(MLUtils.getobs, dcontainer), 1:MLUtils.numobs(dcontainer)) | ||
@test DistributedUtils.allreduce!(backend, [dsum], +)[1] ≈ sum(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Lux, MPI, NCCL, Optimisers, Random, Test | ||
|
||
const input_args = length(ARGS) == 3 ? ARGS[1:3] : | ||
(length(ARGS) == 2 ? (ARGS[2], "mpi") : ("CPU", "mpi")) | ||
const backend_type = input_args[2] == "nccl" ? NCCLBackend : MPIBackend | ||
const dev = input_args[1] == "CPU" ? LuxCPUDevice() : | ||
(input_args[1] == "CUDA" ? LuxCUDADevice() : LuxAMDGPUDevice()) | ||
|
||
DistributedUtils.initialize(backend_type) | ||
backend = DistributedUtils.get_distributed_backend(backend_type) | ||
|
||
opt = Adam(0.001f0) | ||
ps = (a=zeros(4), b=zeros(4)) |> dev | ||
st_opt = Optimisers.setup(opt, ps) | ||
|
||
dopt = DistributedUtils.DistributedOptimizer(backend, opt) | ||
st_dopt = Optimisers.setup(dopt, ps) | ||
|
||
@test st_dopt.a.state == st_opt.a.state | ||
@test st_dopt.b.state == st_opt.b.state | ||
|
||
@test_nowarn DistributedUtils.synchronize!!(backend, st_dopt) | ||
|
||
gs = (a=ones(4), b=ones(4)) |> dev | ||
|
||
_, ps_dopt = Optimisers.update(st_dopt, ps, gs) | ||
_, ps_opt = Optimisers.update(st_opt, ps, gs) | ||
|
||
@test ps_dopt.a≈ps_opt.a atol=1.0e-5 rtol=1.0e-5 | ||
@test ps_dopt.b≈ps_opt.b atol=1.0e-5 rtol=1.0e-5 |
Oops, something went wrong.