Legacy training_tools Migration

Legacy training_tools Migration#

The original mnn.utils.training_tools package remains importable for compatibility, but its split implementation files are legacy APIs. New code should use the single-file API in mnn.utils.training_tools_api.

The new module consolidates the public classes and functions from general_prepare.py, functional.py, and general_train.py into one explicit import surface. The legacy files are expected to be removed in a future release.

From version 0.2.1, training_tools_api.make_model() constructs Torch-first models by default. Existing YAML model fields are accepted and translated to the new constructor names, including num_class, use_cov, predict_bias_var, bn_bias_var, ln_bias, ln_bias_var, and record_bn_mean_var. The mnn_core criterion names are likewise mapped to their Torch-first *Torch implementations; legacy criterion arguments such as num_class, num_sample, is_classify, regular_cov, and normalise are translated as well. config_mnn_activation() configures the shared mnn.mnn_core.torch_activation core used by MomentActivation.

To intentionally construct the old model family for checkpoint comparison or migration, set use_legacy_api: true in the model metadata:

MODEL:
  meta:
    arch: mnn_mlp
    mlp_type: mnn_mlp
    use_legacy_api: true

Migration Table#

Legacy API

New API

mnn.utils.training_tools.general_prepare.make_model

mnn.utils.training_tools_api.make_model

mnn.utils.training_tools.general_prepare.prepare_dataloader

mnn.utils.training_tools_api.prepare_dataloader

mnn.utils.training_tools.general_prepare.prepare_optimizer_scheduler

mnn.utils.training_tools_api.prepare_optimizer_scheduler

mnn.utils.training_tools.general_prepare.deploy_config

mnn.utils.training_tools_api.deploy_config

mnn.utils.training_tools.functional.RecordMethods

mnn.utils.training_tools_api.RecordMethods

mnn.utils.training_tools.functional.InputPreprocess

mnn.utils.training_tools_api.InputPreprocess

mnn.utils.training_tools.functional.DistributedOps

mnn.utils.training_tools_api.DistributedOps

mnn.utils.training_tools.general_train.TrainProcessCollections

mnn.utils.training_tools_api.TrainProcessCollections

mnn.utils.training_tools.general_train.general_train_pipeline

mnn.utils.training_tools_api.general_train_pipeline

mnn.utils.training_tools.general_train.general_distributed_train_pipeline

mnn.utils.training_tools_api.general_distributed_train_pipeline

New Torch MLP to legacy MLP conversion

mnn.utils.torch_legacy.copy_torch_mlp_to_legacy

New Torch MLP to constructed legacy MLP conversion

mnn.utils.torch_legacy.build_legacy_mlp_from_torch

Example#

Legacy:

from mnn.utils.training_tools import general_train

general_train.general_train_pipeline(args)

New:

from mnn.utils import training_tools_api

training_tools_api.general_train_pipeline(args)

Build a legacy MLP from a new Torch-first MLP:

from mnn.models import MomentMlp
from mnn.utils.torch_legacy import build_legacy_mlp_from_torch

new_model = MomentMlp([784, 512], num_classes=10)
legacy_model = build_legacy_mlp_from_torch(new_model)

Notes#

  • The old package-level exports such as mnn.utils.training_tools.make_dataloader remain available for now.

  • The new module keeps the same training pipeline behavior but defaults model construction, MNN activation configuration, and MNN criteria to the Torch-first API.

  • meta.use_legacy_api: true is the explicit compatibility switch for constructing MnnMlp, SnnMlp, MnnMlpNoRho, MnnMlpMeanOnly, or AnnMlp.

  • Torch-to-legacy MLP helpers live in mnn.utils.torch_legacy; training_tools_api re-exports them for compatibility.

  • Torch-to-legacy MLP helpers support MomentMlp, MomentMlpNoCorrelation, and MomentRateMlp; CNN/SNN conversion remains separate.

  • This refactor does not make training tools Torch-only; dataset preparation, seeding, and NumPy-to-Tensor helpers still use NumPy where the original workflow requires it.