entrenar 0.7.8

Training & Optimization library with autograd, LoRA, quantization, and model merging
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
// Auto-generated contract assertions from YAML — DO NOT EDIT.
// Zero cost in release builds (debug_assert!).
// Regenerate: pv codegen contracts/ -o src/generated_contracts.rs
// Include:   #[macro_use] #[allow(unused_macros)] mod generated_contracts;

// Auto-generated from contracts/backward-pass-v1.yaml — DO NOT EDIT
// Contract: backward-pass-v1

/// Preconditions for equation `chain_rule`.
/// Domain-specific. Call: `contract_pre_chain_rule!(slice_expr)`
macro_rules! contract_pre_chain_rule {
    () => {{}};
    ($input:expr) => {{
        let grad_output = &$input;
        debug_assert!(
            !grad_output.is_empty(),
            "Contract chain_rule: precondition violated — !grad_output.is_empty()"
        );
    }};
}

/// Postconditions for equation `chain_rule`.
/// Call before return: `contract_post_chain_rule!(result_expr)`
macro_rules! contract_post_chain_rule {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            ret.len() == input.len(),
            "Contract chain_rule: postcondition violated — ret.len() == input.len()"
        );
        debug_assert!(
            ret.iter().all(|g| g.is_finite()),
            "Contract chain_rule: postcondition violated — ret.iter().all(|g| g.is_finite())"
        );
    }};
}

