Skip to content

Utilities

Minnt framework also provides a few utilities for startup initialization, parameter initialization override, and the current version.

minnt.global_keras_initializers

global_keras_initializers(
    parameter_initialization: bool = True,
    batchnorm_momentum_override: float | None = 0.01,
    norm_layer_epsilon_override: float | None = 0.001,
) -> None

Change default PyTorch initializers to Keras defaults.

The following initializers are used:

  • Linear, Conv1d, Conv2d, Conv3d, ConvTranspose1d, ConvTranspose2d, ConvTranspose3d, Bilinear: Xavier uniform for weights, zeros for biases.
  • Embedding, EmbeddingBag: Uniform [-0.05, 0.05] for weights.
  • RNN, RNNCell, LSTM, LSTMCell, GRU, GRUCell: Xavier uniform for input weights, orthogonal for recurrent weights, zeros for biases (with LSTM forget gate bias set to 1).

Furthermore, for batch normalization layers, the default momentum value is changed from 0.1 to the Keras default of 0.01 (or any other value specified).

Finally, for batch normalization, layer normalization, and group normalization layers, the default epsilon value is changed from 1e-5 to the Keras default of 1e-3 (or any other value specified).

Parameters:

  • parameter_initialization (bool, default: True ) –

    If True, override the default PyTorch initializers with Keras defaults.

  • batchnorm_momentum_override (float | None, default: 0.01 ) –

    If not None, override the default value of batch normalization momentum from 0.1 to this value.

  • norm_layer_epsilon_override (float | None, default: 0.001 ) –

    If not None, override the default value of epsilon for batch normalization, layer normalization, and group normalization layers from 1e-5 to this value.

Source code in minnt/initializers_override.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def global_keras_initializers(
    parameter_initialization: bool = True,
    batchnorm_momentum_override: float | None = 0.01,
    norm_layer_epsilon_override: float | None = 0.001,
) -> None:
    """Change default PyTorch initializers to Keras defaults.

    The following initializers are used:

    - `Linear`, `Conv1d`, `Conv2d`, `Conv3d`, `ConvTranspose1d`, `ConvTranspose2d`, `ConvTranspose3d`, `Bilinear`:
      Xavier uniform for weights, zeros for biases.
    - `Embedding`, `EmbeddingBag`: Uniform [-0.05, 0.05] for weights.
    - `RNN`, `RNNCell`, `LSTM`, `LSTMCell`, `GRU`, `GRUCell`: Xavier uniform for input weights,
      orthogonal for recurrent weights, zeros for biases (with LSTM forget gate bias set to 1).

    Furthermore, for batch normalization layers, the default momentum value is changed
    from 0.1 to the Keras default of 0.01 (or any other value specified).

    Finally, for batch normalization, layer normalization, and group normalization layers,
    the default epsilon value is changed from 1e-5 to the Keras default of 1e-3
    (or any other value specified).

    Parameters:
     parameter_initialization: If True, override the default PyTorch initializers with Keras defaults.
     batchnorm_momentum_override: If not None, override the default value of batch normalization
       momentum from 0.1 to this value.
     norm_layer_epsilon_override: If not None, override the default value of epsilon
       for batch normalization, layer normalization, and group normalization layers from
       1e-5 to this value.
    """
    if parameter_initialization:
        for class_, reset_parameters_method in KerasParameterInitialization.overrides.items():
            class_.reset_parameters = reset_parameters_method

    if batchnorm_momentum_override is not None:
        for batch_norm_super in KerasNormalizationLayers.batch_norms:
            for batch_norm in [batch_norm_super] + batch_norm_super.__subclasses__():
                KerasNormalizationLayers.override_default_argument_value(
                    batch_norm.__init__, "momentum", batchnorm_momentum_override
                )

    if norm_layer_epsilon_override is not None:
        for norm_layer_super in KerasNormalizationLayers.all_norms:
            for norm_layer in [norm_layer_super] + norm_layer_super.__subclasses__():
                KerasNormalizationLayers.override_default_argument_value(
                    norm_layer.__init__, "eps", norm_layer_epsilon_override
                )

minnt.startup

startup(
    seed: int | None = None,
    threads: int | None = None,
    *,
    forkserver_instead_of_fork: bool = True,
    allow_tf32: bool = True,
    expandable_segments: bool | None = True
) -> None

Initialize the environment.

  • Set the random seed if given.
  • Set the number of threads if given.
  • Use forkserver instead of fork multiprocessing start method unless disallowed.
  • Allow using TF32 for matrix multiplication unless disallowed.
  • Enable expandable segments in the CUDA memory allocator unless disallowed.

