Utilities
Minnt framework provides a few utilities for startup initialization, parameter initialization override, logdir formatting, tensor manipulation, logging, and versioning.
Startup
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
forkserverinstead offorkmultiprocessing 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
Noneof 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, useforkserverinstead offorkas 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; ifFalse, disable them; ifNone, 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 toforkorforkserver, uses the specified method as the multiprocessing start method.MINNT_ALLOW_TF32: If set to0or1, overrides theallow_tf32parameter.MINNT_EXPANDABLE_SEGMENTS: If set to0or1, overrides theexpandable_segmentsparameter.
Source code in minnt/startup_impl.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 | |
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
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 142 | |
Formatting
minnt.format_logdir
Format the log directory path by filling in placeholders.
The logdir_template is formatted using str.format, where the {key} placeholders
are replaced by the corresponding values from kwargs. Importantly, several
placeholders are always provided automatically:
{config}: A comma-separated list ofkey=valuepairs for sorted key-value items inkwargs. The keys are abbreviated to their first character per segment (with segments separated by hyphens or underscores). The maximum length of the placeholder is limited to 200 characters; if exceeded, the longest entries are truncated with ellipses (...) to fit within the limit.{file}: The base name of the script file (without extension) that called this function; empty string if called from an interactive environment (e.g., Jupyter notebook).{timestamp}: The current date and time in the formatYYYYMMDD_HHMMSS.
Path-unsafe characters in the placeholder values are replaced with underscores, and for convenience, several additional variants of each placeholder are supported:
{key-},{key_}: same as{key}, but with additional hyphen/underscore if the value is non-empty,{-key},{_key}: same as{key}, but with leading hyphen/underscore if the value is non-empty.
Finally, both slashes and backslashes are replaced with the current OS path separator.
Parameters:
-
logdir_template(str) –The log directory template with placeholders.
-
**kwargs(Any, default:{}) –The keyword arguments to fill the template.
Returns:
-
str–The formatted log directory path.
Example
parser = argparse.ArgumentParser()
...
args = parser.parse_args()
logdir = minnt.format_logdir("logs/{file-}{timestamp}{-config}", **vars(args))
Source code in minnt/format_logdir_impl.py
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 83 | |
Tensor Manipulation
minnt.tensors_to_device
tensors_to_device(x: TensorOrTensors, device: device) -> TensorOrTensors
Asynchronously move the input tensor or the input tensor structure to the given device.
Parameters:
-
x(TensorOrTensors) –The input tensor or tensor structure to move to the device, where tensor structures can be tuples, lists, or dictionaries containing other tensor structures and non-tensor values, or completely custom data structures. All tensors in tuples, lists, and dictionary values are moved.
-
device(device) –The device to move the tensors to.
Returns:
-
TensorOrTensors–The input tensor or tensor structure with all tensors moved to the given device.
Source code in minnt/trainable_module.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
Logging
minnt.ProgressLogger
Bases: tqdm
A slim wrapper around tqdm.tqdm for showing a progress bar, optionally with logs.
Source code in minnt/progress_logger.py
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 83 84 85 86 87 88 89 90 91 92 93 94 | |
__init__
__init__(
data: Iterable,
description: str,
console: int | None = None,
logs_fn: Callable[[], Logs] | None = None,
) -> None
Create a ProgressLogger instance.
Parameters:
-
data(Iterable) –Any iterable data to wrap, usually a torch.utils.data.DataLoader.
-
description(str) –A description string to show in front of the progress bar.
-
console(int | None, default:None) –Controls the console verbosity: 0 and 1 for silent, 2 for only-when-writing-to-console progress bar, 3 for persistent progress bar. The default is 2, but can be overridden by the
MINNT_PROGRESSenvironment variable. -
logs_fn(Callable[[], Logs] | None, default:None) –An optional function returning the current logs to show alongside the progress bar. If given, the logs are fully computed and shown on each refresh.
Source code in minnt/progress_logger.py
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 | |
log_console
staticmethod
log_console(
message: str,
end: str = "\n",
progress_only: bool = False,
console: int | None = None,
) -> None
Write the given message to the console, correctly even if a progress bar is being used.
Parameters:
-
message(str) –The message to write.
-
end(str, default:'\n') –The string appended after the message.
-
progress_only(bool, default:False) –If
False(the default), the message is written to standard output when current console verbosity is at least 1; ifTrue, the message is written to standard error only when the progress bar is being shown (console verbosity 2 and writing to the console, or console verbosity 3). -
console(int | None, default:None) –Controls the current console verbosity. The default is 2, but can be overridden by the
MINNT_PROGRESSenvironment variable.
Source code in minnt/progress_logger.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
Versioning
minnt.__version__
module-attribute
__version__ = '1.0.1'
The current version of the Minnt package, formatted according to Semantic Versioning.
The version string is in the format major.minor.patch[-prerelease], where the prerelease part is optional and is empty for stable releases.
minnt.require_version
require_version(required_version: str) -> None
Verify the installed version is at least required_version, and set API compatibility for that version.
This method has two purposes: to ensure that the installed version of Minnt meets the minimum required version, and to set the API compatibility level for the Minnt package.
The goal of the API compatibility is to ensure that the API in newer versions of Minnt has the same
intended behavior as in the required_version.
Example
If a package required Minnt version 1.3 and version 1.4 introduced for example a new default
override in minnt.global_keras_initializers, with minnt.require_version("1.3") the package
would still get the old behavior from version 1.3, even when running with Minnt 1.4 or newer.
Warning
The API compatibility does not guarantee completely identical behavior between versions, for example bugs may be fixed in newer versions changing the original behavior. That is why we talk about intended behavior.
Parameters:
-
required_version(str) –The minimum required version, in the format major.minor.patch, and the required API compatibility.
Source code in minnt/version.py
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 | |