Configuration for CUDA graphs.
| 105 | |
| 106 | |
| 107 | class CudaGraphConfig(StrictBaseModel): |
| 108 | """ |
| 109 | Configuration for CUDA graphs. |
| 110 | """ |
| 111 | # List of batch sizes to create CUDA graphs for. |
| 112 | batch_sizes: Optional[List[int]] = Field( |
| 113 | default=None, |
| 114 | description="List of batch sizes to create CUDA graphs for.") |
| 115 | |
| 116 | max_batch_size: int = Field( |
| 117 | default=0, description="Maximum batch size for CUDA graphs.") |
| 118 | |
| 119 | enable_padding: bool = Field( |
| 120 | default=False, |
| 121 | description= |
| 122 | "If true, batches are rounded up to the nearest cuda_graph_batch_size. This is usually a net win for performance." |
| 123 | ) |
| 124 | |
| 125 | @field_validator('max_batch_size') |
| 126 | @classmethod |
| 127 | def validate_cuda_graph_max_batch_size(cls, v): |
| 128 | """Validate cuda_graph_config.max_batch_size is non-negative.""" |
| 129 | if v < 0: |
| 130 | raise ValueError( |
| 131 | "cuda_graph_config.max_batch_size must be non-negative") |
| 132 | return v |
| 133 | |
| 134 | @staticmethod |
| 135 | def _generate_cuda_graph_batch_sizes(max_batch_size: int, |
| 136 | enable_padding: bool) -> List[int]: |
| 137 | """Generate a list of batch sizes for CUDA graphs. |
| 138 | |
| 139 | Args: |
| 140 | max_batch_size: Maximum batch size to generate up to |
| 141 | enable_padding: Whether padding is enabled, which affects the batch size distribution |
| 142 | |
| 143 | Returns: |
| 144 | List of batch sizes to create CUDA graphs for |
| 145 | """ |
| 146 | if enable_padding: |
| 147 | batch_sizes = [1, 2, 4] + [i * 8 for i in range(1, 17)] |
| 148 | else: |
| 149 | batch_sizes = list(range(1, 32)) + [32, 64, 128] |
| 150 | |
| 151 | # Add powers of 2 up to max_batch_size |
| 152 | batch_sizes += [ |
| 153 | 2**i for i in range(8, math.ceil(math.log(max_batch_size, 2))) |
| 154 | ] |
| 155 | |
| 156 | # Filter and sort batch sizes |
| 157 | batch_sizes = sorted( |
| 158 | [size for size in batch_sizes if size <= max_batch_size]) |
| 159 | |
| 160 | # Add max_batch_size if not already included |
| 161 | if max_batch_size != batch_sizes[-1]: |
| 162 | batch_sizes.append(max_batch_size) |
| 163 | |
| 164 | return batch_sizes |