bugfix: ensure tensor contiguous layout before protobuf serialization.#1468
Open
a120092009 wants to merge 2 commits into
Open
bugfix: ensure tensor contiguous layout before protobuf serialization.#1468a120092009 wants to merge 2 commits into
a120092009 wants to merge 2 commits into
Conversation
NHWC (channels_last) tensors pass raw bytes directly into the proto buffer when serialized via torch_to_proto. On deserialization the reconstructed tensor assumes NCHW (contiguous) layout, which corrupts the channel order for bfloat16 and float16 image data. Force a contiguous copy before reading data_ptr() so the byte stream always matches the expected row-major order.
Contributor
There was a problem hiding this comment.
Code Review
This pull request ensures that tensors are contiguous before serializing raw bytes in torch_to_proto, specifically for kBFloat16 and kHalf types. The review feedback correctly identifies that the current implementation performs a redundant .contiguous() call for all tensor types at the top of the function, which can lead to performance regressions. Additionally, the feedback points out violations of the repository style guide regarding the use of auto for torch::Tensor and the placement of variable declarations. It is recommended to move the contiguity check locally within the specific switch cases where it is required.
…ranches. Avoid redundant contiguous() for types where set_data_to_contents already handles the conversion, and use explicit torch::Tensor type per style guide. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
XuZhang99
approved these changes
May 18, 2026
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.
NHWC (channels_last) tensors pass raw bytes directly into the proto buffer when serialized via torch_to_proto. On deserialization the reconstructed tensor assumes NCHW (contiguous) layout, which corrupts the channel order for bfloat16 and float16 image data. Force a contiguous copy before reading data_ptr() so the byte stream always matches the expected row-major order.