llama-cpp-bindings 0.6.0

llama.cpp bindings for Rust
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
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
//! A safe wrapper around `llama_context_params`.
use std::fmt::Debug;
use std::num::NonZeroU32;

/// A rusty wrapper around `rope_scaling_type`.
#[repr(i8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RopeScalingType {
    /// The scaling type is unspecified
    Unspecified = -1,
    /// No scaling
    None = 0,
    /// Linear scaling
    Linear = 1,
    /// Yarn scaling
    Yarn = 2,
}

/// Create a `RopeScalingType` from a `c_int` - returns `RopeScalingType::ScalingUnspecified` if
/// the value is not recognized.
impl From<i32> for RopeScalingType {
    fn from(value: i32) -> Self {
        match value {
            0 => Self::None,
            1 => Self::Linear,
            2 => Self::Yarn,
            _ => Self::Unspecified,
        }
    }
}

/// Create a `c_int` from a `RopeScalingType`.
impl From<RopeScalingType> for i32 {
    fn from(value: RopeScalingType) -> Self {
        match value {
            RopeScalingType::None => 0,
            RopeScalingType::Linear => 1,
            RopeScalingType::Yarn => 2,
            RopeScalingType::Unspecified => -1,
        }
    }
}

/// A rusty wrapper around `LLAMA_POOLING_TYPE`.
#[repr(i8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LlamaPoolingType {
    /// The pooling type is unspecified
    Unspecified = -1,
    /// No pooling
    None = 0,
    /// Mean pooling
    Mean = 1,
    /// CLS pooling
    Cls = 2,
    /// Last pooling
    Last = 3,
    /// Rank pooling
    Rank = 4,
}

/// Create a `LlamaPoolingType` from a `c_int` - returns `LlamaPoolingType::Unspecified` if
/// the value is not recognized.
impl From<i32> for LlamaPoolingType {
    fn from(value: i32) -> Self {
        match value {
            0 => Self::None,
            1 => Self::Mean,
            2 => Self::Cls,
            3 => Self::Last,
            4 => Self::Rank,
            _ => Self::Unspecified,
        }
    }
}

/// Create a `c_int` from a `LlamaPoolingType`.
impl From<LlamaPoolingType> for i32 {
    fn from(value: LlamaPoolingType) -> Self {
        match value {
            LlamaPoolingType::None => 0,
            LlamaPoolingType::Mean => 1,
            LlamaPoolingType::Cls => 2,
            LlamaPoolingType::Last => 3,
            LlamaPoolingType::Rank => 4,
            LlamaPoolingType::Unspecified => -1,
        }
    }
}

/// A rusty wrapper around `LLAMA_ATTENTION_TYPE`.
#[repr(i8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LlamaAttentionType {
    /// The attention type is unspecified
    Unspecified = -1,
    /// Causal attention
    Causal = 0,
    /// Non-causal attention
    NonCausal = 1,
}

impl From<i32> for LlamaAttentionType {
    fn from(value: i32) -> Self {
        match value {
            0 => Self::Causal,
            1 => Self::NonCausal,
            _ => Self::Unspecified,
        }
    }
}

impl From<LlamaAttentionType> for i32 {
    fn from(value: LlamaAttentionType) -> Self {
        match value {
            LlamaAttentionType::Causal => 0,
            LlamaAttentionType::NonCausal => 1,
            LlamaAttentionType::Unspecified => -1,
        }
    }
}

/// A rusty wrapper around `ggml_type` for KV cache types.
#[expect(
    non_camel_case_types,
    reason = "variant names mirror llama.cpp's `enum ggml_type` symbol names verbatim so they can \
              be matched 1:1 against the C ABI without a translation table"
)]
#[expect(
    missing_docs,
    reason = "each variant denotes a quantisation flavour whose semantics are defined upstream in \
              ggml; restating the upstream spec inline would risk drifting from the source of truth"
)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum KvCacheType {
    /// Represents an unknown or not-yet-mapped `ggml_type` and carries the raw value.
    /// When passed through FFI, the raw value is used as-is (if llama.cpp supports it,
    /// the runtime will operate with that type).
    /// This variant preserves API compatibility when new `ggml_type` values are
    /// introduced in the future.
    Unknown(llama_cpp_bindings_sys::ggml_type),
    F32,
    F16,
    Q4_0,
    Q4_1,
    Q5_0,
    Q5_1,
    Q8_0,
    Q8_1,
    Q2_K,
    Q3_K,
    Q4_K,
    Q5_K,
    Q6_K,
    Q8_K,
    IQ2_XXS,
    IQ2_XS,
    IQ3_XXS,
    IQ1_S,
    IQ4_NL,
    IQ3_S,
    IQ2_S,
    IQ4_XS,
    I8,
    I16,
    I32,
    I64,
    F64,
    IQ1_M,
    BF16,
    TQ1_0,
    TQ2_0,
    MXFP4,
}

