Skip to content

Commit 44a914d

Browse files
authored
Merge pull request #309 from FilippoZanelli/feature
Replace std.debug.print with std.log.* and custom log scopes in tests
2 parents a2c0de5 + a871fef commit 44a914d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1185
-1137
lines changed

benchmarks/matmul/benchmark.zig

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const allocator = pkgAllocator.allocator;
66
const TensMath = zant.core.tensor.math_standard;
77
const std = @import("std");
88

9+
const benchmark_log = std.log.scoped(.benchmark);
10+
911
const macs_mat_mul = @import("macs_matmul.zig").lean_mat_mul;
1012
const simple_mat_mul = @import("simple_matmul.zig").lean_mat_mul;
1113
const blocked_matmul = @import("blocked_matmul.zig").lean_mat_mul;
@@ -40,20 +42,20 @@ const test_data_types = [_]type{
4042
const test_data_n = [_]usize{5};
4143

4244
fn print_mat(comptime T: anytype, mat: *const Tensor(T)) void {
43-
std.debug.print("Matrix shape: ", .{});
44-
for (mat.shape) |dim| std.debug.print("{} ", .{dim});
45-
std.debug.print("\n", .{});
45+
benchmark_log.debug("Matrix shape: ", .{});
46+
for (mat.shape) |dim| benchmark_log.debug("{} ", .{dim});
47+
benchmark_log.debug("\n", .{});
4648

4749
for (0..mat.shape[0]) |i| {
4850
for (0..mat.shape[1]) |j| {
49-
std.debug.print("{d} ", .{mat.data[i * mat.shape[1] + j]});
51+
benchmark_log.debug("{d} ", .{mat.data[i * mat.shape[1] + j]});
5052
}
51-
std.debug.print("\n", .{});
53+
benchmark_log.debug("\n", .{});
5254
}
5355
}
5456

5557
inline fn mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const Tensor(T), lean_mat_mul: mat_mul_fn) !Tensor(T) {
56-
// std.debug.print("\nStarting matrix multiplication validation...\n", .{});
58+
// benchmark_log.debug("\nStarting matrix multiplication validation...\n", .{});
5759

5860
const dim_num = A.shape.len;
5961

@@ -77,9 +79,9 @@ inline fn mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const Tensor(T),
7779
var Y = try Tensor(T).fromShape(&allocator, out_shape);
7880
errdefer Y.deinit();
7981

80-
// std.debug.print("Output tensor shape: ", .{});
81-
// for (Y.shape) |dim| std.debug.print("{} ", .{dim});
82-
// std.debug.print("\n", .{});
82+
// benchmark_log.debug("Output tensor shape: ", .{});
83+
// for (Y.shape) |dim| benchmark_log.debug("{} ", .{dim});
84+
// benchmark_log.debug("\n", .{});
8385

8486
@memset(Y.data, 0);
8587

@@ -216,22 +218,22 @@ fn mat_mul_bench(comptime T: anytype, comptime N: u8, comptime tests_num: usize)
216218
fn run_mat_mul_benchmarks(comptime T: anytype, comptime N: u8, comptime tests_num: usize) !void {
217219
const results = try mat_mul_bench(T, N, tests_num);
218220

219-
std.debug.print("MatMul benchmark results for type {any} with base {d}:\n\n", .{ T, N });
221+
benchmark_log.info("MatMul benchmark results for type {any} with base {d}:\n\n", .{ T, N });
220222

221223
for (0..results.len) |i| {
222224
const result = results[i];
223-
std.debug.print("Test #{d}\n", .{i});
224-
std.debug.print("\tShapes\n", .{});
225-
std.debug.print("\t\tA Shape: {any}\n \t\tB Shape: {any} \n", .{ result.a_shape, result.b_shape });
226-
std.debug.print("\tTimes:\n", .{});
225+
benchmark_log.debug("Test #{d}\n", .{i});
226+
benchmark_log.debug("\tShapes\n", .{});
227+
benchmark_log.debug("\t\tA Shape: {any}\n \t\tB Shape: {any} \n", .{ result.a_shape, result.b_shape });
228+
benchmark_log.debug("\tTimes:\n", .{});
227229
for (0..result.names.len) |mat_mul_i| {
228-
std.debug.print("\t\t{s} took {d} ms\n", .{ result.names[mat_mul_i], @as(f64, @floatFromInt(result.times[mat_mul_i])) / 1_000_000.0 });
230+
benchmark_log.debug("\t\t{s} took {d} ms\n", .{ result.names[mat_mul_i], @as(f64, @floatFromInt(result.times[mat_mul_i])) / 1_000_000.0 });
229231
}
230232

231-
std.debug.print("\tSpeedups:\n", .{});
233+
benchmark_log.debug("\tSpeedups:\n", .{});
232234
for (2..result.names.len) |mat_mul_i| {
233235
const current_speedup = @as(f64, @floatFromInt(result.times[1])) / @as(f64, @floatFromInt(result.times[mat_mul_i]));
234-
std.debug.print("\t\t{s}: {d:.2}x\n", .{ result.names[mat_mul_i], current_speedup });
236+
benchmark_log.debug("\t\t{s}: {d:.2}x\n", .{ result.names[mat_mul_i], current_speedup });
235237
}
236238
}
237239
}
@@ -243,7 +245,7 @@ pub fn run() !void {
243245
inline for (0..test_data_n.len) |j| {
244246
const N = test_data_n[j];
245247
try run_mat_mul_benchmarks(T, N, 5);
246-
std.debug.print("\n\n", .{});
248+
benchmark_log.debug("\n\n", .{});
247249
}
248250
}
249251
} else {

benchmarks/matmul/macs_matmul.zig

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ const TensMath = zant.core.tensor.math_standard;
66

77
const CACHE_BLOCK_SIZE_BYTES: usize = std.atomic.cache_line;
88

9-
10-
119
pub inline fn lean_mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const Tensor(T), Y: *Tensor(T)) !void {
1210
const DEFAULT_VECTOR_WIDTH: usize = comptime (std.simd.suggestVectorLength(T) orelse 4);
1311
const dim_num = A.shape.len;
@@ -16,16 +14,16 @@ pub inline fn lean_mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const T
1614
const N = B.shape[dim_num - 1];
1715
const K = A.shape[dim_num - 1];
1816

19-
// std.debug.print("\nMatrix multiplication dimensions: M={}, N={}, K={}\n", .{ M, N, K });
20-
// std.debug.print("Input tensor A shape: ", .{});
21-
// for (A.shape) |dim| std.debug.print("{} ", .{dim});
22-
// std.debug.print("\nInput tensor B shape: ", .{});
23-
// for (B.shape) |dim| std.debug.print("{} ", .{dim});
24-
// std.debug.print("\n", .{});
17+
// std.log.debug("\nMatrix multiplication dimensions: M={}, N={}, K={}\n", .{ M, N, K });
18+
// std.log.debug("Input tensor A shape: ", .{});
19+
// for (A.shape) |dim| std.log.debug("{} ", .{dim});
20+
// std.log.debug("\nInput tensor B shape: ", .{});
21+
// for (B.shape) |dim| std.log.debug("{} ", .{dim});
22+
// std.log.debug("\n", .{});
2523

26-
// std.debug.print("Output tensor Y shape: ", .{});
27-
// for (Y.shape) |dim| std.debug.print("{} ", .{dim});
28-
// std.debug.print("\n", .{});
24+
// std.log.debug("Output tensor Y shape: ", .{});
25+
// for (Y.shape) |dim| std.log.debug("{} ", .{dim});
26+
// std.log.debug("\n", .{});
2927

3028
// SIMD vector type
3129
const Vec = @Vector(DEFAULT_VECTOR_WIDTH, T);
@@ -39,7 +37,7 @@ pub inline fn lean_mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const T
3937
// Main matrix multiplication loop with SIMD
4038
var i: usize = 0;
4139
while (i < M) : (i += 1) {
42-
// if (i % 100 == 0) std.debug.print("Processing row {}/{}\n", .{ i, M });
40+
// if (i % 100 == 0) std.log.debug("Processing row {}/{}\n", .{ i, M });
4341
const row_offset = i * K;
4442
const out_offset = i * N;
4543

@@ -88,5 +86,5 @@ pub inline fn lean_mat_mul(comptime T: anytype, A: *const Tensor(T), B: *const T
8886
}
8987
}
9088

91-
// std.debug.print("Matrix multiplication completed\n", .{});
92-
}
89+
// std.log.debug("Matrix multiplication completed\n", .{});
90+
}

build.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn build(b: *std.Build) void {
1616
.arch_os_abi = target_str,
1717
.cpu_features = cpu_str,
1818
}) catch |err| {
19-
std.debug.print("Error parsing target: {}\n", .{err});
19+
std.log.scoped(.build).warn("Error parsing target: {}\n", .{err});
2020
return;
2121
};
2222

@@ -69,26 +69,26 @@ pub fn build(b: *std.Build) void {
6969
// Name and path of the model
7070
const model_name_option = b.option([]const u8, "model", "Model name") orelse "mnist-8";
7171
const model_path_option = b.option([]const u8, "model_path", "Model path") orelse std.fmt.allocPrint(b.allocator, "datasets/models/{s}/{s}.onnx", .{ model_name_option, model_name_option }) catch |err| {
72-
std.debug.print("Error allocating model path: {}\n", .{err});
72+
std.log.scoped(.build).warn("Error allocating model path: {}\n", .{err});
7373
return;
7474
};
7575

7676
// Generated path
7777
var generated_path_option = b.option([]const u8, "generated_path", "Generated path") orelse "";
7878
if (generated_path_option.len == 0) {
7979
generated_path_option = std.fmt.allocPrint(b.allocator, "generated/{s}/", .{model_name_option}) catch |err| {
80-
std.debug.print("Error allocating generated path: {}\n", .{err});
80+
std.log.scoped(.build).warn("Error allocating generated path: {}\n", .{err});
8181
return;
8282
};
8383
} else {
8484
if (!std.mem.endsWith(u8, generated_path_option, "/")) {
8585
generated_path_option = std.fmt.allocPrint(b.allocator, "{s}/", .{generated_path_option}) catch |err| {
86-
std.debug.print("Error normalizing path: {}\n", .{err});
86+
std.log.scoped(.build).warn("Error normalizing path: {}\n", .{err});
8787
return;
8888
};
8989
}
9090
generated_path_option = std.fmt.allocPrint(b.allocator, "{s}{s}/", .{ generated_path_option, model_name_option }) catch |err| {
91-
std.debug.print("Error allocating generated path: {}\n", .{err});
91+
std.log.scoped(.build).warn("Error allocating generated path: {}\n", .{err});
9292
return;
9393
};
9494
}
@@ -122,7 +122,7 @@ pub fn build(b: *std.Build) void {
122122
// ************************************************ STATIC LIBRARY CREATION ************************************************
123123

124124
const lib_model_path = std.fmt.allocPrint(b.allocator, "{s}lib_{s}.zig", .{ generated_path_option, model_name_option }) catch |err| {
125-
std.debug.print("Error allocating lib model path: {}\n", .{err});
125+
std.log.scoped(.build).warn("Error allocating lib model path: {}\n", .{err});
126126
return;
127127
};
128128

@@ -145,16 +145,16 @@ pub fn build(b: *std.Build) void {
145145
if (output_path_option.len != 0) {
146146
if (!std.mem.endsWith(u8, output_path_option, "/")) {
147147
output_path_option = std.fmt.allocPrint(b.allocator, "{s}/", .{output_path_option}) catch |err| {
148-
std.debug.print("Error normalizing path: {}\n", .{err});
148+
std.log.scoped(.build).warn("Error normalizing path: {}\n", .{err});
149149
return;
150150
};
151151
}
152152
const old_path = std.fmt.allocPrint(b.allocator, "zig-out/{s}/", .{model_name_option}) catch |err| {
153-
std.debug.print("Error allocating old path: {}\n", .{err});
153+
std.log.scoped(.build).warn("Error allocating old path: {}\n", .{err});
154154
return;
155155
};
156156
output_path_option = std.fmt.allocPrint(b.allocator, "{s}{s}/", .{ output_path_option, model_name_option }) catch |err| {
157-
std.debug.print("Error allocating output path: {}\n", .{err});
157+
std.log.scoped(.build).warn("Error allocating output path: {}\n", .{err});
158158
return;
159159
};
160160
const move_step = b.addSystemCommand(&[_][]const u8{
@@ -172,7 +172,7 @@ pub fn build(b: *std.Build) void {
172172

173173
// Add test for generated library
174174
const test_model_path = std.fmt.allocPrint(b.allocator, "{s}test_{s}.zig", .{ generated_path_option, model_name_option }) catch |err| {
175-
std.debug.print("Error allocating test model path: {}\n", .{err});
175+
std.log.scoped(.build).warn("Error allocating test model path: {}\n", .{err});
176176
return;
177177
};
178178

@@ -279,7 +279,7 @@ pub fn build(b: *std.Build) void {
279279

280280
// Path to the generated model options file (moved here)
281281
const model_options_path = std.fmt.allocPrint(b.allocator, "{s}model_options.zig", .{generated_path_option}) catch |err| {
282-
std.debug.print("Error allocating model options path: {}\n", .{err});
282+
std.log.scoped(.build).warn("Error allocating model options path: {}\n", .{err});
283283
return;
284284
};
285285

gui/sdl/sdl-standalone.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn runCodeGen() !void {
240240

241241
const exit_status = try child.wait();
242242

243-
std.debug.print("\nExit Status: {}\n", .{exit_status});
243+
std.log.info("\nExit Status: {}\n", .{exit_status});
244244
if (exit_status.Exited != 0) {
245245
page = .select_model;
246246
}
@@ -392,7 +392,7 @@ pub fn runLibGen() !void {
392392

393393
const exit_status = try child.wait();
394394

395-
std.debug.print("\nExit Status: {}\n", .{exit_status});
395+
std.log.info("\nExit Status: {}\n", .{exit_status});
396396
if (exit_status.Exited != 0) {
397397
page = .deploy_options;
398398
}

0 commit comments

Comments
 (0)