Skip to content

hy4_flash: host vision config, patchify, bilinear taps, and 3D M-RoPE layout (slice 4a of #4795) - #5022

Merged
ashhart merged 3 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-vision-host
Nov 25, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
GrgaPalcic:pr/hy4-vision-host

Conversation

@GrgaPalcic

@GrgaPalcic GrgaPalcic commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Adds host-side SigLIP2 vision configuration, image preprocessing, bilinear 128x128 position-embedding taps, and 3D M-RoPE layout for tencent/Hy4-flash-FP8 inside zig/src/families/hy4_flash/ (vision_config.zig and vision_config_test.zig):

  • Config.read / Config.parse: parses vision_config from config.json and optional normalization/pixel bounds from processor_config.json / preprocessor_config.json (returning null for text-only checkpoints), validating Hy4-flash SigLIP2 geometry (hidden=1152, heads=16, depth<=32, intermediate=4304, out_hidden=3072, patch=16, merge=2, channels=3, max_image_size=2048, hidden_act="gelu") and rejecting non-finite or non-positive eps, rescale, std_, or inverted pixel bounds.
  • tokensForGrid, prepare, prepareRgb, patchifyRaster: smart-resizes images onto 32-pixel multiples (patch * merge) within min_pixels..max_pixels and max_tokens (accounting for one image_newline per merged row), resizes via core.picture.resample.resize(.pillow, ...), and patchifies in raster (gh, gw, C=3, ph=16, pw=16) order with (scaled - mean) / std.
  • positions: computes bilinear 4-tap indices and weights into the 128x128 position grid (scale_factor = (size + 0.1) / 128, with exact identity at 128x128).
  • layout: computes src_idx [n] (-1 on text, 0..total_img_tokens on image pads), 3D M-RoPE pos3 [3, n] (T, H, W), and rope_delta for single-image, multi-image, and text-only prompts, rejecting odd grids, overlapping/unsorted runs, and out-of-bounds spans.
  • Registers vision_config_test.zig in zig/src/families/hy4_flash/host_tests.zig (48/48 pass in test-hy4-flash-host).

No served path, model load, default, or GPU kernel is changed in this slice. Remaining work for #4795: CUDA kernels + kernel contract gates/controls/limits, served W8A16 TP=2/TP=4 + DFly2 CUDA Graphs with serial token-equality receipts, and CUDA SigLIP2 --vision + 3D M-RoPE integration.

Checks on this head

  • Head commit: 9777ec144f8278d8a4d13367f02c35992dbec2ee (base ef4d6429fc873fe34cf37379f69921a791b04ab3)
  • Lean checker, python3 tools/zig/lean_check.py, on the base and on this head: 165 on base (ef4d6429f, inherited), 165 on head (9777ec144), 0 new (python3 tools/pr_check.py --base origin/main: 0 error(s))
  • Tests run, with the command and pass, fail and skip counts:
    • zig build -Dfixed-cpu test-hy4-flash-host --summary all on 9777ec144 (Zig 0.17.0, Linux x86_64): 48 pass, 0 fail, 0 skip (42 baseline + 6 new; exit 0)
    • zig fmt --check build.zig zig && git diff --check: exit 0
  • Not run, and why: GPU kernel contract and real-model serving/vision benchmarks (host-only vision config, patchify, position-tap, and 3D M-RoPE slice; no GPU kernel or inference path is touched; device gates and serving receipts follow in the CUDA slices of Hy4-flash-FP8 (official W8A16) on 2x and 4x DGX Spark: native CUDA support in hy4_flash #4795). Windows/macOS hosts not run locally (covered by CI).

Receipt

n/a — host-only vision preprocessing and 3D M-RoPE layout module and unit tests; does not reach inference, model load, serving, or any GPU kernel.

  • Main against branch: n/a (host-only, no inference path changed)
  • Boxes it reaches: Linux x86_64 host tests (no GPU); macOS/Windows covered by CI
  • New switch? Default, accepted values, profile row: n/a (no switches or environment variables added)
  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: base ef4d6429f, head 9777ec144, Zig 0.17.0 (-Dfixed-cpu), Linux x86_64 host (no GPU); targets official tencent/Hy4-flash-FP8 @ 003107fa248f5892064ab1c43af0a7003dd68c9b
  • Paths and platforms the change reaches, which I ran and which I could not: ran Linux x86_64 host build and test suite (Windows/macOS covered by CI); did not run any CUDA/Metal/HIP/XPU GPU (host-only code)
  • Exactness, from tools/bench_concurrent.py --alone --serial or token_sha against "draft": false: n/a (does not reach inference; exactness of config parsing, raster patchify normalization, 128x128 identity and closed-form bilinear 4-tap interpolation, single/multi-image 3D M-RoPE coordinates and rope_delta, plus refusal controls is verified in vision_config_test.zig)
  • Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): n/a (host-only slice, no inference path changed)
  • Decode speed before and after, from tools/bench_openai.py: n/a (host-only slice)
  • Prompt speed before and after: n/a (host-only slice, cannot reach prompt processing)
  • Licence of any new weights or drafter, or "n/a": n/a (no weights or drafter bundled)

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@AIFlowML

Copy link
Copy Markdown
Contributor

I trial-merged 19a0adca3f cleanly into b5df430692 on macOS arm64. zig build test-hy4-flash-host -Dfixed-cpu -j2 --summary all passed 37/37. I also loaded both official config files from tencent/Hy4-flash-FP8@003107fa248f5892064ab1c43af0a7003dd68c9b in a local fixture and verified the geometry, image token and processor bounds.

One case needs a resize regression before this goes into serving. When one side is clamped to the minimum 32 pixels, the resized area can exceed the requested ceiling. The retry then derives its next ceiling from that larger area and can repeat the same grid indefinitely. A scratch portrait and landscape fixture with a four-token budget hit a 14-second test timeout on the current implementation. The result should fit four tokens, including the row newline.

A local prototype makes the ceiling decrease on every retry and handles a single merged row or column directly. One column can fit at most max_tokens / 2 rows. One row can fit at most max_tokens - 1 columns. With the narrow-image fixture and the pinned config test, the same host target passed 39/39.

Igor Lessio - AIFLOW LABS

@GrgaPalcic

Copy link
Copy Markdown
Contributor Author

Great catch, @AIFlowML — thank you for testing 19a0adca3f on macOS arm64 and flagging that narrow-strip non-termination bug!

Fixed in zig/src/families/hy4_flash/vision_config.zig (prepareRgb):

  • When size.h == 32 (single merged row), clamp size.w directly to (max_tokens - 1) * 32 (u64 arithmetic before narrowing) and break.
  • When size.w == 32 (single merged column), clamp size.h directly to (max_tokens / 2) * 32 and break.
  • Otherwise decrease ceiling strictly monotonically via @min(ceiling, cur_px) - unit_px, falling back to (32, 32) if the cap reaches unit_px.

Added regression coverage in zig/src/families/hy4_flash/vision_config_test.zig (prepareRgb terminates and respects max_tokens for narrow portrait and landscape images clamped to 32 px) covering 128x16 and 16x128 at max_tokens = 4 as well as 800x8 and 8x800 strips at max_tokens = 5.

zig build -Dfixed-cpu test-hy4-flash-host --summary all passes 48/48 (958ms), with 0 new lean_check findings (pr_check.py) and zig fmt --check clean.

@AIFlowML

Copy link
Copy Markdown
Contributor

Thanks for the fix. I trial-merged 9777ec144f cleanly into 47a605aa12 and reran this on the M5 Max:

zig build test-hy4-flash-host -Dfixed-cpu -j2 --summary all

The author's revised suite passed 38/38. Adding the original independent 320×32 and 32×320 portraits and landscapes, each with a four-token budget, plus the pinned official config check, brought it to 40/40. Both orientations returned within budget, including each row's newline token. That closes my host resize finding. A served vision path and CUDA device qualification are outside this slice.

Igor Lessio - AIFLOW LABS

@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks @GrgaPalcic. The narrow-image fix now bounds the remaining axis and makes the general area budget decrease. The raster patch order, position taps and image-newline M-RoPE layout are consistent in the host-only helper, and the earlier termination issue is addressed in source.

This looks safe to land as the host-only slice through the normal checks. Full image preprocessing and engine token equivalence remain qualification work for the later integration.

  • Synthena

@ashhart
ashhart merged commit 12535c3 into ashhart:main Nov 25, 2026
4 checks passed
@ashhart

ashhart commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thank you @GrgaPalcic. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants