-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
tensor_dataclass.py
350 lines (279 loc) · 13.3 KB
/
tensor_dataclass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# Copyright 2022 the Regents of the University of California, Nerfstudio Team and contributors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tensor dataclass"""
import dataclasses
from copy import deepcopy
from typing import Callable, Dict, List, NoReturn, Optional, Tuple, TypeVar, Union
import numpy as np
import torch
TensorDataclassT = TypeVar("TensorDataclassT", bound="TensorDataclass")
class TensorDataclass:
"""@dataclass of tensors with the same size batch. Allows indexing and standard tensor ops.
Fields that are not Tensors will not be batched unless they are also a TensorDataclass.
Any fields that are dictionaries will have their Tensors or TensorDataclasses batched, and
dictionaries will have their tensors or TensorDataclasses considered in the initial broadcast.
Tensor fields must have at least 1 dimension, meaning that you must convert a field like torch.Tensor(1)
to torch.Tensor([1])
Example:
.. code-block:: python
@dataclass
class TestTensorDataclass(TensorDataclass):
a: torch.Tensor
b: torch.Tensor
c: torch.Tensor = None
# Create a new tensor dataclass with batch size of [2,3,4]
test = TestTensorDataclass(a=torch.ones((2, 3, 4, 2)), b=torch.ones((4, 3)))
test.shape # [2, 3, 4]
test.a.shape # [2, 3, 4, 2]
test.b.shape # [2, 3, 4, 3]
test.reshape((6,4)).shape # [6, 4]
test.flatten().shape # [24,]
test[..., 0].shape # [2, 3]
test[:, 0, :].shape # [2, 4]
"""
_shape: tuple
# A mapping from field-name (str): n (int)
# Any field OR any key in a dictionary field with this name (field-name) and a corresponding
# torch.Tensor will be assumed to have n dimensions after the batch dims. These n final dimensions
# will remain the same shape when doing reshapes, broadcasting, etc on the tensordataclass
_field_custom_dimensions: Dict[str, int] = {}
def __post_init__(self) -> None:
"""Finishes setting up the TensorDataclass
This will 1) find the broadcasted shape and 2) broadcast all fields to this shape 3)
set _shape to be the broadcasted shape.
"""
for k, v in self._field_custom_dimensions.items():
assert (
isinstance(v, int) and v > 1
), f"Custom dimensions must be an integer greater than 1, since 1 is the default, received {k}: {v}"
# Shim to prevent pyright from narrowing `self` to DataclassInstance.
self_dc = self
if not dataclasses.is_dataclass(self_dc):
raise TypeError("TensorDataclass must be a dataclass")
batch_shapes = self._get_dict_batch_shapes({f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)})
if len(batch_shapes) == 0:
raise ValueError("TensorDataclass must have at least one tensor")
batch_shape = torch.broadcast_shapes(*batch_shapes)
broadcasted_fields = self._broadcast_dict_fields(
{f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)}, batch_shape
)
for f, v in broadcasted_fields.items():
object.__setattr__(self, f, v)
object.__setattr__(self, "_shape", batch_shape)
def _get_dict_batch_shapes(self, dict_: Dict) -> List:
"""Returns batch shapes of all tensors in a dictionary
Args:
dict_: The dictionary to get the batch shapes of.
Returns:
The batch shapes of all tensors in the dictionary.
"""
batch_shapes = []
for k, v in dict_.items():
if isinstance(v, torch.Tensor):
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
batch_shapes.append(v.shape[: -self._field_custom_dimensions[k]])
else:
batch_shapes.append(v.shape[:-1])
elif isinstance(v, TensorDataclass):
batch_shapes.append(v.shape)
elif isinstance(v, Dict):
batch_shapes.extend(self._get_dict_batch_shapes(v))
return batch_shapes
def _broadcast_dict_fields(self, dict_: Dict, batch_shape) -> Dict:
"""Broadcasts all tensors in a dictionary according to batch_shape
Args:
dict_: The dictionary to broadcast.
Returns:
The broadcasted dictionary.
"""
new_dict = {}
for k, v in dict_.items():
if isinstance(v, torch.Tensor):
# Apply field-specific custom dimensions.
if isinstance(self._field_custom_dimensions, dict) and k in self._field_custom_dimensions:
new_dict[k] = v.broadcast_to(
(
*batch_shape,
*v.shape[-self._field_custom_dimensions[k] :],
)
)
else:
new_dict[k] = v.broadcast_to((*batch_shape, v.shape[-1]))
elif isinstance(v, TensorDataclass):
new_dict[k] = v.broadcast_to(batch_shape)
elif isinstance(v, Dict):
new_dict[k] = self._broadcast_dict_fields(v, batch_shape)
else:
# Don't broadcast the remaining fields
new_dict[k] = v
return new_dict
def __getitem__(self: TensorDataclassT, indices) -> TensorDataclassT:
if isinstance(indices, (torch.Tensor)):
return self._apply_fn_to_fields(lambda x: x[indices])
if isinstance(indices, (int, slice, type(Ellipsis))):
indices = (indices,)
assert isinstance(indices, tuple)
def tensor_fn(x):
return x[indices + (slice(None),)]
def dataclass_fn(x):
return x[indices]
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k]
return v[indices + ((slice(None),) * custom_dims)]
return self._apply_fn_to_fields(tensor_fn, dataclass_fn, custom_tensor_dims_fn=custom_tensor_dims_fn)
def __setitem__(self, indices, value) -> NoReturn:
raise RuntimeError("Index assignment is not supported for TensorDataclass")
def __len__(self) -> int:
if len(self._shape) == 0:
raise TypeError("len() of a 0-d tensor")
return self.shape[0]
def __bool__(self) -> bool:
if len(self) == 0:
raise ValueError(
f"The truth value of {self.__class__.__name__} when `len(x) == 0` "
"is ambiguous. Use `len(x)` or `x is not None`."
)
return True
@property
def shape(self) -> Tuple[int, ...]:
"""Returns the batch shape of the tensor dataclass."""
return self._shape
@property
def size(self) -> int:
"""Returns the number of elements in the tensor dataclass batch dimension."""
if len(self._shape) == 0:
return 1
return int(np.prod(self._shape))
@property
def ndim(self) -> int:
"""Returns the number of dimensions of the tensor dataclass."""
return len(self._shape)
def reshape(self: TensorDataclassT, shape: Tuple[int, ...]) -> TensorDataclassT:
"""Returns a new TensorDataclass with the same data but with a new shape.
This should deepcopy as well.
Args:
shape: The new shape of the tensor dataclass.
Returns:
A new TensorDataclass with the same data but with a new shape.
"""
if isinstance(shape, int):
shape = (shape,)
def tensor_fn(x):
return x.reshape((*shape, x.shape[-1]))
def dataclass_fn(x):
return x.reshape(shape)
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k]
return v.reshape((*shape, *v.shape[-custom_dims:]))
return self._apply_fn_to_fields(tensor_fn, dataclass_fn, custom_tensor_dims_fn=custom_tensor_dims_fn)
def flatten(self: TensorDataclassT) -> TensorDataclassT:
"""Returns a new TensorDataclass with flattened batch dimensions
Returns:
TensorDataclass: A new TensorDataclass with the same data but with a new shape.
"""
return self.reshape((-1,))
def broadcast_to(self: TensorDataclassT, shape: Union[torch.Size, Tuple[int, ...]]) -> TensorDataclassT:
"""Returns a new TensorDataclass broadcast to new shape.
Changes to the original tensor dataclass should effect the returned tensor dataclass,
meaning it is NOT a deepcopy, and they are still linked.
Args:
shape: The new shape of the tensor dataclass.
Returns:
A new TensorDataclass with the same data but with a new shape.
"""
def custom_tensor_dims_fn(k, v):
custom_dims = self._field_custom_dimensions[k]
return v.broadcast_to((*shape, *v.shape[-custom_dims:]))
return self._apply_fn_to_fields(
lambda x: x.broadcast_to((*shape, x.shape[-1])), custom_tensor_dims_fn=custom_tensor_dims_fn
)
def to(self: TensorDataclassT, device) -> TensorDataclassT:
"""Returns a new TensorDataclass with the same data but on the specified device.
Args:
device: The device to place the tensor dataclass.
Returns:
A new TensorDataclass with the same data but on the specified device.
"""
return self._apply_fn_to_fields(lambda x: x.to(device))
def pin_memory(self: TensorDataclassT) -> TensorDataclassT:
"""Pins the tensor dataclass memory
Returns:
TensorDataclass: A new TensorDataclass with the same data but pinned.
"""
return self._apply_fn_to_fields(lambda x: x.pin_memory())
def _apply_fn_to_fields(
self: TensorDataclassT,
fn: Callable,
dataclass_fn: Optional[Callable] = None,
custom_tensor_dims_fn: Optional[Callable] = None,
) -> TensorDataclassT:
"""Applies a function to all fields of the tensor dataclass.
TODO: Someone needs to make a high level design choice for whether not not we want this
to apply the function to any fields in arbitray superclasses. This is an edge case until we
upgrade to python 3.10 and dataclasses can actually be subclassed with vanilla python and no
janking, but if people try to jank some subclasses that are grandchildren of TensorDataclass
(imagine if someone tries to subclass the RayBundle) this will matter even before upgrading
to 3.10 . Currently we aren't going to be able to work properly for grandchildren, but you
want to use self.__dict__ if you want to apply this to grandchildren instead of our dictionary
from dataclasses.fields(self) as we do below and in other places.
Args:
fn: The function to apply to tensor fields.
dataclass_fn: The function to apply to TensorDataclass fields.
Returns:
A new TensorDataclass with the same data but with a new shape.
"""
self_dc = self
assert dataclasses.is_dataclass(self_dc)
new_fields = self._apply_fn_to_dict(
{f.name: getattr(self, f.name) for f in dataclasses.fields(self_dc)},
fn,
dataclass_fn,
custom_tensor_dims_fn,
)
return dataclasses.replace(self_dc, **new_fields)
def _apply_fn_to_dict(
self,
dict_: Dict,
fn: Callable,
dataclass_fn: Optional[Callable] = None,
custom_tensor_dims_fn: Optional[Callable] = None,
) -> Dict:
"""A helper function for _apply_fn_to_fields, applying a function to all fields of dict_
Args:
dict_: The dictionary to apply the function to.
fn: The function to apply to tensor fields.
dataclass_fn: The function to apply to TensorDataclass fields.
Returns:
A new dictionary with the same data but with a new shape. Will deep copy"""
field_names = dict_.keys()
new_dict = {}
for f in field_names:
v = dict_[f]
if v is not None:
if isinstance(v, TensorDataclass) and dataclass_fn is not None:
new_dict[f] = dataclass_fn(v)
# This is the case when we have a custom dimensions tensor
elif (
isinstance(v, torch.Tensor)
and f in self._field_custom_dimensions
and custom_tensor_dims_fn is not None
):
new_dict[f] = custom_tensor_dims_fn(f, v)
elif isinstance(v, (torch.Tensor, TensorDataclass)):
new_dict[f] = fn(v)
elif isinstance(v, Dict):
new_dict[f] = self._apply_fn_to_dict(v, fn, dataclass_fn)
else:
new_dict[f] = deepcopy(v)
return new_dict