44
55namespace infinilm ::engine {
66
7- namespace {
8- bool same_shape_and_dtype (const infinicore::Tensor &lhs, const infinicore::Tensor &rhs) {
9- return lhs->shape () == rhs->shape () && lhs->dtype () == rhs->dtype ();
10- }
11-
12- bool is_mrope_decode_position_ids (const infinicore::Tensor &position_ids, size_t batch_size) {
13- const auto shape = position_ids->shape ();
14- return shape.size () == 2 && shape[0 ] == batch_size && shape[1 ] == 3 ;
15- }
16- } // namespace
17-
187PagedCompiler::PagedCompiler (const std::shared_ptr<InfinilmModel> &model, RankBarrier *barrier)
198 : GraphCompiler(model, barrier) {
209 for (size_t b = 1 ; b < 64 ; ++b) {
@@ -36,19 +25,14 @@ void PagedCompiler::compile() {
3625 size_t nblocks = dynamic_cast <const cache::PagedKVCacheConfig *>(model_->get_cache_config ())->num_blocks ();
3726 size_t max_batch_size = *std::max_element (decode_batch_sizes_.begin (), decode_batch_sizes_.end ());
3827 compiled_map_decode_.clear ();
39- compiled_map_decode_mrope_.clear ();
4028 block_tables_holder_ = infinicore::Tensor::empty (
4129 {nblocks * max_batch_size}, infinicore::DataType::I32 , infinicore::context::getDevice ());
4230 set_zeros (block_tables_holder_);
4331
44- auto make_decode_input = [&](size_t b, bool mrope_position_ids ) {
32+ auto make_decode_input = [&](size_t b) {
4533 InfinilmModel::Input input;
4634 input.input_ids = infinicore::Tensor::empty ({1 , b}, infinicore::DataType::I64 , infinicore::context::getDevice ());
47- if (mrope_position_ids) {
48- input.position_ids = infinicore::Tensor::empty ({b, 3 }, infinicore::DataType::I64 , infinicore::context::getDevice ());
49- } else {
50- input.position_ids = infinicore::Tensor::empty ({b}, infinicore::DataType::I64 , infinicore::context::getDevice ());
51- }
35+ input.position_ids = infinicore::Tensor::empty ({b}, infinicore::DataType::I64 , infinicore::context::getDevice ());
5236 input.total_sequence_lengths = infinicore::Tensor::empty ({b}, infinicore::DataType::I32 , infinicore::context::getDevice ());
5337 set_zeros (input.input_ids .value ());
5438 set_zeros (input.position_ids .value ());
@@ -82,7 +66,7 @@ void PagedCompiler::compile() {
8266
8367 {
8468 const size_t warmup_batch_size = std::min (max_batch_size, static_cast <size_t >(64 ));
85- auto input = make_decode_input (warmup_batch_size, false );
69+ auto input = make_decode_input (warmup_batch_size);
8670 model_->forward (input);
8771 infinicore::context::syncStream ();
8872 // Warmup runs the eager Marlin path and may leave per-layer lock
@@ -92,10 +76,8 @@ void PagedCompiler::compile() {
9276 infinicore::context::syncStream ();
9377 }
9478
95- auto capture_decode_graph = [&](size_t b,
96- bool mrope_position_ids,
97- std::unordered_map<size_t , CompiledResult> &compiled_map) {
98- auto input = make_decode_input (b, mrope_position_ids);
79+ for (size_t b : decode_batch_sizes_) {
80+ auto input = make_decode_input (b);
9981
10082 barrier_->wait ();
10183 (void )model_->forward (input);
@@ -114,15 +96,7 @@ void PagedCompiler::compile() {
11496 auto shared_output = std::shared_ptr<InfinilmModel::Output>(
11597 new InfinilmModel::Output{infinicore::graph::GraphTensor (output.logits )});
11698
117- compiled_map[b] = CompiledResult{std::move (input), std::make_tuple (graph, shared_output)};
118- };
119-
120- const bool compile_mrope_decode = model_->supports_mrope_position_ids ();
121- for (size_t b : decode_batch_sizes_) {
122- capture_decode_graph (b, false , compiled_map_decode_);
123- if (compile_mrope_decode) {
124- capture_decode_graph (b, true , compiled_map_decode_mrope_);
125- }
99+ compiled_map_decode_[b] = CompiledResult{std::move (input), std::make_tuple (graph, shared_output)};
126100 }
127101 }
128102}
@@ -136,23 +110,12 @@ PagedCompiler::Compiled PagedCompiler::get_compiled(const InfinilmModel::Input &
136110 if (batch_size != input.input_ids .value ()->size (1 )) {
137111 return {nullptr , nullptr };
138112 } else {
139- const bool mrope_position_ids = is_mrope_decode_position_ids (input.position_ids .value (), batch_size);
140- auto &compiled_map = mrope_position_ids ? compiled_map_decode_mrope_ : compiled_map_decode_;
141- auto result = compiled_map.find (batch_size);
142- if (result == compiled_map.end ()) {
113+ auto result = compiled_map_decode_.find (batch_size);
114+ if (result == compiled_map_decode_.end ()) {
143115 return {nullptr , nullptr };
144116 }
145117 auto &graph_input = result->second .input ;
146118
147- if (!same_shape_and_dtype (graph_input.input_ids .value (), input.input_ids .value ()) ||
148- !same_shape_and_dtype (graph_input.position_ids .value (), input.position_ids .value ()) ||
149- !same_shape_and_dtype (graph_input.total_sequence_lengths .value (), input.total_sequence_lengths .value ()) ||
150- !same_shape_and_dtype (graph_input.input_offsets .value (), input.input_offsets .value ()) ||
151- !same_shape_and_dtype (graph_input.cu_seqlens .value (), input.cu_seqlens .value ()) ||
152- !same_shape_and_dtype (graph_input.slot_mapping .value (), input.slot_mapping .value ())) {
153- return {nullptr , nullptr };
154- }
155-
156119 graph_input.input_ids .value ()->copy_from (input.input_ids .value ());
157120 graph_input.position_ids .value ()->copy_from (input.position_ids .value ());
158121 graph_input.total_sequence_lengths .value ()->copy_from (input.total_sequence_lengths .value ());
@@ -169,9 +132,6 @@ PagedCompiler::Compiled PagedCompiler::get_compiled(const InfinilmModel::Input &
169132 // runtime logical region. Avoid clearing the full preallocated
170133 // holder on every decode token.
171134 auto &graph_block_tables = graph_input.block_tables .value ();
172- if (graph_block_tables->dtype () != input.block_tables .value ()->dtype ()) {
173- return {nullptr , nullptr };
174- }
175135 set_minus_one_device_async (graph_block_tables);
176136 graph_block_tables->narrow ({{1 , 0 , block_per_req}})->copy_from (input.block_tables .value ());
177137 graph_input.slot_mapping .value ()->copy_from (input.slot_mapping .value ());
0 commit comments