Skip to content

zamba.pytorch.utils

build_multilayer_perceptron(input_size, hidden_layer_sizes, output_size, activation=torch.nn.ReLU, dropout=None, output_dropout=None, output_activation=None)

Builds a multilayer perceptron.

Parameters:

Name Type Description Default
input_size int

Size of first input layer.

required
hidden_layer_sizes tuple of int

If provided, size of hidden layers.

required
output_size int

Size of the last output layer.

required
activation Module

Activation layer between each pair of layers.

ReLU
dropout float

If provided, insert dropout layers with the following dropout rate in between each pair of layers.

None
output_dropout float

If provided, insert a dropout layer with the following dropout rate before the output.

None
output_activation Module

Activation layer after the final layer.

None

Returns: torch.nn.Sequential

Source code in zamba/pytorch/utils.py
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def build_multilayer_perceptron(
    input_size: int,
    hidden_layer_sizes: Optional[Tuple[int]],
    output_size: int,
    activation: Optional[torch.nn.Module] = torch.nn.ReLU,
    dropout: Optional[float] = None,
    output_dropout: Optional[float] = None,
    output_activation: Optional[torch.nn.Module] = None,
) -> torch.nn.Sequential:
    """Builds a multilayer perceptron.

    Args:
        input_size (int): Size of first input layer.
        hidden_layer_sizes (tuple of int, optional): If provided, size of hidden layers.
        output_size (int): Size of the last output layer.
        activation (torch.nn.Module, optional): Activation layer between each pair of layers.
        dropout (float, optional): If provided, insert dropout layers with the following dropout
            rate in between each pair of layers.
        output_dropout (float, optional): If provided, insert a dropout layer with the following
            dropout rate before the output.
        output_activation (torch.nn.Module, optional): Activation layer after the final layer.
    Returns:
        torch.nn.Sequential
    """
    if (hidden_layer_sizes is None) or len(hidden_layer_sizes) == 0:
        return torch.nn.Linear(input_size, output_size)

    layers = [torch.nn.Linear(input_size, hidden_layer_sizes[0])]
    if activation is not None:
        layers.append(activation())

    if (dropout is not None) and (dropout > 0):
        layers.append(torch.nn.Dropout(dropout))

    for in_size, out_size in zip(hidden_layer_sizes[:-1], hidden_layer_sizes[1:]):
        layers.append(torch.nn.Linear(in_size, out_size))
        if activation is not None:
            layers.append(activation())

        if (dropout is not None) and (dropout > 0):
            layers.append(torch.nn.Dropout(dropout))

    layers.append(torch.nn.Linear(hidden_layer_sizes[-1], output_size))

    if (output_dropout is not None) and (output_dropout > 0):
        layers.append(torch.nn.Dropout(dropout))

    if output_activation is not None:
        layers.append(output_activation())

    return torch.nn.Sequential(*layers)

configure_inference_determinism(*, seed=None, deterministic=False)

Seed RNGs for inference and optionally enable strict GPU determinism.

Seeding always runs so frame sampling and other stochastic preprocessing are reproducible. When deterministic is True, also request deterministic CUDA/cuDNN algorithms (best effort; may reduce GPU throughput).

Parameters:

Name Type Description Default
seed Optional[int]

Random seed for Python, NumPy, and PyTorch. Defaults to INFERENCE_SEED from settings (env INFERENCE_SEED, default 55).

None
deterministic bool

If True, enable strict deterministic CUDA/cuDNN algorithms where supported (best effort; some GPU ops may remain non-deterministic and will warn rather than error). Disables cuDNN benchmark mode. May reduce GPU throughput.

False
Source code in zamba/pytorch/utils.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def configure_inference_determinism(
    *,
    seed: Optional[int] = None,
    deterministic: bool = False,
) -> None:
    """Seed RNGs for inference and optionally enable strict GPU determinism.

    Seeding always runs so frame sampling and other stochastic preprocessing are
    reproducible. When ``deterministic`` is True, also request deterministic CUDA/cuDNN
    algorithms (best effort; may reduce GPU throughput).

    Args:
        seed: Random seed for Python, NumPy, and PyTorch. Defaults to ``INFERENCE_SEED``
            from settings (env ``INFERENCE_SEED``, default 55).
        deterministic: If True, enable strict deterministic CUDA/cuDNN algorithms where
            supported (best effort; some GPU ops may remain non-deterministic and will warn
            rather than error). Disables cuDNN benchmark mode. May reduce GPU throughput.
    """
    effective_seed = INFERENCE_SEED if seed is None else seed
    pl.seed_everything(effective_seed, workers=True)

    if deterministic:
        # cuBLAS needs this for reproducible GEMMs; must be set before CUDA init.
        os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":4096:8")
        torch.use_deterministic_algorithms(True, warn_only=True)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

filter_scheduler_params(scheduler_cls, params)

Return scheduler kwargs supported by the scheduler constructor.

Configs and checkpoints may include deprecated args (e.g. verbose was removed from PyTorch lr schedulers in 2.7).

Source code in zamba/pytorch/utils.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def filter_scheduler_params(
    scheduler_cls: Type,
    params: Optional[Dict[str, Any]],
) -> Dict[str, Any]:
    """Return scheduler kwargs supported by the scheduler constructor.

    Configs and checkpoints may include deprecated args (e.g. ``verbose`` was removed
    from PyTorch lr schedulers in 2.7).
    """
    if not params:
        return {}
    signature = inspect.signature(scheduler_cls.__init__)
    supported = set(signature.parameters) - {"self", "optimizer"}
    return {key: value for key, value in params.items() if key in supported}