diff --git a/src/omega_gpu_q38_graph.c b/src/omega_gpu_q38_graph.c index ad10394e..614fe00d 100644 --- a/src/omega_gpu_q38_graph.c +++ b/src/omega_gpu_q38_graph.c @@ -13,6 +13,7 @@ #include "omega_gpu_fmt.h" #include "omega_gpu_mem_cached.h" #include "cudapp_native.h" +#include #include #include @@ -248,10 +249,18 @@ uint64_t omega_gpu_q38_exact_extra_bytes(void) { const uint8_t unused[H*4]={0};uint64_t m[16]={3,248320},q[192]={0},result=0; return !omega_q38_runtime(&result,m,16,unused,sizeof unused,q,192) && result==1 ? q[10] : 0; } +/* A NaN, negative or infinite coefficient would silently pick a wrong greedy token; refuse it. */ +int omega_gpu_q38_exact_coef_ok(const float *coef, size_t n) { + if (!coef) return 0; + for (size_t i = 0; i < n; i++) + if (!(coef[i] >= 0.0f) || coef[i] > FLT_MAX) return 0; + return 1; +} int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head, const float *coef, uint32_t roundings) { if (!g || !head || !coef || g->in_token || !g->final_set || g->exact8 || g->head8 || - !shape_is(head, g->cfg.vocab, H) || g->cfg.eps != 1e-6f) return OMEGA_GPU_DGRAPH_BAD_ARGS; + !shape_is(head, g->cfg.vocab, H) || g->cfg.eps != 1e-6f || + !omega_gpu_q38_exact_coef_ok(coef, g->cfg.vocab)) return OMEGA_GPU_DGRAPH_BAD_ARGS; uint64_t m[16]={1,g->cfg.vocab},result=0,entry[2]; if (omega_q38_runtime(&result,m,16,cpu_of(g,g->lo.fnorm),H*4,g->exact_info,192) || result!=1 || roundings!=g->exact_info[2] || omega_gpu_fp8_tensor_entry(head,0,&entry[0],&entry[1])) diff --git a/src/omega_gpu_q38_graph.h b/src/omega_gpu_q38_graph.h index 30ae934b..ee6d73fd 100644 --- a/src/omega_gpu_q38_graph.h +++ b/src/omega_gpu_q38_graph.h @@ -97,6 +97,8 @@ int omega_gpu_q38_graph_set_lm_head_fp8(OmegaGpuQ38Graph *g, const OmegaGpuFp8Te * The original head is retained. Disable for sampled requests needing all logits. */ int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head, const float *coef, uint32_t roundings); +/* 1 when all n coefficients are finite and >= 0 (set_exact_head refuses anything else), else 0. */ +int omega_gpu_q38_exact_coef_ok(const float *coef, size_t n); uint64_t omega_gpu_q38_exact_extra_bytes(void); void omega_gpu_q38_graph_set_exact_head_on(OmegaGpuQ38Graph *g, int on); int omega_gpu_q38_graph_exact_head_active(const OmegaGpuQ38Graph *g); diff --git a/tests/gpu_q38_graph_test.c b/tests/gpu_q38_graph_test.c index 81b5b344..e767bbac 100644 --- a/tests/gpu_q38_graph_test.c +++ b/tests/gpu_q38_graph_test.c @@ -227,6 +227,16 @@ static int host_only(void) { OmegaGpuQ38GraphConfig c; memset(&c, 0, sizeof c); CHECK(omega_gpu_q38_graph_create(&c, &g) == OMEGA_GPU_DGRAPH_BAD_ARGS && !g, "create refuses an empty config"); CHECK(omega_gpu_q38_graph_create(NULL, &g) == OMEGA_GPU_DGRAPH_BAD_ARGS, "create refuses NULL"); + float coef[4] = {0.0f, 1.5f, 3.0e38f, 2.0f}; + CHECK(omega_gpu_q38_exact_coef_ok(coef, 4) == 1, "exact coef: finite nonnegative table accepted"); + const float bad_vals[3] = {NAN, -1.0f, INFINITY}; + const char *bad_names[3] = {"NaN", "negative", "infinite"}; + for (int i = 0; i < 3; i++) { + coef[2] = bad_vals[i]; + CHECK(omega_gpu_q38_exact_coef_ok(coef, 4) == 0, "exact coef: %s entry refused", bad_names[i]); + } + CHECK(omega_gpu_q38_exact_coef_ok(NULL, 4) == 0, "exact coef: NULL table refused"); + CHECK(omega_gpu_q38_graph_set_exact_head(NULL, NULL, coef, 0) == OMEGA_GPU_DGRAPH_BAD_ARGS, "set_exact_head refuses NULL graph"); return g_failed ? 1 : 0; }