modalities.nn package

Subpackages

Submodules

modalities.nn.attention module

class modalities.nn.attention.AttentionConfig(**data)[source]

Bases: BaseModel

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Parameters:

attention_engine_type (AttentionEngineType)

attention_engine_type: AttentionEngineType
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class modalities.nn.attention.AttentionEngineType(value)[source]

Bases: str, Enum

DEFAULT_ATTENTION = 'default_attention'
PYTORCH_FLASH_ATTENTION = 'pytorch_flash_attention'
class modalities.nn.attention.AttentionType(value)[source]

Bases: str, Enum

CAUSAL_SELF_ATTENTION = 'causal_self_attention'
CROSS_ATTENTION = 'cross_attention'
NON_CAUSAL_SELF_ATTENTION = 'non_causal_self_attention'
class modalities.nn.attention.MultiHeadAttention(attention_config=None, attention_type=AttentionType.CAUSAL_SELF_ATTENTION, n_embd=768, n_head=8, bias=True, dropout=0.0, block_size=1024)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
forward(x, context=None)[source]

Define 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 Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:

modalities.nn.mlp module

class modalities.nn.mlp.MLP(in_features, hidden_features=None, out_features=None, bias=True, dropout=0.0, act_fn=<class 'torch.nn.modules.activation.GELU'>)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
forward(x)[source]

Define 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 Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor

Parameters:

x (Tensor)

Module contents