From ac3c8e28eb4a26493ea56649b2e0c68eca589ad4 Mon Sep 17 00:00:00 2001 From: project6-dev Date: Mon, 10 Aug 2026 06:25:15 +0000 Subject: [PATCH] =?UTF-8?q?fix(bridge):=20c10::nullopt=20=E2=86=92=20typed?= =?UTF-8?q?=20std::optional{}=20for=20CoreX=20torch=20compat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CoreX torch's c10::nullopt cannot implicitly convert to const std::optional&. Solution: use static typed empty optionals (kNoneTensor, kNoneBool). Also unified all c10::optional forward decls to std::optional. Applied same fix to ix_moe_bridge.cpp. --- ex_engine/csrc/ix_full_bridge.cpp | 48 ++++++++++++++++++------------- ex_engine/csrc/ix_moe_bridge.cpp | 37 +++++++++++++----------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/ex_engine/csrc/ix_full_bridge.cpp b/ex_engine/csrc/ix_full_bridge.cpp index b39ba94d..4f82deb8 100644 --- a/ex_engine/csrc/ix_full_bridge.cpp +++ b/ex_engine/csrc/ix_full_bridge.cpp @@ -16,6 +16,14 @@ #include #include +// ============================================================================ +// Compatibility: CoreX torch uses c10::optional which may not implicitly +// convert from kNoneTensor to const std::optional&. +// Use typed empty optionals instead. +// ============================================================================ +static const std::optional kNoneTensor = {}; +static const std::optional kNoneBool = {}; + // ============================================================================ // Forward-declare ixformer::infer namespace — matches ixformer.h exactly // We forward-declare instead of #include to avoid build-time dependency @@ -83,8 +91,8 @@ torch::Tensor ixformer_linear( torch::Tensor ixformer_linear_ex( torch::Tensor& input, torch::Tensor& weight, - const c10::optional& bias, - const c10::optional& out); + const std::optional& bias, + const std::optional& out); // --- MoE --- void topk_softmax( @@ -95,28 +103,28 @@ void topk_softmax( void moe_compute_token_index_api( torch::Tensor& topk_ids, torch::Tensor& src_dst, torch::Tensor& dst_src, torch::Tensor& expert_sizes_gpu, - const c10::optional& expert_mask, - const c10::optional& expert_sizes_cpu, - const c10::optional& expand_tokens_gpu, + const std::optional& expert_mask, + const std::optional& expert_sizes_cpu, + const std::optional& expand_tokens_gpu, int64_t start_expert_id, int64_t end_expert_id, int64_t num_experts); void moe_expand_input( torch::Tensor outputs, torch::Tensor inputs, torch::Tensor dst_to_src, - const c10::optional& src_to_dst, + const std::optional& src_to_dst, int64_t dst_tokens, int64_t expand_factor); void moe_w16a16_group_gemm( torch::Tensor output, torch::Tensor inputs, torch::Tensor weights, torch::Tensor tokens_per_experts, - const c10::optional& dst_to_src, - const c10::optional& bias, + const std::optional& dst_to_src, + const std::optional& bias, std::string format, int64_t persistent, int64_t output_n); void moe_output_reduce_sum( torch::Tensor outputs, torch::Tensor inputs, - const c10::optional& mul_weight, - const c10::optional& mask, - const c10::optional& extra_residual, + const std::optional& mul_weight, + const std::optional& mask, + const std::optional& extra_residual, double scaling_factor); } // namespace infer @@ -154,7 +162,7 @@ std::vector ix_moe_gen_idx( auto expert_sizes_gpu = expert_id.new_empty({expert_num}); ixformer::infer::moe_compute_token_index_api( expert_id, src_dst, dst_src, expert_sizes_gpu, - c10::nullopt, c10::nullopt, c10::nullopt, 0, expert_num, expert_num); + kNoneTensor, kNoneTensor, kNoneTensor, 0, expert_num, expert_num); auto expert_sizes_gpu_cumsum = expert_sizes_gpu.cumsum(-1); return {src_dst, dst_src, expert_sizes_gpu, expert_sizes_gpu_cumsum}; } @@ -178,7 +186,7 @@ torch::Tensor ix_group_gemm( auto output = inputs.new_empty({total_tokens, output_n}); ixformer::infer::moe_w16a16_group_gemm( output, inputs, weights, token_count, - c10::nullopt, c10::nullopt, "NT", 0, output_n); + kNoneTensor, kNoneTensor, "NT", 0, output_n); return output; } @@ -195,7 +203,7 @@ torch::Tensor ix_moe_combine_result(torch::Tensor input, torch::Tensor weight) { input = input.view({-1, weight.size(1), input.size(1)}); auto output = input.new_empty({input.size(0), input.size(2)}); ixformer::infer::moe_output_reduce_sum( - output, input, weight, c10::nullopt, c10::nullopt, 1.0); + output, input, weight, kNoneTensor, kNoneTensor, 1.0); return output; } @@ -234,7 +242,7 @@ void ix_paged_attention( block_tables, seq_lens, block_size, max_context_len, alibi_slopes, /*causal=*/true, /*window_left=*/-1, /*window_right=*/-1, /*softcap=*/0.0, /*enable_cuda_graph=*/false, /*use_sqrt_alibi=*/false, - /*sinks=*/c10::nullopt); + /*sinks=*/kNoneTensor); } // --- Attention: prefill flash (from ilu/attention.cpp batch_prefill) --- @@ -245,20 +253,20 @@ void ix_flash_attn_prefill( int64_t max_query_len, int64_t max_seq_len, double scale, bool is_causal, int64_t window_left, int64_t window_right) { - std::optional lse = c10::nullopt; + std::optional lse = {}; ixformer::infer::ixinfer_flash_attn_unpad_with_block_tables( query, key, value, output, block_tables, cu_seq_q, cu_seq_k, max_query_len, max_seq_len, is_causal, window_left, window_right, scale, /*softcap=*/0.0, /*sqrt_alibi=*/false, - /*alibi_slopes=*/c10::nullopt, /*sinks=*/c10::nullopt, lse); + /*alibi_slopes=*/kNoneTensor, /*sinks=*/kNoneTensor, lse); } // --- Norm: rms_norm (from ilu/norm.cpp) --- void ix_rms_norm( torch::Tensor output, torch::Tensor input, torch::Tensor weight, double eps) { - ixformer::infer::rms_norm(input, weight, output, c10::nullopt, eps); + ixformer::infer::rms_norm(input, weight, output, kNoneTensor, eps); } // --- Norm: fused residual + rms_norm (from ilu/norm.cpp) --- @@ -268,7 +276,7 @@ void ix_fused_add_rms_norm( torch::Tensor residual_output, double eps) { ixformer::infer::residual_rms_norm( input, residual, weight, output, residual_output, - c10::nullopt, 1.0, eps, false); + kNoneTensor, 1.0, eps, false); } // --- RoPE (from ilu/rope.cpp) --- @@ -297,7 +305,7 @@ torch::Tensor ix_linear( torch::Tensor input, torch::Tensor weight, const std::optional& bias) { return ixformer::infer::ixformer_linear( - input, weight, /*act_type=*/0, bias, c10::nullopt, c10::nullopt); + input, weight, /*act_type=*/-1, bias, kNoneTensor, kNoneBool); } // ============================================================================ diff --git a/ex_engine/csrc/ix_moe_bridge.cpp b/ex_engine/csrc/ix_moe_bridge.cpp index 6a9627a4..7a3d5f5b 100644 --- a/ex_engine/csrc/ix_moe_bridge.cpp +++ b/ex_engine/csrc/ix_moe_bridge.cpp @@ -1,8 +1,13 @@ // ix_moe_bridge.cpp — Full MoE pipeline bridge to ixformer C++ API +static const std::optional kNoneTensor = {}; // +static const std::optional kNoneTensor = {}; // Exposes ALL 6 MoE functions from ixformer::infer (ixformer.h): +static const std::optional kNoneTensor = {}; // 1. topk_softmax — fused routing +static const std::optional kNoneTensor = {}; // 2. moe_compute_token_index_api — permutation maps (src_dst, dst_src) +static const std::optional kNoneTensor = {}; // 3. moe_expand_input — gather tokens by expert // 4. moe_w16a16_group_gemm — batched expert GEMM // 5. silu_and_mul — fused activation @@ -31,9 +36,9 @@ void moe_compute_token_index_api( torch::Tensor& src_dst, torch::Tensor& dst_src, torch::Tensor& expert_sizes_gpu, - const c10::optional& expert_mask, - const c10::optional& expert_sizes_cpu, - const c10::optional& expand_tokens_gpu, + const std::optional& expert_mask, + const std::optional& expert_sizes_cpu, + const std::optional& expand_tokens_gpu, int64_t start_expert_id, int64_t end_expert_id, int64_t num_experts); @@ -41,7 +46,7 @@ void moe_compute_token_index_api( void moe_expand_input(torch::Tensor outputs, torch::Tensor inputs, torch::Tensor dst_to_src, - const c10::optional& src_to_dst, + const std::optional& src_to_dst, int64_t dst_tokens, int64_t expand_factor); @@ -49,17 +54,17 @@ void moe_w16a16_group_gemm(torch::Tensor output, torch::Tensor inputs, torch::Tensor weights, torch::Tensor tokens_per_experts, - const c10::optional& dst_to_src, - const c10::optional& bias, + const std::optional& dst_to_src, + const std::optional& bias, std::string format, int64_t persistent, int64_t output_n); void moe_output_reduce_sum(torch::Tensor outputs, torch::Tensor inputs, - const c10::optional& mul_weight, - const c10::optional& mask, - const c10::optional& extra_residual, + const std::optional& mul_weight, + const std::optional& mask, + const std::optional& extra_residual, double scaling_factor); void silu_and_mul(torch::Tensor& input, torch::Tensor& output); @@ -110,9 +115,9 @@ std::vector ix_moe_gen_idx( ixformer::infer::moe_compute_token_index_api( expert_id, src_dst, dst_src, expert_sizes_gpu, - /*expert_mask=*/c10::nullopt, - /*expert_sizes_cpu=*/c10::nullopt, - /*expand_tokens_gpu=*/c10::nullopt, + /*expert_mask=*/kNoneTensor, + /*expert_sizes_cpu=*/kNoneTensor, + /*expand_tokens_gpu=*/kNoneTensor, 0, expert_num, expert_num); expert_sizes_gpu_cumsum = expert_sizes_gpu.cumsum(-1); @@ -144,8 +149,8 @@ torch::Tensor ix_group_gemm( ixformer::infer::moe_w16a16_group_gemm( output, inputs, weights, token_count, - /*dst_to_src=*/c10::nullopt, - /*bias=*/c10::nullopt, + /*dst_to_src=*/kNoneTensor, + /*bias=*/kNoneTensor, /*format=*/"NT", /*persistent=*/0, /*output_n=*/output_n); @@ -169,8 +174,8 @@ torch::Tensor ix_moe_combine_result( ixformer::infer::moe_output_reduce_sum( output, input, weight, - /*mask=*/c10::nullopt, - /*extra_residual=*/c10::nullopt, + /*mask=*/kNoneTensor, + /*extra_residual=*/kNoneTensor, /*scaling_factor=*/1.0); return output; }