impl From<KvCacheType> for llama_cpp_bindings_sys::ggml_type {
    fn from(value: KvCacheType) -> Self {
        match value {
            KvCacheType::Unknown(raw) => raw,
            KvCacheType::F32 => llama_cpp_bindings_sys::GGML_TYPE_F32,
            KvCacheType::F16 => llama_cpp_bindings_sys::GGML_TYPE_F16,
            KvCacheType::Q4_0 => llama_cpp_bindings_sys::GGML_TYPE_Q4_0,
            KvCacheType::Q4_1 => llama_cpp_bindings_sys::GGML_TYPE_Q4_1,
            KvCacheType::Q5_0 => llama_cpp_bindings_sys::GGML_TYPE_Q5_0,
            KvCacheType::Q5_1 => llama_cpp_bindings_sys::GGML_TYPE_Q5_1,
            KvCacheType::Q8_0 => llama_cpp_bindings_sys::GGML_TYPE_Q8_0,
            KvCacheType::Q8_1 => llama_cpp_bindings_sys::GGML_TYPE_Q8_1,
            KvCacheType::Q2_K => llama_cpp_bindings_sys::GGML_TYPE_Q2_K,
            KvCacheType::Q3_K => llama_cpp_bindings_sys::GGML_TYPE_Q3_K,
            KvCacheType::Q4_K => llama_cpp_bindings_sys::GGML_TYPE_Q4_K,
            KvCacheType::Q5_K => llama_cpp_bindings_sys::GGML_TYPE_Q5_K,
            KvCacheType::Q6_K => llama_cpp_bindings_sys::GGML_TYPE_Q6_K,
            KvCacheType::Q8_K => llama_cpp_bindings_sys::GGML_TYPE_Q8_K,
            KvCacheType::IQ2_XXS => llama_cpp_bindings_sys::GGML_TYPE_IQ2_XXS,
            KvCacheType::IQ2_XS => llama_cpp_bindings_sys::GGML_TYPE_IQ2_XS,
            KvCacheType::IQ3_XXS => llama_cpp_bindings_sys::GGML_TYPE_IQ3_XXS,
            KvCacheType::IQ1_S => llama_cpp_bindings_sys::GGML_TYPE_IQ1_S,
            KvCacheType::IQ4_NL => llama_cpp_bindings_sys::GGML_TYPE_IQ4_NL,
            KvCacheType::IQ3_S => llama_cpp_bindings_sys::GGML_TYPE_IQ3_S,
            KvCacheType::IQ2_S => llama_cpp_bindings_sys::GGML_TYPE_IQ2_S,
            KvCacheType::IQ4_XS => llama_cpp_bindings_sys::GGML_TYPE_IQ4_XS,
            KvCacheType::I8 => llama_cpp_bindings_sys::GGML_TYPE_I8,
            KvCacheType::I16 => llama_cpp_bindings_sys::GGML_TYPE_I16,
            KvCacheType::I32 => llama_cpp_bindings_sys::GGML_TYPE_I32,
            KvCacheType::I64 => llama_cpp_bindings_sys::GGML_TYPE_I64,
            KvCacheType::F64 => llama_cpp_bindings_sys::GGML_TYPE_F64,
            KvCacheType::IQ1_M => llama_cpp_bindings_sys::GGML_TYPE_IQ1_M,
            KvCacheType::BF16 => llama_cpp_bindings_sys::GGML_TYPE_BF16,
            KvCacheType::TQ1_0 => llama_cpp_bindings_sys::GGML_TYPE_TQ1_0,
            KvCacheType::TQ2_0 => llama_cpp_bindings_sys::GGML_TYPE_TQ2_0,
            KvCacheType::MXFP4 => llama_cpp_bindings_sys::GGML_TYPE_MXFP4,
        }
    }
}

impl From<llama_cpp_bindings_sys::ggml_type> for KvCacheType {
    fn from(value: llama_cpp_bindings_sys::ggml_type) -> Self {
        match value {
            x if x == llama_cpp_bindings_sys::GGML_TYPE_F32 => Self::F32,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_F16 => Self::F16,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q4_0 => Self::Q4_0,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q4_1 => Self::Q4_1,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q5_0 => Self::Q5_0,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q5_1 => Self::Q5_1,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q8_0 => Self::Q8_0,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q8_1 => Self::Q8_1,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q2_K => Self::Q2_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q3_K => Self::Q3_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q4_K => Self::Q4_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q5_K => Self::Q5_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q6_K => Self::Q6_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_Q8_K => Self::Q8_K,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ2_XXS => Self::IQ2_XXS,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ2_XS => Self::IQ2_XS,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ3_XXS => Self::IQ3_XXS,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ1_S => Self::IQ1_S,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ4_NL => Self::IQ4_NL,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ3_S => Self::IQ3_S,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ2_S => Self::IQ2_S,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ4_XS => Self::IQ4_XS,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_I8 => Self::I8,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_I16 => Self::I16,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_I32 => Self::I32,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_I64 => Self::I64,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_F64 => Self::F64,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_IQ1_M => Self::IQ1_M,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_BF16 => Self::BF16,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_TQ1_0 => Self::TQ1_0,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_TQ2_0 => Self::TQ2_0,
            x if x == llama_cpp_bindings_sys::GGML_TYPE_MXFP4 => Self::MXFP4,
            _ => Self::Unknown(value),
        }
    }
}

/// A safe wrapper around `llama_context_params`.
///
/// Generally this should be created with [`Default::default()`] and then modified with `with_*` methods.
///
/// # Examples
///
/// ```rust
/// # use std::num::NonZeroU32;
/// use llama_cpp_bindings::context::params::LlamaContextParams;
///
///let ctx_params = LlamaContextParams::default()
///    .with_n_ctx(NonZeroU32::new(2048));
///
/// assert_eq!(ctx_params.n_ctx(), NonZeroU32::new(2048));
/// ```
#[derive(Debug, Clone)]
#[expect(
    missing_docs,
    reason = "field meanings mirror llama.cpp's `llama_context_params` C struct; restating each \
              one inline would risk drift from the upstream spec — the doc-comment on the struct \
              points at the canonical reference"
)]
#[expect(
    clippy::module_name_repetitions,
    reason = "`LlamaContextParams` is the canonical Rust name in the public API; renaming it to \
              `Params` would force `params::Params` at every call site"
)]
pub struct LlamaContextParams {
    pub context_params: llama_cpp_bindings_sys::llama_context_params,
}

