fix(cuda): guard CPU index tensors in index/_unsafe_index boxing - #87
Open
ethanZZZZZZZZZZZZ wants to merge 1 commit into
Open
fix(cuda): guard CPU index tensors in index/_unsafe_index boxing#87ethanZZZZZZZZZZZZ wants to merge 1 commit into
ethanZZZZZZZZZZZZ wants to merge 1 commit into
Conversation
Collaborator
|
绑定issue |
gen_optlist-generated kernels boxed every defined optional<Tensor> in the
index list via the unconditional BoxToCuda, rewriting the metadata of
host-memory CPU indices into fake-CUDA tensors. Vendor kernels then reject
them ("indices should be either on cpu or on the same device as the indexed
tensor (cuda:0)"), breaking transformers assisted decoding
(q[:, torch.arange(n), ids], generation/utils.py). CPU indices are legal in
advanced indexing (PyTorch copies them device-side itself); only tensors
actually on PrivateUse1 may be boxed.
Also make the unbox sequence exception-safe: when the at:: call throws,
boxed tensors previously kept their forged CUDA metadata forever, poisoning
every later op touching them -- observed in UT batches as cascading bogus
device errors and as a segfault inside pytest's traceback repr rendering.
Restrict the boxing loop to is_privateuseone() tensors (same guard as
DeviceBoxingGuard::maybe_box) and wrap the call in try/catch that always
restores flagos metadata on the failure path.
Verified on MetaX C550 (driver 3.8.1, MACA 3.8.0.23, torch 2.10.0+cpu
boxing venv): cpu/flagos/mixed advanced-indexing cases pass with
CPU-identical values, post-exception metadata hygiene holds, and
tests/models/whisper/test_modeling_whisper.py -k assisted (6 UTs incl.
WhisperStandaloneDecoderModelTest::test_assisted_decoding_sample) passes.
Co-Authored-By: Claude <noreply@anthropic.com>
ethanZZZZZZZZZZZZ
force-pushed
the
fix/index-optlist-guard
branch
from
August 13, 2026 02:44
4cf2c04 to
d9acae0
Compare
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.
问题
Fixes #86
boxing 路线上,
aten::index.Tensor/aten::_unsafe_index.Tensor的索引列表中一旦含有 CPU 张量(PyTorch 高级索引的合法用法,PyTorch 会自行把 CPU 索引搬到设备侧),生成的 CUDA boxing kernel 会无条件对它做零拷贝元数据改写(BoxToCuda),把一个存储在 host 内存上的张量伪装成cuda:0,厂商 kernel 设备检查直接拒绝:影响真实模型路径:transformers 推测解码(
generation/utils.py中q[:, torch.arange(candidate_length), new_candidate_input_ids],arange在 CPU、ids 在设备上),导致 HF 官方 UT 回归:同一代码点还有第二个缺陷:
at::index抛异常(越界、索引 dtype 不合法等)时,已 box 的张量不会被还原,伪造的 CUDA 元数据永久残留——同进程后续所有涉及该张量的 op 连环错乱,且在 pytest 渲染失败repr时直接段错误(torch/_tensor_str.py→torch/_tensor.py __iter__),失败信息丢失、批量测试结果不可信。根因
scripts/codegen_ops.py的gen_optlist模板(仅index.Tensor/_unsafe_index.Tensor两个 op 走special_optlist类别)生成的 box 循环条件为:缺少
is_privateuseone()检查。BoxToCuda(csrc/aten/device_boxing.h:50-52)是无条件改写 TensorImpl 设备字段的裸原语;仓库其余调用点(DeviceBoxingGuard::maybe_box、BoxTensorListToCuda)都带该守卫,唯此遗漏。同时api(...)之后的UnboxToFlagos序列写在正常返回路径上,无异常安全。修复
is_privateuseone()的张量;CPU 索引原样透传,由 PyTorch 按原生语义处理(与DeviceBoxingGuard::maybe_box的既有惯例对齐);try/catch(...),失败路径同样还原全部已 box 张量的元数据;csrc/aten/generated/cuda_kernels.cc为重新生成产物(diff 仅IndexTensorKernelCuda/PrivUnsafeIndexTensorKernelCuda两个函数),生成器两次运行幂等。验证(MetaX C550 ×8,driver 3.8.1,MACA 3.8.0.23,torch 2.10.0+cpu boxing 环境,transformers v5.12.1)
_unsafe_index、异常后元数据卫生共 7 项)WhisperStandaloneDecoderModelTest::test_assisted_decoding_sample-k assisted,6 个)Module.to)备注
scripts/codegen_ops.py模板改动 + 重新生成的csrc/aten/generated/cuda_kernels.cc;其余生成产物(flaggems_python_kernels.cc、*_flaggems.conf)未变。DeviceBoxingGuard(RAII)或BoxTensorListToCuda,天然具备守卫与异常安全,本次修复使gen_optlist这一特例对齐。