immanuelpeter commited on
Commit
a87dfdd
·
verified ·
1 Parent(s): 977d6fd

Add the Hy4 inference example and complete the model card

Browse files
Files changed (3) hide show
  1. LICENSE-CODE +21 -0
  2. README.md +63 -6
  3. examples/inference.py +59 -0
LICENSE-CODE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Immanuel Peter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -2,17 +2,74 @@
2
  license: other
3
  license_name: tencent-hy-license
4
  license_link: LICENSE
5
- base_model: tencent/Hy4
6
  pipeline_tag: image-feature-extraction
7
  library_name: transformers
 
 
 
 
 
 
8
  ---
9
 
10
- # Hy4 Vision
11
 
12
- The standalone BF16 Tower and Projector extracted from [Tencent Hy4](https://huggingface.co/tencent/Hy4/tree/c10c0de5b6f850703fb0a90e65d1c32b558b9167) for frozen-feature probing. The release preserves the 649 vision tensors and removes the language model.
13
 
14
- The benchmark uses 512×512 RGB crops normalized with mean and standard deviation 0.5. The 40-block Tower produces a 32×32 raster grid at width 1536. The Projector produces 16×16 grids at width 6144. The `merged` Stage groups RMS-normalized 2×2 neighborhoods. The `projected` Stage emits the convolution output without row delimiters.
15
 
16
- Load with `AutoModel.from_pretrained("immanuelpeter/Hy4-Vision", trust_remote_code=True)`. Pass a batch of normalized RGB tensors and the desired one-indexed block depths. The model returns the requested Tower outputs, merged features, and projected features.
 
 
 
 
 
 
 
17
 
18
- The [Tencent Hy License 1.0](LICENSE) governs the weights. See [NOTICE](NOTICE) for attribution and changes.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: other
3
  license_name: tencent-hy-license
4
  license_link: LICENSE
 
5
  pipeline_tag: image-feature-extraction
6
  library_name: transformers
7
+ tags:
8
+ - vision
9
+ - feature-extraction
10
+ - hy4
11
+ base_model:
12
+ - tencent/Hy4
13
  ---
14
 
15
+ # Hy4-Vision
16
 
17
+ This repository packages the 1.16B-parameter vision Tower and 75.5M-parameter Projector from [Tencent Hy4](https://huggingface.co/tencent/Hy4). The weights come from the [pinned parent revision](https://huggingface.co/tencent/Hy4/tree/c10c0de5b6f850703fb0a90e65d1c32b558b9167).
18
 
19
+ ## Contents
20
 
21
+ | File | Tensors | What it holds |
22
+ |---|---|---|
23
+ | `model.safetensors` | 649 | The parent's BF16 `vit.*` tensors with the `vit.` prefix removed. There are 643 Tower tensors and 6 Projector tensors. |
24
+ | `modeling_hy4.py` | | Standalone Tower and Projector for Transformers |
25
+ | `config.json` | | Vision configuration and `AutoModel` mapping |
26
+ | `export_manifest.json` | | Source repo, revision, tensor count and weight bytes |
27
+ | [`examples/inference.py`](examples/inference.py) | | Image preprocessing and feature extraction |
28
+ | `LICENSE`, `LICENSE-CODE`, `NOTICE` | | Weight license, code license and attribution |
29
 
30
+ ## Architecture
31
+
32
+ | Component | Details |
33
+ |---|---|
34
+ | Tower | 40 pre-norm blocks, width 1536, MLP width 6144, 16 heads of width 96, GELU and LayerNorm with eps 1e-5 |
35
+ | Patches | 16 × 16 pixels, with bias in the patch embedding |
36
+ | Positions | Learned absolute positions on a 128 × 128 grid. The table has shape `[16385, 1536]` with row 0 unused. Bilinear interpolation uses `scale_factor = (size + 0.1) / 128` and `align_corners=False`. |
37
+ | Tower output | The last block feeds the Projector directly. Features contain patch tokens in raster order. |
38
+ | Projector | RMSNorm with eps 1e-5, a 2 × 2 stride-2 convolution from 1536 to 6144, GELU, and a 1 × 1 convolution at width 6144 |
39
+
40
+ The benchmark resizes the shorter side of the RGB input to 512 px with bicubic interpolation, then center-crops to 512 × 512. Each channel uses mean and standard deviation 0.5.
41
+
42
+ | Stage | Shape for one image | Features |
43
+ |---|---|---|
44
+ | `tower` | `(1, 1024, 1536)` | 32 × 32 patch grid at a requested block |
45
+ | `merged` | `(1, 256, 6144)` | RMS-normalized tokens grouped in 2 × 2 neighborhoods |
46
+ | `projected` | `(1, 256, 6144)` | Projector output with the learned row delimiters omitted |
47
+
48
+ ## Usage
49
+
50
+ With PyTorch, Torchvision, Pillow and Transformers 5.17 or later installed, download the bundle and run [the inference example](examples/inference.py).
51
+
52
+ ```bash
53
+ hf download immanuelpeter/Hy4-Vision --local-dir Hy4-Vision
54
+ python Hy4-Vision/examples/inference.py image.jpg
55
+ ```
56
+
57
+ The example prints the final Tower, merged and projected shapes. It uses BF16 on CUDA and FP32 on CPU. Pass `--device cpu` to select CPU explicitly.
58
+
59
+ `AutoModel.from_pretrained(..., trust_remote_code=True)` loads the standalone model. Its forward accepts normalized `pixel_values` of shape `(batch, 3, 512, 512)` and a tuple of one-indexed `depths`. It returns `(hidden_by_depth, merged, projected)`.
60
+
61
+ ## Validation
62
+
63
+ The [parity check](https://github.com/immanuel-peter/vision-tower-bench/blob/main/vtb/parity/hy4.py) compares every exported tensor and all three Stages with the pinned parent, using the `hy_v4_vl` implementation in [Transformers commit `1031277`](https://github.com/huggingface/transformers/commit/1031277cef60200fda98628e9adc8fb15f0e1549). All 649 weights and all 10 forward comparisons per run were bit-exact on CUDA in FP32 and BF16 and on CPU in FP32.
64
+
65
+ ## Reproduction
66
+
67
+ From a [vision-tower-bench](https://github.com/immanuel-peter/vision-tower-bench) checkout, run `python -m vtb.export hy4`. The [exporter](https://github.com/immanuel-peter/vision-tower-bench/blob/main/vtb/adapters/hy4.py) range-reads 649 `vit.*` tensors from eight of the parent's 245 shards and preserves the original BF16 values.
68
+
69
+ ## Credits
70
+
71
+ Tencent released the Hy4 weights. The vision forward follows the Hugging Face Transformers implementation.
72
+
73
+ ## License
74
+
75
+ The weights use the [Tencent Hy License, Version 1.0](LICENSE). Section 10 contains terms for large Model-as-a-Service businesses. The standalone modeling code and inference example use the [MIT License](LICENSE-CODE). See [NOTICE](NOTICE) for attribution and extraction details.
examples/inference.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Extract Tower, merged, and projected features from a downloaded Hy4 bundle."""
2
+
3
+ import argparse
4
+ from pathlib import Path
5
+
6
+ import torch
7
+ from PIL import Image
8
+ from torchvision import transforms
9
+ from transformers import AutoModel
10
+
11
+ BUNDLE = Path(__file__).absolute().parents[1]
12
+ RESOLUTION = 512
13
+
14
+
15
+ def preprocess(image: Image.Image) -> torch.Tensor:
16
+ crop = transforms.Compose(
17
+ [
18
+ transforms.Resize(
19
+ RESOLUTION, interpolation=transforms.InterpolationMode.BICUBIC
20
+ ),
21
+ transforms.CenterCrop(RESOLUTION),
22
+ transforms.ToTensor(),
23
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
24
+ ]
25
+ )
26
+ return crop(image.convert("RGB"))
27
+
28
+
29
+ def main() -> None:
30
+ parser = argparse.ArgumentParser(description=__doc__)
31
+ parser.add_argument("image", type=Path)
32
+ parser.add_argument(
33
+ "--device", default="cuda" if torch.cuda.is_available() else "cpu"
34
+ )
35
+ args = parser.parse_args()
36
+
37
+ device = torch.device(args.device)
38
+ dtype = torch.bfloat16 if device.type == "cuda" else torch.float32
39
+ model = (
40
+ AutoModel.from_pretrained(
41
+ BUNDLE, trust_remote_code=True, local_files_only=True, dtype=dtype
42
+ )
43
+ .to(device)
44
+ .eval()
45
+ )
46
+ with Image.open(args.image) as image:
47
+ pixels = preprocess(image).unsqueeze(0).to(device=device, dtype=dtype)
48
+
49
+ last = model.config.num_hidden_layers
50
+ with torch.inference_mode():
51
+ hidden, merged, projected = model(pixels, depths=(last,))
52
+
53
+ print("tower", tuple(hidden[last].shape))
54
+ print("merged", tuple(merged.shape))
55
+ print("projected", tuple(projected.shape))
56
+
57
+
58
+ if __name__ == "__main__":
59
+ main()