Parameters:

  • seed (int | None, default: None ) –

    If not None, set the Python, Numpy, and PyTorch random seeds to this value.

  • threads (int | None, default: None ) –

    If not None of 0, set the number of threads to this value. Otherwise, use as many threads as cores.

  • forkserver_instead_of_fork (bool, default: True ) –

    If True, use forkserver instead of fork as the default start multiprocessing method. This will be the default one in Python 3.14.

  • allow_tf32 (bool, default: True ) –

    If False, disable TF32 for matrix multiplication even when available.

  • expandable_segments (bool | None, default: True ) –

    If True, enable expandable segments in the CUDA memory allocator; if False, disable them; if None, do not change the current setting.

Environment variables: The following environment variables can be used to override the method parameters:

  • MINNT_START_METHOD: If set to fork or forkserver, uses the specified method as the multiprocessing start method.
  • MINNT_ALLOW_TF32: If set to 0 or 1, overrides the allow_tf32 parameter.
  • MINTT_EXPANDABLE_SEGMENTS: If set to 0 or 1, overrides the expandable_segments parameter.
Source code in minnt/startup.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
54
55
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
def startup(
    seed: int | None = None,
    threads: int | None = None,
    *,
    forkserver_instead_of_fork: bool = True,
    allow_tf32: bool = True,
    expandable_segments: bool | None = True,
) -> None:
    """Initialize the environment.

    - Set the random seed if given.
    - Set the number of threads if given.
    - Use `forkserver` instead of `fork` multiprocessing start method unless disallowed.
    - Allow using TF32 for matrix multiplication unless disallowed.
    - Enable expandable segments in the CUDA memory allocator unless disallowed.

    Parameters:
      seed: If not `None`, set the Python, Numpy, and PyTorch random seeds to this value.
      threads: If not `None` of 0, set the number of threads to this value.
        Otherwise, use as many threads as cores.
      forkserver_instead_of_fork: If `True`, use `forkserver` instead of `fork` as the
        default start multiprocessing method. This will be the default one in Python 3.14.
      allow_tf32: If `False`, disable TF32 for matrix multiplication even when available.
      expandable_segments: If `True`, enable expandable segments in the CUDA memory allocator;
        if `False`, disable them; if `None`, do not change the current setting.

    **Environment variables:** The following environment variables can be used
    to override the method parameters:

    - `MINNT_START_METHOD`: If set to `fork` or `forkserver`, uses the specified method as
      the multiprocessing start method.
    - `MINNT_ALLOW_TF32`: If set to `0` or `1`, overrides the `allow_tf32` parameter.
    - `MINTT_EXPANDABLE_SEGMENTS`: If set to `0` or `1`, overrides the `expandable_segments` parameter.
    """

    # Set random seed if not None.
    if seed is not None:
        random.seed(seed)
        np.random.seed(seed)
        torch.manual_seed(seed)

    # Set number of threads if > 0; otherwise, use as many threads as cores.
    if threads is not None and threads > 0:
        if torch.get_num_threads() != threads:
            torch.set_num_threads(threads)
        if torch.get_num_interop_threads() != threads:
            torch.set_num_interop_threads(threads)

    # If instructed, use `forkserver` instead of `fork` (which will be the default in Python 3.14).
    if "fork" in torch.multiprocessing.get_all_start_methods():
        if os.environ.get("MINNT_START_METHOD") == "fork":
            if torch.multiprocessing.get_start_method(allow_none=True) != "fork":
                torch.multiprocessing.set_start_method("fork")
        elif forkserver_instead_of_fork or os.environ.get("MINNT_START_METHOD") == "forkserver":
            if torch.multiprocessing.get_start_method(allow_none=True) != "forkserver":
                torch.multiprocessing.set_start_method("forkserver")

    # Allow TF32 for matrix multiplication if available, unless instructed otherwise.
    if os.environ.get("MINNT_ALLOW_TF32") in ["0", "1"]:
        allow_tf32 = os.environ.get("MINNT_ALLOW_TF32") == "1"
    torch.backends.cuda.matmul.allow_tf32 = allow_tf32

    # On NVIDIA GPUs, allow or disallow expandable segments in the CUDA memory allocator if requested.
    if os.environ.get("MINNT_EXPANDABLE_SEGMENTS") in ["0", "1"]:
        expandable_segments = os.environ.get("MINNT_EXPANDABLE_SEGMENTS") == "1"
    if expandable_segments is not None:
        expandable_segments = bool(expandable_segments)
        if f"expandable_segments:{str(not expandable_segments)}" not in os.environ.get("PYTORCH_CUDA_ALLOC_CONF", ""):
            if torch.cuda.is_available() and torch.version.cuda:
                torch.cuda.memory._set_allocator_settings(f"expandable_segments:{str(expandable_segments)}")

minnt.__version__ module-attribute

__version__ = '0.0.4-alpha'