/// SAFETY: we do not currently allow setting or reading the pointers that cause this to not be automatically send or sync.
unsafe impl Send for LlamaContextParams {}
unsafe impl Sync for LlamaContextParams {}

impl LlamaContextParams {
    /// Set the side of the context
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use std::num::NonZeroU32;
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// let params = params.with_n_ctx(NonZeroU32::new(2048));
    /// assert_eq!(params.n_ctx(), NonZeroU32::new(2048));
    /// ```
    #[must_use]
    pub fn with_n_ctx(mut self, n_ctx: Option<NonZeroU32>) -> Self {
        self.context_params.n_ctx = n_ctx.map_or(0, NonZeroU32::get);
        self
    }

    /// Get the size of the context.
    ///
    /// [`None`] if the context size is specified by the model and not the context.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.n_ctx(), std::num::NonZeroU32::new(512));
    #[must_use]
    pub const fn n_ctx(&self) -> Option<NonZeroU32> {
        NonZeroU32::new(self.context_params.n_ctx)
    }

    /// Set the `n_batch`
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use std::num::NonZeroU32;
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_n_batch(2048);
    /// assert_eq!(params.n_batch(), 2048);
    /// ```
    #[must_use]
    pub const fn with_n_batch(mut self, n_batch: u32) -> Self {
        self.context_params.n_batch = n_batch;
        self
    }

    /// Get the `n_batch`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// assert_eq!(params.n_batch(), 2048);
    /// ```
    #[must_use]
    pub const fn n_batch(&self) -> u32 {
        self.context_params.n_batch
    }

    /// Set the `n_ubatch`
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use std::num::NonZeroU32;
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_n_ubatch(512);
    /// assert_eq!(params.n_ubatch(), 512);
    /// ```
    #[must_use]
    pub const fn with_n_ubatch(mut self, n_ubatch: u32) -> Self {
        self.context_params.n_ubatch = n_ubatch;
        self
    }

    /// Get the `n_ubatch`
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// assert_eq!(params.n_ubatch(), 512);
    /// ```
    #[must_use]
    pub const fn n_ubatch(&self) -> u32 {
        self.context_params.n_ubatch
    }

    /// Set the flash attention policy using llama.cpp enum
    #[must_use]
    pub const fn with_flash_attention_policy(
        mut self,
        policy: llama_cpp_bindings_sys::llama_flash_attn_type,
    ) -> Self {
        self.context_params.flash_attn_type = policy;
        self
    }

    /// Get the flash attention policy
    #[must_use]
    pub const fn flash_attention_policy(&self) -> llama_cpp_bindings_sys::llama_flash_attn_type {
        self.context_params.flash_attn_type
    }

    /// Set the `offload_kqv` parameter to control offloading KV cache & KQV ops to GPU
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_offload_kqv(false);
    /// assert_eq!(params.offload_kqv(), false);
    /// ```
    #[must_use]
    pub const fn with_offload_kqv(mut self, enabled: bool) -> Self {
        self.context_params.offload_kqv = enabled;
        self
    }

    /// Get the `offload_kqv` parameter
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// assert_eq!(params.offload_kqv(), true);
    /// ```
    #[must_use]
    pub const fn offload_kqv(&self) -> bool {
        self.context_params.offload_kqv
    }

    /// Set the type of rope scaling.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::{LlamaContextParams, RopeScalingType};
    /// let params = LlamaContextParams::default()
    ///     .with_rope_scaling_type(RopeScalingType::Linear);
    /// assert_eq!(params.rope_scaling_type(), RopeScalingType::Linear);
    /// ```
    #[must_use]
    pub fn with_rope_scaling_type(mut self, rope_scaling_type: RopeScalingType) -> Self {
        self.context_params.rope_scaling_type = i32::from(rope_scaling_type);
        self
    }

    /// Get the type of rope scaling.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.rope_scaling_type(), llama_cpp_bindings::context::params::RopeScalingType::Unspecified);
    /// ```
    #[must_use]
    pub fn rope_scaling_type(&self) -> RopeScalingType {
        RopeScalingType::from(self.context_params.rope_scaling_type)
    }

    /// Set the rope frequency base.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///    .with_rope_freq_base(0.5);
    /// assert_eq!(params.rope_freq_base(), 0.5);
    /// ```
    #[must_use]
    pub const fn with_rope_freq_base(mut self, rope_freq_base: f32) -> Self {
        self.context_params.rope_freq_base = rope_freq_base;
        self
    }

    /// Get the rope frequency base.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.rope_freq_base(), 0.0);
    /// ```
    #[must_use]
    pub const fn rope_freq_base(&self) -> f32 {
        self.context_params.rope_freq_base
    }

    /// Set the rope frequency scale.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///   .with_rope_freq_scale(0.5);
    /// assert_eq!(params.rope_freq_scale(), 0.5);
    /// ```
    #[must_use]
    pub const fn with_rope_freq_scale(mut self, rope_freq_scale: f32) -> Self {
        self.context_params.rope_freq_scale = rope_freq_scale;
        self
    }

    /// Get the rope frequency scale.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.rope_freq_scale(), 0.0);
    /// ```
    #[must_use]
    pub const fn rope_freq_scale(&self) -> f32 {
        self.context_params.rope_freq_scale
    }

    /// Get the number of threads.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.n_threads(), 4);
    /// ```
    #[must_use]
    pub const fn n_threads(&self) -> i32 {
        self.context_params.n_threads
    }

