314{
315 if (!out || !graph) {
316 return;
317 }
318
320
321 fprintf(out,
322 "typedef struct {\n"
323 " size_t offset;\n"
324 " size_t size;\n"
325 "} CKV2Span;\n\n");
326
327 fprintf(out, "typedef struct {\n");
329 fprintf(out, "} CKV2HeaderWeights;\n\n");
330
331 fprintf(out, "typedef struct {\n");
333 fprintf(out, "} CKV2LayerWeights;\n\n");
334
335 fprintf(out, "typedef struct {\n");
337 fprintf(out, "} CKV2FooterWeights;\n\n");
338
339 fprintf(out, "typedef struct {\n");
341 fprintf(out, "} CKV2HeaderActivations;\n\n");
342
343 fprintf(out, "typedef struct {\n");
345 fprintf(out, "} CKV2LayerActivations;\n\n");
346
347 fprintf(out, "typedef struct {\n");
349 fprintf(out, "} CKV2FooterActivations;\n\n");
350
351 fprintf(out, "typedef struct {\n");
353 fprintf(out, "} CKV2HeaderGrads;\n\n");
354
355 fprintf(out, "typedef struct {\n");
357 fprintf(out, "} CKV2LayerGrads;\n\n");
358
359 fprintf(out, "typedef struct {\n");
361 fprintf(out, "} CKV2FooterGrads;\n\n");
362
363 fprintf(out,
364 "typedef struct {\n"
365 " CKV2HeaderWeights header;\n"
366 " CKV2LayerWeights body;\n"
367 " CKV2FooterWeights footer;\n"
368 " size_t layer_stride_bytes;\n"
369 " size_t total_bytes;\n"
370 "} CKV2WeightLayout;\n\n");
371
372 fprintf(out,
373 "typedef struct {\n"
374 " CKV2HeaderActivations header;\n"
375 " CKV2LayerActivations body;\n"
376 " CKV2FooterActivations footer;\n"
377 " size_t layer_stride_bytes;\n"
378 " size_t total_bytes;\n"
379 "} CKV2ActivationLayout;\n\n");
380
381 fprintf(out,
382 "typedef struct {\n"
383 " CKV2HeaderGrads header;\n"
384 " CKV2LayerGrads body;\n"
385 " CKV2FooterGrads footer;\n"
386 " size_t layer_stride_bytes;\n"
387 " size_t total_bytes;\n"
388 "} CKV2GradLayout;\n\n");
389
390 fprintf(out,
391 "typedef struct {\n"
392 " int num_layers;\n"
393 " int context_window;\n"
394 " int hidden_size;\n"
395 " int intermediate_size;\n"
396 " int num_heads;\n"
397 " int num_kv_heads;\n"
398 " int head_dim;\n"
399 " int vocab_size;\n"
400 "} CKV2ModelConfig;\n\n");
401
402 fprintf(out,
403 "typedef struct {\n"
404 " CKV2WeightLayout weights;\n"
405 " CKV2ActivationLayout activations;\n"
406 " CKV2GradLayout grads;\n"
407 "} CKV2SectionLayout;\n\n");
408
409 fprintf(out,
410 "typedef struct {\n"
411 " CKV2SectionLayout prefill;\n"
412 " CKV2SectionLayout decode;\n"
413 " CKV2SectionLayout backward;\n"
414 "} CKV2SectionLayouts;\n\n");
415
416 fprintf(out,
417 "typedef struct {\n"
418 " CKV2ModelConfig config;\n"
419 " CKV2SectionLayouts decoder;\n"
420 "} CKV2RuntimeLayout;\n\n");
421
422 fprintf(out, "static const CKV2RuntimeLayout ck_v2_layout = {\n");
423 fprintf(out, " .config = {\n");
430 fprintf(out, " .head_dim = %d,\n",
433 fprintf(out, " },\n");
434 fprintf(out, " .decoder = {\n");
435
436 const CKMemPlan *plans[3] = { prefill_plan, decode_plan, backward_plan };
437 const char *modes[3] = { "prefill", "decode", "backward" };
438
439 for (int mode = 0; mode < 3; ++mode) {
441 fprintf(out, " .%s = {\n", modes[mode]);
442
443 size_t offset = 0;
444 size_t header_end = 0;
445 size_t layer_stride = 0;
446 size_t footer_base = 0;
447
448 fprintf(out, " .weights = {\n");
449 fprintf(out, " .header = {\n");
451 header_end = offset;
452 fprintf(out, " },\n");
453
454 fprintf(out, " .body = {\n");
456 fprintf(out, " },\n");
457
458 footer_base = header_end + layer_stride * (size_t)L;
459 offset = footer_base;
460 fprintf(out, " .footer = {\n");
462 fprintf(out, " },\n");
463
464 fprintf(out, " .layer_stride_bytes = %zu,\n", layer_stride);
465 fprintf(out, " .total_bytes = %zu\n", offset);
466 fprintf(out, " },\n");
467
468 offset = 0;
469 header_end = 0;
470 layer_stride = 0;
471 footer_base = 0;
472
473 fprintf(out, " .activations = {\n");
474 fprintf(out, " .header = {\n");
476 header_end = offset;
477 fprintf(out, " },\n");
478
479 fprintf(out, " .body = {\n");
481 fprintf(out, " },\n");
482
483 footer_base = header_end + layer_stride * (size_t)L;
484 offset = footer_base;
485 fprintf(out, " .footer = {\n");
487 fprintf(out, " },\n");
488
489 fprintf(out, " .layer_stride_bytes = %zu,\n", layer_stride);
490 fprintf(out, " .total_bytes = %zu\n", offset);
491 fprintf(out, " },\n");
492
493 offset = 0;
494 header_end = 0;
495 layer_stride = 0;
496 footer_base = 0;
497
498 fprintf(out, " .grads = {\n");
499 fprintf(out, " .header = {\n");
501 header_end = offset;
502 fprintf(out, " },\n");
503
504 fprintf(out, " .body = {\n");
506 fprintf(out, " },\n");
507
508 footer_base = header_end + layer_stride * (size_t)L;
509 offset = footer_base;
510 fprintf(out, " .footer = {\n");
512 fprintf(out, " },\n");
513
514 fprintf(out, " .layer_stride_bytes = %zu,\n", layer_stride);
515 fprintf(out, " .total_bytes = %zu\n", offset);
516 fprintf(out, " }\n");
517
518 fprintf(out, " }%s\n", mode == 2 ? "" : ",");
519 }
520
521 fprintf(out, " }\n");
522 fprintf(out, "};\n\n");
523}
static void emit_footer_fields(FILE *out, const CKIRV2Graph *graph, CKBufferRole role_filter, int activation_group)
static size_t emit_body_values(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan, CKBufferRole role_filter, int activation_group)
static void emit_header_values(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan, CKBufferRole role_filter, int activation_group, size_t *offset)
static void emit_body_fields(FILE *out, const CKIRV2Graph *graph, CKBufferRole role_filter, int activation_group)
static void emit_header_fields(FILE *out, const CKIRV2Graph *graph, CKBufferRole role_filter, int activation_group)
static void emit_footer_values(FILE *out, const CKIRV2Graph *graph, const CKMemPlan *plan, CKBufferRole role_filter, int activation_group, size_t *offset)