# ascend/pytorch architecture

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

- Interactive diagram: https://gitdiagram.com/ascend/pytorch
- Repository: https://github.com/ascend/pytorch

## Overview

##### Project Purpose and Overall Structure
- The project is an extension to PyTorch enabling support for Ascend NPUs. Its primary goal is to integrate accelerator-specific code (including C++ kernels and device management) with the existing PyTorch infrastructure.
- The repository contains build configuration files (e.g., CMakeLists.txt, setup.py), code generation modules, compiled C++ backends, Python wrapper libraries (under the “torch_npu” package), extensive CI/CD scripts, and a very broad test suite.

##### Main Components
a. Core Library (torch_npu)
– The heart of the project where most PyTorch extension code lives.
– Contains:
- Python modules (e.g., __init__.py, contrib, distributed, dynamo, onnx, optim, profiler, utils).
- A large “csrc” subdirectory managing native C++ implementations. This includes further subdivisions for:
○ ATen-level functions (for interoperability with PyTorch).
○ Core components (NPUBridge, NPUStorageImpl, error handling).
○ Distributed modules (handling parallel and multi-device support).
○ Profiling, logging, and toolkit utilities.
b. Code Generation (codegen)
– Automatically produces C++ boilerplate for autograd functions and others.
– Contains Python-based code generation scripts, template files, and custom functions.
c. Build & Configuration System
– Top-level CMakeLists.txt, setup.py, and setup.cfg that define how the library is built as both C++ and Python packages.
– Build scripts such as “build_libtorch_npu.py” and docker build scripts in the “ci/docker” directories.
d. CI/CD and Testing Infrastructure
– Multiple CI configurations under “.ci”, “.github” workflows, and scripts in “ci” (e.g., Dockerfiles, build.sh).
– A comprehensive “test” suite is organized into folder subcomponents for autograd, distributed, JIT, FX, NPU, and more.
– This area covers automated testing for unit tests, integration tests, codestyle, and performance/benchmark tests.
e. Third-Party Dependencies
– A “third_party” directory includes supporting libraries such as ACL, HCCL, Tensorpipe, and others.
– These external components are integral for interfacing with Ascend hardware and protocols.
f. Examples and Documentation
– “examples” directory for user demonstrations (e.g., libtorch_npu, libtorch_resnet).
– “docs” directory and README provide API explanations, release notes, and user manuals.

##### Relationships and Flow Between Components
- Auto-code generation process: the “codegen” module feeds into the building of C++ components, especially for autograd functionality.
- Build pipeline’s flow: source code (CMake configuration, setup.py, build scripts) → build system (using Docker and CI tasks) → produced binary/whl packages.

##### Architectural Patterns to Emphasize
- Layered Architecture – separate presentation/user interfaces (Python API), business logic (NPU-specific operations and distributed support), and low-level hardware interfaces (C++ drivers interacting with Ascend hardware).
- Plugin/Extension Model – torch_npu extends PyTorch by “plugging in” custom kernels and operations via code generation.
- Modular Testing & CI/CD – the project uses a clear separation among development (CI scripts, docker configurations), testing (organized under “test”), and production (packaged releases).

##### Final Steps for the Principal Software Engineer
– From torch_npu Python modules to the C++ layer via bindings.
– From build scripts and CI pipelines into the build and packaging process.
– From code generation outputs into the C++ source directories.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Core Library Subgraph
    subgraph "Core Library (torch_npu)"
        Python_API["Torch NPU Python API"]:::python
        CPP_Core["C++ Core (torch_npu/csrc)"]:::cpp
        Python_API -->|"wraps"| CPP_Core
    end

    %% Other Components
    CodeGen["Code Generation Module (codegen)"]:::codegen
    Build["Build & Configuration System"]:::build
    CI["CI/CD Infrastructure"]:::ci
    Testing["Testing & Validation"]:::test
    ThirdParty["Third-Party Dependencies"]:::thirdparty

    subgraph "User Resources"
        Examples["Examples"]:::resources
        Documentation["Documentation"]:::resources
    end

    %% Connections between components
    CodeGen -->|"generates"| CPP_Core
    CodeGen -->|"integrates"| Build
    Build -->|"builds"| Python_API
    Build -->|"builds"| CPP_Core
    CI -->|"orchestrates"| Build
    CI -->|"triggers"| Testing
    ThirdParty -->|"links"| CPP_Core
    Examples -->|"demonstrates"| Python_API
    Documentation -->|"documents"| Python_API

    %% Click Events
    click Python_API "https://github.com/ascend/pytorch/blob/master/torch_npu/__init__.py"
    click CPP_Core "https://github.com/ascend/pytorch/tree/master/torch_npu/csrc"
    click CodeGen "https://github.com/ascend/pytorch/tree/master/codegen"
    click Build "https://github.com/ascend/pytorch/blob/master/setup.py"
    click CI "https://github.com/ascend/pytorch/blob/master/.ci"
    click Testing "https://github.com/ascend/pytorch/tree/master/test"
    click ThirdParty "https://github.com/ascend/pytorch/tree/master/third_party"
    click Examples "https://github.com/ascend/pytorch/tree/master/examples"
    click Documentation "https://github.com/ascend/pytorch/tree/master/docs"

    %% Styles
    classDef python fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef cpp fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef codegen fill:#D5F5E3,stroke:#28B463,stroke-width:2px;
    classDef build fill:#FAD7A0,stroke:#CA6F1E,stroke-width:2px;
    classDef ci fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
    classDef test fill:#F5CBA7,stroke:#E67E22,stroke-width:2px;
    classDef thirdparty fill:#FCF3CF,stroke:#B9770E,stroke-width:2px;
    classDef resources fill:#D6EAF8,stroke:#2471A3,stroke-width:2px;
```
