fix: skip pin_memory and non_blocking transfer for tensor subclasses in group offloading#13305
Open
s-zx wants to merge 1 commit intohuggingface:mainfrom
Open
fix: skip pin_memory and non_blocking transfer for tensor subclasses in group offloading#13305s-zx wants to merge 1 commit intohuggingface:mainfrom
s-zx wants to merge 1 commit intohuggingface:mainfrom
Conversation
…in group offloading Tensor subclasses such as torchao's AffineQuantizedTensor may not support pin_memory() correctly and can silently lose quantization metadata when pinned. Similarly, non-blocking host-to-device transfers may race with forward computation for subclasses that override .to(). - _init_cpu_param_dict: call pin_memory() only for plain torch.Tensor - _pinned_memory_tensors: same guard in the context-manager path - _transfer_tensor_to_device: force synchronous transfer for subclasses, and skip record_stream() when the result is not a plain tensor Fixes huggingface#13281
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 does this PR do?
Fixes #13281.
When combining torchao quantization (e.g.
Float8WeightOnlyConfig) with group offloading (use_stream=True), inference fails with a device mismatch: the quantized weight remains on CPU while the input is on CUDA.Root cause
GroupOffloadingHook._init_cpu_param_dictcalls.pin_memory()on every parameter/buffer, and_pinned_memory_tensorspins them again before the async host-to-device transfer. For plaintorch.Tensorthis works correctly, but for tensor subclasses such as torchao'sAffineQuantizedTensor,pin_memory()may silently strip the quantization metadata, producing a plain CPU float tensor. The subsequent.to(device, non_blocking=True)then moves a plain float tensor instead of the quantized one, leaving the quantized weight behind on CPU.Similarly,
_transfer_tensor_to_deviceschedulesrecord_streamfor all results, butAffineQuantizedTensor.to(...)may not return a plaintorch.Tensor, sorecord_streamcould raise or be a no-op.Fix
_init_cpu_param_dict: introduce_maybe_pin()helper that callspin_memory()only whentype(data) is torch.Tensor. Tensor subclasses are stored as-is (already on CPU)._pinned_memory_tensors: apply the sametype(tensor) is torch.Tensorguard._transfer_tensor_to_device: force synchronous (blocking) transfer for tensor subclasses, and skiprecord_streamwhen the result is not a plain tensor.Plain-tensor performance is completely unaffected; the async-stream path is preserved for all non-quantized parameters.