Repository navigation
Serialize only a view constant's own bytes in XNNPACK and Vulkan - #24208
Open
pytorchbot wants to merge 1 commit into
Open
pytorchbot wants to merge 1 commit into
pytorchbot wants to merge 1 commit into
Conversation
) ## What is wrong today A constant tensor can be a view into a larger tensor. For example: ```python self.register_buffer("weight", torch.randn(3, 8, 8)[1]) # starts 64 floats into its storage ... return F.linear(x, self.weight) ``` Constant folding can also leave a weight as a view, like one chunk of the packed `in_proj_weight` of `nn.MultiheadAttention` in cross-attention. The XNNPACK and Vulkan serializers copy the bytes of the whole underlying storage, starting at its first byte. For a view that starts later in the storage, the delegate gets the wrong weights, and the .pte gives wrong results with no error. In the model above, the largest XNNPACK error was about 6 to 8. For a view that starts at the beginning, the values are right, but the file carries the whole parent. XNNPACK per-channel scales are read the same way. They also skip the `contiguous()` step that weights get, so a transposed groupwise scale is written in the wrong order. ## What this change does - Before reading a weight's bytes, both serializers now clone it if its storage is larger than the tensor. The ExecuTorch emitter already uses this rule for weights that are views (`exir/tensor.py`, `exir/emit/_emitter.py`). - XNNPACK per-channel scales are small, so they are now always copied into contiguous storage of their own. - The Vulkan path that serializes raw tensors (only tests use it today) now does the same. ## What was tested On macOS arm64. Each of these fails before this change and passes after: - `test_fp32_linear_view_weight` (XNNPACK): a linear whose weight is a view. - `test_qd8_linear_transposed_scale` (XNNPACK): a groupwise int4 linear with a transposed scale. - `test_view_constant_serializes_only_its_own_bytes` (Vulkan graph builder): checks the bytes of a view constant, no GPU needed. - `test_serialize_vulkan_binary` (existing): now also serializes a view and a transposed view. The default `backends/xnnpack/test` run passes. It skips `test_linear.py`, so that file was run directly: all tests pass except `test_linear_qd8_{f32,f16}_per_channel_int4`, which fail the same way without this change. A per-channel scale replaced by an equal view was also checked by hand: off by about 4e4 before, exact after. ## Not in this change `_tensor_to_bytes` in `exir/_serialize/_named_data_store.py` has a similar problem: for a non-contiguous tensor, such as a channels_last slice of a bigger tensor, it writes the whole storage. XNNPACK and Vulkan pass it plain bytes, so this change does not depend on it. It is left for a separate fix. cc @SS-JIA @manuelcandales @digantdesai @cbilgin (cherry picked from commit b3bd402)
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24208
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New FailureAs of commit 8f7115f with merge base 4db778e ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is wrong today
A constant tensor can be a view into a larger tensor. For example:
Constant folding can also leave a weight as a view, like one chunk of the packed
in_proj_weightofnn.MultiheadAttentionin cross-attention.The XNNPACK and Vulkan serializers copy the bytes of the whole underlying storage, starting at its first byte. For a view that starts later in the storage, the delegate gets the wrong weights, and the .pte gives wrong results with no error. In the model above, the largest XNNPACK error was about 6 to 8. For a view that starts at the beginning, the values are right, but the file carries the whole parent.
XNNPACK per-channel scales are read the same way. They also skip the
contiguous()step that weights get, so a transposed groupwise scale is written in the wrong order.What this change does
exir/tensor.py,exir/emit/_emitter.py).What was tested
On macOS arm64. Each of these fails before this change and passes after:
test_fp32_linear_view_weight(XNNPACK): a linear whose weight is a view.test_qd8_linear_transposed_scale(XNNPACK): a groupwise int4 linear with a transposed scale.test_view_constant_serializes_only_its_own_bytes(Vulkan graph builder): checks the bytes of a view constant, no GPU needed.test_serialize_vulkan_binary(existing): now also serializes a view and a transposed view.The default
backends/xnnpack/testrun passes. It skipstest_linear.py, so that file was run directly: all tests pass excepttest_linear_qd8_{f32,f16}_per_channel_int4, which fail the same way without this change. A per-channel scale replaced by an equal view was also checked by hand: off by about 4e4 before, exact after.Not in this change
_tensor_to_bytesinexir/_serialize/_named_data_store.pyhas a similar problem: for a non-contiguous tensor, such as a channels_last slice of a bigger tensor, it writes the whole storage. XNNPACK and Vulkan pass it plain bytes, so this change does not depend on it. It is left for a separate fix.cc @SS-JIA @manuelcandales @digantdesai @cbilgin