-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: flatten multi-component position_ids to 1D for nested tensor compatibility #5886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -56,15 +56,21 @@ def left_right_2_no_padding(data: TensorDict) -> TensorDict: | |||||
| input_ids_nested = torch.nested.nested_tensor_from_jagged(input_ids_rmpad.squeeze(-1), offsets=cu_seqlens) | ||||||
|
|
||||||
| position_ids_list = [] | ||||||
| num_pos_components = 0 # 0 means 1D position_ids, >0 means multi-component (e.g. 4 for Qwen3.5/Qwen2-VL) | ||||||
| for i in range(attention_mask.shape[0]): | ||||||
| curr_mask = attention_mask[i].bool() | ||||||
| curr_pos_ids = position_ids[i] | ||||||
| if curr_pos_ids.dim() == 1: # (seq_len,) | ||||||
| valid_ids = curr_pos_ids[curr_mask] | ||||||
| else: # (4, seq_len) | ||||||
| valid_ids = curr_pos_ids[:, curr_mask] | ||||||
| else: # (num_components, seq_len) — flatten to 1D for nested tensor compatibility | ||||||
| # 3D jagged nested tensors have broken unbind() and to_padded_tensor() in PyTorch | ||||||
| # (see pytorch/pytorch#153238), so we flatten to 1D and reshape back in prepare_model_inputs | ||||||
| num_pos_components = curr_pos_ids.shape[0] | ||||||
| valid_ids = curr_pos_ids[:, curr_mask].T.contiguous().flatten() # (valid_len * num_components,) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation performs multiple intermediate operations and an explicit contiguous copy. Using
Suggested change
|
||||||
| position_ids_list.append(valid_ids) | ||||||
| position_ids_nested = torch.nested.as_nested_tensor(position_ids_list, layout=torch.jagged) | ||||||
| if num_pos_components > 0: | ||||||
| tu.assign_non_tensor_data(data, "num_pos_components", num_pos_components) | ||||||
|
|
||||||
| data["input_ids"] = input_ids_nested | ||||||
| data["position_ids"] = position_ids_nested | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The explicit calculation of
total_nnzis redundant here. Using-1in theviewmethod is more idiomatic and robust, as it allows PyTorch to automatically infer the dimension size while ensuring the total number of elements is compatible withnum_pos_components. This also makes the code slightly cleaner by removing an unnecessary intermediate variable.