|
| 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 |
0 commit comments