# allenai/hidden-networks architecture

> Architecture diagram of the GitHub repository allenai/hidden-networks, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/allenai/hidden-networks
- Repository: https://github.com/allenai/hidden-networks

## Overview

##### Project Type and Purpose
- This project is a research and experimental deep learning framework. It allows users to configure and run neural network experiments (training, pruning, evaluation) via a configuration-driven approach.
- The README and file structure highlight components for both “smallscale” and “largescale” experiments, dedicated model definitions (e.g., ResNet, Wide ResNet) and dataset modules, together with utilities for logging, tensorboard integration, and experiment tracking.

##### Analyzing the File Structure
- Notice the top-level files like main.py, simple_mnist_example.py (a demo script), and __init__.py indicating a Python package.
- The configs/ folder is key because it nests several experiment configuration directories (largescale and smallscale) with subdirectories organized by baseline types, architecture names (conv2, conv4, resnet18, etc.) and even further by training methodologies.
- The models/ folder contains network architecture definitions (e.g., resnet.py, resnet_cifar.py) and additional model variants (e.g., frankle.py which could be referencing the lottery ticket hypothesis).
- The data/ directory contains data loader or dataset modules for several datasets (CIFAR, ImageNet, MNIST).
- The trainers/ directory holds different training scripts such as default, lottery, and “ss”, which suggests varying training regimes or pruning processes.
- The utils/ folder houses helper functions, logging, model building tools, network utilities, and evaluation utilities.

##### Key Components
a. Configuration Management:
- Component: Config Loader / Parser (use configs/parser.py)
- Role: Reads YAML files (.yml, .yaml) that set up experiment parameters such as network type, training regimes, prune rates, and overrides.
b. Experiment Entry Point:
- Component: main.py
- Role: Acts as the orchestrator that reads CLI arguments, loads the configuration and kicks off the complete experiment.
c. Data Handling:
- Component: Data Modules (files under data/ like cifar.py, imagenet.py, etc.)
- Role: Responsible for loading and preprocessing datasets, which are specified in the configuration.
d. Model Definition:
- Component: Model Modules (models/resnet.py, models/frankle.py)
- Role: Contains network architecture implementation and perhaps subsections for applying random weights, pruning masks, etc.
e. Training/Experiment Management:
- Component: Trainers (trainers/default.py, trainers/lottery.py, trainers/ss.py)
- Role: Encapsulates training loops, model checkpointing, logging, and experiment tracking.
f. Utilities:
- Component: Utils (logging, builders, scheduling, conv and BN type utilities)
- Role: Provides shared functions across the system for tasks like fitness evaluation, network building, logging progress, and profiling performance.
g. External Dependencies:
- Components: PyTorch, Torchvision, Tensorboard, and dependencies listed in requirements.txt.
- Role: Underpin model training, dataset handling, and experiment tracking.

##### Relationships and Interactions
- main.py serves as the central orchestrator that, based on CLI arguments including a configuration file path, initializes:
- Configuration Manager → Reads YAML configurations from the configs/ directory.
- Data Loader → Using data/ modules to load the specified dataset.
- Model Builder → From models/ and related utils, creating the network architecture.
- Trainer → Chooses and runs one of the training modules (from trainers/).
- Logging/Tracking System → Uses tensorboard for monitoring and saving experiment results.
- The YAML configurations govern how components interact by specifying which model (and its initialization/pruning method) to use, which training regime to invoke, and what hyperparameters to apply.
- There is also a feedback loop from training (via utils/logging.py and checkpoint directories under runs/) back to the experiment tracking for saving and evaluating models.

##### Architectural Patterns & Design Principles
- Modularity: The clear separation between configuration, data handling, modeling, training, and utility functions suggests a modular architecture.
- Config-driven Execution: All experiments are defined via YAML config files, making the system highly customizable without code changes.
- Reusability and Extensibility: With separate directories for models, trainers, and utilities, it’s straightforward to add new network architectures or training algorithms.
- Experiment Tracking: The design supports automated logging (tied with Tensorboard) and organized file structure for checkpoints and results.

