66#include <math.h>
77#include <string.h>
88#include <stdlib.h>
9+ #include <limits.h>
910
1011#ifndef __EMSCRIPTEN__
1112#include <unistd.h>
@@ -43,7 +44,7 @@ typedef struct {
4344} BnMoECache ;
4445
4546static uint32_t moe_cache_hash (int layer , int expert_idx ) {
46- uint32_t key = (( uint32_t )layer << 16 ) | (uint32_t )( expert_idx & 0xFFFF ) ;
47+ uint32_t key = (uint32_t )layer * 65537u + (uint32_t )expert_idx ;
4748 // murmurhash3 finalizer
4849 key ^= key >> 16 ;
4950 key *= 0x85ebca6b ;
@@ -187,8 +188,10 @@ void *bn_moe_cache_create(size_t budget_bytes, size_t gate_bytes,
187188 size_t entry_bytes = gate_bytes + up_bytes + down_bytes ;
188189 if (entry_bytes == 0 ) return NULL ;
189190
190- int n_slots = (int )(budget_bytes / entry_bytes );
191- if (n_slots < 1 ) return NULL ;
191+ size_t raw_slots = budget_bytes / entry_bytes ;
192+ if (raw_slots < 1 ) return NULL ;
193+ if (raw_slots > (size_t )INT_MAX / 2 ) raw_slots = (size_t )INT_MAX / 2 ; // cap to avoid overflow
194+ int n_slots = (int )raw_slots ;
192195
193196 BnMoECache * c = (BnMoECache * )calloc (1 , sizeof (BnMoECache ));
194197 if (!c ) return NULL ;
@@ -198,10 +201,10 @@ void *bn_moe_cache_create(size_t budget_bytes, size_t gate_bytes,
198201 c -> up_bytes = up_bytes ;
199202 c -> n_slots = n_slots ;
200203
201- // Hash table: next power of 2 >= 2 * n_slots
202- int hs = 1 ;
203- while (hs < 2 * n_slots ) hs *= 2 ;
204- c -> hash_size = hs ;
204+ // Hash table: next power of 2 >= 2 * n_slots (unsigned to avoid overflow)
205+ unsigned hs = 1 ;
206+ while (hs < ( unsigned ) n_slots * 2 ) hs *= 2 ;
207+ c -> hash_size = ( int ) hs ;
205208
206209 // Allocate slab (32-byte aligned)
207210 size_t slab_size = (size_t )n_slots * entry_bytes ;
@@ -222,8 +225,8 @@ void *bn_moe_cache_create(size_t budget_bytes, size_t gate_bytes,
222225 return NULL ;
223226 }
224227
225- // Initialize
226- memset ( c -> hash_table , 0xFF , ( size_t ) hs * sizeof (int )); // -1
228+ // Initialize hash table to -1 (empty)
229+ for ( int i = 0 ; i < (int )hs ; i ++ ) c -> hash_table [ i ] = -1 ;
227230 c -> lru_head = c -> lru_tail = -1 ;
228231
229232 // Build free list (singly-linked via .next)
@@ -896,14 +899,16 @@ void bn_moe_forward(BnModel *m, BnLayerWeights *lw, int l) {
896899 ms -> prefetch_wait_ms += moe_time_ms () - tw ;
897900 COLLECT_PF_STATS (pf_gu );
898901 if (!ok ) {
899- pread (ms -> fd , miss_g_dst , miss_g_sz , (off_t )miss_g_off );
900- pread (ms -> fd , miss_u_dst , miss_u_sz , (off_t )miss_u_off );
902+ if (pread (ms -> fd , miss_g_dst , miss_g_sz , (off_t )miss_g_off ) < 0 )
903+ SH_LOG_ERROR ("Fallback gate pread failed" );
904+ if (pread (ms -> fd , miss_u_dst , miss_u_sz , (off_t )miss_u_off ) < 0 )
905+ SH_LOG_ERROR ("Fallback up pread failed" );
901906 }
902907 } else {
903- pread (ms -> fd , miss_g_dst , miss_g_sz , (off_t )miss_g_off );
904- pread (ms -> fd , miss_u_dst , miss_u_sz , (off_t )miss_u_off );
908+ ( void ) pread (ms -> fd , miss_g_dst , miss_g_sz , (off_t )miss_g_off );
909+ ( void ) pread (ms -> fd , miss_u_dst , miss_u_sz , (off_t )miss_u_off );
905910 if (!pf_dn )
906- pread (ms -> fd , miss_d_dst , miss_d_sz , (off_t )miss_d_off );
911+ ( void ) pread (ms -> fd , miss_d_dst , miss_d_sz , (off_t )miss_d_off );
907912 }
908913 gate_ptr = miss_g_dst ;
909914 up_ptr = miss_u_dst ;
@@ -934,13 +939,15 @@ void bn_moe_forward(BnModel *m, BnLayerWeights *lw, int l) {
934939 ms -> prefetch_wait_ms += moe_time_ms () - tw ;
935940 COLLECT_PF_STATS (pf_gu );
936941 if (!ok ) {
937- pread (ms -> fd , g_dst , g_sz , (off_t )g_off );
938- pread (ms -> fd , u_dst , u_sz , (off_t )u_off );
942+ if (pread (ms -> fd , g_dst , g_sz , (off_t )g_off ) < 0 )
943+ SH_LOG_ERROR ("Fallback gate pread failed" );
944+ if (pread (ms -> fd , u_dst , u_sz , (off_t )u_off ) < 0 )
945+ SH_LOG_ERROR ("Fallback up pread failed" );
939946 }
940947 } else {
941- pread (ms -> fd , g_dst , g_sz , (off_t )g_off );
942- pread (ms -> fd , u_dst , u_sz , (off_t )u_off );
943- pread (ms -> fd , d_dst , d_sz , (off_t )d_off );
948+ ( void ) pread (ms -> fd , g_dst , g_sz , (off_t )g_off );
949+ ( void ) pread (ms -> fd , u_dst , u_sz , (off_t )u_off );
950+ ( void ) pread (ms -> fd , d_dst , d_sz , (off_t )d_off );
944951 }
945952
946953 gate_ptr = g_dst ;
@@ -1148,10 +1155,14 @@ void bn_moe_prefetch_create(BnMoEState *ms) {
11481155 if (ms -> fd >= 0 && !ms -> mmap_base ) {
11491156 ms -> prefetch = moe_prefetch_init (ms -> fd );
11501157 ms -> prefetch_down = moe_prefetch_init (ms -> fd );
1151- if (ms -> prefetch && ms -> prefetch_down )
1158+ if (ms -> prefetch && ms -> prefetch_down ) {
11521159 SH_LOG_INFO ("MoE I/O prefetch threads" , "status" , "2 created (gate+up, down)" );
1153- else
1154- SH_LOG_INFO ("MoE I/O prefetch threads" , "status" , "partial" );
1160+ } else {
1161+ // Clean up partial init — free whichever succeeded
1162+ if (ms -> prefetch ) { moe_prefetch_free ((BnMoEPrefetch * )ms -> prefetch ); ms -> prefetch = NULL ; }
1163+ if (ms -> prefetch_down ) { moe_prefetch_free ((BnMoEPrefetch * )ms -> prefetch_down ); ms -> prefetch_down = NULL ; }
1164+ SH_LOG_WARN ("MoE I/O prefetch threads failed to create" );
1165+ }
11551166 }
11561167#endif
11571168}
0 commit comments