Terrace API
terrace.batch module
- class terrace.batch.BatchView(batch: Batch, index: int | slice)[source]
Bases:
BatchViewBase[T]View of an item in a batch. Should act like said item in most circumstances. We use views instead of creating actual items because, for many use cases, lazily indexing batches is much faster
- class terrace.batch.Batchable[source]
Bases:
objectBase class for all objects we want to batchify
This method can also define static methods collate_{attribute} and index_{attribute}
- class terrace.batch.DataLoader(dataset, batch_size=1, shuffle=False, **kwargs)[source]
Bases:
DataLoaderDataloader that correctly batchifies Batchable data
- batch_size: int | None
- dataset: Dataset[T_co]
- drop_last: bool
- num_workers: int
- pin_memory: bool
- pin_memory_device: str
- prefetch_factor: int | None
- sampler: Sampler | Iterable
- timeout: float
- class terrace.batch.LazyBatch(items)[source]
Bases:
BatchBase[T]This is mainly used if you’re recollating after indexing a batch ( eg collate([batch[0]])). If you use collate([batch[0]], lazy=True) (recommended), it will return a LazyBatch. This is nice because it will only re-collate the attributes of the batch on the fly
terrace.categorical_tensor module
- class terrace.categorical_tensor.CategoricalTensor(tensor: Tensor, num_classes: int | Tuple[int, ...])[source]
Bases:
TensorSubclass of torch Tensors with
dtypelong. They have an additional num_classes member that is either an int of a tuple. If num_classes is an int, it is the number of classes in the tensor as whole (that is, all numbers in the tensor are in the range [0, num_classes)), If num_classes is a tuple, is is the number of classes along the last dimension of the tensor. For instance, ift.num_classesis(4,8), thent[...0]has 4 classes andt[...,1]has 8 classes.
- class terrace.categorical_tensor.NoStackCatTensor(tensor: Tensor, num_classes: int | Tuple[int, ...])[source]
Bases:
CategoricalTensor
terrace.dataframe module
terrace.graph module
- class terrace.graph.Graph(nodes: Sequence[N], edges: Sequence[Tuple[int, int]], edata: List[E] | None = None, directed: bool = False, device='cpu')[source]
Bases:
GraphBase[N,E],BatchableWrapper around dgl graph allowing easier access to data
- class terrace.graph.GraphBase(_dgl_graph: graph | batch, _node_type_tree: TypeTree, _edge_type_tree: TypeTree | None)[source]
Bases:
Generic[N,E]- property edges: List[Tuple[int, int]]
- class terrace.graph.GraphBatch(items: List[Graph[N, E]])[source]
Bases:
BatchBase[G],GraphBase- edge_slices: List[slice]
- node_slices: List[slice]
- class terrace.graph.GraphBatchView(batch: Batch, index: int | slice)[source]
Bases:
BatchViewBase[G]- property edges: List[Tuple[int, int]]
- class terrace.graph.TypeTree(type: Type, subtypes: Dict[str, TypeTree])[source]
Bases:
object- type: Type
- terrace.graph.flatten_batch(b: Batch) Tuple[Dict[str, Tensor], TypeTree][source]
Flattens batch to a single dict from keys to tensors. Also returns all the batch types. This is neccessary because dgl stores ndata and edata as such dicts.
- terrace.graph.make_batch_from_unflat_dict(unflat_dict, type_tree)[source]
Helper function for unflatten_batch. Makes a batch from a dict that has been unflattened
terrace.module module
- class terrace.module.LazyEmbedding(embedding_dims: Tuple[int] | int)[source]
Bases:
ModuleLazyEmbedding uses the num_classes from CategoricalTensors to determine embedding weight size. Note that it assumes tensors have shape (…, N) where N is the number of categorical features. So, in most cases where you have a batch of single categorical features, you must give the embedding a tensor of shape (B, 1). Admittedly this is a bit weird, but it does nicely extend to cases where you have multiple categorical features. In this case, the embedding creates an
nn.Embeddinglayer for each feature and concatenates the result together. Thus the output of this layer has shape (B, E*N), where E is theembedding_dim.- forward(x: CategoricalTensor)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class terrace.module.LazyLayerNorm(*args, **kwargs)[source]
Bases:
WrapperModule- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class terrace.module.LazyLinear(*args, **kwargs)[source]
Bases:
WrapperModuletorch >=1.13 has this, but wanted to try my hand at implementing it myself (and ensuring that older torch versions work)
- forward(x)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class terrace.module.LazyMultiheadAttention(*args, **kwargs)[source]
Bases:
WrapperModule- forward(q, k, v)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- class terrace.module.Module[source]
Bases:
Module- parameters(recurse: bool = True)[source]
Returns an iterator over module parameters.
This is typically passed to an optimizer.
- Parameters:
recurse (bool) – if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module.
- Yields:
Parameter – module parameter
Example:
>>> # xdoctest: +SKIP("undefined vars") >>> for param in model.parameters(): >>> print(type(param), param.size()) <class 'torch.Tensor'> (20L,) <class 'torch.Tensor'> (20L, 1L, 5L, 5L)
- training: bool