diff --git a/src/omega_gpu_elementwise_api.c b/src/omega_gpu_elementwise_api.c index d825a8d0..0e59a5c0 100644 --- a/src/omega_gpu_elementwise_api.c +++ b/src/omega_gpu_elementwise_api.c @@ -1061,6 +1061,18 @@ static int begin(OmegaGpuEwOp op, Slot **ks, OmegaGpuEwInfo *info) { return OMEGA_GPU_EW_OK; } +/* Turn the current record count into the only extent run_1d may stage. Keep + * the outputs unchanged on refusal so no caller can reuse a partial extent. */ +static int ew_chunk_1d(uint32_t remaining, uint64_t record_count, + uint32_t *count, size_t *bytes) { + if (record_count == 0 || record_count > remaining || + record_count > SIZE_MAX / sizeof(uint32_t)) + return OMEGA_GPU_EW_BAD_ARGS; + *count = (uint32_t)record_count; + *bytes = (size_t)record_count * sizeof(uint32_t); + return OMEGA_GPU_EW_OK; +} + int omega_gpu_rmsnorm_f32(uint32_t rows, uint32_t dim, const float *x, const float *weight, float eps, float *out, OmegaGpuEwInfo *info) { uint64_t t0 = now_ns(); @@ -1126,11 +1138,16 @@ static int run_1d(OmegaGpuEwOp op, uint32_t n, const void *a, const void *b, voi if (rc != OMEGA_GPU_EW_OK) return rc; for (uint32_t i0 = 0; i0 < n && rc == OMEGA_GPU_EW_OK; ) { uint64_t rec[8]; - if (av_ew_rec(0, n - i0, 0, 0, rec)) { rc = OMEGA_GPU_EW_BAD_ARGS; break; } - uint32_t ni = (uint32_t)rec[0]; + uint32_t remaining = n - i0; + if (av_ew_rec(0, remaining, 0, 0, rec)) { rc = OMEGA_GPU_EW_BAD_ARGS; break; } + uint32_t ni; + size_t bytes; + if (ew_chunk_1d(remaining, rec[0], &ni, &bytes) != OMEGA_GPU_EW_OK) { + rc = OMEGA_GPU_EW_BAD_ARGS; + break; + } uint32_t grid = (uint32_t)rec[1]; uint32_t params[4] = { ni, 0, 0, 0 }; - size_t bytes=p[10]; rc = run_abc(k, (const uint8_t *)a + (size_t)i0 * 4, bytes, b ? (const uint8_t *)b + (size_t)i0 * 4 : a, bytes, (uint8_t *)c + (size_t)i0 * 4, bytes, (uint32_t)rec[6], grid, params, info); i0 += ni; diff --git a/tests/api_kgen_ew_diff_test.c b/tests/api_kgen_ew_diff_test.c index 46311c76..f0bfc717 100644 --- a/tests/api_kgen_ew_diff_test.c +++ b/tests/api_kgen_ew_diff_test.c @@ -17,6 +17,39 @@ static int cubin_same(const uint8_t *a, size_t na, const uint8_t *b, size_t nb) int same=ra==0 && rb==0 && nca==ncb && memcmp(ca,cb,nca)==0; omega_cubin_free(ca);omega_cubin_free(cb);return same; } +static int chunk_partition(void) { + struct { + uint32_t remaining; + uint64_t record_count; + int rc; + uint32_t count; + size_t bytes; + } cases[] = { + { 131073, 131072, OMEGA_GPU_EW_OK, 131072, 524288 }, + { 1, 1, OMEGA_GPU_EW_OK, 1, 4 }, + { 128, 128, OMEGA_GPU_EW_OK, 128, 512 }, + { 1, 0, OMEGA_GPU_EW_BAD_ARGS, 0, 0 }, + { 1, 2, OMEGA_GPU_EW_BAD_ARGS, 0, 0 }, + { UINT32_MAX, UINT64_MAX, OMEGA_GPU_EW_BAD_ARGS, 0, 0 }, + }; + unsigned accepted=0,refused=0; + for(size_t i=0;i n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n); c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2); + guard_fill(got + n, guard_words); c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2); + CHECK(guard_bad(got + n, guard_words) == 0, "ex2 n=131073 mutant output guard remains intact"); c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; omega_gpu_elementwise_test_set_mutant(0); record(c, out); free(x); free(got); free(want); free(wrong); } /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */ { - size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4); + size_t n = 131200, guard_words = OMEGA_GPU_EW_MAX_CTAS * OMEGA_GPU_EW_THREADS - 128; + size_t capacity = n + guard_words; + uint32_t *x = malloc(capacity * 4), *got = malloc(capacity * 4); for (size_t i = 0; i < n; i++) x[i] = lcg(&seed); + guard_fill(x + n, guard_words); guard_fill(got + n, guard_words); Case c = { .name = "shared_xchg_probe", .n = n }; c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++; + CHECK(guard_bad(x + n, guard_words) == 0, "xchg n=131200 input guard remains intact"); + CHECK(guard_bad(got + n, guard_words) == 0, "xchg n=131200 output guard remains intact"); c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG); + guard_fill(got + n, guard_words); c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++; + CHECK(guard_bad(got + n, guard_words) == 0, "xchg n=131200 mutant output guard remains intact"); c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; omega_gpu_elementwise_test_set_mutant(0); record(c, out); free(x); free(got);