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
//! Execution context for managing computational graphs
/// Context for managing the computational graph
pub struct Context {
// For now, context is minimal. In future, it could track:
// - All tensors for memory management
// - Training vs inference mode
// - Random state
training: bool,
}
impl Context {
/// Create a new context
pub fn new() -> Self {
Self { training: true }
}
/// Set training mode
pub fn train(&mut self) {
self.training = true;
}
/// Set evaluation mode
pub fn eval(&mut self) {
self.training = false;
}
/// Check if in training mode
pub fn is_training(&self) -> bool {
self.training
}
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_new() {
let ctx = Context::new();
assert!(ctx.is_training());
}
#[test]
fn test_context_default() {
let ctx = Context::default();
assert!(ctx.is_training());
}
#[test]
fn test_context_train_mode() {
let mut ctx = Context::new();
ctx.eval();
assert!(!ctx.is_training());
ctx.train();
assert!(ctx.is_training());
}
#[test]
fn test_context_eval_mode() {
let mut ctx = Context::new();
ctx.eval();
assert!(!ctx.is_training());
}
}