oarust 0.19.0

GPU-first semantic computing runtime written in Rust
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Training hooks and built-in callbacks.

pub use crate::ml::types::ValidationResult;
use std::{
	io::{self, Write},
	time::{Duration, Instant},
};

use crate::{Error, Result};

use super::{TrainingCallbackContext, TrainingConfig, TrainingControl, TrainingSnapshot};

mod checkpoint;
mod csv;
mod phase;
mod policy;
mod validation;

pub use checkpoint::CheckpointCallback;
pub use csv::CsvLoggerCallback;
pub use phase::{PhaseCallback, TrainingPhase};
pub use policy::{EarlyStopMode, EarlyStoppingCallback, LearningRateCallback};
pub use validation::{ValidationCallback, ValidationMetric};

/// Stateful policy invoked in deterministic registration order.
pub trait TrainingCallback {
	fn on_train_begin(
		&mut self,
		_context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		Ok(TrainingControl::Continue)
	}

	fn on_epoch_begin(
		&mut self,
		_context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		Ok(TrainingControl::Continue)
	}

	fn on_step_end(&mut self, _context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		Ok(TrainingControl::Continue)
	}

	fn on_epoch_end(
		&mut self,
		_context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		Ok(TrainingControl::Continue)
	}

	fn on_train_end(
		&mut self,
		_context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		Ok(TrainingControl::Continue)
	}
}

/// Throttled Keras/tqdm-style wall-throughput progress display.
pub struct ProgressBarCallback {
	width: usize,
	show_epoch_header: bool,
	last_print: Option<Instant>,
	last_finalized_step: u64,
}

impl ProgressBarCallback {
	/// Construct a progress bar with the requested cell width.
	pub fn new(width: usize) -> Self {
		Self {
			width: width.max(1),
			show_epoch_header: true,
			last_print: None,
			last_finalized_step: 0,
		}
	}

	/// Enable or suppress the `epoch N/M` header.
	pub fn set_show_epoch_header(&mut self, show: bool) {
		self.show_epoch_header = show;
	}

	/// Format one deterministic progress line without writing it.
	pub fn render_line(&self, state: TrainingSnapshot, config: &TrainingConfig) -> String {
		let has_epochs = state.total_epochs() > 0;
		let step = if has_epochs {
			state.step_in_epoch()
		} else {
			state.step_count()
		};
		let total = if has_epochs {
			state.steps_in_epoch()
		} else {
			state.total_steps()
		};
		let elapsed = if has_epochs {
			state.epoch_elapsed()
		} else {
			state.elapsed()
		};
		let mut line = String::from("\r  ");
		if total > 0 {
			let eighths = ((step as u128 * self.width as u128 * 8 + u128::from(total / 2))
				/ u128::from(total)) as usize;
			let full = (eighths / 8).min(self.width);
			let partial = eighths % 8;
			line.push_str(&format!("{step}/{total} |"));
			line.push_str(&"█".repeat(full));
			if full < self.width && partial > 0 {
				line.push_str(["", "▏", "▎", "▍", "▌", "▋", "▊", "▉"][partial]);
				line.push_str(&"░".repeat(self.width - full - 1));
			} else {
				line.push_str(&"░".repeat(self.width - full));
			}
			line.push_str("| ");
		} else {
			line.push_str(&format!("step {step} "));
		}
		let seconds = elapsed.as_secs_f64();
		let milliseconds = if step > 0 {
			seconds * 1000.0 / step as f64
		} else {
			0.0
		};
		let samples_per_second = if seconds > 0.0 {
			step as f64 * config.batch_size as f64 / seconds
		} else {
			0.0
		};
		line.push_str(&format!(
			"{seconds:.2}s · {milliseconds:.2} ms/step · {} sample/s",
			format_rate(samples_per_second)
		));
		if config.sequence_length > 0 {
			line.push_str(&format!(
				" · {} {}/s",
				format_rate(samples_per_second * config.sequence_length as f64),
				config.sequence_unit
			));
		}
		if state.total_source_units() > 0 {
			let rate = if has_epochs {
				state.epoch_source_units_per_second()
			} else {
				state.wall_source_units_per_second()
			};
			line.push_str(&format!(
				" · {} {}/s",
				format_rate(rate),
				config.source_unit
			));
		}
		if let Some(loss) = state.last_loss() {
			line.push_str(&format!(" · loss: {loss:.4}"));
		}
		line.push_str("        ");
		line
	}

