-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
123 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
const linear_algebra = @import("lib/linear_algebra.zig"); | ||
|
||
pub const add = linear_algebra.add; | ||
pub const dotProduct = linear_algebra.dotProduct; | ||
pub const matmul = linear_algebra.matmul; | ||
pub const matmul2 = linear_algebra.matmul2; | ||
pub const matmul3 = linear_algebra.matmul3; | ||
pub const add = @import("lib/add.zig").add; | ||
pub const dot = @import("lib/dot.zig").dot; | ||
pub const matmul = @import("lib/matmul.zig").matmul; | ||
pub const matmul2 = @import("lib/matmul.zig").matmul2; | ||
pub const matmul3 = @import("lib/matmul.zig").matmul3; | ||
pub const rmsnorm = @import("lib/rmsnorm.zig").rmsnorm; | ||
pub const rope = @import("lib/rope.zig").rope; | ||
pub const softmax = @import("lib/softmax.zig").softmax; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const std = @import("std"); | ||
|
||
pub fn add(a: []f32, b: []const f32) void { | ||
@setFloatMode(.Optimized); | ||
|
||
std.debug.assert(a.len == b.len); | ||
|
||
for (a, 0..) |*element, index| { | ||
element.* += b[index]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const std = @import("std"); | ||
|
||
const max_vector_len: comptime_int = 16; | ||
const min_vector_len: comptime_int = 4; | ||
|
||
pub fn dot(a: []const f32, b: []const f32) f32 { | ||
@setFloatMode(.Optimized); | ||
|
||
std.debug.assert(a.len == b.len); | ||
|
||
const rest_len = a.len % max_vector_len; | ||
|
||
std.debug.assert(rest_len % min_vector_len == 0); | ||
|
||
var max_len_accu: @Vector(max_vector_len, f32) = @splat(0.0); | ||
var index: usize = 0; | ||
|
||
while (index < a.len - rest_len) : (index += max_vector_len) { | ||
max_len_accu += | ||
@as(@Vector(max_vector_len, f32), a[index..][0..max_vector_len].*) * | ||
@as(@Vector(max_vector_len, f32), b[index..][0..max_vector_len].*); | ||
} | ||
|
||
var result = @reduce(.Add, max_len_accu); | ||
|
||
if (rest_len > 0) { | ||
var min_len_accu: @Vector(min_vector_len, f32) = @splat(0.0); | ||
|
||
index = a.len - rest_len; | ||
|
||
while (index < a.len) : (index += min_vector_len) { | ||
min_len_accu += | ||
@as(@Vector(min_vector_len, f32), a[index..][0..min_vector_len].*) * | ||
@as(@Vector(min_vector_len, f32), b[index..][0..min_vector_len].*); | ||
} | ||
|
||
result += @reduce(.Add, min_len_accu); | ||
} | ||
|
||
return result; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const std = @import("std"); | ||
|
||
const dot = @import("dot.zig").dot; | ||
|
||
pub fn matmul(result: []f32, a: []const f32, b: []const f32) void { | ||
std.debug.assert(b.len == result.len * a.len); | ||
|
||
for (result, 0..) |*entry, i| { | ||
entry.* = dot(a, b[(i * a.len)..][0..a.len]); | ||
} | ||
} | ||
|
||
pub fn matmul2(args_1: anytype, args_2: anytype, multi_threaded: bool) !void { | ||
const cpu_count = std.Thread.getCpuCount() catch 1; | ||
|
||
if (multi_threaded and cpu_count > 2) { | ||
const thread_1 = try std.Thread.spawn(.{}, matmul, args_1); | ||
const thread_2 = try std.Thread.spawn(.{}, matmul, args_2); | ||
|
||
thread_1.join(); | ||
thread_2.join(); | ||
} else { | ||
@call(.auto, matmul, args_1); | ||
@call(.auto, matmul, args_2); | ||
} | ||
} | ||
|
||
pub fn matmul3(args_1: anytype, args_2: anytype, args_3: anytype, multi_threaded: bool) !void { | ||
const cpu_count = std.Thread.getCpuCount() catch 1; | ||
|
||
if (multi_threaded and cpu_count > 3) { | ||
const thread_1 = try std.Thread.spawn(.{}, matmul, args_1); | ||
const thread_2 = try std.Thread.spawn(.{}, matmul, args_2); | ||
const thread_3 = try std.Thread.spawn(.{}, matmul, args_3); | ||
|
||
thread_1.join(); | ||
thread_2.join(); | ||
thread_3.join(); | ||
} else { | ||
@call(.auto, matmul, args_1); | ||
@call(.auto, matmul, args_2); | ||
@call(.auto, matmul, args_3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const std = @import("std"); | ||
|
||
pub fn softmax(vector: []f32) void { | ||
@setFloatMode(.Optimized); | ||
|
||
var max: f32 = std.mem.max(f32, vector); | ||
var sum: f32 = 0; | ||
|
||
for (vector) |*element| { | ||
element.* = std.math.exp(element.* - max); | ||
sum += element.*; | ||
} | ||
|
||
for (vector) |*element| { | ||
element.* /= sum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters