Skip to content

Commit 8d7459c

Browse files
FindHaofacebook-github-bot
authored andcommitted
Update test suite and configuration for tritonparse (#24)
Summary: - Added a comprehensive test suite in `tests/test_tritonparse.py` to cover various functionalities of tritonparse. - Introduced `conftest.py` for pytest configuration, including custom markers and fixtures for CUDA device management. - Updated `.gitignore` to exclude `parsed_output/` directory. - Added a README file in the tests directory to document the test structure, running instructions, and categories. - Removed outdated `unit_tests.py` file. This update enhances the testing framework and documentation, ensuring better maintainability and usability for developers. Pull Request resolved: #24 Reviewed By: xuzhao9 Differential Revision: D77936819 Pulled By: FindHao fbshipit-source-id: 459a462efdc40469c50405662a2051ab41f94095
1 parent 7a9828d commit 8d7459c

13 files changed

+483
-113
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ MANIFEST
4949
.pytest_cache/
5050
.coverage
5151
htmlcov/
52+
parsed_output/
53+
!tests/example_output/
54+
!tests/example_output/parsed_output/
55+
!tests/example_output/logs/
5256
.tox/
5357
.nox/
5458
.hypothesis/

pyproject.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ dependencies = [
99
"triton",
1010
]
1111

12+
[project.optional-dependencies]
13+
test = [
14+
"pytest>=7.0.0",
15+
"pytest-cov>=4.0.0",
16+
"pytest-xdist>=3.0.0",
17+
]
18+
1219
[tool.setuptools.packages.find]
1320
include = ["tritonparse*"]
1421

@@ -19,5 +26,20 @@ line-length = 88
1926
line-length = 88
2027
target-version = ["py310"]
2128

29+
[tool.pytest.ini_options]
30+
testpaths = ["tests"]
31+
python_files = ["test_*.py"]
32+
python_classes = ["Test*"]
33+
python_functions = ["test_*"]
34+
addopts = [
35+
"-v",
36+
"--tb=short",
37+
"--strict-markers",
38+
"--disable-warnings",
39+
]
40+
markers = [
41+
"cuda: mark test as requiring CUDA",
42+
]
43+
2244
[project.urls]
2345
"Homepage" = "https://github.com/pytorch-labs/tritonparse"

tests/README.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# Tritonparse Tests
2+
3+
This directory contains the test suite for tritonparse, including both automated unit tests and manual test examples.
4+
5+
## Test Structure
6+
7+
### Automated Tests
8+
- `test_tritonparse.py`: Comprehensive automated test suite
9+
- `conftest.py`: Pytest configuration and fixtures
10+
- `__init__.py`: Makes the directory a Python package
11+
12+
### Manual Test Examples
13+
- `test_add.py`: Manual test example for Triton kernel addition (not included in automated tests)
14+
15+
### Sample Data
16+
- `example_output/`: Example output directory containing:
17+
- `logs/`: Sample log files
18+
- `parsed_output/`: Sample parsed output files
19+
20+
## Running Tests
21+
22+
### Prerequisites
23+
24+
Install test dependencies:
25+
```bash
26+
pip install -e ".[test]"
27+
```
28+
29+
### Automated Tests
30+
31+
#### Running All Tests
32+
```bash
33+
# Using pytest directly
34+
python -m pytest tests/test_tritonparse.py -v
35+
36+
# With print statements visible
37+
python -m pytest tests/test_tritonparse.py -s -v
38+
39+
# With coverage
40+
python -m pytest tests/test_tritonparse.py --cov=tritonparse --cov-report=html
41+
```
42+
43+
#### Running Specific Test Categories
44+
```bash
45+
# CPU-only tests
46+
python -m pytest tests/test_tritonparse.py -m "not cuda"
47+
48+
# CUDA tests only (requires CUDA)
49+
python -m pytest tests/test_tritonparse.py -m cuda
50+
51+
# Specific test function
52+
python -m pytest tests/test_tritonparse.py::test_whole_workflow -s -v
53+
```
54+
55+
### Manual Test Example
56+
57+
The `test_add.py` file serves as a manual test example that demonstrates:
58+
59+
1. **Basic Triton kernel implementation**
60+
2. **Logging setup and usage**
61+
3. **Manual test execution**
62+
4. **Parsing workflow**
63+
64+
#### Running Manual Test
65+
```bash
66+
# Direct execution (not through pytest)
67+
TORCHINDUCTOR_FX_GRAPH_CACHE=0 TRITONPARSE_DEBUG=1 python tests/test_add.py
68+
```
69+
70+
This will:
71+
- Execute a simple tensor addition kernel
72+
- Generate log files in `./logs`
73+
- Parse the logs and output to `./parsed_output`
74+
75+
## Test Categories
76+
77+
### CPU Tests (No CUDA Required)
78+
- `test_convert()`: Tests data conversion functionality with various data types
79+
- `test_unified_parse()`: Tests parsing functionality with mock data
80+
81+
### CUDA Tests (Require GPU)
82+
- `test_extract_python_source_info()`: Tests Python source code extraction during Triton compilation
83+
- `test_whole_workflow()`: Tests complete workflow from kernel execution to log parsing
84+
85+
## Test Features
86+
87+
### Fixtures
88+
- `triton_hooks_setup`: Manages Triton hooks and compilation settings
89+
- Saves and restores all triton knobs (compilation and runtime hooks)
90+
- Ensures clean state between tests
91+
- Automatically restores settings even if tests fail
92+
93+
### CUDA Device Management
94+
- `cuda_device`: Provides CUDA device for tests
95+
- `cuda_available`: Checks CUDA availability
96+
- Automatic skipping of CUDA tests when CUDA is not available
97+
98+
### Kernel Isolation
99+
Each test function defines its own Triton kernel to avoid compilation cache interference:
100+
- `extract_test_kernel`: Simple multiplication kernel (x * 3.0)
101+
- `test_kernel`: Simple addition kernel (x + 1.0)
102+
103+
## Environment Variables
104+
105+
The following environment variables are used during testing:
106+
107+
- `TORCHINDUCTOR_FX_GRAPH_CACHE=0`: Disable FX graph caching
108+
- `TRITONPARSE_DEBUG=1`: Enable debug logging
109+
- `CUDA_VISIBLE_DEVICES=0`: Use first CUDA device (in CI)
110+
111+
## Test Configuration
112+
113+
### Pytest Configuration (`conftest.py`)
114+
- Custom markers for CUDA tests
115+
- Automatic CUDA availability checking
116+
- Fixtures for device management
117+
118+
### Test Isolation
119+
- Each test function has its own kernel definition
120+
- Fixtures ensure clean state between tests
121+
- Temporary directories are automatically cleaned up
122+
123+
## Adding New Tests
124+
125+
### For Automated Tests
126+
1. Add test functions to `test_tritonparse.py`
127+
2. Use `@pytest.mark.cuda` for CUDA tests
128+
3. Use `triton_hooks_setup` fixture for tests that modify Triton settings
129+
4. Define kernels inside test functions to avoid cache interference
130+
131+
Example:
132+
```python
133+
@pytest.mark.cuda
134+
def test_my_function(cuda_device, triton_hooks_setup):
135+
@triton.jit
136+
def my_kernel(x_ptr, y_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
137+
# Your kernel logic here
138+
pass
139+
140+
# Your test code here
141+
pass
142+
```
143+
144+
### For Manual Tests
145+
1. Create new files following the pattern of `test_add.py`
146+
2. Include direct execution with `if __name__ == "__main__"`
147+
3. Document the test purpose and execution method
148+
149+
## Continuous Integration
150+
151+
Tests are automatically run on GitHub Actions for:
152+
- Multiple Python versions (3.9, 3.10, 3.11)
153+
- CUDA versions (11.8, 12.1)
154+
- Code coverage reporting
155+
- Linting and formatting checks
156+
157+
## Troubleshooting
158+
159+
### Common Issues
160+
1. **CUDA not available**: Tests will be skipped automatically
161+
2. **No log files generated**: Check that Triton compilation is working
162+
3. **Import errors**: Ensure all dependencies are installed
163+
164+
### Debug Mode
165+
```bash
166+
# Run with verbose output and print statements
167+
python -m pytest tests/test_tritonparse.py -s -v --tb=long
168+
```
169+
170+
### Manual Verification
171+
```bash
172+
# Run manual test to verify basic functionality
173+
python tests/test_add.py
174+
175+
### Example Output
176+
The `example_output/` directory demonstrates the expected output structure:
177+
178+
```
179+
example_output/
180+
├── logs/
181+
│ └── dedicated_log_triton_trace_findhao_.ndjson
182+
└── parsed_output/
183+
├── dedicated_log_triton_trace_findhao__mapped.ndjson.gz
184+
├── log_file_list.json
185+
└── f0_fc0_a0_cai-.ndjson.gz
186+
```
187+
188+
These files can be used to:
189+
- Verify parsing functionality
190+
- Understand expected output format
191+
- Debug parsing issues
192+
- Test with real log data

tests/conftest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Pytest configuration for tritonparse tests.
3+
"""
4+
5+
import pytest
6+
import torch
7+
8+
9+
def pytest_configure(config):
10+
"""Configure pytest with custom markers."""
11+
config.addinivalue_line("markers", "cuda: mark test as requiring CUDA")
12+
13+
14+
def pytest_collection_modifyitems(config, items):
15+
"""Skip CUDA tests if CUDA is not available."""
16+
skip_cuda = pytest.mark.skip(reason="CUDA not available")
17+
18+
for item in items:
19+
if "cuda" in item.keywords:
20+
if not torch.cuda.is_available():
21+
item.add_marker(skip_cuda)
22+
23+
24+
@pytest.fixture(scope="session")
25+
def cuda_available():
26+
"""Check if CUDA is available for testing."""
27+
return torch.cuda.is_available()
28+
29+
30+
@pytest.fixture(scope="session")
31+
def cuda_device():
32+
"""Get the first available CUDA device."""
33+
if torch.cuda.is_available():
34+
return torch.device("cuda:0")
35+
else:
36+
pytest.skip("CUDA not available")

tests/dedicated_log_triton_trace_.ndjson

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/dedicated_log_triton_trace__mapped.ndjson

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/example_output/logs/dedicated_log_triton_trace_findhao_.ndjson

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tritonparse_url_prefix": "",
3+
"rank_default": {
4+
"regular_files": [
5+
"f0_fc0_a0_cai-.ndjson.gz"
6+
],
7+
"mapped_file": "dedicated_log_triton_trace_findhao__mapped.ndjson.gz",
8+
"rank_suffix": ""
9+
}
10+
}

0 commit comments

Comments
 (0)