Merged
torch_extention编译优化:延迟编译 #8155
huangbolinAdd on Jul 3
torch_extention编译优化:延迟编译 #8155
Merged
Total: 24 files changed+65-29
| @@ -71,7 +71,6 @@ class CausalConv1dFnOpBuilder(OpBuilder): | |||
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | _causal_conv1d_fn_op_builder = CausalConv1dFnOpBuilder() | 73 | _causal_conv1d_fn_op_builder = CausalConv1dFnOpBuilder() |
| 74 | -_op_module = _causal_conv1d_fn_op_builder.load() | ||
| 75 | 74 | ||
| 76 | 75 | ||
| 77 | @impl(AS_LIBRARY, _causal_conv1d_fn_op_builder.name, "PrivateUse1") | 76 | @impl(AS_LIBRARY, _causal_conv1d_fn_op_builder.name, "PrivateUse1") |
| @@ -93,6 +92,7 @@ def _causal_conv1d_fn( | |||
| 93 | num_computed_tokens: Optional[torch.Tensor] = None, | 92 | num_computed_tokens: Optional[torch.Tensor] = None, |
| 94 | block_size_to_align: int = 0, | 93 | block_size_to_align: int = 0, |
| 95 | ) -> torch.Tensor: | 94 | ) -> torch.Tensor: |
| 95 | + _op_module = _causal_conv1d_fn_op_builder.load() | ||
| 96 | return _op_module.causal_conv1d_fn( | 96 | return _op_module.causal_conv1d_fn( |
| 97 | x, weight, bias, conv_states, | 97 | x, weight, bias, conv_states, |
| 98 | query_start_loc, cache_indices, | 98 | query_start_loc, cache_indices, |
| @@ -63,7 +63,6 @@ class CausalConv1dUpdateOpBuilder(OpBuilder): | |||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | _causal_conv1d_update_op_builder = CausalConv1dUpdateOpBuilder() | 65 | _causal_conv1d_update_op_builder = CausalConv1dUpdateOpBuilder() |
| 66 | -_op_module = _causal_conv1d_update_op_builder.load() | ||
| 67 | 66 | ||
| 68 | 67 | ||
| 69 | @impl(AS_LIBRARY, _causal_conv1d_update_op_builder.name, "PrivateUse1") | 68 | @impl(AS_LIBRARY, _causal_conv1d_update_op_builder.name, "PrivateUse1") |
| @@ -81,6 +80,7 @@ def _causal_conv1d_update( | |||
| 81 | block_idx_last_scheduled_token: Optional[torch.Tensor] = None, | 80 | block_idx_last_scheduled_token: Optional[torch.Tensor] = None, |
| 82 | initial_state_idx: Optional[torch.Tensor] = None, | 81 | initial_state_idx: Optional[torch.Tensor] = None, |
| 83 | ) -> torch.Tensor: | 82 | ) -> torch.Tensor: |
| 83 | + _op_module = _causal_conv1d_update_op_builder.load() | ||
| 84 | return _op_module.causal_conv1d_update( | 84 | return _op_module.causal_conv1d_update( |
| 85 | x, conv_state, weight, bias, | 85 | x, conv_state, weight, bias, |
| 86 | activation, | 86 | activation, |
| @@ -26,6 +26,27 @@ class CommContextOpBuilder(OpBuilder): | |||
| 26 | pass | 26 | pass |
| 27 | 27 | ||
| 28 | comm_context_op_builder = CommContextOpBuilder() | 28 | comm_context_op_builder = CommContextOpBuilder() |
| 29 | -op_module = comm_context_op_builder.load() | ||
| 30 | 29 | ||
| 31 | -CommContextManager = op_module.CommContextManager | 30 | + |
| 31 | +class _LazyClassProxy: | ||
| 32 | + def __init__(self, name, builder): | ||
| 33 | + self._name = name | ||
| 34 | + self._builder = builder | ||
| 35 | + self._real_cls = None | ||
| 36 | + | ||
| 37 | + def __call__(self, *args, **kwargs): | ||
| 38 | + return self._ensure_loaded()(*args, **kwargs) | ||
| 39 | + | ||
| 40 | + def __getattr__(self, name): | ||
| 41 | + return getattr(self._ensure_loaded(), name) | ||
| 42 | + | ||
| 43 | + def _ensure_loaded(self): | ||
| 44 | + if self._real_cls is None: | ||
| 45 | + self._real_cls = getattr(self._builder.load(), self._name) | ||
| 46 | + return self._real_cls | ||
| 47 | + | ||
| 48 | + | ||
| 49 | +def __getattr__(name): | ||
| 50 | + if name == "CommContextManager": | ||
| 51 | + return _LazyClassProxy("CommContextManager", comm_context_op_builder) | ||
| 52 | + raise AttributeError(f"module '{__name__}' has no attribute {name}") | ||
| @@ -59,7 +59,6 @@ class CompressorOpBuilder(OpBuilder): | |||
| 59 | 59 | ||
| 60 | 60 | ||
| 61 | compressor_op_builder = CompressorOpBuilder() | 61 | compressor_op_builder = CompressorOpBuilder() |
| 62 | -op_module = compressor_op_builder.load() | ||
| 63 | 62 | ||
| 64 | 63 | ||
| 65 | @impl(AS_LIBRARY, compressor_op_builder.name, "PrivateUse1") | 64 | @impl(AS_LIBRARY, compressor_op_builder.name, "PrivateUse1") |
| @@ -81,6 +80,7 @@ def compressor( | |||
| 81 | dispatcher implementation for NPU. | 80 | dispatcher implementation for NPU. |
| 82 | 'PrivateUse1' is the combine key for custom NPU backends. | 81 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 83 | """ | 82 | """ |
| 83 | + op_module = compressor_op_builder.load() | ||
| 84 | return op_module.compressor(x, wkv, wgate, state_cache, | 84 | return op_module.compressor(x, wkv, wgate, state_cache, |
| 85 | ape, cmp_ratio, | 85 | ape, cmp_ratio, |
| 86 | state_block_table, cu_seqlens, | 86 | state_block_table, cu_seqlens, |
| @@ -59,7 +59,6 @@ class ElasticBufferOpBuilder(OpBuilder): | |||
| 59 | 59 | ||
| 60 | 60 | ||
| 61 | _elastic_buffer_op_builder = ElasticBufferOpBuilder() | 61 | _elastic_buffer_op_builder = ElasticBufferOpBuilder() |
| 62 | -_elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 63 | 62 | ||
| 64 | 63 | ||
| 65 | @dataclass | 64 | @dataclass |
| @@ -183,6 +182,7 @@ class ElasticBuffer: | |||
| 183 | lambda: f"hidden_size must be 128-aligned, got {hidden_size}") | 182 | lambda: f"hidden_size must be 128-aligned, got {hidden_size}") |
| 184 | torch._check(dtype in (torch.bfloat16, torch.float16, torch.float32), | 183 | torch._check(dtype in (torch.bfloat16, torch.float16, torch.float32), |
| 185 | lambda: f"dtype must be bfloat16/float16/float32, got {dtype}") | 184 | lambda: f"dtype must be bfloat16/float16/float32, got {dtype}") |
| 185 | + _elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 186 | return _elastic_buffer_ops.ElasticBuffer.get_engram_storage_size_hint(num_entries, hidden_size, dtype) | 186 | return _elastic_buffer_ops.ElasticBuffer.get_engram_storage_size_hint(num_entries, hidden_size, dtype) |
| 187 | 187 | ||
| 188 | @staticmethod | 188 | @staticmethod |
| @@ -284,6 +284,7 @@ class ElasticBuffer: | |||
| 284 | ) | 284 | ) |
| 285 | hp_addr = self._prepare_host_counter(args.do_cpu_sync) | 285 | hp_addr = self._prepare_host_counter(args.do_cpu_sync) |
| 286 | 286 | ||
| 287 | + _elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 287 | num_recv_per_rank, num_recv_per_expert, dst_slot = \ | 288 | num_recv_per_rank, num_recv_per_expert, dst_slot = \ |
| 288 | _elastic_buffer_ops.ElasticBuffer.moe_ep_dispatch( | 289 | _elastic_buffer_ops.ElasticBuffer.moe_ep_dispatch( |
| 289 | context, args.x, args.topk_idx, topk_weights, args.scales, args.cached_dst_slot, | 290 | context, args.x, args.topk_idx, topk_weights, args.scales, args.cached_dst_slot, |
| @@ -319,6 +320,7 @@ class ElasticBuffer: | |||
| 319 | bias_0, bias_1 = self._unpack_bias(bias) | 320 | bias_0, bias_1 = self._unpack_bias(bias) |
| 320 | torch._check(((bias_0 is None) and (bias_1 is None)), lambda: (f"bias are not supported.")) | 321 | torch._check(((bias_0 is None) and (bias_1 is None)), lambda: (f"bias are not supported.")) |
| 321 | 322 | ||
| 323 | + _elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 322 | combined_x, combined_topk_weights = _elastic_buffer_ops.ElasticBuffer.moe_ep_combine( | 324 | combined_x, combined_topk_weights = _elastic_buffer_ops.ElasticBuffer.moe_ep_combine( |
| 323 | context, x, handle.topk_idx, handle.recv_src_metadata, | 325 | context, x, handle.topk_idx, handle.recv_src_metadata, |
| 324 | handle.num_recv_tokens_per_expert, topk_weights, bias_0, bias_1, | 326 | handle.num_recv_tokens_per_expert, topk_weights, bias_0, bias_1, |
| @@ -350,6 +352,7 @@ class ElasticBuffer: | |||
| 350 | if self.runtime is None: | 352 | if self.runtime is None: |
| 351 | rank_id = dist.get_rank(self.group) | 353 | rank_id = dist.get_rank(self.group) |
| 352 | self.group_name = self.group._get_backend(torch.device("npu")).get_hccl_comm_name(rank_id, init_comm=True) | 354 | self.group_name = self.group._get_backend(torch.device("npu")).get_hccl_comm_name(rank_id, init_comm=True) |
| 355 | + _elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 353 | self.runtime = _elastic_buffer_ops.ElasticBuffer(self.group_name, self.num_cpu_bytes) | 356 | self.runtime = _elastic_buffer_ops.ElasticBuffer(self.group_name, self.num_cpu_bytes) |
| 354 | return self.runtime | 357 | return self.runtime |
| 355 | 358 | ||
| @@ -427,6 +430,7 @@ class ElasticBuffer: | |||
| 427 | if not do_cpu_sync: | 430 | if not do_cpu_sync: |
| 428 | return 0 | 431 | return 0 |
| 429 | if self._host_pinned_counter is None: | 432 | if self._host_pinned_counter is None: |
| 433 | + _elastic_buffer_ops = _elastic_buffer_op_builder.load() | ||
| 430 | self._host_pinned_counter = _elastic_buffer_ops.HostPinnedCounter() | 434 | self._host_pinned_counter = _elastic_buffer_ops.HostPinnedCounter() |
| 431 | self._host_pinned_counter.reset() | 435 | self._host_pinned_counter.reset() |
| 432 | return self._host_pinned_counter.device_ptr() | 436 | return self._host_pinned_counter.device_ptr() |
| @@ -139,7 +139,6 @@ class FlashAttenOpBuilder(OpBuilder): | |||
| 139 | 139 | ||
| 140 | # Instantiate the builder | 140 | # Instantiate the builder |
| 141 | flash_attn_op_builder = FlashAttenOpBuilder() | 141 | flash_attn_op_builder = FlashAttenOpBuilder() |
| 142 | -op_module = flash_attn_op_builder.load() # Compiles/loads the .so file | ||
| 143 | 142 | ||
| 144 | 143 | ||
| 145 | @impl(AS_LIBRARY, FA_METADATA_OP_NAME, "PrivateUse1") | 144 | @impl(AS_LIBRARY, FA_METADATA_OP_NAME, "PrivateUse1") |
| @@ -156,6 +155,7 @@ def flash_attn_metadata( | |||
| 156 | Dispatcher implementation: NPU. | 155 | Dispatcher implementation: NPU. |
| 157 | 'PrivateUse1' is dispatch key for custom NPU backends. | 156 | 'PrivateUse1' is dispatch key for custom NPU backends. |
| 158 | """ | 157 | """ |
| 158 | + op_module = flash_attn_op_builder.load() | ||
| 159 | batch_size = _calculate_batch_size(batch_size, cu_seqlens_q, seqused_q) if batch_size is None else batch_size | 159 | batch_size = _calculate_batch_size(batch_size, cu_seqlens_q, seqused_q) if batch_size is None else batch_size |
| 160 | max_seqlen_q = -1 if max_seqlen_q is None else max_seqlen_q | 160 | max_seqlen_q = -1 if max_seqlen_q is None else max_seqlen_q |
| 161 | max_seqlen_kv = -1 if max_seqlen_kv is None else max_seqlen_kv | 161 | max_seqlen_kv = -1 if max_seqlen_kv is None else max_seqlen_kv |
| @@ -214,6 +214,7 @@ def flash_attn( | |||
| 214 | dispatcher implementation for NPU. | 214 | dispatcher implementation for NPU. |
| 215 | 'PrivateUse1' is the combine key for custom NPU backends. | 215 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 216 | """ | 216 | """ |
| 217 | + op_module = flash_attn_op_builder.load() | ||
| 217 | return op_module.flash_attn( | 218 | return op_module.flash_attn( |
| 218 | q, k, v, | 219 | q, k, v, |
| 219 | block_table, | 220 | block_table, |
| @@ -64,7 +64,6 @@ class IndexerQuantCacheOpBuilder(OpBuilder): | |||
| 64 | 64 | ||
| 65 | # Instantiate the builder | 65 | # Instantiate the builder |
| 66 | indexer_quant_cache_op_builder = IndexerQuantCacheOpBuilder() | 66 | indexer_quant_cache_op_builder = IndexerQuantCacheOpBuilder() |
| 67 | -op_module = indexer_quant_cache_op_builder.load() # Compiles/loads the .so file | ||
| 68 | 67 | ||
| 69 | 68 | ||
| 70 | @impl(AS_LIBRARY, indexer_quant_cache_op_builder.name, "PrivateUse1") | 69 | @impl(AS_LIBRARY, indexer_quant_cache_op_builder.name, "PrivateUse1") |
| @@ -72,6 +71,7 @@ def _indexer_quant_cache(cache: torch.Tensor, cache_scale: torch.Tensor, x: torc | |||
| 72 | slot_mapping: torch.Tensor, *, quant_mode: str = DEFAULT_QUANT_MODE, | 71 | slot_mapping: torch.Tensor, *, quant_mode: str = DEFAULT_QUANT_MODE, |
| 73 | round_scale: bool = True, x_scale: float = 1.0) -> None: | 72 | round_scale: bool = True, x_scale: float = 1.0) -> None: |
| 74 | quant_mode_int = _resolve_quant_mode(quant_mode) | 73 | quant_mode_int = _resolve_quant_mode(quant_mode) |
| 74 | + op_module = indexer_quant_cache_op_builder.load() | ||
| 75 | op_module.indexer_quant_cache(cache, cache_scale, x, slot_mapping, | 75 | op_module.indexer_quant_cache(cache, cache_scale, x, slot_mapping, |
| 76 | quant_mode_int, round_scale, x_scale) | 76 | quant_mode_int, round_scale, x_scale) |
| 77 | 77 | ||
| @@ -55,7 +55,6 @@ class InplacePartialRotaryMulOpBuilder(OpBuilder): | |||
| 55 | 55 | ||
| 56 | # Instantiate the builder | 56 | # Instantiate the builder |
| 57 | inplace_partial_rotary_mul_op_builder = InplacePartialRotaryMulOpBuilder() | 57 | inplace_partial_rotary_mul_op_builder = InplacePartialRotaryMulOpBuilder() |
| 58 | -op_module = inplace_partial_rotary_mul_op_builder.load() | ||
| 59 | 58 | ||
| 60 | 59 | ||
| 61 | @impl(AS_LIBRARY, inplace_partial_rotary_mul_op_builder.name, "PrivateUse1") | 60 | @impl(AS_LIBRARY, inplace_partial_rotary_mul_op_builder.name, "PrivateUse1") |
| @@ -72,6 +71,7 @@ def _inplace_partial_rotary_mul( | |||
| 72 | 'PrivateUse1' is the combine key for custom NPU backends. | 71 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 73 | """ | 72 | """ |
| 74 | partial_slice = [0, 0] if partial_slice is None else partial_slice | 73 | partial_slice = [0, 0] if partial_slice is None else partial_slice |
| 74 | + op_module = inplace_partial_rotary_mul_op_builder.load() | ||
| 75 | op_module.inplace_partial_rotary_mul(x, r1, r2, rotary_mode, partial_slice) | 75 | op_module.inplace_partial_rotary_mul(x, r1, r2, rotary_mode, partial_slice) |
| 76 | 76 | ||
| 77 | 77 | ||
| @@ -63,7 +63,6 @@ class KvCompressEpilogOpBuilder(OpBuilder): | |||
| 63 | 63 | ||
| 64 | # Instantiate the builder | 64 | # Instantiate the builder |
| 65 | kv_compress_epilog_op_builder = KvCompressEpilogOpBuilder() | 65 | kv_compress_epilog_op_builder = KvCompressEpilogOpBuilder() |
| 66 | -op_module = kv_compress_epilog_op_builder.load() # Compiles/loads the .so file | ||
| 67 | 66 | ||
| 68 | 67 | ||
| 69 | @impl(AS_LIBRARY, kv_compress_epilog_op_builder.name, "PrivateUse1") | 68 | @impl(AS_LIBRARY, kv_compress_epilog_op_builder.name, "PrivateUse1") |
| @@ -71,6 +70,7 @@ def _kv_compress_epilog(cache: torch.Tensor, x: torch.Tensor, slot_mapping: torc | |||
| 71 | quant_group_size: int = 64, quant_mode: str = DEFAULT_QUANT_MODE, | 70 | quant_group_size: int = 64, quant_mode: str = DEFAULT_QUANT_MODE, |
| 72 | round_scale: bool = True, x_scale: float = 1.0) -> None: | 71 | round_scale: bool = True, x_scale: float = 1.0) -> None: |
| 73 | quant_mode_int = _resolve_quant_mode(quant_mode) | 72 | quant_mode_int = _resolve_quant_mode(quant_mode) |
| 73 | + op_module = kv_compress_epilog_op_builder.load() | ||
| 74 | op_module.kv_compress_epilog(cache, x, slot_mapping, quant_group_size, | 74 | op_module.kv_compress_epilog(cache, x, slot_mapping, quant_group_size, |
| 75 | quant_mode_int, round_scale, x_scale) | 75 | quant_mode_int, round_scale, x_scale) |
| 76 | 76 | ||
| @@ -85,7 +85,6 @@ class LightningIndexerOpBuilder(OpBuilder): | |||
| 85 | 85 | ||
| 86 | # Instantiate the builder | 86 | # Instantiate the builder |
| 87 | lightning_indexer_op_builder = LightningIndexerOpBuilder() | 87 | lightning_indexer_op_builder = LightningIndexerOpBuilder() |
| 88 | -op_module = lightning_indexer_op_builder.load() # Compiles/loads the .so file | ||
| 89 | 88 | ||
| 90 | 89 | ||
| 91 | @impl(AS_LIBRARY, LI_METADATA_OP_NAME, "PrivateUse1") | 90 | @impl(AS_LIBRARY, LI_METADATA_OP_NAME, "PrivateUse1") |
| @@ -108,6 +107,7 @@ def lightning_indexer_metadata( | |||
| 108 | mask_mode = 0 if mask_mode is None else mask_mode | 107 | mask_mode = 0 if mask_mode is None else mask_mode |
| 109 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio | 108 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio |
| 110 | 109 | ||
| 110 | + op_module = lightning_indexer_op_builder.load() | ||
| 111 | return op_module.lightning_indexer_metadata( | 111 | return op_module.lightning_indexer_metadata( |
| 112 | num_heads_q, num_heads_k, head_dim, topk, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, cmp_residual_k, | 112 | num_heads_q, num_heads_k, head_dim, topk, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, cmp_residual_k, |
| 113 | batch_size, max_seqlen_q, max_seqlen_k, layout_q, layout_k, mask_mode, cmp_ratio) | 113 | batch_size, max_seqlen_q, max_seqlen_k, layout_q, layout_k, mask_mode, cmp_ratio) |
| @@ -140,6 +140,7 @@ def lightning_indexer(q, k, w, topk, *, cu_seqlens_q=None, cu_seqlens_k=None, | |||
| 140 | dispatcher implementation for NPU.zhe | 140 | dispatcher implementation for NPU.zhe |
| 141 | 'PrivateUse1' is the combine key for custom NPU backends. | 141 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 142 | """ | 142 | """ |
| 143 | + op_module = lightning_indexer_op_builder.load() | ||
| 143 | return op_module.lightning_indexer(q, k, w, topk, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, | 144 | return op_module.lightning_indexer(q, k, w, topk, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, |
| 144 | cmp_residual_k, block_table, output_idx_offset, metadata, max_seqlen_q, | 145 | cmp_residual_k, block_table, output_idx_offset, metadata, max_seqlen_q, |
| 145 | layout_q, layout_k, mask_mode, cmp_ratio, return_value) | 146 | layout_q, layout_k, mask_mode, cmp_ratio, return_value) |
| @@ -63,7 +63,6 @@ class _MegaMoeOpBuilder(OpBuilder): | |||
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | _mega_moe_op_builder = _MegaMoeOpBuilder() | 65 | _mega_moe_op_builder = _MegaMoeOpBuilder() |
| 66 | -_op_module = _mega_moe_op_builder.load() | ||
| 67 | 66 | ||
| 68 | 67 | ||
| 69 | @impl(AS_LIBRARY, _mega_moe_op_builder.name, "PrivateUse1") | 68 | @impl(AS_LIBRARY, _mega_moe_op_builder.name, "PrivateUse1") |
| @@ -74,7 +73,8 @@ def _npu_mega_moe(context, x, topk_ids, topk_weights, weight1, weight2, | |||
| 74 | combine_quant_mode=0, comm_alg="", | 73 | combine_quant_mode=0, comm_alg="", |
| 75 | num_max_tokens_per_rank=0, activation="swiglu", activation_clamp=None, | 74 | num_max_tokens_per_rank=0, activation="swiglu", activation_clamp=None, |
| 76 | dispatch_quant_out_dtype=None, | 75 | dispatch_quant_out_dtype=None, |
| 77 | - weight1_type=None, weight2_type=None, topo_type=None): | 76 | + weight1_type=None, weight2_type=None, topo_type=None): |
| 77 | + _op_module = _mega_moe_op_builder.load() | ||
| 78 | return _op_module.npu_mega_moe( | 78 | return _op_module.npu_mega_moe( |
| 79 | context, x, topk_ids, topk_weights, weight1, weight2, moe_expert_num, ep_world_size, | 79 | context, x, topk_ids, topk_weights, weight1, weight2, moe_expert_num, ep_world_size, |
| 80 | ccl_buffer_size, weight_scales1, weight_scales2, bias1, bias2, x_active_mask, | 80 | ccl_buffer_size, weight_scales1, weight_scales2, bias1, bias2, x_active_mask, |
| @@ -17,6 +17,7 @@ from cann_ops_transformer.op_builder.builder import AS_LIBRARY | |||
| 17 | class MhcPostFunction(torch.autograd.Function): | 17 | class MhcPostFunction(torch.autograd.Function): |
| 18 | @staticmethod | 18 | @staticmethod |
| 19 | def forward(ctx, x, h_res, h_out, h_post): | 19 | def forward(ctx, x, h_res, h_out, h_post): |
| 20 | + op_module = mhc_post_op_builder.load() | ||
| 20 | out = op_module.mhc_post(x, h_res, h_out, h_post) | 21 | out = op_module.mhc_post(x, h_res, h_out, h_post) |
| 21 | ctx.save_for_backward(x, h_res, h_out, h_post) | 22 | ctx.save_for_backward(x, h_res, h_out, h_post) |
| 22 | return out | 23 | return out |
| @@ -47,11 +48,11 @@ class MhcPostOpBuilder(OpBuilder): | |||
| 47 | return torch.empty_like(x) | 48 | return torch.empty_like(x) |
| 48 | 49 | ||
| 49 | mhc_post_op_builder = MhcPostOpBuilder() | 50 | mhc_post_op_builder = MhcPostOpBuilder() |
| 50 | -op_module = mhc_post_op_builder.load() | ||
| 51 | 51 | ||
| 52 | 52 | ||
| 53 | @impl(AS_LIBRARY, mhc_post_op_builder.name, "PrivateUse1") | 53 | @impl(AS_LIBRARY, mhc_post_op_builder.name, "PrivateUse1") |
| 54 | def _mhc_post_dispatch(x, h_res, h_out, h_post): | 54 | def _mhc_post_dispatch(x, h_res, h_out, h_post): |
| 55 | + op_module = mhc_post_op_builder.load() | ||
| 55 | return op_module.mhc_post(x, h_res, h_out, h_post) | 56 | return op_module.mhc_post(x, h_res, h_out, h_post) |
| 56 | 57 | ||
| 57 | 58 | ||
| @@ -61,4 +62,5 @@ def mhc_post(x, h_res, h_out, h_post): | |||
| 61 | if needs_grad: | 62 | if needs_grad: |
| 62 | return MhcPostFunction.apply(x, h_res, h_out, h_post) | 63 | return MhcPostFunction.apply(x, h_res, h_out, h_post) |
| 63 | else: | 64 | else: |
| 65 | + op_module = mhc_post_op_builder.load() | ||
| 64 | return op_module.mhc_post(x, h_res, h_out, h_post) | 66 | return op_module.mhc_post(x, h_res, h_out, h_post) |
| @@ -36,9 +36,9 @@ class MhcPostBackwardOpBuilder(OpBuilder): | |||
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | mhc_post_backward_op_builder = MhcPostBackwardOpBuilder() | 38 | mhc_post_backward_op_builder = MhcPostBackwardOpBuilder() |
| 39 | -op_module = mhc_post_backward_op_builder.load() | ||
| 40 | 39 | ||
| 41 | 40 | ||
| 42 | @impl(AS_LIBRARY, mhc_post_backward_op_builder.name, "PrivateUse1") | 41 | @impl(AS_LIBRARY, mhc_post_backward_op_builder.name, "PrivateUse1") |
| 43 | def mhc_post_backward(grad_output, x, h_res, h_out, h_post): | 42 | def mhc_post_backward(grad_output, x, h_res, h_out, h_post): |
| 43 | + op_module = mhc_post_backward_op_builder.load() | ||
| 44 | return op_module.mhc_post_backward(grad_output, x, h_res, h_out, h_post) | 44 | return op_module.mhc_post_backward(grad_output, x, h_res, h_out, h_post) |
| @@ -20,6 +20,7 @@ class MhcPreSinkhornFunction(torch.autograd.Function): | |||
| 20 | @staticmethod | 20 | @staticmethod |
| 21 | def forward(ctx, x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps): | 21 | def forward(ctx, x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps): |
| 22 | # autograd 路径强制 out_flag=True,以保存 backward 所需的中间结果 | 22 | # autograd 路径强制 out_flag=True,以保存 backward 所需的中间结果 |
| 23 | + op_module = mhc_pre_sinkhorn_op_builder.load() | ||
| 23 | hin, h_post, h_res, h_pre, hc_before_norm, inv_rms, sum_out, norm_out = \ | 24 | hin, h_post, h_res, h_pre, hc_before_norm, inv_rms, sum_out, norm_out = \ |
| 24 | op_module.mhc_pre_sinkhorn(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, True) | 25 | op_module.mhc_pre_sinkhorn(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, True) |
| 25 | 26 | ||
| @@ -86,11 +87,11 @@ class MhcPreSinkhornOpBuilder(OpBuilder): | |||
| 86 | 87 | ||
| 87 | 88 | ||
| 88 | mhc_pre_sinkhorn_op_builder = MhcPreSinkhornOpBuilder() | 89 | mhc_pre_sinkhorn_op_builder = MhcPreSinkhornOpBuilder() |
| 89 | -op_module = mhc_pre_sinkhorn_op_builder.load() | ||
| 90 | 90 | ||
| 91 | 91 | ||
| 92 | @impl(AS_LIBRARY, mhc_pre_sinkhorn_op_builder.name, "PrivateUse1") | 92 | @impl(AS_LIBRARY, mhc_pre_sinkhorn_op_builder.name, "PrivateUse1") |
| 93 | def _mhc_pre_sinkhorn_dispatch(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, out_flag): | 93 | def _mhc_pre_sinkhorn_dispatch(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, out_flag): |
| 94 | + op_module = mhc_pre_sinkhorn_op_builder.load() | ||
| 94 | return op_module.mhc_pre_sinkhorn(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, out_flag) | 95 | return op_module.mhc_pre_sinkhorn(x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, out_flag) |
| 95 | 96 | ||
| 96 | 97 | ||
| @@ -110,6 +111,7 @@ def mhc_pre_sinkhorn(x, phi, alpha, bias, hc_mult, num_iters, hc_eps=1e-6, norm_ | |||
| 110 | ) | 111 | ) |
| 111 | else: | 112 | else: |
| 112 | # 普通路径:out_flag=False,不保存中间变量,只返回 3 个主输出 | 113 | # 普通路径:out_flag=False,不保存中间变量,只返回 3 个主输出 |
| 114 | + op_module = mhc_pre_sinkhorn_op_builder.load() | ||
| 113 | hin, h_post, h_res, _, _, _, _, _ = op_module.mhc_pre_sinkhorn( | 115 | hin, h_post, h_res, _, _, _, _, _ = op_module.mhc_pre_sinkhorn( |
| 114 | x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, False | 116 | x, phi, alpha, bias, hc_mult, num_iters, hc_eps, norm_eps, False |
| 115 | ) | 117 | ) |
| @@ -42,14 +42,14 @@ class MhcPreSinkhornBackwardOpBuilder(OpBuilder): | |||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | mhc_pre_sinkhorn_backward_op_builder = MhcPreSinkhornBackwardOpBuilder() | 44 | mhc_pre_sinkhorn_backward_op_builder = MhcPreSinkhornBackwardOpBuilder() |
| 45 | -op_module = mhc_pre_sinkhorn_backward_op_builder.load() | ||
| 46 | 45 | ||
| 47 | 46 | ||
| 48 | @impl(AS_LIBRARY, mhc_pre_sinkhorn_backward_op_builder.name, "PrivateUse1") | 47 | @impl(AS_LIBRARY, mhc_pre_sinkhorn_backward_op_builder.name, "PrivateUse1") |
| 49 | def mhc_pre_sinkhorn_backward(grad_hin, grad_h_post, grad_h_res, | 48 | def mhc_pre_sinkhorn_backward(grad_hin, grad_h_post, grad_h_res, |
| 50 | - x, phi, alpha, bias, | 49 | + x, phi, alpha, bias, |
| 51 | - h_pre, hc_before_norm, inv_rms, | 50 | + h_pre, hc_before_norm, inv_rms, |
| 52 | - sum_out, norm_out, hc_eps): | 51 | + sum_out, norm_out, hc_eps): |
| 52 | + op_module = mhc_pre_sinkhorn_backward_op_builder.load() | ||
| 53 | return op_module.mhc_pre_sinkhorn_backward(grad_hin, grad_h_post, grad_h_res, | 53 | return op_module.mhc_pre_sinkhorn_backward(grad_hin, grad_h_post, grad_h_res, |
| 54 | x, phi, alpha, bias, | 54 | x, phi, alpha, bias, |
| 55 | h_pre, hc_before_norm, inv_rms, | 55 | h_pre, hc_before_norm, inv_rms, |
| @@ -118,7 +118,6 @@ class MixedQuantSparseFlashMlaOpBuilder(OpBuilder): | |||
| 118 | 118 | ||
| 119 | # Instantiate the builder | 119 | # Instantiate the builder |
| 120 | mixed_quant_sparse_flash_mla_op_builder = MixedQuantSparseFlashMlaOpBuilder() | 120 | mixed_quant_sparse_flash_mla_op_builder = MixedQuantSparseFlashMlaOpBuilder() |
| 121 | -op_module = mixed_quant_sparse_flash_mla_op_builder.load() # Compiles/loads the .so file | ||
| 122 | 121 | ||
| 123 | 122 | ||
| 124 | @impl(AS_LIBRARY, MQSMLA_METADATA_OP_NAME, "PrivateUse1") | 123 | @impl(AS_LIBRARY, MQSMLA_METADATA_OP_NAME, "PrivateUse1") |
| @@ -155,6 +154,7 @@ def mixed_quant_sparse_flash_mla_metadata( | |||
| 155 | has_ori_kv = True if has_ori_kv is None else has_ori_kv | 154 | has_ori_kv = True if has_ori_kv is None else has_ori_kv |
| 156 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv | 155 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv |
| 157 | 156 | ||
| 157 | + op_module = mixed_quant_sparse_flash_mla_op_builder.load() | ||
| 158 | return op_module.mixed_quant_sparse_flash_mla_metadata( | 158 | return op_module.mixed_quant_sparse_flash_mla_metadata( |
| 159 | num_heads_q, num_heads_kv, head_dim, quant_mode, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, | 159 | num_heads_q, num_heads_kv, head_dim, quant_mode, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, |
| 160 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, | 160 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, |
| @@ -208,6 +208,7 @@ def mixed_quant_sparse_flash_mla(q, | |||
| 208 | dispatcher implementation for NPU | 208 | dispatcher implementation for NPU |
| 209 | 'PrivateUse1' is the combine key for custom NPU backends. | 209 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 210 | """ | 210 | """ |
| 211 | + op_module = mixed_quant_sparse_flash_mla_op_builder.load() | ||
| 211 | return op_module.mixed_quant_sparse_flash_mla(q, | 212 | return op_module.mixed_quant_sparse_flash_mla(q, |
| 212 | ori_kv, cmp_kv, | 213 | ori_kv, cmp_kv, |
| 213 | ori_sparse_indices, cmp_sparse_indices, | 214 | ori_sparse_indices, cmp_sparse_indices, |
| @@ -57,7 +57,6 @@ class MoeDistributeCombineOpBuilder(OpBuilder): | |||
| 57 | 57 | ||
| 58 | # Instantiate the builder | 58 | # Instantiate the builder |
| 59 | moe_distribute_combine_op_builder = MoeDistributeCombineOpBuilder() | 59 | moe_distribute_combine_op_builder = MoeDistributeCombineOpBuilder() |
| 60 | -op_module = moe_distribute_combine_op_builder.load() # Compiles/loads the .so file | ||
| 61 | 60 | ||
| 62 | 61 | ||
| 63 | @impl(AS_LIBRARY, moe_distribute_combine_op_builder.name, "PrivateUse1") | 62 | @impl(AS_LIBRARY, moe_distribute_combine_op_builder.name, "PrivateUse1") |
| @@ -74,6 +73,7 @@ def npu_moe_distribute_combine(context, expand_x, expert_ids, assist_info_for_co | |||
| 74 | dispatcher implementation for NPU. | 73 | dispatcher implementation for NPU. |
| 75 | 'PrivateUse1' is the combine key for custom NPU backends. | 74 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 76 | """ | 75 | """ |
| 76 | + op_module = moe_distribute_combine_op_builder.load() # Compiles/loads the .so file | ||
| 77 | return op_module.npu_moe_distribute_combine(context, expand_x, expert_ids, assist_info_for_combine, | 77 | return op_module.npu_moe_distribute_combine(context, expand_x, expert_ids, assist_info_for_combine, |
| 78 | ep_send_counts, expert_scales, ep_world_size, ep_rank_id, | 78 | ep_send_counts, expert_scales, ep_world_size, ep_rank_id, |
| 79 | moe_expert_num, ccl_buffer_size, | 79 | moe_expert_num, ccl_buffer_size, |
| @@ -210,7 +210,6 @@ class MoeDistributeDispatchOpBuilder(OpBuilder): | |||
| 210 | 210 | ||
| 211 | # Instantiate the builder | 211 | # Instantiate the builder |
| 212 | moe_distribute_dispatch_op_builder = MoeDistributeDispatchOpBuilder() | 212 | moe_distribute_dispatch_op_builder = MoeDistributeDispatchOpBuilder() |
| 213 | -op_module = moe_distribute_dispatch_op_builder.load() # Compiles/loads the .so file | ||
| 214 | 213 | ||
| 215 | 214 | ||
| 216 | @impl(AS_LIBRARY, moe_distribute_dispatch_op_builder.name, "PrivateUse1") | 215 | @impl(AS_LIBRARY, moe_distribute_dispatch_op_builder.name, "PrivateUse1") |
| @@ -225,6 +224,7 @@ def npu_moe_distribute_dispatch(context, x, expert_ids, ep_world_size, ep_rank_i | |||
| 225 | Dispatcher implementation for NPU. | 224 | Dispatcher implementation for NPU. |
| 226 | 'PrivateUse1' is the dispatch key for custom NPU backends. | 225 | 'PrivateUse1' is the dispatch key for custom NPU backends. |
| 227 | """ | 226 | """ |
| 227 | + op_module = moe_distribute_dispatch_op_builder.load() # Compiles/loads the .so file | ||
| 228 | return op_module.npu_moe_distribute_dispatch(context, x, expert_ids, ep_world_size, ep_rank_id, moe_expert_num, | 228 | return op_module.npu_moe_distribute_dispatch(context, x, expert_ids, ep_world_size, ep_rank_id, moe_expert_num, |
| 229 | ccl_buffer_size, scales, x_active_mask, expert_scales, elastic_info, | 229 | ccl_buffer_size, scales, x_active_mask, expert_scales, elastic_info, |
| 230 | performance_info, tp_world_size, tp_rank_id, | 230 | performance_info, tp_world_size, tp_rank_id, |
| @@ -121,11 +121,11 @@ class MoeTokenPermuteOpBuilder(OpBuilder): | |||
| 121 | 121 | ||
| 122 | 122 | ||
| 123 | moe_token_permute_op_builder = MoeTokenPermuteOpBuilder() | 123 | moe_token_permute_op_builder = MoeTokenPermuteOpBuilder() |
| 124 | -op_module = moe_token_permute_op_builder.load() | ||
| 125 | 124 | ||
| 126 | 125 | ||
| 127 | @impl(AS_LIBRARY, moe_token_permute_op_builder.name, "PrivateUse1") | 126 | @impl(AS_LIBRARY, moe_token_permute_op_builder.name, "PrivateUse1") |
| 128 | def _moe_token_permute(tokens, indices, num_out_tokens=None, padded_mode=False, quant_mode=-1): | 127 | def _moe_token_permute(tokens, indices, num_out_tokens=None, padded_mode=False, quant_mode=-1): |
| 128 | + op_module = moe_token_permute_op_builder.load() | ||
| 129 | return op_module.moe_token_permute(tokens, indices, num_out_tokens, padded_mode, quant_mode) | 129 | return op_module.moe_token_permute(tokens, indices, num_out_tokens, padded_mode, quant_mode) |
| 130 | 130 | ||
| 131 | 131 | ||
| @@ -90,7 +90,6 @@ class QuantLightningIndexerOpBuilder(OpBuilder): | |||
| 90 | 90 | ||
| 91 | # Instantiate the builder | 91 | # Instantiate the builder |
| 92 | quant_lightning_indexer_op_builder = QuantLightningIndexerOpBuilder() | 92 | quant_lightning_indexer_op_builder = QuantLightningIndexerOpBuilder() |
| 93 | -op_module = quant_lightning_indexer_op_builder.load() # Compiles/loads the .so file | ||
| 94 | 93 | ||
| 95 | 94 | ||
| 96 | @impl(AS_LIBRARY, QLI_METADATA_OP_NAME, "PrivateUse1") | 95 | @impl(AS_LIBRARY, QLI_METADATA_OP_NAME, "PrivateUse1") |
| @@ -113,6 +112,7 @@ def quant_lightning_indexer_metadata( | |||
| 113 | mask_mode = 0 if mask_mode is None else mask_mode | 112 | mask_mode = 0 if mask_mode is None else mask_mode |
| 114 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio | 113 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio |
| 115 | 114 | ||
| 115 | + op_module = quant_lightning_indexer_op_builder.load() | ||
| 116 | return op_module.quant_lightning_indexer_metadata( | 116 | return op_module.quant_lightning_indexer_metadata( |
| 117 | num_heads_q, num_heads_k, head_dim, topk, quant_mode, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, | 117 | num_heads_q, num_heads_k, head_dim, topk, quant_mode, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, |
| 118 | cmp_residual_k, batch_size, max_seqlen_q, max_seqlen_k, layout_q, layout_k, mask_mode, cmp_ratio) | 118 | cmp_residual_k, batch_size, max_seqlen_q, max_seqlen_k, layout_q, layout_k, mask_mode, cmp_ratio) |
| @@ -146,6 +146,7 @@ def quant_lightning_indexer(query, key, weights, query_dequant_scale, key_dequan | |||
| 146 | dispatcher implementation for NPU.zhe | 146 | dispatcher implementation for NPU.zhe |
| 147 | 'PrivateUse1' is the combine key for custom NPU backends. | 147 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 148 | """ | 148 | """ |
| 149 | + op_module = quant_lightning_indexer_op_builder.load() | ||
| 149 | return op_module.quant_lightning_indexer(query, key, weights, query_dequant_scale, key_dequant_scale, | 150 | return op_module.quant_lightning_indexer(query, key, weights, query_dequant_scale, key_dequant_scale, |
| 150 | topk, quant_mode, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, | 151 | topk, quant_mode, cu_seqlens_q, cu_seqlens_k, seqused_q, seqused_k, |
| 151 | cmp_residual_k, block_table, output_idx_offset, metadata, | 152 | cmp_residual_k, block_table, output_idx_offset, metadata, |
| @@ -89,7 +89,6 @@ class ScatterPaKvCacheWithKScaleOpBuilder(OpBuilder): | |||
| 89 | 89 | ||
| 90 | # 实例化OpBuilder | 90 | # 实例化OpBuilder |
| 91 | scatter_pa_kv_cache_with_k_scale_op_builder = ScatterPaKvCacheWithKScaleOpBuilder() | 91 | scatter_pa_kv_cache_with_k_scale_op_builder = ScatterPaKvCacheWithKScaleOpBuilder() |
| 92 | -op_module = scatter_pa_kv_cache_with_k_scale_op_builder.load() | ||
| 93 | 92 | ||
| 94 | 93 | ||
| 95 | @impl(AS_LIBRARY, scatter_pa_kv_cache_with_k_scale_op_builder.name, "PrivateUse1") | 94 | @impl(AS_LIBRARY, scatter_pa_kv_cache_with_k_scale_op_builder.name, "PrivateUse1") |
| @@ -125,6 +124,7 @@ def scatter_pa_kv_cache_with_k_scale( | |||
| 125 | 2. slot_mapping值必须在[0, num_blocks*block_size-1]范围内 | 124 | 2. slot_mapping值必须在[0, num_blocks*block_size-1]范围内 |
| 126 | 3. 超出范围的slot会被跳过(不更新对应cache) | 125 | 3. 超出范围的slot会被跳过(不更新对应cache) |
| 127 | """ | 126 | """ |
| 127 | + op_module = scatter_pa_kv_cache_with_k_scale_op_builder.load() | ||
| 128 | return op_module.scatter_pa_kv_cache_with_k_scale( | 128 | return op_module.scatter_pa_kv_cache_with_k_scale( |
| 129 | key, value, key_cache, value_cache, | 129 | key, value, key_cache, value_cache, |
| 130 | slot_mapping, key_scale, key_scale_cache, | 130 | slot_mapping, key_scale, key_scale_cache, |
| @@ -115,7 +115,6 @@ class SparseFlashMlaOpBuilder(OpBuilder): | |||
| 115 | 115 | ||
| 116 | # Instantiate the builder | 116 | # Instantiate the builder |
| 117 | sparse_flash_mla_op_builder = SparseFlashMlaOpBuilder() | 117 | sparse_flash_mla_op_builder = SparseFlashMlaOpBuilder() |
| 118 | -op_module = sparse_flash_mla_op_builder.load() # Compiles/loads the .so file | ||
| 119 | 118 | ||
| 120 | 119 | ||
| 121 | @impl(AS_LIBRARY, SMLA_METADATA_OP_NAME, "PrivateUse1") | 120 | @impl(AS_LIBRARY, SMLA_METADATA_OP_NAME, "PrivateUse1") |
| @@ -150,6 +149,7 @@ def sparse_flash_mla_metadata( | |||
| 150 | has_ori_kv = True if has_ori_kv is None else has_ori_kv | 149 | has_ori_kv = True if has_ori_kv is None else has_ori_kv |
| 151 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv | 150 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv |
| 152 | 151 | ||
| 152 | + op_module = sparse_flash_mla_op_builder.load() | ||
| 153 | return op_module.sparse_flash_mla_metadata( | 153 | return op_module.sparse_flash_mla_metadata( |
| 154 | num_heads_q, num_heads_kv, head_dim, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, | 154 | num_heads_q, num_heads_kv, head_dim, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, |
| 155 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, | 155 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, |
| @@ -200,6 +200,7 @@ def sparse_flash_mla(q, | |||
| 200 | dispatcher implementation for NPU. | 200 | dispatcher implementation for NPU. |
| 201 | 'PrivateUse1' is the combine key for custom NPU backends. | 201 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 202 | """ | 202 | """ |
| 203 | + op_module = sparse_flash_mla_op_builder.load() | ||
| 203 | return op_module.sparse_flash_mla(q, | 204 | return op_module.sparse_flash_mla(q, |
| 204 | ori_kv, cmp_kv, | 205 | ori_kv, cmp_kv, |
| 205 | ori_sparse_indices, cmp_sparse_indices, | 206 | ori_sparse_indices, cmp_sparse_indices, |
| @@ -97,7 +97,6 @@ class SparseFlashMlaGradOpBuilder(OpBuilder): | |||
| 97 | 97 | ||
| 98 | # Instantiate the builder | 98 | # Instantiate the builder |
| 99 | smlag_op_builder = SparseFlashMlaGradOpBuilder() | 99 | smlag_op_builder = SparseFlashMlaGradOpBuilder() |
| 100 | -op_module = smlag_op_builder.load() # Compiles/loads the .so file | ||
| 101 | 100 | ||
| 102 | 101 | ||
| 103 | @impl(AS_LIBRARY, SMLAG_METADATA_OP_NAME, "PrivateUse1") | 102 | @impl(AS_LIBRARY, SMLAG_METADATA_OP_NAME, "PrivateUse1") |
| @@ -132,6 +131,7 @@ def sparse_flash_mla_grad_metadata( | |||
| 132 | has_ori_kv = True if has_ori_kv is None else has_ori_kv | 131 | has_ori_kv = True if has_ori_kv is None else has_ori_kv |
| 133 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv | 132 | has_cmp_kv = True if has_cmp_kv is None else has_cmp_kv |
| 134 | 133 | ||
| 134 | + op_module = smlag_op_builder.load() | ||
| 135 | return op_module.sparse_flash_mla_grad_metadata( | 135 | return op_module.sparse_flash_mla_grad_metadata( |
| 136 | num_heads_q, num_heads_kv, head_dim, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, | 136 | num_heads_q, num_heads_kv, head_dim, cu_seqlens_q, cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, |
| 137 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, | 137 | seqused_ori_kv, seqused_cmp_kv, cmp_residual_kv, ori_topk_length, cmp_topk_length, batch_size, max_seqlen_q, |
| @@ -176,6 +176,7 @@ def sparse_flash_mla_grad(q, dout, attn_out, softmax_lse, ori_kv=None, cmp_kv=No | |||
| 176 | dispatcher implementation for NPU. | 176 | dispatcher implementation for NPU. |
| 177 | 'PrivateUse1' is the combine key for custom NPU backends. | 177 | 'PrivateUse1' is the combine key for custom NPU backends. |
| 178 | """ | 178 | """ |
| 179 | + op_module = smlag_op_builder.load() | ||
| 179 | return op_module.sparse_flash_mla_grad(q, dout, attn_out, softmax_lse, ori_kv, cmp_kv, | 180 | return op_module.sparse_flash_mla_grad(q, dout, attn_out, softmax_lse, ori_kv, cmp_kv, |
| 180 | ori_sparse_indices, cmp_sparse_indices, cu_seqlens_q, | 181 | ori_sparse_indices, cmp_sparse_indices, cu_seqlens_q, |
| 181 | cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, | 182 | cu_seqlens_ori_kv, cu_seqlens_cmp_kv, seqused_q, |
| @@ -102,7 +102,6 @@ class SparseLightningIndexerKLLossGradOpBuilder(OpBuilder): | |||
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | sparse_lightning_indexer_kl_loss_grad_op_builder = SparseLightningIndexerKLLossGradOpBuilder() | 104 | sparse_lightning_indexer_kl_loss_grad_op_builder = SparseLightningIndexerKLLossGradOpBuilder() |
| 105 | -op_module = sparse_lightning_indexer_kl_loss_grad_op_builder.load() | ||
| 106 | 105 | ||
| 107 | 106 | ||
| 108 | @impl(AS_LIBRARY, SLI_KL_LOSS_GRAD_METADATA_OP_NAME, "PrivateUse1") | 107 | @impl(AS_LIBRARY, SLI_KL_LOSS_GRAD_METADATA_OP_NAME, "PrivateUse1") |
| @@ -134,6 +133,7 @@ def sparse_lightning_indexer_kl_loss_grad_metadata( | |||
| 134 | mask_mode = 0 if mask_mode is None else mask_mode | 133 | mask_mode = 0 if mask_mode is None else mask_mode |
| 135 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio | 134 | cmp_ratio = 1 if cmp_ratio is None else cmp_ratio |
| 136 | 135 | ||
| 136 | + op_module = sparse_lightning_indexer_kl_loss_grad_op_builder.load() | ||
| 137 | return op_module.sparse_lightning_indexer_kl_loss_grad_metadata( | 137 | return op_module.sparse_lightning_indexer_kl_loss_grad_metadata( |
| 138 | num_heads_q, | 138 | num_heads_q, |
| 139 | num_heads_k, | 139 | num_heads_k, |
| @@ -216,6 +216,7 @@ def sparse_lightning_indexer_kl_loss_grad( | |||
| 216 | mask_mode=3, | 216 | mask_mode=3, |
| 217 | cmp_ratio=1, | 217 | cmp_ratio=1, |
| 218 | ): | 218 | ): |
| 219 | + op_module = sparse_lightning_indexer_kl_loss_grad_op_builder.load() | ||
| 219 | return op_module.sparse_lightning_indexer_kl_loss_grad( | 220 | return op_module.sparse_lightning_indexer_kl_loss_grad( |
| 220 | q, | 221 | q, |
| 221 | k, | 222 | k, |