Skip to content

torch.cat/concat/concatenate: support dim=None (flatten before concat)#182204

Open
senekis316 wants to merge 1 commit intopytorch:mainfrom
senekis316:fix/cat-dim-none
Open

torch.cat/concat/concatenate: support dim=None (flatten before concat)#182204
senekis316 wants to merge 1 commit intopytorch:mainfrom
senekis316:fix/cat-dim-none

Conversation

@senekis316
Copy link
Copy Markdown
Contributor

Fixes #70925

Summary

The Python Array API Standard specifies that concat(tensors, dim=None)
should first flatten all tensors then concatenate them, consistent with
numpy.concatenate(axis=None).

Previously, torch.cat([t1, t2], dim=None) raised a RuntimeError.
After this PR:

t1 = torch.tensor([[1, 2], [3, 4]])
t2 = torch.tensor([[5, 6]])
torch.cat([t1, t2], dim=None)
# tensor([1, 2, 3, 4, 5, 6])

Changes
torch/functional.py: Added Python wrapper for cat with dim=None support; concat and concatenate now point to the same wrapper
torch/_refs/__init__.py: Updated _refs.cat (FakeTensor path) with the same logic
common_methods_invocations.py: Added test cases for dim=None

…first

Implements the Python Array API Standard behavior where dim=None causes
all input tensors to be flattened before concatenation, consistent with
numpy.concatenate(axis=None).

Fixes pytorch#70925
@senekis316 senekis316 requested a review from mruberry as a code owner May 2, 2026 04:24
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 2, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/182204

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 2, 2026

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

allow dim=None in concat

3 participants