	fn render(&self, context: &TrainingCallbackContext<'_>, newline: bool) -> Result<()> {
		let mut output = self.render_line(context.snapshot(), context.config());
		if newline {
			output.push('\n');
		}
		write_stdout(&output)
	}
}

impl Default for ProgressBarCallback {
	fn default() -> Self {
		Self::new(10)
	}
}

impl TrainingCallback for ProgressBarCallback {
	fn on_train_begin(
		&mut self,
		_context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		self.last_print = Some(Instant::now());
		Ok(TrainingControl::Continue)
	}

	fn on_epoch_begin(
		&mut self,
		context: &mut TrainingCallbackContext<'_>,
	) -> Result<TrainingControl> {
		let state = context.snapshot();
		if self.show_epoch_header && state.total_epochs() > 0 {
			write_stdout(&format!(
				"epoch {}/{}\n",
				state.epoch(),
				state.total_epochs()
			))?;
		}
		Ok(TrainingControl::Continue)
	}

	fn on_step_end(&mut self, context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		let state = context.snapshot();
		if state.is_epoch_boundary() {
			return Ok(TrainingControl::Continue);
		}
		let now = Instant::now();
		let boundary = state.is_last_step();
		if !boundary
			&& self
				.last_print
				.is_some_and(|last| now.duration_since(last) < Duration::from_millis(33))
		{
			return Ok(TrainingControl::Continue);
		}
		self.last_print = Some(now);
		self.render(context, boundary)?;
		if boundary {
			self.last_finalized_step = state.step_count();
		}
		Ok(TrainingControl::Continue)
	}

	fn on_epoch_end(&mut self, context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		self.render(context, true)?;
		self.last_finalized_step = context.snapshot().step_count();
		Ok(TrainingControl::Continue)
	}

	fn on_train_end(&mut self, context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		let state = context.snapshot();
		if self.last_finalized_step != state.step_count() {
			self.render(context, true)?;
		}
		Ok(TrainingControl::Continue)
	}
}

/// Final loss, wall-time, device-time, throughput, and run summary.
pub struct SummaryCallback {
	track_initial_loss: bool,
	initial_loss: Option<f32>,
	validation_metric: Option<ValidationMetric>,
}

impl SummaryCallback {
	/// Construct a summary that includes the first completed loss.
	#[inline]
	#[must_use]
	pub fn new() -> Self {
		Self::default()
	}

	/// Construct a summary callback with explicit first-loss tracking.
	pub const fn new_with_initial_loss(track_initial_loss: bool) -> Self {
		Self {
			track_initial_loss,
			initial_loss: None,
			validation_metric: None,
		}
	}

	/// Display the latest completed validation metric in the final report.
	pub fn set_validation_metric(&mut self, metric: ValidationMetric) {
		self.validation_metric = Some(metric);
	}