    /// Get the number of threads allocated for batches.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.n_threads_batch(), 4);
    /// ```
    #[must_use]
    pub const fn n_threads_batch(&self) -> i32 {
        self.context_params.n_threads_batch
    }

    /// Set the number of threads.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///    .with_n_threads(8);
    /// assert_eq!(params.n_threads(), 8);
    /// ```
    #[must_use]
    pub const fn with_n_threads(mut self, n_threads: i32) -> Self {
        self.context_params.n_threads = n_threads;
        self
    }

    /// Set the number of threads allocated for batches.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///    .with_n_threads_batch(8);
    /// assert_eq!(params.n_threads_batch(), 8);
    /// ```
    #[must_use]
    pub const fn with_n_threads_batch(mut self, n_threads: i32) -> Self {
        self.context_params.n_threads_batch = n_threads;
        self
    }

    /// Check whether embeddings are enabled
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert!(!params.embeddings());
    /// ```
    #[must_use]
    pub const fn embeddings(&self) -> bool {
        self.context_params.embeddings
    }

    /// Enable the use of embeddings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///    .with_embeddings(true);
    /// assert!(params.embeddings());
    /// ```
    #[must_use]
    pub const fn with_embeddings(mut self, embedding: bool) -> Self {
        self.context_params.embeddings = embedding;
        self
    }

    /// Set the evaluation callback.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// extern "C" fn cb_eval_fn(
    ///     t: *mut llama_cpp_bindings_sys::ggml_tensor,
    ///     ask: bool,
    ///     user_data: *mut std::ffi::c_void,
    /// ) -> bool {
    ///     false
    /// }
    ///
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default().with_cb_eval(Some(cb_eval_fn));
    /// ```
    #[must_use]
    pub fn with_cb_eval(
        mut self,
        cb_eval: llama_cpp_bindings_sys::ggml_backend_sched_eval_callback,
    ) -> Self {
        self.context_params.cb_eval = cb_eval;
        self
    }

    /// Set the evaluation callback user data.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// let user_data = std::ptr::null_mut();
    /// let params = params.with_cb_eval_user_data(user_data);
    /// ```
    #[must_use]
    pub const fn with_cb_eval_user_data(
        mut self,
        cb_eval_user_data: *mut std::ffi::c_void,
    ) -> Self {
        self.context_params.cb_eval_user_data = cb_eval_user_data;
        self
    }

    /// Set the type of pooling.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::{LlamaContextParams, LlamaPoolingType};
    /// let params = LlamaContextParams::default()
    ///     .with_pooling_type(LlamaPoolingType::Last);
    /// assert_eq!(params.pooling_type(), LlamaPoolingType::Last);
    /// ```
    #[must_use]
    pub fn with_pooling_type(mut self, pooling_type: LlamaPoolingType) -> Self {
        self.context_params.pooling_type = i32::from(pooling_type);
        self
    }

    /// Get the type of pooling.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.pooling_type(), llama_cpp_bindings::context::params::LlamaPoolingType::Unspecified);
    /// ```
    #[must_use]
    pub fn pooling_type(&self) -> LlamaPoolingType {
        LlamaPoolingType::from(self.context_params.pooling_type)
    }

    /// Set whether to use full sliding window attention
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_swa_full(false);
    /// assert_eq!(params.swa_full(), false);
    /// ```
    #[must_use]
    pub const fn with_swa_full(mut self, enabled: bool) -> Self {
        self.context_params.swa_full = enabled;
        self
    }

    /// Get whether full sliding window attention is enabled
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// assert_eq!(params.swa_full(), true);
    /// ```
    #[must_use]
    pub const fn swa_full(&self) -> bool {
        self.context_params.swa_full
    }

    /// Set the max number of sequences (i.e. distinct states for recurrent models)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_n_seq_max(64);
    /// assert_eq!(params.n_seq_max(), 64);
    /// ```
    #[must_use]
    pub const fn with_n_seq_max(mut self, n_seq_max: u32) -> Self {
        self.context_params.n_seq_max = n_seq_max;
        self
    }

    /// Get the max number of sequences (i.e. distinct states for recurrent models)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default();
    /// assert_eq!(params.n_seq_max(), 1);
    /// ```
    #[must_use]
    pub const fn n_seq_max(&self) -> u32 {
        self.context_params.n_seq_max
    }
    /// Set the KV cache data type for K
    /// use `llama_cpp_bindings::context::params::{LlamaContextParams`, `KvCacheType`};
    /// let params = `LlamaContextParams::default().with_type_k(KvCacheType::Q4_0)`;
    /// `assert_eq!(params.type_k()`, `KvCacheType::Q4_0`);
    /// ```
    #[must_use]
    pub fn with_type_k(mut self, type_k: KvCacheType) -> Self {
        self.context_params.type_k = type_k.into();
        self
    }

    /// Get the KV cache data type for K
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// let _ = params.type_k();
    /// ```
    #[must_use]
    pub fn type_k(&self) -> KvCacheType {
        KvCacheType::from(self.context_params.type_k)
    }

    /// Set the KV cache data type for V
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::{LlamaContextParams, KvCacheType};
    /// let params = LlamaContextParams::default().with_type_v(KvCacheType::Q4_1);
    /// assert_eq!(params.type_v(), KvCacheType::Q4_1);
    /// ```
    #[must_use]
    pub fn with_type_v(mut self, type_v: KvCacheType) -> Self {
        self.context_params.type_v = type_v.into();
        self
    }

    /// Get the KV cache data type for V
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// let _ = params.type_v();
    /// ```
    #[must_use]
    pub fn type_v(&self) -> KvCacheType {
        KvCacheType::from(self.context_params.type_v)
    }

