-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgradient_checkpointing.zig
More file actions
170 lines (139 loc) · 5.74 KB
/
Copy pathgradient_checkpointing.zig
File metadata and controls
170 lines (139 loc) · 5.74 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
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
const std = @import("std");
pub const CheckpointConfig = struct {
checkpoint_every_n: usize = 2,
max_checkpoints: usize = 64,
};
pub const CheckpointStore = struct {
allocator: std.mem.Allocator,
checkpoints: std.ArrayList([]f32),
layer_sizes: []const usize,
config: CheckpointConfig,
num_layers: usize,
pub fn init(
allocator: std.mem.Allocator,
layer_sizes: []const usize,
config: CheckpointConfig,
) !CheckpointStore {
var total_layers: usize = 0;
for (layer_sizes[1..]) |_| total_layers += 1;
return .{
.allocator = allocator,
.checkpoints = std.ArrayList([]f32).init(allocator),
.layer_sizes = layer_sizes,
.config = config,
.num_layers = total_layers,
};
}
pub fn deinit(self: *CheckpointStore) void {
for (self.checkpoints.items) |cp| {
self.allocator.free(cp);
}
self.checkpoints.deinit();
}
pub fn saveCheckpoint(self: *CheckpointStore, layer_idx: usize, activation: []const f32) !void {
if (layer_idx % self.config.checkpoint_every_n != 0) return;
if (self.checkpoints.items.len >= self.config.max_checkpoints) {
const oldest = self.checkpoints.orderedRemove(0);
self.allocator.free(oldest);
}
const copy = try self.allocator.dupe(f32, activation);
try self.checkpoints.append(copy);
}
pub fn recompute(self: *CheckpointStore, from_layer: usize, to_layer: usize, activations: [][]f32) void {
_ = from_layer;
_ = to_layer;
_ = activations;
}
pub fn memoryUsedMB(self: *const CheckpointStore) f64 {
var total: usize = 0;
for (self.checkpoints.items) |cp| {
total += cp.len * @sizeOf(f32);
}
return @as(f64, @floatFromInt(total)) / (1024.0 * 1024.0);
}
pub fn savedMemoryMB(self: *const CheckpointStore, full_activations_size: usize) f64 {
const full_mb: f64 = @floatFromInt(full_activations_size * @sizeOf(f32) * self.num_layers);
return full_mb - self.memoryUsedMB();
}
pub fn checkpointCount(self: *const CheckpointStore) usize {
return self.checkpoints.items.len;
}
};
pub const MemoryBudget = struct {
total_bytes: usize,
model_bytes: usize,
optimizer_bytes: usize,
gradient_bytes: usize,
activation_bytes: usize,
pub fn availableForCheckpoints(self: MemoryBudget) usize {
const used = self.model_bytes + self.optimizer_bytes + self.gradient_bytes + self.activation_bytes;
return if (self.total_bytes > used) self.total_bytes - used else 0;
}
pub fn recommendedCheckpointEvery(self: MemoryBudget, num_layers: usize, layer_activation_bytes: usize) usize {
const available = self.availableForCheckpoints();
const max_storable = available / @max(layer_activation_bytes, 1);
if (max_storable >= num_layers) return 1;
if (max_storable == 0) return num_layers;
return @max(num_layers / max_storable, 1);
}
};
test "checkpoint store saves and counts" {
const allocator = std.testing.allocator;
const sizes = [_]usize{ 4, 8, 4 };
var store = try CheckpointStore.init(allocator, &sizes, .{ .checkpoint_every_n = 1 });
defer store.deinit();
const act1 = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
try store.saveCheckpoint(0, &act1);
try std.testing.expectEqual(@as(usize, 1), store.checkpointCount());
const act2 = [_]f32{ 0.1, 0.2, 0.3, 0.4 };
try store.saveCheckpoint(1, &act2);
try std.testing.expectEqual(@as(usize, 2), store.checkpointCount());
}
test "checkpoint store skips non-checkpoint layers" {
const allocator = std.testing.allocator;
const sizes = [_]usize{ 4, 8, 4 };
var store = try CheckpointStore.init(allocator, &sizes, .{ .checkpoint_every_n = 2 });
defer store.deinit();
const act = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
try store.saveCheckpoint(0, &act);
try store.saveCheckpoint(1, &act);
try store.saveCheckpoint(2, &act);
try std.testing.expectEqual(@as(usize, 2), store.checkpointCount());
}
test "checkpoint store evicts oldest when full" {
const allocator = std.testing.allocator;
const sizes = [_]usize{ 4, 8, 4 };
var store = try CheckpointStore.init(allocator, &sizes, .{ .checkpoint_every_n = 1, .max_checkpoints = 2 });
defer store.deinit();
const act1 = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
const act2 = [_]f32{ 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 };
const act3 = [_]f32{ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 };
try store.saveCheckpoint(0, &act1);
try store.saveCheckpoint(1, &act2);
try store.saveCheckpoint(2, &act3);
try std.testing.expectEqual(@as(usize, 2), store.checkpointCount());
}
test "memory tracking" {
const allocator = std.testing.allocator;
const sizes = [_]usize{ 4, 8, 4 };
var store = try CheckpointStore.init(allocator, &sizes, .{ .checkpoint_every_n = 1 });
defer store.deinit();
const act = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
try store.saveCheckpoint(0, &act);
const mb = store.memoryUsedMB();
try std.testing.expect(mb > 0);
try std.testing.expect(mb < 1.0);
}
test "memory budget recommended checkpoint interval" {
const budget = MemoryBudget{
.total_bytes = 1024 * 1024 * 1024,
.model_bytes = 512 * 1024 * 1024,
.optimizer_bytes = 256 * 1024 * 1024,
.gradient_bytes = 128 * 1024 * 1024,
.activation_bytes = 64 * 1024 * 1024,
};
const available = budget.availableForCheckpoints();
try std.testing.expect(available > 0);
const every = budget.recommendedCheckpointEvery(12, 1024 * 1024);
try std.testing.expect(every >= 1);
}