|
| 1 | +""" |
| 2 | +Custom ops to enable multi-stream execution. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from threading import RLock |
| 8 | +from typing import Any, Callable, Dict, Tuple |
| 9 | + |
| 10 | +import torch |
| 11 | + |
| 12 | + |
| 13 | +class _Singleton(type): |
| 14 | + _instances: Dict[type, Any] = {} |
| 15 | + _lock = RLock() |
| 16 | + |
| 17 | + def __call__(cls, *args: Any, **kwargs: Any) -> Any: |
| 18 | + if cls not in cls._instances: |
| 19 | + with cls._lock: |
| 20 | + if cls not in cls._instances: # double-checked locking |
| 21 | + cls._instances[cls] = super().__call__(*args, **kwargs) |
| 22 | + return cls._instances[cls] |
| 23 | + |
| 24 | + |
| 25 | +# A singleton that holds the pointers to the cuda streams and events. |
| 26 | +# In multi-gpu scenario, each GPU/rank has its own CudaStreamManager. |
| 27 | +class CudaStreamManager(metaclass=_Singleton): |
| 28 | + AUX_STREAM_NAME = "aux" |
| 29 | + MAIN_STREAM_NAME = "main" |
| 30 | + |
| 31 | + def __init__(self) -> None: |
| 32 | + # In case __init__ ever gets called twice, guard against re-init |
| 33 | + if hasattr(self, "streams"): |
| 34 | + return |
| 35 | + |
| 36 | + self._lock = RLock() |
| 37 | + |
| 38 | + # Events needed for stream synchronization |
| 39 | + self.events: Dict[str, Any] = { |
| 40 | + self.AUX_STREAM_NAME: torch.cuda.Event(), |
| 41 | + self.MAIN_STREAM_NAME: torch.cuda.Event(), |
| 42 | + } |
| 43 | + |
| 44 | + # Streams for multi-stream execution |
| 45 | + self.aux_stream = torch.cuda.Stream() |
| 46 | + self.streams: Dict[str, Any] = { |
| 47 | + self.AUX_STREAM_NAME: self.aux_stream, |
| 48 | + self.MAIN_STREAM_NAME: torch.cuda.default_stream(), |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | +cuda_stream_manager = CudaStreamManager() |
| 53 | + |
| 54 | + |
| 55 | +@torch.library.custom_op("auto_deploy::record_event", mutates_args=()) |
| 56 | +def record_event(stream_name: str) -> None: |
| 57 | + event = cuda_stream_manager.events[stream_name] |
| 58 | + event.record() |
| 59 | + |
| 60 | + |
| 61 | +@torch.library.custom_op("auto_deploy::wait_event", mutates_args=()) |
| 62 | +def wait_event(event_name: str) -> None: |
| 63 | + event = cuda_stream_manager.events[event_name] |
| 64 | + event.wait() |
| 65 | + |
| 66 | + |
| 67 | +# skip during compilation |
| 68 | +@torch._dynamo.disable |
| 69 | +def record_event_wrapper( |
| 70 | + fn: Callable, |
| 71 | + *args: Tuple[Any, ...], |
| 72 | + **kwargs: Dict[str, Any], |
| 73 | +) -> torch.Tensor: |
| 74 | + output = fn(*args, **kwargs) |
| 75 | + torch.ops.auto_deploy.record_event(cuda_stream_manager.MAIN_STREAM_NAME) |
| 76 | + return output |
| 77 | + |
| 78 | + |
| 79 | +@torch._dynamo.disable |
| 80 | +def aux_stream_wrapper( |
| 81 | + fn: Callable, |
| 82 | + *args: Tuple[Any, ...], |
| 83 | + **kwargs: Dict[str, Any], |
| 84 | +) -> torch.Tensor: |
| 85 | + stream_name = cuda_stream_manager.AUX_STREAM_NAME |
| 86 | + with torch.cuda.stream(cuda_stream_manager.streams[stream_name]): |
| 87 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.MAIN_STREAM_NAME) |
| 88 | + output = fn(*args, **kwargs) |
| 89 | + torch.ops.auto_deploy.record_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 90 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 91 | + return output |
| 92 | + |
| 93 | + |
| 94 | +# trtllm bf16 |
| 95 | +@torch.library.custom_op("auto_deploy::trtllm_moe_fused_aux", mutates_args=()) |
| 96 | +def trtllm_moe_fused_aux( |
| 97 | + x: torch.Tensor, |
| 98 | + selected_experts: torch.Tensor, |
| 99 | + routing_weights: torch.Tensor, |
| 100 | + w3_w1_stacked_weight: torch.Tensor, |
| 101 | + w2_stacked_weight: torch.Tensor, |
| 102 | + mlp_style: str = "gated_mlp", |
| 103 | + act_fn: str = "silu", |
| 104 | +) -> torch.Tensor: |
| 105 | + with torch.cuda.stream(cuda_stream_manager.streams[cuda_stream_manager.AUX_STREAM_NAME]): |
| 106 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.MAIN_STREAM_NAME) |
| 107 | + output = torch.ops.auto_deploy.trtllm_moe_fused( |
| 108 | + x, |
| 109 | + selected_experts, |
| 110 | + routing_weights, |
| 111 | + w3_w1_stacked_weight, |
| 112 | + w2_stacked_weight, |
| 113 | + mlp_style, |
| 114 | + act_fn, |
| 115 | + ) |
| 116 | + torch.ops.auto_deploy.record_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 117 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 118 | + return output |
| 119 | + |
| 120 | + |
| 121 | +@trtllm_moe_fused_aux.register_fake |
| 122 | +def trtllm_moe_fused_aux_fake( |
| 123 | + x: torch.Tensor, |
| 124 | + selected_experts: torch.Tensor, |
| 125 | + routing_weights: torch.Tensor, |
| 126 | + w3_w1_stacked_weight: torch.Tensor, |
| 127 | + w2_stacked_weight: torch.Tensor, |
| 128 | + mlp_style: str = "gated_mlp", |
| 129 | + act_fn: str = "silu", |
| 130 | +) -> torch.Tensor: |
| 131 | + return torch.empty_like(x) |
| 132 | + |
| 133 | + |
| 134 | +# triton bf16 |
| 135 | +@torch.library.custom_op("auto_deploy::triton_moe_fused_aux", mutates_args=()) |
| 136 | +def triton_moe_fused_aux( |
| 137 | + x: torch.Tensor, |
| 138 | + selected_experts: torch.Tensor, |
| 139 | + routing_weights: torch.Tensor, |
| 140 | + w1_stacked_weight: torch.Tensor, |
| 141 | + w2_stacked_weight: torch.Tensor, |
| 142 | +) -> torch.Tensor: |
| 143 | + with torch.cuda.stream(cuda_stream_manager.streams[cuda_stream_manager.AUX_STREAM_NAME]): |
| 144 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.MAIN_STREAM_NAME) |
| 145 | + output = torch.ops.auto_deploy.triton_moe_fused( |
| 146 | + x, |
| 147 | + selected_experts, |
| 148 | + routing_weights, |
| 149 | + w1_stacked_weight, |
| 150 | + w2_stacked_weight, |
| 151 | + ) |
| 152 | + torch.ops.auto_deploy.record_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 153 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 154 | + return output |
| 155 | + |
| 156 | + |
| 157 | +@triton_moe_fused_aux.register_fake |
| 158 | +def triton_moe_fused_aux_fake( |
| 159 | + x: torch.Tensor, |
| 160 | + selected_experts: torch.Tensor, |
| 161 | + routing_weights: torch.Tensor, |
| 162 | + w1_stacked_weight: torch.Tensor, |
| 163 | + w2_stacked_weight: torch.Tensor, |
| 164 | +) -> torch.Tensor: |
| 165 | + return torch.empty_like(x) |
| 166 | + |
| 167 | + |
| 168 | +# trtllm fp8 |
| 169 | +@torch.library.custom_op("auto_deploy::trtllm_quant_fp8_moe_fused_aux", mutates_args=()) |
| 170 | +def trtllm_quant_fp8_moe_fused_aux( |
| 171 | + x: torch.Tensor, |
| 172 | + selected_experts: torch.Tensor, |
| 173 | + routing_weights: torch.Tensor, |
| 174 | + w1_weight: torch.Tensor, # [E, I, H] stacked FP8 weights |
| 175 | + w2_weight: torch.Tensor, # [E, H, I] stacked FP8 weights |
| 176 | + w3_weight: torch.Tensor, # [E, I, H] for gated_mlp, unused for mlp |
| 177 | + w1_input_scale: torch.Tensor, # [E] stacked input scales |
| 178 | + w2_input_scale: torch.Tensor, # [E] stacked input scales |
| 179 | + w3_input_scale: torch.Tensor, # [E] or unused |
| 180 | + w1_weight_scale: torch.Tensor, # [E] stacked weight scales |
| 181 | + w2_weight_scale: torch.Tensor, # [E] stacked weight scales |
| 182 | + w3_weight_scale: torch.Tensor, # [E] or unused |
| 183 | + gemm1_dequant: torch.Tensor, # [E] |
| 184 | + gemm2_act_quant: torch.Tensor, # [E] |
| 185 | + gemm2_dequant: torch.Tensor, # [E] |
| 186 | + mlp_style: str = "gated_mlp", |
| 187 | + act_fn: str = "silu", |
| 188 | +) -> torch.Tensor: |
| 189 | + with torch.cuda.stream(cuda_stream_manager.streams[cuda_stream_manager.AUX_STREAM_NAME]): |
| 190 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.MAIN_STREAM_NAME) |
| 191 | + output = torch.ops.auto_deploy.trtllm_quant_fp8_moe_fused( |
| 192 | + x, |
| 193 | + selected_experts, |
| 194 | + routing_weights, |
| 195 | + w1_weight, |
| 196 | + w2_weight, |
| 197 | + w3_weight, |
| 198 | + w1_input_scale, |
| 199 | + w2_input_scale, |
| 200 | + w3_input_scale, |
| 201 | + w1_weight_scale, |
| 202 | + w2_weight_scale, |
| 203 | + w3_weight_scale, |
| 204 | + gemm1_dequant, |
| 205 | + gemm2_act_quant, |
| 206 | + gemm2_dequant, |
| 207 | + mlp_style, |
| 208 | + act_fn, |
| 209 | + ) |
| 210 | + torch.ops.auto_deploy.record_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 211 | + torch.ops.auto_deploy.wait_event(cuda_stream_manager.AUX_STREAM_NAME) |
| 212 | + return output |
| 213 | + |
| 214 | + |
| 215 | +@trtllm_quant_fp8_moe_fused_aux.register_fake |
| 216 | +def trtllm_quant_fp8_moe_fused_aux_fake( |
| 217 | + x: torch.Tensor, |
| 218 | + selected_experts: torch.Tensor, |
| 219 | + routing_weights: torch.Tensor, |
| 220 | + w1_weight: torch.Tensor, |
| 221 | + w2_weight: torch.Tensor, |
| 222 | + w3_weight: torch.Tensor, |
| 223 | + w1_input_scale: torch.Tensor, |
| 224 | + w2_input_scale: torch.Tensor, |
| 225 | + w3_input_scale: torch.Tensor, |
| 226 | + w1_weight_scale: torch.Tensor, |
| 227 | + w2_weight_scale: torch.Tensor, |
| 228 | + w3_weight_scale: torch.Tensor, |
| 229 | + gemm1_dequant: torch.Tensor, |
| 230 | + gemm2_act_quant: torch.Tensor, |
| 231 | + gemm2_dequant: torch.Tensor, |
| 232 | + mlp_style: str = "gated_mlp", |
| 233 | + act_fn: str = "silu", |
| 234 | +) -> torch.Tensor: |
| 235 | + return torch.empty_like(x) |
0 commit comments