    /// Set the attention type
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::{LlamaContextParams, LlamaAttentionType};
    /// let params = LlamaContextParams::default()
    ///     .with_attention_type(LlamaAttentionType::NonCausal);
    /// assert_eq!(params.attention_type(), LlamaAttentionType::NonCausal);
    /// ```
    #[must_use]
    pub fn with_attention_type(mut self, attention_type: LlamaAttentionType) -> Self {
        self.context_params.attention_type = i32::from(attention_type);
        self
    }

    /// Get the attention type
    ///
    /// # Examples
    ///
    /// ```rust
    /// let params = llama_cpp_bindings::context::params::LlamaContextParams::default();
    /// assert_eq!(params.attention_type(), llama_cpp_bindings::context::params::LlamaAttentionType::Unspecified);
    /// ```
    #[must_use]
    pub fn attention_type(&self) -> LlamaAttentionType {
        LlamaAttentionType::from(self.context_params.attention_type)
    }

    /// Set the `YaRN` extrapolation factor
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_yarn_ext_factor(1.0);
    /// assert!((params.yarn_ext_factor() - 1.0).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    pub const fn with_yarn_ext_factor(mut self, yarn_ext_factor: f32) -> Self {
        self.context_params.yarn_ext_factor = yarn_ext_factor;
        self
    }

    /// Get the `YaRN` extrapolation factor
    #[must_use]
    pub const fn yarn_ext_factor(&self) -> f32 {
        self.context_params.yarn_ext_factor
    }

    /// Set the `YaRN` attention factor
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_yarn_attn_factor(2.0);
    /// assert!((params.yarn_attn_factor() - 2.0).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    pub const fn with_yarn_attn_factor(mut self, yarn_attn_factor: f32) -> Self {
        self.context_params.yarn_attn_factor = yarn_attn_factor;
        self
    }

    /// Get the `YaRN` attention factor
    #[must_use]
    pub const fn yarn_attn_factor(&self) -> f32 {
        self.context_params.yarn_attn_factor
    }

    /// Set the `YaRN` low correction dim
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_yarn_beta_fast(32.0);
    /// assert!((params.yarn_beta_fast() - 32.0).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    pub const fn with_yarn_beta_fast(mut self, yarn_beta_fast: f32) -> Self {
        self.context_params.yarn_beta_fast = yarn_beta_fast;
        self
    }

    /// Get the `YaRN` low correction dim
    #[must_use]
    pub const fn yarn_beta_fast(&self) -> f32 {
        self.context_params.yarn_beta_fast
    }

    /// Set the `YaRN` high correction dim
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_yarn_beta_slow(1.0);
    /// assert!((params.yarn_beta_slow() - 1.0).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    pub const fn with_yarn_beta_slow(mut self, yarn_beta_slow: f32) -> Self {
        self.context_params.yarn_beta_slow = yarn_beta_slow;
        self
    }

    /// Get the `YaRN` high correction dim
    #[must_use]
    pub const fn yarn_beta_slow(&self) -> f32 {
        self.context_params.yarn_beta_slow
    }

    /// Set the `YaRN` original context size
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_yarn_orig_ctx(4096);
    /// assert_eq!(params.yarn_orig_ctx(), 4096);
    /// ```
    #[must_use]
    pub const fn with_yarn_orig_ctx(mut self, yarn_orig_ctx: u32) -> Self {
        self.context_params.yarn_orig_ctx = yarn_orig_ctx;
        self
    }

    /// Get the `YaRN` original context size
    #[must_use]
    pub const fn yarn_orig_ctx(&self) -> u32 {
        self.context_params.yarn_orig_ctx
    }

    /// Set the KV cache defragmentation threshold
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_defrag_thold(0.1);
    /// assert!((params.defrag_thold() - 0.1).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    pub const fn with_defrag_thold(mut self, defrag_thold: f32) -> Self {
        self.context_params.defrag_thold = defrag_thold;
        self
    }

    /// Get the KV cache defragmentation threshold
    #[must_use]
    pub const fn defrag_thold(&self) -> f32 {
        self.context_params.defrag_thold
    }

    /// Set whether performance timings are disabled
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_no_perf(true);
    /// assert!(params.no_perf());
    /// ```
    #[must_use]
    pub const fn with_no_perf(mut self, no_perf: bool) -> Self {
        self.context_params.no_perf = no_perf;
        self
    }

    /// Get whether performance timings are disabled
    #[must_use]
    pub const fn no_perf(&self) -> bool {
        self.context_params.no_perf
    }

    /// Set whether to offload ops to GPU
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_op_offload(false);
    /// assert!(!params.op_offload());
    /// ```
    #[must_use]
    pub const fn with_op_offload(mut self, op_offload: bool) -> Self {
        self.context_params.op_offload = op_offload;
        self
    }

    /// Get whether ops are offloaded to GPU
    #[must_use]
    pub const fn op_offload(&self) -> bool {
        self.context_params.op_offload
    }

    /// Set whether to use a unified KV cache buffer across input sequences
    ///
    /// # Examples
    ///
    /// ```rust
    /// use llama_cpp_bindings::context::params::LlamaContextParams;
    /// let params = LlamaContextParams::default()
    ///     .with_kv_unified(true);
    /// assert!(params.kv_unified());
    /// ```
    #[must_use]
    pub const fn with_kv_unified(mut self, kv_unified: bool) -> Self {
        self.context_params.kv_unified = kv_unified;
        self
    }

    /// Get whether a unified KV cache buffer is used across input sequences
    #[must_use]
    pub const fn kv_unified(&self) -> bool {
        self.context_params.kv_unified
    }
}

