|
| 1 | +# LLM API Change Guide |
| 2 | + |
| 3 | +This guide explains how to modify and manage APIs in TensorRT LLM, focusing on the high-level LLM API. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +TensorRT LLM provides multiple API levels: |
| 8 | + |
| 9 | +1. **LLM API** - The highest-level API (e.g., the `LLM` class) |
| 10 | +2. **PyExecutor API** - The mid-level API (e.g., the `PyExecutor` class) |
| 11 | + |
| 12 | +This guide focuses on the LLM API, which is the primary interface for most users. |
| 13 | + |
| 14 | +## API Types and Stability Guarantees |
| 15 | + |
| 16 | +TensorRT LLM classifies APIs into two categories: |
| 17 | + |
| 18 | +### 1. Committed APIs |
| 19 | +- **Stable** and guaranteed to remain consistent across releases |
| 20 | +- No breaking changes without major version updates |
| 21 | +- Schema stored in: `tests/unittest/api_stability/references_committed/` |
| 22 | + |
| 23 | +### 2. Non-committed APIs |
| 24 | +- Under active development and may change between releases |
| 25 | +- Marked with a `status` field in the docstring: |
| 26 | + - `prototype` - Early experimental stage |
| 27 | + - `beta` - More stable but still subject to change |
| 28 | + - `deprecated` - Scheduled for removal |
| 29 | +- Schema stored in: `tests/unittest/api_stability/references/` |
| 30 | +- See [API status documentation](https://nvidia.github.io/TensorRT-LLM/llm-api/reference.html) for complete details |
| 31 | + |
| 32 | +## API Schema Management |
| 33 | + |
| 34 | +All API schemas are: |
| 35 | +- Stored as YAML files in the codebase |
| 36 | +- Protected by unit tests in `tests/unittest/api_stability/` |
| 37 | +- Automatically validated to ensure consistency |
| 38 | + |
| 39 | +## Modifying LLM Constructor Arguments |
| 40 | + |
| 41 | +The LLM class accepts numerous configuration parameters for models, runtime, and other components. These are managed through a Pydantic dataclass called `LlmArgs`. |
| 42 | + |
| 43 | +### Architecture |
| 44 | + |
| 45 | +- The LLM's `__init__` method parameters map directly to `LlmArgs` fields |
| 46 | +- `LlmArgs` is an alias for `TorchLlmArgs` (defined in `tensorrt_llm/llmapi/llm_args.py`) |
| 47 | +- All arguments are validated and type-checked through Pydantic |
| 48 | + |
| 49 | +### Adding a New Argument |
| 50 | + |
| 51 | +Follow these steps to add a new constructor argument: |
| 52 | + |
| 53 | +#### 1. Add the field to `TorchLlmArgs` |
| 54 | + |
| 55 | +```python |
| 56 | +garbage_collection_gen0_threshold: int = Field( |
| 57 | + default=20000, |
| 58 | + description=( |
| 59 | + "Threshold for Python garbage collection of generation 0 objects. " |
| 60 | + "Lower values trigger more frequent garbage collection." |
| 61 | + ), |
| 62 | + status="beta" # Required for non-committed arguments |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +**Field requirements:** |
| 67 | +- **Type annotation**: Required for all fields |
| 68 | +- **Default value**: Recommended unless the field is mandatory |
| 69 | +- **Description**: Clear explanation of the parameter's purpose |
| 70 | +- **Status**: Required for non-committed arguments (`prototype`, `beta`, etc.) |
| 71 | + |
| 72 | +#### 2. Update the API schema |
| 73 | + |
| 74 | +Add the field to the appropriate schema file: |
| 75 | + |
| 76 | +- **Non-committed arguments**: `tests/unittest/api_stability/references/llm_args.yaml` |
| 77 | + ```yaml |
| 78 | + garbage_collection_gen0_threshold: |
| 79 | + type: int |
| 80 | + default: 20000 |
| 81 | + status: beta # Must match the status in code |
| 82 | + ``` |
| 83 | +
|
| 84 | +- **Committed arguments**: `tests/unittest/api_stability/references_committed/llm_args.yaml` |
| 85 | + ```yaml |
| 86 | + garbage_collection_gen0_threshold: |
| 87 | + type: int |
| 88 | + default: 20000 |
| 89 | + # No status field for committed arguments |
| 90 | + ``` |
| 91 | + |
| 92 | +#### 3. Run validation tests |
| 93 | + |
| 94 | +```bash |
| 95 | +python -m pytest tests/unittest/api_stability/test_llm_api.py |
| 96 | +``` |
| 97 | + |
| 98 | +## Modifying LLM Class Methods |
| 99 | + |
| 100 | +Public methods in the LLM class constitute the API surface. All changes must be properly documented and tracked. |
| 101 | + |
| 102 | +### Implementation Details |
| 103 | + |
| 104 | +- The actual implementation is in the `_TorchLLM` class ([llm.py](https://github.com/NVIDIA/TensorRT-LLM/blob/release/1.0/tensorrt_llm/llmapi/llm.py)) |
| 105 | +- Public methods (not starting with `_`) are automatically exposed as APIs |
| 106 | + |
| 107 | +### Adding a New Method |
| 108 | + |
| 109 | +Follow these steps to add a new API method: |
| 110 | + |
| 111 | +#### 1. Implement the method in `_TorchLLM` |
| 112 | + |
| 113 | +For non-committed APIs, use the `@set_api_status` decorator: |
| 114 | + |
| 115 | +```python |
| 116 | +@set_api_status("beta") |
| 117 | +def generate_with_streaming( |
| 118 | + self, |
| 119 | + prompts: List[str], |
| 120 | + **kwargs |
| 121 | +) -> Iterator[GenerationOutput]: |
| 122 | + """Generate text with streaming output. |
| 123 | + |
| 124 | + Args: |
| 125 | + prompts: Input prompts for generation |
| 126 | + **kwargs: Additional generation parameters |
| 127 | + |
| 128 | + Returns: |
| 129 | + Iterator of generation outputs |
| 130 | + """ |
| 131 | + # Implementation here |
| 132 | + pass |
| 133 | +``` |
| 134 | + |
| 135 | +For committed APIs, no decorator is needed: |
| 136 | + |
| 137 | +```python |
| 138 | +def generate(self, prompts: List[str], **kwargs) -> GenerationOutput: |
| 139 | + """Generate text from prompts.""" |
| 140 | + # Implementation here |
| 141 | + pass |
| 142 | +``` |
| 143 | + |
| 144 | +#### 2. Update the API schema |
| 145 | + |
| 146 | +Add the method to the appropriate `llm.yaml` file: |
| 147 | + |
| 148 | +**Non-committed API** (`tests/unittest/api_stability/references/llm.yaml`): |
| 149 | +```yaml |
| 150 | +generate_with_streaming: |
| 151 | + status: beta # Must match @set_api_status |
| 152 | + parameters: |
| 153 | + - name: prompts |
| 154 | + type: List[str] |
| 155 | + - name: kwargs |
| 156 | + type: dict |
| 157 | + returns: Iterator[GenerationOutput] |
| 158 | +``` |
| 159 | + |
| 160 | +**Committed API** (`tests/unittest/api_stability/references_committed/llm.yaml`): |
| 161 | +```yaml |
| 162 | +generate: |
| 163 | + parameters: |
| 164 | + - name: prompts |
| 165 | + type: List[str] |
| 166 | + - name: kwargs |
| 167 | + type: dict |
| 168 | + returns: GenerationOutput |
| 169 | +``` |
| 170 | + |
| 171 | +### Modifying Existing Methods |
| 172 | + |
| 173 | +When modifying existing methods: |
| 174 | + |
| 175 | +1. **Non-breaking changes** (adding optional parameters): |
| 176 | + - Update the method signature |
| 177 | + - Update the schema file |
| 178 | + - No status change needed |
| 179 | + |
| 180 | +2. **Breaking changes** (changing required parameters, return types): |
| 181 | + - Only allowed for non-committed APIs |
| 182 | + - Consider deprecation path for beta APIs |
| 183 | + - Update documentation with migration guide |
| 184 | + |
| 185 | +### Best Practices |
| 186 | + |
| 187 | +1. **Documentation**: Always include comprehensive docstrings |
| 188 | +2. **Type hints**: Use proper type annotations for all parameters and returns |
| 189 | +3. **Testing**: Add unit tests for new methods |
| 190 | +4. **Examples**: Provide usage examples in the docstring |
| 191 | +5. **Validation**: Run API stability tests before submitting changes |
| 192 | + |
| 193 | +### Running Tests |
| 194 | + |
| 195 | +Validate your changes: |
| 196 | + |
| 197 | +```bash |
| 198 | +# Run API stability tests |
| 199 | +python -m pytest tests/unittest/api_stability/ |
| 200 | +
|
| 201 | +# Run specific test for LLM API |
| 202 | +python -m pytest tests/unittest/api_stability/test_llm_api.py -v |
| 203 | +``` |
| 204 | + |
| 205 | +## Common Workflows |
| 206 | + |
| 207 | +### Promoting an API from Beta to Committed |
| 208 | + |
| 209 | +1. Remove the `@set_api_status("beta")` decorator from the method |
| 210 | +2. Move the schema entry from `tests/unittest/api_stability/references/` to `tests/unittest/api_stability/references_committed/` |
| 211 | +3. Remove the `status` field from the schema |
| 212 | +4. Update any documentation referring to the API's beta status |
| 213 | + |
| 214 | +### Deprecating an API |
| 215 | + |
| 216 | +1. Add `@set_api_status("deprecated")` to the method |
| 217 | +2. Update the schema with `status: deprecated` |
| 218 | +3. Add deprecation warning in the method: |
| 219 | + ```python |
| 220 | + import warnings |
| 221 | + warnings.warn( |
| 222 | + "This method is deprecated and will be removed in v2.0. " |
| 223 | + "Use new_method() instead.", |
| 224 | + DeprecationWarning, |
| 225 | + stacklevel=2 |
| 226 | + ) |
| 227 | + ``` |
| 228 | +4. Document the migration path |
0 commit comments