Types#

Support for runtime type validation and conversion.

class tmol.types.Casting(*values)[source]#

Bases: Enum

Casting specifications for array types, see ndarray.astype.

class tmol.types.ConvertAttrs[source]#

Bases: object

Convert attrs fields according to their declared annotations.

class tmol.types.Dim(size)[source]#

Bases: object

One dimension in a runtime-validated tensor shape.

class tmol.types.NDArray[source]#

Bases: _TensorType

Runtime-checkable NumPy array annotation with dtype and shape metadata.

class tmol.types.Shape(dims)[source]#

Bases: object

Runtime-validatable tensor shape specification.

class Factory[source]#

Bases: object

Construct Shape objects through subscription syntax.

validate(shape: Sequence[int]) bool[source]#

Validate concrete dimensions against this specification.

class tmol.types.SubscriptableType[source]#

Bases: type

This metaclass will allow a type to become subscriptable.

>>> class SomeType(metaclass=SubscriptableType):
...     pass
>>> SomeTypeSub = SomeType['some args']
>>> SomeTypeSub.__args__
'some args'
>>> SomeTypeSub.__origin__.__name__
'SomeType'
class tmol.types.Tensor[source]#

Bases: _TensorType

Runtime-checkable PyTorch tensor annotation with dtype and shape metadata.

class tmol.types.TensorGroup[source]#

Bases: object

Mixin for immutable structures whose fields are tensors or tensor groups.

to(*args, **kwargs)[source]#

Perform dtype/device conversion for all subtensors.

Note that this may be an invalid operations if the TensorGroup contains heterogenous tensor dtypes.

Performs Tensor dtype and/or device conversion. A torch.dtype and torch.device are inferred from the arguments of self.to(*args, **kwargs).

If all subtensors already have the correct dtype and device then self is returned.

class tmol.types.ValidateAttrs[source]#

Bases: object

Validate attrs fields according to their declared annotations.

tmol.types.cat(seq, dim=0, out=None)[source]#

Concatenate tensors or compatible tensor groups along a dimension.

tmol.types.constructor_convert(type_annotation='__no__default__', value='__no__default__')[source]#

Convert a value by calling the annotated type when necessary.

tmol.types.convert_args(f)[source]#

Decorate a callable with runtime conversion of annotated values.

tmol.types.get_converter(type_annotation)[source]#

Return the registered value converter for a type annotation.

tmol.types.get_validator(type_annotation)[source]#

Return the registered runtime validator for a type annotation.

tmol.types.like_kwargs(t: Tensor)[source]#

Extract kwargs args needed to initialize an identical tensor.

tmol.types.register_converter(type_predicate, converter)[source]#

Register a converter factory for annotations matching a predicate.

tmol.types.register_validator(type_predicate, validator)[source]#

Register a validator factory for annotations matching a predicate.

tmol.types.torch_dtype(dt)[source]#
tmol.types.torch_dtype(dt: dtype)

Resolve a torch dtype via numpy’s dtype parsing system.

tmol.types.union_convert(union_annotation='__no__default__', value='__no__default__')[source]#

Convert a value using the first compatible member of a union.

tmol.types.validate_args(f)[source]#

Decorate a callable with runtime validation of annotated values.

tmol.types.validate_convert(type_annotation='__no__default__', value='__no__default__')[source]#

Validate a value against an annotation and return it unchanged.

tmol.types.validate_isinstance(type_annotation='__no__default__', value='__no__default__')[source]#

Require a value to be an instance of the annotated type.