/// Default parameters for `LlamaContext`. (as defined in llama.cpp by `llama_context_default_params`)
/// ```
/// # use std::num::NonZeroU32;
/// use llama_cpp_bindings::context::params::{LlamaContextParams, RopeScalingType};
/// let params = LlamaContextParams::default();
/// assert_eq!(params.n_ctx(), NonZeroU32::new(512), "n_ctx should be 512");
/// assert_eq!(params.rope_scaling_type(), RopeScalingType::Unspecified);
/// ```
impl Default for LlamaContextParams {
    fn default() -> Self {
        let context_params = unsafe { llama_cpp_bindings_sys::llama_context_default_params() };
        Self { context_params }
    }
}

#[cfg(test)]
mod tests {
    use super::{KvCacheType, LlamaAttentionType, LlamaPoolingType, RopeScalingType};

    #[test]
    fn rope_scaling_type_unknown_defaults_to_unspecified() {
        assert_eq!(RopeScalingType::from(99), RopeScalingType::Unspecified);
        assert_eq!(RopeScalingType::from(-100), RopeScalingType::Unspecified);
    }

    #[test]
    fn pooling_type_unknown_defaults_to_unspecified() {
        assert_eq!(LlamaPoolingType::from(99), LlamaPoolingType::Unspecified);
        assert_eq!(LlamaPoolingType::from(-50), LlamaPoolingType::Unspecified);
    }

    #[test]
    fn kv_cache_type_unknown_preserves_raw_value() {
        let unknown_raw: llama_cpp_bindings_sys::ggml_type = 99999;
        let cache_type = KvCacheType::from(unknown_raw);

        assert_eq!(cache_type, KvCacheType::Unknown(99999));

        let back: llama_cpp_bindings_sys::ggml_type = cache_type.into();

        assert_eq!(back, 99999);
    }

    #[test]
    fn default_params_have_expected_values() {
        let params = super::LlamaContextParams::default();

        assert_eq!(params.n_ctx(), std::num::NonZeroU32::new(512));
        assert_eq!(params.n_batch(), 2048);
        assert_eq!(params.n_ubatch(), 512);
        assert_eq!(params.rope_scaling_type(), RopeScalingType::Unspecified);
        assert_eq!(params.pooling_type(), LlamaPoolingType::Unspecified);
    }

    #[test]
    fn with_n_ctx_sets_value() {
        let params =
            super::LlamaContextParams::default().with_n_ctx(std::num::NonZeroU32::new(2048));

        assert_eq!(params.n_ctx(), std::num::NonZeroU32::new(2048));
    }

    #[test]
    fn with_n_ctx_none_sets_zero() {
        let params = super::LlamaContextParams::default().with_n_ctx(None);

        assert_eq!(params.n_ctx(), None);
    }

    #[test]
    fn with_n_batch_sets_value() {
        let params = super::LlamaContextParams::default().with_n_batch(4096);

        assert_eq!(params.n_batch(), 4096);
    }

    #[test]
    fn with_n_ubatch_sets_value() {
        let params = super::LlamaContextParams::default().with_n_ubatch(1024);

        assert_eq!(params.n_ubatch(), 1024);
    }

    #[test]
    fn with_n_seq_max_sets_value() {
        let params = super::LlamaContextParams::default().with_n_seq_max(64);

        assert_eq!(params.n_seq_max(), 64);
    }

    #[test]
    fn with_embeddings_enables() {
        let params = super::LlamaContextParams::default().with_embeddings(true);

        assert!(params.embeddings());
    }

    #[test]
    fn with_embeddings_disables() {
        let params = super::LlamaContextParams::default().with_embeddings(false);

        assert!(!params.embeddings());
    }

    #[test]
    fn with_offload_kqv_disables() {
        let params = super::LlamaContextParams::default().with_offload_kqv(false);

        assert!(!params.offload_kqv());
    }

    #[test]
    fn with_offload_kqv_enables() {
        let params = super::LlamaContextParams::default().with_offload_kqv(true);

        assert!(params.offload_kqv());
    }

    #[test]
    fn with_swa_full_disables() {
        let params = super::LlamaContextParams::default().with_swa_full(false);

        assert!(!params.swa_full());
    }

    #[test]
    fn with_swa_full_enables() {
        let params = super::LlamaContextParams::default().with_swa_full(true);

        assert!(params.swa_full());
    }

    #[test]
    fn with_rope_scaling_type_linear() {
        let params =
            super::LlamaContextParams::default().with_rope_scaling_type(RopeScalingType::Linear);

        assert_eq!(params.rope_scaling_type(), RopeScalingType::Linear);
    }

    #[test]
    fn with_rope_scaling_type_yarn() {
        let params =
            super::LlamaContextParams::default().with_rope_scaling_type(RopeScalingType::Yarn);

        assert_eq!(params.rope_scaling_type(), RopeScalingType::Yarn);
    }

    #[test]
    fn with_rope_scaling_type_none() {
        let params =
            super::LlamaContextParams::default().with_rope_scaling_type(RopeScalingType::None);

        assert_eq!(params.rope_scaling_type(), RopeScalingType::None);
    }

    #[test]
    fn with_rope_freq_base_sets_value() {
        let params = super::LlamaContextParams::default().with_rope_freq_base(10000.0);

        assert!((params.rope_freq_base() - 10000.0).abs() < f32::EPSILON);
    }

    #[test]
    fn with_rope_freq_scale_sets_value() {
        let params = super::LlamaContextParams::default().with_rope_freq_scale(0.5);

        assert!((params.rope_freq_scale() - 0.5).abs() < f32::EPSILON);
    }

