Skip to content

Commit 5f43962

Browse files
mstrathmanclaude
andcommitted
fix: address CodeQL + CodeRabbit on the training core
- CodeQL (high, `cpp/integer-multiplication-cast-to-long`): widen `fo->lr` before the float multiply in the GBT boosting step so the product is computed in `double`, not overflowed in `float` then converted. - CodeRabbit: `bld_leaf` no longer swallows an allocation failure. If the class-count buffer can't be allocated it now returns `SQLITE_NOMEM`, which `bld_build` propagates as `-1` and the trainer surfaces as `PREDICT_ERR_RESOURCE`, instead of writing a silently-wrong leaf. - CodeRabbit: prefix the five cross-translation-unit training externs with `predict0_` per the repo convention (`bld_build`, `train_gbt`, `train_mlp`, `intern_label`, `register_student`), updating the header, definitions, and every call site in predict-train.c and predict-distill.c. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent daddbe7 commit 5f43962

4 files changed

Lines changed: 52 additions & 45 deletions

File tree

‎predict-distill.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
400400
} else if (!teacher) {
401401
for (int i = 0; i < n; i++) {
402402
if (classify) {
403-
int cl = intern_label(&labels, &nclass, &nlab_cap, y_true_c[i], &rc);
403+
int cl = predict0_intern_label(&labels, &nclass, &nlab_cap, y_true_c[i], &rc);
404404
if (cl < 0)
405405
goto done;
406406
y_teach[i] = cl;
@@ -434,7 +434,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
434434
if (classify) {
435435
const char *pred = (const char *)sqlite3_column_text(tqs, 0);
436436
int cl =
437-
intern_label(&labels, &nclass, &nlab_cap, pred ? pred : "", &rc);
437+
predict0_intern_label(&labels, &nclass, &nlab_cap, pred ? pred : "", &rc);
438438
if (cl < 0)
439439
goto done;
440440
y_teach[ti] = cl;
@@ -483,7 +483,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
483483
PREDICT_ERR_OPTIONS);
484484
goto done;
485485
}
486-
rc = train_mlp(X, n_fit, nfeat, nclass, y_teach, soft ? soft_P : NULL,
486+
rc = predict0_train_mlp(X, n_fit, nfeat, nclass, y_teach, soft ? soft_P : NULL,
487487
0 /* classify */, MLP_HIDDEN, MLP_EPOCHS, MLP_LR,
488488
0 /* no skip */, &mlp, errmsg);
489489
if (rc != SQLITE_OK)
@@ -534,7 +534,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
534534
goto done;
535535
}
536536
} else if (is_gbt) {
537-
rc = train_gbt(X, n_fit, nfeat, classify ? 0 : 1, nclass, y_teach,
537+
rc = predict0_train_gbt(X, n_fit, nfeat, classify ? 0 : 1, nclass, y_teach,
538538
y_teach_r, soft ? soft_P : NULL, &forest, errmsg);
539539
if (rc != SQLITE_OK)
540540
goto done;
@@ -600,7 +600,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
600600
b.yr = y_teach_r;
601601
b.nclass = nclass;
602602
b.task = classify ? 0 : 1;
603-
int root = bld_build(&b, idx, n_fit, 0);
603+
int root = predict0_bld_build(&b, idx, n_fit, 0);
604604
if (root < 0) {
605605
sqlite3_free(b.nodes);
606606
rc = SQLITE_NOMEM;
@@ -647,7 +647,7 @@ static int distill_train(sqlite3 *db, const char *tq, DistOpts *o,
647647
goto done;
648648
}
649649
}
650-
rc = register_student(db, o->student_id, blob, blob_len, res->content_hash,
650+
rc = predict0_register_student(db, o->student_id, blob, blob_len, res->content_hash,
651651
errmsg);
652652
if (rc != SQLITE_OK)
653653
goto done;
@@ -978,7 +978,7 @@ static int fdistill_fit(sqlite3 *db, const f32 *X, const f32 *Y, int n, int L,
978978
if (n_hold < 1)
979979
n_hold = 1;
980980
int n_fit = n - n_hold;
981-
rc = train_mlp(X, n_fit, L, nout, NULL, Y, 1 /* regress */, nhid, epochs, lr,
981+
rc = predict0_train_mlp(X, n_fit, L, nout, NULL, Y, 1 /* regress */, nhid, epochs, lr,
982982
1 /* linear skip */, &mlp, errmsg);
983983
if (rc != SQLITE_OK)
984984
goto done;
@@ -1013,7 +1013,7 @@ static int fdistill_fit(sqlite3 *db, const f32 *X, const f32 *Y, int n, int L,
10131013
sqlite3_mprintf("%s: student serialize failed", PREDICT_ERR_RESOURCE);
10141014
goto done;
10151015
}
1016-
rc = register_student(db, student_id, blob, blob_len, res->content_hash,
1016+
rc = predict0_register_student(db, student_id, blob, blob_len, res->content_hash,
10171017
errmsg);
10181018
if (rc != SQLITE_OK)
10191019
goto done;

‎predict-student.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,8 @@ void predict0_mlp_free(MLP *m) {
468468

469469
/* forward on RAW features x; writes hidden[nhid] and out[nout] logits. With a
470470
* linear skip (Wskip, forecast student) the hidden path is scaled down and the
471-
* skip term added; nhid=0 makes it a pure linear map. Must match train_mlp. */
471+
* skip term added; nhid=0 makes it a pure linear map. Must match
472+
* predict0_train_mlp. */
472473
void predict0_mlp_forward(const MLP *m, const f32 *x, f32 *hid, f32 *out) {
473474
f64 rs = m->Wskip ? FCST_RES_SCALE : 1.0;
474475
for (int j = 0; j < m->nhid; j++) {

‎predict-train.c‎

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static void mlp_adam(f32 *p, f64 *g, f64 *mm, f64 *vv, int sz, f64 lr,
5353
* DLinear/TiDE skip: the linear part carries seasonal-naive + trend, the hidden
5454
* path a scaled nonlinear correction); with nhid=0 the model is purely linear.
5555
* Deterministic full-batch Adam; the caller sets any feat_names/labels. */
56-
int train_mlp(const f32 *X, int n, int nfeat, int nout, const i32 *yc,
56+
int predict0_train_mlp(const f32 *X, int n, int nfeat, int nout, const i32 *yc,
5757
const f32 *soft, int task, int nhid, int epochs, f32 lr,
5858
int use_skip, MLP *m, char **errmsg) {
5959
memset(m, 0, sizeof(*m));
@@ -258,25 +258,26 @@ static int bld_new_node(Builder *b) {
258258
return b->n++;
259259
}
260260

261-
/* Fill node `ni` as a leaf over rows idx[0..n). */
262-
static void bld_leaf(Builder *b, int ni, const int *idx, int n) {
261+
/* Fill node `ni` as a leaf over rows idx[0..n). Returns SQLITE_NOMEM (never a
262+
* bogus leaf) if the class-count buffer cannot be allocated. */
263+
static int bld_leaf(Builder *b, int ni, const int *idx, int n) {
263264
TreeNode *nd = &b->nodes[ni];
264265
nd->feature = -1;
265266
nd->left = nd->right = -1;
266267
if (b->task == 0) {
267268
int *cnt = sqlite3_malloc(sizeof(int) * b->nclass);
269+
if (!cnt)
270+
return SQLITE_NOMEM;
271+
memset(cnt, 0, sizeof(int) * b->nclass);
268272
int best = 0, bestc = -1;
269-
if (cnt) {
270-
memset(cnt, 0, sizeof(int) * b->nclass);
271-
for (int i = 0; i < n; i++)
272-
cnt[b->yc[idx[i]]]++;
273-
for (int c = 0; c < b->nclass; c++)
274-
if (cnt[c] > bestc) {
275-
bestc = cnt[c];
276-
best = c;
277-
}
278-
sqlite3_free(cnt);
279-
}
273+
for (int i = 0; i < n; i++)
274+
cnt[b->yc[idx[i]]]++;
275+
for (int c = 0; c < b->nclass; c++)
276+
if (cnt[c] > bestc) {
277+
bestc = cnt[c];
278+
best = c;
279+
}
280+
sqlite3_free(cnt);
280281
nd->klass = best;
281282
nd->conf = n ? (f32)bestc / (f32)n : 0.f;
282283
} else if (b->hess) {
@@ -297,6 +298,7 @@ static void bld_leaf(Builder *b, int ni, const int *idx, int n) {
297298
nd->value = n ? (f32)(s / n) : 0.f;
298299
nd->klass = -1;
299300
}
301+
return SQLITE_OK;
300302
}
301303

302304
/* Best (feature, threshold) split minimizing impurity, or feature<0 if none
@@ -395,7 +397,7 @@ static int bld_best_split(Builder *b, const int *idx, int n, int *feat_out,
395397
}
396398

397399
/* Recursively build; returns the node index, or -1 on OOM. */
398-
int bld_build(Builder *b, int *idx, int n, int depth) {
400+
int predict0_bld_build(Builder *b, int *idx, int n, int depth) {
399401
int ni = bld_new_node(b);
400402
if (ni < 0)
401403
return -1;
@@ -413,15 +415,17 @@ int bld_build(Builder *b, int *idx, int n, int depth) {
413415
int maxd = b->max_depth ? b->max_depth : TREE_MAX_DEPTH;
414416
int mins = b->min_split ? b->min_split : TREE_MIN_SPLIT;
415417
if (depth >= maxd || n < mins || pure) {
416-
bld_leaf(b, ni, idx, n);
418+
if (bld_leaf(b, ni, idx, n) != SQLITE_OK)
419+
return -1;
417420
return ni;
418421
}
419422
int feat;
420423
f32 thr;
421424
if (bld_best_split(b, idx, n, &feat, &thr) != SQLITE_OK)
422425
return -1;
423426
if (feat < 0) {
424-
bld_leaf(b, ni, idx, n);
427+
if (bld_leaf(b, ni, idx, n) != SQLITE_OK)
428+
return -1;
425429
return ni;
426430
}
427431
/* partition idx: rows with X[.,feat] < thr to the front */
@@ -438,13 +442,14 @@ int bld_build(Builder *b, int *idx, int n, int depth) {
438442
}
439443
int nl = lo;
440444
if (nl == 0 || nl == n) { /* degenerate split; make a leaf */
441-
bld_leaf(b, ni, idx, n);
445+
if (bld_leaf(b, ni, idx, n) != SQLITE_OK)
446+
return -1;
442447
return ni;
443448
}
444-
int L = bld_build(b, idx, nl, depth + 1);
449+
int L = predict0_bld_build(b, idx, nl, depth + 1);
445450
if (L < 0)
446451
return -1;
447-
int R = bld_build(b, idx + nl, n - nl, depth + 1);
452+
int R = predict0_bld_build(b, idx + nl, n - nl, depth + 1);
448453
if (R < 0)
449454
return -1;
450455
/* node index ni is stable across the reallocs above; set it now */
@@ -465,7 +470,7 @@ int bld_build(Builder *b, int *idx, int n, int depth) {
465470
* whole distribution (soft-label distillation), transferring the calibrated
466471
* probabilities a hard argmax throws away. NULL `soft` keeps the hard-label
467472
* path. Regression ignores `soft`. */
468-
int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
473+
int predict0_train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
469474
const i32 *yc, const f32 *yr, const f32 *soft, Forest *fo,
470475
char **errmsg) {
471476
memset(fo, 0, sizeof(*fo));
@@ -482,7 +487,7 @@ int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
482487
fo->tree_off = sqlite3_malloc(sizeof(int) * (rounds * nscore + 1));
483488
f64 *F = sqlite3_malloc(sizeof(f64) * (size_t)n * nscore);
484489
f64 *p = task == 0 ? sqlite3_malloc(sizeof(f64) * (size_t)n * nscore) : NULL;
485-
int *idx = sqlite3_malloc(sizeof(int) * n); /* scratch for bld_build */
490+
int *idx = sqlite3_malloc(sizeof(int) * n); /* scratch for predict0_bld_build */
486491
f32 *grad = sqlite3_malloc(sizeof(f32) * n);
487492
f32 *hess = task == 0 ? sqlite3_malloc(sizeof(f32) * n) : NULL;
488493
if (!fo->init || !fo->tree_off || !F || !idx || !grad ||
@@ -564,7 +569,7 @@ int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
564569
b.lambda = GBT_LAMBDA;
565570
for (int i = 0; i < n; i++)
566571
idx[i] = i;
567-
int root = bld_build(&b, idx, n, 0);
572+
int root = predict0_bld_build(&b, idx, n, 0);
568573
if (root < 0) {
569574
sqlite3_free(b.nodes);
570575
rc = SQLITE_NOMEM;
@@ -573,7 +578,8 @@ int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
573578
}
574579
for (int i = 0; i < n; i++)
575580
F[(size_t)i * nscore + s] +=
576-
fo->lr * predict0_reg_tree_value(b.nodes, b.n, &X[(size_t)i * nfeat]);
581+
(f64)fo->lr *
582+
predict0_reg_tree_value(b.nodes, b.n, &X[(size_t)i * nfeat]);
577583

578584
int need = fo->tree_off[nt] + b.n;
579585
if (need > pool_cap) {
@@ -606,7 +612,7 @@ int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
606612
return rc;
607613
}
608614

609-
int intern_label(char ***labels, int *nclass, int *cap, const char *s,
615+
int predict0_intern_label(char ***labels, int *nclass, int *cap, const char *s,
610616
int *rc) {
611617
for (int k = 0; k < *nclass; k++)
612618
if (strcmp((*labels)[k], s) == 0)
@@ -629,7 +635,7 @@ int intern_label(char ***labels, int *nclass, int *cap, const char *s,
629635
return (*nclass)++;
630636
}
631637

632-
int register_student(sqlite3 *db, const char *student_id,
638+
int predict0_register_student(sqlite3 *db, const char *student_id,
633639
const void *blob, int blob_len,
634640
char hash_out[PREDICT_HEX_BUFSIZE],
635641
char **errmsg) {
@@ -712,7 +718,7 @@ int predict0_train_student(sqlite3 *db, const f32 *X, int n, int nfeat,
712718
goto done;
713719
}
714720
for (int i = 0; i < n; i++) {
715-
int k = intern_label(&labels, &nclass, &lcap, ylab[i] ? ylab[i] : "", &rc);
721+
int k = predict0_intern_label(&labels, &nclass, &lcap, ylab[i] ? ylab[i] : "", &rc);
716722
if (k < 0)
717723
goto done;
718724
y_teach[i] = k;
@@ -736,7 +742,7 @@ int predict0_train_student(sqlite3 *db, const f32 *X, int n, int nfeat,
736742
}
737743

738744
if (is_gbt) {
739-
rc = train_gbt(X, n, nfeat, classify ? 0 : 1, nclass, y_teach, y_teach_r,
745+
rc = predict0_train_gbt(X, n, nfeat, classify ? 0 : 1, nclass, y_teach, y_teach_r,
740746
NULL, &forest, errmsg);
741747
if (rc != SQLITE_OK)
742748
goto done;
@@ -772,7 +778,7 @@ int predict0_train_student(sqlite3 *db, const f32 *X, int n, int nfeat,
772778
b.yr = y_teach_r;
773779
b.nclass = nclass;
774780
b.task = classify ? 0 : 1;
775-
int root = bld_build(&b, idx, n, 0);
781+
int root = predict0_bld_build(&b, idx, n, 0);
776782
if (root < 0) {
777783
sqlite3_free(b.nodes);
778784
rc = SQLITE_NOMEM;
@@ -809,7 +815,7 @@ int predict0_train_student(sqlite3 *db, const f32 *X, int n, int nfeat,
809815
rc = predict0_registry_ensure(db, errmsg); /* create _predict_models if new */
810816
if (rc != SQLITE_OK)
811817
goto done;
812-
rc = register_student(db, register_id, blob, blob_len, hash, errmsg);
818+
rc = predict0_register_student(db, register_id, blob, blob_len, hash, errmsg);
813819
if (rc != SQLITE_OK)
814820
goto done;
815821
}

‎predict-train.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define MLP_EPOCHS 400
1414
#define MLP_LR 0.02
1515

16-
/* Tree builder. The distill recipe constructs one in place and calls bld_build. */
16+
/* Tree builder. The distill recipe constructs one in place and calls predict0_bld_build. */
1717
typedef struct {
1818
TreeNode *nodes;
1919
int n, cap;
@@ -29,15 +29,15 @@ typedef struct {
2929
f32 lambda; /* GBT only: L2 leaf regularization */
3030
} Builder;
3131

32-
int bld_build(Builder *b, int *idx, int n, int depth);
33-
int train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
32+
int predict0_bld_build(Builder *b, int *idx, int n, int depth);
33+
int predict0_train_gbt(const f32 *X, int n, int nfeat, int task, int nclass,
3434
const i32 *yc, const f32 *yr, const f32 *soft, Forest *fo,
3535
char **errmsg);
36-
int train_mlp(const f32 *X, int n, int nfeat, int nout, const i32 *yc,
36+
int predict0_train_mlp(const f32 *X, int n, int nfeat, int nout, const i32 *yc,
3737
const f32 *soft, int task, int nhid, int epochs, f32 lr,
3838
int use_skip, MLP *m, char **errmsg);
39-
int intern_label(char ***labels, int *nclass, int *cap, const char *s, int *rc);
40-
int register_student(sqlite3 *db, const char *student_id, const void *blob,
39+
int predict0_intern_label(char ***labels, int *nclass, int *cap, const char *s, int *rc);
40+
int predict0_register_student(sqlite3 *db, const char *student_id, const void *blob,
4141
int blob_len, char hash_out[PREDICT_HEX_BUFSIZE],
4242
char **errmsg);
4343

0 commit comments

Comments
 (0)