structured-data-models 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sdm/__init__.py +58 -0
- sdm/_constants.py +6 -0
- sdm/_inference.py +42 -0
- sdm/_kernels/__init__.py +10 -0
- sdm/_kernels/rmsnorm_cast.py +92 -0
- sdm/_kernels/segment_multi_reduce.py +141 -0
- sdm/_kernels/triton/__init__.py +7 -0
- sdm/_kernels/triton/rmsnorm_cast.py +136 -0
- sdm/_kernels/triton/segment_multi_reduce.py +173 -0
- sdm/_memory.py +34 -0
- sdm/_warnings.py +29 -0
- sdm/cache.py +163 -0
- sdm/ensemble.py +501 -0
- sdm/evaluation/__init__.py +11 -0
- sdm/evaluation/classification.py +205 -0
- sdm/explain/__init__.py +12 -0
- sdm/explain/base.py +123 -0
- sdm/explain/gradient.py +112 -0
- sdm/models/__init__.py +20 -0
- sdm/models/_huggingface.py +58 -0
- sdm/models/base.py +966 -0
- sdm/models/callback.py +106 -0
- sdm/models/ecoc.py +197 -0
- sdm/models/kumo/__init__.py +13 -0
- sdm/models/kumo/relational/NOTICE +5 -0
- sdm/models/kumo/relational/__init__.py +11 -0
- sdm/models/kumo/relational/graph.py +101 -0
- sdm/models/kumo/relational/invariant_gnn.py +122 -0
- sdm/models/kumo/relational/model.py +602 -0
- sdm/models/kumo/relational/recipe.py +18 -0
- sdm/models/kumo/relational/task.py +113 -0
- sdm/models/kumo/tabular/__init__.py +17 -0
- sdm/models/kumo/tabular/batching.py +139 -0
- sdm/models/kumo/tabular/block.py +100 -0
- sdm/models/kumo/tabular/cell_embedding.py +85 -0
- sdm/models/kumo/tabular/icl.py +136 -0
- sdm/models/kumo/tabular/model.py +299 -0
- sdm/models/kumo/tabular/recipe.py +71 -0
- sdm/models/kumo/tabular/row_embedding.py +214 -0
- sdm/models/tabfm/LICENSE +209 -0
- sdm/models/tabfm/__init__.py +11 -0
- sdm/models/tabfm/block.py +94 -0
- sdm/models/tabfm/cell_embedding.py +219 -0
- sdm/models/tabfm/ckpt.py +242 -0
- sdm/models/tabfm/icl.py +107 -0
- sdm/models/tabfm/model.py +261 -0
- sdm/models/tabfm/recipe.py +46 -0
- sdm/models/tabfm/row_embedding.py +241 -0
- sdm/models/tabiclv2/LICENSE +33 -0
- sdm/models/tabiclv2/__init__.py +11 -0
- sdm/models/tabiclv2/block.py +81 -0
- sdm/models/tabiclv2/ckpt.py +134 -0
- sdm/models/tabiclv2/icl.py +420 -0
- sdm/models/tabiclv2/model.py +293 -0
- sdm/models/tabiclv2/recipe.py +46 -0
- sdm/models/tabiclv2/row_embedding.py +254 -0
- sdm/nn/__init__.py +28 -0
- sdm/nn/_buffer.py +91 -0
- sdm/nn/attention.py +825 -0
- sdm/nn/glu.py +78 -0
- sdm/nn/linear.py +46 -0
- sdm/nn/rms_norm.py +59 -0
- sdm/nn/rope.py +107 -0
- sdm/nn/scaling.py +240 -0
- sdm/nn/set_transformer.py +162 -0
- sdm/nn/softplus.py +65 -0
- sdm/processing/__init__.py +96 -0
- sdm/processing/base.py +254 -0
- sdm/processing/categorical/__init__.py +16 -0
- sdm/processing/categorical/_categorical.py +60 -0
- sdm/processing/categorical/align.py +540 -0
- sdm/processing/categorical/count.py +121 -0
- sdm/processing/categorical/impute.py +107 -0
- sdm/processing/categorical/shuffle.py +204 -0
- sdm/processing/common/__init__.py +32 -0
- sdm/processing/common/callable.py +29 -0
- sdm/processing/common/choice.py +185 -0
- sdm/processing/common/drop.py +28 -0
- sdm/processing/common/ensemble.py +187 -0
- sdm/processing/common/identity.py +18 -0
- sdm/processing/common/select.py +107 -0
- sdm/processing/common/sequential.py +158 -0
- sdm/processing/common/shuffle.py +195 -0
- sdm/processing/common/stype.py +199 -0
- sdm/processing/common/table.py +120 -0
- sdm/processing/common/task.py +135 -0
- sdm/processing/common/to_numerical.py +60 -0
- sdm/processing/datetime/__init__.py +8 -0
- sdm/processing/datetime/calendar.py +196 -0
- sdm/processing/ensemble.py +203 -0
- sdm/processing/execution.py +418 -0
- sdm/processing/numerical/__init__.py +38 -0
- sdm/processing/numerical/_stats.py +29 -0
- sdm/processing/numerical/cast.py +33 -0
- sdm/processing/numerical/clip.py +48 -0
- sdm/processing/numerical/clip_soft.py +54 -0
- sdm/processing/numerical/constant.py +235 -0
- sdm/processing/numerical/flip_sign.py +78 -0
- sdm/processing/numerical/impute.py +52 -0
- sdm/processing/numerical/pca.py +62 -0
- sdm/processing/numerical/power.py +325 -0
- sdm/processing/numerical/quantile.py +306 -0
- sdm/processing/numerical/quantile_clip.py +63 -0
- sdm/processing/numerical/random_projection.py +86 -0
- sdm/processing/numerical/rank_gaussian.py +168 -0
- sdm/processing/numerical/robust_scale.py +88 -0
- sdm/processing/numerical/sigma_clip.py +123 -0
- sdm/processing/numerical/standardize.py +71 -0
- sdm/processing/output/__init__.py +14 -0
- sdm/processing/output/reduce.py +86 -0
- sdm/processing/output/softmax.py +48 -0
- sdm/processing/output/sort.py +16 -0
- sdm/processing/recipe.py +154 -0
- sdm/processing/text/__init__.py +12 -0
- sdm/processing/text/sentence_transformer.py +451 -0
- sdm/processing/text/tfidf.py +478 -0
- sdm/relational/__init__.py +16 -0
- sdm/relational/backend/__init__.py +12 -0
- sdm/relational/backend/_cugraph.py +546 -0
- sdm/relational/backend/_pyg_lib.py +175 -0
- sdm/relational/data.py +393 -0
- sdm/relational/join.py +173 -0
- sdm/relational/sampler.py +259 -0
- sdm/relational/task.py +465 -0
- sdm/stype.py +396 -0
- sdm/task.py +53 -0
- sdm/tensor/__init__.py +20 -0
- sdm/tensor/categorical.py +953 -0
- sdm/tensor/columnar.py +1127 -0
- sdm/tensor/io/__init__.py +22 -0
- sdm/tensor/io/arrow.py +120 -0
- sdm/tensor/io/cudf.py +47 -0
- sdm/tensor/mixin.py +124 -0
- sdm/tensor/nullable.py +963 -0
- sdm/tensor/string.py +526 -0
- sdm/tensor/table.py +2035 -0
- sdm/tensor/var_len.py +1486 -0
- sdm/testing/__init__.py +14 -0
- sdm/testing/decorators.py +61 -0
- sdm/testing/memory.py +92 -0
- structured_data_models-0.2.0.dist-info/METADATA +142 -0
- structured_data_models-0.2.0.dist-info/RECORD +152 -0
- structured_data_models-0.2.0.dist-info/WHEEL +5 -0
- structured_data_models-0.2.0.dist-info/licenses/LICENSE +204 -0
- structured_data_models-0.2.0.dist-info/licenses/THIRD_PARTY_LICENSES.md +40 -0
- structured_data_models-0.2.0.dist-info/licenses/sdm/models/kumo/relational/NOTICE +5 -0
- structured_data_models-0.2.0.dist-info/licenses/sdm/models/tabfm/LICENSE +209 -0
- structured_data_models-0.2.0.dist-info/licenses/sdm/models/tabiclv2/LICENSE +33 -0
- structured_data_models-0.2.0.dist-info/licenses/third_party/contributor-covenant/LICENSE +20 -0
- structured_data_models-0.2.0.dist-info/licenses/third_party/pytorch/LICENSE +84 -0
- structured_data_models-0.2.0.dist-info/licenses/third_party/tabpfn-extensions/LICENSE +202 -0
- structured_data_models-0.2.0.dist-info/top_level.txt +1 -0
sdm/tensor/columnar.py
ADDED
|
@@ -0,0 +1,1127 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import functools
|
|
7
|
+
import math
|
|
8
|
+
from collections.abc import Callable, Sequence
|
|
9
|
+
from itertools import chain
|
|
10
|
+
from typing import TYPE_CHECKING, Any, ClassVar, Self, SupportsIndex, cast
|
|
11
|
+
|
|
12
|
+
import pyarrow as pa
|
|
13
|
+
import torch
|
|
14
|
+
from torch import Tensor
|
|
15
|
+
from typing_extensions import override
|
|
16
|
+
|
|
17
|
+
from sdm.tensor import NullableTensor, StringTensor, VarLenTensor
|
|
18
|
+
from sdm.tensor.io import arrow_as_tensor, to_arrow, to_cudf
|
|
19
|
+
from sdm.tensor.mixin import _resolve_device
|
|
20
|
+
|
|
21
|
+
if TYPE_CHECKING:
|
|
22
|
+
import cudf
|
|
23
|
+
|
|
24
|
+
aten = torch.ops.aten
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def preserve_view_inference_mode(fn: Callable) -> Callable:
|
|
28
|
+
r"""Preserve input inference state for tensor view operations."""
|
|
29
|
+
|
|
30
|
+
@functools.wraps(fn)
|
|
31
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
32
|
+
with torch.inference_mode(args[0].is_inference()):
|
|
33
|
+
return fn(*args, **kwargs)
|
|
34
|
+
|
|
35
|
+
return wrapper
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ColumnarTensor(Tensor):
|
|
39
|
+
r"""A :class:`torch.Tensor` for column-wise heterogeneous data.
|
|
40
|
+
|
|
41
|
+
A :class:`ColumnarTensor` exposes a tensor-centric interface for columnar
|
|
42
|
+
data with shape ``[..., C]``.
|
|
43
|
+
Each of the ``C`` columns is stored independently and may use a different
|
|
44
|
+
tensor subclass or dtype.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
columns: Per-column values.
|
|
48
|
+
size: The shape of the tensor ``[...]``.
|
|
49
|
+
device: The device.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
HANDLED_FUNCTIONS: ClassVar[
|
|
53
|
+
dict[Callable[..., Any], Callable[..., Any]]
|
|
54
|
+
] = {}
|
|
55
|
+
|
|
56
|
+
_columns: tuple[Tensor, ...]
|
|
57
|
+
|
|
58
|
+
# Route tensor operations through `__torch_dispatch__` only.
|
|
59
|
+
__torch_function__ = torch._C._disabled_torch_function_impl # type: ignore
|
|
60
|
+
|
|
61
|
+
# Constructors ############################################################
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
columns: Sequence[Tensor],
|
|
66
|
+
size: Sequence[int] | None = None,
|
|
67
|
+
device: torch.device | str | None = None,
|
|
68
|
+
) -> None:
|
|
69
|
+
pass
|
|
70
|
+
|
|
71
|
+
def __new__(
|
|
72
|
+
cls,
|
|
73
|
+
columns: Sequence[Tensor],
|
|
74
|
+
size: Sequence[int] | None = None,
|
|
75
|
+
device: torch.device | str | None = None,
|
|
76
|
+
) -> Self:
|
|
77
|
+
r"""Create a tensor wrapper."""
|
|
78
|
+
# Avoid a circular import through `sdm.tensor`.
|
|
79
|
+
from sdm.tensor import ( # noqa: PLC0415
|
|
80
|
+
CategoricalTensor,
|
|
81
|
+
TableTensor,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
if size is not None and len(size) == 0:
|
|
85
|
+
raise ValueError("Expected 'size' to be non-empty")
|
|
86
|
+
|
|
87
|
+
for i, column in enumerate(columns):
|
|
88
|
+
if isinstance(
|
|
89
|
+
column, CategoricalTensor | ColumnarTensor | TableTensor
|
|
90
|
+
):
|
|
91
|
+
raise TypeError(
|
|
92
|
+
f"Expected value {i} in {cls.__name__!r} to be a single "
|
|
93
|
+
f"column tensor (got {column.__class__.__name__!r})"
|
|
94
|
+
)
|
|
95
|
+
columns = tuple(columns)
|
|
96
|
+
size = tuple(size) if size is not None else size
|
|
97
|
+
device = _resolve_device(device)
|
|
98
|
+
|
|
99
|
+
for i, column in enumerate(columns):
|
|
100
|
+
size = tuple(column.size()) if size is None else size
|
|
101
|
+
device = column.device if device is None else device
|
|
102
|
+
|
|
103
|
+
if column.size() != size:
|
|
104
|
+
raise ValueError(
|
|
105
|
+
f"Expected value {i} in {cls.__name__!r} to have size "
|
|
106
|
+
f"{size} (got {tuple(column.size())})"
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
if column.device != device:
|
|
110
|
+
raise ValueError(
|
|
111
|
+
f"Expected value {i} in {cls.__name__!r} to be on "
|
|
112
|
+
f"device '{device}' (got '{column.device}')"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
if size is None:
|
|
116
|
+
raise ValueError(
|
|
117
|
+
"Expected 'size' to be given for zero columnar data"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
out = Tensor._make_wrapper_subclass(
|
|
121
|
+
cls,
|
|
122
|
+
size=(*size, len(columns)),
|
|
123
|
+
dtype=torch.uint8, # NOTE Do not use.
|
|
124
|
+
device=device,
|
|
125
|
+
requires_grad=False,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
out._columns = columns
|
|
129
|
+
|
|
130
|
+
return out
|
|
131
|
+
|
|
132
|
+
@classmethod
|
|
133
|
+
def from_arrow(
|
|
134
|
+
cls,
|
|
135
|
+
array: pa.Array | pa.ChunkedArray,
|
|
136
|
+
*,
|
|
137
|
+
device: torch.device | str | None = None,
|
|
138
|
+
) -> Self:
|
|
139
|
+
r"""Create tensor from a :class:`pyarrow.Array`.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
array: The :class:`pyarrow.Array` or
|
|
143
|
+
:class:`pyarrow.ChunkedArray`.
|
|
144
|
+
device: The device.
|
|
145
|
+
"""
|
|
146
|
+
device = torch.device("cpu" if device is None else device)
|
|
147
|
+
|
|
148
|
+
is_string = pa.types.is_string(array.type)
|
|
149
|
+
is_large_string = pa.types.is_large_string(array.type)
|
|
150
|
+
if is_string or is_large_string:
|
|
151
|
+
column = StringTensor.from_arrow(array, device=device)
|
|
152
|
+
elif array.null_count > 0 and (
|
|
153
|
+
pa.types.is_integer(array.type) or pa.types.is_boolean(array.type)
|
|
154
|
+
):
|
|
155
|
+
column = NullableTensor.from_arrow(array, device=device)
|
|
156
|
+
else:
|
|
157
|
+
column = arrow_as_tensor(array, device=device)
|
|
158
|
+
|
|
159
|
+
return cls(columns=(column,), device=device)
|
|
160
|
+
|
|
161
|
+
@classmethod
|
|
162
|
+
def from_cudf(
|
|
163
|
+
cls,
|
|
164
|
+
ser: cudf.Series | cudf.Index,
|
|
165
|
+
*,
|
|
166
|
+
device: torch.device | str | None = None,
|
|
167
|
+
) -> Self:
|
|
168
|
+
r"""Create tensor from a :class:`cudf.Series`.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
ser: The :class:`cudf.Series` or :class:`cudf.Index`.
|
|
172
|
+
device: The device.
|
|
173
|
+
"""
|
|
174
|
+
from cudf.api.types import (
|
|
175
|
+
is_bool_dtype,
|
|
176
|
+
is_integer_dtype,
|
|
177
|
+
is_string_dtype,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
if is_string_dtype(ser.dtype):
|
|
181
|
+
column = StringTensor.from_cudf(ser, device=device)
|
|
182
|
+
elif ser._column.null_count > 0 and (
|
|
183
|
+
is_integer_dtype(ser.dtype) or is_bool_dtype(ser.dtype)
|
|
184
|
+
):
|
|
185
|
+
column = NullableTensor.from_cudf(ser, device=device)
|
|
186
|
+
else:
|
|
187
|
+
if ser._column.null_count > 0:
|
|
188
|
+
ser = ser.fillna(float("nan"))
|
|
189
|
+
column = torch.from_dlpack(ser.to_dlpack()).to(device)
|
|
190
|
+
|
|
191
|
+
return cls(columns=(column,), device=device)
|
|
192
|
+
|
|
193
|
+
def to_arrow(self, names: Sequence[str] | None = None) -> pa.Table:
|
|
194
|
+
r"""Convert this tensor to a two-dimensional :class:`pyarrow.Table`.
|
|
195
|
+
|
|
196
|
+
Args:
|
|
197
|
+
names: The column names.
|
|
198
|
+
"""
|
|
199
|
+
if names is None:
|
|
200
|
+
names = tuple(str(i) for i in range(self.size(-1)))
|
|
201
|
+
elif len(names) != self.size(-1):
|
|
202
|
+
raise ValueError(
|
|
203
|
+
f"Expected 'names' to contain {self.size(-1)} entries "
|
|
204
|
+
f"(got {len(names)})"
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
return pa.Table.from_arrays(
|
|
208
|
+
arrays=[
|
|
209
|
+
column.to_arrow()
|
|
210
|
+
if isinstance(column, VarLenTensor | NullableTensor)
|
|
211
|
+
else to_arrow(column)
|
|
212
|
+
for column in self.unbind(-1)
|
|
213
|
+
],
|
|
214
|
+
names=names,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
def to_cudf(self, names: Sequence[str] | None = None) -> cudf.DataFrame:
|
|
218
|
+
r"""Convert this tensor to a two-dimensional :class:`cudf.DataFrame`.
|
|
219
|
+
|
|
220
|
+
Args:
|
|
221
|
+
names: The column names.
|
|
222
|
+
"""
|
|
223
|
+
import cudf
|
|
224
|
+
|
|
225
|
+
if names is None:
|
|
226
|
+
names = tuple(str(i) for i in range(self.size(-1)))
|
|
227
|
+
elif len(names) != self.size(-1):
|
|
228
|
+
raise ValueError(
|
|
229
|
+
f"Expected 'names' to contain {self.size(-1)} entries "
|
|
230
|
+
f"(got {len(names)})"
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
return cudf.DataFrame(
|
|
234
|
+
{
|
|
235
|
+
name: column.to_cudf()
|
|
236
|
+
if isinstance(column, StringTensor | NullableTensor)
|
|
237
|
+
else to_cudf(column)
|
|
238
|
+
for name, column in zip(names, self.unbind(-1))
|
|
239
|
+
},
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# Decorators ##############################################################
|
|
243
|
+
|
|
244
|
+
@classmethod
|
|
245
|
+
def implements(
|
|
246
|
+
cls,
|
|
247
|
+
torch_function: Callable[..., Any],
|
|
248
|
+
) -> Callable[..., Any]:
|
|
249
|
+
r"""Register a ``__torch_dispatch__`` implementation.
|
|
250
|
+
|
|
251
|
+
See PyTorch's
|
|
252
|
+
:ref:`calling convention <torch-dispatch-calling-convention>`.
|
|
253
|
+
"""
|
|
254
|
+
if "HANDLED_FUNCTIONS" not in cls.__dict__:
|
|
255
|
+
cls.HANDLED_FUNCTIONS = cls.HANDLED_FUNCTIONS.copy()
|
|
256
|
+
|
|
257
|
+
def decorator(my_function: Callable[..., Any]) -> Callable[..., Any]:
|
|
258
|
+
cls.HANDLED_FUNCTIONS[torch_function] = my_function
|
|
259
|
+
return my_function
|
|
260
|
+
|
|
261
|
+
return decorator
|
|
262
|
+
|
|
263
|
+
# PyTorch/Python builtins #################################################
|
|
264
|
+
|
|
265
|
+
def __reduce_ex__(self, proto: SupportsIndex) -> Any:
|
|
266
|
+
args = (self._columns, tuple(self.size())[:-1], self.device)
|
|
267
|
+
return (self.__class__, args)
|
|
268
|
+
|
|
269
|
+
@classmethod
|
|
270
|
+
def __torch_dispatch__( # type: ignore
|
|
271
|
+
cls,
|
|
272
|
+
func: Callable[..., Any],
|
|
273
|
+
types: tuple[type[Any], ...],
|
|
274
|
+
args: tuple[Any, ...] = (),
|
|
275
|
+
kwargs: dict[str, Any] | None = None,
|
|
276
|
+
) -> Any:
|
|
277
|
+
if (handler := cls.HANDLED_FUNCTIONS.get(func)) is not None:
|
|
278
|
+
return handler(*args, **(kwargs or {}))
|
|
279
|
+
|
|
280
|
+
raise NotImplementedError(
|
|
281
|
+
f"'{func}' is not supported for {cls.__name__!r}"
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
@override
|
|
285
|
+
def is_shared(self) -> bool:
|
|
286
|
+
return all(column.is_shared() for column in self._columns)
|
|
287
|
+
|
|
288
|
+
@override
|
|
289
|
+
def share_memory_(self) -> Self:
|
|
290
|
+
for column in self._columns:
|
|
291
|
+
column.share_memory_()
|
|
292
|
+
return self
|
|
293
|
+
|
|
294
|
+
@override
|
|
295
|
+
def is_contiguous(
|
|
296
|
+
self,
|
|
297
|
+
memory_format: torch.memory_format = torch.contiguous_format,
|
|
298
|
+
) -> bool:
|
|
299
|
+
return all(
|
|
300
|
+
column.is_contiguous(memory_format=memory_format)
|
|
301
|
+
for column in self._columns
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
@override
|
|
305
|
+
def contiguous(
|
|
306
|
+
self,
|
|
307
|
+
memory_format: torch.memory_format = torch.contiguous_format,
|
|
308
|
+
) -> Self:
|
|
309
|
+
if self.is_contiguous(memory_format=memory_format):
|
|
310
|
+
return self
|
|
311
|
+
return _contiguous(self, memory_format=memory_format)
|
|
312
|
+
|
|
313
|
+
@override
|
|
314
|
+
def tolist(self) -> Any:
|
|
315
|
+
def columns_to_rows(
|
|
316
|
+
columns: Sequence[Any],
|
|
317
|
+
size: tuple[int, ...],
|
|
318
|
+
) -> Any:
|
|
319
|
+
if len(size) == 0:
|
|
320
|
+
return list(columns)
|
|
321
|
+
|
|
322
|
+
return [
|
|
323
|
+
columns_to_rows(
|
|
324
|
+
columns=[column[i] for column in columns],
|
|
325
|
+
size=size[1:],
|
|
326
|
+
)
|
|
327
|
+
for i in range(size[0])
|
|
328
|
+
]
|
|
329
|
+
|
|
330
|
+
columns = [column.tolist() for column in self._columns]
|
|
331
|
+
return columns_to_rows(columns, size=tuple(self.size()[:-1]))
|
|
332
|
+
|
|
333
|
+
def __repr__(self, *, tensor_contents: Any = None) -> str:
|
|
334
|
+
out = f"{self.__class__.__name__}("
|
|
335
|
+
out += f"size={tuple(self.size())}"
|
|
336
|
+
if not self.is_cpu:
|
|
337
|
+
out += f", device={self.device}"
|
|
338
|
+
out += ")"
|
|
339
|
+
return out
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
@ColumnarTensor.implements(aten.alias.default)
|
|
343
|
+
@preserve_view_inference_mode
|
|
344
|
+
def _alias(inp: ColumnarTensor) -> ColumnarTensor:
|
|
345
|
+
return inp.__class__(
|
|
346
|
+
columns=inp._columns,
|
|
347
|
+
size=inp.size()[:-1],
|
|
348
|
+
device=inp.device,
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
@ColumnarTensor.implements(aten.record_stream.default)
|
|
353
|
+
def _record_stream(inp: ColumnarTensor, stream: torch.Stream) -> None:
|
|
354
|
+
for column in inp._columns:
|
|
355
|
+
column.record_stream(stream)
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
@ColumnarTensor.implements(aten.to.dtype_layout)
|
|
359
|
+
def _to_dtype_layout(
|
|
360
|
+
inp: ColumnarTensor,
|
|
361
|
+
*,
|
|
362
|
+
dtype: torch.dtype | None = None,
|
|
363
|
+
layout: torch.layout | None = None,
|
|
364
|
+
device: torch.device | str | None = None,
|
|
365
|
+
pin_memory: bool | None = None,
|
|
366
|
+
non_blocking: bool = False,
|
|
367
|
+
copy: bool = False,
|
|
368
|
+
memory_format: torch.memory_format | None = None,
|
|
369
|
+
) -> ColumnarTensor:
|
|
370
|
+
|
|
371
|
+
if dtype is not None and dtype != inp.dtype:
|
|
372
|
+
raise TypeError(
|
|
373
|
+
f"Can't convert {inp.__class__.__name__!r} to dtype '{dtype}'"
|
|
374
|
+
)
|
|
375
|
+
if device is None:
|
|
376
|
+
device = inp.device
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
not copy
|
|
380
|
+
and torch.device(device) == inp.device
|
|
381
|
+
and (layout is None or layout == inp.layout)
|
|
382
|
+
and (
|
|
383
|
+
memory_format is None
|
|
384
|
+
or memory_format == torch.preserve_format
|
|
385
|
+
or (
|
|
386
|
+
memory_format == torch.contiguous_format
|
|
387
|
+
and inp.is_contiguous()
|
|
388
|
+
)
|
|
389
|
+
)
|
|
390
|
+
):
|
|
391
|
+
return inp
|
|
392
|
+
|
|
393
|
+
return inp.__class__(
|
|
394
|
+
columns=[
|
|
395
|
+
aten.to.dtype_layout(
|
|
396
|
+
column,
|
|
397
|
+
dtype=None,
|
|
398
|
+
layout=layout,
|
|
399
|
+
device=device,
|
|
400
|
+
pin_memory=pin_memory,
|
|
401
|
+
non_blocking=non_blocking,
|
|
402
|
+
copy=copy,
|
|
403
|
+
memory_format=memory_format,
|
|
404
|
+
)
|
|
405
|
+
for column in inp._columns
|
|
406
|
+
],
|
|
407
|
+
size=inp.size()[:-1],
|
|
408
|
+
device=device,
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
@ColumnarTensor.implements(aten.to.dtype)
|
|
413
|
+
def _to_dtype(
|
|
414
|
+
inp: ColumnarTensor,
|
|
415
|
+
dtype: torch.dtype,
|
|
416
|
+
non_blocking: bool = False,
|
|
417
|
+
copy: bool = False,
|
|
418
|
+
memory_format: torch.memory_format | None = None,
|
|
419
|
+
) -> ColumnarTensor:
|
|
420
|
+
return _to_dtype_layout(
|
|
421
|
+
inp,
|
|
422
|
+
dtype=dtype,
|
|
423
|
+
non_blocking=non_blocking,
|
|
424
|
+
copy=copy,
|
|
425
|
+
memory_format=memory_format,
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
@ColumnarTensor.implements(aten.to.device)
|
|
430
|
+
def _to_device(
|
|
431
|
+
inp: ColumnarTensor,
|
|
432
|
+
device: torch.device,
|
|
433
|
+
dtype: torch.dtype,
|
|
434
|
+
non_blocking: bool = False,
|
|
435
|
+
copy: bool = False,
|
|
436
|
+
memory_format: torch.memory_format | None = None,
|
|
437
|
+
) -> ColumnarTensor:
|
|
438
|
+
return _to_dtype_layout(
|
|
439
|
+
inp,
|
|
440
|
+
dtype=dtype,
|
|
441
|
+
device=device,
|
|
442
|
+
non_blocking=non_blocking,
|
|
443
|
+
copy=copy,
|
|
444
|
+
memory_format=memory_format,
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
@ColumnarTensor.implements(aten.to.other)
|
|
449
|
+
def _to_other(
|
|
450
|
+
inp: ColumnarTensor,
|
|
451
|
+
other: Tensor,
|
|
452
|
+
non_blocking: bool = False,
|
|
453
|
+
copy: bool = False,
|
|
454
|
+
memory_format: torch.memory_format | None = None,
|
|
455
|
+
) -> ColumnarTensor:
|
|
456
|
+
return _to_dtype_layout(
|
|
457
|
+
inp,
|
|
458
|
+
dtype=other.dtype,
|
|
459
|
+
layout=other.layout,
|
|
460
|
+
device=other.device,
|
|
461
|
+
non_blocking=non_blocking,
|
|
462
|
+
copy=copy,
|
|
463
|
+
memory_format=memory_format,
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
@ColumnarTensor.implements(aten._to_copy.default)
|
|
468
|
+
def _to_copy(
|
|
469
|
+
inp: ColumnarTensor,
|
|
470
|
+
*,
|
|
471
|
+
dtype: torch.dtype | None = None,
|
|
472
|
+
layout: torch.layout | None = None,
|
|
473
|
+
device: torch.device | str | None = None,
|
|
474
|
+
pin_memory: bool = False, # Ignored by PyTorch.
|
|
475
|
+
non_blocking: bool = False,
|
|
476
|
+
memory_format: torch.memory_format | None = None,
|
|
477
|
+
) -> ColumnarTensor:
|
|
478
|
+
return _to_dtype_layout(
|
|
479
|
+
inp,
|
|
480
|
+
dtype=dtype,
|
|
481
|
+
layout=layout,
|
|
482
|
+
device=device,
|
|
483
|
+
pin_memory=pin_memory,
|
|
484
|
+
non_blocking=non_blocking,
|
|
485
|
+
copy=True,
|
|
486
|
+
memory_format=memory_format,
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
@ColumnarTensor.implements(aten.clone.default)
|
|
491
|
+
def _clone(
|
|
492
|
+
inp: ColumnarTensor,
|
|
493
|
+
*,
|
|
494
|
+
memory_format: torch.memory_format | None = None,
|
|
495
|
+
) -> ColumnarTensor:
|
|
496
|
+
return _to_dtype_layout(inp, copy=True, memory_format=memory_format)
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
@ColumnarTensor.implements(aten.contiguous.default)
|
|
500
|
+
def _contiguous(
|
|
501
|
+
inp: ColumnarTensor,
|
|
502
|
+
*,
|
|
503
|
+
memory_format: torch.memory_format = torch.contiguous_format,
|
|
504
|
+
) -> ColumnarTensor:
|
|
505
|
+
return inp.__class__(
|
|
506
|
+
columns=[
|
|
507
|
+
column.contiguous(memory_format=memory_format)
|
|
508
|
+
for column in inp._columns
|
|
509
|
+
],
|
|
510
|
+
size=inp.size()[:-1],
|
|
511
|
+
device=inp.device,
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
@ColumnarTensor.implements(aten.is_pinned.default)
|
|
516
|
+
def _is_pinned(inp: ColumnarTensor) -> bool:
|
|
517
|
+
return all(column.is_pinned() for column in inp._columns)
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
@ColumnarTensor.implements(aten._pin_memory.default)
|
|
521
|
+
def _pin_memory(inp: ColumnarTensor) -> ColumnarTensor:
|
|
522
|
+
return inp.__class__(
|
|
523
|
+
columns=[column.pin_memory() for column in inp._columns],
|
|
524
|
+
size=inp.size()[:-1],
|
|
525
|
+
device=inp.device,
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
@ColumnarTensor.implements(aten.pin_memory.default)
|
|
530
|
+
def _pin_memory_composite(
|
|
531
|
+
inp: ColumnarTensor,
|
|
532
|
+
device: torch.device | None = None,
|
|
533
|
+
) -> ColumnarTensor:
|
|
534
|
+
if _is_pinned(inp):
|
|
535
|
+
return inp
|
|
536
|
+
return _pin_memory(inp)
|
|
537
|
+
|
|
538
|
+
|
|
539
|
+
@ColumnarTensor.implements(aten.equal.default)
|
|
540
|
+
def _equal(inp: ColumnarTensor, other: Tensor) -> bool:
|
|
541
|
+
if inp.__class__ is not other.__class__:
|
|
542
|
+
return False
|
|
543
|
+
if inp.size() != other.size():
|
|
544
|
+
return False
|
|
545
|
+
|
|
546
|
+
for column1, column2 in zip(inp._columns, other._columns):
|
|
547
|
+
if not column1.equal(column2):
|
|
548
|
+
return False
|
|
549
|
+
|
|
550
|
+
return True
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+
@ColumnarTensor.implements(aten.allclose.default)
|
|
554
|
+
def _allclose(
|
|
555
|
+
inp: ColumnarTensor,
|
|
556
|
+
other: Tensor,
|
|
557
|
+
rtol: float = 1e-05,
|
|
558
|
+
atol: float = 1e-08,
|
|
559
|
+
equal_nan: bool = False,
|
|
560
|
+
) -> bool:
|
|
561
|
+
if inp.__class__ is not other.__class__:
|
|
562
|
+
return False
|
|
563
|
+
if inp.size() != other.size():
|
|
564
|
+
return False
|
|
565
|
+
|
|
566
|
+
for column1, column2 in zip(inp._columns, other._columns):
|
|
567
|
+
if column1.is_floating_point() and column2.is_floating_point():
|
|
568
|
+
if not column1.allclose(column2, rtol, atol, equal_nan):
|
|
569
|
+
return False
|
|
570
|
+
elif not column1.equal(column2):
|
|
571
|
+
return False
|
|
572
|
+
|
|
573
|
+
return True
|
|
574
|
+
|
|
575
|
+
|
|
576
|
+
@ColumnarTensor.implements(aten.view.default)
|
|
577
|
+
@preserve_view_inference_mode
|
|
578
|
+
def _view(inp: ColumnarTensor, size: Sequence[int]) -> ColumnarTensor:
|
|
579
|
+
size = tuple(size)
|
|
580
|
+
for i, dim_size in enumerate(size):
|
|
581
|
+
if dim_size < -1:
|
|
582
|
+
raise RuntimeError(
|
|
583
|
+
f"Invalid shape dimension {dim_size} at index {i} of shape "
|
|
584
|
+
f"{size}"
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
if size.count(-1) > 1:
|
|
588
|
+
raise RuntimeError("Only one dimension can be inferred")
|
|
589
|
+
|
|
590
|
+
if -1 in size:
|
|
591
|
+
known = math.prod(dim_size for dim_size in size if dim_size != -1)
|
|
592
|
+
if known == 0:
|
|
593
|
+
raise RuntimeError(
|
|
594
|
+
f"Cannot reshape tensor of {inp.numel()} elements into "
|
|
595
|
+
f"shape {size} because the unspecified dimension size -1 can "
|
|
596
|
+
f"be any value and is ambiguous"
|
|
597
|
+
)
|
|
598
|
+
if inp.numel() % known != 0:
|
|
599
|
+
raise RuntimeError(
|
|
600
|
+
f"Shape {size} is invalid for input of size {inp.numel()}"
|
|
601
|
+
)
|
|
602
|
+
dim = size.index(-1)
|
|
603
|
+
size = (*size[:dim], inp.numel() // known, *size[dim + 1 :])
|
|
604
|
+
|
|
605
|
+
if len(size) == 0 or size[-1] != inp.size(-1):
|
|
606
|
+
_columns = "column" if inp.size(-1) == 1 else "columns"
|
|
607
|
+
raise RuntimeError(
|
|
608
|
+
f"Can't reshape {inp.__class__.__name__!r} with "
|
|
609
|
+
f"{inp.size(-1)} {_columns} into shape {size}"
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
return inp.__class__(
|
|
613
|
+
columns=[column.view(size[:-1]) for column in inp._columns],
|
|
614
|
+
size=size[:-1],
|
|
615
|
+
device=inp.device,
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
@ColumnarTensor.implements(aten._unsafe_view.default)
|
|
620
|
+
@preserve_view_inference_mode
|
|
621
|
+
def _unsafe_view(
|
|
622
|
+
inp: ColumnarTensor,
|
|
623
|
+
size: Sequence[int],
|
|
624
|
+
) -> ColumnarTensor:
|
|
625
|
+
return _view(inp, size)
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
@ColumnarTensor.implements(aten.reshape.default)
|
|
629
|
+
def _reshape(inp: ColumnarTensor, size: Sequence[int]) -> ColumnarTensor:
|
|
630
|
+
return cast(
|
|
631
|
+
ColumnarTensor,
|
|
632
|
+
aten.reshape.default.decompose(inp, size),
|
|
633
|
+
)
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
@ColumnarTensor.implements(aten.flatten.using_ints)
|
|
637
|
+
def _flatten(
|
|
638
|
+
inp: ColumnarTensor,
|
|
639
|
+
start_dim: int = 0,
|
|
640
|
+
end_dim: int = -1,
|
|
641
|
+
) -> ColumnarTensor:
|
|
642
|
+
return cast(
|
|
643
|
+
ColumnarTensor,
|
|
644
|
+
aten.flatten.using_ints.decompose(inp, start_dim, end_dim),
|
|
645
|
+
)
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
@ColumnarTensor.implements(aten.squeeze.default)
|
|
649
|
+
@preserve_view_inference_mode
|
|
650
|
+
def _squeeze(inp: ColumnarTensor) -> ColumnarTensor:
|
|
651
|
+
return _squeeze_dims(inp, range(inp.dim() - 1))
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
@ColumnarTensor.implements(aten.squeeze.dim)
|
|
655
|
+
@preserve_view_inference_mode
|
|
656
|
+
def _squeeze_dim(inp: ColumnarTensor, dim: int) -> ColumnarTensor:
|
|
657
|
+
return _squeeze_dims(inp, (dim,))
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
@ColumnarTensor.implements(aten.squeeze.dims)
|
|
661
|
+
@preserve_view_inference_mode
|
|
662
|
+
def _squeeze_dims(inp: ColumnarTensor, dim: Sequence[int]) -> ColumnarTensor:
|
|
663
|
+
dims = tuple(_normalize_dim(inp, d) for d in dim)
|
|
664
|
+
|
|
665
|
+
if any(dim == inp.dim() - 1 for dim in dims):
|
|
666
|
+
raise RuntimeError(
|
|
667
|
+
f"Can't squeeze the column dimension of {inp.__class__.__name__!r}"
|
|
668
|
+
)
|
|
669
|
+
|
|
670
|
+
return inp.__class__(
|
|
671
|
+
columns=[column.squeeze(dims) for column in inp._columns],
|
|
672
|
+
size=tuple(
|
|
673
|
+
dim_size
|
|
674
|
+
for i, dim_size in enumerate(inp.size()[:-1])
|
|
675
|
+
if i not in dims or dim_size != 1
|
|
676
|
+
),
|
|
677
|
+
device=inp.device,
|
|
678
|
+
)
|
|
679
|
+
|
|
680
|
+
|
|
681
|
+
@ColumnarTensor.implements(aten.unsqueeze.default)
|
|
682
|
+
@preserve_view_inference_mode
|
|
683
|
+
def _unsqueeze(inp: ColumnarTensor, dim: int) -> ColumnarTensor:
|
|
684
|
+
if dim < -inp.dim() - 1 or dim > inp.dim():
|
|
685
|
+
raise IndexError(
|
|
686
|
+
f"Dimension out of range (expected to be in range of "
|
|
687
|
+
f"[{-inp.dim() - 1}, {inp.dim()}], but got {dim})"
|
|
688
|
+
)
|
|
689
|
+
dim = dim % (inp.dim() + 1)
|
|
690
|
+
|
|
691
|
+
if dim == inp.dim():
|
|
692
|
+
raise RuntimeError(
|
|
693
|
+
f"Can't unsqueeze after the column dimension of "
|
|
694
|
+
f"{inp.__class__.__name__!r}"
|
|
695
|
+
)
|
|
696
|
+
|
|
697
|
+
return inp.__class__(
|
|
698
|
+
columns=[column.unsqueeze(dim) for column in inp._columns],
|
|
699
|
+
size=(*inp.size()[:dim], 1, *inp.size()[dim:-1]),
|
|
700
|
+
device=inp.device,
|
|
701
|
+
)
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
@ColumnarTensor.implements(aten.expand.default)
|
|
705
|
+
@preserve_view_inference_mode
|
|
706
|
+
def _expand(
|
|
707
|
+
inp: ColumnarTensor,
|
|
708
|
+
size: Sequence[int],
|
|
709
|
+
*,
|
|
710
|
+
implicit: bool = False,
|
|
711
|
+
) -> ColumnarTensor:
|
|
712
|
+
size = tuple(size)
|
|
713
|
+
|
|
714
|
+
if len(size) < inp.dim():
|
|
715
|
+
raise RuntimeError(
|
|
716
|
+
f"expand: the number of sizes provided ({len(size)}) must be "
|
|
717
|
+
f"greater or equal to the number of dimensions in the tensor "
|
|
718
|
+
f"({inp.dim()})"
|
|
719
|
+
)
|
|
720
|
+
|
|
721
|
+
if size[-1] not in (-1, inp.size(-1)):
|
|
722
|
+
_columns = "column" if inp.size(-1) == 1 else "columns"
|
|
723
|
+
raise RuntimeError(
|
|
724
|
+
f"Can't expand {inp.__class__.__name__!r} with "
|
|
725
|
+
f"{inp.size(-1)} {_columns} to shape {size}"
|
|
726
|
+
)
|
|
727
|
+
|
|
728
|
+
old_size = (*(1,) * (len(size) - inp.dim()), *inp.size())
|
|
729
|
+
|
|
730
|
+
return inp.__class__(
|
|
731
|
+
columns=[
|
|
732
|
+
aten.expand.default(
|
|
733
|
+
column,
|
|
734
|
+
size[:-1],
|
|
735
|
+
implicit=implicit,
|
|
736
|
+
)
|
|
737
|
+
for column in inp._columns
|
|
738
|
+
],
|
|
739
|
+
size=tuple(
|
|
740
|
+
old if new == -1 else new for old, new in zip(old_size, size)
|
|
741
|
+
)[:-1],
|
|
742
|
+
device=inp.device,
|
|
743
|
+
)
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
@ColumnarTensor.implements(aten.transpose.int)
|
|
747
|
+
@preserve_view_inference_mode
|
|
748
|
+
def _transpose(
|
|
749
|
+
inp: ColumnarTensor,
|
|
750
|
+
dim0: int,
|
|
751
|
+
dim1: int,
|
|
752
|
+
) -> ColumnarTensor:
|
|
753
|
+
|
|
754
|
+
dim0 = _normalize_dim(inp, dim0)
|
|
755
|
+
dim1 = _normalize_dim(inp, dim1)
|
|
756
|
+
|
|
757
|
+
if dim0 == dim1 == inp.dim() - 1:
|
|
758
|
+
return _alias(inp)
|
|
759
|
+
|
|
760
|
+
if inp.dim() - 1 in (dim0, dim1):
|
|
761
|
+
raise RuntimeError(
|
|
762
|
+
f"Can't transpose the column dimension of "
|
|
763
|
+
f"{inp.__class__.__name__!r}"
|
|
764
|
+
)
|
|
765
|
+
|
|
766
|
+
columns = [column.transpose(dim0, dim1) for column in inp._columns]
|
|
767
|
+
|
|
768
|
+
size = list(inp.size())
|
|
769
|
+
size[dim0], size[dim1] = size[dim1], size[dim0]
|
|
770
|
+
|
|
771
|
+
return inp.__class__(
|
|
772
|
+
columns=columns,
|
|
773
|
+
size=size[:-1],
|
|
774
|
+
device=inp.device,
|
|
775
|
+
)
|
|
776
|
+
|
|
777
|
+
|
|
778
|
+
@ColumnarTensor.implements(aten.permute.default)
|
|
779
|
+
@preserve_view_inference_mode
|
|
780
|
+
def _permute(inp: ColumnarTensor, dims: Sequence[int]) -> ColumnarTensor:
|
|
781
|
+
dims = tuple(_normalize_dim(inp, dim) for dim in dims)
|
|
782
|
+
if dims[-1] != inp.dim() - 1:
|
|
783
|
+
raise RuntimeError(
|
|
784
|
+
f"Can't permute the column dimension of {inp.__class__.__name__!r}"
|
|
785
|
+
)
|
|
786
|
+
|
|
787
|
+
return inp.__class__(
|
|
788
|
+
columns=[column.permute(dims[:-1]) for column in inp._columns],
|
|
789
|
+
size=tuple(inp.size(dim) for dim in dims[:-1]),
|
|
790
|
+
device=inp.device,
|
|
791
|
+
)
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
@ColumnarTensor.implements(aten.movedim.int)
|
|
795
|
+
def _movedim_int(
|
|
796
|
+
inp: ColumnarTensor,
|
|
797
|
+
source: int,
|
|
798
|
+
destination: int,
|
|
799
|
+
) -> ColumnarTensor:
|
|
800
|
+
return cast(
|
|
801
|
+
ColumnarTensor,
|
|
802
|
+
aten.movedim.int.decompose(inp, source, destination),
|
|
803
|
+
)
|
|
804
|
+
|
|
805
|
+
|
|
806
|
+
@ColumnarTensor.implements(aten.movedim.intlist)
|
|
807
|
+
def _movedim_intlist(
|
|
808
|
+
inp: ColumnarTensor,
|
|
809
|
+
source: Sequence[int],
|
|
810
|
+
destination: Sequence[int],
|
|
811
|
+
) -> ColumnarTensor:
|
|
812
|
+
return cast(
|
|
813
|
+
ColumnarTensor,
|
|
814
|
+
aten.movedim.intlist.decompose(inp, source, destination),
|
|
815
|
+
)
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
@ColumnarTensor.implements(aten.select.int)
|
|
819
|
+
@preserve_view_inference_mode
|
|
820
|
+
def _select(inp: ColumnarTensor, dim: int, index: int) -> Tensor:
|
|
821
|
+
dim = _normalize_dim(inp, dim)
|
|
822
|
+
|
|
823
|
+
if dim == inp.dim() - 1:
|
|
824
|
+
return aten.alias.default(inp._columns[index])
|
|
825
|
+
|
|
826
|
+
return inp.__class__(
|
|
827
|
+
columns=[column.select(dim, index) for column in inp._columns],
|
|
828
|
+
size=(*inp.size()[:dim], *inp.size()[dim + 1 : -1]),
|
|
829
|
+
device=inp.device,
|
|
830
|
+
)
|
|
831
|
+
|
|
832
|
+
|
|
833
|
+
@ColumnarTensor.implements(aten.slice.Tensor)
|
|
834
|
+
@preserve_view_inference_mode
|
|
835
|
+
def _slice(
|
|
836
|
+
inp: ColumnarTensor,
|
|
837
|
+
dim: int = 0,
|
|
838
|
+
start: int | None = None,
|
|
839
|
+
end: int | None = None,
|
|
840
|
+
step: int = 1,
|
|
841
|
+
) -> ColumnarTensor:
|
|
842
|
+
dim = _normalize_dim(inp, dim)
|
|
843
|
+
|
|
844
|
+
if dim == inp.dim() - 1:
|
|
845
|
+
return inp.__class__(
|
|
846
|
+
columns=inp._columns[slice(start, end, step)],
|
|
847
|
+
size=inp.size()[:-1],
|
|
848
|
+
device=inp.device,
|
|
849
|
+
)
|
|
850
|
+
|
|
851
|
+
return inp.__class__(
|
|
852
|
+
columns=[
|
|
853
|
+
aten.slice.Tensor(column, dim, start, end, step)
|
|
854
|
+
for column in inp._columns
|
|
855
|
+
],
|
|
856
|
+
size=(
|
|
857
|
+
*inp.size()[:dim],
|
|
858
|
+
len(range(inp.size(dim))[slice(start, end, step)]),
|
|
859
|
+
*inp.size()[dim + 1 : -1],
|
|
860
|
+
),
|
|
861
|
+
device=inp.device,
|
|
862
|
+
)
|
|
863
|
+
|
|
864
|
+
|
|
865
|
+
@ColumnarTensor.implements(aten.narrow.default)
|
|
866
|
+
@preserve_view_inference_mode
|
|
867
|
+
def _narrow(
|
|
868
|
+
inp: ColumnarTensor,
|
|
869
|
+
dim: int,
|
|
870
|
+
start: int,
|
|
871
|
+
length: int,
|
|
872
|
+
) -> ColumnarTensor:
|
|
873
|
+
return _slice(inp, dim=dim, start=start, end=start + length)
|
|
874
|
+
|
|
875
|
+
|
|
876
|
+
@ColumnarTensor.implements(aten.unbind.int)
|
|
877
|
+
@preserve_view_inference_mode
|
|
878
|
+
def _unbind(inp: ColumnarTensor, dim: int = 0) -> tuple[Tensor, ...]:
|
|
879
|
+
dim = _normalize_dim(inp, dim)
|
|
880
|
+
|
|
881
|
+
if dim == inp.dim() - 1:
|
|
882
|
+
return tuple(aten.alias.default(column) for column in inp._columns)
|
|
883
|
+
|
|
884
|
+
columns_list = [column.unbind(dim) for column in inp._columns]
|
|
885
|
+
if len(columns_list) == 0:
|
|
886
|
+
size = (*inp.size()[:dim], *inp.size()[dim + 1 : -1])
|
|
887
|
+
return tuple(
|
|
888
|
+
inp.__class__(
|
|
889
|
+
columns=(),
|
|
890
|
+
size=size,
|
|
891
|
+
device=inp.device,
|
|
892
|
+
)
|
|
893
|
+
for _ in range(inp.size(dim))
|
|
894
|
+
)
|
|
895
|
+
|
|
896
|
+
return tuple(
|
|
897
|
+
inp.__class__(columns=columns, device=inp.device)
|
|
898
|
+
for columns in zip(*columns_list)
|
|
899
|
+
)
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
@ColumnarTensor.implements(aten.split.Tensor)
|
|
903
|
+
@preserve_view_inference_mode
|
|
904
|
+
def _split(
|
|
905
|
+
inp: ColumnarTensor,
|
|
906
|
+
split_size: int,
|
|
907
|
+
dim: int = 0,
|
|
908
|
+
) -> tuple[ColumnarTensor, ...]:
|
|
909
|
+
split_sizes = tuple(
|
|
910
|
+
min(split_size, inp.size(dim) - start)
|
|
911
|
+
for start in range(0, inp.size(dim), split_size)
|
|
912
|
+
)
|
|
913
|
+
return _split_with_sizes(inp, split_sizes, dim=dim)
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
@ColumnarTensor.implements(aten.split.sizes)
|
|
917
|
+
@ColumnarTensor.implements(aten.split.default)
|
|
918
|
+
@ColumnarTensor.implements(aten.split_with_sizes.default)
|
|
919
|
+
@preserve_view_inference_mode
|
|
920
|
+
def _split_with_sizes(
|
|
921
|
+
inp: ColumnarTensor,
|
|
922
|
+
split_sizes: Sequence[int],
|
|
923
|
+
dim: int = 0,
|
|
924
|
+
) -> tuple[ColumnarTensor, ...]:
|
|
925
|
+
dim = _normalize_dim(inp, dim)
|
|
926
|
+
split_sizes = tuple(split_sizes)
|
|
927
|
+
|
|
928
|
+
if dim == inp.dim() - 1:
|
|
929
|
+
if sum(split_sizes) != inp.size(dim):
|
|
930
|
+
raise RuntimeError(
|
|
931
|
+
f"split_with_sizes expects split_sizes to sum exactly to "
|
|
932
|
+
f"{inp.size(dim)} (input tensor's size at dimension {dim}), "
|
|
933
|
+
f"but got split_sizes={split_sizes}"
|
|
934
|
+
)
|
|
935
|
+
|
|
936
|
+
start = 0
|
|
937
|
+
outs = []
|
|
938
|
+
for split_size in split_sizes:
|
|
939
|
+
out = inp.__class__(
|
|
940
|
+
columns=inp._columns[start : start + split_size],
|
|
941
|
+
size=inp.size()[:-1],
|
|
942
|
+
device=inp.device,
|
|
943
|
+
)
|
|
944
|
+
start += split_size
|
|
945
|
+
outs.append(out)
|
|
946
|
+
return tuple(outs)
|
|
947
|
+
|
|
948
|
+
columns_list = [col.split(split_sizes, dim=dim) for col in inp._columns]
|
|
949
|
+
if len(columns_list) == 0:
|
|
950
|
+
return tuple(
|
|
951
|
+
inp.__class__(
|
|
952
|
+
columns=(),
|
|
953
|
+
size=(
|
|
954
|
+
*inp.size()[:dim],
|
|
955
|
+
split_size,
|
|
956
|
+
*inp.size()[dim + 1 : -1],
|
|
957
|
+
),
|
|
958
|
+
device=inp.device,
|
|
959
|
+
)
|
|
960
|
+
for split_size in split_sizes
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
return tuple(
|
|
964
|
+
inp.__class__(columns=columns, device=inp.device)
|
|
965
|
+
for columns in zip(*columns_list)
|
|
966
|
+
)
|
|
967
|
+
|
|
968
|
+
|
|
969
|
+
@ColumnarTensor.implements(aten.index_select.default)
|
|
970
|
+
def _index_select(
|
|
971
|
+
inp: ColumnarTensor,
|
|
972
|
+
dim: int,
|
|
973
|
+
index: Tensor,
|
|
974
|
+
) -> ColumnarTensor:
|
|
975
|
+
dim = _normalize_dim(inp, dim)
|
|
976
|
+
if dim == inp.dim() - 1:
|
|
977
|
+
return inp.__class__(
|
|
978
|
+
columns=[inp._columns[i] for i in index.tolist()],
|
|
979
|
+
size=inp.size()[:-1],
|
|
980
|
+
device=inp.device,
|
|
981
|
+
)
|
|
982
|
+
|
|
983
|
+
return inp.__class__(
|
|
984
|
+
columns=[column.index_select(dim, index) for column in inp._columns],
|
|
985
|
+
size=(
|
|
986
|
+
*inp.size()[:dim],
|
|
987
|
+
index.numel(),
|
|
988
|
+
*inp.size()[dim + 1 : -1],
|
|
989
|
+
),
|
|
990
|
+
device=inp.device,
|
|
991
|
+
)
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
@ColumnarTensor.implements(aten.index.Tensor)
|
|
995
|
+
def _index(
|
|
996
|
+
inp: ColumnarTensor,
|
|
997
|
+
indices: Sequence[Tensor | None],
|
|
998
|
+
) -> ColumnarTensor:
|
|
999
|
+
current_dim = 0
|
|
1000
|
+
has_other_index = False
|
|
1001
|
+
column_index: Tensor | None = None
|
|
1002
|
+
for index in indices:
|
|
1003
|
+
if index is None:
|
|
1004
|
+
current_dim += 1
|
|
1005
|
+
continue
|
|
1006
|
+
|
|
1007
|
+
# Check whether we index the column dimension:
|
|
1008
|
+
num_indexed_dims = index.dim() if index.dtype == torch.bool else 1
|
|
1009
|
+
if current_dim <= inp.dim() - 1 < current_dim + num_indexed_dims:
|
|
1010
|
+
if num_indexed_dims != 1:
|
|
1011
|
+
raise RuntimeError(
|
|
1012
|
+
f"Can't index the column dimension of "
|
|
1013
|
+
f"{inp.__class__.__name__!r} with a multi-dimensional "
|
|
1014
|
+
f"index"
|
|
1015
|
+
)
|
|
1016
|
+
column_index = index
|
|
1017
|
+
else:
|
|
1018
|
+
has_other_index = True
|
|
1019
|
+
current_dim += num_indexed_dims
|
|
1020
|
+
|
|
1021
|
+
if column_index is not None:
|
|
1022
|
+
if has_other_index or column_index.dim() != 1:
|
|
1023
|
+
raise RuntimeError(
|
|
1024
|
+
f"Can't index the column dimension of "
|
|
1025
|
+
f"{inp.__class__.__name__!r} together with other dimensions"
|
|
1026
|
+
)
|
|
1027
|
+
if column_index.dtype == torch.bool:
|
|
1028
|
+
column_index = column_index.nonzero().view(-1)
|
|
1029
|
+
return inp.__class__(
|
|
1030
|
+
columns=[inp._columns[i] for i in column_index.tolist()],
|
|
1031
|
+
size=inp.size()[:-1],
|
|
1032
|
+
device=inp.device,
|
|
1033
|
+
)
|
|
1034
|
+
|
|
1035
|
+
if len(inp._columns) == 0:
|
|
1036
|
+
dummy = torch.empty(inp.size(), device=inp.device)
|
|
1037
|
+
return inp.__class__(
|
|
1038
|
+
columns=(),
|
|
1039
|
+
size=aten.index.Tensor(dummy, indices).size()[:-1],
|
|
1040
|
+
device=inp.device,
|
|
1041
|
+
)
|
|
1042
|
+
|
|
1043
|
+
return inp.__class__(
|
|
1044
|
+
columns=[
|
|
1045
|
+
aten.index.Tensor(column, indices) for column in inp._columns
|
|
1046
|
+
],
|
|
1047
|
+
device=inp.device,
|
|
1048
|
+
)
|
|
1049
|
+
|
|
1050
|
+
|
|
1051
|
+
@ColumnarTensor.implements(aten.cat.default)
|
|
1052
|
+
def _cat(tensors: Sequence[Tensor], dim: int = 0) -> ColumnarTensor:
|
|
1053
|
+
if not all(isinstance(tensor, ColumnarTensor) for tensor in tensors):
|
|
1054
|
+
raise TypeError(
|
|
1055
|
+
f"Expected all tensors to be {ColumnarTensor.__name__!r} instances"
|
|
1056
|
+
)
|
|
1057
|
+
|
|
1058
|
+
tensors = cast(Sequence[ColumnarTensor], tensors)
|
|
1059
|
+
dim = _normalize_dim(tensors[0], dim)
|
|
1060
|
+
|
|
1061
|
+
if dim == tensors[0].dim() - 1:
|
|
1062
|
+
return tensors[0].__class__(
|
|
1063
|
+
columns=tuple(chain.from_iterable(t._columns for t in tensors)),
|
|
1064
|
+
size=tensors[0].size()[:-1],
|
|
1065
|
+
device=tensors[0].device,
|
|
1066
|
+
)
|
|
1067
|
+
|
|
1068
|
+
return tensors[0].__class__(
|
|
1069
|
+
columns=[
|
|
1070
|
+
torch.cat([tensor._columns[i] for tensor in tensors], dim=dim)
|
|
1071
|
+
for i in range(tensors[0].size(-1))
|
|
1072
|
+
],
|
|
1073
|
+
size=(
|
|
1074
|
+
*tensors[0].size()[:dim],
|
|
1075
|
+
sum(tensor.size(dim) for tensor in tensors),
|
|
1076
|
+
*tensors[0].size()[dim + 1 : -1],
|
|
1077
|
+
),
|
|
1078
|
+
device=tensors[0].device,
|
|
1079
|
+
)
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
@ColumnarTensor.implements(aten.stack.default)
|
|
1083
|
+
def _stack(tensors: Sequence[Tensor], dim: int = 0) -> ColumnarTensor:
|
|
1084
|
+
if not all(isinstance(tensor, ColumnarTensor) for tensor in tensors):
|
|
1085
|
+
raise TypeError(
|
|
1086
|
+
f"Expected all tensors to be {ColumnarTensor.__name__!r} instances"
|
|
1087
|
+
)
|
|
1088
|
+
|
|
1089
|
+
tensors = cast(Sequence[ColumnarTensor], tensors)
|
|
1090
|
+
|
|
1091
|
+
if dim < -tensors[0].dim() - 1 or dim > tensors[0].dim():
|
|
1092
|
+
raise IndexError(
|
|
1093
|
+
f"Dimension out of range (expected to be in range of "
|
|
1094
|
+
f"[{-tensors[0].dim() - 1}, {tensors[0].dim()}], but got {dim})"
|
|
1095
|
+
)
|
|
1096
|
+
dim = dim % (tensors[0].dim() + 1)
|
|
1097
|
+
|
|
1098
|
+
if dim >= tensors[0].dim():
|
|
1099
|
+
raise RuntimeError(
|
|
1100
|
+
f"Can't stack after the column dimension of "
|
|
1101
|
+
f"{tensors[0].__class__.__name__!r}"
|
|
1102
|
+
)
|
|
1103
|
+
|
|
1104
|
+
return tensors[0].__class__(
|
|
1105
|
+
columns=[
|
|
1106
|
+
torch.stack([tensor._columns[i] for tensor in tensors], dim=dim)
|
|
1107
|
+
for i in range(tensors[0].size(-1))
|
|
1108
|
+
],
|
|
1109
|
+
size=(
|
|
1110
|
+
*tensors[0].size()[:dim],
|
|
1111
|
+
len(tensors),
|
|
1112
|
+
*tensors[0].size()[dim:-1],
|
|
1113
|
+
),
|
|
1114
|
+
device=tensors[0].device,
|
|
1115
|
+
)
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
# Helpers #####################################################################
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
def _normalize_dim(inp: Tensor, dim: int) -> int:
|
|
1122
|
+
if dim < -inp.dim() or dim >= inp.dim():
|
|
1123
|
+
raise IndexError(
|
|
1124
|
+
f"Dimension out of range (expected to be in range of "
|
|
1125
|
+
f"[{-inp.dim()}, {inp.dim() - 1}], but got {dim})"
|
|
1126
|
+
)
|
|
1127
|
+
return dim % inp.dim()
|