    #[test]
    fn with_n_threads_sets_value() {
        let params = super::LlamaContextParams::default().with_n_threads(16);

        assert_eq!(params.n_threads(), 16);
    }

    #[test]
    fn with_n_threads_batch_sets_value() {
        let params = super::LlamaContextParams::default().with_n_threads_batch(16);

        assert_eq!(params.n_threads_batch(), 16);
    }

    #[test]
    fn with_pooling_type_mean() {
        let params = super::LlamaContextParams::default().with_pooling_type(LlamaPoolingType::Mean);

        assert_eq!(params.pooling_type(), LlamaPoolingType::Mean);
    }

    #[test]
    fn with_pooling_type_cls() {
        let params = super::LlamaContextParams::default().with_pooling_type(LlamaPoolingType::Cls);

        assert_eq!(params.pooling_type(), LlamaPoolingType::Cls);
    }

    #[test]
    fn with_pooling_type_last() {
        let params = super::LlamaContextParams::default().with_pooling_type(LlamaPoolingType::Last);

        assert_eq!(params.pooling_type(), LlamaPoolingType::Last);
    }

    #[test]
    fn with_pooling_type_rank() {
        let params = super::LlamaContextParams::default().with_pooling_type(LlamaPoolingType::Rank);

        assert_eq!(params.pooling_type(), LlamaPoolingType::Rank);
    }

    #[test]
    fn with_pooling_type_none() {
        let params = super::LlamaContextParams::default().with_pooling_type(LlamaPoolingType::None);

        assert_eq!(params.pooling_type(), LlamaPoolingType::None);
    }

    #[test]
    fn with_type_k_sets_value() {
        let params = super::LlamaContextParams::default().with_type_k(KvCacheType::Q4_0);

        assert_eq!(params.type_k(), KvCacheType::Q4_0);
    }

    #[test]
    fn with_type_v_sets_value() {
        let params = super::LlamaContextParams::default().with_type_v(KvCacheType::Q4_1);

        assert_eq!(params.type_v(), KvCacheType::Q4_1);
    }

    #[test]
    fn with_flash_attention_policy_sets_value() {
        let params = super::LlamaContextParams::default()
            .with_flash_attention_policy(llama_cpp_bindings_sys::LLAMA_FLASH_ATTN_TYPE_ENABLED);

        assert_eq!(
            params.flash_attention_policy(),
            llama_cpp_bindings_sys::LLAMA_FLASH_ATTN_TYPE_ENABLED
        );
    }

    #[test]
    fn builder_chaining_preserves_all_values() {
        let params = super::LlamaContextParams::default()
            .with_n_ctx(std::num::NonZeroU32::new(1024))
            .with_n_batch(4096)
            .with_n_ubatch(256)
            .with_n_threads(8)
            .with_n_threads_batch(12)
            .with_embeddings(true)
            .with_offload_kqv(false)
            .with_rope_scaling_type(RopeScalingType::Yarn)
            .with_rope_freq_base(5000.0)
            .with_rope_freq_scale(0.25);

        assert_eq!(params.n_ctx(), std::num::NonZeroU32::new(1024));
        assert_eq!(params.n_batch(), 4096);
        assert_eq!(params.n_ubatch(), 256);
        assert_eq!(params.n_threads(), 8);
        assert_eq!(params.n_threads_batch(), 12);
        assert!(params.embeddings());
        assert!(!params.offload_kqv());
        assert_eq!(params.rope_scaling_type(), RopeScalingType::Yarn);
        assert!((params.rope_freq_base() - 5000.0).abs() < f32::EPSILON);
        assert!((params.rope_freq_scale() - 0.25).abs() < f32::EPSILON);
    }

    #[test]
    fn rope_scaling_type_roundtrip_all_variants() {
        for (raw, expected) in [
            (-1, RopeScalingType::Unspecified),
            (0, RopeScalingType::None),
            (1, RopeScalingType::Linear),
            (2, RopeScalingType::Yarn),
        ] {
            let from_raw = RopeScalingType::from(raw);
            assert_eq!(from_raw, expected);

            let back_to_raw: i32 = from_raw.into();
            assert_eq!(back_to_raw, raw);
        }
    }

    #[test]
    fn pooling_type_roundtrip_all_variants() {
        for (raw, expected) in [
            (-1, LlamaPoolingType::Unspecified),
            (0, LlamaPoolingType::None),
            (1, LlamaPoolingType::Mean),
            (2, LlamaPoolingType::Cls),
            (3, LlamaPoolingType::Last),
            (4, LlamaPoolingType::Rank),
        ] {
            let from_raw = LlamaPoolingType::from(raw);
            assert_eq!(from_raw, expected);

            let back_to_raw: i32 = from_raw.into();
            assert_eq!(back_to_raw, raw);
        }
    }

    #[test]
    fn kv_cache_type_all_known_variants_roundtrip() {
        let all_variants = [
            KvCacheType::F32,
            KvCacheType::F16,
            KvCacheType::Q4_0,
            KvCacheType::Q4_1,
            KvCacheType::Q5_0,
            KvCacheType::Q5_1,
            KvCacheType::Q8_0,
            KvCacheType::Q8_1,
            KvCacheType::Q2_K,
            KvCacheType::Q3_K,
            KvCacheType::Q4_K,
            KvCacheType::Q5_K,
            KvCacheType::Q6_K,
            KvCacheType::Q8_K,
            KvCacheType::IQ2_XXS,
            KvCacheType::IQ2_XS,
            KvCacheType::IQ3_XXS,
            KvCacheType::IQ1_S,
            KvCacheType::IQ4_NL,
            KvCacheType::IQ3_S,
            KvCacheType::IQ2_S,
            KvCacheType::IQ4_XS,
            KvCacheType::I8,
            KvCacheType::I16,
            KvCacheType::I32,
            KvCacheType::I64,
            KvCacheType::F64,
            KvCacheType::IQ1_M,
            KvCacheType::BF16,
            KvCacheType::TQ1_0,
            KvCacheType::TQ2_0,
            KvCacheType::MXFP4,
        ];

        for variant in all_variants {
            let ggml_type: llama_cpp_bindings_sys::ggml_type = variant.into();
            let back = KvCacheType::from(ggml_type);

            assert_eq!(back, variant);
        }
    }

