Skip to content

Commit 7ec259a

Browse files
committed
fix(clip): 支持 f32 norm,交换 up/down
Signed-off-by: YdrMaster <ydrml@hotmail.com>
1 parent 88d3c23 commit 7ec259a

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

models/clip/common/src/compute.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ where
148148
let Args { raw, pos } = args;
149149
let ClipMeta {
150150
dt,
151+
dt_norm,
151152
nblk,
152153
nh,
153154
nkvh,
@@ -267,7 +268,7 @@ where
267268

268269
let weights = &self.weights.weights;
269270
let q0 = Tensor::new(dt, &[dq, d]).map(|_| weights.resampler_q(queue));
270-
let ln_qkv = Tensor::new(dt, &[d]);
271+
let ln_qkv = Tensor::new(dt_norm, &[d]);
271272

272273
let q = Tensor::new(dt, q0.shape());
273274
let kv = Tensor::new(dt, &[np, d]);

models/clip/common/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod ext {
2323
#[derive(Clone, Debug)]
2424
pub struct ClipMeta {
2525
pub dt: DigitLayout,
26+
pub dt_norm: DigitLayout,
2627

2728
pub d_patch: usize,
2829
pub d_image: usize,
@@ -66,7 +67,7 @@ impl ClipMeta {
6667

6768
pub fn norm(&self) -> Tensor<usize> {
6869
let &Self { d, .. } = self;
69-
Tensor::new(self.dt, &[d])
70+
Tensor::new(self.dt_norm, &[d])
7071
}
7172

7273
pub fn attn_qkv_w(&self) -> Tensor<usize> {

models/clip/common/src/projector/resampler.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ impl<'a> Storage<&'a [u8]> {
4141
#[rustfmt::skip]
4242
pub fn from_gguf(gguf: &GGufModel<'a>) -> Self {
4343
Self {
44-
wkv : tensor![gguf => "resampler.kv.weight" ].data ,
45-
q : tensor![gguf => "resampler.query" ].data ,
46-
ln_q : [tensor![gguf => "resampler.ln_q.weight" ].data ,
47-
tensor![gguf => "resampler.ln_q.bias" ].data],
48-
ln_kv : [tensor![gguf => "resampler.ln_kv.weight" ].data ,
49-
tensor![gguf => "resampler.ln_kv.bias" ].data],
50-
attn_q: [tensor![gguf => "resampler.attn.q.weight"].data ,
51-
tensor![gguf => "resampler.attn.q.bias" ].data],
52-
attn_k: [tensor![gguf => "resampler.attn.k.weight"].data ,
53-
tensor![gguf => "resampler.attn.k.bias" ].data],
54-
attn_v: [tensor![gguf => "resampler.attn.v.weight"].data ,
55-
tensor![gguf => "resampler.attn.v.bias" ].data],
56-
attn_o: [tensor![gguf => "resampler.attn.o.weight"].data ,
57-
tensor![gguf => "resampler.attn.o.bias" ].data],
44+
wkv : tensor![gguf => "resampler.kv.weight" ].data ,
45+
q : tensor![gguf => "resampler.query" ].data ,
46+
ln_q : [tensor![gguf => "resampler.ln_q.weight" ].data ,
47+
tensor![gguf => "resampler.ln_q.bias" ].data],
48+
ln_kv : [tensor![gguf => "resampler.ln_kv.weight" ].data ,
49+
tensor![gguf => "resampler.ln_kv.bias" ].data],
50+
attn_q: [tensor![gguf => "resampler.attn.q.weight" ].data ,
51+
tensor![gguf => "resampler.attn.q.bias" ].data],
52+
attn_k: [tensor![gguf => "resampler.attn.k.weight" ].data ,
53+
tensor![gguf => "resampler.attn.k.bias" ].data],
54+
attn_v: [tensor![gguf => "resampler.attn.v.weight" ].data ,
55+
tensor![gguf => "resampler.attn.v.bias" ].data],
56+
attn_o: [tensor![gguf => "resampler.attn.out.weight"].data ,
57+
tensor![gguf => "resampler.attn.out.bias" ].data],
5858
}
5959
}
6060
}

models/clip/common/src/storage.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ pub struct BlkStorage<T> {
3636
impl<'a> Storage<&'a [u8]> {
3737
pub fn from_gguf(gguf: &GGufModel<'a>) -> Self {
3838
let pos_embd = &gguf.tensors["v.position_embd.weight"];
39+
let ln1_0 = &gguf.tensors["v.blk.0.ln1.weight"];
3940

4041
let d = meta![gguf => (usize) "clip.vision.embedding_length"];
4142
let nh = meta![gguf => (usize) "clip.vision.attention.head_count"];
4243

4344
#[rustfmt::skip]
4445
let meta = ClipMeta {
4546
dt : pos_embd.ty,
47+
dt_norm: ln1_0.ty,
4648
d_patch: meta![gguf => (usize) "clip.vision.patch_size"],
4749
d_image: meta![gguf => (usize) "clip.vision.image_size"],
4850

@@ -70,10 +72,10 @@ impl<'a> Storage<&'a [u8]> {
7072

7173
ffn_norm_w: tensor![gguf => format!("v.blk.{i}.ln2.weight" )].data,
7274
ffn_norm_b: tensor![gguf => format!("v.blk.{i}.ln2.bias" )].data,
73-
ffn_up_w: tensor![gguf => format!("v.blk.{i}.ffn_up.weight" )].data,
74-
ffn_up_b: tensor![gguf => format!("v.blk.{i}.ffn_up.bias" )].data,
75-
ffn_down_w: tensor![gguf => format!("v.blk.{i}.ffn_down.weight")].data,
76-
ffn_down_b: tensor![gguf => format!("v.blk.{i}.ffn_down.bias" )].data,
75+
ffn_up_w: tensor![gguf => format!("v.blk.{i}.ffn_down.weight")].data,
76+
ffn_up_b: tensor![gguf => format!("v.blk.{i}.ffn_down.bias" )].data,
77+
ffn_down_w: tensor![gguf => format!("v.blk.{i}.ffn_up.weight" )].data,
78+
ffn_down_b: tensor![gguf => format!("v.blk.{i}.ffn_up.bias" )].data,
7779
})
7880
.collect();
7981

@@ -100,7 +102,7 @@ fn get_rgb(gguf: &GGufModel, key: &str) -> [f32; 3] {
100102
let mut arr = gguf.get_f32_arr(key).unwrap();
101103
let mut ans = [0.0; 3];
102104
for x in ans.iter_mut() {
103-
*x = arr.next().unwrap().unwrap();
105+
*x = arr.next().unwrap().unwrap()
104106
}
105107
ans
106108
}

0 commit comments

Comments
 (0)