| 1996 | return step_cache_.export_state(); |
| 1997 | } |
| 1998 | |
| 1999 | void run_step_into( |
| 2000 | int32_t token, |
| 2001 | std::vector<float> & logits, |
| 2002 | const std::vector<int32_t> * visible_prefix = nullptr) { |
| 2003 | const auto & config = runtime_->assets().config.planner; |
| 2004 | if (step_cache_.valid_steps() >= cache_steps_) { |
| 2005 | throw std::runtime_error("ACE-Step planner decode cache exhausted"); |
| 2006 | } |
| 2007 | ggml_backend_tensor_set(token_id_, &token, 0, sizeof(int32_t)); |
| 2008 | const int32_t position = static_cast<int32_t>(step_cache_.current_end()); |
| 2009 | if (visible_prefix != nullptr && |
| 2010 | static_cast<int64_t>(visible_prefix->size()) != step_cache_.current_end()) { |
| 2011 | throw std::runtime_error("ACE-Step planner decode visible-prefix length mismatch"); |
| 2012 | } |
| 2013 | ggml_backend_tensor_set(positions_, &position, 0, sizeof(int32_t)); |
| 2014 | const auto masked = ggml_fp32_to_fp16(-std::numeric_limits<float>::infinity()); |
| 2015 | const auto visible = ggml_fp32_to_fp16(0.0F); |
| 2016 | std::fill(attention_mask_values_.begin(), attention_mask_values_.end(), masked); |
| 2017 | if (visible_prefix != nullptr) { |
| 2018 | for (int64_t i = 0; i < step_cache_.valid_steps(); ++i) { |
| 2019 | if ((*visible_prefix)[static_cast<size_t>(i)] != 0) { |
| 2020 | attention_mask_values_[static_cast<size_t>(i)] = visible; |
| 2021 | } |
| 2022 | } |
| 2023 | } else { |
| 2024 | for (int64_t i = 0; i < step_cache_.valid_steps(); ++i) { |
| 2025 | attention_mask_values_[static_cast<size_t>(i)] = visible; |
| 2026 | } |
| 2027 | } |
| 2028 | attention_mask_values_[static_cast<size_t>(cache_steps_)] = visible; |
| 2029 | ggml_backend_tensor_set( |
| 2030 | attention_mask_, |
| 2031 | attention_mask_values_.data(), |
| 2032 | 0, |
| 2033 | attention_mask_values_.size() * sizeof(ggml_fp16_t)); |
| 2034 | core::set_backend_threads(runtime_->backend(), runtime_->threads()); |
| 2035 | const ggml_status status = engine::core::compute_backend_graph(runtime_->backend(), graph_); |
| 2036 | ggml_backend_synchronize(runtime_->backend()); |
| 2037 | if (status != GGML_STATUS_SUCCESS) { |
| 2038 | throw std::runtime_error("ACE-Step planner decode graph compute failed"); |
| 2039 | } |
| 2040 | logits.resize(static_cast<size_t>(config.vocab_size)); |
| 2041 | ggml_backend_tensor_get(logits_, logits.data(), 0, logits.size() * sizeof(float)); |
| 2042 | const size_t dst_slot = static_cast<size_t>(step_cache_.valid_steps()); |
| 2043 | for (size_t layer = 0; layer < key_sources_.size(); ++layer) { |
| 2044 | ggml_backend_tensor_copy(key_sources_[layer], key_destinations_[dst_slot][layer]); |
| 2045 | ggml_backend_tensor_copy(value_sources_[layer], value_destinations_[dst_slot][layer]); |
| 2046 | } |
| 2047 | step_cache_.advance_after_direct_append(1); |
| 2048 | } |
| 2049 | |
| 2050 | private: |
no test coverage detected