	/// Format the complete summary without writing it.
	pub fn render_report(&self, state: TrainingSnapshot, config: &TrainingConfig) -> String {
		let mut report = String::from("\nSummary:\n");
		match (self.initial_loss, state.last_loss()) {
			(_, None) => report.push_str("  loss: n/a (no loss recorded)\n"),
			(Some(initial), Some(final_loss)) if self.track_initial_loss => report.push_str(&format!(
				"  loss: initial {initial:.6} · final {final_loss:.6} · mean {:.6}\n",
				state.training_mean_loss()
			)),
			(_, Some(final_loss)) => report.push_str(&format!(
				"  loss: final {final_loss:.6} · mean {:.6}\n",
				state.training_mean_loss()
			)),
		}
		if let Some(metric) = &self.validation_metric
			&& let Some(value) = metric.value()
		{
			report.push_str(&format!("  Validation: {} {value:.6}\n", metric.name()));
		}
		report.push_str(&format!(
			"  Wall: {:.2} ms/step · {} sample/s",
			state.wall_ms_per_step(),
			format_rate(state.wall_samples_per_second())
		));
		if config.sequence_length > 0 {
			report.push_str(&format!(
				" · {} {}/s",
				format_rate(state.wall_units_per_second()),
				config.sequence_unit
			));
		}
		if state.total_source_units() > 0 {
			report.push_str(&format!(
				" · {} {}/s",
				format_rate(state.wall_source_units_per_second()),
				config.source_unit
			));
		}
		report.push('\n');

		let gpu = state.gpu_timing_stats();
		if gpu.count == 0 {
			report.push_str(&format!(
				"  GPU ({}): n/a (timer unavailable)\n",
				config.timer_name
			));
		} else {
			report.push_str(&format!(
				"  GPU ({}): mean {:.3} ms/step · p50 {:.3} · p95 {:.3} · min {:.3} · max {:.3} · {} sample/s",
				config.timer_name,
				gpu.mean_ms,
				gpu.median_ms,
				gpu.p95_ms,
				gpu.min_ms,
				gpu.max_ms,
				format_rate(state.gpu_samples_per_second())
			));
			if config.sequence_length > 0 {
				report.push_str(&format!(
					" · {} {}/s",
					format_rate(state.gpu_units_per_second()),
					config.sequence_unit
				));
			}
			if state.total_source_units() > 0 {
				report.push_str(&format!(
					" · {} {}/s",
					format_rate(state.gpu_source_units_per_second()),
					config.source_unit
				));
			}
			let gap = if state.gpu_samples_per_second() > 0.0 {
				(100.0 * (1.0 - state.wall_samples_per_second() / state.gpu_samples_per_second()))
					.clamp(0.0, 100.0)
			} else {
				0.0
			};
			report.push_str(&format!(" · wall-GPU gap {gap:.0}%\n"));
		}
		report.push_str(&format!(
			"  run: {:.2}s · {} steps · batch {}",
			state.elapsed().as_secs_f64(),
			state.step_count(),
			config.batch_size
		));
		if config.sequence_length > 0 {
			report.push_str(&format!(
				" · sequence {} {}/sample",
				config.sequence_length, config.sequence_unit
			));
		}
		report.push('\n');
		report
	}
}

impl Default for SummaryCallback {
	fn default() -> Self {
		Self::new_with_initial_loss(true)
	}
}

impl TrainingCallback for SummaryCallback {
	fn on_step_end(&mut self, context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		if self.track_initial_loss
			&& self.initial_loss.is_none()
			&& context.snapshot().step_count() == 1
		{
			self.initial_loss = context.snapshot().last_loss();
		}
		Ok(TrainingControl::Continue)
	}

	fn on_train_end(&mut self, context: &mut TrainingCallbackContext<'_>) -> Result<TrainingControl> {
		write_stdout(&self.render_report(context.snapshot(), context.config()))?;
		Ok(TrainingControl::Continue)
	}
}

fn format_rate(rate: f64) -> String {
	if rate >= 1.0e6 {
		format!("{:.2}M", rate / 1.0e6)
	} else if rate >= 1.0e3 {
		format!("{:.2}K", rate / 1.0e3)
	} else {
		format!("{rate:.0}")
	}
}

fn write_stdout(text: &str) -> Result<()> {
	let mut stdout = io::stdout().lock();
	stdout
		.write_all(text.as_bytes())
		.map_err(|error| Error::io("write training progress", error))?;
	stdout
		.flush()
		.map_err(|error| Error::io("flush training progress", error))
}