# junghwamin/nmfc-plan2-v3-module-variants architecture

> Architecture diagram of the GitHub repository junghwamin/nmfc-plan2-v3-module-variants, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/junghwamin/nmfc-plan2-v3-module-variants
- Repository: https://github.com/junghwamin/nmfc-plan2-v3-module-variants

## Overview

#### Purpose and entry points
This is a PyTorch experiment system for reproducing and comparing NMFC v3 module variants across four axes: energy, logit, APT warmup, and reference bank. Primary entry point is `python -m src.train --config <yaml>` via `src/train.py`; experiment matrices live in `configs/cifar100_variants.yaml` and `configs/ood_cifar10_variants.yaml`.

#### Variant registry and compatibility boundary
`src/variants.py` is the architectural switchboard. It resolves and validates `variant:` config blocks, encodes experiment names, rejects incompatible combinations, and selects head/warmup implementations. The key compatibility boundary is v1 preservation: absent or v1-equivalent variants route to the existing `NMFCHead`; non-v1 variants route to v3 composition.
- `src/variants.py`
- `src/models/heads.py`
- `src/models/heads_v3.py`

#### Data and embedding pipeline
Image datasets feed either PK-sampled minibatches or indexed shuffled loaders required by online-bank leave-one-out behavior. The encoder produces embeddings consumed by NMFC heads; datasets and sampling are separate from variant math.
- `src/datasets/image.py`
- `src/datasets/samplers.py`
- `src/models/encoders.py`

#### Head and energy/logit execution
The legacy head retains mean-energy and scaled-logit behavior. `VariantNMFCHead` implements configurable soft-min energies, hinge-temperature logits, batch versus online references, prediction, and OOD scoring. Soft-min neighbor selection is stop-gradient; hinge logits cap negative energy at a learned/set margin.
- `src/models/heads_v3.py`
- `src/nmfc/softmin.py`
- `src/nmfc/energy.py`

#### Reference-bank lifecycle
Training references can be minibatch-based or an online, class-balanced FIFO cross-batch memory. Inference is deliberately separated: a frozen, deterministic k-center coreset bank supplies logits and OOD scores rather than reusing transient training references. Bank state supports persistence/restoration.
- `src/models/banks.py`
- `src/models/heads_v3.py`
- `src/train.py`

#### Two-phase training control
`src/train.py` orchestrates Phase I representation warmup followed by Phase II NMFC NLL training. Reciprocal MFA uses the inherited APT controller; negative Fisher-J uses the v3 ACS controller. Controllers detect stagnation and latch the transition, with training responsible for loader changes, bank filling, margin setup, optimizer assembly, and history tracking.
- `src/train.py`
- `src/nmfc/apt.py`
- `src/nmfc/acs.py`

#### Evaluation, outputs, and runtime
Evaluation reports classification, embedding geometry, and OOD AUC; v3 OOD uses minimum positive soft-min energy. Runs are cached/namespaced and write histories and metrics beneath `산출물/`; checkpoints and large embeddings are external/regenerable. The system is Python/PyTorch-based, uses CIFAR-10/100 and SVHN data, and requires GPU infrastructure for practical 224px full experiments.
- `src/evaluate.py`
- `src/utils/experiments.py`
- `산출물/`

