Repository navigation
Expand file tree
/
Copy path11_gpu_cuda.rs
More file actions
130 lines (117 loc) · 4.26 KB
/
Copy path11_gpu_cuda.rs
File metadata and controls
130 lines (117 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Chapter 11 — the same network on the CPU and on the GPU, timed.
//!
//! cargo run --release --features cuda --bin 11_gpu_cuda
//!
//! Without `--features cuda` this still builds and runs; it reports that the
//! GPU section was skipped rather than failing to compile.
use rusting_brain::{
Activation, Dataset, Network, NetworkError, Optimizer, TrainConfig, TrainingBackend,
TrainingSession, accelerator_doctor, estimate_tensor_memory_mib,
};
use std::time::Instant;
const BUDGET_MIB: usize = 8192;
const BATCH_SIZE: usize = 256;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let data = synthetic_dataset(8192, 64);
let base = Network::builder()
.input_size(64)
.dense(256, Activation::Relu)
.dense(256, Activation::Relu)
.dense(1, Activation::Linear)
.optimizer(Optimizer::adam(0.001))
.seed(42)
.build();
let config = TrainConfig {
epochs: 20,
batch_size: BATCH_SIZE,
shuffle: true,
seed: Some(7),
};
println!(
"tensors for batch {BATCH_SIZE}: {} MiB",
estimate_tensor_memory_mib(&base, BATCH_SIZE)?
);
let backend = TrainingBackend::Cuda {
device: 0,
memory_budget_mib: BUDGET_MIB,
};
// 11.3 — ask the device before committing a run to it.
match accelerator_doctor(backend) {
Ok(Some(report)) => {
println!(
"{} ({}), {} MiB total, {:?} MiB free",
report.name, report.revision, report.total_memory_mib, report.free_memory_mib
);
println!(
"cuBLAS={}, kernels={}, allocation={}",
report.gemm_available, report.kernel_available, report.allocation_test
);
}
Ok(None) => unreachable!("Cuda backend never reports None"),
Err(NetworkError::CudaFeatureDisabled) => {
println!("built without `--features cuda`; running the CPU half only");
let mut cpu = base.clone();
let started = Instant::now();
cpu.fit(&data, config)?;
println!("cpu: {:?}", started.elapsed());
return Ok(());
}
Err(other) => return Err(other.into()),
}
// 11.7 — the only honest way to decide: time both, in release mode.
let mut cpu = base.clone();
let cpu_started = Instant::now();
cpu.fit(&data, config)?;
let cpu_time = cpu_started.elapsed();
let mut gpu = base.clone();
let gpu_started = Instant::now();
gpu.fit_with_backend(&data, config, backend)?;
let gpu_time = gpu_started.elapsed();
println!(
"cpu {cpu_time:?}, gpu {gpu_time:?}, speedup {:.2}x",
cpu_time.as_secs_f64() / gpu_time.as_secs_f64()
);
// 11.6 — one warm session, checkpointing only when the loss improves.
let one_epoch = TrainConfig { epochs: 1, ..config };
let mut model = base.clone();
let mut session = TrainingSession::new(&model, &data, one_epoch, backend)?
.expect("the Cuda backend always opens a session");
let mut best = f32::INFINITY;
for epoch in 0..20 {
let loss = session.train_epoch()?;
if loss < best {
best = loss;
model.restore_cuda_checkpoint(session.checkpoint()?)?;
model.save_json("best.json")?;
println!("epoch {epoch}: loss {loss:.6} (checkpointed)");
} else {
println!("epoch {epoch}: loss {loss:.6}");
}
}
let stats = session.stats();
println!(
"peak {} MiB, setup {:?}, training {:?}, checkpointing {:?}, dataset resident: {}",
stats.peak_allocated_bytes / (1024 * 1024),
stats.setup_time,
stats.training_time,
stats.checkpoint_time,
stats.dataset_resident
);
Ok(())
}
/// A learnable but non-trivial regression target, big enough that the GPU has
/// something to do.
fn synthetic_dataset(rows: usize, features: usize) -> Dataset {
let inputs: Vec<Vec<f32>> = (0..rows)
.map(|r| {
(0..features)
.map(|c| ((r * 31 + c * 17) % 101) as f32 / 101.0)
.collect()
})
.collect();
let targets = inputs
.iter()
.map(|row| vec![row.iter().take(8).sum::<f32>() / 8.0])
.collect();
Dataset::new(inputs, targets)
}