    #[test]
    fn with_cb_eval_sets_callback() {
        extern "C" fn test_cb_eval(
            _tensor: *mut llama_cpp_bindings_sys::ggml_tensor,
            _ask: bool,
            _user_data: *mut std::ffi::c_void,
        ) -> bool {
            false
        }

        let result = test_cb_eval(std::ptr::null_mut(), false, std::ptr::null_mut());

        assert!(!result);

        let params = super::LlamaContextParams::default().with_cb_eval(Some(test_cb_eval));

        assert!(params.context_params.cb_eval.is_some());
    }

    #[test]
    fn with_cb_eval_user_data_sets_pointer() {
        let mut value: i32 = 42;
        let user_data = (&raw mut value).cast::<std::ffi::c_void>();
        let params = super::LlamaContextParams::default().with_cb_eval_user_data(user_data);

        assert_eq!(params.context_params.cb_eval_user_data, user_data);
    }

    #[test]
    fn with_flash_attention_policy_disabled() {
        let params = super::LlamaContextParams::default()
            .with_flash_attention_policy(llama_cpp_bindings_sys::LLAMA_FLASH_ATTN_TYPE_DISABLED);

        assert_eq!(
            params.flash_attention_policy(),
            llama_cpp_bindings_sys::LLAMA_FLASH_ATTN_TYPE_DISABLED
        );
    }

    #[test]
    fn attention_type_unknown_defaults_to_unspecified() {
        assert_eq!(
            LlamaAttentionType::from(99),
            LlamaAttentionType::Unspecified
        );
        assert_eq!(
            LlamaAttentionType::from(-50),
            LlamaAttentionType::Unspecified
        );
    }

    #[test]
    fn attention_type_roundtrip_all_variants() {
        for (raw, expected) in [
            (-1, LlamaAttentionType::Unspecified),
            (0, LlamaAttentionType::Causal),
            (1, LlamaAttentionType::NonCausal),
        ] {
            let from_raw = LlamaAttentionType::from(raw);
            assert_eq!(from_raw, expected);

            let back_to_raw: i32 = from_raw.into();
            assert_eq!(back_to_raw, raw);
        }
    }

    #[test]
    fn with_attention_type_causal() {
        let params =
            super::LlamaContextParams::default().with_attention_type(LlamaAttentionType::Causal);

        assert_eq!(params.attention_type(), LlamaAttentionType::Causal);
    }

    #[test]
    fn with_attention_type_non_causal() {
        let params =
            super::LlamaContextParams::default().with_attention_type(LlamaAttentionType::NonCausal);

        assert_eq!(params.attention_type(), LlamaAttentionType::NonCausal);
    }

    #[test]
    fn with_yarn_ext_factor_sets_value() {
        let params = super::LlamaContextParams::default().with_yarn_ext_factor(1.5);

        assert!((params.yarn_ext_factor() - 1.5).abs() < f32::EPSILON);
    }

    #[test]
    fn with_yarn_attn_factor_sets_value() {
        let params = super::LlamaContextParams::default().with_yarn_attn_factor(2.0);

        assert!((params.yarn_attn_factor() - 2.0).abs() < f32::EPSILON);
    }

    #[test]
    fn with_yarn_beta_fast_sets_value() {
        let params = super::LlamaContextParams::default().with_yarn_beta_fast(32.0);

        assert!((params.yarn_beta_fast() - 32.0).abs() < f32::EPSILON);
    }

    #[test]
    fn with_yarn_beta_slow_sets_value() {
        let params = super::LlamaContextParams::default().with_yarn_beta_slow(1.0);

        assert!((params.yarn_beta_slow() - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn with_yarn_orig_ctx_sets_value() {
        let params = super::LlamaContextParams::default().with_yarn_orig_ctx(4096);

        assert_eq!(params.yarn_orig_ctx(), 4096);
    }

    #[test]
    fn with_defrag_thold_sets_value() {
        let params = super::LlamaContextParams::default().with_defrag_thold(0.1);

        assert!((params.defrag_thold() - 0.1).abs() < f32::EPSILON);
    }

    #[test]
    fn with_no_perf_enables() {
        let params = super::LlamaContextParams::default().with_no_perf(true);

        assert!(params.no_perf());
    }

    #[test]
    fn with_no_perf_disables() {
        let params = super::LlamaContextParams::default().with_no_perf(false);

        assert!(!params.no_perf());
    }

    #[test]
    fn with_op_offload_enables() {
        let params = super::LlamaContextParams::default().with_op_offload(true);

        assert!(params.op_offload());
    }

    #[test]
    fn with_op_offload_disables() {
        let params = super::LlamaContextParams::default().with_op_offload(false);

        assert!(!params.op_offload());
    }

    #[test]
    fn with_kv_unified_enables() {
        let params = super::LlamaContextParams::default().with_kv_unified(true);

        assert!(params.kv_unified());
    }

    #[test]
    fn with_kv_unified_disables() {
        let params = super::LlamaContextParams::default().with_kv_unified(false);

        assert!(!params.kv_unified());
    }
}