Main entry points: [`src/train.py`](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/train.py) (Training entry point), [`src/models/heads_v3.py`](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/heads_v3.py) (Variant NMFC head), [`src/variants.py`](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/variants.py) (Variant registry), [`src/models/encoders.py`](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/encoders.py) (Embedding encoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_experiment["Experiment Control"]
  node_configs["Variant matrices<br/>YAML configs"]
  node_train["Training entry point<br/>PyTorch runner<br/>[train.py]"]
  node_variants{{"Variant registry<br/>compatibility switchboard<br/>[variants.py]"}}
  node_apt["APT controller<br/>warmup controller<br/>[apt.py]"]
  node_acs["ACS controller<br/>warmup controller<br/>[acs.py]"]
end

subgraph group_model["Model and NMFC"]
  node_encoder["Embedding encoder<br/>PyTorch model<br/>[encoders.py]"]
  node_legacy_head["Legacy NMFC head<br/>v1 head<br/>[heads.py]"]
  node_v3_head["Variant NMFC head<br/>v3 head<br/>[heads_v3.py]"]
  node_softmin["Soft-min neighbors<br/>energy selection<br/>[softmin.py]"]
  node_energy["Energy and hinge logits<br/>NMFC math<br/>[energy.py]"]
  node_banks[("Reference banks<br/>memory lifecycle<br/>[banks.py]")]
end

subgraph group_runtime["Data, Evaluation, Outputs"]
  node_image_data["Image datasets<br/>dataset provider<br/>[image.py]"]
  node_samplers["Batch samplers<br/>loader strategy<br/>[samplers.py]"]
  node_evaluate["Evaluation<br/>metrics runner<br/>[evaluate.py]"]
  node_experiments[("Run cache and namespace<br/>experiment storage<br/>[experiments.py]")]
  node_outputs["Run outputs<br/>artifact directory"]
end

node_configs -->|"selects run"| node_train
node_configs -->|"variant block"| node_variants
node_train -->|"resolve head and warmup"| node_variants
node_variants -->|"absent or v1 variant"| node_legacy_head
node_variants -->|"non-v1 variant"| node_v3_head
node_image_data -->|"dataset indices"| node_samplers
node_samplers -->|"minibatches or indexed loader"| node_train
node_train -->|"optimizes representation"| node_encoder
node_encoder -->|"embeddings"| node_legacy_head
node_encoder -->|"embeddings"| node_v3_head
node_v3_head -->|"neighbor energies"| node_softmin
node_softmin -->|"selected references"| node_energy
node_energy -->|"energies and logits"| node_v3_head
node_banks -->|"training and inference references"| node_v3_head
node_train -->|"fill, freeze, persist"| node_banks
node_apt -->|"warmup transition"| node_train
node_acs -->|"warmup transition"| node_train
node_train -->|"trained model and frozen bank"| node_evaluate
node_v3_head -->|"positive soft-min OOD score"| node_evaluate
node_train -->|"cache and namespace"| node_experiments
node_evaluate -->|"metrics"| node_outputs
node_experiments -->|"histories"| node_outputs

click node_configs "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/configs/cifar100_variants.yaml"
click node_train "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/train.py"
click node_variants "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/variants.py"
click node_apt "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/apt.py"
click node_acs "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/acs.py"
click node_image_data "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/datasets/image.py"
click node_samplers "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/datasets/samplers.py"
click node_encoder "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/encoders.py"
click node_legacy_head "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/heads.py"
click node_v3_head "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/heads_v3.py"
click node_softmin "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/softmin.py"
click node_energy "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/energy.py"
click node_banks "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/banks.py"
click node_evaluate "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/evaluate.py"
click node_experiments "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/utils/experiments.py"
click node_outputs "https://github.com/junghwamin/nmfc-plan2-v3-module-variants/tree/main/%EC%82%B0%EC%B6%9C%EB%AC%BC"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_configs,node_train,node_variants,node_apt,node_acs toneBlue
class node_encoder,node_legacy_head,node_v3_head,node_softmin,node_energy,node_banks toneAmber
class node_image_data,node_samplers,node_evaluate,node_experiments,node_outputs toneMint
```

## Components

### Experiment Control

Configuration, variant resolution, and two-phase training.

- [Variant matrices](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/configs/cifar100_variants.yaml): YAML configs · CIFAR-100 and OOD CIFAR-10 experiment definitions. · `configs/cifar100_variants.yaml`
- [Training entry point](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/train.py): PyTorch runner · Runs warmup and NMFC training phases. · `src/train.py`
- [Variant registry](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/variants.py): compatibility switchboard · Validates variants and preserves v1 routing. · `src/variants.py`
- [APT controller](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/apt.py): warmup controller · Inherited reciprocal-MFA transition controller. · `src/nmfc/apt.py`
- [ACS controller](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/acs.py): warmup controller · Controls negative Fisher-J warmup transition. · `src/nmfc/acs.py`

### Model and NMFC

Embedding, heads, energy math, and reference banks.

- [Embedding encoder](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/encoders.py): PyTorch model · Transforms images into embeddings. · `src/models/encoders.py`
- [Legacy NMFC head](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/heads.py): v1 head · Preserves mean-energy and scaled-logit behavior. · `src/models/heads.py`
- [Variant NMFC head](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/heads_v3.py): v3 head · Composes energy, logits, references, prediction, and OOD scoring. · `src/models/heads_v3.py`
- [Soft-min neighbors](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/softmin.py): energy selection · Stop-gradient neighbor selection for configurable soft-min energy. · `src/nmfc/softmin.py`
- [Energy and hinge logits](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/nmfc/energy.py): NMFC math · Computes energies and margin-capped logits. · `src/nmfc/energy.py`
- [Reference banks](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/models/banks.py): memory lifecycle · Online FIFO training bank and frozen k-center inference bank. · `src/models/banks.py`

### Data, Evaluation, Outputs

Loaders, reporting, and persisted run artifacts.

- [Image datasets](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/datasets/image.py): dataset provider · CIFAR and SVHN image loading. · `src/datasets/image.py`
- [Batch samplers](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/datasets/samplers.py): loader strategy · PK sampling or indexed shuffled loading. · `src/datasets/samplers.py`
- [Evaluation](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/evaluate.py): metrics runner · Reports classification, geometry, and OOD AUC. · `src/evaluate.py`
- [Run cache and namespace](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/blob/main/src/utils/experiments.py): experiment storage · Names and caches experiment runs. · `src/utils/experiments.py`
- [Run outputs](https://github.com/junghwamin/nmfc-plan2-v3-module-variants/tree/main/%EC%82%B0%EC%B6%9C%EB%AC%BC): artifact directory · Histories and metrics; large checkpoints and embeddings are regenerable. · `산출물`

## Connections

- Variant matrices → Training entry point: selects run
- Variant matrices → Variant registry: variant block
- Training entry point → Variant registry: resolve head and warmup
- Variant registry → Legacy NMFC head: absent or v1 variant
- Variant registry → Variant NMFC head: non-v1 variant
- Image datasets → Batch samplers: dataset indices
- Batch samplers → Training entry point: minibatches or indexed loader
- Training entry point → Embedding encoder: optimizes representation
- Embedding encoder → Legacy NMFC head: embeddings
- Embedding encoder → Variant NMFC head: embeddings
- Variant NMFC head → Soft-min neighbors: neighbor energies
- Soft-min neighbors → Energy and hinge logits: selected references
- Energy and hinge logits → Variant NMFC head: energies and logits
- Reference banks → Variant NMFC head: training and inference references
- Training entry point → Reference banks: fill, freeze, persist
- APT controller → Training entry point: warmup transition
- ACS controller → Training entry point: warmup transition
- Training entry point → Evaluation: trained model and frozen bank
- Variant NMFC head → Evaluation: positive soft-min OOD score
- Training entry point → Run cache and namespace: cache and namespace
- Evaluation → Run outputs: metrics
- Run cache and namespace → Run outputs: histories
