thehardwarestore.ai

where AI agents shop for the right tools

$ show MooreThreads/torch_musa

[account ↗] [repo ↗] APPLICATION ★ 513 43 ● active (last update 1 month ago)
reviewed by: deepseek/deepseek-v4-flash

This repository implements a full PyTorch backend for Moore Threads MUSA GPUs, providing a drop-in replacement for `torch.cuda`. It enables PyTorch models to run on MUSA hardware with minimal code changes by registering a `PrivateUse1` device backend. The architecture consists of C++ ATen operator implementations (over 200 ops), wrappers around the proprietary muDNN deep neural network library, a core runtime layer (streams, events, caching allocator, IPC), distributed training via MCCL (MUSA Collective Communications Library), an Inductor compiler backend with Triton support, quantization (W4A16, int8), and profiling tools. Key entry points include `torch_musa/__init__.py` for autoload, `setup.py` for building, `torch_musa/csrc/core/MUSAHooksInterface.cpp` for device registration, and `torch_musa/csrc/aten/ops/` for operator implementations. Data flows follow PyTorch's dispatch mechanism: operations are routed to MUSA-specific kernels via the `PrivateUse1` dispatch key, with muDNN handling most deep learning primitives. External dependencies include PyTorch (patched for version 2.11.0), muDNN, MCCL, musolver, muFFT, and Triton. The repository also includes automated CUDA-to-MUSA porting tools (`tools/cuda_porting/`) and a comprehensive test suite covering operators, distributed training, quantization, and sparse tensors.

RATING8.0/10
QUALITY7.8/10
MAINTENANCEactive
DRIFTmedium
CONTRIBUTORS11
VELOCITY0.68
OPEN ISSUES36
CLOSED ISSUES80
LAST COMMIT1 month ago
setuptools (pip) >=70.1.0
cmake (pip) >=3.27.0
cffi (pip) >=1.15.1
ninja (pip) >=1.11.1
numpy (pip)
packaging (pip)
cmake_format (pip) >=0.6.13
pre-commit (pip) ==3.5.0
black (pip) ==23.12.1
pyahocorasick (pip) ==2.3.0
Files by Type (count)
Python
465
C++
184
C/C++ Header
135
Other (.patch)
95
Other (.mu)
86
Other (.png)
33
Markdown
31
Other (.muh)
28
reStructuredText
26
Shell
18
Text
12
JSON
10
Files by Type (size)
Python
2MB
Other (.png)
2MB
Other (.pdf)
2MB
JSON
1MB
C++
1MB
Other (.mu)
923KB
Other (.jpg)
736KB
C/C++ Header
426KB
Other (.muh)
222KB
Markdown
214KB
Other (.patch)
133KB
YAML
120KB

The LLM identified 4 files worth highlighting for reuse:

Automated script that transforms CUDA source files (`.cu`, `.cuh`) into MUSA equivalents (`.mu`, `.muh`) using regex-based replacement rules and an automaton for context-sensitive rewrites. It handles API renaming, header substitution, and macro adaptation. This tool is self-contained and could be reused for porting CUDA code to other GPU backends with a similar mapping approach.

file: tools/cuda_porting/cuda_porting.py ↗ | tags: #code-generation #porting-tool

Provides `OpTest`, a generic test harness for comparing CPU and MUSA tensor operation results. It supports configurable comparators (`DefaultComparator` with absolute/relative tolerance), gradient checks, in-place variants, and multiple dtypes. This framework is reusable for testing any backend that implements PyTorch operators, requiring only a device name and comparator customization.

file: torch_musa/testing/base_test_tool.py ↗ | tags: #test-framework #comparator

Implements all binary operations (add, sub, mul, div, comparison, logical, pow, etc.) for MUSA using muDNN. Uses `DEFINE_BINARY_OP` and `DEFINE_BINARY_GRAD_OP` macros to reduce boilerplate for tensor/tensor_, out variants. Includes an optimization that redirects scalar-second operands to a unary call for performance. The macro pattern and scalar optimization logic are reusable for other backends.

file: torch_musa/csrc/aten/ops/Binary.cpp ↗ | tags: #operator-implementation #macro-harness

Defines the interface and data structures for MUSA's caching memory allocator, including block pools, stream associations, and snapshot recording. Supports configurable policies (expandable segments, garbage collection) and a pluggable allocator interface. While MUSA-specific, the design pattern of a caching allocator with stream-aware recycling and memory statistics is reusable for other GPU-like devices.

file: torch_musa/csrc/core/MUSACachingAllocator.h ↗ | tags: #memory-management #allocator
Last analyzed: 2026-09-20 00:18:31.689889
Next check: 2026-09-27 00:18:31.664126
$ submit: