Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2c1850e
feat: add FlashComm1 and MMRS fusion support
Cooofish Jul 7, 2026
12b45dc
Merge branch 'main' into flashcomm1-pr-default-off
pjgao Jul 7, 2026
17a9648
bugfix: make flashcomm1 pr build reliably.
Cooofish Jul 7, 2026
48672c1
refactor: simplify flashcomm1 mmrs wrapper.
Cooofish Jul 7, 2026
385a67f
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 7, 2026
e24773e
Merge branch 'main' into flashcomm1-pr-default-off
pjgao Jul 7, 2026
5f0a4e0
bugfix: address flashcomm1 review comments.
Cooofish Jul 7, 2026
f9b4934
bugfix: drop cmake and third_party changes from flashcomm1 pr.
Cooofish Jul 7, 2026
179a4cd
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 7, 2026
bf3d04f
refactor: reduce flashcomm1 pr diff scope
Cooofish Jul 8, 2026
0768651
merge: resolve conflicts with latest main
Cooofish Jul 13, 2026
4a239c7
merge: resolve conflicts with latest main
Cooofish Jul 20, 2026
19b1f31
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 20, 2026
8c24997
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 21, 2026
a0535fa
style: format flashcomm1 changes for ci.
Cooofish Jul 21, 2026
2b5a7d1
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 21, 2026
93ccc08
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 21, 2026
b56d8e5
merge: resolve PR conflicts with latest main.
Cooofish Jul 22, 2026
e1d5e7e
refactor: align FC1 config and backend scope.
Cooofish Jul 22, 2026
ab428ba
refactor: remove unused FC1 MMRS buffers
Cooofish Jul 22, 2026
f189579
refactor: share row-parallel forward implementation
Cooofish Jul 22, 2026
80b4afd
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 22, 2026
971dd79
fix: remove stale MMRS output validation
Cooofish Jul 22, 2026
15df323
fix: address flashcomm1 review feedback
Cooofish Jul 23, 2026
6fa9994
@Cooofish Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 23, 2026
d7c411b
fix: address FlashComm1 review comments
Cooofish Jul 24, 2026
a41771c
fix: harden MMRS weight lifecycle
Cooofish Jul 24, 2026
16a9d89
refactor: remove stale FlashComm1 paths
Cooofish Jul 24, 2026
b931967
refactor: clean up remaining FlashComm1 residue
Cooofish Jul 24, 2026
bf0a955
Merge branch 'main' into flashcomm1-pr-default-off
Cooofish Jul 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xllm/core/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ cc_library(
types.h
device_monitor.h
version_singleton.h
flash_comm1_context.h
SRCS
etcd_client.cpp
metrics.cpp
$<$<BOOL:${USE_NPU}>:mspti_helper.cpp>
options.cpp
rate_limiter.cpp
device_monitor.cpp
flash_comm1_context.cpp
DEPS
:config
util
parallel_state
absl::random_random
absl::strings
torch
Expand Down
313 changes: 313 additions & 0 deletions xllm/core/common/flash_comm1_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
/* Copyright 2026 The xLLM Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://github.com/jd-opensource/xllm/blob/main/LICENSE

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "flash_comm1_context.h"

#include <glog/logging.h>

#include <algorithm>

#include "framework/parallel_state/parallel_state.h"

namespace xllm {

namespace {

constexpr int32_t kFc1LocalTokenAlignment = 16;
constexpr int32_t kFc1MinTpSize = 8;
thread_local const FlashComm1Context* current_flash_comm1_context = nullptr;

int32_t round_up_to_multiple(int32_t value, int32_t multiple) {
CHECK_GT(multiple, 0);
const int32_t remainder = value % multiple;
return remainder == 0 ? value : value + multiple - remainder;
}

int32_t local_num_tokens_for_rank_impl(int32_t num_tokens,
int32_t world_size,
int32_t rank) {
const int32_t base = num_tokens / world_size;
const int32_t remainder = num_tokens % world_size;
return base + (rank < remainder ? 1 : 0);
}

int64_t shard_start_for_rank_impl(int32_t num_tokens,
int32_t world_size,
int32_t rank) {
const int32_t base = num_tokens / world_size;
const int32_t remainder = num_tokens % world_size;
return static_cast<int64_t>(rank) * base + std::min(rank, remainder);
}

} // namespace

FlashComm1ContextScope::FlashComm1ContextScope(const FlashComm1Context* ctx)
: previous_(current_flash_comm1_context) {
current_flash_comm1_context = ctx;
}

FlashComm1ContextScope::~FlashComm1ContextScope() {
current_flash_comm1_context = previous_;
}

const FlashComm1Context* get_current_flash_comm1_context() {
return current_flash_comm1_context;
}

bool is_sequence_sharded(const FlashComm1Context& ctx) {
return ctx.enabled && ctx.tp_world_size > 1;
}

int32_t local_num_tokens_for_rank(const FlashComm1Context& ctx, int32_t rank) {
CHECK_GE(rank, 0);
CHECK_LT(rank, ctx.tp_world_size);
return local_num_tokens_for_rank_impl(
ctx.original_num_tokens, ctx.tp_world_size, rank);
}

std::vector<int32_t> token_num_list(const FlashComm1Context& ctx) {
std::vector<int32_t> token_nums(ctx.tp_world_size);
for (int32_t rank = 0; rank < ctx.tp_world_size; ++rank) {
token_nums[rank] = local_num_tokens_for_rank(ctx, rank);
}
return token_nums;
}

int64_t get_shard_start(const FlashComm1Context& ctx) {
return shard_start_for_rank_impl(
ctx.original_num_tokens, ctx.tp_world_size, ctx.tp_rank);
}

int64_t get_shard_end(const FlashComm1Context& ctx) {
return get_shard_start(ctx) + ctx.local_num_tokens;
}

torch::Tensor pad_rows_by_copy(const torch::Tensor& input,
int64_t padded_rows) {
CHECK_GE(padded_rows, input.size(0));
if (padded_rows == input.size(0)) {
return input;
}

auto output_shape = input.sizes().vec();
output_shape[0] = padded_rows;
auto output = torch::empty(output_shape, input.options());
output.slice(0, 0, input.size(0)).copy_(input);
output.slice(0, input.size(0), padded_rows).zero_();
return output;
}

FlashComm1Context build_flash_comm1_context(int32_t num_tokens,
bool is_prefill,
const ParallelArgs& parallel_args,
const FlashComm1Options& options) {
int32_t actual_tp_size = parallel_args.world_size() /
(parallel_args.dp_size() * parallel_args.cp_size());

FlashComm1Context ctx;

if (!options.enable_flashcomm1) {
return ctx;
}

if (!is_prefill) {
return ctx;
}

#if !defined(USE_NPU)
return ctx;
#endif

if (parallel_args.dp_size() != 1 || parallel_args.cp_size() != 1) {
return ctx;
}

if (actual_tp_size < kFc1MinTpSize) {
return ctx;
}

ProcessGroup* tp_group = parallel_args.tp_group_;
if (!tp_group) {
return ctx;
}

int32_t threshold = options.min_prefill_tokens;

if (num_tokens < threshold) {
return ctx;
}

ctx.enabled = true;
ctx.tp_rank = tp_group->rank();
ctx.tp_world_size = tp_group->world_size();
ctx.original_num_tokens = num_tokens;
ctx.is_prefill = is_prefill;
ctx.enable_mmrs_fusion = options.enable_mmrs_fusion;
ctx.mmrs_comm_mode = options.mmrs_comm_mode;
ctx.tp_group = tp_group;

const int32_t token_alignment = ctx.tp_world_size * kFc1LocalTokenAlignment;
ctx.padded_num_tokens = round_up_to_multiple(num_tokens, token_alignment);
ctx.pad_size = ctx.padded_num_tokens - num_tokens;
ctx.local_num_tokens = local_num_tokens_for_rank(ctx, ctx.tp_rank);
ctx.padded_local_num_tokens = ctx.padded_num_tokens / ctx.tp_world_size;

return ctx;
}

torch::Tensor shard_sequence(const torch::Tensor& input,
const FlashComm1Context& ctx) {
if (!is_sequence_sharded(ctx)) {
return input;
}

CHECK_EQ(input.size(0), ctx.original_num_tokens);
torch::Tensor padded_input = input;
if (ctx.pad_size > 0) {
padded_input = pad_rows_by_copy(input, ctx.padded_num_tokens);
}

const int64_t shard_start =
static_cast<int64_t>(ctx.tp_rank) * ctx.padded_local_num_tokens;
const int64_t shard_end = shard_start + ctx.padded_local_num_tokens;
return padded_input.slice(0, shard_start, shard_end).contiguous();
Comment thread
yingxudeng marked this conversation as resolved.
Outdated
}

torch::Tensor gather_sequence(const torch::Tensor& input,
const FlashComm1Context& ctx) {
if (!is_sequence_sharded(ctx)) {
return input;
}

const int32_t current_local_size = input.size(0);
const int32_t expected_even_size = ctx.padded_num_tokens / ctx.tp_world_size;
const int32_t expected_local_size = ctx.local_num_tokens;

std::vector<int32_t> token_nums = token_num_list(ctx);
if (ctx.pad_size > 0 && current_local_size == expected_even_size) {
for (int32_t i = 0; i < ctx.tp_world_size; ++i) {
token_nums[i] = expected_even_size;
}
} else {
CHECK_EQ(current_local_size, expected_local_size)
<< "FC1 gather expected local real-token shard size "
<< expected_local_size << ", got " << current_local_size
<< ", rank=" << ctx.tp_rank << ", world=" << ctx.tp_world_size;
}

auto gathered = parallel_state::gather(input, ctx.tp_group, token_nums);

if (ctx.pad_size > 0 && gathered.size(0) > ctx.original_num_tokens) {
return gathered.slice(0, 0, ctx.original_num_tokens);
}
return gathered;
}

torch::Tensor gather_and_unpad_sequence(const torch::Tensor& input,
const FlashComm1Context& ctx) {
auto gathered = gather_sequence(input, ctx);
if (ctx.pad_size > 0) {
return gathered.slice(0, 0, ctx.original_num_tokens);
Comment thread
yingxudeng marked this conversation as resolved.
Outdated
}
return gathered;
}

namespace {

torch::Tensor reduce_scatter_padded_local(const torch::Tensor& input,
const FlashComm1Context& ctx) {
CHECK(ctx.tp_group);
CHECK(is_sequence_sharded(ctx));
CHECK_EQ(input.size(0), ctx.original_num_tokens)
<< "FC1 row-parallel reduce_scatter expects full real-token output "
<< "before communication.";

torch::Tensor padded_input = input;
if (ctx.pad_size > 0) {
padded_input = pad_rows_by_copy(input, ctx.padded_num_tokens);
}

auto output_shape = padded_input.sizes().vec();
output_shape[0] = ctx.padded_local_num_tokens;
torch::Tensor output = torch::empty(output_shape, padded_input.options());
ctx.tp_group->reduce_scatter(padded_input, output);
return output;
}

} // namespace

torch::Tensor maybe_pad_and_reduce(torch::Tensor input,
const FlashComm1Context& ctx,
RowParallelReduceMode mode) {
if (mode == RowParallelReduceMode::NONE) {
return input;
}

CHECK(mode == RowParallelReduceMode::ALL_REDUCE ||
mode == RowParallelReduceMode::REDUCE_SCATTER ||
mode == RowParallelReduceMode::MATMUL_REDUCE_SCATTER)
<< "Unsupported row-parallel reduce mode.";

if (!is_sequence_sharded(ctx)) {
if (ctx.tp_group && ctx.tp_group->world_size() > 1) {
return parallel_state::reduce(input, ctx.tp_group);
}
return input;
}

return reduce_scatter_padded_local(input, ctx);
}

RowParallelReduceMode row_parallel_reduce_mode_for_fc1(
const FlashComm1Context& ctx) {
return ctx.enable_mmrs_fusion ? RowParallelReduceMode::MATMUL_REDUCE_SCATTER
: RowParallelReduceMode::REDUCE_SCATTER;
}

torch::Tensor maybe_chunk_residual(const torch::Tensor& residual,
Comment thread
yingxudeng marked this conversation as resolved.
Outdated
int32_t tp_rank,
int32_t tp_world_size) {
if (tp_world_size <= 1) {
return residual;
}

CHECK_GE(tp_rank, 0);
CHECK_LT(tp_rank, tp_world_size);
const int32_t num_tokens = static_cast<int32_t>(residual.size(0));
const int64_t start =
shard_start_for_rank_impl(num_tokens, tp_world_size, tp_rank);
const int64_t end = start + local_num_tokens_for_rank_impl(
num_tokens, tp_world_size, tp_rank);
return residual.slice(0, start, end).contiguous();
}

torch::Tensor maybe_shard_residual(const torch::Tensor& residual,
const FlashComm1Context& ctx) {
if (!is_sequence_sharded(ctx)) {
return residual;
}
if (residual.size(0) == ctx.padded_local_num_tokens) {
return residual;
}
if (residual.size(0) == ctx.original_num_tokens) {
return shard_sequence(residual, ctx);
}
CHECK_EQ(residual.size(0), ctx.padded_local_num_tokens)
Comment thread
yingxudeng marked this conversation as resolved.
Outdated
<< "FC1 residual layout must be either full real-token or padded local "
<< "sequence shard.";
return residual;
}

} // namespace xllm
Loading
Loading