|
2 | 2 | # Copyright (c) 2026 RL-Kernel Contributors |
3 | 3 |
|
4 | 4 | import importlib |
| 5 | +import os |
5 | 6 | from enum import Enum, EnumMeta |
6 | 7 | from typing import Any, Dict, Optional, Set, Type |
7 | 8 |
|
@@ -32,13 +33,15 @@ class OpBackend(Enum, metaclass=_KernelEnumMeta): |
32 | 33 | # AMD ROCm optimized stack |
33 | 34 | ROCM_AITER = "rl_engine.kernels.ops.rocm.aiter.AiterOp" |
34 | 35 | ROCM_CK = "rl_engine.kernels.ops.rocm.composable_kernel.CKOp" |
| 36 | + ROCM_FLASH_ATTN = "rl_engine.kernels.ops.rocm.attention.flash_attn.RocmFlashAttentionOp" |
35 | 37 |
|
36 | 38 | # GRPO loss (group reward normalization + clipped surrogate + KL) |
37 | 39 | TRITON_GRPO_LOSS = "rl_engine.kernels.ops.triton.triton_grpo_loss.TritonGRPOLossOp" |
38 | 40 | PYTORCH_GRPO_LOSS = "rl_engine.kernels.ops.pytorch.loss.grpo_loss.NativeGRPOLossOp" |
39 | 41 |
|
40 | 42 | # Generic fallback |
41 | 43 | TRITON_GENERIC = "rl_engine.kernels.ops.triton.generic.TritonOp" |
| 44 | + PYTORCH_ATTN = "rl_engine.kernels.ops.pytorch.attention.NativeAttentionOp" |
42 | 45 | PYTORCH_NATIVE = "rl_engine.kernels.ops.pytorch.loss.logp.NativeLogpOp" |
43 | 46 |
|
44 | 47 |
|
@@ -72,24 +75,43 @@ def __init__(self): |
72 | 75 | OpBackend.CUDA_FUSED_LOGP_GENERIC, |
73 | 76 | OpBackend.PYTORCH_NATIVE, |
74 | 77 | ], |
75 | | - "attn": [OpBackend.FLASH_ATTN, OpBackend.TRITON_GENERIC, OpBackend.PYTORCH_NATIVE], |
| 78 | + "attn": [OpBackend.FLASH_ATTN, OpBackend.TRITON_GENERIC, OpBackend.PYTORCH_ATTN], |
76 | 79 | "grpo_loss": [OpBackend.TRITON_GRPO_LOSS, OpBackend.PYTORCH_GRPO_LOSS], |
77 | 80 | # Default dispatch logic for new operators |
78 | 81 | }, |
79 | 82 | "rocm": { |
80 | 83 | "logp": [OpBackend.ROCM_AITER, OpBackend.TRITON_GENERIC, OpBackend.PYTORCH_NATIVE], |
81 | | - "attn": [OpBackend.TRITON_GENERIC, OpBackend.PYTORCH_NATIVE], |
| 84 | + "attn": [ |
| 85 | + OpBackend.PYTORCH_ATTN, |
| 86 | + OpBackend.ROCM_FLASH_ATTN, |
| 87 | + OpBackend.TRITON_GENERIC, |
| 88 | + ], |
82 | 89 | "grpo_loss": [OpBackend.TRITON_GRPO_LOSS, OpBackend.PYTORCH_GRPO_LOSS], |
83 | 90 | }, |
84 | 91 | "cpu": { |
85 | 92 | "logp": [OpBackend.PYTORCH_NATIVE], |
86 | | - "attn": [OpBackend.PYTORCH_NATIVE], |
| 93 | + "attn": [OpBackend.PYTORCH_ATTN], |
87 | 94 | "grpo_loss": [OpBackend.PYTORCH_GRPO_LOSS], |
88 | 95 | }, |
89 | 96 | } |
90 | 97 | logger.info(f"KernelRegistry initialized for {device_ctx.device_type}") |
| 98 | + self._adjust_priority_from_env() |
91 | 99 | self._adjust_priority_for_hardware() |
92 | 100 |
|
| 101 | + def _adjust_priority_from_env(self): |
| 102 | + rocm_attn_backend = os.getenv("RL_KERNEL_ROCM_ATTN_BACKEND", "").strip().lower() |
| 103 | + if rocm_attn_backend in {"flash_attn", "flash-attn", "flash_attention"}: |
| 104 | + self._priority_map["rocm"]["attn"] = [ |
| 105 | + OpBackend.ROCM_FLASH_ATTN, |
| 106 | + OpBackend.PYTORCH_ATTN, |
| 107 | + OpBackend.TRITON_GENERIC, |
| 108 | + ] |
| 109 | + elif rocm_attn_backend and rocm_attn_backend not in {"native", "pytorch", "sdpa"}: |
| 110 | + logger.warning( |
| 111 | + "Unknown RL_KERNEL_ROCM_ATTN_BACKEND=%s; using default ROCm attention priority.", |
| 112 | + rocm_attn_backend, |
| 113 | + ) |
| 114 | + |
93 | 115 | def _adjust_priority_for_hardware(self): |
94 | 116 | """Prioritize the fused TMA LogP kernel only when it is compiled into the |
95 | 117 | extension and the device is TMA-capable (SM90/100/120).""" |
|
0 commit comments