/// Combined pre+post contract for equation `chain_rule`.
macro_rules! contract_chain_rule {
    ($input:expr, $body:expr) => {{
        contract_pre_chain_rule!($input);
        let _contract_result = $body;
        contract_post_chain_rule!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `matmul_backward`.
/// Domain-specific. Call: `contract_pre_matmul_backward!(slice_expr)`
macro_rules! contract_pre_matmul_backward {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `matmul_backward`.
/// Call before return: `contract_post_matmul_backward!(result_expr)`
macro_rules! contract_post_matmul_backward {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(grad_a.len() == a.len(), "Contract matmul_backward: postcondition violated — grad_a.len() == a.len()");
        debug_assert!(grad_b.len() == b.len(), "Contract matmul_backward: postcondition violated — grad_b.len() == b.len()");
        debug_assert!(grad_a.iter().all(|g| g.is_finite()), "Contract matmul_backward: postcondition violated — grad_a.iter().all(|g| g.is_finite())");
        debug_assert!(grad_b.iter().all(|g| g.is_finite()), "Contract matmul_backward: postcondition violated — grad_b.iter().all(|g| g.is_finite())");
    }};
}

/// Combined pre+post contract for equation `matmul_backward`.
macro_rules! contract_matmul_backward {
    ($input:expr, $body:expr) => {{
        contract_pre_matmul_backward!($input);
        let _contract_result = $body;
        contract_post_matmul_backward!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `softmax_backward`.
/// Domain-specific. Call: `contract_pre_softmax_backward!(slice_expr)`
macro_rules! contract_pre_softmax_backward {
    () => {{}};
    ($input:expr) => {{
        let y = &$input;
        debug_assert!(!y.is_empty(),
            "Contract softmax_backward: precondition violated — !y.is_empty()");
        debug_assert!(y.iter().all(|v| *v >= 0.0 && v.is_finite()),
            "Contract softmax_backward: precondition violated — y.iter().all(|v| *v >= 0.0 && v.is_finite())");
        debug_assert!((y.iter().sum::<f32>() - 1.0).abs() < 1e-5,
            "Contract softmax_backward: precondition violated — (y.iter().sum::<f32>() - 1.0).abs() < 1e-5");
    }};
}

/// Postconditions for equation `softmax_backward`.
/// Call before return: `contract_post_softmax_backward!(result_expr)`
macro_rules! contract_post_softmax_backward {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret.len() == y.len(), "Contract softmax_backward: postcondition violated — ret.len() == y.len()");
        debug_assert!(ret.iter().all(|g| g.is_finite()), "Contract softmax_backward: postcondition violated — ret.iter().all(|g| g.is_finite())");
    }};
}

/// Combined pre+post contract for equation `softmax_backward`.
macro_rules! contract_softmax_backward {
    ($input:expr, $body:expr) => {{
        contract_pre_softmax_backward!($input);
        let _contract_result = $body;
        contract_post_softmax_backward!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/batch-v1.yaml — DO NOT EDIT
// Contract: batch-v1

/// Preconditions for equation `batch_partition`.
/// Call at function entry: `contract_pre_batch_partition!(input_expr)`
macro_rules! contract_pre_batch_partition {
    ($input:expr) => {{
        let _contract_input = &$input;
        debug_assert!(n > 0, "Contract batch_partition: precondition violated — n > 0");
        debug_assert!(
            batch_size > 0,
            "Contract batch_partition: precondition violated — batch_size > 0"
        );
    }};
}

/// Postconditions for equation `batch_partition`.
/// Call before return: `contract_post_batch_partition!(result_expr)`
macro_rules! contract_post_batch_partition {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret >= 1, "Contract batch_partition: postcondition violated — ret >= 1");
        debug_assert!(ret == (n + batch_size - 1) / batch_size, "Contract batch_partition: postcondition violated — ret == (n + batch_size - 1) / batch_size");
    }};
}

/// Combined pre+post contract for equation `batch_partition`.
macro_rules! contract_batch_partition {
    ($input:expr, $body:expr) => {{
        contract_pre_batch_partition!($input);
        let _contract_result = $body;
        contract_post_batch_partition!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `gradient_scaling`.
/// Domain-specific. Call: `contract_pre_gradient_scaling!(slice_expr)`
macro_rules! contract_pre_gradient_scaling {
    () => {{}};
    ($input:expr) => {{
        let gradients = &$input;
        debug_assert!(
            !gradients.is_empty(),
            "Contract gradient_scaling: precondition violated — !gradients.is_empty()"
        );
    }};
}

/// Postconditions for equation `gradient_scaling`.
/// Call before return: `contract_post_gradient_scaling!(result_expr)`
macro_rules! contract_post_gradient_scaling {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret.len() == gradients[0].len(), "Contract gradient_scaling: postcondition violated — ret.len() == gradients[0].len()");
        debug_assert!(ret.iter().all(|g| g.is_finite()), "Contract gradient_scaling: postcondition violated — ret.iter().all(|g| g.is_finite())");
    }};
}

/// Combined pre+post contract for equation `gradient_scaling`.
macro_rules! contract_gradient_scaling {
    ($input:expr, $body:expr) => {{
        contract_pre_gradient_scaling!($input);
        let _contract_result = $body;
        contract_post_gradient_scaling!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/checkpoint-v1.yaml — DO NOT EDIT
// Contract: checkpoint-v1

/// Preconditions for equation `checkpoint_memory`.
/// Call at function entry: `contract_pre_checkpoint_memory!(input_expr)`
macro_rules! contract_pre_checkpoint_memory {
    ($input:expr) => {{
        let _contract_input = &$input;
        debug_assert!(
            num_layers > 0,
            "Contract checkpoint_memory: precondition violated — num_layers > 0"
        );
        debug_assert!(
            activation_size > 0,
            "Contract checkpoint_memory: precondition violated — activation_size > 0"
        );
    }};
}

/// Postconditions for equation `checkpoint_memory`.
/// Call before return: `contract_post_checkpoint_memory!(result_expr)`
macro_rules! contract_post_checkpoint_memory {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(checkpoint_memory <= (num_layers as f64).sqrt().ceil() as usize * activation_size, "Contract checkpoint_memory: postcondition violated — checkpoint_memory <= (num_layers as f64).sqrt().ceil() as usize * activation_size");
    }};
}

/// Combined pre+post contract for equation `checkpoint_memory`.
macro_rules! contract_checkpoint_memory {
    ($input:expr, $body:expr) => {{
        contract_pre_checkpoint_memory!($input);
        let _contract_result = $body;
        contract_post_checkpoint_memory!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `save_restore_identity`.
/// Domain-specific. Call: `contract_pre_save_restore_identity!(slice_expr)`
macro_rules! contract_pre_save_restore_identity {
    () => {{}};
    ($input:expr) => {{
        let model_state = &$input;
    }};
}

/// Postconditions for equation `save_restore_identity`.
/// Call before return: `contract_post_save_restore_identity!(result_expr)`
macro_rules! contract_post_save_restore_identity {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(restored.weights == model_state.weights, "Contract save_restore_identity: postcondition violated — restored.weights == model_state.weights");
        debug_assert!(restored.epoch == model_state.epoch, "Contract save_restore_identity: postcondition violated — restored.epoch == model_state.epoch");
        debug_assert!(restored.optimizer_state == model_state.optimizer_state, "Contract save_restore_identity: postcondition violated — restored.optimizer_state == model_state.optimizer_state");
    }};
}

/// Combined pre+post contract for equation `save_restore_identity`.
macro_rules! contract_save_restore_identity {
    ($input:expr, $body:expr) => {{
        contract_pre_save_restore_identity!($input);
        let _contract_result = $body;
        contract_post_save_restore_identity!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/gemm-v1.yaml — DO NOT EDIT
// Contract: gemm-v1

/// Preconditions for equation `bf16_precision`.
/// Domain-specific. Call: `contract_pre_bf16_precision!(slice_expr)`
macro_rules! contract_pre_bf16_precision {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
        debug_assert!(
            !a.is_empty(),
            "Contract bf16_precision: precondition violated — !a.is_empty()"
        );
    }};
}

/// Postconditions for equation `bf16_precision`.
/// Call before return: `contract_post_bf16_precision!(result_expr)`
macro_rules! contract_post_bf16_precision {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            ret.iter().all(|v| v.is_finite()),
            "Contract bf16_precision: postcondition violated — ret.iter().all(|v| v.is_finite())"
        );
        debug_assert!(
            ret.len() == m * n,
            "Contract bf16_precision: postcondition violated — ret.len() == m * n"
        );
    }};
}

/// Combined pre+post contract for equation `bf16_precision`.
macro_rules! contract_bf16_precision {
    ($input:expr, $body:expr) => {{
        contract_pre_bf16_precision!($input);
        let _contract_result = $body;
        contract_post_bf16_precision!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `gemm`.
/// Domain-specific. Call: `contract_pre_gemm!(slice_expr)`
macro_rules! contract_pre_gemm {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `gemm`.
/// Call before return: `contract_post_gemm!(result_expr)`
macro_rules! contract_post_gemm {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(c.len() == m * n, "Contract gemm: postcondition violated — c.len() == m * n");
        debug_assert!(c.iter().all(|v| v.is_finite()), "Contract gemm: postcondition violated — c.iter().all(|v| v.is_finite())");
    }};
}

/// Combined pre+post contract for equation `gemm`.
macro_rules! contract_gemm {
    ($input:expr, $body:expr) => {{
        contract_pre_gemm!($input);
        let _contract_result = $body;
        contract_post_gemm!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `gemm_backward`.
/// Domain-specific. Call: `contract_pre_gemm_backward!(slice_expr)`
macro_rules! contract_pre_gemm_backward {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `gemm_backward`.
/// Call before return: `contract_post_gemm_backward!(result_expr)`
macro_rules! contract_post_gemm_backward {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(grad_a.len() == m * k, "Contract gemm_backward: postcondition violated — grad_a.len() == m * k");
        debug_assert!(grad_b.len() == k * n, "Contract gemm_backward: postcondition violated — grad_b.len() == k * n");
        debug_assert!(grad_a.iter().all(|v| v.is_finite()), "Contract gemm_backward: postcondition violated — grad_a.iter().all(|v| v.is_finite())");
        debug_assert!(grad_b.iter().all(|v| v.is_finite()), "Contract gemm_backward: postcondition violated — grad_b.iter().all(|v| v.is_finite())");
    }};
}

/// Combined pre+post contract for equation `gemm_backward`.
macro_rules! contract_gemm_backward {
    ($input:expr, $body:expr) => {{
        contract_pre_gemm_backward!($input);
        let _contract_result = $body;
        contract_post_gemm_backward!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/lora-v1.yaml — DO NOT EDIT
// Contract: lora-v1

/// Preconditions for equation `lora_decomposition`.
/// Domain-specific. Call: `contract_pre_lora_decomposition!(slice_expr)`
macro_rules! contract_pre_lora_decomposition {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `lora_decomposition`.
/// Call before return: `contract_post_lora_decomposition!(result_expr)`
macro_rules! contract_post_lora_decomposition {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret.len() == d * k, "Contract lora_decomposition: postcondition violated — ret.len() == d * k");
        debug_assert!(ret.iter().all(|v| v.is_finite()), "Contract lora_decomposition: postcondition violated — ret.iter().all(|v| v.is_finite())");
        debug_assert!(w_frozen_after == w_frozen_before, "Contract lora_decomposition: postcondition violated — w_frozen_after == w_frozen_before");
    }};
}

/// Combined pre+post contract for equation `lora_decomposition`.
macro_rules! contract_lora_decomposition {
    ($input:expr, $body:expr) => {{
        contract_pre_lora_decomposition!($input);
        let _contract_result = $body;
        contract_post_lora_decomposition!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/matmul-v1.yaml — DO NOT EDIT
// Contract: matmul-v1

/// Preconditions for equation `backward`.
/// Domain-specific. Call: `contract_pre_backward!(slice_expr)`
macro_rules! contract_pre_backward {
    () => {{}};
    ($input:expr) => {{
        let grad_output = &$input;
    }};
}

/// Postconditions for equation `backward`.
/// Call before return: `contract_post_backward!(result_expr)`
macro_rules! contract_post_backward {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            grad_a.len() == m * k,
            "Contract backward: postcondition violated — grad_a.len() == m * k"
        );
        debug_assert!(
            grad_b.len() == k * n,
            "Contract backward: postcondition violated — grad_b.len() == k * n"
        );
        debug_assert!(
            grad_a.iter().all(|g| g.is_finite()),
            "Contract backward: postcondition violated — grad_a.iter().all(|g| g.is_finite())"
        );
        debug_assert!(
            grad_b.iter().all(|g| g.is_finite()),
            "Contract backward: postcondition violated — grad_b.iter().all(|g| g.is_finite())"
        );
    }};
}

/// Combined pre+post contract for equation `backward`.
macro_rules! contract_backward {
    ($input:expr, $body:expr) => {{
        contract_pre_backward!($input);
        let _contract_result = $body;
        contract_post_backward!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `matmul`.
/// Domain-specific. Call: `contract_pre_matmul!(slice_expr)`
macro_rules! contract_pre_matmul {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `matmul`.
/// Call before return: `contract_post_matmul!(result_expr)`
macro_rules! contract_post_matmul {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            ret.len() == m * n,
            "Contract matmul: postcondition violated — ret.len() == m * n"
        );
        debug_assert!(
            ret.data().iter().all(|v| v.is_finite()),
            "Contract matmul: postcondition violated — ret.data().iter().all(|v| v.is_finite())"
        );
    }};
}

/// Combined pre+post contract for equation `matmul`.
macro_rules! contract_matmul {
    ($input:expr, $body:expr) => {{
        contract_pre_matmul!($input);
        let _contract_result = $body;
        contract_post_matmul!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `matmul_nt`.
/// Domain-specific. Call: `contract_pre_matmul_nt!(slice_expr)`
macro_rules! contract_pre_matmul_nt {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `matmul_nt`.
/// Call before return: `contract_post_matmul_nt!(result_expr)`
macro_rules! contract_post_matmul_nt {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret.len() == m * n, "Contract matmul_nt: postcondition violated — ret.len() == m * n");
        debug_assert!(ret.data().iter().all(|v| v.is_finite()), "Contract matmul_nt: postcondition violated — ret.data().iter().all(|v| v.is_finite())");
    }};
}

/// Combined pre+post contract for equation `matmul_nt`.
macro_rules! contract_matmul_nt {
    ($input:expr, $body:expr) => {{
        contract_pre_matmul_nt!($input);
        let _contract_result = $body;
        contract_post_matmul_nt!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/optimizer-v1.yaml — DO NOT EDIT
// Contract: optimizer-v1

/// Preconditions for equation `adamw_update`.
/// Domain-specific. Call: `contract_pre_adamw_update!(slice_expr)`
macro_rules! contract_pre_adamw_update {
    () => {{}};
    ($input:expr) => {{
        let x = &$input;
    }};
}

/// Postconditions for equation `adamw_update`.
/// Call before return: `contract_post_adamw_update!(result_expr)`
macro_rules! contract_post_adamw_update {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            params.iter().all(|p| p.is_finite()),
            "Contract adamw_update: postcondition violated — params.iter().all(|p| p.is_finite())"
        );
        debug_assert!(
            params.len() == params_before.len(),
            "Contract adamw_update: postcondition violated — params.len() == params_before.len()"
        );
    }};
}

/// Combined pre+post contract for equation `adamw_update`.
macro_rules! contract_adamw_update {
    ($input:expr, $body:expr) => {{
        contract_pre_adamw_update!($input);
        let _contract_result = $body;
        contract_post_adamw_update!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `convergence`.
/// Domain-specific. Call: `contract_pre_convergence!(slice_expr)`
macro_rules! contract_pre_convergence {
    () => {{}};
    ($input:expr) => {{
        let x = &$input;
    }};
}

/// Postconditions for equation `convergence`.
/// Call before return: `contract_post_convergence!(result_expr)`
macro_rules! contract_post_convergence {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            loss_final.is_finite(),
            "Contract convergence: postcondition violated — loss_final.is_finite()"
        );
        debug_assert!(
            loss_final <= loss_initial,
            "Contract convergence: postcondition violated — loss_final <= loss_initial"
        );
    }};
}

/// Combined pre+post contract for equation `convergence`.
macro_rules! contract_convergence {
    ($input:expr, $body:expr) => {{
        contract_pre_convergence!($input);
        let _contract_result = $body;
        contract_post_convergence!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `sgd_momentum_update`.
/// Domain-specific. Call: `contract_pre_sgd_momentum_update!(slice_expr)`
macro_rules! contract_pre_sgd_momentum_update {
    () => {{}};
    ($input:expr) => {{
        let grad = &$input;
    }};
}

/// Postconditions for equation `sgd_momentum_update`.
/// Call before return: `contract_post_sgd_momentum_update!(result_expr)`
macro_rules! contract_post_sgd_momentum_update {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(params.iter().all(|p| p.is_finite()), "Contract sgd_momentum_update: postcondition violated — params.iter().all(|p| p.is_finite())");
        debug_assert!(velocity.len() == params.len(), "Contract sgd_momentum_update: postcondition violated — velocity.len() == params.len()");
    }};
}

/// Combined pre+post contract for equation `sgd_momentum_update`.
macro_rules! contract_sgd_momentum_update {
    ($input:expr, $body:expr) => {{
        contract_pre_sgd_momentum_update!($input);
        let _contract_result = $body;
        contract_post_sgd_momentum_update!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/quantization-v1.yaml — DO NOT EDIT
// Contract: quantization-v1

/// Preconditions for equation `compression_ratio`.
/// Domain-specific. Call: `contract_pre_compression_ratio!(slice_expr)`
macro_rules! contract_pre_compression_ratio {
    () => {{}};
    ($input:expr) => {{
        let self = &$input;
        debug_assert!(
            self.len > 0,
            "Contract compression_ratio: precondition violated — self.len > 0"
        );
    }};
}

/// Postconditions for equation `compression_ratio`.
/// Call before return: `contract_post_compression_ratio!(result_expr)`
macro_rules! contract_post_compression_ratio {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret > 1.0, "Contract compression_ratio: postcondition violated — ret > 1.0");
        debug_assert!(ret.is_finite(), "Contract compression_ratio: postcondition violated — ret.is_finite()");
    }};
}

/// Combined pre+post contract for equation `compression_ratio`.
macro_rules! contract_compression_ratio {
    ($input:expr, $body:expr) => {{
        contract_pre_compression_ratio!($input);
        let _contract_result = $body;
        contract_post_compression_ratio!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `quantization_error`.
/// Domain-specific. Call: `contract_pre_quantization_error!(slice_expr)`
macro_rules! contract_pre_quantization_error {
    () => {{}};
    ($input:expr) => {{
        let x = &$input;
        debug_assert!(
            !x.is_empty(),
            "Contract quantization_error: precondition violated — !x.is_empty()"
        );
    }};
}

/// Postconditions for equation `quantization_error`.
/// Call before return: `contract_post_quantization_error!(result_expr)`
macro_rules! contract_post_quantization_error {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(mse.is_finite(), "Contract quantization_error: postcondition violated — mse.is_finite()");
        debug_assert!(mse >= 0.0, "Contract quantization_error: postcondition violated — mse >= 0.0");
        debug_assert!(mse <= (scale / 7.0).powi(2) / 4.0, "Contract quantization_error: postcondition violated — mse <= (scale / 7.0).powi(2) / 4.0");
    }};
}

/// Combined pre+post contract for equation `quantization_error`.
macro_rules! contract_quantization_error {
    ($input:expr, $body:expr) => {{
        contract_pre_quantization_error!($input);
        let _contract_result = $body;
        contract_post_quantization_error!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `symmetric_4bit`.
/// Domain-specific. Call: `contract_pre_symmetric_4bit!(slice_expr)`
macro_rules! contract_pre_symmetric_4bit {
    () => {{}};
    ($input:expr) => {{
        let input = &$input;
        debug_assert!(
            !input.is_empty(),
            "Contract symmetric_4bit: precondition violated — !input.is_empty()"
        );
        debug_assert!(
            input.iter().all(|v| v.is_finite()),
            "Contract symmetric_4bit: precondition violated — input.iter().all(|v| v.is_finite())"
        );
    }};
}

/// Postconditions for equation `symmetric_4bit`.
/// Call before return: `contract_post_symmetric_4bit!(result_expr)`
macro_rules! contract_post_symmetric_4bit {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(quantized.iter().all(|q| *q >= -7 && *q <= 7), "Contract symmetric_4bit: postcondition violated — quantized.iter().all(|q| *q >= -7 && *q <= 7)");
        debug_assert!(scale >= 0.0, "Contract symmetric_4bit: postcondition violated — scale >= 0.0");
    }};
}

/// Combined pre+post contract for equation `symmetric_4bit`.
macro_rules! contract_symmetric_4bit {
    ($input:expr, $body:expr) => {{
        contract_pre_symmetric_4bit!($input);
        let _contract_result = $body;
        contract_post_symmetric_4bit!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/softmax-v1.yaml — DO NOT EDIT
// Contract: softmax-v1

/// Preconditions for equation `log_sum_exp`.
/// Domain-specific. Call: `contract_pre_log_sum_exp!(slice_expr)`
macro_rules! contract_pre_log_sum_exp {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `log_sum_exp`.
/// Call before return: `contract_post_log_sum_exp!(result_expr)`
macro_rules! contract_post_log_sum_exp {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            ret.is_finite(),
            "Contract log_sum_exp: postcondition violated — ret.is_finite()"
        );
    }};
}

/// Combined pre+post contract for equation `log_sum_exp`.
macro_rules! contract_log_sum_exp {
    ($input:expr, $body:expr) => {{
        contract_pre_log_sum_exp!($input);
        let _contract_result = $body;
        contract_post_log_sum_exp!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `softmax`.
/// Domain-specific. Call: `contract_pre_softmax!(slice_expr)`
macro_rules! contract_pre_softmax {
    () => {{}};
    ($input:expr) => {{
        let a = &$input;
    }};
}

/// Postconditions for equation `softmax`.
/// Call before return: `contract_post_softmax!(result_expr)`
macro_rules! contract_post_softmax {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(ret.len() == a.len(), "Contract softmax: postcondition violated — ret.len() == a.len()");
        debug_assert!(ret.data().iter().all(|v| *v >= 0.0), "Contract softmax: postcondition violated — ret.data().iter().all(|v| *v >= 0.0)");
        debug_assert!((ret.data().iter().copied().sum::<f32>() - 1.0).abs() < 1e-6, "Contract softmax: postcondition violated — (ret.data().iter().copied().sum::<f32>() - 1.0).abs() < 1e-6");
    }};
}

/// Combined pre+post contract for equation `softmax`.
macro_rules! contract_softmax {
    ($input:expr, $body:expr) => {{
        contract_pre_softmax!($input);
        let _contract_result = $body;
        contract_post_softmax!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/tokenizer-v1.yaml — DO NOT EDIT
// Contract: tokenizer-v1

/// Preconditions for equation `bpe_merge`.
/// Call at function entry: `contract_pre_bpe_merge!(input_expr)`
macro_rules! contract_pre_bpe_merge {
    ($input:expr) => {{
        let _contract_input = &$input;
        debug_assert!(
            !corpus.is_empty(),
            "Contract bpe_merge: precondition violated — !corpus.is_empty()"
        );
        debug_assert!(k > 0, "Contract bpe_merge: precondition violated — k > 0");
        debug_assert!(
            k <= vocab_size - base_vocab_size,
            "Contract bpe_merge: precondition violated — k <= vocab_size - base_vocab_size"
        );
    }};
}

/// Postconditions for equation `bpe_merge`.
/// Call before return: `contract_post_bpe_merge!(result_expr)`
macro_rules! contract_post_bpe_merge {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(vocab.len() == base_vocab_size + k, "Contract bpe_merge: postcondition violated — vocab.len() == base_vocab_size + k");
        debug_assert!(total_tokens_after <= total_tokens_before, "Contract bpe_merge: postcondition violated — total_tokens_after <= total_tokens_before");
    }};
}

/// Combined pre+post contract for equation `bpe_merge`.
macro_rules! contract_bpe_merge {
    ($input:expr, $body:expr) => {{
        contract_pre_bpe_merge!($input);
        let _contract_result = $body;
        contract_post_bpe_merge!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `encode_decode_identity`.
/// Domain-specific. Call: `contract_pre_encode_decode_identity!(slice_expr)`
macro_rules! contract_pre_encode_decode_identity {
    () => {{}};
    ($input:expr) => {{
        let tokenizer = &$input;
        debug_assert!(
            tokenizer.is_trained(),
            "Contract encode_decode_identity: precondition violated — tokenizer.is_trained()"
        );
    }};
}

/// Postconditions for equation `encode_decode_identity`.
/// Call before return: `contract_post_encode_decode_identity!(result_expr)`
macro_rules! contract_post_encode_decode_identity {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(!ret.is_empty(), "Contract encode_decode_identity: postcondition violated — !ret.is_empty()");
        debug_assert!(ret.iter().all(|id| *id < vocab_size), "Contract encode_decode_identity: postcondition violated — ret.iter().all(|id| *id < vocab_size)");
    }};
}

/// Combined pre+post contract for equation `encode_decode_identity`.
macro_rules! contract_encode_decode_identity {
    ($input:expr, $body:expr) => {{
        contract_pre_encode_decode_identity!($input);
        let _contract_result = $body;
        contract_post_encode_decode_identity!(_contract_result);
        _contract_result
    }};
}

// Auto-generated from contracts/training-loop-v1.yaml — DO NOT EDIT
// Contract: training-loop-v1

/// Preconditions for equation `gradient_norm`.
/// Domain-specific. Call: `contract_pre_gradient_norm!(slice_expr)`
macro_rules! contract_pre_gradient_norm {
    () => {{}};
    ($input:expr) => {{
        let gradients = &$input;
        debug_assert!(
            !gradients.is_empty(),
            "Contract gradient_norm: precondition violated — !gradients.is_empty()"
        );
    }};
}

/// Postconditions for equation `gradient_norm`.
/// Call before return: `contract_post_gradient_norm!(result_expr)`
macro_rules! contract_post_gradient_norm {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(grad_norm <= clip_value, "Contract gradient_norm: postcondition violated — grad_norm <= clip_value");
        debug_assert!(gradients.iter().all(|g| g.is_finite()), "Contract gradient_norm: postcondition violated — gradients.iter().all(|g| g.is_finite())");
    }};
}

/// Combined pre+post contract for equation `gradient_norm`.
macro_rules! contract_gradient_norm {
    ($input:expr, $body:expr) => {{
        contract_pre_gradient_norm!($input);
        let _contract_result = $body;
        contract_post_gradient_norm!(_contract_result);
        _contract_result
    }};
}

/// Preconditions for equation `loss_decrease`.
/// Domain-specific. Call: `contract_pre_loss_decrease!(slice_expr)`
macro_rules! contract_pre_loss_decrease {
    () => {{}};
    ($input:expr) => {{
        let x = &$input;
    }};
}

/// Postconditions for equation `loss_decrease`.
/// Call before return: `contract_post_loss_decrease!(result_expr)`
macro_rules! contract_post_loss_decrease {
    ($result:expr) => {{
        let _contract_result = &$result;
        debug_assert!(
            loss.is_finite(),
            "Contract loss_decrease: postcondition violated — loss.is_finite()"
        );
        debug_assert!(
            !loss.is_nan(),
            "Contract loss_decrease: postcondition violated — !loss.is_nan()"
        );
    }};
}

/// Combined pre+post contract for equation `loss_decrease`.
macro_rules! contract_loss_decrease {
    ($input:expr, $body:expr) => {{
        contract_pre_loss_decrease!($input);
        let _contract_result = $body;
        contract_post_loss_decrease!(_contract_result);
        _contract_result
    }};
}

// Total: 19 preconditions, 60 postconditions from 11 contracts