Skip to content

Commit 19449dd

Browse files
committed
feat: add onerec worker impl.
1 parent ea71989 commit 19449dd

File tree

13 files changed

+529
-11
lines changed

13 files changed

+529
-11
lines changed

xllm/core/common/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cc_library(
1414
$<$<BOOL:${USE_NPU}>:mspti_helper.h>
1515
options.h
1616
rate_limiter.h
17+
rec_model_utils.h
1718
types.h
1819
device_monitor.h
1920
version_singleton.h
@@ -66,4 +67,3 @@ cc_test(
6667
target_link_libraries(common PRIVATE OpenSSL::SSL OpenSSL::Crypto protobuf::libprotobuf)
6768
add_dependencies(common brpc-static)
6869

69-

xllm/core/common/rec_model_utils.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2025 The xLLM Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://github.com/jd-opensource/xllm/blob/main/LICENSE
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#pragma once
17+
18+
#include <cstdint>
19+
#include <string_view>
20+
21+
namespace xllm {
22+
23+
enum class RecModelKind : int8_t {
24+
kNone = 0,
25+
kOneRec = 1,
26+
kLlmRec = 2,
27+
};
28+
29+
inline constexpr bool is_onerec_model_type(std::string_view model_type) {
30+
return model_type == "onerec";
31+
}
32+
33+
inline constexpr bool is_llmrec_model_type(std::string_view model_type) {
34+
return model_type == "qwen2" || model_type == "qwen3";
35+
}
36+
37+
inline constexpr RecModelKind get_rec_model_kind(std::string_view model_type) {
38+
if (is_onerec_model_type(model_type)) {
39+
return RecModelKind::kOneRec;
40+
}
41+
if (is_llmrec_model_type(model_type)) {
42+
return RecModelKind::kLlmRec;
43+
}
44+
return RecModelKind::kNone;
45+
}
46+
47+
} // namespace xllm
48+

xllm/core/distributed_runtime/master.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ limitations under the License.
2222
#include <boost/algorithm/string.hpp>
2323
#include <csignal>
2424
#include <memory>
25+
#include <optional>
2526
#include <thread>
2627
#include <utility>
2728

@@ -30,6 +31,7 @@ limitations under the License.
3031
#include "common/types.h"
3132
#include "dit_master.h"
3233
#include "framework/model/model_args.h"
34+
#include "framework/model_loader.h"
3335
#include "framework/request/request.h"
3436
#include "llm_engine.h"
3537
#include "llm_master.h"
@@ -237,8 +239,13 @@ Master::Master(const Options& options, EngineType type) : options_(options) {
237239
options_.enable_schedule_overlap(false);
238240
LOG(WARNING) << "Force to disable schedule overlap for REC model, not "
239241
"supported yet.";
242+
243+
auto model_loader = ModelLoader::create(options_.model_path());
244+
const std::string rec_model_type = model_loader->model_args().model_type();
245+
240246
runtime::Options eng_options;
241247
eng_options.model_path(options_.model_path())
248+
.rec_model_type(std::make_optional(rec_model_type))
242249
.devices(devices)
243250
.backend(options_.backend())
244251
.block_size(options_.block_size())

xllm/core/distributed_runtime/rec_master.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ limitations under the License.
2525

2626
#include "common/macros.h"
2727
#include "common/metrics.h"
28+
#include "common/rec_model_utils.h"
2829
#include "common/types.h"
2930
#include "framework/request/mm_data.h"
3031
#include "models/model_registry.h"
@@ -42,12 +43,14 @@ namespace {
4243
constexpr int32_t kDefaultPlaceholderToken = 20152019;
4344

4445
RecType get_rec_type(const ModelArgs& model_args) {
45-
const auto& model_type = model_args.model_type();
46-
if (model_type == "onerec") {
47-
return RecType::kOneRec;
48-
}
49-
if (model_type == "qwen2" || model_type == "qwen3") {
50-
return RecType::kLlmRec;
46+
const auto kind = get_rec_model_kind(model_args.model_type());
47+
switch (kind) {
48+
case RecModelKind::kOneRec:
49+
return RecType::kOneRec;
50+
case RecModelKind::kLlmRec:
51+
return RecType::kLlmRec;
52+
case RecModelKind::kNone:
53+
return RecType::kNone;
5154
}
5255
return RecType::kNone;
5356
}

xllm/core/runtime/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ cc_library(
2222
dit_worker.h
2323
embed_worker_impl.h
2424
embed_vlm_worker_impl.h
25+
rec_worker_impl.h
26+
llmrec_worker_impl.h
27+
onerec_worker_impl.h
2528
worker_client.h
2629
xservice_client.h
2730
speculative_worker_impl.h
@@ -38,6 +41,9 @@ cc_library(
3841
dit_worker.cpp
3942
embed_worker_impl.cpp
4043
embed_vlm_worker_impl.cpp
44+
rec_worker_impl.cpp
45+
llmrec_worker_impl.cpp
46+
onerec_worker_impl.cpp
4147
worker_client.cpp
4248
xservice_client.cpp
4349
params_utils.cpp
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* Copyright 2025 The xLLM Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://github.com/jd-opensource/xllm/blob/main/LICENSE
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#include "llmrec_worker_impl.h"
17+
18+
#include <glog/logging.h>
19+
#include <torch/torch.h>
20+
21+
#include <algorithm>
22+
#include <vector>
23+
24+
#include "common/types.h"
25+
#include "core/layers/word_embedding.h"
26+
27+
namespace xllm {
28+
29+
LlmRecWorkerImpl::LlmRecWorkerImpl(const ParallelArgs& parallel_args,
30+
const torch::Device& device,
31+
const runtime::Options& options)
32+
: RecWorkerImpl(parallel_args, device, options) {}
33+
34+
void LlmRecWorkerImpl::prepare_work_before_execute(
35+
const ForwardInput& inputs,
36+
ForwardInput& processed_inputs) {
37+
WorkerImpl::prepare_work_before_execute(inputs, processed_inputs);
38+
39+
if (!inputs.input_params.mm_data.valid()) {
40+
return;
41+
}
42+
43+
torch::Tensor input_embedding;
44+
torch::Tensor input_tokens_tensor;
45+
torch::Tensor input_indices_tensor;
46+
47+
const auto& mm_data = inputs.input_params.mm_data;
48+
const auto& processed_mm_data = processed_inputs.input_params.mm_data;
49+
50+
if (auto res =
51+
processed_mm_data.get<torch::Tensor>(LLM_REC_INPUT_TOKENS)) {
52+
input_tokens_tensor = res.value();
53+
}
54+
55+
// input indices 需要在 Host 侧生成位置索引
56+
if (auto res = mm_data.get<torch::Tensor>(LLM_REC_INPUT_INDICES)) {
57+
input_indices_tensor = res.value();
58+
}
59+
60+
if (auto res =
61+
processed_mm_data.get<torch::Tensor>(LLM_REC_INPUT_EMBEDDING)) {
62+
input_embedding = res.value();
63+
}
64+
65+
if (input_embedding.defined()) {
66+
input_embedding = input_embedding.to(dtype());
67+
}
68+
69+
if (input_indices_tensor.defined()) {
70+
layer::WordEmbedding word_embedding = get_word_embedding();
71+
torch::Tensor input_tokens_embedding =
72+
word_embedding(input_tokens_tensor, 0);
73+
74+
if (input_embedding.defined()) {
75+
std::vector<int> input_indices(
76+
input_indices_tensor.data_ptr<int>(),
77+
input_indices_tensor.data_ptr<int>() + input_indices_tensor.numel());
78+
79+
processed_inputs.input_params.input_embedding =
80+
merge_embeddings_by_indices(
81+
input_tokens_embedding, input_embedding, input_indices);
82+
} else {
83+
processed_inputs.input_params.input_embedding = input_tokens_embedding;
84+
}
85+
} else if (input_embedding.defined()) {
86+
processed_inputs.input_params.input_embedding = input_embedding;
87+
}
88+
}
89+
90+
torch::Tensor LlmRecWorkerImpl::merge_embeddings_by_indices(
91+
const torch::Tensor& input_tokens_embedding,
92+
const torch::Tensor& input_embedding,
93+
const std::vector<int>& input_indices) {
94+
CHECK_EQ(input_embedding.dim(), 2);
95+
CHECK_EQ(input_tokens_embedding.dim(), 2);
96+
CHECK_EQ(input_tokens_embedding.size(1), input_embedding.size(1));
97+
CHECK_EQ(input_tokens_embedding.dtype(), input_embedding.dtype());
98+
CHECK_EQ(input_tokens_embedding.device(), input_embedding.device());
99+
100+
const int64_t total_rows =
101+
input_tokens_embedding.size(0) + input_embedding.size(0);
102+
const int64_t cols = input_embedding.size(1);
103+
104+
torch::Device device = input_embedding.device();
105+
torch::Tensor merged = torch::empty(
106+
{total_rows, cols}, torch::dtype(input_embedding.dtype()).device(device));
107+
108+
std::vector<int> input_embedding_indices;
109+
for (int i = 0; i < static_cast<int>(total_rows); ++i) {
110+
if (std::find(input_indices.begin(), input_indices.end(), i) ==
111+
input_indices.end()) {
112+
input_embedding_indices.push_back(i);
113+
}
114+
}
115+
116+
CHECK_EQ(input_embedding_indices.size(), input_embedding.size(0));
117+
118+
torch::Tensor input_embedding_indices_tensor =
119+
torch::tensor(input_embedding_indices, torch::kInt64).to(device);
120+
merged.index_put_({input_embedding_indices_tensor, torch::indexing::Ellipsis},
121+
input_embedding);
122+
123+
torch::Tensor input_indices_tensor =
124+
torch::tensor(input_indices, torch::kInt64).to(device);
125+
merged.index_put_({input_indices_tensor, torch::indexing::Ellipsis},
126+
input_tokens_embedding);
127+
128+
return merged;
129+
}
130+
131+
} // namespace xllm
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright 2025 The xLLM Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
https://github.com/jd-opensource/xllm/blob/main/LICENSE
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
16+
#pragma once
17+
18+
#include <torch/torch.h>
19+
20+
#include <vector>
21+
22+
#include "runtime/rec_worker_impl.h"
23+
24+
namespace xllm {
25+
26+
class LlmRecWorkerImpl final : public RecWorkerImpl {
27+
public:
28+
LlmRecWorkerImpl(const ParallelArgs& parallel_args,
29+
const torch::Device& device,
30+
const runtime::Options& options);
31+
32+
~LlmRecWorkerImpl() override = default;
33+
34+
void prepare_work_before_execute(const ForwardInput& inputs,
35+
ForwardInput& processed_inputs) override;
36+
37+
private:
38+
torch::Tensor merge_embeddings_by_indices(
39+
const torch::Tensor& input_tokens_embedding,
40+
const torch::Tensor& input_embedding,
41+
const std::vector<int>& input_indices);
42+
};
43+
44+
} // namespace xllm

0 commit comments

Comments
 (0)