# jepetau/pynnlib architecture

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

- Interactive diagram: https://gitdiagram.com/jepetau/pynnlib
- Repository: https://github.com/jepetau/pynnlib

## Overview

##### Main Components
- Core Library Components – At the top, you have modules such as architecture.py, capabilities.py, core.py, framework.py, and logger.py. These files represent the foundational functionality and utilities for the library.
- Model Handling and Abstraction Layer – The primary API is exposed through the “nnlib” interface (seen in the README examples using functions like open(), convert_to_tensorrt(), session(), and set_session_constructor()). This layer abstracts model initialization, conversion, and inference.
- Framework-Specific Backends – There are three major directories for backend implementations:
– nn_pytorch – Contains many subdirectories for different architectures (e.g., AnimeSR, BSVD, EfficientTAM, etc.). It implements model construction, session handling, and inference for PyTorch models.
– nn_onnx – Provides conversion and inference scripts for ONNX models.
– nn_tensor_rt – Contains similar files for building inference sessions via TensorRT.
- Scripts and Utility Tools – Under “scripts” and “utils”, there are helper scripts (like convert_model.py, parse_models.py, and img_infer.py) that demonstrate usage of the library and the conversion/inference workflow.
- Session Management – The README shows how custom inference sessions can be set up with the set_session_constructor() API. This abstraction also includes components managing device (e.g., CUDA, CPU) and data type conversions.

##### Relationships and Interactions
- User Code/Applications call the high-level API functions exposed by the core library (the “nnlib” interface) to load models, initiate conversions, and create inference sessions.
- The “open()” function delegates the task of model loading to underlying modules in the appropriate framework-specific subfolder (for example, within nn_pytorch, nn_onnx, etc.).
- If a conversion is required (e.g., converting a PyTorch model to TensorRT), the conversion functions (such as convert_to_tensorrt()) will interface with modules in the nn_tensor_rt directory and might also interact with common components in framework.py and import_libs.py.
- Inference sessions are created using the “session” API, which under the hood relies on the specific logic (found in nn_pytorch/inference, nn_onnx/inference, etc.) for transferring images/data and running execution.
- Script utilities (from the scripts folder) provide user-friendly command line interfaces to tasks such as model conversion, image inference, and model parsing. They serve as example orchestrations and can be depicted as “client” nodes that invoke the core library functions.

##### Architectural Patterns and Design Principles
- Abstraction & Modularity – The design is modular, with a clear separation between framework-agnostic core functionalities and framework-specific implementations. This allows the library to be easily integrated as a submodule and extended over time.
- Plug-in Mechanism for Sessions – The use of set_session_constructor() to plug in custom sessions for PyTorch and TensorRT shows that the library is designed with flexibility and extensibility in mind.
- Hierarchical Organization – The deep directory structure under nn_pytorch (and similarly under nn_onnx, nn_tensor_rt) represents the separation of concerns for different neural network architectures and converters.
- Submodule Integration – The README emphasizes that pynnlib is to be integrated as a submodule. This encourages a loosely coupled design where the internal API is not considered stable.

##### Specific Elements
- An additional block for “Scripts / CLI Tools” that shows utilities like parse_models, convert_model, and img_infer.
– API request flow from the core interface to the framework-specific modules.
– Data flow between model loading → conversion → inference session → output.

## Diagram (Mermaid)

```mermaid
flowchart TD
    classDef external fill:#FFCC00,stroke:#000,stroke-width:2px;
    classDef core fill:#BBDEFB,stroke:#0D47A1,stroke-width:2px;
    classDef backend fill:#C8E6C9,stroke:#1B5E20,stroke-width:2px;
    classDef utility fill:#F8BBD0,stroke:#880E4F,stroke-width:2px;

    %% External User Interface
    U["User/Application"]:::external

    %% pynnlib API Interface (nnlib)
    API["pynnlib API (nnlib)\nopen(),session(),convert_to_tensorrt(),set_session_constructor()"]:::core

    %% Core Library Components
    subgraph "Core Library Components"
        A["Architecture.py"]:::core
        CAP["Capabilities.py"]:::core
        C["Core.py"]:::core
        F["Framework.py"]:::core
        L["Logger.py"]:::core
        INIT["Model Handling (__init__.py & model.py)"]:::core
    end

    %% Framework-specific Backends
    subgraph "PyTorch Backend"
        PT_A["PyTorch Archs"]:::backend
        PT_S["PyTorch Session\n(inference/session.py)"]:::backend
    end

    subgraph "ONNX Backend"
        ONNX_A["ONNX Archs"]:::backend
        ONNX_S["ONNX Session\n(inference/session.py)"]:::backend
    end

    subgraph "TensorRT Backend"
        TRT_A["TensorRT Archs"]:::backend
        TRT_S["TensorRT Session\n(inference/session.py)"]:::backend
    end

    %% Scripts / CLI Utilities
    subgraph "Scripts / CLI Utilities"
        SM1["convert_model.py"]:::utility
        SM2["img_infer.py"]:::utility
        SM3["parse_models.py"]:::utility
    end

    %% Connections
    U -->|"calls"| API
    API -->|"delegates"| INIT
    API -->|"uses"| A
    API -->|"uses"| CAP
    API -->|"uses"| C
    API -->|"uses"| F
    API -->|"uses"| L

    API -->|"loads models"| PT_A
    PT_A -->|"creates session"| PT_S

    API -->|"converts model"| TRT_A
    TRT_A -->|"builds inference"| TRT_S

    API -->|"handles conversion/inference"| ONNX_A
    ONNX_A -->|"builds inference"| ONNX_S

    SM1 -.->|"orchestrates"| API
    SM2 -.->|"orchestrates"| API
    SM3 -.->|"orchestrates"| API

    %% Click Events for Core Components
    click A "https://github.com/jepetau/pynnlib/blob/main/architecture.py"
    click CAP "https://github.com/jepetau/pynnlib/blob/main/capabilities.py"
    click C "https://github.com/jepetau/pynnlib/blob/main/core.py"
    click F "https://github.com/jepetau/pynnlib/blob/main/framework.py"
    click L "https://github.com/jepetau/pynnlib/blob/main/logger.py"
    click INIT "https://github.com/jepetau/pynnlib/blob/main/__init__.py"

    %% Click Events for PyTorch Backend
    click PT_A "https://github.com/jepetau/pynnlib/tree/main/nn_pytorch/archs"
    click PT_S "https://github.com/jepetau/pynnlib/blob/main/nn_pytorch/inference/session.py"

    %% Click Events for ONNX Backend
    click ONNX_A "https://github.com/jepetau/pynnlib/tree/main/nn_onnx/archs"
    click ONNX_S "https://github.com/jepetau/pynnlib/blob/main/nn_onnx/inference/session.py"

    %% Click Events for TensorRT Backend
    click TRT_A "https://github.com/jepetau/pynnlib/tree/main/nn_tensor_rt/archs"
    click TRT_S "https://github.com/jepetau/pynnlib/blob/main/nn_tensor_rt/inference/session.py"

    %% Click Events for Scripts / CLI Utilities
    click SM1 "https://github.com/jepetau/pynnlib/blob/main/scripts/convert_model.py"
    click SM2 "https://github.com/jepetau/pynnlib/blob/main/scripts/img_infer.py"
    click SM3 "https://github.com/jepetau/pynnlib/blob/main/scripts/parse_models.py"
```
