Skip to content

Commit 96bd7f0

Browse files
Initial GPUArray transition (#1020)
* Drop LocalPreferences.toml from .gitignore * Transition Project.toml to GPUArrays * Support Metal in RLCore tests * Log GPU_Backend * Make tests work w/o CUDA * Fix CUDA import * Drop CUDA requirement * Make tests GPU aware * Drop CUDA from tests (keep in dependencies for now) * Drop CUDA from test dependencies * Add info statement Flux.GPU * Check if Apple silicon * Apple silicon check * Fix duplicate compat entry * Fix CUDA compat (allow v5) * Fix RLZoo compat * Add missing test import * Use julia-actions cache instead of generic Github one
1 parent 54ace04 commit 96bd7f0

File tree

13 files changed

+81
-72
lines changed

13 files changed

+81
-72
lines changed

.github/workflows/ci.yml

+5-51
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,7 @@ jobs:
3838
with:
3939
version: ${{ matrix.version }}
4040
arch: ${{ matrix.arch }}
41-
- uses: actions/cache@v4
42-
env:
43-
cache-name: cache-artifacts
44-
with:
45-
path: ~/.julia/artifacts
46-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
47-
restore-keys: |
48-
${{ runner.os }}-test-${{ env.cache-name }}-
49-
${{ runner.os }}-test-
50-
${{ runner.os }}-
41+
- uses: julia-actions/cache@v1
5142
- name: Get changed files
5243
id: RLBase-changed
5344
uses: tj-actions/changed-files@v42
@@ -86,16 +77,7 @@ jobs:
8677
with:
8778
version: ${{ matrix.version }}
8879
arch: ${{ matrix.arch }}
89-
- uses: actions/cache@v4
90-
env:
91-
cache-name: cache-artifacts
92-
with:
93-
path: ~/.julia/artifacts
94-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
95-
restore-keys: |
96-
${{ runner.os }}-test-${{ env.cache-name }}-
97-
${{ runner.os }}-test-
98-
${{ runner.os }}-
80+
- uses: julia-actions/cache@v1
9981
- name: Get changed files
10082
id: RLCore-changed
10183
uses: tj-actions/changed-files@v42
@@ -137,16 +119,7 @@ jobs:
137119
with:
138120
version: ${{ matrix.version }}
139121
arch: ${{ matrix.arch }}
140-
- uses: actions/cache@v4
141-
env:
142-
cache-name: cache-artifacts
143-
with:
144-
path: ~/.julia/artifacts
145-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
146-
restore-keys: |
147-
${{ runner.os }}-test-${{ env.cache-name }}-
148-
${{ runner.os }}-test-
149-
${{ runner.os }}-
122+
- uses: julia-actions/cache@v1
150123
- name: Get changed files
151124
id: RLZoo-changed
152125
uses: tj-actions/changed-files@v42
@@ -190,16 +163,7 @@ jobs:
190163
with:
191164
version: ${{ matrix.version }}
192165
arch: ${{ matrix.arch }}
193-
- uses: actions/cache@v4
194-
env:
195-
cache-name: cache-artifacts
196-
with:
197-
path: ~/.julia/artifacts
198-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
199-
restore-keys: |
200-
${{ runner.os }}-test-${{ env.cache-name }}-
201-
${{ runner.os }}-test-
202-
${{ runner.os }}-
166+
- uses: julia-actions/cache@v1
203167
- name: Get changed files
204168
id: RLEnvironments-changed
205169
uses: tj-actions/changed-files@v42
@@ -245,17 +209,7 @@ jobs:
245209
with:
246210
version: ${{ matrix.version }}
247211
arch: ${{ matrix.arch }}
248-
- uses: actions/cache@v4
249-
env:
250-
cache-name: cache-artifacts
251-
with:
252-
path: ~/.julia/artifacts
253-
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
254-
restore-keys: |
255-
${{ runner.os }}-test-${{ env.cache-name }}-
256-
${{ runner.os }}-test-
257-
${{ runner.os }}-
258-
212+
- uses: julia-actions/cache@v1
259213
- name: Get changed files
260214
id: RLExperiments-changed
261215
uses: tj-actions/changed-files@v42

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ docs/experiments
3333
# environment.
3434
Manifest.toml
3535
**/Manifest.toml
36+
LocalPreferences.toml
3637

3738
.vscode/*
3839

@@ -59,3 +60,5 @@ Sessionx.vim
5960
tags
6061
# Persistent undo
6162
[._]*.un~
63+
64+

src/ReinforcementLearningCore/Project.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ FillArrays = "0.8, 0.9, 0.10, 0.11, 0.12, 0.13, 1"
3939
Flux = "0.13, 0.14"
4040
Functors = "0.1, 0.2, 0.3, 0.4"
4141
GPUArrays = "8, 9, 10"
42+
Metal = "1.0"
4243
Parsers = "2"
4344
ProgressMeter = "1"
4445
Reexport = "1"
@@ -54,8 +55,11 @@ julia = "1.9"
5455
[extras]
5556
CommonRLInterface = "d842c3ba-07a1-494f-bbec-f5741b0a3e98"
5657
DomainSets = "5b8099bc-c8ec-5219-889f-1d9e522a28bf"
58+
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
59+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
5760
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
5861
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
62+
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
5963

6064
[targets]
61-
test = ["CommonRLInterface", "DomainSets", "Test", "Random"]
65+
test = ["CommonRLInterface", "DomainSets", "Metal", "Preferences", "Test", "UUIDs"]

src/ReinforcementLearningCore/test/runtests.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
1+
using Test
2+
using UUIDs
3+
using Preferences
4+
5+
if Sys.isapple() && Sys.ARCH === :aarch64
6+
flux_uuid = UUID("587475ba-b771-5e3f-ad9e-33799f191a9c")
7+
set_preferences!(flux_uuid, "gpu_backend" => "Metal")
8+
9+
using Metal
10+
else
11+
using CUDA, cuDNN
12+
end
13+
114
using ReinforcementLearningBase
215
using ReinforcementLearningCore
316
using ReinforcementLearningTrajectories
417

5-
using Test
6-
using CUDA
718
using CircularArrayBuffers
819
using Flux
920

21+
@info "Flux.GPU_BACKEND = $(Flux.GPU_BACKEND)"
22+
1023
include("environments/randomwalk1D.jl")
1124
include("environments/tictactoe.jl")
1225
include("environments/rockpaperscissors.jl")

src/ReinforcementLearningCore/test/utils/device.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@test device(Conv((2, 2), 1 => 16, relu)) == Val(:cpu)
66
@test device(Chain(x -> x .^ 2, Dense(2, 3))) == Val(:cpu)
77

8-
if CUDA.functional()
8+
if (@isdefined CUDA) && CUDA.functional()
99
@test device(rand(2) |> gpu) isa CuDevice
1010
@test device(Dense(2, 3) |> gpu) isa CuDevice
1111
@test device(Conv((2, 2), 1 => 16, relu) |> gpu) isa CuDevice

src/ReinforcementLearningCore/test/utils/distributions.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Test, LinearAlgebra, Distributions, CUDA, Flux
1+
using Test, LinearAlgebra, Distributions, Flux
22

33
@testset "utils/distributions" begin
44
@testset "logdetLorU" begin
@@ -8,7 +8,7 @@ using Test, LinearAlgebra, Distributions, CUDA, Flux
88
logdetM = logdet(M)
99
@test logdetM == ReinforcementLearningCore.logdetLorU(L)
1010
@test logdetM == ReinforcementLearningCore.logdetLorU(U)
11-
if CUDA.functional()
11+
if (@isdefined CUDA) && CUDA.functional()
1212
L_d = cu(L)
1313
U_d = cu(U)
1414
@test logdetM == ReinforcementLearningCore.logdetLorU(L_d)
@@ -126,7 +126,7 @@ using Test, LinearAlgebra, Distributions, CUDA, Flux
126126
end
127127
end
128128
@testset "CUDA" begin
129-
if CUDA.functional()
129+
if (@isdefined CUDA) && CUDA.functional()
130130
CUDA.allowscalar(false)
131131
#These only test GPU compatibility, exactness of results is tested above on the CPU
132132
@testset "Diagonal Gaussian" begin

src/ReinforcementLearningCore/test/utils/networks.jl

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using Test, Flux, CUDA, ChainRulesCore, LinearAlgebra, Distributions
2-
using Flux: params, gradient, unsqueeze
1+
using Test, Flux, ChainRulesCore, LinearAlgebra, Distributions, ReinforcementLearningCore
2+
using Flux: params, gradient, unsqueeze, InvDecay, gpu, cpu
3+
import ReinforcementLearningBase: RLBase
4+
35
@testset "Approximators" begin
46
#= These may need to be updated due to recent changes
57
@testset "TabularApproximator" begin
@@ -110,7 +112,7 @@ using Flux: params, gradient, unsqueeze
110112
end
111113
end
112114
@testset "CUDA" begin
113-
if CUDA.functional()
115+
if (@isdefined CUDA) && CUDA.functional()
114116
CUDA.allowscalar(false)
115117
gn = GaussianNetwork(Dense(20,15), Dense(15,10), Dense(15,10, softplus)) |> gpu
116118
state = rand(20,3) |> gpu #batch of 3 states
@@ -262,7 +264,7 @@ using Flux: params, gradient, unsqueeze
262264
end
263265
end
264266
@testset "CUDA" begin
265-
if CUDA.functional()
267+
if (@isdefined CUDA) && CUDA.functional()
266268
CUDA.allowscalar(false)
267269
rng = CUDA.CURAND.RNG()
268270
pre = Dense(20,15) |> gpu

src/ReinforcementLearningExperiments/Project.toml

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ Weave = "44d3d7a6-8a23-5bf8-98c5-b353f8df5ec9"
1919
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
2020

2121
[compat]
22-
CUDA = "5"
22+
CUDA = "4, 5"
2323
Distributions = "0.25"
2424
Flux = "0.13, 0.14"
2525
IntervalSets = "0.7"
26+
Metal = "1.0"
2627
Reexport = "1"
2728
ReinforcementLearningBase = "0.12"
2829
ReinforcementLearningCore = "0.13.1"
@@ -35,7 +36,11 @@ julia = "1.9"
3536

3637
[extras]
3738
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
39+
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
40+
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
41+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
3842
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
43+
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
3944

4045
[targets]
41-
test = ["CUDA", "Test"]
46+
test = ["Metal", "Preferences", "Test", "UUIDs"]

src/ReinforcementLearningExperiments/deps/experiments/experiments/CFR/JuliaRL_DeepCFR_OpenSpiel.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function RLCore.Experiment(
5959
n_training_steps_Π = 2000,
6060
batchsize_V = 2048,
6161
batchsize_Π = 2048,
62-
initializer = glorot_normal(CUDA.CURAND.default_rng()),
62+
initializer = glorot_normal((@isdefined CUDA) && CUDA.functional() ? CUDA.CURAND.RNG() : rng),
6363
)
6464
Experiment(p, env, StopAfterStep(500, is_show_progress=!haskey(ENV, "CI")), EmptyHook(), "# run DeepcCFR on leduc_poker")
6565
end

src/ReinforcementLearningExperiments/deps/experiments/experiments/DQN/Dopamine_IQN_Atari.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function RLCore.Experiment(
182182
)
183183
rng = Random.default_rng()
184184
Random.seed!(rng, seed)
185-
device_rng = CUDA.functional() ? CUDA.CURAND.RNG() : rng
185+
device_rng = ((@isdefined CUDA) && CUDA.functional()) ? CUDA.CURAND.RNG() : rng
186186
Random.seed!(device_rng, isnothing(seed) ? nothing : hash(seed + 1))
187187

188188
if isnothing(save_dir)

src/ReinforcementLearningExperiments/test/runtests.jl

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
using ReinforcementLearningExperiments
2-
using CUDA
1+
using UUIDs
2+
using Preferences
3+
4+
if Sys.isapple() && Sys.ARCH === :aarch64
5+
flux_uuid = UUID("587475ba-b771-5e3f-ad9e-33799f191a9c")
6+
set_preferences!(flux_uuid, "gpu_backend" => "Metal")
7+
8+
using Metal
9+
else
10+
using CUDA, cuDNN
11+
CUDA.allowscalar(false)
12+
end
313

4-
CUDA.allowscalar(false)
14+
@info "Flux.GPU_BACKEND = $(Flux.GPU_BACKEND)"
15+
16+
using ReinforcementLearningExperiments
517

618
run(E`JuliaRL_BasicDQN_CartPole`)
719
run(E`JuliaRL_DQN_CartPole`)

src/ReinforcementLearningZoo/Project.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ ReinforcementLearningBase = "e575027e-6cd6-5018-9292-cdc6200d2b44"
1818
ReinforcementLearningCore = "de1b191a-4ae0-4afa-a27b-92d07f46b2d6"
1919
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
2020
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
21-
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
2221

2322
[compat]
2423
CUDA = "4, 5"
@@ -27,6 +26,7 @@ Distributions = "0.25"
2726
Flux = "0.13, 0.14"
2827
Functors = "0.2, 0.3, 0.4"
2928
LogExpFunctions = "0.3"
29+
3030
MLUtils = "0.4"
3131
NNlib = "0.8, 0.9"
3232
Optim = "1"
@@ -38,7 +38,11 @@ cuDNN = "1"
3838
julia = "1.9"
3939

4040
[extras]
41+
Metal = "dde4c033-4e86-420c-a63e-0dd931031962"
42+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
4143
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
44+
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
45+
cuDNN = "02a925ec-e4fe-4b08-9a7e-0d78e3d38ccd"
4246

4347
[targets]
44-
test = ["Test"]
48+
test = ["Metal", "Preferences", "Test", "UUIDs", "cuDNN"]

src/ReinforcementLearningZoo/test/runtests.jl

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
using Test
1+
using UUIDs
2+
using Preferences
3+
4+
if Sys.isapple()
5+
flux_uuid = UUID("587475ba-b771-5e3f-ad9e-33799f191a9c")
6+
set_preferences!(flux_uuid, "gpu_backend" => "Metal")
27

8+
using Metal
9+
else
10+
using CUDA, cuDNN
11+
CUDA.allowscalar(false)
12+
end
13+
14+
using Test
315
@testset "ReinforcementLearningZoo.jl" begin
416
# include("cfr/cfr.jl")
517
# include("hooks.jl")

0 commit comments

Comments
 (0)