How ascend/pytorch works
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.