Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions colossalai/tensor/colo_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def __torch_function__(cls, func, types, args=..., kwargs=None):
with torch._C.DisableTorchFunction():
func = ColoParamOpHookManager.rewrite_op(func)
ret = super().__torch_function__(func, types, args, kwargs)
# Transformers v5 Qwen3-MoE indexes 3D expert parameters and keeps
# the resulting view for backward. Gemini may release the source
# chunk before that backward read, so detach the indexed view from
# the chunk storage while preserving its autograd history.
if func is torch.Tensor.__getitem__ and any(p.ndim >= 3 for p in params):
with torch._C.DisableTorchFunction():
ret = ret.clone()
with torch._C.DisableTorchFunction():
ret = ColoParamOpHookManager.post_op(params, ret)
return _convert_output(ret, func)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import torch

from colossalai.tensor.colo_parameter import ColoParameter
from colossalai.tensor.param_op_hook import ColoParamOpHook, ColoParamOpHookManager
from colossalai.utils import free_storage


class ReleaseParameterStorageHook(ColoParamOpHook):
def pre_forward(self, params):
pass

def post_forward(self, params):
for param in params:
free_storage(param)

def pre_backward(self, params):
pass

def post_backward(self, params):
pass


def test_indexed_3d_parameter_survives_storage_release():
param = ColoParameter(torch.randn(4, 3, 2, requires_grad=True))
hook = ReleaseParameterStorageHook()

with ColoParamOpHookManager.use_hooks(hook):
expert = param[0]
assert expert.storage().size() == 3 * 2
expert.square().sum().backward()

assert param.grad is not None
assert param.grad.shape == param.shape