-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_backend.zig
391 lines (298 loc) · 14.2 KB
/
cpu_backend.zig
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
const std = @import("std");
const Allocator = std.mem.Allocator;
const backend = @import("backend.zig");
const ComputeBackend = backend.ComputeBackend;
const BackendType = backend.BackendType;
const Matrix = backend.Matrix;
/// CPU-specific implementation of a matrix
const CPUMatrix = struct {
data: []f64,
allocator: Allocator,
};
/// CPU backend implementation
pub const CPUBackend = struct {
allocator: Allocator,
/// Initialize a new CPU backend
pub fn init(allocator: Allocator) !*CPUBackend {
const cpu_backend = try allocator.create(CPUBackend);
cpu_backend.* = CPUBackend{
.allocator = allocator,
};
return cpu_backend;
}
/// Free all resources used by the CPU backend
pub fn deinit(self: *CPUBackend) void {
self.allocator.destroy(self);
}
// ComputeBackend interface implementations
pub fn initMatrix(_: *anyopaque, allocator: Allocator, rows: usize, cols: usize) error{OutOfMemory}!*Matrix {
// Allocate the matrix wrapper
const matrix = try allocator.create(Matrix);
errdefer allocator.destroy(matrix);
// Allocate the CPU-specific data
const cpu_data = try allocator.create(CPUMatrix);
errdefer allocator.destroy(cpu_data);
// Allocate the data array
const data = try allocator.alloc(f64, rows * cols);
errdefer allocator.free(data);
// Initialize with zeros
@memset(data, 0);
// Set up the CPU matrix data
cpu_data.* = .{
.data = data,
.allocator = allocator,
};
// Set up the matrix
matrix.* = .{
.rows = rows,
.cols = cols,
.backend = undefined, // Will be set by the caller
.impl_data = cpu_data,
};
return matrix;
}
pub fn deinitMatrix(_: *anyopaque, matrix: *Matrix) void {
// First, cast to get the CPU-specific data
const cpu_data = @as(*CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
const allocator = cpu_data.allocator;
// Free the data array
allocator.free(cpu_data.data);
// Free the CPU matrix data
allocator.destroy(cpu_data);
// Free the matrix wrapper
allocator.destroy(matrix);
}
pub fn getMatrixElement(_: *anyopaque, matrix: *const Matrix, row: usize, col: usize) f64 {
if (row >= matrix.rows or col >= matrix.cols) {
@panic("Index out of bounds");
}
const cpu_data = @as(*const CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
return cpu_data.data[row * matrix.cols + col];
}
pub fn setMatrixElement(_: *anyopaque, matrix: *Matrix, row: usize, col: usize, value: f64) void {
if (row >= matrix.rows or col >= matrix.cols) {
@panic("Index out of bounds");
}
const cpu_data = @as(*CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
cpu_data.data[row * matrix.cols + col] = value;
}
pub fn fillMatrix(_: *anyopaque, matrix: *Matrix, value: f64) void {
const cpu_data = @as(*CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
for (cpu_data.data) |*element| {
element.* = value;
}
}
pub fn copyMatrix(ptr: *anyopaque, source: *const Matrix, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, source.rows, source.cols);
errdefer deinitMatrix(undefined, result);
const source_cpu_data = @as(*const CPUMatrix, @ptrCast(@alignCast(source.impl_data)));
const result_cpu_data = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
// Copy all data
@memcpy(result_cpu_data.data, source_cpu_data.data);
return result;
}
pub fn dotProduct(ptr: *anyopaque, a: *const Matrix, b: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (a.cols != b.rows) {
return error.DimensionMismatch;
}
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, a.rows, b.cols);
errdefer deinitMatrix(undefined, result);
const a_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(a.impl_data)));
const b_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(b.impl_data)));
for (0..a.rows) |i| {
for (0..b.cols) |j| {
var sum: f64 = 0;
for (0..a.cols) |k| {
sum += a_cpu.data[i * a.cols + k] * b_cpu.data[k * b.cols + j];
}
setMatrixElement(undefined, result, i, j, sum);
}
}
return result;
}
pub fn add(ptr: *anyopaque, a: *const Matrix, b: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (a.rows != b.rows or a.cols != b.cols) {
return error.DimensionMismatch;
}
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, a.rows, a.cols);
errdefer deinitMatrix(undefined, result);
const a_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(a.impl_data)));
const b_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(b.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..a.rows * a.cols) |i| {
result_cpu.data[i] = a_cpu.data[i] + b_cpu.data[i];
}
return result;
}
pub fn subtract(ptr: *anyopaque, a: *const Matrix, b: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (a.rows != b.rows or a.cols != b.cols) {
return error.DimensionMismatch;
}
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, a.rows, a.cols);
errdefer deinitMatrix(undefined, result);
const a_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(a.impl_data)));
const b_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(b.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..a.rows * a.cols) |i| {
result_cpu.data[i] = a_cpu.data[i] - b_cpu.data[i];
}
return result;
}
pub fn elementWiseMultiply(ptr: *anyopaque, a: *const Matrix, b: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (a.rows != b.rows or a.cols != b.cols) {
return error.DimensionMismatch;
}
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, a.rows, a.cols);
errdefer deinitMatrix(undefined, result);
const a_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(a.impl_data)));
const b_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(b.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..a.rows * a.cols) |i| {
result_cpu.data[i] = a_cpu.data[i] * b_cpu.data[i];
}
return result;
}
pub fn scale(ptr: *anyopaque, matrix: *const Matrix, scalar: f64, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, matrix.rows, matrix.cols);
errdefer deinitMatrix(undefined, result);
const matrix_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..matrix.rows * matrix.cols) |i| {
result_cpu.data[i] = matrix_cpu.data[i] * scalar;
}
return result;
}
pub fn sumRows(ptr: *anyopaque, matrix: *const Matrix, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, 1, matrix.cols);
errdefer deinitMatrix(undefined, result);
const matrix_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
for (0..matrix.cols) |j| {
var sum: f64 = 0;
for (0..matrix.rows) |i| {
sum += matrix_cpu.data[i * matrix.cols + j];
}
setMatrixElement(undefined, result, 0, j, sum);
}
return result;
}
pub fn transpose(ptr: *anyopaque, matrix: *const Matrix, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, matrix.cols, matrix.rows);
errdefer deinitMatrix(undefined, result);
for (0..matrix.rows) |i| {
for (0..matrix.cols) |j| {
const value = getMatrixElement(undefined, matrix, i, j);
setMatrixElement(undefined, result, j, i, value);
}
}
return result;
}
pub fn extractBatch(ptr: *anyopaque, matrix: *const Matrix, start: usize, end: usize, allocator: Allocator) error{ OutOfMemory, InvalidBatchIndices }!*Matrix {
if (start >= matrix.rows or end > matrix.rows or start >= end) {
return error.InvalidBatchIndices;
}
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const batch_size = end - start;
const result = try initMatrix(undefined, allocator, batch_size, matrix.cols);
errdefer deinitMatrix(undefined, result);
const matrix_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..batch_size) |i| {
const source_offset = (start + i) * matrix.cols;
const dest_offset = i * matrix.cols;
@memcpy(result_cpu.data[dest_offset..(dest_offset + matrix.cols)], matrix_cpu.data[source_offset..(source_offset + matrix.cols)]);
}
return result;
}
pub fn randomize(_: *anyopaque, matrix: *Matrix, min: f64, max: f64) void {
var prng = std.Random.DefaultPrng.init(@intCast(std.time.milliTimestamp()));
const rand = prng.random();
const matrix_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
for (0..matrix.rows * matrix.cols) |i| {
const rand_float = rand.float(f64);
matrix_cpu.data[i] = min + (rand_float * (max - min));
}
}
pub fn applyActivation(ptr: *anyopaque, matrix: *const Matrix, activation: fn (f64) f64, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, matrix.rows, matrix.cols);
errdefer deinitMatrix(undefined, result);
const matrix_cpu = @as(*const CPUMatrix, @ptrCast(@alignCast(matrix.impl_data)));
const result_cpu = @as(*CPUMatrix, @ptrCast(@alignCast(result.impl_data)));
for (0..matrix.rows * matrix.cols) |i| {
result_cpu.data[i] = activation(matrix_cpu.data[i]);
}
return result;
}
pub fn applySoftmax(ptr: *anyopaque, matrix: *const Matrix, allocator: Allocator) error{OutOfMemory}!*Matrix {
_ = @as(*CPUBackend, @ptrCast(@alignCast(ptr)));
const result = try initMatrix(undefined, allocator, matrix.rows, matrix.cols);
errdefer deinitMatrix(undefined, result);
// Process each row independently
for (0..matrix.rows) |i| {
// Find max value in row for numerical stability
var max_val: f64 = -std.math.inf(f64);
for (0..matrix.cols) |j| {
max_val = @max(max_val, getMatrixElement(undefined, matrix, i, j));
}
// Compute exp(x - max) and sum
var sum: f64 = 0.0;
for (0..matrix.cols) |j| {
const shifted = getMatrixElement(undefined, matrix, i, j) - max_val;
const exp_val = std.math.exp(shifted);
setMatrixElement(undefined, result, i, j, exp_val);
sum += exp_val;
}
// Normalize by sum
for (0..matrix.cols) |j| {
const val = getMatrixElement(undefined, result, i, j) / sum;
setMatrixElement(undefined, result, i, j, val);
}
}
return result;
}
pub fn applyGLU(ptr: *anyopaque, linear_part: *const Matrix, gating_part: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (linear_part.rows != gating_part.rows or linear_part.cols != gating_part.cols) {
return error.DimensionMismatch;
}
// Apply sigmoid to the gating part
const sigmoid_gate = try applyActivation(ptr, gating_part, sigmoid, allocator);
defer deinitMatrix(undefined, sigmoid_gate);
// Element-wise multiply with the linear part
return elementWiseMultiply(ptr, linear_part, sigmoid_gate, allocator);
}
pub fn applySwiGLU(ptr: *anyopaque, linear_part: *const Matrix, gating_part: *const Matrix, allocator: Allocator) error{ OutOfMemory, DimensionMismatch }!*Matrix {
if (linear_part.rows != gating_part.rows or linear_part.cols != gating_part.cols) {
return error.DimensionMismatch;
}
// Apply swish to the gating part
const swish_gate = try applyActivation(ptr, gating_part, swish, allocator);
defer deinitMatrix(undefined, swish_gate);
// Element-wise multiply with the linear part
return elementWiseMultiply(ptr, linear_part, swish_gate, allocator);
}
fn getBackendType(_: *anyopaque) BackendType {
return .CPU;
}
};
// Helper activation functions needed for GLU and SwiGLU
fn sigmoid(x: f64) f64 {
return 1.0 / (1.0 + std.math.exp(-x));
}
fn swish(x: f64) f64 {
const beta: f64 = 1.0;
return x * sigmoid(beta * x);
}
/// Creates a CPU backend instance, returning an opaque pointer
pub fn createCPUBackend(allocator: Allocator) !*anyopaque {
const cpu_backend = try CPUBackend.init(allocator);
errdefer cpu_backend.deinit(); // Ensure cleanup on populate error if any
return @ptrCast(cpu_backend);
}