Summary¶
multimolecule.utils.summary.calculate_flops
¶
calculate_flops(
model: Module,
*args,
module_ops: Mapping[type, Callable] | None = None,
excluded_modules: Type | Tuple[Type, ...] | None = None,
format_spec: str | None = None,
**kwargs
) -> int | str
Calculate the number of FLOPs (floating point operations) in a PyTorch model.
This performs a single forward pass with hooks attached to count operations per module. For HuggingFace transformer models, attention matrix operations (Q@K^T and attn@V) that use raw torch.matmul are automatically estimated from the model config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The model for which to calculate the FLOPs. |
required |
|
Positional arguments forwarded to |
()
|
|
|
Mapping[type, Callable]
|
Custom per-module-type
hooks. Each callable has signature |
None
|
|
type | tuple[type, ...]
|
Module types to exclude from the calculation. |
None
|
|
str
|
A format specifier to format the output. If is None, the number of FLOPs is returned as an int. If is not None, the number of FLOPs is returned as a str formatted according to the format specifier. Default to None. |
None
|
|
Keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
int | str
|
int | str: The number of FLOPs in the model. |
Examples:
| Python Console Session | |
|---|---|
Source code in multimolecule/utils/summary/flops.py
multimolecule.utils.summary.calculate_macs
¶
calculate_macs(
model: Module,
*args,
module_ops: Mapping[type, Callable] | None = None,
excluded_modules: Type | Tuple[Type, ...] | None = None,
format_spec: str | None = None,
**kwargs
) -> int | str
Calculate the number of MACs (multiply-accumulate operations) in a PyTorch model.
This performs a single forward pass with hooks attached to count operations per module. For HuggingFace transformer models, attention matrix operations (Q@K^T and attn@V) that use raw torch.matmul are automatically estimated from the model config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The model for which to calculate the MACs. |
required |
|
Positional arguments forwarded to |
()
|
|
|
Mapping[type, Callable]
|
Custom per-module-type
hooks. Each callable has signature |
None
|
|
type | tuple[type, ...]
|
Module types to exclude from the calculation. |
None
|
|
str
|
A format specifier to format the output. If is None, the number of MACs is returned as an int. If is not None, the number of MACs is returned as a str formatted according to the format specifier. Default to None. |
None
|
|
Keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
int | str
|
int | str: The number of MACs in the model. |
Examples:
| Python Console Session | |
|---|---|
Source code in multimolecule/utils/summary/flops.py
multimolecule.utils.summary.count_parameters
¶
count_parameters(
model: Module,
trainable: bool = True,
unique: bool = True,
format_spec: str | None = None,
) -> int | str
Count the number of parameters in a PyTorch model, optionally only counting those that require gradients (i.e., are trainable) and/or are unique.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The model for which to count the parameters. |
required |
|
bool
|
Whether to count only parameters that require gradients. Default to True. |
True
|
|
bool
|
Whether to count only unique parameters. Default to True. |
True
|
|
str
|
A format specifier to format the output. If is None, the number of parameters is returned as an int. If is not None, the number of parameters is returned as a str formatted according to the format specifier. Default to None. |
None
|
Returns:
| Type | Description |
|---|---|
int | str
|
int | str: The number of parameters in the model, according to the criteria specified by |
Examples:
| Python Console Session | |
|---|---|
| Python Console Session | |
|---|---|
| Python Console Session | |
|---|---|
| Python Console Session | |
|---|---|