| 1271 | * represented by the VM through shared zero-page bookkeeping. The CPU decode |
| 1272 | * KV cache grows one token at a time, so using calloc here can move thousands |
| 1273 | * of first-touch faults into generation. On Darwin we have observed this end |
| 1274 | * in a kernel cpt_mapcnt_inc overflow panic instead of a user-space error. |
| 1275 | * |
| 1276 | * Explicitly writing the zeroes while the cache is allocated keeps those VM |
| 1277 | * faults out of the token loop and gives the cache private resident pages. |
| 1278 | */ |
| 1279 | memset(p, 0, total); |
| 1280 | return p; |
| 1281 | } |
| 1282 | |
| 1283 | static double now_sec(void) { |
| 1284 | struct timespec ts; |
| 1285 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 1286 | return (double)ts.tv_sec + (double)ts.tv_nsec * 1.0e-9; |
| 1287 | } |
| 1288 | |
| 1289 | /* ========================================================================= |
| 1290 | * Metal Routed Expert Locality Profiler. |
| 1291 | * ========================================================================= |
| 1292 | * |
| 1293 | * SSD streaming decode needs routed-expert locality data before a cache policy |
| 1294 | * is meaningful. This profiler is intentionally Metal-decode-only for now: |
| 1295 | * it reads the tiny selected-expert and route-weight tensors after router |
| 1296 | * selection, records histograms, and simulates per-layer latest-N unique expert |
| 1297 | * caches without changing normal inference when disabled. |
| 1298 | */ |
| 1299 | |
| 1300 | enum { DS4_EXPERT_PROFILE_MAX_CAPS = 10 }; |
| 1301 | |
| 1302 | static const uint32_t ds4_expert_profile_cap_candidates[DS4_EXPERT_PROFILE_MAX_CAPS] = { |
| 1303 | 1, 2, 4, 8, 16, 32, 64, 128, 256, 384 |
no test coverage detected