Views
No views yet
https://huggingface.co/HuggingFaceM4/siglip-so400m-14-384-flash-attn2 -> just don't specify the patch_attention_mask1import torch
2from modeling_siglip import SiglipVisionModel
3
4DEVICE = torch.device("cuda:0")
5PATCH_SIZE = 14
6
7pixel_values = torch.randn(2, 3, 28, 42, dtype=torch.bfloat16, device=DEVICE)
8pixel_attention_mask = [
9 [
10 [1] * 14 + [1] * 14 + [1] * 14,
11 [1] * 14 + [1] * 14 + [1] * 14,
12 [1] * 14 + [1] * 14 + [1] * 14,
13 [1] * 14 + [1] * 14 + [1] * 14,
14 [1] * 14 + [1] * 14 + [1] * 14,
15 [1] * 14 + [1] * 14 + [1] * 14,
16 [1] * 14 + [1] * 14 + [1] * 14,
17 [1] * 14 + [1] * 14 + [1] * 14,
18 [1] * 14 + [1] * 14 + [1] * 14,
19 [1] * 14 + [1] * 14 + [1] * 14,
20 [1] * 14 + [1] * 14 + [1] * 14,
21 [1] * 14 + [1] * 14 + [1] * 14,
22 [1] * 14 + [1] * 14 + [1] * 14,
23 [1] * 14 + [1] * 14 + [1] * 14,
24
25 [0] * 14 + [0] * 14 + [0] * 14,
26 [0] * 14 + [0] * 14 + [0] * 14,
27 [0] * 14 + [0] * 14 + [0] * 14,
28 [0] * 14 + [0] * 14 + [0] * 14,
29 [0] * 14 + [0] * 14 + [0] * 14,
30 [0] * 14 + [0] * 14 + [0] * 14,
31 [0] * 14 + [0] * 14 + [0] * 14,
32 [0] * 14 + [0] * 14 + [0] * 14,
33 [0] * 14 + [0] * 14 + [0] * 14,
34 [0] * 14 + [0] * 14 + [0] * 14,
35 [0] * 14 + [0] * 14 + [0] * 14,
36 [0] * 14 + [0] * 14 + [0] * 14,
37 [0] * 14 + [0] * 14 + [0] * 14,
38 [0] * 14 + [0] * 14 + [0] * 14,
39 ],
40 [
41 [1] * 14 + [1] * 14 + [0] * 14,
42 [1] * 14 + [1] * 14 + [0] * 14,
43 [1] * 14 + [1] * 14 + [0] * 14,
44 [1] * 14 + [1] * 14 + [0] * 14,
45 [1] * 14 + [1] * 14 + [0] * 14,
46 [1] * 14 + [1] * 14 + [0] * 14,
47 [1] * 14 + [1] * 14 + [0] * 14,
48 [1] * 14 + [1] * 14 + [0] * 14,
49 [1] * 14 + [1] * 14 + [0] * 14,
50 [1] * 14 + [1] * 14 + [0] * 14,
51 [1] * 14 + [1] * 14 + [0] * 14,
52 [1] * 14 + [1] * 14 + [0] * 14,
53 [1] * 14 + [1] * 14 + [0] * 14,
54 [1] * 14 + [1] * 14 + [0] * 14,
55
56 [1] * 14 + [1] * 14 + [0] * 14,
57 [1] * 14 + [1] * 14 + [0] * 14,
58 [1] * 14 + [1] * 14 + [0] * 14,
59 [1] * 14 + [1] * 14 + [0] * 14,
60 [1] * 14 + [1] * 14 + [0] * 14,
61 [1] * 14 + [1] * 14 + [0] * 14,
62 [1] * 14 + [1] * 14 + [0] * 14,
63 [1] * 14 + [1] * 14 + [0] * 14,
64 [1] * 14 + [1] * 14 + [0] * 14,
65 [1] * 14 + [1] * 14 + [0] * 14,
66 [1] * 14 + [1] * 14 + [0] * 14,
67 [1] * 14 + [1] * 14 + [0] * 14,
68 [1] * 14 + [1] * 14 + [0] * 14,
69 [1] * 14 + [1] * 14 + [0] * 14,
70 ],
71]
72pixel_attention_mask = torch.tensor(pixel_attention_mask, dtype=torch.bool, device=DEVICE)
73patches_subgrid = pixel_attention_mask.unfold(
74 dimension=1, size=PATCH_SIZE, step=PATCH_SIZE
75).unfold(dimension=2, size=PATCH_SIZE, step=PATCH_SIZE)
76patch_attention_mask = (patches_subgrid.sum(dim=(-1, -2)) > 0).bool()
77
78model = SiglipVisionModel.from_pretrained("HuggingFaceM4/siglip-so400m-14-980-flash-attn2-navit", _flash_attn_2_enabled=True)
79model.train()
80model.vision_model.to(DEVICE, dtype=torch.bfloat16)
81
82output = model.vision_model(pixel_values=pixel_values, patch_attention_mask=patch_attention_mask)