(requested: str, device: torch.device, dtype: torch.dtype)
| 145 | |
| 146 | |
| 147 | def resolve_attn_implementation(requested: str, device: torch.device, dtype: torch.dtype) -> str | None: |
| 148 | requested_norm = (requested or "").strip().lower() |
| 149 | |
| 150 | if requested_norm in {"none"}: |
| 151 | return None |
| 152 | |
| 153 | if requested_norm not in {"", "auto"}: |
| 154 | return requested |
| 155 | |
| 156 | # Prefer FlashAttention 2 when package + device conditions are met. |
| 157 | if ( |
| 158 | device.type == "cuda" |
| 159 | and importlib.util.find_spec("flash_attn") is not None |
| 160 | and dtype in {torch.float16, torch.bfloat16} |
| 161 | ): |
| 162 | major, _ = torch.cuda.get_device_capability(device) |
| 163 | if major >= 8: |
| 164 | return "flash_attention_2" |
| 165 | |
| 166 | # CUDA fallback: use PyTorch SDPA kernels. |
| 167 | if device.type == "cuda": |
| 168 | return "sdpa" |
| 169 | |
| 170 | # CPU fallback. |
| 171 | return "eager" |
| 172 | |
| 173 | |
| 174 | def detect_text_language(text: str) -> str: |
no outgoing calls
no test coverage detected