Separate packed features
Divide a feature or embedding dimension into separate projections.
PyTorch · Function
Splits a tensor along a chosen dimension into a requested number of chunks. Outputs are views and may be fewer than requested when the division is not possible.
torch.chunk divides a tensor along a selected dimension and returns tuple elements that are views of the original tensor. It aims for the requested number of pieces, distributing elements as evenly as possible, but can return fewer pieces when the requested division cannot be performed.
torch.chunk(input: Tensor, chunks: int, dim: int = 0) → Tuple[Tensor, ...]| Name | Type | Description |
|---|---|---|
input | Tensor | Tensor to split. |
chunks | int | Number of chunks to return. |
dim | int = 0 | Dimension along which to split the tensor; defaults to 0. |
Tuple[Tensor, ...]
import torch
x = torch.arange(13)
parts = torch.chunk(x, chunks=6)
print(len(parts))
print([part.tolist() for part in parts])This creates six requested chunks along dimension 0 and prints their count and contents; because 13 cannot be divided evenly into six parts, the final chunk is smaller.
Divide a feature or embedding dimension into separate projections.
Partition a tensor for independent processing across branches.
Split a batch or sequence dimension into manageable pieces.
torch.chunk(input, chunks, dim=0) splits input along dim and returns a tuple of tensors. The returned pieces are views, and dim defaults to 0.
Follow-up: How would you guarantee exactly the requested number of outputs? Use torch.tensor_split(), which the documentation identifies as always returning exactly that number.
When the selected dimension divides evenly by chunks, all outputs have the same size. Otherwise, the pieces are generally balanced, with the final piece smaller.
Follow-up: What happens if the requested division is not possible? The function may return fewer chunks than requested, so code should not blindly unpack the expected count.
The chunks argument specifies the requested number of chunks, while dim selects the dimension to split and defaults to zero. The input argument is the tensor being divided.
Follow-up: How can the operation be applied to the last dimension? Pass dim=-1, provided that dimension exists for the input tensor.
torch.tensor_split()torch.Tensor.chunk