Thank you for your interest in contributing to torchada! This guide will help you get started.
git clone https://github.com/MooreThreads/torchada.git
cd torchada
pip install -e ".[dev]"We use pre-commit to automatically check and format code before each commit.
# Install the hooks (one-time setup)
pre-commit install
# (Optional) Run hooks against all files to verify setup
pre-commit run --all-filesOnce installed, the hooks run automatically on git commit. They will:
- Format code with black (line-length 100)
- Sort imports with isort (black profile)
- Lint with ruff (unused imports F401, undefined names F821)
- Check for common issues (trailing whitespace, YAML/TOML syntax, merge conflicts, debug statements, etc.)
- Spell-check with codespell
If a hook modifies files (e.g., reformatting), the commit will be aborted. Simply git add the changes and commit again.
# Run all hooks on all files
pre-commit run --all-files
# Run a specific hook
pre-commit run black --all-files
pre-commit run isort --all-files
# Run hooks only on staged files (same as what happens on commit)
pre-commit run
# Update hook versions
pre-commit autoupdate- Formatter: black with line-length 100
- Import sorting: isort with black profile
- Python version: >=3.8
These are enforced automatically by pre-commit hooks. You can also run them manually:
isort src/ tests/ && black src/ tests/# Run all tests
pytest tests/
# Run specific test class
pytest tests/test_cuda_patching.py::TestLibraryImpl -v
# Run with short traceback
pytest tests/ --tb=short@pytest.mark.musa— Requires MUSA platform@pytest.mark.cuda— Requires CUDA platform@pytest.mark.gpu— Requires any GPU@pytest.mark.slow— Slow tests
Tests should pass on both torch_musa versions:
docker exec -w /ws yeahdongcn python -m pytest tests/ --tb=short # torch_musa 2.7.0
docker exec -w /ws yeahdongcn1 python -m pytest tests/ --tb=short # torch_musa 2.7.1- Add patch function in
src/torchada/_patch.py:
@patch_function
@requires_import("torch_musa")
def _patch_feature_name():
"""Docstring explaining what this patch does."""
original_func = torch.module.func
def patched_func(*args, **kwargs):
# Translation logic
return original_func(*args, **kwargs)
torch.module.func = patched_func- Add tests in
tests/test_cuda_patching.py - Update documentation in
README.mdandREADME_CN.md
- Never patch
torch.cuda.is_available()ortorch.version.cuda— downstream projects use these for platform detection - Import order matters:
import torchadamust come before other torch imports in user code - MUSA tensors use
PrivateUse1dispatch key, notCUDA— always translate dispatch keys
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes (pre-commit hooks will auto-format on commit)
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request