base

Interface contract for our models.

class mfai.pytorch.models.base.AutoPaddingModel[source]

Bases: ABC

input_shape: tuple[int, ...]
abstract property settings: Any

Returns the settings instance used to configure for this model.

abstract validate_input_shape(input_shape)[source]
Given an input shape, verifies whether the inputs fit with the

calling model’s specifications.

Parameters:

input_shape (Size) – The shape of the input data, excluding any batch dimension and channel dimension. For example, for a batch of 2D tensors of shape [B,C,W,H], [W,H] should be passed. For 3D data instead of shape [B,C,W,H,D], instead, [W,H,D] should be passed.

Returns:

Returns a tuple where the first element is a boolean signaling whether the given input shape

already fits the model’s requirements. If that value is False, the second element contains the closest shape that fits the model, otherwise it will be None.

Return type:

tuple[bool, Size]

class mfai.pytorch.models.base.BaseModel(*args, **kwargs)[source]

Bases: ModelABC, Module

Parameters:
class mfai.pytorch.models.base.ModelABC[source]

Bases: ABC

check_required_attributes()[source]
Return type:

None

abstract property features_last: bool

Indicates if the features are the last dimension in the input/output tensors. Conv and ViT typically have features as the second dimension (Batch, Features, …) versus GNNs for which features are the last dimension (Batch, …, Features).

property features_second: bool
in_channels: int
input_shape: tuple[int, ...]
abstract property model_type: ModelType

Returns the model type.

abstract property num_spatial_dims: int

Returns the number of spatial dimensions of the instanciated model.

abstract property onnx_supported: bool

Indicates if our model supports onnx export.

out_channels: int
register: bool = False
abstract property settings: Any

Returns the settings instance used to configure for this model.

abstract property settings_kls: Any

Returns the settings class for this model.

abstract property supported_num_spatial_dims: tuple[int, ...]

Returns the number of input spatial dimensions supported by the model. A 2d vision model supporting (H, W) should return (2,). A model supporting both 2d and 3d inputs (by settings) should return (2, 3). Once instanciated the model will be in 2d OR 3d mode.

class mfai.pytorch.models.base.ModelType(value)[source]

Bases: Enum

Enum to classify the models depending on their architecture family. Having the model expose this as an attributee facilitates top level code: reshaping input tensors, iterating only on a subset of models, etc.

CONVOLUTIONAL = 2
GRAPH = 1
IDENTITY = 7
LLM = 4
MULTIMODAL_LLM = 5
PANGU = 6
VISION_TRANSFORMER = 3