@@ -12,7 +12,6 @@ namespace turbomind {
1212struct GuidedDecoding ::Data {
1313 Tensor_<int32_t > bitmask;
1414 bool active{};
15- bool needs_apply{};
1615
1716 std::vector<std::shared_ptr<xgrammar::GrammarMatcher>> matchers;
1817};
@@ -64,14 +63,14 @@ void GuidedDecoding::FillMask(int phase, TensorMap& env)
6463
6564 std::vector<xgrammar::GrammarMatcher> active_matchers;
6665 std::vector<int32_t > active_indices;
67-
68- d. needs_apply = false ;
66+ active_matchers. reserve (d. matchers . size ());
67+ active_indices. reserve (d. matchers . size ()) ;
6968
7069 if (tp_group_->rank () == 0 ) {
7170 for (size_t i = 0 ; i < d.matchers .size (); ++i) {
7271 if (const auto & m = d.matchers [i]; m && !m->IsTerminated ()) {
73- active_matchers.push_back (*m);
74- active_indices.push_back (static_cast <int32_t >(i));
72+ active_matchers.emplace_back (*m);
73+ active_indices.emplace_back (static_cast <int32_t >(i));
7574 }
7675 else {
7776 std::fill_n (bitmask_buf_.data () + i * bitmask_buf_.stride (0 ),
@@ -82,15 +81,14 @@ void GuidedDecoding::FillMask(int phase, TensorMap& env)
8281
8382 if (!active_matchers.empty ()) {
8483 batch_matcher_.BatchFillNextTokenBitmask (&active_matchers, &dlbitmask, active_indices);
85- d.needs_apply = true ;
8684 }
8785 }
8886 }
8987}
9088
9189void GuidedDecoding::ApplyMask (int phase, TensorMap& env)
9290{
93- if (auto & d = *data_.at (phase); d.active && d. needs_apply ) {
91+ if (auto & d = *data_.at (phase); d.active ) {
9492 const ssize_t numel = d.matchers .size () * bitmask_buf_.stride (0 );
9593 if (tp_group_->n_ranks () > 1 ) {
9694 // bcast the data instead of `bitmask_buf` instance (which may avoid copying the data)
@@ -132,11 +130,13 @@ void GuidedDecoding::FinishUpdate(int phase)
132130 // Collect active matchers and their token IDs for batch AcceptToken
133131 std::vector<xgrammar::GrammarMatcher> active_matchers;
134132 std::vector<int32_t > active_token_ids;
133+ active_matchers.reserve (d.matchers .size ());
134+ active_token_ids.reserve (d.matchers .size ());
135135
136136 for (size_t i = 0 ; i < d.matchers .size (); ++i) {
137137 if (const auto & m = d.matchers [i]; m && !m->IsTerminated ()) {
138- active_matchers.push_back (*m); // copy: refcount++
139- active_token_ids.push_back (output_ids_buf_[i]);
138+ active_matchers.emplace_back (*m);
139+ active_token_ids.emplace_back (output_ids_buf_[i]);
140140 }
141141 }
142142
0 commit comments