##### Final Advice
- Be as detailed as possible to capture all essential interactions, making sure to incorporate every major folder’s contribution to the system’s design.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Entry Point
    subgraph "Entry Point"
        A["main.py (Entry Point)"]:::entry
    end

    %% Configuration Layer
    subgraph "Configuration"
        B["Config Loader (configs/parser.py)"]:::config
    end

    %% Data Handling Layer
    subgraph "Data Handling"
        D1["MNIST (data/mnist.py)"]:::data
        D2["CIFAR (data/cifar.py)"]:::data
        D3["ImageNet (data/imagenet.py)"]:::data
        D4["TinyImageNet (data/tinyimagenet.py)"]:::data
    end

    %% Model Definitions Layer
    subgraph "Model Definitions"
        M1["ResNet (models/resnet.py)"]:::model
        M2["ResNet CIFAR (models/resnet_cifar.py)"]:::model
        M3["Frankle (models/frankle.py)"]:::model
    end

    %% Training Engine Layer
    subgraph "Training & Experiment Management"
        T1["Default Trainer (trainers/default.py)"]:::train
        T2["Lottery Trainer (trainers/lottery.py)"]:::train
        T3["SS Trainer (trainers/ss.py)"]:::train
    end

    %% Utilities & Tracking Layer
    subgraph "Utilities & Tracking"
        U1["Logging (utils/logging.py)"]:::util
        U2["Builder (utils/builder.py)"]:::util
        U3["Network Utils (utils/net_utils.py)"]:::util
        U4["Profiling (utils/profiling.py)"]:::util
        U5["Schedulers (utils/schedulers.py)"]:::util
        UT["Tensorboard/Experiment Tracker"]:::tracker
    end

    %% External Dependencies
    subgraph "External Dependencies"
        EX["PyTorch/Torchvision"]:::external
        R["requirements.txt"]:::external
    end

    %% Connections
    A -->|"loads"| B

    B -->|"specifies-dataset"| D1
    B -->|"specifies-dataset"| D2
    B -->|"specifies-dataset"| D3
    B -->|"specifies-dataset"| D4

    B -->|"specifies-model"| M1
    B -->|"specifies-model"| M2
    B -->|"specifies-model"| M3

    B -->|"specifies-training"| T1
    B -->|"specifies-training"| T2
    B -->|"specifies-training"| T3

    M1 -->|"builds-network"| U2
    M2 -->|"builds-network"| U2
    M3 -->|"builds-network"| U2

    T1 -->|"logs"| U1
    T2 -->|"logs"| U1
    T3 -->|"logs"| U1

    T1 -->|"uses-utils"| U3
    T1 -->|"uses-utils"| U4
    T1 -->|"uses-utils"| U5
    T2 -->|"uses-utils"| U3
    T2 -->|"uses-utils"| U4
    T2 -->|"uses-utils"| U5
    T3 -->|"uses-utils"| U3
    T3 -->|"uses-utils"| U4
    T3 -->|"uses-utils"| U5

    U1 -->|"forwards"| UT
    T1 -->|"tracks"| UT
    T2 -->|"tracks"| UT
    T3 -->|"tracks"| UT

    B -->|"reads"| R
    T1 -->|"requires"| EX
    T2 -->|"requires"| EX
    T3 -->|"requires"| EX

    %% Click Events
    click A "https://github.com/allenai/hidden-networks/blob/master/main.py"
    click B "https://github.com/allenai/hidden-networks/blob/master/configs/parser.py"
    click D1 "https://github.com/allenai/hidden-networks/blob/master/data/mnist.py"
    click D2 "https://github.com/allenai/hidden-networks/blob/master/data/cifar.py"
    click D3 "https://github.com/allenai/hidden-networks/blob/master/data/imagenet.py"
    click D4 "https://github.com/allenai/hidden-networks/blob/master/data/tinyimagenet.py"
    click M1 "https://github.com/allenai/hidden-networks/blob/master/models/resnet.py"
    click M2 "https://github.com/allenai/hidden-networks/blob/master/models/resnet_cifar.py"
    click M3 "https://github.com/allenai/hidden-networks/blob/master/models/frankle.py"
    click T1 "https://github.com/allenai/hidden-networks/blob/master/trainers/default.py"
    click T2 "https://github.com/allenai/hidden-networks/blob/master/trainers/lottery.py"
    click T3 "https://github.com/allenai/hidden-networks/blob/master/trainers/ss.py"
    click U1 "https://github.com/allenai/hidden-networks/blob/master/utils/logging.py"
    click U2 "https://github.com/allenai/hidden-networks/blob/master/utils/builder.py"
    click U3 "https://github.com/allenai/hidden-networks/blob/master/utils/net_utils.py"
    click U4 "https://github.com/allenai/hidden-networks/blob/master/utils/profiling.py"
    click U5 "https://github.com/allenai/hidden-networks/blob/master/utils/schedulers.py"
    click R "https://github.com/allenai/hidden-networks/blob/master/requirements.txt"

    %% Styles
    classDef entry fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef config fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef data fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef model fill:#FADBD8,stroke:#C0392B,stroke-width:2px;
    classDef train fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef util fill:#D6EAF8,stroke:#2E86C1,stroke-width:2px;
    classDef tracker fill:#FEF9E7,stroke:#F1C40F,stroke-width:2px;
    classDef external fill:#FBEFEF,stroke:#E74C3C,stroke-width:2px;
```
