Skip to content

core/picture: normalize, the processors' u8-to-float arithmetic as one table a channel (stacked on #1584) - #1592

Merged
ashhart merged 2 commits into
ashhart:mainfrom
jschmied:picture-normalize
Oct 29, 2026
Merged

ashhart merged 2 commits into
ashhart:mainfrom
jschmied:picture-normalize

Conversation

@jschmied

Copy link
Copy Markdown
Contributor

Stacked on #1584: only the top commit is new here.

What

Six places turned a pixel's u8 into the tower's float on their own:

  • core/picture/patches.zig;
  • Qwen-VL's video;
  • LFM2's tiles;
  • GLM5-Next's images and video;
  • Step-5.

Between them they computed four different expressions. core/picture/normalize.zig names them as modes and builds a 256-entry table per channel, which each patch loop now indexes:

mode arithmetic used by
unfused x * rescale in f64 to f32, then (x - mean) / std (transformers' PIL processors) patches (PIL path), Step-5
fused (x - f32(mean / rescale)) / f32(std / rescale) (torchvision, the fast processors) patches (fused), Qwen-VL video, LFM2
fused_f32 mean and std times f32(1 / rescale), in f32 GLM5-Next video (0.6.6)
rescale_f32 x * f32(rescale) in f32, then (x - mean) / std GLM5-Next images (0.6.6)

Two findings came out of the exhaustive tests:

  • Step-5's ToTensor (x / 255 in f32) gives unfused's value for every u8, independent of mean and std. So Step-5 takes unfused and needs no mode of its own.
  • fused_f32 gives fused's bits for CLIP's and 0.5's mean and std. It stays separate because GLM reads its mean and std from the checkpoint at run time.

Gemma 4's [0, 1] is unfused with mean 0 and std 1. It follows once #1589 lands, since that PR rewrites the same file.

Checks

  • normalize_test.zig checks each mode against the expression its family computed in place before this change. It covers all 256 values of every channel, bit for bit, with the families' own means and stds, and also checks that unfused, fused and rescale_f32 differ.
  • Every patch value is unchanged.
  • Lean checker: no new problems.
  • zig build test: passes.

jschmied and others added 2 commits October 29, 2026 23:43
…e table a channel

Six places turned a pixel's u8 into the tower's float on their own: core/picture/patches,
Qwen-VL's video, LFM2's tiles, GLM5-Next's images and video, and Step-5. They compute
four different expressions, now named modes of core/picture/normalize.zig:

- unfused: transformers' PIL processors, x * rescale in f64 to f32, then (x - mean) / std;
- fused: torchvision's, (x - f32(mean / rescale)) / f32(std / rescale);
- fused_f32: GLM5-Next's video, mean and std times f32(1 / rescale) in f32;
- rescale_f32: GLM5-Next's images, x * f32(rescale) in f32.

Step-5's ToTensor, x / 255 in f32, is unfused's value for every u8, so it takes unfused.
GLM's fused_f32 gives fused's bits for CLIP's and 0.5's mean and std; it stays its own
mode because the checkpoint's mean and std are read at run time.

normalize_test.zig checks each mode against the expression its family computed in place,
all 256 values of every channel, bit for bit, with the families' means and stds, and that
unfused, fused and rescale_f32 differ. Every patch value is unchanged; the loops now index
the table.

(cherry picked from commit 9d8f165de09129a53946d5263c92f523a02cdf4f)
(cherry picked from commit 5001d17e5fac53806913a0d936c1d8fb95641668)
@ashhart
ashhart merged commit 2613085 into ashhart:main Oct 29, 2026
9 checks passed
@ashhart

ashhart commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. This is in TensorFold 1.2.0, released today: https://github.com/ashhart/TensorFold/releases/tag/v1.2.0

It landed with your commits under your own name, so you're listed as a TensorFold contributor, and it's credited to you in the release notes and the changelog.

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.

2 participants