From 3ea999ea9b12c83ae0f56952b93128654f9e0b10 Mon Sep 17 00:00:00 2001 From: Luke Hinds Date: Tue, 21 Oct 2025 15:52:26 +0100 Subject: [PATCH] Add error checking for weight file loading - Add fopen validation to catch missing weight files - Add fread validation for all weight reads (w1, b1, w2, b2) Signed-off-by: Luke Hinds --- src/main.cu | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main.cu b/src/main.cu index 05da523..45f6492 100644 --- a/src/main.cu +++ b/src/main.cu @@ -57,12 +57,31 @@ int main(int argc, char* argv[]) { model.b2 = (float*)malloc(out_features * sizeof(float)); FILE* f = fopen(weights_file, "rb"); - fread(model.w1, sizeof(float), in_features*hidden_features, f); - fread(model.b1, sizeof(float), hidden_features, f); - fread(model.w2, sizeof(float), hidden_features*out_features, f); - fread(model.b2, sizeof(float), out_features, f); + if (!f) { + fprintf(stderr, "Error: Could not open weights file: %s\n", weights_file); + return 1; + } + if (fread(model.w1, sizeof(float), in_features*hidden_features, f) != in_features*hidden_features) { + fprintf(stderr, "Error: Incomplete read of w1\n"); + fclose(f); + return 1; + } + if (fread(model.b1, sizeof(float), hidden_features, f) != hidden_features) { + fprintf(stderr, "Error: Incomplete read of b1\n"); + fclose(f); + return 1; + } + if (fread(model.w2, sizeof(float), hidden_features*out_features, f) != hidden_features*out_features) { + fprintf(stderr, "Error: Incomplete read of w2\n"); + fclose(f); + return 1; + } + if (fread(model.b2, sizeof(float), out_features, f) != out_features) { + fprintf(stderr, "Error: Incomplete read of b2\n"); + fclose(f); + return 1; + } fclose(f); - // ---------------- Allocate GPU weights ---------------- float *d_input, *d_output; float *d_w1, *d_b1, *d_w2, *d_b2; @@ -101,7 +120,6 @@ int main(int argc, char* argv[]) { for (int i = 0; i < 5; i++) { forward_tiled(model, d_input, d_hidden, d_output, B, tileB, in_features, out_features); } - ; cudaDeviceSynchronize(); // ---------------- Timing ----------------