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/var_len.py
ADDED
|
@@ -0,0 +1,1486 @@
|
|
|
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 typing import Any, ClassVar, Self, SupportsIndex, cast
|
|
10
|
+
|
|
11
|
+
import pyarrow as pa
|
|
12
|
+
import torch
|
|
13
|
+
from torch import Tensor
|
|
14
|
+
from torch.overrides import enable_reentrant_dispatch
|
|
15
|
+
from typing_extensions import override
|
|
16
|
+
|
|
17
|
+
from sdm.tensor.io import (
|
|
18
|
+
ARROW_TORCH_DTYPES,
|
|
19
|
+
arrow_as_tensor,
|
|
20
|
+
combine_arrow_chunks,
|
|
21
|
+
to_arrow,
|
|
22
|
+
)
|
|
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 VarLenTensor(Tensor):
|
|
39
|
+
r"""A :class:`torch.Tensor` for rectangular variable-length values.
|
|
40
|
+
|
|
41
|
+
Values are stored in a flat contiguous ``data`` tensor and indexed by an
|
|
42
|
+
``offset`` tensor.
|
|
43
|
+
|
|
44
|
+
.. testcode::
|
|
45
|
+
|
|
46
|
+
import torch
|
|
47
|
+
from sdm import VarLenTensor
|
|
48
|
+
|
|
49
|
+
tensor = VarLenTensor(
|
|
50
|
+
data=torch.tensor([1, 2, 3, 4, 5, 6]),
|
|
51
|
+
offset=torch.tensor([0, 2, 5, 5, 6]),
|
|
52
|
+
valid=None,
|
|
53
|
+
size=(2, 2),
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
data: Flat contiguous tensor containing all element values.
|
|
58
|
+
offset: One-dimensional offsets into ``data``.
|
|
59
|
+
valid: One-dimensional mask indicating valid, non-null element values.
|
|
60
|
+
size: The shape of the tensor.
|
|
61
|
+
stride: The stride of the tensor.
|
|
62
|
+
storage_offset: The offset into the logical ``offset`` storage.
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
ALLOWED_DTYPES: ClassVar[tuple[torch.dtype, ...] | None] = None
|
|
66
|
+
HANDLED_FUNCTIONS: ClassVar[
|
|
67
|
+
dict[Callable[..., Any], Callable[..., Any]]
|
|
68
|
+
] = {}
|
|
69
|
+
|
|
70
|
+
_data: Tensor
|
|
71
|
+
_offset: Tensor
|
|
72
|
+
_valid: Tensor | None
|
|
73
|
+
|
|
74
|
+
# Constructors ############################################################
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
data: Tensor,
|
|
79
|
+
offset: Tensor,
|
|
80
|
+
valid: Tensor | None,
|
|
81
|
+
size: Sequence[int],
|
|
82
|
+
stride: Sequence[int] | None = None,
|
|
83
|
+
storage_offset: int = 0,
|
|
84
|
+
) -> None:
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
def __new__(
|
|
88
|
+
cls,
|
|
89
|
+
data: Tensor,
|
|
90
|
+
offset: Tensor,
|
|
91
|
+
valid: Tensor | None,
|
|
92
|
+
size: Sequence[int],
|
|
93
|
+
stride: Sequence[int] | None = None,
|
|
94
|
+
storage_offset: int = 0,
|
|
95
|
+
) -> Self:
|
|
96
|
+
r"""Create a tensor wrapper."""
|
|
97
|
+
size = tuple(size)
|
|
98
|
+
if any(dim_size < -1 for dim_size in size):
|
|
99
|
+
raise ValueError(f"Invalid shape dimensions (got '{size}')")
|
|
100
|
+
if size.count(-1) > 1:
|
|
101
|
+
raise ValueError("Only one dimension can be inferred")
|
|
102
|
+
|
|
103
|
+
if -1 in size:
|
|
104
|
+
if stride is not None:
|
|
105
|
+
raise ValueError("Can't infer size when stride is given")
|
|
106
|
+
|
|
107
|
+
numel = offset.numel() - 1 - storage_offset
|
|
108
|
+
known = math.prod(dim_size for dim_size in size if dim_size != -1)
|
|
109
|
+
if numel < 0 or known == 0 or numel % known != 0:
|
|
110
|
+
raise ValueError(
|
|
111
|
+
f"Shape '{size}' is invalid for input of size {numel}"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
dim = size.index(-1)
|
|
115
|
+
size = (*size[:dim], numel // known, *size[dim + 1 :])
|
|
116
|
+
|
|
117
|
+
stride = _contiguous_stride(size) if stride is None else tuple(stride)
|
|
118
|
+
if any(dim_stride < 0 for dim_stride in stride):
|
|
119
|
+
raise ValueError(
|
|
120
|
+
f"Negative strides are not supported (got '{stride}')"
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
if (
|
|
124
|
+
cls.ALLOWED_DTYPES is not None
|
|
125
|
+
and data.dtype not in cls.ALLOWED_DTYPES
|
|
126
|
+
):
|
|
127
|
+
raise ValueError(
|
|
128
|
+
f"Expected 'data' in {cls.__name__!r} to have dtype "
|
|
129
|
+
f"in '{cls.ALLOWED_DTYPES}' (got '{data.dtype}')"
|
|
130
|
+
)
|
|
131
|
+
if data.dim() != 1:
|
|
132
|
+
raise ValueError(
|
|
133
|
+
f"Expected 'data' in {cls.__name__!r} to be one-dimensional "
|
|
134
|
+
f"(got {data.dim()}D tensor)"
|
|
135
|
+
)
|
|
136
|
+
if not data.is_contiguous():
|
|
137
|
+
raise ValueError(
|
|
138
|
+
f"Expected 'data' in {cls.__name__!r} to be contiguous"
|
|
139
|
+
)
|
|
140
|
+
if offset.dtype not in (torch.int32, torch.int64):
|
|
141
|
+
raise ValueError(
|
|
142
|
+
f"Expected 'offset' in {cls.__name__!r} to have dtype "
|
|
143
|
+
f"'torch.int32' or 'torch.int64' (got '{offset.dtype}')"
|
|
144
|
+
)
|
|
145
|
+
if offset.dim() != 1:
|
|
146
|
+
raise ValueError(
|
|
147
|
+
f"Expected 'offset' in {cls.__name__!r} to be one-dimensional "
|
|
148
|
+
f"(got {offset.dim()}D tensor)"
|
|
149
|
+
)
|
|
150
|
+
if not offset.is_contiguous():
|
|
151
|
+
raise ValueError(
|
|
152
|
+
f"Expected 'offset' in {cls.__name__!r} to be contiguous"
|
|
153
|
+
)
|
|
154
|
+
if data.device != offset.device:
|
|
155
|
+
raise ValueError(
|
|
156
|
+
f"Expected 'data' and 'offset' in {cls.__name__!r} to be on "
|
|
157
|
+
f"the same device (got '{data.device}' and '{offset.device}')"
|
|
158
|
+
)
|
|
159
|
+
if valid is not None:
|
|
160
|
+
if valid.dtype != torch.bool:
|
|
161
|
+
raise ValueError(
|
|
162
|
+
f"Expected 'valid' in {cls.__name__!r} to have dtype "
|
|
163
|
+
f"'torch.bool' (got '{valid.dtype}')"
|
|
164
|
+
)
|
|
165
|
+
if valid.dim() != 1:
|
|
166
|
+
raise ValueError(
|
|
167
|
+
f"Expected 'valid' in {cls.__name__!r} to be "
|
|
168
|
+
f"one-dimensional (got {valid.dim()}D tensor)"
|
|
169
|
+
)
|
|
170
|
+
if not valid.is_contiguous():
|
|
171
|
+
raise ValueError(
|
|
172
|
+
f"Expected 'valid' in {cls.__name__!r} to be contiguous"
|
|
173
|
+
)
|
|
174
|
+
if data.device != valid.device:
|
|
175
|
+
raise ValueError(
|
|
176
|
+
f"Expected 'data' and 'valid' in {cls.__name__!r} to be "
|
|
177
|
+
f"on the same device (got '{data.device}' and "
|
|
178
|
+
f"'{valid.device}')"
|
|
179
|
+
)
|
|
180
|
+
if len(size) != len(stride):
|
|
181
|
+
raise ValueError(
|
|
182
|
+
f"Expected 'size' and 'stride' in {cls.__name__!r} to have "
|
|
183
|
+
f"the same length (got {len(size)} and {len(stride)})"
|
|
184
|
+
)
|
|
185
|
+
if storage_offset < 0:
|
|
186
|
+
raise ValueError(
|
|
187
|
+
f"Expected 'storage_offset' in {cls.__name__!r} to be "
|
|
188
|
+
f"non-negative"
|
|
189
|
+
)
|
|
190
|
+
if storage_offset + _span_len(size, stride) >= offset.numel():
|
|
191
|
+
raise ValueError(
|
|
192
|
+
f"'offset' in {cls.__name__!r} is out of bounds (got "
|
|
193
|
+
f"{offset.numel()} entries, but expected at least "
|
|
194
|
+
f"{storage_offset + _span_len(size, stride) + 1} entries)"
|
|
195
|
+
)
|
|
196
|
+
if (
|
|
197
|
+
valid is not None
|
|
198
|
+
and storage_offset + _span_len(size, stride) > valid.numel()
|
|
199
|
+
):
|
|
200
|
+
raise ValueError(
|
|
201
|
+
f"'valid' in {cls.__name__!r} is out of bounds (got "
|
|
202
|
+
f"{valid.numel()} entries, but expected at least "
|
|
203
|
+
f"{storage_offset + _span_len(size, stride)} entries)"
|
|
204
|
+
)
|
|
205
|
+
if data.numel() > torch.iinfo(offset.dtype).max:
|
|
206
|
+
raise ValueError(
|
|
207
|
+
f"Expected 'offset' in {cls.__name__!r} to represent "
|
|
208
|
+
f"{data.numel()} elements, but '{offset.dtype}' can only "
|
|
209
|
+
f"represent {torch.iinfo(offset.dtype).max} elements"
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
# NOTE We do not validate offset values here (e.g., monotonicity) due
|
|
213
|
+
# to device synchronization.
|
|
214
|
+
|
|
215
|
+
out = Tensor._make_wrapper_subclass(
|
|
216
|
+
cls,
|
|
217
|
+
size=size,
|
|
218
|
+
strides=stride,
|
|
219
|
+
storage_offset=storage_offset,
|
|
220
|
+
dtype=data.dtype,
|
|
221
|
+
device=data.device,
|
|
222
|
+
requires_grad=False, # Autograd lives on `_data` only.
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
out._data = data
|
|
226
|
+
out._offset = offset
|
|
227
|
+
out._valid = valid
|
|
228
|
+
|
|
229
|
+
return out
|
|
230
|
+
|
|
231
|
+
@classmethod
|
|
232
|
+
def from_tensor(
|
|
233
|
+
cls,
|
|
234
|
+
tensor: Tensor,
|
|
235
|
+
*,
|
|
236
|
+
offset_dtype: torch.dtype = torch.int64,
|
|
237
|
+
) -> Self:
|
|
238
|
+
r"""Wrap a dense tensor as fixed-size variable-length elements.
|
|
239
|
+
|
|
240
|
+
Args:
|
|
241
|
+
tensor: The dense tensor.
|
|
242
|
+
offset_dtype: The dtype of the ``offset`` tensor.
|
|
243
|
+
"""
|
|
244
|
+
data = tensor
|
|
245
|
+
if tensor.stride() != (1,) or int(tensor.storage_offset()) != 0:
|
|
246
|
+
span_len = _span_len(tensor.size(), tensor.stride())
|
|
247
|
+
data = torch.as_strided(
|
|
248
|
+
tensor,
|
|
249
|
+
size=(int(data.storage_offset()) + span_len,),
|
|
250
|
+
stride=(1,),
|
|
251
|
+
storage_offset=0,
|
|
252
|
+
)
|
|
253
|
+
offset = torch.arange(
|
|
254
|
+
data.numel() + 1,
|
|
255
|
+
dtype=offset_dtype,
|
|
256
|
+
device=data.device,
|
|
257
|
+
)
|
|
258
|
+
return cls(
|
|
259
|
+
data=data,
|
|
260
|
+
offset=offset,
|
|
261
|
+
valid=None,
|
|
262
|
+
size=tensor.size(),
|
|
263
|
+
stride=tensor.stride(),
|
|
264
|
+
storage_offset=int(tensor.storage_offset()),
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
@classmethod
|
|
268
|
+
def from_arrow(
|
|
269
|
+
cls,
|
|
270
|
+
array: pa.Array | pa.ChunkedArray,
|
|
271
|
+
*,
|
|
272
|
+
size: Sequence[int] | None = None,
|
|
273
|
+
device: torch.device | str | None = None,
|
|
274
|
+
) -> Self:
|
|
275
|
+
r"""Create tensor from a list :class:`pyarrow.Array`.
|
|
276
|
+
|
|
277
|
+
.. testcode::
|
|
278
|
+
|
|
279
|
+
import pyarrow as pa
|
|
280
|
+
from sdm import VarLenTensor
|
|
281
|
+
|
|
282
|
+
array = pa.array([[1, 2], [3, 4, 5], [], None, [6]])
|
|
283
|
+
tensor = VarLenTensor.from_arrow(array)
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
array: The list :class:`pyarrow.Array` or
|
|
287
|
+
:class:`pyarrow.ChunkedArray`.
|
|
288
|
+
size: The shape of the tensor.
|
|
289
|
+
device: The device.
|
|
290
|
+
"""
|
|
291
|
+
if isinstance(array, pa.ChunkedArray):
|
|
292
|
+
array = combine_arrow_chunks(array)
|
|
293
|
+
|
|
294
|
+
if size is None:
|
|
295
|
+
size = (len(array),)
|
|
296
|
+
elif math.prod(size) != len(array):
|
|
297
|
+
raise ValueError(
|
|
298
|
+
f"Expected 'size' in '{cls.__name__}.from_arrow' to contain "
|
|
299
|
+
f"{len(array)} elements (got {math.prod(size)})"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
is_list = pa.types.is_list(array.type)
|
|
303
|
+
is_large_list = pa.types.is_large_list(array.type)
|
|
304
|
+
if not is_list and not is_large_list:
|
|
305
|
+
raise TypeError(
|
|
306
|
+
f"Expected 'array' in '{cls.__name__}.from_arrow' to have "
|
|
307
|
+
f"'list' or 'large_list' type (got '{array.type}')"
|
|
308
|
+
)
|
|
309
|
+
|
|
310
|
+
if array.values.null_count > 0:
|
|
311
|
+
raise ValueError(
|
|
312
|
+
f"{cls.__name__!r} cannot represent inner null values"
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
dtype = ARROW_TORCH_DTYPES.get(array.values.type)
|
|
316
|
+
if dtype is None:
|
|
317
|
+
raise TypeError(f"Unsupported value type '{array.values.type}'")
|
|
318
|
+
|
|
319
|
+
buffer = array.values.buffers()[1]
|
|
320
|
+
if buffer is not None and buffer.size > 0:
|
|
321
|
+
if dtype == torch.bool:
|
|
322
|
+
data = arrow_as_tensor(
|
|
323
|
+
array.values,
|
|
324
|
+
dtype=dtype,
|
|
325
|
+
device=device,
|
|
326
|
+
)
|
|
327
|
+
else:
|
|
328
|
+
data = torch.frombuffer(buffer, dtype=dtype)
|
|
329
|
+
start = array.values.offset
|
|
330
|
+
data = data[start : start + len(array.values)].to(device)
|
|
331
|
+
else:
|
|
332
|
+
data = torch.empty(0, dtype=dtype, device=device)
|
|
333
|
+
|
|
334
|
+
offset = torch.frombuffer(
|
|
335
|
+
array.buffers()[1],
|
|
336
|
+
dtype=torch.int32 if is_list else torch.int64,
|
|
337
|
+
)
|
|
338
|
+
valid: Tensor | None = None
|
|
339
|
+
storage_offset = array.offset
|
|
340
|
+
if array.null_count > 0:
|
|
341
|
+
offset = offset[array.offset : array.offset + len(array) + 1]
|
|
342
|
+
valid = arrow_as_tensor(
|
|
343
|
+
array.is_valid(),
|
|
344
|
+
dtype=torch.bool,
|
|
345
|
+
device=device,
|
|
346
|
+
)
|
|
347
|
+
storage_offset = 0
|
|
348
|
+
|
|
349
|
+
return cls(
|
|
350
|
+
data=data,
|
|
351
|
+
offset=offset.to(device),
|
|
352
|
+
valid=valid,
|
|
353
|
+
size=size,
|
|
354
|
+
storage_offset=storage_offset,
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
def to_arrow(self) -> pa.Array:
|
|
358
|
+
r"""Convert this tensor to a flat :class:`pyarrow.Array`."""
|
|
359
|
+
tensor = cast(VarLenTensor, self.detach().contiguous().cpu())
|
|
360
|
+
array = to_arrow(tensor._data)
|
|
361
|
+
|
|
362
|
+
return pa.Array.from_buffers(
|
|
363
|
+
pa.list_(array.type)
|
|
364
|
+
if tensor._offset.dtype == torch.int32
|
|
365
|
+
else pa.large_list(array.type),
|
|
366
|
+
length=tensor.numel(),
|
|
367
|
+
buffers=[
|
|
368
|
+
pa.array(tensor._valid.numpy(), type=pa.bool_()).buffers()[1]
|
|
369
|
+
if tensor._valid is not None
|
|
370
|
+
else None,
|
|
371
|
+
pa.py_buffer(tensor._offset.numpy()),
|
|
372
|
+
],
|
|
373
|
+
null_count=-1,
|
|
374
|
+
children=[array],
|
|
375
|
+
offset=int(tensor.storage_offset()),
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
@classmethod
|
|
379
|
+
def from_list(
|
|
380
|
+
cls,
|
|
381
|
+
values: Sequence[Any],
|
|
382
|
+
*,
|
|
383
|
+
dtype: torch.dtype | None = None,
|
|
384
|
+
device: torch.device | str | None = None,
|
|
385
|
+
offset_dtype: torch.dtype = torch.int64,
|
|
386
|
+
) -> Self:
|
|
387
|
+
r"""Create tensor from a rectangular Python list.
|
|
388
|
+
|
|
389
|
+
.. testcode::
|
|
390
|
+
|
|
391
|
+
from sdm import VarLenTensor
|
|
392
|
+
|
|
393
|
+
tensor = VarLenTensor.from_list([
|
|
394
|
+
[[1, 2], [3, 4, 5]],
|
|
395
|
+
[[], [6]],
|
|
396
|
+
[[7, 8], None],
|
|
397
|
+
])
|
|
398
|
+
|
|
399
|
+
Args:
|
|
400
|
+
values: The rectangular Python list.
|
|
401
|
+
dtype: The dtype of the ``value`` tensor.
|
|
402
|
+
device: The device.
|
|
403
|
+
offset_dtype: The dtype of the ``offset`` tensor.
|
|
404
|
+
"""
|
|
405
|
+
data: list[Any] = []
|
|
406
|
+
offset: list[int] = [0]
|
|
407
|
+
valid: list[bool] = []
|
|
408
|
+
|
|
409
|
+
def is_sequence(value: Any) -> bool:
|
|
410
|
+
return isinstance(value, Sequence) and not isinstance(
|
|
411
|
+
value, str | bytes | bytearray
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
def is_leaf(value: Any) -> bool:
|
|
415
|
+
if value is None:
|
|
416
|
+
return True
|
|
417
|
+
if not is_sequence(value):
|
|
418
|
+
return False
|
|
419
|
+
return len(value) == 0 or not is_sequence(value[0])
|
|
420
|
+
|
|
421
|
+
def flatten_leaf(value: Any) -> None:
|
|
422
|
+
if value is None:
|
|
423
|
+
offset.append(len(data))
|
|
424
|
+
valid.append(False)
|
|
425
|
+
return
|
|
426
|
+
|
|
427
|
+
for item in value:
|
|
428
|
+
if item is None:
|
|
429
|
+
raise ValueError(
|
|
430
|
+
f"{cls.__name__!r} cannot represent inner null values"
|
|
431
|
+
)
|
|
432
|
+
if is_sequence(item):
|
|
433
|
+
raise ValueError(
|
|
434
|
+
f"{cls.__name__!r} data must be rectangular"
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
data.extend(value)
|
|
438
|
+
valid.append(True)
|
|
439
|
+
offset.append(len(data))
|
|
440
|
+
|
|
441
|
+
def flatten(value: Any) -> tuple[int, ...]:
|
|
442
|
+
if is_leaf(value):
|
|
443
|
+
flatten_leaf(value)
|
|
444
|
+
return ()
|
|
445
|
+
|
|
446
|
+
if not is_sequence(value):
|
|
447
|
+
raise ValueError(f"{cls.__name__!r} data must be rectangular")
|
|
448
|
+
|
|
449
|
+
child_size: tuple[int, ...] | None = None
|
|
450
|
+
for item in value:
|
|
451
|
+
if is_leaf(item):
|
|
452
|
+
item_size = ()
|
|
453
|
+
flatten_leaf(item)
|
|
454
|
+
else:
|
|
455
|
+
item_size = flatten(item)
|
|
456
|
+
|
|
457
|
+
if child_size is None:
|
|
458
|
+
child_size = item_size
|
|
459
|
+
elif item_size != child_size:
|
|
460
|
+
raise ValueError(
|
|
461
|
+
f"{cls.__name__!r} data must be rectangular"
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
assert child_size is not None
|
|
465
|
+
return (len(value), *child_size)
|
|
466
|
+
|
|
467
|
+
size = (0,) if len(values) == 0 else flatten(values)
|
|
468
|
+
|
|
469
|
+
return cls(
|
|
470
|
+
data=torch.tensor(data, dtype=dtype, device=device),
|
|
471
|
+
offset=torch.tensor(offset, dtype=offset_dtype, device=device),
|
|
472
|
+
valid=torch.tensor(valid, dtype=torch.bool, device=device)
|
|
473
|
+
if False in valid
|
|
474
|
+
else None,
|
|
475
|
+
size=size,
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
# Properties ##############################################################
|
|
479
|
+
|
|
480
|
+
@property
|
|
481
|
+
def data_offset(self) -> tuple[Tensor, Tensor]:
|
|
482
|
+
r"""Return contiguous data and normalized offsets.
|
|
483
|
+
|
|
484
|
+
Returns:
|
|
485
|
+
``(data, offset)`` tuple.
|
|
486
|
+
"""
|
|
487
|
+
if not self.is_contiguous():
|
|
488
|
+
raise RuntimeError(
|
|
489
|
+
f"Can't access 'data_offset' for non-contiguous "
|
|
490
|
+
f"{self.__class__.__name__!r}"
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
start = int(self.storage_offset())
|
|
494
|
+
offset = self._offset[start : start + self.numel() + 1]
|
|
495
|
+
data = self._data[offset[0] : offset[-1]]
|
|
496
|
+
if offset.is_cpu and int(offset[0]) == 0:
|
|
497
|
+
return data, offset
|
|
498
|
+
return data, offset - offset[0]
|
|
499
|
+
|
|
500
|
+
@property
|
|
501
|
+
def valid(self) -> Tensor | None:
|
|
502
|
+
r"""Return the logical validity mask.
|
|
503
|
+
|
|
504
|
+
Returns:
|
|
505
|
+
Boolean mask with shape ``self.size()`` indicating valid,
|
|
506
|
+
non-null tensor elements, or ``None`` when all elements are valid.
|
|
507
|
+
"""
|
|
508
|
+
if self._valid is None:
|
|
509
|
+
return None
|
|
510
|
+
|
|
511
|
+
return torch.as_strided(
|
|
512
|
+
self._valid,
|
|
513
|
+
size=self.size(),
|
|
514
|
+
stride=self.stride(),
|
|
515
|
+
storage_offset=int(self.storage_offset()),
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
@property
|
|
519
|
+
def is_nullable(self) -> bool:
|
|
520
|
+
r"""Whether this tensor has a validity mask."""
|
|
521
|
+
return self._valid is not None
|
|
522
|
+
|
|
523
|
+
# Decorators ##############################################################
|
|
524
|
+
|
|
525
|
+
@classmethod
|
|
526
|
+
def implements(
|
|
527
|
+
cls,
|
|
528
|
+
torch_function: Callable[..., Any],
|
|
529
|
+
) -> Callable[..., Any]:
|
|
530
|
+
r"""Register a ``__torch_dispatch__`` implementation.
|
|
531
|
+
|
|
532
|
+
See PyTorch's
|
|
533
|
+
:ref:`calling convention <torch-dispatch-calling-convention>`.
|
|
534
|
+
"""
|
|
535
|
+
if "HANDLED_FUNCTIONS" not in cls.__dict__:
|
|
536
|
+
cls.HANDLED_FUNCTIONS = cls.HANDLED_FUNCTIONS.copy()
|
|
537
|
+
|
|
538
|
+
def decorator(my_function: Callable[..., Any]) -> Callable[..., Any]:
|
|
539
|
+
cls.HANDLED_FUNCTIONS[torch_function] = my_function
|
|
540
|
+
return my_function
|
|
541
|
+
|
|
542
|
+
return decorator
|
|
543
|
+
|
|
544
|
+
# PyTorch/Python builtins #################################################
|
|
545
|
+
|
|
546
|
+
def __tensor_flatten__(self) -> tuple[list[str], tuple[Any, ...]]:
|
|
547
|
+
attrs = ["_data", "_offset"]
|
|
548
|
+
if self._valid is not None:
|
|
549
|
+
attrs.append("_valid")
|
|
550
|
+
ctx = (self.__class__, self.storage_offset())
|
|
551
|
+
return attrs, ctx
|
|
552
|
+
|
|
553
|
+
@staticmethod
|
|
554
|
+
def __tensor_unflatten__(
|
|
555
|
+
inner_tensors: dict[str, Any],
|
|
556
|
+
ctx: tuple[Any, ...],
|
|
557
|
+
outer_size: tuple[int, ...],
|
|
558
|
+
outer_stride: tuple[int, ...],
|
|
559
|
+
) -> VarLenTensor:
|
|
560
|
+
cls, storage_offset = ctx
|
|
561
|
+
return cls(
|
|
562
|
+
data=inner_tensors["_data"],
|
|
563
|
+
offset=inner_tensors["_offset"],
|
|
564
|
+
valid=inner_tensors.get("_valid"),
|
|
565
|
+
size=outer_size,
|
|
566
|
+
stride=outer_stride,
|
|
567
|
+
storage_offset=storage_offset,
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
def __reduce_ex__(self, proto: SupportsIndex) -> Any:
|
|
571
|
+
args = (
|
|
572
|
+
self._data,
|
|
573
|
+
self._offset,
|
|
574
|
+
self._valid,
|
|
575
|
+
tuple(self.size()),
|
|
576
|
+
tuple(self.stride()),
|
|
577
|
+
int(self.storage_offset()),
|
|
578
|
+
)
|
|
579
|
+
return (self.__class__, args)
|
|
580
|
+
|
|
581
|
+
@classmethod
|
|
582
|
+
def __torch_function__(
|
|
583
|
+
cls,
|
|
584
|
+
func: Callable[..., Any],
|
|
585
|
+
types: tuple[type[Any], ...],
|
|
586
|
+
args: tuple[Any, ...] = (),
|
|
587
|
+
kwargs: dict[str, Any] | None = None,
|
|
588
|
+
) -> Any:
|
|
589
|
+
if func is torch.isfinite or func is Tensor.isfinite:
|
|
590
|
+
assert isinstance(args[0], VarLenTensor)
|
|
591
|
+
return _isfinite(args[0])
|
|
592
|
+
|
|
593
|
+
with torch._C.DisableTorchFunction():
|
|
594
|
+
return func(*args, **(kwargs or {}))
|
|
595
|
+
|
|
596
|
+
@classmethod
|
|
597
|
+
def __torch_dispatch__( # type: ignore
|
|
598
|
+
cls,
|
|
599
|
+
func: Callable[..., Any],
|
|
600
|
+
types: tuple[type[Any], ...],
|
|
601
|
+
args: tuple[Any, ...] = (),
|
|
602
|
+
kwargs: dict[str, Any] | None = None,
|
|
603
|
+
) -> Any:
|
|
604
|
+
if (handler := cls.HANDLED_FUNCTIONS.get(func)) is not None:
|
|
605
|
+
with enable_reentrant_dispatch(): # Record autograd in `_data`.
|
|
606
|
+
return handler(*args, **(kwargs or {}))
|
|
607
|
+
|
|
608
|
+
raise NotImplementedError(
|
|
609
|
+
f"'{func}' is not supported for {cls.__name__!r}"
|
|
610
|
+
)
|
|
611
|
+
|
|
612
|
+
@override
|
|
613
|
+
def is_shared(self) -> bool:
|
|
614
|
+
is_shared = self._data.is_shared() and self._offset.is_shared()
|
|
615
|
+
return is_shared and (self._valid is None or self._valid.is_shared())
|
|
616
|
+
|
|
617
|
+
@override
|
|
618
|
+
def share_memory_(self) -> Self:
|
|
619
|
+
self._data.share_memory_()
|
|
620
|
+
self._offset.share_memory_()
|
|
621
|
+
if self._valid is not None:
|
|
622
|
+
self._valid.share_memory_()
|
|
623
|
+
return self
|
|
624
|
+
|
|
625
|
+
@property
|
|
626
|
+
@override
|
|
627
|
+
def grad(self) -> Tensor | None:
|
|
628
|
+
return self._data.grad
|
|
629
|
+
|
|
630
|
+
@property
|
|
631
|
+
@override
|
|
632
|
+
def requires_grad(self) -> bool:
|
|
633
|
+
return self._data.requires_grad
|
|
634
|
+
|
|
635
|
+
@requires_grad.setter
|
|
636
|
+
def requires_grad(self, requires_grad: bool) -> None:
|
|
637
|
+
self._data.requires_grad_(requires_grad)
|
|
638
|
+
|
|
639
|
+
@override
|
|
640
|
+
def requires_grad_(self, mode: bool = True) -> Self:
|
|
641
|
+
self._data.requires_grad_(mode)
|
|
642
|
+
return self
|
|
643
|
+
|
|
644
|
+
@override
|
|
645
|
+
def detach_(self) -> Self:
|
|
646
|
+
raise RuntimeError(
|
|
647
|
+
f"Can't detach a '{self.__class__.__name__} in-place. Use "
|
|
648
|
+
f"'detach() instead."
|
|
649
|
+
)
|
|
650
|
+
|
|
651
|
+
@override
|
|
652
|
+
def tolist(self) -> Any:
|
|
653
|
+
def reshape(values: list[Any], size: tuple[int, ...]) -> Any:
|
|
654
|
+
if len(size) == 0:
|
|
655
|
+
return values[0]
|
|
656
|
+
if len(size) == 1:
|
|
657
|
+
return values
|
|
658
|
+
|
|
659
|
+
step = math.prod(size[1:])
|
|
660
|
+
return [
|
|
661
|
+
reshape(values[i * step : (i + 1) * step], size[1:])
|
|
662
|
+
for i in range(size[0])
|
|
663
|
+
]
|
|
664
|
+
|
|
665
|
+
values = self.to_arrow().to_pylist()
|
|
666
|
+
return reshape(values, tuple(self.size()))
|
|
667
|
+
|
|
668
|
+
@override
|
|
669
|
+
def item(self) -> list[Any] | None: # type: ignore
|
|
670
|
+
if self.numel() != 1:
|
|
671
|
+
raise RuntimeError(
|
|
672
|
+
f"{self.__class__.__name__!r} with {self.numel()} "
|
|
673
|
+
f"elements cannot be converted to a single item"
|
|
674
|
+
)
|
|
675
|
+
return self.view(-1).tolist()[0]
|
|
676
|
+
|
|
677
|
+
def __repr__(self, *, tensor_contents: Any = None) -> str:
|
|
678
|
+
# TODO Support tensor content printing.
|
|
679
|
+
out = f"{self.__class__.__name__}("
|
|
680
|
+
out += f"size={tuple(self.size())}"
|
|
681
|
+
out += f", dtype={self.dtype}"
|
|
682
|
+
if self.valid is not None:
|
|
683
|
+
out += f", null_count={int((~self.valid).sum())}"
|
|
684
|
+
if not self.is_cpu:
|
|
685
|
+
out += f", device={self.device}"
|
|
686
|
+
if self._data.grad_fn is not None:
|
|
687
|
+
out += f", grad_fn=<{type(self._data.grad_fn).__name__}>"
|
|
688
|
+
elif self.requires_grad:
|
|
689
|
+
out += ", requires_grad=True"
|
|
690
|
+
out += ")"
|
|
691
|
+
return out
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
@VarLenTensor.implements(aten.alias.default)
|
|
695
|
+
@preserve_view_inference_mode
|
|
696
|
+
def _alias(inp: VarLenTensor) -> VarLenTensor:
|
|
697
|
+
return inp.__class__(
|
|
698
|
+
data=aten.alias.default(inp._data),
|
|
699
|
+
offset=aten.alias.default(inp._offset),
|
|
700
|
+
valid=aten.alias.default(inp._valid)
|
|
701
|
+
if inp._valid is not None
|
|
702
|
+
else None,
|
|
703
|
+
size=inp.size(),
|
|
704
|
+
stride=inp.stride(),
|
|
705
|
+
storage_offset=int(inp.storage_offset()),
|
|
706
|
+
)
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
@VarLenTensor.implements(aten.record_stream.default)
|
|
710
|
+
def _record_stream(inp: VarLenTensor, stream: torch.Stream) -> None:
|
|
711
|
+
inp._data.record_stream(stream)
|
|
712
|
+
inp._offset.record_stream(stream)
|
|
713
|
+
if inp._valid is not None:
|
|
714
|
+
inp._valid.record_stream(stream)
|
|
715
|
+
|
|
716
|
+
|
|
717
|
+
@VarLenTensor.implements(aten.to.dtype_layout)
|
|
718
|
+
def _to_dtype_layout(
|
|
719
|
+
inp: VarLenTensor,
|
|
720
|
+
*,
|
|
721
|
+
dtype: torch.dtype | None = None,
|
|
722
|
+
layout: torch.layout | None = None,
|
|
723
|
+
device: torch.device | str | None = None,
|
|
724
|
+
pin_memory: bool | None = None, # Ignored by PyTorch.
|
|
725
|
+
non_blocking: bool = False,
|
|
726
|
+
copy: bool = False,
|
|
727
|
+
memory_format: torch.memory_format | None = None,
|
|
728
|
+
) -> VarLenTensor:
|
|
729
|
+
if dtype is None:
|
|
730
|
+
dtype = inp.dtype
|
|
731
|
+
if layout is None:
|
|
732
|
+
layout = inp.layout
|
|
733
|
+
if device is None:
|
|
734
|
+
device = inp.device
|
|
735
|
+
device = torch.device(device)
|
|
736
|
+
if memory_format is None:
|
|
737
|
+
memory_format = torch.preserve_format
|
|
738
|
+
|
|
739
|
+
if inp.ALLOWED_DTYPES is not None and dtype not in inp.ALLOWED_DTYPES:
|
|
740
|
+
raise TypeError(
|
|
741
|
+
f"Can't convert {inp.__class__.__name__!r} to dtype '{dtype}'"
|
|
742
|
+
)
|
|
743
|
+
if layout != torch.strided:
|
|
744
|
+
raise TypeError(
|
|
745
|
+
f"Can't convert {inp.__class__.__name__!r} to layout '{layout}'"
|
|
746
|
+
)
|
|
747
|
+
if memory_format not in (torch.preserve_format, torch.contiguous_format):
|
|
748
|
+
raise ValueError(
|
|
749
|
+
f"Unsupported memory format '{memory_format}' for "
|
|
750
|
+
f"'{inp.__class__.__name__}.clone'"
|
|
751
|
+
)
|
|
752
|
+
|
|
753
|
+
if (
|
|
754
|
+
not copy
|
|
755
|
+
and dtype == inp.dtype
|
|
756
|
+
and device == inp.device
|
|
757
|
+
and layout == inp.layout
|
|
758
|
+
and (
|
|
759
|
+
memory_format == torch.preserve_format
|
|
760
|
+
or (
|
|
761
|
+
memory_format == torch.contiguous_format
|
|
762
|
+
and inp.is_contiguous()
|
|
763
|
+
)
|
|
764
|
+
)
|
|
765
|
+
):
|
|
766
|
+
return inp
|
|
767
|
+
|
|
768
|
+
# Copying has two cases:
|
|
769
|
+
# 1. Slice when the output layout can reuse the input storage order.
|
|
770
|
+
# 2. Materialize in case of holes, overlaps, or change in memory format.
|
|
771
|
+
use_slice = (
|
|
772
|
+
inp.numel() == 0
|
|
773
|
+
or (
|
|
774
|
+
memory_format == torch.preserve_format
|
|
775
|
+
and torch._debug_has_internal_overlap(_layout_view(inp)) == 0
|
|
776
|
+
)
|
|
777
|
+
or (memory_format == torch.contiguous_format and inp.is_contiguous())
|
|
778
|
+
)
|
|
779
|
+
if not use_slice:
|
|
780
|
+
return _materialize(
|
|
781
|
+
inp,
|
|
782
|
+
lambda x: x.clone(memory_format=memory_format),
|
|
783
|
+
device=device,
|
|
784
|
+
dtype=dtype,
|
|
785
|
+
non_blocking=non_blocking,
|
|
786
|
+
)
|
|
787
|
+
|
|
788
|
+
storage_offset = int(inp.storage_offset())
|
|
789
|
+
span_len = _span_len(inp.size(), inp.stride())
|
|
790
|
+
offset = inp._offset[storage_offset : storage_offset + span_len + 1]
|
|
791
|
+
data = inp._data[offset[0] : offset[-1]].to(
|
|
792
|
+
device=device,
|
|
793
|
+
dtype=dtype,
|
|
794
|
+
non_blocking=non_blocking,
|
|
795
|
+
copy=copy,
|
|
796
|
+
)
|
|
797
|
+
offset = (offset - offset[0]).to(device=device, non_blocking=non_blocking)
|
|
798
|
+
valid: Tensor | None = None
|
|
799
|
+
if inp._valid is not None:
|
|
800
|
+
valid = inp._valid[storage_offset : storage_offset + span_len].to(
|
|
801
|
+
device=device,
|
|
802
|
+
non_blocking=non_blocking,
|
|
803
|
+
copy=copy,
|
|
804
|
+
)
|
|
805
|
+
|
|
806
|
+
return inp.__class__(
|
|
807
|
+
data=data,
|
|
808
|
+
offset=offset,
|
|
809
|
+
valid=valid,
|
|
810
|
+
size=inp.size(),
|
|
811
|
+
stride=inp.stride()
|
|
812
|
+
if memory_format == torch.preserve_format
|
|
813
|
+
else None,
|
|
814
|
+
storage_offset=0,
|
|
815
|
+
)
|
|
816
|
+
|
|
817
|
+
|
|
818
|
+
@VarLenTensor.implements(aten.to.dtype)
|
|
819
|
+
def _to_dtype(
|
|
820
|
+
inp: VarLenTensor,
|
|
821
|
+
dtype: torch.dtype,
|
|
822
|
+
non_blocking: bool = False,
|
|
823
|
+
copy: bool = False,
|
|
824
|
+
memory_format: torch.memory_format | None = None,
|
|
825
|
+
) -> VarLenTensor:
|
|
826
|
+
return _to_dtype_layout(
|
|
827
|
+
inp,
|
|
828
|
+
dtype=dtype,
|
|
829
|
+
non_blocking=non_blocking,
|
|
830
|
+
copy=copy,
|
|
831
|
+
memory_format=memory_format,
|
|
832
|
+
)
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
@VarLenTensor.implements(aten.to.device)
|
|
836
|
+
def _to_device(
|
|
837
|
+
inp: VarLenTensor,
|
|
838
|
+
device: torch.device,
|
|
839
|
+
dtype: torch.dtype,
|
|
840
|
+
non_blocking: bool = False,
|
|
841
|
+
copy: bool = False,
|
|
842
|
+
memory_format: torch.memory_format | None = None,
|
|
843
|
+
) -> VarLenTensor:
|
|
844
|
+
return _to_dtype_layout(
|
|
845
|
+
inp,
|
|
846
|
+
dtype=dtype,
|
|
847
|
+
device=device,
|
|
848
|
+
non_blocking=non_blocking,
|
|
849
|
+
copy=copy,
|
|
850
|
+
memory_format=memory_format,
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
|
|
854
|
+
@VarLenTensor.implements(aten.to.other)
|
|
855
|
+
def _to_other(
|
|
856
|
+
inp: VarLenTensor,
|
|
857
|
+
other: Tensor,
|
|
858
|
+
non_blocking: bool = False,
|
|
859
|
+
copy: bool = False,
|
|
860
|
+
memory_format: torch.memory_format | None = None,
|
|
861
|
+
) -> VarLenTensor:
|
|
862
|
+
return _to_dtype_layout(
|
|
863
|
+
inp,
|
|
864
|
+
dtype=other.dtype,
|
|
865
|
+
layout=other.layout,
|
|
866
|
+
device=other.device,
|
|
867
|
+
non_blocking=non_blocking,
|
|
868
|
+
copy=copy,
|
|
869
|
+
memory_format=memory_format,
|
|
870
|
+
)
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
@VarLenTensor.implements(aten._to_copy.default)
|
|
874
|
+
def _to_copy(
|
|
875
|
+
inp: VarLenTensor,
|
|
876
|
+
*,
|
|
877
|
+
dtype: torch.dtype | None = None,
|
|
878
|
+
layout: torch.layout | None = None,
|
|
879
|
+
device: torch.device | str | None = None,
|
|
880
|
+
pin_memory: bool = False, # Ignored by PyTorch.
|
|
881
|
+
non_blocking: bool = False,
|
|
882
|
+
memory_format: torch.memory_format | None = None,
|
|
883
|
+
) -> VarLenTensor:
|
|
884
|
+
return _to_dtype_layout(
|
|
885
|
+
inp,
|
|
886
|
+
dtype=dtype,
|
|
887
|
+
layout=layout,
|
|
888
|
+
device=device,
|
|
889
|
+
pin_memory=pin_memory,
|
|
890
|
+
non_blocking=non_blocking,
|
|
891
|
+
copy=True,
|
|
892
|
+
memory_format=memory_format,
|
|
893
|
+
)
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
@VarLenTensor.implements(aten.clone.default)
|
|
897
|
+
def _clone(
|
|
898
|
+
inp: VarLenTensor,
|
|
899
|
+
*,
|
|
900
|
+
memory_format: torch.memory_format | None = None,
|
|
901
|
+
) -> VarLenTensor:
|
|
902
|
+
return _to_dtype_layout(inp, copy=True, memory_format=memory_format)
|
|
903
|
+
|
|
904
|
+
|
|
905
|
+
@VarLenTensor.implements(aten.contiguous.default)
|
|
906
|
+
def _contiguous(
|
|
907
|
+
inp: VarLenTensor,
|
|
908
|
+
*,
|
|
909
|
+
memory_format: torch.memory_format = torch.contiguous_format,
|
|
910
|
+
) -> VarLenTensor:
|
|
911
|
+
return _to_dtype_layout(inp, copy=True, memory_format=memory_format)
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
@VarLenTensor.implements(aten.detach.default)
|
|
915
|
+
@preserve_view_inference_mode
|
|
916
|
+
def _detach(inp: VarLenTensor) -> VarLenTensor:
|
|
917
|
+
return inp.__class__(
|
|
918
|
+
data=inp._data.detach(),
|
|
919
|
+
offset=inp._offset,
|
|
920
|
+
valid=inp._valid,
|
|
921
|
+
size=inp.size(),
|
|
922
|
+
stride=inp.stride(),
|
|
923
|
+
storage_offset=int(inp.storage_offset()),
|
|
924
|
+
)
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
@VarLenTensor.implements(aten.is_pinned.default)
|
|
928
|
+
def _is_pinned(inp: VarLenTensor) -> bool:
|
|
929
|
+
is_pinned = inp._data.is_pinned() and inp._offset.is_pinned()
|
|
930
|
+
return is_pinned and (inp._valid is None or inp._valid.is_pinned())
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
@VarLenTensor.implements(aten._pin_memory.default)
|
|
934
|
+
def _pin_memory(inp: VarLenTensor) -> VarLenTensor:
|
|
935
|
+
return inp.__class__(
|
|
936
|
+
data=inp._data.pin_memory(),
|
|
937
|
+
offset=inp._offset.pin_memory(),
|
|
938
|
+
valid=inp._valid.pin_memory() if inp._valid is not None else None,
|
|
939
|
+
size=inp.size(),
|
|
940
|
+
stride=inp.stride(),
|
|
941
|
+
storage_offset=int(inp.storage_offset()),
|
|
942
|
+
)
|
|
943
|
+
|
|
944
|
+
|
|
945
|
+
@VarLenTensor.implements(aten.pin_memory.default)
|
|
946
|
+
def _pin_memory_composite(
|
|
947
|
+
inp: VarLenTensor,
|
|
948
|
+
device: torch.device | None = None,
|
|
949
|
+
) -> VarLenTensor:
|
|
950
|
+
if _is_pinned(inp):
|
|
951
|
+
return inp
|
|
952
|
+
return _pin_memory(inp)
|
|
953
|
+
|
|
954
|
+
|
|
955
|
+
@VarLenTensor.implements(aten.isnan.default)
|
|
956
|
+
def _isnan(inp: VarLenTensor) -> Tensor:
|
|
957
|
+
valid = inp.valid
|
|
958
|
+
if valid is None:
|
|
959
|
+
return torch.zeros(
|
|
960
|
+
inp.size(),
|
|
961
|
+
dtype=torch.bool,
|
|
962
|
+
device=inp.device,
|
|
963
|
+
)
|
|
964
|
+
return ~valid
|
|
965
|
+
|
|
966
|
+
|
|
967
|
+
@VarLenTensor.implements(aten.isfinite.default)
|
|
968
|
+
def _isfinite(inp: VarLenTensor) -> Tensor:
|
|
969
|
+
valid = inp.valid
|
|
970
|
+
if valid is None:
|
|
971
|
+
return torch.ones(
|
|
972
|
+
inp.size(),
|
|
973
|
+
dtype=torch.bool,
|
|
974
|
+
device=inp.device,
|
|
975
|
+
)
|
|
976
|
+
return valid.clone()
|
|
977
|
+
|
|
978
|
+
|
|
979
|
+
@VarLenTensor.implements(aten.equal.default)
|
|
980
|
+
def _equal(inp: VarLenTensor, other: Tensor) -> bool:
|
|
981
|
+
if inp.__class__ is not other.__class__:
|
|
982
|
+
return False
|
|
983
|
+
if inp.size() != other.size():
|
|
984
|
+
return False
|
|
985
|
+
|
|
986
|
+
valid1, valid2 = inp.valid, other.valid
|
|
987
|
+
if valid1 is not None and valid2 is not None:
|
|
988
|
+
if not valid1.equal(valid2):
|
|
989
|
+
return False
|
|
990
|
+
elif (valid1 is not None and not valid1.all()) or (
|
|
991
|
+
valid2 is not None and not valid2.all()
|
|
992
|
+
):
|
|
993
|
+
return False
|
|
994
|
+
|
|
995
|
+
inp = cast(
|
|
996
|
+
VarLenTensor,
|
|
997
|
+
inp.contiguous() if valid1 is None else inp[valid1],
|
|
998
|
+
)
|
|
999
|
+
other = cast(
|
|
1000
|
+
VarLenTensor,
|
|
1001
|
+
other.contiguous() if valid2 is None else other[valid2],
|
|
1002
|
+
)
|
|
1003
|
+
|
|
1004
|
+
data1, offset1 = inp.data_offset
|
|
1005
|
+
data2, offset2 = other.data_offset
|
|
1006
|
+
|
|
1007
|
+
if not offset1.equal(offset2):
|
|
1008
|
+
return False
|
|
1009
|
+
|
|
1010
|
+
return data1.equal(data2)
|
|
1011
|
+
|
|
1012
|
+
|
|
1013
|
+
@VarLenTensor.implements(aten.allclose.default)
|
|
1014
|
+
def _allclose(
|
|
1015
|
+
inp: VarLenTensor,
|
|
1016
|
+
other: Tensor,
|
|
1017
|
+
rtol: float = 1e-05,
|
|
1018
|
+
atol: float = 1e-08,
|
|
1019
|
+
equal_nan: bool = False,
|
|
1020
|
+
) -> bool:
|
|
1021
|
+
if inp.__class__ is not other.__class__:
|
|
1022
|
+
return False
|
|
1023
|
+
if inp.size() != other.size():
|
|
1024
|
+
return False
|
|
1025
|
+
|
|
1026
|
+
valid1, valid2 = inp.valid, other.valid
|
|
1027
|
+
if valid1 is not None and valid2 is not None:
|
|
1028
|
+
if not valid1.equal(valid2):
|
|
1029
|
+
return False
|
|
1030
|
+
elif (valid1 is not None and not valid1.all()) or (
|
|
1031
|
+
valid2 is not None and not valid2.all()
|
|
1032
|
+
):
|
|
1033
|
+
return False
|
|
1034
|
+
|
|
1035
|
+
inp = cast(
|
|
1036
|
+
VarLenTensor,
|
|
1037
|
+
inp.contiguous() if valid1 is None else inp[valid1],
|
|
1038
|
+
)
|
|
1039
|
+
other = cast(
|
|
1040
|
+
VarLenTensor,
|
|
1041
|
+
other.contiguous() if valid2 is None else other[valid2],
|
|
1042
|
+
)
|
|
1043
|
+
|
|
1044
|
+
data1, offset1 = inp.data_offset
|
|
1045
|
+
data2, offset2 = other.data_offset
|
|
1046
|
+
|
|
1047
|
+
if not offset1.equal(offset2):
|
|
1048
|
+
return False
|
|
1049
|
+
|
|
1050
|
+
if data1.is_floating_point() and data2.is_floating_point():
|
|
1051
|
+
return data1.allclose(data2, rtol, atol, equal_nan)
|
|
1052
|
+
|
|
1053
|
+
return data1.equal(data2)
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
@VarLenTensor.implements(aten.view.default)
|
|
1057
|
+
@preserve_view_inference_mode
|
|
1058
|
+
def _view(inp: VarLenTensor, size: Sequence[int]) -> VarLenTensor:
|
|
1059
|
+
view = _layout_view(inp).view(tuple(size))
|
|
1060
|
+
return _from_layout_view(inp, view)
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
@VarLenTensor.implements(aten._unsafe_view.default)
|
|
1064
|
+
@preserve_view_inference_mode
|
|
1065
|
+
def _unsafe_view(inp: VarLenTensor, size: Sequence[int]) -> VarLenTensor:
|
|
1066
|
+
view = aten._unsafe_view.default(_layout_view(inp), size)
|
|
1067
|
+
return _from_layout_view(inp, view)
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
@VarLenTensor.implements(aten.reshape.default)
|
|
1071
|
+
def _reshape(inp: VarLenTensor, size: Sequence[int]) -> VarLenTensor:
|
|
1072
|
+
return cast(
|
|
1073
|
+
VarLenTensor,
|
|
1074
|
+
aten.reshape.default.decompose(inp, size),
|
|
1075
|
+
)
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
@VarLenTensor.implements(aten.flatten.using_ints)
|
|
1079
|
+
def _flatten(
|
|
1080
|
+
inp: VarLenTensor,
|
|
1081
|
+
start_dim: int = 0,
|
|
1082
|
+
end_dim: int = -1,
|
|
1083
|
+
) -> VarLenTensor:
|
|
1084
|
+
return cast(
|
|
1085
|
+
VarLenTensor,
|
|
1086
|
+
aten.flatten.using_ints.decompose(inp, start_dim, end_dim),
|
|
1087
|
+
)
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
@VarLenTensor.implements(aten.squeeze.default)
|
|
1091
|
+
@preserve_view_inference_mode
|
|
1092
|
+
def _squeeze(inp: VarLenTensor) -> VarLenTensor:
|
|
1093
|
+
view = _layout_view(inp).squeeze()
|
|
1094
|
+
return _from_layout_view(inp, view)
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
@VarLenTensor.implements(aten.squeeze.dim)
|
|
1098
|
+
@preserve_view_inference_mode
|
|
1099
|
+
def _squeeze_dim(inp: VarLenTensor, dim: int) -> VarLenTensor:
|
|
1100
|
+
view = _layout_view(inp).squeeze(dim)
|
|
1101
|
+
return _from_layout_view(inp, view)
|
|
1102
|
+
|
|
1103
|
+
|
|
1104
|
+
@VarLenTensor.implements(aten.squeeze.dims)
|
|
1105
|
+
@preserve_view_inference_mode
|
|
1106
|
+
def _squeeze_dims(inp: VarLenTensor, dim: Sequence[int]) -> VarLenTensor:
|
|
1107
|
+
view = _layout_view(inp).squeeze(tuple(dim))
|
|
1108
|
+
return _from_layout_view(inp, view)
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
@VarLenTensor.implements(aten.unsqueeze.default)
|
|
1112
|
+
@preserve_view_inference_mode
|
|
1113
|
+
def _unsqueeze(inp: VarLenTensor, dim: int) -> VarLenTensor:
|
|
1114
|
+
view = _layout_view(inp).unsqueeze(dim)
|
|
1115
|
+
return _from_layout_view(inp, view)
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
@VarLenTensor.implements(aten.expand.default)
|
|
1119
|
+
@preserve_view_inference_mode
|
|
1120
|
+
def _expand(
|
|
1121
|
+
inp: VarLenTensor,
|
|
1122
|
+
size: Sequence[int],
|
|
1123
|
+
*,
|
|
1124
|
+
implicit: bool = False,
|
|
1125
|
+
) -> VarLenTensor:
|
|
1126
|
+
view = aten.expand.default(_layout_view(inp), size, implicit=implicit)
|
|
1127
|
+
return _from_layout_view(inp, view)
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
@VarLenTensor.implements(aten.t.default)
|
|
1131
|
+
@preserve_view_inference_mode
|
|
1132
|
+
def _t(inp: VarLenTensor) -> VarLenTensor:
|
|
1133
|
+
view = _layout_view(inp).t()
|
|
1134
|
+
return _from_layout_view(inp, view)
|
|
1135
|
+
|
|
1136
|
+
|
|
1137
|
+
@VarLenTensor.implements(aten.transpose.int)
|
|
1138
|
+
@preserve_view_inference_mode
|
|
1139
|
+
def _transpose(inp: VarLenTensor, dim0: int, dim1: int) -> VarLenTensor:
|
|
1140
|
+
view = _layout_view(inp).transpose(dim0, dim1)
|
|
1141
|
+
return _from_layout_view(inp, view)
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
@VarLenTensor.implements(aten.permute.default)
|
|
1145
|
+
@preserve_view_inference_mode
|
|
1146
|
+
def _permute(inp: VarLenTensor, dims: Sequence[int]) -> VarLenTensor:
|
|
1147
|
+
view = _layout_view(inp).permute(tuple(dims))
|
|
1148
|
+
return _from_layout_view(inp, view)
|
|
1149
|
+
|
|
1150
|
+
|
|
1151
|
+
@VarLenTensor.implements(aten.movedim.int)
|
|
1152
|
+
def _movedim_int(
|
|
1153
|
+
inp: VarLenTensor,
|
|
1154
|
+
source: int,
|
|
1155
|
+
destination: int,
|
|
1156
|
+
) -> VarLenTensor:
|
|
1157
|
+
return cast(
|
|
1158
|
+
VarLenTensor,
|
|
1159
|
+
aten.movedim.int.decompose(inp, source, destination),
|
|
1160
|
+
)
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
@VarLenTensor.implements(aten.movedim.intlist)
|
|
1164
|
+
def _movedim_intlist(
|
|
1165
|
+
inp: VarLenTensor,
|
|
1166
|
+
source: Sequence[int],
|
|
1167
|
+
destination: Sequence[int],
|
|
1168
|
+
) -> VarLenTensor:
|
|
1169
|
+
return cast(
|
|
1170
|
+
VarLenTensor,
|
|
1171
|
+
aten.movedim.intlist.decompose(inp, source, destination),
|
|
1172
|
+
)
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
@VarLenTensor.implements(aten.select.int)
|
|
1176
|
+
@preserve_view_inference_mode
|
|
1177
|
+
def _select(inp: VarLenTensor, dim: int, index: int) -> VarLenTensor:
|
|
1178
|
+
view = _layout_view(inp).select(dim, index)
|
|
1179
|
+
return _from_layout_view(inp, view)
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
@VarLenTensor.implements(aten.slice.Tensor)
|
|
1183
|
+
@preserve_view_inference_mode
|
|
1184
|
+
def _slice(
|
|
1185
|
+
inp: VarLenTensor,
|
|
1186
|
+
dim: int = 0,
|
|
1187
|
+
start: int | None = None,
|
|
1188
|
+
end: int | None = None,
|
|
1189
|
+
step: int = 1,
|
|
1190
|
+
) -> VarLenTensor:
|
|
1191
|
+
view = aten.slice.Tensor(_layout_view(inp), dim, start, end, step)
|
|
1192
|
+
return _from_layout_view(inp, view)
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
@VarLenTensor.implements(aten.narrow.default)
|
|
1196
|
+
@preserve_view_inference_mode
|
|
1197
|
+
def _narrow(
|
|
1198
|
+
inp: VarLenTensor,
|
|
1199
|
+
dim: int,
|
|
1200
|
+
start: int,
|
|
1201
|
+
length: int,
|
|
1202
|
+
) -> VarLenTensor:
|
|
1203
|
+
view = _layout_view(inp).narrow(dim, start, length)
|
|
1204
|
+
return _from_layout_view(inp, view)
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
@VarLenTensor.implements(aten.unbind.int)
|
|
1208
|
+
@preserve_view_inference_mode
|
|
1209
|
+
def _unbind(inp: VarLenTensor, dim: int = 0) -> tuple[VarLenTensor, ...]:
|
|
1210
|
+
return tuple(
|
|
1211
|
+
_from_layout_view(inp, view) for view in _layout_view(inp).unbind(dim)
|
|
1212
|
+
)
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
@VarLenTensor.implements(aten.split.Tensor)
|
|
1216
|
+
@preserve_view_inference_mode
|
|
1217
|
+
def _split(
|
|
1218
|
+
inp: VarLenTensor,
|
|
1219
|
+
split_size: int,
|
|
1220
|
+
dim: int = 0,
|
|
1221
|
+
) -> tuple[VarLenTensor, ...]:
|
|
1222
|
+
return tuple(
|
|
1223
|
+
_from_layout_view(inp, view)
|
|
1224
|
+
for view in _layout_view(inp).split(split_size, dim)
|
|
1225
|
+
)
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
@VarLenTensor.implements(aten.split.sizes)
|
|
1229
|
+
@VarLenTensor.implements(aten.split.default)
|
|
1230
|
+
@VarLenTensor.implements(aten.split_with_sizes.default)
|
|
1231
|
+
@preserve_view_inference_mode
|
|
1232
|
+
def _split_with_sizes(
|
|
1233
|
+
inp: VarLenTensor,
|
|
1234
|
+
split_sizes: Sequence[int],
|
|
1235
|
+
dim: int = 0,
|
|
1236
|
+
) -> tuple[VarLenTensor, ...]:
|
|
1237
|
+
return tuple(
|
|
1238
|
+
_from_layout_view(inp, view)
|
|
1239
|
+
for view in _layout_view(inp).split(tuple(split_sizes), dim)
|
|
1240
|
+
)
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
@VarLenTensor.implements(aten.masked_select.default)
|
|
1244
|
+
def _masked_select(inp: VarLenTensor, mask: Tensor) -> VarLenTensor:
|
|
1245
|
+
return _materialize(inp, lambda x: x.masked_select(mask))
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
@VarLenTensor.implements(aten.index_select.default)
|
|
1249
|
+
def _index_select(
|
|
1250
|
+
inp: VarLenTensor,
|
|
1251
|
+
dim: int,
|
|
1252
|
+
index: Tensor,
|
|
1253
|
+
) -> VarLenTensor:
|
|
1254
|
+
return _materialize(inp, lambda x: x.index_select(dim, index))
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
@VarLenTensor.implements(aten.take.default)
|
|
1258
|
+
def _take(inp: VarLenTensor, index: Tensor) -> VarLenTensor:
|
|
1259
|
+
return _materialize(inp, lambda x: x.take(index))
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
@VarLenTensor.implements(aten.index.Tensor)
|
|
1263
|
+
def _index(
|
|
1264
|
+
inp: VarLenTensor,
|
|
1265
|
+
indices: Sequence[Tensor | None],
|
|
1266
|
+
) -> VarLenTensor:
|
|
1267
|
+
return _materialize(inp, lambda x: aten.index.Tensor(x, indices))
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
@VarLenTensor.implements(aten.cat.default)
|
|
1271
|
+
def _cat(tensors: Sequence[Tensor], dim: int = 0) -> VarLenTensor:
|
|
1272
|
+
if len(tensors) == 0:
|
|
1273
|
+
raise ValueError("Expected a non-empty list of Tensors")
|
|
1274
|
+
|
|
1275
|
+
if not isinstance(tensors[0], VarLenTensor):
|
|
1276
|
+
raise TypeError(
|
|
1277
|
+
f"Expected {VarLenTensor.__name__!r} as element 0, but got "
|
|
1278
|
+
f"{tensors[0].__class__.__name__!r}"
|
|
1279
|
+
)
|
|
1280
|
+
|
|
1281
|
+
tensor_cls = tensors[0].__class__
|
|
1282
|
+
for i, tensor in enumerate(tensors):
|
|
1283
|
+
if tensor.__class__ is not tensor_cls:
|
|
1284
|
+
raise TypeError(
|
|
1285
|
+
f"Expected {tensor_cls.__name__!r} as element {i}, but got "
|
|
1286
|
+
f"{tensor.__class__.__name__!r}"
|
|
1287
|
+
)
|
|
1288
|
+
|
|
1289
|
+
tensors = tuple(
|
|
1290
|
+
cast(VarLenTensor, tensor.contiguous()) for tensor in tensors
|
|
1291
|
+
)
|
|
1292
|
+
data_list, offsets = zip(*(tensor.data_offset for tensor in tensors))
|
|
1293
|
+
valid: Tensor | None = None
|
|
1294
|
+
if any(tensor._valid is not None for tensor in tensors):
|
|
1295
|
+
valid_views = tuple(
|
|
1296
|
+
tensor.valid
|
|
1297
|
+
if tensor.valid is not None
|
|
1298
|
+
else torch.ones(
|
|
1299
|
+
tensor.size(),
|
|
1300
|
+
dtype=torch.bool,
|
|
1301
|
+
device=tensor.device,
|
|
1302
|
+
)
|
|
1303
|
+
for tensor in tensors
|
|
1304
|
+
)
|
|
1305
|
+
valid = torch.cat(valid_views, dim=dim).contiguous().view(-1)
|
|
1306
|
+
|
|
1307
|
+
offset_dtype: torch.dtype = torch.int32
|
|
1308
|
+
if (
|
|
1309
|
+
any(offset.dtype == torch.int64 for offset in offsets)
|
|
1310
|
+
or sum(d.numel() for d in data_list) > torch.iinfo(torch.int32).max
|
|
1311
|
+
):
|
|
1312
|
+
offset_dtype = torch.int64
|
|
1313
|
+
|
|
1314
|
+
dim_size = 0
|
|
1315
|
+
storage_offset = 0
|
|
1316
|
+
start_views, end_views = [], []
|
|
1317
|
+
for tensor, data, offset in zip(tensors, data_list, offsets):
|
|
1318
|
+
offset = offset.to(offset_dtype) + storage_offset
|
|
1319
|
+
dim_size += tensor.size(dim)
|
|
1320
|
+
storage_offset += data.numel()
|
|
1321
|
+
start_views.append(offset[:-1].view(tensor.size()))
|
|
1322
|
+
end_views.append(offset[1:].view(tensor.size()))
|
|
1323
|
+
|
|
1324
|
+
size = tensors[0].size()
|
|
1325
|
+
dim = dim % len(size)
|
|
1326
|
+
size = (*size[:dim], dim_size, *size[dim + 1 :])
|
|
1327
|
+
offset = offsets[0].new_empty(math.prod(size) + 1, dtype=offset_dtype)
|
|
1328
|
+
start = torch.cat(start_views, dim=dim, out=offset[:-1].view(size))
|
|
1329
|
+
data = torch.cat(data_list, dim=0)
|
|
1330
|
+
|
|
1331
|
+
if math.prod(tensors[0].size()[:dim]) == 1: # Contiguous path:
|
|
1332
|
+
offset[-1] = storage_offset
|
|
1333
|
+
return tensor_cls(data=data, offset=offset, valid=valid, size=size)
|
|
1334
|
+
|
|
1335
|
+
end = torch.cat(end_views, dim=dim)
|
|
1336
|
+
start = torch.as_strided(start, size=(start.numel(),), stride=(1,))
|
|
1337
|
+
end = torch.as_strided(end, size=(end.numel(),), stride=(1,))
|
|
1338
|
+
|
|
1339
|
+
offset, index = _compact(start, end)
|
|
1340
|
+
|
|
1341
|
+
return tensor_cls(data=data[index], offset=offset, valid=valid, size=size)
|
|
1342
|
+
|
|
1343
|
+
|
|
1344
|
+
@VarLenTensor.implements(aten.stack.default)
|
|
1345
|
+
def _stack(tensors: Sequence[Tensor], dim: int = 0) -> VarLenTensor:
|
|
1346
|
+
out = torch.cat([tensor.unsqueeze(dim) for tensor in tensors], dim=dim)
|
|
1347
|
+
return cast(VarLenTensor, out)
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
# Helpers #####################################################################
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
def _contiguous_stride(size: Sequence[int]) -> tuple[int, ...]:
|
|
1354
|
+
value = 1
|
|
1355
|
+
stride = []
|
|
1356
|
+
for dim_size in reversed(size):
|
|
1357
|
+
stride.append(value)
|
|
1358
|
+
value *= dim_size
|
|
1359
|
+
return tuple(stride[::-1])
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
def _span_len(size: Sequence[int], stride: Sequence[int]) -> int:
|
|
1363
|
+
if math.prod(size) == 0:
|
|
1364
|
+
return 0
|
|
1365
|
+
return 1 + sum(
|
|
1366
|
+
(dim_size - 1) * dim_stride
|
|
1367
|
+
for dim_size, dim_stride in zip(size, stride)
|
|
1368
|
+
)
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
def _layout_view(inp: VarLenTensor) -> Tensor:
|
|
1372
|
+
return torch.as_strided(
|
|
1373
|
+
inp._offset,
|
|
1374
|
+
size=inp.size(),
|
|
1375
|
+
stride=inp.stride(),
|
|
1376
|
+
storage_offset=int(inp.storage_offset()),
|
|
1377
|
+
)
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
def _from_layout_view(inp: VarLenTensor, view: Tensor) -> VarLenTensor:
|
|
1381
|
+
return inp.__class__(
|
|
1382
|
+
data=inp._data,
|
|
1383
|
+
offset=inp._offset,
|
|
1384
|
+
valid=inp._valid,
|
|
1385
|
+
size=view.size(),
|
|
1386
|
+
stride=view.stride(),
|
|
1387
|
+
storage_offset=0 if view.numel() == 0 else int(view.storage_offset()),
|
|
1388
|
+
)
|
|
1389
|
+
|
|
1390
|
+
|
|
1391
|
+
def _compact(start: Tensor, end: Tensor) -> tuple[Tensor, Tensor]:
|
|
1392
|
+
count = end - start
|
|
1393
|
+
|
|
1394
|
+
offset = count.new_empty(count.numel() + 1, dtype=torch.int64)
|
|
1395
|
+
offset[0] = 0
|
|
1396
|
+
offset[1:] = count.cumsum(dim=0, dtype=torch.int64)
|
|
1397
|
+
|
|
1398
|
+
total = int(offset[-1])
|
|
1399
|
+
if start.dtype == torch.int32 and total <= torch.iinfo(torch.int32).max:
|
|
1400
|
+
offset = offset.to(torch.int32)
|
|
1401
|
+
|
|
1402
|
+
local = torch.arange(
|
|
1403
|
+
end=total,
|
|
1404
|
+
dtype=offset.dtype,
|
|
1405
|
+
device=offset.device,
|
|
1406
|
+
)
|
|
1407
|
+
local -= offset[:-1].repeat_interleave(
|
|
1408
|
+
count,
|
|
1409
|
+
output_size=total,
|
|
1410
|
+
)
|
|
1411
|
+
index = start.repeat_interleave(count, output_size=total)
|
|
1412
|
+
index += local
|
|
1413
|
+
|
|
1414
|
+
return offset, index
|
|
1415
|
+
|
|
1416
|
+
|
|
1417
|
+
def _materialize(
|
|
1418
|
+
inp: VarLenTensor,
|
|
1419
|
+
function: Callable[[Tensor], Tensor],
|
|
1420
|
+
*,
|
|
1421
|
+
device: torch.device | str | None = None,
|
|
1422
|
+
dtype: torch.dtype | None = None,
|
|
1423
|
+
non_blocking: bool = False,
|
|
1424
|
+
) -> VarLenTensor:
|
|
1425
|
+
# Use PyTorch's own memory-format semantics to materialize data:
|
|
1426
|
+
start = torch.as_strided(
|
|
1427
|
+
inp._offset,
|
|
1428
|
+
size=inp.size(),
|
|
1429
|
+
stride=inp.stride(),
|
|
1430
|
+
storage_offset=int(inp.storage_offset()),
|
|
1431
|
+
)
|
|
1432
|
+
start = function(start)
|
|
1433
|
+
assert start.storage_offset() == 0
|
|
1434
|
+
assert _span_len(start.size(), start.stride()) == start.numel()
|
|
1435
|
+
|
|
1436
|
+
end = torch.as_strided(
|
|
1437
|
+
inp._offset,
|
|
1438
|
+
size=inp.size(),
|
|
1439
|
+
stride=inp.stride(),
|
|
1440
|
+
storage_offset=int(inp.storage_offset()) + 1,
|
|
1441
|
+
)
|
|
1442
|
+
end = function(end)
|
|
1443
|
+
assert end.storage_offset() == 0
|
|
1444
|
+
assert _span_len(end.size(), end.stride()) == end.numel()
|
|
1445
|
+
|
|
1446
|
+
size = start.size()
|
|
1447
|
+
stride = start.stride()
|
|
1448
|
+
|
|
1449
|
+
start = torch.as_strided(
|
|
1450
|
+
start,
|
|
1451
|
+
size=(start.numel(),),
|
|
1452
|
+
stride=(1,),
|
|
1453
|
+
storage_offset=start.storage_offset(),
|
|
1454
|
+
)
|
|
1455
|
+
end = torch.as_strided(
|
|
1456
|
+
end,
|
|
1457
|
+
size=(end.numel(),),
|
|
1458
|
+
stride=(1,),
|
|
1459
|
+
storage_offset=end.storage_offset(),
|
|
1460
|
+
)
|
|
1461
|
+
valid: Tensor | None = None
|
|
1462
|
+
if inp._valid is not None:
|
|
1463
|
+
valid = torch.as_strided(
|
|
1464
|
+
inp._valid,
|
|
1465
|
+
size=inp.size(),
|
|
1466
|
+
stride=inp.stride(),
|
|
1467
|
+
storage_offset=int(inp.storage_offset()),
|
|
1468
|
+
)
|
|
1469
|
+
valid = function(valid).contiguous().view(-1)
|
|
1470
|
+
|
|
1471
|
+
offset, index = _compact(start, end)
|
|
1472
|
+
|
|
1473
|
+
return inp.__class__(
|
|
1474
|
+
data=inp._data[index].to(
|
|
1475
|
+
device=device,
|
|
1476
|
+
dtype=dtype,
|
|
1477
|
+
non_blocking=non_blocking,
|
|
1478
|
+
),
|
|
1479
|
+
offset=offset.to(device, non_blocking=non_blocking),
|
|
1480
|
+
valid=valid.to(device=device, non_blocking=non_blocking)
|
|
1481
|
+
if valid is not None
|
|
1482
|
+
else None,
|
|
1483
|
+
size=size,
|
|
1484
|
+
stride=stride,
|
|
1485
|
+
storage_offset=0,
|
|
1486
|
+
)
|