composer.types

This is our reference for common types used throughout our library.

Tensor Types

class composer.types.Tensor

Alias for torch.Tensor

class composer.types.Tensors

Commonly used to represent e.g. a set of inputs, where it is unclear whether each input has its own tensor, or if all the inputs are concatenated in a single tensor.

Type: torch.Tensor, tuple of torch.Tensors, or list of torch.Tensors

Batch Types

A batch of data can be represented in several formats, depending on the application.

class composer.types.Batch

Union type covering the most common representations of batches.

Type: BatchPair, BatchDict, or torch.Tensor

class composer.types.BatchPair

Commonly used in computer vision tasks. The object is assumed to contain exactly two elements, where the first represents inputs and the second represents targets.

Type: List of Tensors

class composer.types.BatchDict

Commonly used in natural language processing tasks.

Type: str to Tensor dict

We provide helper functions to cast batches in situations where the batch type is known.

composer.types.as_batch_dict(batch: composer.core.types.Batch) composer.core.types.BatchDict

Casts a Batch as a BatchDict.

Parameters

batch (Batch) – A batch.

Raises

TypeError – If the batch is not a BatchDict.

Returns

BatchDict – The batch, represented as a BatchDict.

composer.types.as_batch_pair(batch: composer.core.types.Batch) composer.core.types.BatchPair

Casts a Batch as a BatchPair.

Parameters

batch (Batch) – A batch.

Returns

BatchPair – The batch, represented as a BatchPair.

Raises

TypeError – If the batch is not a BatchPair.

Dataset and Data Loader Types

class composer.types.Dataset

Alias for torch.utils.data.Dataset[Batch]

class composer.types.DataLoader(*args, **kwargs)

Protocol for custom DataLoaders compatible with torch.utils.data.DataLoader.

dataset

Dataset from which to load the data.

Type

Dataset

batch_size

How many samples per batch to load (default: 1).

Type

int, optional

num_workers

How many subprocesses to use for data loading. 0 means that the data will be loaded in the main process.

Type

int

pin_memory

If True, the data loader will copy Tensors into CUDA pinned memory before returning them.

Type

bool

drop_last

If len(dataset) is not evenly divisible by batch_size, whether the last batch is dropped (if True) or truncated (if False).

Type

bool

timeout

The timeout for collecting a batch from workers.

Type

float

sampler

The dataloader sampler.

Type

Sampler[int]

prefetch_factor

Number of samples loaded in advance by each worker. 2 means there will be a total of 2 * num_workers samples prefetched across all workers.

Type

int

Trainer Types

class composer.types.Metrics

Union type covering common formats for representing metrics.

Type: Metric or MetricCollection

class composer.types.Optimizer

Alias for torch.optim.Optimizer

class composer.types.Optimizers

Union type for indeterminate amounts of optimizers.

Type: Optimizer or tuple of Optimizer

class composer.types.Scheduler

Alias for torch.optim.lr_scheduler._LRScheduler

class composer.types.Schedulers

Union type for indeterminate amounts of schedulers.

Type: Scheduler or tuple of Scheduler

class composer.types.Scaler

Alias for torch.cuda.amp.grad_scaler.GradScaler

class composer.types.Model

Alias for torch.nn.Module

Miscellaneous Types

class composer.types.ModelParameters

Type alias for model parameters used to initialize optimizers.

Type: List of torch.Tensor or list of str to torch.Tensor dicts

class composer.types.JSON

JSON data.

class composer.types.Precision(value)

Enum class for the numerical precision to be used by the model.

AMP

Use torch.cuda.amp. Only compatible with GPUs.

FP32

Use 32-bit floating-point precision. Compatible with CPUs and GPUs.

class composer.types.TPrefetchFn

Type alias for prefetch functions used for initializing dataloaders.

Type: A function that takes a Batch and returns a Batch

class composer.types.Serializable

Interface for serialization; used by checkpointing.

load_state_dict(state: composer.core.types.StateDict) None

Restores the state of the object.

Parameters

state (StateDict) – The state of the object, as previously returned by state_dict()

state_dict() composer.core.types.StateDict

Returns a dictionary representing the internal state.

The returned dictionary must be pickale-able via torch.save().

Returns

StateDict – The state of the object

class composer.types.StateDict

Class for representing serialized data.

Type: dict with str keys.

exception composer.types.BreakEpochException

Raising this exception will immediately end the current epoch.

If you’re wondering whether you should use this, the answer is no.