Reza2kn commited on
Commit
7c8a83b
·
verified ·
1 Parent(s): cfecb93

Upload INT4-G128 quant for facebook/sapiens2-normal-1b

Browse files
README.md ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: sapiens2-license
4
+ license_link: https://github.com/facebookresearch/sapiens2/blob/main/LICENSE.md
5
+ base_model: facebook/sapiens2-normal-1b
6
+ base_model_relation: quantized
7
+ library_name: sapiens
8
+ tags:
9
+ - sapiens
10
+ - sapiens2
11
+ - int4
12
+ - quantized
13
+ - safetensors
14
+ ---
15
+
16
+ # sapiens2-normal-1b INT4-G128
17
+
18
+ Packed 4-bit derivative of [facebook/sapiens2-normal-1b](https://huggingface.co/facebook/sapiens2-normal-1b).
19
+
20
+ This artifact uses symmetric per-group INT4 packing with group size 128 for large floating-point weight tensors. Norms, biases, positional/rope tensors, and small tensors are kept in their source dtype. It is a storage/runtime-loader quant for the current official Sapiens2 code path, not an AWQ/GGUF/NVFP4 LLM artifact.
21
+
22
+ ## Files
23
+
24
+ - `facebook__sapiens2-normal-1b-int4-g128.safetensors`: packed INT4 safetensors artifact.
25
+ - `load_sapiens2_int4.py`: loader that reconstructs a PyTorch state dict for the official Sapiens2 model code.
26
+ - `config.json` and `preprocessor_config.json`: copied from the source repo.
27
+ - `quantization_report.json`: build report.
28
+
29
+ ## Quantization Report
30
+
31
+ - Source revision: `c4c2641bd89c585a3973e88ea5cca5d1aa2498ed`
32
+ - Group size: `128`
33
+ - Source bytes: `6157337292`
34
+ - Artifact bytes: `797079156`
35
+ - Compression ratio: `7.7249x`
36
+ - Tensors: `704`
37
+ - Quantized tensors: `250`
38
+ - Max tensor MAE during dequant smoke: `0.00477664`
39
+
40
+
41
+ ## Fidelity Validation
42
+
43
+ The packed INT4 artifact was dequantized back to floating-point tensors and compared against the source checkpoint.
44
+
45
+ - Validation gate: global floating-tensor similarity >= `90.00%`
46
+ - Result: `PASS`
47
+ - Global floating-tensor similarity: `99.340750%`
48
+ - Minimum large-tensor cosine: `0.987384617`
49
+
50
+
51
+ ## Loading
52
+
53
+ ```python
54
+ from load_sapiens2_int4 import load_state_dict
55
+
56
+ state_dict = load_state_dict("facebook__sapiens2-normal-1b-int4-g128.safetensors", device="cpu")
57
+ # Then instantiate the matching official Sapiens2 architecture and load:
58
+ # model.load_state_dict(state_dict, strict=True)
59
+ ```
60
+
61
+ ## Limitations
62
+
63
+ This is a verified packed-weight artifact with a dequantizing loader. It does not claim native INT4 CUDA kernels for Sapiens2 yet. Runtime speedups require a Sapiens2-specific kernel/export path and should be benchmarked separately.
config.json ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Sapiens2ForNormalEstimation"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "drop_path_rate": 0.0,
7
+ "flip_pairs": null,
8
+ "head_config": {
9
+ "_name_or_path": "",
10
+ "architectures": null,
11
+ "chunk_size_feed_forward": 0,
12
+ "conv_kernel_size": 1,
13
+ "conv_kernel_sizes": [
14
+ 3,
15
+ 3,
16
+ 3
17
+ ],
18
+ "conv_out_channels": [
19
+ 64,
20
+ 32,
21
+ 16
22
+ ],
23
+ "dtype": null,
24
+ "id2label": {
25
+ "0": "LABEL_0",
26
+ "1": "LABEL_1"
27
+ },
28
+ "is_encoder_decoder": false,
29
+ "label2id": {
30
+ "LABEL_0": 0,
31
+ "LABEL_1": 1
32
+ },
33
+ "model_type": "sapiens2_head",
34
+ "output_attentions": false,
35
+ "output_hidden_states": false,
36
+ "problem_type": null,
37
+ "return_dict": true,
38
+ "scale_conv_kernel_size": 1,
39
+ "scale_conv_kernel_sizes": null,
40
+ "scale_conv_out_channels": null,
41
+ "scale_final_hidden_sizes": null,
42
+ "scale_final_input_size": null,
43
+ "upsample_kernel_size": 4,
44
+ "upsample_kernel_sizes": [
45
+ 3,
46
+ 3,
47
+ 3,
48
+ 3
49
+ ],
50
+ "upsample_out_channels": [
51
+ 768,
52
+ 512,
53
+ 256,
54
+ 128
55
+ ],
56
+ "use_pixel_shuffle": true
57
+ },
58
+ "hidden_act": "silu",
59
+ "hidden_size": 1536,
60
+ "id2label": {
61
+ "0": "LABEL_0",
62
+ "1": "LABEL_1",
63
+ "2": "LABEL_2"
64
+ },
65
+ "image_size": [
66
+ 1024,
67
+ 768
68
+ ],
69
+ "initializer_range": 0.02,
70
+ "intermediate_size": 6144,
71
+ "key_bias": true,
72
+ "label2id": {
73
+ "LABEL_0": 0,
74
+ "LABEL_1": 1,
75
+ "LABEL_2": 2
76
+ },
77
+ "layerscale_value": 1.0,
78
+ "mlp_bias": true,
79
+ "model_type": "sapiens2",
80
+ "normalize_backbone_outputs": true,
81
+ "num_attention_heads": 24,
82
+ "num_channels": 3,
83
+ "num_first_full_attention_layers": 8,
84
+ "num_hidden_layers": 40,
85
+ "num_key_value_attention_heads": 12,
86
+ "num_key_value_heads_per_layer": [
87
+ 24,
88
+ 24,
89
+ 24,
90
+ 24,
91
+ 24,
92
+ 24,
93
+ 24,
94
+ 24,
95
+ 12,
96
+ 12,
97
+ 12,
98
+ 12,
99
+ 12,
100
+ 12,
101
+ 12,
102
+ 12,
103
+ 12,
104
+ 12,
105
+ 12,
106
+ 12,
107
+ 12,
108
+ 12,
109
+ 12,
110
+ 12,
111
+ 12,
112
+ 12,
113
+ 12,
114
+ 12,
115
+ 12,
116
+ 12,
117
+ 12,
118
+ 12,
119
+ 24,
120
+ 24,
121
+ 24,
122
+ 24,
123
+ 24,
124
+ 24,
125
+ 24,
126
+ 24
127
+ ],
128
+ "num_last_full_attention_layers": 8,
129
+ "num_register_tokens": 8,
130
+ "out_features": [
131
+ "stage40"
132
+ ],
133
+ "out_indices": [
134
+ 40
135
+ ],
136
+ "patch_size": 16,
137
+ "pos_embed_jitter": null,
138
+ "pos_embed_rescale": 2.0,
139
+ "pos_embed_shift": null,
140
+ "proj_bias": true,
141
+ "query_bias": true,
142
+ "reshape_hidden_states": true,
143
+ "rms_norm_eps": 1e-06,
144
+ "rope_theta": 100.0,
145
+ "semantic_loss_ignore_index": 255,
146
+ "stage_names": [
147
+ "stem",
148
+ "stage1",
149
+ "stage2",
150
+ "stage3",
151
+ "stage4",
152
+ "stage5",
153
+ "stage6",
154
+ "stage7",
155
+ "stage8",
156
+ "stage9",
157
+ "stage10",
158
+ "stage11",
159
+ "stage12",
160
+ "stage13",
161
+ "stage14",
162
+ "stage15",
163
+ "stage16",
164
+ "stage17",
165
+ "stage18",
166
+ "stage19",
167
+ "stage20",
168
+ "stage21",
169
+ "stage22",
170
+ "stage23",
171
+ "stage24",
172
+ "stage25",
173
+ "stage26",
174
+ "stage27",
175
+ "stage28",
176
+ "stage29",
177
+ "stage30",
178
+ "stage31",
179
+ "stage32",
180
+ "stage33",
181
+ "stage34",
182
+ "stage35",
183
+ "stage36",
184
+ "stage37",
185
+ "stage38",
186
+ "stage39",
187
+ "stage40"
188
+ ],
189
+ "transformers_version": "5.10.0.dev0",
190
+ "use_gated_mlp": true,
191
+ "use_mask_token": false,
192
+ "use_qk_norm": true,
193
+ "value_bias": true
194
+ }
facebook__sapiens2-normal-1b-int4-g128.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d484690015ac19d478e240946383061d70b7373687f59e41e0c2f542fbec2f7
3
+ size 797282804
load_sapiens2_int4.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Load a sapiens2-int4-safetensors artifact back into a PyTorch state dict."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import json
8
+ from pathlib import Path
9
+
10
+ import torch
11
+ from safetensors import safe_open
12
+ from safetensors.torch import save_file
13
+
14
+
15
+ def unpack_int4(packed: torch.Tensor, elements: int) -> torch.Tensor:
16
+ packed = packed.cpu().to(torch.uint8)
17
+ lo = (packed & 0x0F).to(torch.int16)
18
+ hi = ((packed >> 4) & 0x0F).to(torch.int16)
19
+ vals = torch.empty(packed.numel() * 2, dtype=torch.int16)
20
+ vals[0::2] = lo
21
+ vals[1::2] = hi
22
+ vals = vals[:elements]
23
+ vals = torch.where(vals >= 8, vals - 16, vals)
24
+ return vals.to(torch.float32)
25
+
26
+
27
+ def dequantize_tensor(packed: torch.Tensor, scales: torch.Tensor, shape: list[int], group_size: int, dtype: str) -> torch.Tensor:
28
+ elements = 1
29
+ for dim in shape:
30
+ elements *= dim
31
+ pad = (-elements) % group_size
32
+ q = unpack_int4(packed, elements + pad).view(-1, group_size)
33
+ out = (q * scales.to(torch.float32)[:, None]).flatten()[:elements].view(*shape)
34
+ target_dtype = getattr(torch, dtype, torch.float16)
35
+ return out.to(target_dtype if target_dtype.is_floating_point else torch.float32)
36
+
37
+
38
+ def load_state_dict(path: str | Path, device: str = "cpu") -> dict[str, torch.Tensor]:
39
+ with safe_open(str(path), framework="pt", device="cpu") as f:
40
+ metadata = f.metadata() or {}
41
+ manifest = json.loads(metadata["manifest_json"])
42
+ state = {}
43
+ for name, info in manifest.items():
44
+ if info.get("quantized"):
45
+ state[name] = dequantize_tensor(
46
+ f.get_tensor(info["qweight"]),
47
+ f.get_tensor(info["scales"]),
48
+ info["shape"],
49
+ int(info["group_size"]),
50
+ info.get("dtype", "float16"),
51
+ ).to(device)
52
+ else:
53
+ state[name] = f.get_tensor(name).to(device)
54
+ return state
55
+
56
+
57
+ def main() -> None:
58
+ parser = argparse.ArgumentParser()
59
+ parser.add_argument("artifact")
60
+ parser.add_argument("--save-dequantized", help="Optional safetensors path for the dequantized state dict")
61
+ args = parser.parse_args()
62
+
63
+ state = load_state_dict(args.artifact)
64
+ print(f"loaded {len(state)} tensors")
65
+ if args.save_dequantized:
66
+ save_file(state, args.save_dequantized)
67
+ print(f"saved {args.save_dequantized}")
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()
preprocessor_config.json ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "crop_size": {
3
+ "height": 224,
4
+ "width": 224
5
+ },
6
+ "do_center_crop": false,
7
+ "do_normalize": true,
8
+ "do_pad": true,
9
+ "do_reduce_labels": false,
10
+ "do_rescale": true,
11
+ "do_resize": true,
12
+ "image_mean": [
13
+ 0.485,
14
+ 0.456,
15
+ 0.406
16
+ ],
17
+ "image_processor_type": "Sapiens2ImageProcessor",
18
+ "image_std": [
19
+ 0.229,
20
+ 0.224,
21
+ 0.225
22
+ ],
23
+ "resample": 2,
24
+ "rescale_factor": 0.00392156862745098,
25
+ "size": {
26
+ "height": 1024,
27
+ "width": 768
28
+ }
29
+ }
quantization_report.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "input": "/home/rezo/sapiens2_quant/run/downloads/facebook__sapiens2-normal-1b/sapiens2_1b_normal.safetensors",
3
+ "output": "/home/rezo/sapiens2_quant/run/artifacts/facebook__sapiens2-normal-1b-int4-g128.safetensors",
4
+ "group_size": 128,
5
+ "tensor_count": 704,
6
+ "quantized_tensor_count": 250,
7
+ "source_bytes": 6157337292,
8
+ "artifact_bytes": 797079156,
9
+ "compression_ratio": 7.724875560539687,
10
+ "max_tensor_mae": 0.004776639398187399,
11
+ "repo": "facebook/sapiens2-normal-1b",
12
+ "fidelity": {
13
+ "global_floating_similarity_percent": 99.34075001308999,
14
+ "min_large_tensor_cosine": 0.9873846173286438,
15
+ "threshold": 0.9
16
+ }
17
+ }