-
Notifications
You must be signed in to change notification settings - Fork 7
/
file_architecture.go
680 lines (612 loc) · 24.9 KB
/
file_architecture.go
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
package gguf_parser
import (
"regexp"
"strings"
)
// Types for the architecture metadata of a GGUF file.
type (
// GGUFArchitecture represents the architecture metadata of a GGUF file.
GGUFArchitecture struct {
/* Basic */
// Type describes the type of the file,
// default is "model".
Type string `json:"type"`
// Architecture describes what architecture this model implements.
//
// All lowercase ASCII.
Architecture string `json:"architecture"`
// MaximumContextLength(n_ctx_train) is the maximum context length of the model.
//
// For most architectures, this is the hard limit on the length of the input.
// Architectures, like RWKV,
// that are not reliant on transformer-style attention may be able to handle larger inputs,
// but this is not guaranteed.
MaximumContextLength uint64 `json:"maximumContextLength,omitempty"`
// EmbeddingLength(n_embd) is the length of the embedding layer.
EmbeddingLength uint64 `json:"embeddingLength,omitempty"`
// BlockCount(n_layer) is the number of blocks of attention and feed-forward layers,
// i.e. the bulk of the LLM.
// This does not include the input or embedding layers.
BlockCount uint64 `json:"blockCount,omitempty"`
// FeedForwardLength(n_ff) is the length of the feed-forward layer.
FeedForwardLength uint64 `json:"feedForwardLength,omitempty"`
// ExpertFeedForwardLength(expert_feed_forward_length) is the length of the feed-forward layer in the expert model.
ExpertFeedForwardLength uint64 `json:"expertFeedForwardLength,omitempty"`
// ExpertSharedFeedForwardLength(expert_shared_feed_forward_length) is the length of the shared feed-forward layer in the expert model.
ExpertSharedFeedForwardLength uint64 `json:"expertSharedFeedForwardLength,omitempty"`
// ExpertCount(n_expert) is the number of experts in MoE models.
ExpertCount uint32 `json:"expertCount,omitempty"`
// ExpertUsedCount(n_expert_used) is the number of experts used during each token evaluation in MoE models.
ExpertUsedCount uint32 `json:"expertUsedCount,omitempty"`
// AttentionHeadCount(n_head) is the number of attention heads.
AttentionHeadCount uint64 `json:"attentionHeadCount,omitempty"`
// AttentionHeadCountKV(n_head_kv) is the number of attention heads per group used in Grouped-Query-Attention.
//
// If not provided or equal to AttentionHeadCount,
// the model does not use Grouped-Query-Attention.
AttentionHeadCountKV uint64 `json:"attentionHeadCountKV,omitempty"`
// AttentionMaxALiBIBias is the maximum bias to use for ALiBI.
AttentionMaxALiBIBias float32 `json:"attentionMaxALiBIBias,omitempty"`
// AttentionClampKQV describes a value `C`,
// which is used to clamp the values of the `Q`, `K` and `V` tensors between `[-C, C]`.
AttentionClampKQV float32 `json:"attentionClampKQV,omitempty"`
// AttentionLayerNormEpsilon is the epsilon value used in the LayerNorm(Layer Normalization).
AttentionLayerNormEpsilon float32 `json:"attentionLayerNormEpsilon,omitempty"`
// AttentionLayerNormRMSEpsilon is the epsilon value used in the RMSNorm(root Mean Square Layer Normalization),
// which is a simplification of the original LayerNorm.
AttentionLayerNormRMSEpsilon float32 `json:"attentionLayerNormRMSEpsilon,omitempty"`
// AttentionKeyLength(n_embd_head_k) is the size of a key head.
//
// Defaults to `EmbeddingLength / AttentionHeadCount`.
AttentionKeyLength uint32 `json:"attentionKeyLength,omitempty"`
// AttentionValueLength(n_embd_head_v) is the size of a value head.
//
// Defaults to `EmbeddingLength / AttentionHeadCount`.
AttentionValueLength uint32 `json:"attentionValueLength,omitempty"`
// AttentionCausal is true if the attention is causal.
AttentionCausal bool `json:"attentionCausal,omitempty"`
// RoPEDimensionCount is the number of dimensions in the RoPE(Rotary Positional Encoding).
RoPEDimensionCount uint64 `json:"ropeDimensionCount,omitempty"`
// RoPEFrequencyBase is the base frequency of the RoPE.
RoPEFrequencyBase float32 `json:"ropeFrequencyBase,omitempty"`
// RoPEFrequencyScale is the frequency scale of the RoPE.
RoPEScalingType string `json:"ropeScalingType,omitempty"`
// RoPEScalingFactor is the scaling factor of the RoPE.
RoPEScalingFactor float32 `json:"ropeScalingFactor,omitempty"`
// RoPEScalingOriginalContextLength is the original context length of the RoPE scaling.
RoPEScalingOriginalContextLength uint64 `json:"ropeScalingOriginalContextLength,omitempty"`
// RoPEScalingFinetuned is true if the RoPE scaling is fine-tuned.
RoPEScalingFinetuned bool `json:"ropeScalingFinetuned,omitempty"`
// SSMConvolutionKernel is the size of the convolution kernel used in the SSM(Selective State Space Model).
SSMConvolutionKernel uint32 `json:"ssmConvolutionKernel,omitempty"`
// SSMInnerSize is the embedding size of the state in SSM.
SSMInnerSize uint32 `json:"ssmInnerSize,omitempty"`
// SSMStateSize is the size of the recurrent state in SSM.
SSMStateSize uint32 `json:"ssmStateSize,omitempty"`
// SSMTimeStepRank is the rank of the time steps in SSM.
SSMTimeStepRank uint32 `json:"ssmTimeStepRank,omitempty"`
// VocabularyLength is the size of the vocabulary.
//
// VocabularyLength is the same as the tokenizer's token size.
VocabularyLength uint64 `json:"vocabularyLength,omitempty"`
/* Appendix */
// EmbeddingGGQA is the GQA of the embedding layer.
EmbeddingGQA uint64 `json:"embeddingGQA,omitempty"`
// EmbeddingKeyGQA is the number of key GQA in the embedding layer.
EmbeddingKeyGQA uint64 `json:"embeddingKeyGQA,omitempty"`
// EmbeddingValueGQA is the number of value GQA in the embedding layer.
EmbeddingValueGQA uint64 `json:"embeddingValueGQA,omitempty"`
// ClipHasTextEncoder indicates whether the clip model has text encoder or not.
//
// Only used when Architecture is "clip".
ClipHasTextEncoder bool `json:"clipHasTextEncoder,omitempty"`
// ClipHasVisionEncoder indicates whether the clip model has vision encoder or not.
//
// Only used when Architecture is "clip".
ClipHasVisionEncoder bool `json:"clipHasVisionEncoder,omitempty"`
// ClipProjectorType is the type of the projector used in the clip model.
//
// Only used when Architecture is "clip".
ClipProjectorType string `json:"clipProjectorType,omitempty"`
// AdapterType is the type of the adapter.
AdapterType string `json:"adapterType,omitempty"`
// AdapterLoRAAlpha is the alpha value of the LoRA adapter.
//
// Only used when AdapterType is "lora".
AdapterLoRAAlpha float32 `json:"adapterLoRAAlpha,omitempty"`
// AdapterControlVectorLayerCount is the number of layers in the control vector.
//
// Only used when Architecture is "control_vector".
AdapterControlVectorLayerCount uint32 `json:"adapterControlVectorLayerCount,omitempty"`
// DiffusionArchitecture is the actual architecture of the diffusion model.
//
// Only used when Architecture is "diffusion".
DiffusionArchitecture string `json:"diffusionArchitecture,omitempty"`
// DiffusionTransformer indicates whether the diffusion model is a diffusion transformer or not.
//
DiffusionTransformer bool `json:"diffusionTransformer,omitempty"`
// DiffusionConditioners is the list of diffusion conditioners.
//
// Only used when Architecture is "diffusion".
DiffusionConditioners GGUFArchitectureDiffusionConditioners `json:"diffusionConditioners,omitempty"`
// DiffusionAutoencoder represents the autoencoder of the diffusion model.
//
// Only used when Architecture is "diffusion".
DiffusionAutoencoder *GGUFArchitectureDiffusionAutoencoder `json:"diffusionAutoencoder,omitempty"`
}
// GGUFArchitectureDiffusionConditioners is the list of GGUFArchitectureDiffusionConditioner.
GGUFArchitectureDiffusionConditioners []GGUFArchitectureDiffusionConditioner
// GGUFArchitectureDiffusionConditioner represents the conditioner metadata of the diffusion architecture.
GGUFArchitectureDiffusionConditioner struct {
// Architecture is the architecture of the diffusion conditioner.
Architecture string `json:"architecture"`
// FileType describes the type of the majority of the tensors in the GGUF file.
FileType GGUFFileType `json:"fileType"`
}
// GGUFArchitectureDiffusionAutoencoder represents the autoencoder metadata of the diffusion architecture.
GGUFArchitectureDiffusionAutoencoder struct {
// Architecture is the architecture of the diffusion autoencoder.
//
// Currently, only "VAE" is supported.
Architecture string `json:"architecture"`
// FileType describes the type of the majority of the tensors in the GGUF file.
FileType GGUFFileType `json:"fileType"`
}
)
// DiffusionHasConditioners returns true if the diffusion model has conditioners.
func (ga GGUFArchitecture) DiffusionHasConditioners() bool {
return len(ga.DiffusionConditioners) > 0
}
// DiffusionHasAutoencoder returns true if the diffusion model has an autoencoder.
func (ga GGUFArchitecture) DiffusionHasAutoencoder() bool {
return ga.DiffusionAutoencoder != nil && ga.DiffusionAutoencoder.Architecture != ""
}
func (gacs GGUFArchitectureDiffusionConditioners) String() string {
var sb strings.Builder
for i, gac := range gacs {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(gac.String())
}
return sb.String()
}
func (gac GGUFArchitectureDiffusionConditioner) String() string {
return gac.Architecture + " (" + gac.FileType.String() + ")"
}
func (gaa GGUFArchitectureDiffusionAutoencoder) String() string {
return gaa.Architecture + " (" + gaa.FileType.String() + ")"
}
// Architecture returns the architecture metadata of the GGUF file.
func (gf *GGUFFile) Architecture() (ga GGUFArchitecture) {
if gf.TensorInfos.Match(regexp.MustCompile(`^model\.diffusion_model\..*`)) ||
gf.TensorInfos.Match(regexp.MustCompile(`^double_blocks\..*`)) {
return gf.diffuserArchitecture()
}
var (
generalTypeKey = "general.type"
generalArchitectureKey = "general.architecture"
controlVectorModelHintKey = "controlvector.model_hint"
)
m, _ := gf.Header.MetadataKV.Index([]string{
generalTypeKey,
generalArchitectureKey,
controlVectorModelHintKey,
})
typ, arch := "model", "llama" // nolint: goconst
{
if v, ok := m[generalTypeKey]; ok {
typ = v.ValueString()
}
if v, ok := m[generalArchitectureKey]; ok {
arch = v.ValueString()
}
}
switch {
case arch == "clip":
return gf.clipArchitecture()
case arch == "controlvector":
arch = "llama"
if v, ok := m[controlVectorModelHintKey]; ok {
arch = v.ValueString()
}
return gf.adapterArchitecture(arch)
case typ == "adapter":
return gf.adapterArchitecture(arch)
}
return gf.transformerArchitecture(arch)
}
func (gf *GGUFFile) diffuserArchitecture() (ga GGUFArchitecture) {
const (
// Diffusion
sdKey = "model.diffusion_model.output_blocks.11.1.transformer_blocks.0.attn2.to_v.weight" // SD 1.x/2.x
sdXlKey = "model.diffusion_model.output_blocks.5.1.transformer_blocks.1.attn1.to_v.weight" // SD XL
sdXlRefinerKey = "model.diffusion_model.output_blocks.8.1.transformer_blocks.1.attn1.to_v.weight" // SD XL Refiner
sd3Key = "model.diffusion_model.joint_blocks.23.x_block.attn.proj.weight" // SD 3.x
fluxKey = "model.diffusion_model.double_blocks.0.txt_attn.proj.weight" // FLUX.1
fluxKey2 = "double_blocks.0.txt_attn.proj.weight"
// Conditioner
openAiClipVitL14Key = "cond_stage_model.transformer.text_model.encoder.layers.11.self_attn.k_proj.weight" // OpenAI CLIP ViT-L/14
openClipVitH14Key = "cond_stage_model.transformer.text_model.encoder.layers.22.self_attn.k_proj.weight" // OpenCLIP ViT-H/14
openClipVitG14Key = "cond_stage_model.1.transformer.text_model.encoder.layers.31.self_attn.k_proj.weight" // OpenCLIP ViT-G/14
t5xxlKey = "cond_stage_model.1.transformer.encoder.block.23.layer.0.SelfAttention.k.weight" // Google T5-xxl
t5xxlKey2 = "cond_stage_model.2.transformer.encoder.block.23.layer.0.SelfAttention.k.weight"
)
tis, _ := gf.TensorInfos.Index([]string{
sdKey,
sdXlKey,
sdXlRefinerKey,
sd3Key,
fluxKey,
fluxKey2,
openAiClipVitL14Key,
openClipVitH14Key,
openClipVitG14Key,
t5xxlKey,
t5xxlKey2,
})
ga.Type = "model"
ga.Architecture = "diffusion"
if ti, ok := tis[sdKey]; ok {
ga.DiffusionArchitecture = "Stable Diffusion 1.x"
if ti.Dimensions[0] == 1024 {
ga.DiffusionArchitecture = "Stable Diffusion 2.x"
}
} else if _, ok := tis[sdXlKey]; ok {
ga.DiffusionArchitecture = "Stable Diffusion XL"
if _, ok = tis[sdXlRefinerKey]; ok {
ga.DiffusionArchitecture = "Stable Diffusion XL Refiner"
}
} else if _, ok := tis[sd3Key]; ok {
ga.DiffusionArchitecture = "Stable Diffusion 3.x"
ga.DiffusionTransformer = true
}
if _, ok := tis[fluxKey]; ok {
ga.DiffusionArchitecture = "FLUX.1"
ga.DiffusionTransformer = true
} else if _, ok := tis[fluxKey2]; ok {
ga.DiffusionArchitecture = "FLUX.1"
ga.DiffusionTransformer = true
}
if ti, ok := tis[openAiClipVitL14Key]; ok {
cond := GGUFArchitectureDiffusionConditioner{
Architecture: "OpenAI CLIP ViT-L/14",
FileType: ti.GetFileType(),
}
if ti, ok = tis[openClipVitH14Key]; ok {
cond = GGUFArchitectureDiffusionConditioner{
Architecture: "OpenCLIP ViT-H/14",
FileType: ti.GetFileType(),
}
}
ga.DiffusionConditioners = append(ga.DiffusionConditioners, cond)
}
if ti, ok := tis[openClipVitG14Key]; ok {
ga.DiffusionConditioners = append(ga.DiffusionConditioners, GGUFArchitectureDiffusionConditioner{
Architecture: "OpenCLIP ViT-G/14",
FileType: ti.GetFileType(),
})
}
if ti, ok := tis[t5xxlKey]; ok {
ga.DiffusionConditioners = append(ga.DiffusionConditioners, GGUFArchitectureDiffusionConditioner{
Architecture: "Google T5-xxl",
FileType: ti.GetFileType(),
})
} else if ti, ok = tis[t5xxlKey2]; ok {
ga.DiffusionConditioners = append(ga.DiffusionConditioners, GGUFArchitectureDiffusionConditioner{
Architecture: "Google T5-xxl",
FileType: ti.GetFileType(),
})
}
if tis := gf.TensorInfos.Search(regexp.MustCompile(`^first_stage_model\..*`)); len(tis) != 0 {
ga.DiffusionAutoencoder = &GGUFArchitectureDiffusionAutoencoder{
Architecture: ga.DiffusionArchitecture + " VAE",
FileType: GGUFTensorInfos(tis).GetFileType(),
}
}
return ga
}
func (gf *GGUFFile) clipArchitecture() (ga GGUFArchitecture) {
var (
hasTextEncoderKey = "clip.has_text_encoder"
hasVisionEncoderKey = "clip.has_vision_encoder"
projectorTypeKey = "clip.projector_type"
textEmbeddingLengthKey = "clip.text.embedding_length"
textBlockCountKey = "clip.text.block_count"
textFeedForwardLengthKey = "clip.text.feed_forward_length"
textAttentionHeadCountKey = "clip.text.attention.head_count"
textAttentionLayerNormRMSEpsilonKey = "clip.text.attention.layer_norm_epsilon"
visionEmbeddingLengthKey = "clip.vision.embedding_length"
visionBlockCountKey = "clip.vision.block_count"
visionFeedForwardLengthKey = "clip.vision.feed_forward_length"
visionAttentionHeadCountKey = "clip.vision.attention.head_count"
visionAttentionLayerNormRMSEpsilonKey = "clip.vision.attention.layer_norm_epsilon"
)
ga.Type = "projector"
ga.Architecture = "clip"
m, _ := gf.Header.MetadataKV.Index([]string{
hasTextEncoderKey,
hasVisionEncoderKey,
projectorTypeKey,
textEmbeddingLengthKey,
textBlockCountKey,
textFeedForwardLengthKey,
textAttentionHeadCountKey,
textAttentionLayerNormRMSEpsilonKey,
visionEmbeddingLengthKey,
visionBlockCountKey,
visionFeedForwardLengthKey,
visionAttentionHeadCountKey,
visionAttentionLayerNormRMSEpsilonKey,
})
if v, ok := m[hasTextEncoderKey]; ok {
ga.ClipHasTextEncoder = v.ValueBool()
}
if v, ok := m[hasVisionEncoderKey]; ok {
ga.ClipHasVisionEncoder = v.ValueBool()
}
if v, ok := m[projectorTypeKey]; ok {
ga.ClipProjectorType = v.ValueString()
} else {
ga.ClipProjectorType = "mlp"
}
if v, ok := m[textEmbeddingLengthKey]; ok {
ga.EmbeddingLength = ValueNumeric[uint64](v)
}
if v, ok := m[textBlockCountKey]; ok {
ga.BlockCount = ValueNumeric[uint64](v)
}
if v, ok := m[textFeedForwardLengthKey]; ok {
ga.FeedForwardLength = ValueNumeric[uint64](v)
}
if v, ok := m[textAttentionHeadCountKey]; ok {
ga.AttentionHeadCount = ValueNumeric[uint64](v)
}
if v, ok := m[textAttentionLayerNormRMSEpsilonKey]; ok {
ga.AttentionLayerNormRMSEpsilon = ValueNumeric[float32](v)
}
if v, ok := m[visionEmbeddingLengthKey]; ok {
ga.EmbeddingLength = ValueNumeric[uint64](v)
}
if v, ok := m[visionBlockCountKey]; ok {
ga.BlockCount = ValueNumeric[uint64](v)
}
if v, ok := m[visionFeedForwardLengthKey]; ok {
ga.FeedForwardLength = ValueNumeric[uint64](v)
}
if v, ok := m[visionAttentionHeadCountKey]; ok {
ga.AttentionHeadCount = ValueNumeric[uint64](v)
}
if v, ok := m[visionAttentionLayerNormRMSEpsilonKey]; ok {
ga.AttentionLayerNormRMSEpsilon = ValueNumeric[float32](v)
}
ga.AttentionHeadCountKV = ga.AttentionHeadCount
{
if ga.AttentionHeadCountKV > 0 {
ga.EmbeddingGQA = ga.AttentionHeadCount / ga.AttentionHeadCountKV
}
if ga.AttentionHeadCount > 0 {
ga.EmbeddingKeyGQA = uint64(ga.AttentionKeyLength) * ga.AttentionHeadCountKV
ga.EmbeddingValueGQA = uint64(ga.AttentionValueLength) * ga.AttentionHeadCountKV
}
if ga.Architecture == "mamba" {
ga.EmbeddingKeyGQA = uint64((ga.SSMConvolutionKernel - 1) * ga.SSMInnerSize)
ga.EmbeddingValueGQA = uint64(ga.SSMStateSize * ga.SSMInnerSize)
}
}
return ga
}
func (gf *GGUFFile) adapterArchitecture(arch string) (ga GGUFArchitecture) {
var (
typeKey = "adapter.type"
loraAlphaKey = "adapter.lora.alpha"
controlVectorLayerCountKey = "adapter.control_vector.layer_count"
controlVectorLayerCountKey2 = "control_vector.layer_count"
)
ga.Type = "adapter"
ga.Architecture = arch
m, _ := gf.Header.MetadataKV.Index([]string{
typeKey,
loraAlphaKey,
controlVectorLayerCountKey,
controlVectorLayerCountKey2,
})
if v, ok := m[typeKey]; ok {
ga.AdapterType = v.ValueString()
}
if v, ok := m[loraAlphaKey]; ok {
ga.AdapterLoRAAlpha = ValueNumeric[float32](v)
}
if v, ok := m[controlVectorLayerCountKey]; ok {
ga.AdapterControlVectorLayerCount = ValueNumeric[uint32](v)
} else if v, ok := m[controlVectorLayerCountKey2]; ok {
ga.AdapterControlVectorLayerCount = ValueNumeric[uint32](v)
}
return ga
}
func (gf *GGUFFile) transformerArchitecture(arch string) (ga GGUFArchitecture) {
var (
contextLengthKey = arch + ".context_length"
embeddingLengthKey = arch + ".embedding_length"
blockCountKey = arch + ".block_count"
feedForwardLengthKey = arch + ".feed_forward_length"
expertFeedForwardLengthKey = arch + ".expert_feed_forward_length"
expertSharedFeedForwardLengthKey = arch + ".expert_shared_feed_forward_length"
expertCountKey = arch + ".expert_count"
expertUsedCountKey = arch + ".expert_used_count"
attentionHeadCountKey = arch + ".attention.head_count"
attentionHeadCountKVKey = arch + ".attention.head_count_kv"
attentionMaxALiBIBiasKey = arch + ".attention.max_alibi_bias"
attentionMaxALiBIBiasKey2 = arch + ".attention.alibi_bias_max"
attentionClampKQVKey = arch + ".attention.clamp_kqv"
attentionClampKQVKey2 = arch + ".attention.clip_kqv"
attentionLayerNormEpsilonKey = arch + ".attention.layer_norm_epsilon"
attentionLayerNormRMSEpsilonKey = arch + ".attention.layer_norm_rms_epsilon"
attentionKeyLengthKey = arch + ".attention.key_length"
attentionValueLengthKey = arch + ".attention.value_length"
attentionCausalKey = arch + ".attention.causal"
ropeDimensionCountKey = arch + ".rope.dimension_count"
ropeFrequencyBaseKey = arch + ".rope.freq_base"
ropeScaleLinearKey = arch + ".rope.scale_linear"
ropeScalingTypeKey = arch + ".rope.scaling.type"
ropeScalingFactorKey = arch + ".rope.scaling.factor"
ropeScalingOriginalContextKey = arch + ".rope.scaling.original_context_length" // uint32 maybe
ropeScalingFinetunedKey = arch + ".rope.scaling.finetuned"
ssmConvolutionKernelKey = arch + ".ssm.conv_kernel"
ssmInnerSizeKey = arch + ".ssm.inner_size"
ssmStateSizeKey = arch + ".ssm.state_size"
ssmTimeStepRankKey = arch + ".ssm.time_step_rank"
vocabularyLengthKey = arch + ".vocab_size"
tokenizerGGMLTokensKey = "tokenizer.ggml.tokens"
)
ga.Type = "model"
ga.Architecture = arch
m, _ := gf.Header.MetadataKV.Index([]string{
contextLengthKey,
embeddingLengthKey,
blockCountKey,
feedForwardLengthKey,
expertCountKey,
expertUsedCountKey,
attentionHeadCountKey,
attentionHeadCountKVKey,
attentionMaxALiBIBiasKey,
attentionMaxALiBIBiasKey2,
attentionClampKQVKey,
attentionClampKQVKey2,
attentionLayerNormEpsilonKey,
attentionLayerNormRMSEpsilonKey,
attentionKeyLengthKey,
attentionValueLengthKey,
attentionCausalKey,
ropeDimensionCountKey,
ropeFrequencyBaseKey,
ropeScaleLinearKey,
ropeScalingTypeKey,
ropeScalingFactorKey,
ropeScalingOriginalContextKey,
ropeScalingFinetunedKey,
ssmConvolutionKernelKey,
ssmInnerSizeKey,
ssmStateSizeKey,
ssmTimeStepRankKey,
vocabularyLengthKey,
tokenizerGGMLTokensKey,
})
if v, ok := m[contextLengthKey]; ok {
ga.MaximumContextLength = ValueNumeric[uint64](v)
}
if v, ok := m[embeddingLengthKey]; ok {
ga.EmbeddingLength = ValueNumeric[uint64](v)
}
if v, ok := m[blockCountKey]; ok {
ga.BlockCount = ValueNumeric[uint64](v)
}
if v, ok := m[feedForwardLengthKey]; ok {
ga.FeedForwardLength = ValueNumeric[uint64](v)
}
if v, ok := m[expertCountKey]; ok {
ga.ExpertCount = ValueNumeric[uint32](v)
}
if v, ok := m[expertUsedCountKey]; ok {
ga.ExpertUsedCount = ValueNumeric[uint32](v)
}
if v, ok := m[expertFeedForwardLengthKey]; ok {
ga.ExpertFeedForwardLength = ValueNumeric[uint64](v)
}
if v, ok := m[expertSharedFeedForwardLengthKey]; ok {
ga.ExpertSharedFeedForwardLength = ValueNumeric[uint64](v)
}
if v, ok := m[attentionHeadCountKey]; ok {
ga.AttentionHeadCount = ValueNumeric[uint64](v)
}
if v, ok := m[attentionHeadCountKVKey]; ok {
ga.AttentionHeadCountKV = ValueNumeric[uint64](v)
} else {
ga.AttentionHeadCountKV = ga.AttentionHeadCount
}
if v, ok := m[attentionMaxALiBIBiasKey]; ok {
ga.AttentionMaxALiBIBias = ValueNumeric[float32](v)
} else if v, ok := m[attentionMaxALiBIBiasKey2]; ok {
ga.AttentionMaxALiBIBias = ValueNumeric[float32](v)
}
if v, ok := m[attentionClampKQVKey]; ok {
ga.AttentionClampKQV = ValueNumeric[float32](v)
} else if v, ok := m[attentionClampKQVKey2]; ok {
ga.AttentionClampKQV = ValueNumeric[float32](v)
}
if v, ok := m[attentionLayerNormEpsilonKey]; ok {
ga.AttentionLayerNormEpsilon = ValueNumeric[float32](v)
}
if v, ok := m[attentionLayerNormRMSEpsilonKey]; ok {
ga.AttentionLayerNormRMSEpsilon = ValueNumeric[float32](v)
}
if v, ok := m[attentionKeyLengthKey]; ok {
ga.AttentionKeyLength = ValueNumeric[uint32](v)
} else if ga.AttentionHeadCount != 0 {
ga.AttentionKeyLength = uint32(ga.EmbeddingLength / ga.AttentionHeadCount)
}
if v, ok := m[attentionValueLengthKey]; ok {
ga.AttentionValueLength = ValueNumeric[uint32](v)
} else if ga.AttentionHeadCount != 0 {
ga.AttentionValueLength = uint32(ga.EmbeddingLength / ga.AttentionHeadCount)
}
if v, ok := m[attentionCausalKey]; ok {
ga.AttentionCausal = v.ValueBool()
} else {
ga.AttentionCausal = true
}
if v, ok := m[ropeDimensionCountKey]; ok {
ga.RoPEDimensionCount = ValueNumeric[uint64](v)
}
if v, ok := m[ropeFrequencyBaseKey]; ok {
ga.RoPEFrequencyBase = ValueNumeric[float32](v)
}
if v, ok := m[ropeScaleLinearKey]; ok {
ga.RoPEScalingType = "linear"
ga.RoPEScalingFactor = ValueNumeric[float32](v)
}
if v, ok := m[ropeScalingTypeKey]; ok {
ga.RoPEScalingType = v.ValueString()
}
if v, ok := m[ropeScalingFactorKey]; ok {
ga.RoPEScalingFactor = ValueNumeric[float32](v)
}
if v, ok := m[ropeScalingOriginalContextKey]; ok {
ga.RoPEScalingOriginalContextLength = ValueNumeric[uint64](v)
}
if v, ok := m[ropeScalingFinetunedKey]; ok {
ga.RoPEScalingFinetuned = v.ValueBool()
}
if v, ok := m[ssmConvolutionKernelKey]; ok {
ga.SSMConvolutionKernel = ValueNumeric[uint32](v)
}
if v, ok := m[ssmInnerSizeKey]; ok {
ga.SSMInnerSize = ValueNumeric[uint32](v)
}
if v, ok := m[ssmStateSizeKey]; ok {
ga.SSMStateSize = ValueNumeric[uint32](v)
}
if v, ok := m[ssmTimeStepRankKey]; ok {
ga.SSMTimeStepRank = ValueNumeric[uint32](v)
}
if v, ok := m[vocabularyLengthKey]; ok {
ga.VocabularyLength = ValueNumeric[uint64](v)
} else if v, ok := m[tokenizerGGMLTokensKey]; ok {
ga.VocabularyLength = v.ValueArray().Len
}
{
if ga.AttentionHeadCountKV > 0 {
ga.EmbeddingGQA = ga.AttentionHeadCount / ga.AttentionHeadCountKV
}
if ga.AttentionHeadCount > 0 {
ga.EmbeddingKeyGQA = uint64(ga.AttentionKeyLength) * ga.AttentionHeadCountKV
ga.EmbeddingValueGQA = uint64(ga.AttentionValueLength) * ga.AttentionHeadCountKV
}
if ga.Architecture == "mamba" {
ga.EmbeddingKeyGQA = uint64((ga.SSMConvolutionKernel - 1) * ga.SSMInnerSize)
ga.EmbeddingValueGQA = uint64(ga.SSMStateSize * ga.SSMInnerSize)
}
}
return ga
}