@@ -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 }
0 commit comments