Util
TorchTensorContainerType = Union[torch.Tensor, Collection[torch.Tensor], Mapping[str, torch.Tensor]]
module-attribute
¶
Type for a PyTorch tensor or a container thereof.
TorchNumpyConverter(device=None)
¶
Bases: NumpyConverter[Tensor]
Helper class for converting between torch.Tensor and numpy.ndarray
PARAMETER | DESCRIPTION |
---|---|
device |
Optional device parameter to move the resulting torch tensors to the specified device |
Source code in src/pydvl/influence/torch/util.py
to_numpy(x)
¶
Convert a detached torch.Tensor to numpy.ndarray
from_numpy(x)
¶
Convert a numpy.ndarray to torch.Tensor and optionally move it to a provided device
Source code in src/pydvl/influence/torch/util.py
TorchCatAggregator
¶
Bases: SequenceAggregator[Tensor]
An aggregator that concatenates tensors using PyTorch's torch.cat function. Concatenation is done along the first dimension of the chunks.
__call__(tensor_generator)
¶
Aggregates tensors from a single-level generator into a single tensor by concatenating them. This method is a straightforward way to combine a sequence of tensors into one larger tensor.
PARAMETER | DESCRIPTION |
---|---|
tensor_generator |
A generator that yields |
RETURNS | DESCRIPTION |
---|---|
A single tensor formed by concatenating all tensors from the generator. The concatenation is performed along the default dimension (0). |
Source code in src/pydvl/influence/torch/util.py
NestedTorchCatAggregator
¶
Bases: NestedSequenceAggregator[Tensor]
An aggregator that concatenates tensors using PyTorch's torch.cat function. Concatenation is done along the first two dimensions of the chunks.
__call__(nested_generators_of_tensors)
¶
Aggregates tensors from a nested generator structure into a single tensor by concatenating. Each inner generator is first concatenated along dimension 1 into a tensor, and then these tensors are concatenated along dimension 0 together to form the final tensor.
PARAMETER | DESCRIPTION |
---|---|
nested_generators_of_tensors |
A generator of generators, where each inner
generator yields |
RETURNS | DESCRIPTION |
---|---|
A single tensor formed by concatenating all tensors from the nested |
|
generators. |
Source code in src/pydvl/influence/torch/util.py
to_model_device(x, model)
¶
Returns the tensor x
moved to the device of the model
, if device of model is set
PARAMETER | DESCRIPTION |
---|---|
x |
The tensor to be moved to the device of the model.
TYPE:
|
model |
The model whose device will be used to move the tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
The tensor |
Source code in src/pydvl/influence/torch/util.py
reshape_vector_to_tensors(input_vector, target_shapes)
¶
Reshape a 1D tensor into multiple tensors with specified shapes.
This function takes a 1D tensor (input_vector) and reshapes it into a series of tensors with shapes given by 'target_shapes'. The reshaped tensors are returned as a tuple in the same order as their corresponding shapes.
Note
The total number of elements in 'input_vector' must be equal to the sum of the products of the shapes in 'target_shapes'.
PARAMETER | DESCRIPTION |
---|---|
input_vector |
The 1D tensor to be reshaped. Must be 1D.
TYPE:
|
target_shapes |
An iterable of tuples. Each tuple defines the shape of a tensor to be reshaped from the 'input_vector'. |
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, ...]
|
A tuple of reshaped tensors. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'input_vector' is not a 1D tensor or if the total number of elements in 'input_vector' does not match the sum of the products of the shapes in 'target_shapes'. |
Source code in src/pydvl/influence/torch/util.py
align_structure(source, target)
¶
This function transforms target
to have the same structure as source
, i.e.,
it should be a dictionary with the same keys as source
and each corresponding
value in target
should have the same shape as the value in source
.
PARAMETER | DESCRIPTION |
---|---|
source |
The reference dictionary containing PyTorch tensors. |
target |
The input to be harmonized. It can be a dictionary, tuple, or tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Dict[str, Tensor]
|
The harmonized version of |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in src/pydvl/influence/torch/util.py
align_with_model(x, model)
¶
Aligns an input to the model's parameter structure, i.e. transforms it into a dict with the same keys as model.named_parameters() and matching tensor shapes
PARAMETER | DESCRIPTION |
---|---|
x |
The input to be aligned. It can be a dictionary, tuple, or tensor.
TYPE:
|
model |
model to use for alignment
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
The aligned version of |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in src/pydvl/influence/torch/util.py
flatten_dimensions(tensors, shape=None, concat_at=-1)
¶
Flattens the dimensions of each tensor in the given iterable and concatenates them along a specified dimension.
This function takes an iterable of PyTorch tensors and flattens each tensor.
Optionally, each tensor can be reshaped to a specified shape before concatenation.
The concatenation is performed along the dimension specified by concat_at
.
PARAMETER | DESCRIPTION |
---|---|
tensors |
An iterable containing PyTorch tensors to be flattened and concatenated. |
shape |
A tuple representing the desired shape to which each tensor is reshaped before concatenation. If None, tensors are flattened to 1D. |
concat_at |
The dimension along which to concatenate the tensors.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
A single tensor resulting from the concatenation of the input tensors, |
Tensor
|
each either flattened or reshaped as specified. |
Example
Source code in src/pydvl/influence/torch/util.py
torch_dataset_to_dask_array(dataset, chunk_size, total_size=None, resulting_dtype=np.float32)
¶
Construct tuple of dask arrays from a PyTorch dataset, using dask.delayed
PARAMETER | DESCRIPTION |
---|---|
dataset |
A PyTorch dataset
TYPE:
|
chunk_size |
The size of the chunks for the resulting Dask arrays.
TYPE:
|
total_size |
If the dataset does not implement len, provide the length via this parameter. If None the length of the dataset is inferred via accessing the dataset once. |
resulting_dtype |
The dtype of the resulting dask.array.Array
DEFAULT:
|
Example
RETURNS | DESCRIPTION |
---|---|
Tuple[Array, ...]
|
Tuple of Dask arrays corresponding to each tensor in the dataset. |
Source code in src/pydvl/influence/torch/util.py
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 351 352 353 354 355 356 357 358 359 360 361 362 363 |
|
Created: 2023-12-21