Repository navigation
fix(torch_bin): honour _rebuild_tensor_v2 storage_offset and reject non-contiguous tensors - #757
Merged
Conversation
…on-contiguous tensors
The torch .bin reader used by VibeVoice fine-tune adapters kept only the
storage and size arguments of _rebuild_tensor_v2 and always read a tensor
from byte 0 of its storage. torch.save writes views that share a storage
once, each with its own storage_offset, so a checkpoint saved as
{"head": x[:4], "tail": x[4:]} loaded "tail" with head's values, and a
transposed tensor was read in storage order. Neither raised an error.
Read storage_offset and stride, start each tensor at its offset, bound the
offset against the storage size, and reject non-contiguous strides (size-1
dims may carry any stride, as in torch's is_contiguous). This matches what
the RVC checkpoint reader already does. Ordinary module state_dicts (offset
0, contiguous) load exactly as before.
Add torch_bin_storage_offset_test covering 1-D and 2-D views, size-1 dims,
and the rejected transposed and out-of-range cases.
Owner
|
@Zhuoxi2000 Thanks! PR merged. |
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 / why
The torch
.binreader insrc/framework/assets/torch_bin.cpploads VibeVoice fine-tune adapters:diffusion_head_full.binand the connectorpytorch_model.binfiles, viasrc/models/vibevoice/lora.cpp. From each_rebuild_tensor_v2(storage, storage_offset, size, stride, ...)it kept onlystorageandsize, and every tensor was read from byte 0 of its storage.torch.save stores tensors that share a storage only once, and records a separate
storage_offsetfor each one. So a file saved as{"head": x[:4], "tail": x[4:]}loadedtailwithhead's values, and a transposed tensor was read in storage order. Neither raised an error, so the model silently ran with the wrong weights.After this change, such files either load the values
torch.loadreturns or fail with a clear error. The reader now behaves the same way as the RVC checkpoint reader insrc/models/rvc/voice_model.cpp.Change
src/framework/assets/torch_bin.cpp:op_reducerequiresstorage_offset(args[1]) to be an Int andstride(args[3]) to be a Tuple of Int, and stores both on the tensor. It rejects negative offsets and dimensions, and a stride whose rank differs from the shape.byte_offset = storage.offset + storage_offset * element_bytes. It replaces the "larger than its storage" check with a bounds check that accounts for the offset and cannot overflow.torch .bin tensor is not contiguous, which is not supported: <name>. The check follows torch'sis_contiguous, which ignores the stride of size-1 dimensions: for exampletorch.arange(4.).reshape(1, 4).t()(shape(4, 1), stride(1, 4)) is laid out row-major and is accepted. RVC'scontiguous_stridesis stricter and would reject that tensor; I followed torch here. I did not add a strided gather, to keep the change small.include/engine/framework/assets/torch_bin.h: the comment now describes this behaviour.tests/unittests/test_torch_bin_storage_offset.cpp(new) is registered inCMakeLists.txtaftersafetensors_metadata_depth_test. It builds the checkpoint in memory as a STORED zip with a protocol 2 pickle, using the same opcodes torch.save emits. It checks:chunkview at offset 9Ordinary
module.state_dict()files are unaffected: every tensor is at offset 0 and contiguous, and the bytes read are the same as before. There is no performance or memory impact; the change only adds a few integer checks per tensor at load time.Validation
macOS on Apple M4, Release build, CPU:
Unit test. On main (f16f23a), with only the new test added (this branch with
torch_bin.cppandtorch_bin.hreverted to main):With this PR, all four tests pass:
Real
torch.savefiles. I wrote these files with torch 2.14.1 and read them with thetorch_bin_paritytool (tests/vibevoice/torch_bin_parity.cpp, built by the commands above), once with main's loader (f16f23a) and once with this PR:torch .bin tensor is not contiguous, which is not supported: wt(exit 1)Regression check on ordinary state dicts. I saved state dicts with
torch.save(module.state_dict()):nn.Linear(4, 4);fc1 = Linear(64, 128),LayerNorm(128),fc2 = Linear(128, 128)), in F32 and in BF16.torch_bin_parityoutput (dtype, shape, mean and first values of every tensor) is byte-identical between main and this PR for all three files. I do not have a real VibeVoice fine-tune adapter locally, so I did not run end-to-end VibeVoice inference with an adapter. Adapters written withtorch.save(module.state_dict())take the unchanged path shown above.AI assistance: this change was drafted with an AI coding assistant (Claude) and verified locally with the tests above.