Files
Xuanwo 1658c8d762 feat(python)!: support vector columns in permutation torch formats (#5131)
BREAKING CHANGE: `Permutation.with_format("torch_row")` now uses PyTorch
dtype promotion instead of NumPy promotion. Mixed numeric columns can
change dtype, for example int64 and float32 now produce float32 instead
of float64. Cast columns before formatting when the old dtype matters.
Nullable scalar integer and floating columns still produce NaN; nulls in
nested fixed-size-list or tensor columns, and in bfloat16 columns, are
rejected.

## Problem

`Permutation.with_format("torch")`, `"torch_col"`, and `"torch_row"`
fail when the selected columns include a vector (`FixedSizeList`)
column, which is the column training workloads need. The conversion also
has to preserve Arrow slice offsets, logical `FixedShapeTensor` axes,
and visible nested nulls instead of flattening over them.

## Behavior

- Add `with_format("torch_dict")`: a dict of per-column `(B, *S)`
tensors, usable directly with a `DataLoader` identity collate. Tensors
share Arrow storage where the layout allows.
- Extend `torch` per-row dicts to nested `FixedSizeList` and
`FixedShapeTensor`; PyTorch's default collate stacks them back into `(B,
*S)` tensors.
- Preserve `torch_col` as `(C, B, *S)`, including a zero-copy
`unsqueeze(0)` view for one column.
- Preserve `torch_row` as per-row `(C, *S)` tensors, including `(1,)`
for one scalar column, with `torch.promote_types` for mixed numeric
dtypes. Nullable scalar integer and floating columns keep their previous
NaN behavior; nested fixed-size-list and tensor nulls are rejected.
- `torch_col` and `torch_row` reject heterogeneous sample shapes with
the field names, shapes, and guidance to `torch_dict`.
- Support nested fixed-size lists, `FixedShapeTensor` logical axis
permutations, boolean/integer/floating leaves, empty batches, and
nonzero slice offsets. `torch_dict`, `torch`, and `torch_col` reject
visible nulls.
- NumPy still returns one ndarray. Its pandas conversion may copy, may
promote mixed numeric columns to a common numeric dtype (for example
int64 and float32 to float64), and produces an object array for nested
or incompatible columns.
- `lance.bfloat16` is accepted only for a `FixedSizeBinary(2)` leaf
carrying the `ARROW:extension:name` metadata; offsets/length and byte
order are handled before viewing as `torch.bfloat16`. Unmarked binary
columns are rejected.

This is Python-only and does not change persistence, pickling, or public
method signatures.

## Validation

- `pyarrow 26.0.1`, `torch 2.15.0+cu132`, Python 3.12: `test_torch.py`
23 passed; `test_permutation.py` 84 passed.
- Minimum `pyarrow 16.0.0`, `torch 2.0.1+cpu`, Python 3.11: the nullable
scalar, mixed-dtype, null-rejection, and NumPy tests passed (4 focused
tests). The new NumPy test skips without pandas via
`pytest.importorskip`.
- The pre-existing fork DataLoader tests are flaky only when the full
`test_torch.py` file runs on the minimum stack; each passes when run
alone. Their failure is on the default Python format path and does not
exercise the changed format conversions.
- `ruff format`, `ruff check`, and `git diff --check` pass.
2026-11-21 02:21:51 +08:00
..