Repository navigation
[muse-glimmer] Fix bf16 positional embedding interpolation on CPU - #23798
Conversation
interpolate_pos_emb passed a permuted view of the bf16 positional table (strides (1536, 1, 49152, 1536)) to F.grid_sample. For Half and BFloat16 the CPU kernel computes gather offsets in int16 (int_same_size_t), and grid_sampler_2d_cpu checks offset overflow only for float32. The H stride 49152 does not fit in int16, so the offsets wrap. On the 32x32 grid about half of the reads land before the start of the storage and about 98% of the outputs are wrong, some NaN. Those out of bounds reads can fault when the memory before the storage is unmapped, which is consistent with the intermittent segfault in grid_sample that test_vision_precompute.py hits on the macOS unittest job. Make the view contiguous before grid_sample. The largest gather offset becomes 1023, and the interpolation stays in the table's dtype as the docstring requires. The tests only checked shapes. Require the 32x32 identity interpolation to reproduce the table exactly, and the 8x8 bf16 result to match a float32 reference within the bf16 summation bound. Both assertions fail on the previous code.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23798
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 87b7e52 with merge base a90db24 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: examples" |
|
The |
Récupère pytorch#24050 et pytorch#24137, qui réparent unittest-cuda-muse-glimmer-batching et test-muse-glimmer-cuda-e2e-kquant-17gb-solo-text-batching, rouges sur main à la base précédente de la PR (d1f7f1f). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Fixes #23797
interpolate_pos_embpassed a permuted view of the bf16 positional table (strides(1536, 1, 49152, 1536)) toF.grid_sample. For Half and BFloat16 the CPU kernel computes gather offsets in int16, and only float32 is guarded against offset overflow, so the H stride of 49152 makes the offsets wrap. On the 32x32 grid about 98% of the outputs are wrong, some NaN, and the reads that land before the storage are consistent with the intermittentgrid_samplesegfault in the macOS unittest job. The issue has the details and references.The underlying kernel bug is reported upstream in pytorch/pytorch#200949.
This change makes the view contiguous before
grid_sample. The largest gather offset becomes 1023, and the interpolation stays in the table's dtype, as the docstring requires. The tests only checked shapes and dtypes:test_identity_grid_matches_tablenow requires the 32x32 interpolation to return the table exactly, and the newtest_downscaled_matches_float32compares the 8x8 bf16 result with a float32 reference, within3 * epstimes the weighted absolute sum of the texels. That bound covers the three bf16 roundings of the accumulation with a factor of two to spare.Computing in float32 and casting once would also avoid the kernel issue. However, it departs from the documented computation in the table's dtype and changes the outputs: about a third of the 8x8 values, by up to 0.0156, and about 97% of the 74x74 values, by up to about 0.75 (the separate question in the issue). I kept the smaller change.
cc @mergennachin @iseeyuan @lucylq @helunwencser @tarun292 @kimishpatel @jackzhxng @nil-is-all @digantdesai, who added this example in #21711.
Test plan
I ran this test file locally on Windows (x86 AVX2), three times under torch 2.14.0+cpu (Python 3.12) and three times under torch 2.11.0+cu128 (Python 3.14). Neither environment has an editable install, so a small wrapper made
executorch.examples.models.muse_glimmerresolve to this branch's sources before calling pytest with the repository'spytest.ini. With this change, every test passes, exceptResizeReferenceParityTest::test_matches_pinned_goldenunder torch 2.14, which fails in that environment only because PIL is not installed there. Withprecompute.pyfrommainand the new tests,test_identity_grid_matches_tablefails (97.5% to 97.8% of the values mismatched), and so doestest_downscaled_matches_float32(more than 96000 of 98304 values out of the bound).Over 50 seeds, the identity case is exact and the 8x8 error stays below 1.34 times
epstimes the weighted absolute sum, against 3 in the test.ufmt checkandflake8, at the versions pinned inrequirements-lintrunner.txt, report no findings; thetorchfixplugin was not installed. I could not run the macOS arm64 job.Disclosure: this PR was authored with AI assistance (Claude Code).