| 1754 | |
| 1755 | class PrefillGraph { |
| 1756 | public: |
| 1757 | PrefillGraph( |
| 1758 | std::shared_ptr<PlannerWeightsRuntime> runtime, |
| 1759 | int64_t prompt_steps, |
| 1760 | size_t graph_arena_bytes) |
| 1761 | : runtime_(std::move(runtime)), |
| 1762 | prompt_steps_(prompt_steps) { |
| 1763 | if (prompt_steps_ <= 0) { |
| 1764 | throw std::runtime_error("ACE-Step planner prefill requires positive prompt length"); |
| 1765 | } |
| 1766 | ggml_init_params params{graph_arena_bytes, nullptr, true}; |
| 1767 | ctx_.reset(ggml_init(params)); |
| 1768 | if (ctx_ == nullptr) { |
| 1769 | throw std::runtime_error("failed to initialize ACE-Step planner prefill graph context"); |
| 1770 | } |
| 1771 | const auto & config = runtime_->assets().config.planner; |
| 1772 | const auto & weights = runtime_->weights(); |
| 1773 | core::ModuleBuildContext ctx{ctx_.get(), "ace_step.planner.prefill", runtime_->backend_type()}; |
| 1774 | token_ids_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, prompt_steps_); |
| 1775 | auto token_ids = core::wrap_tensor(token_ids_, core::TensorShape::from_dims({prompt_steps_}), GGML_TYPE_I32); |
| 1776 | auto x = modules::EmbeddingModule({config.vocab_size, config.hidden_size}) |
| 1777 | .build(ctx, token_ids, weights.token_embedding); |
| 1778 | x = core::reshape_tensor(ctx, x, core::TensorShape::from_dims({1, prompt_steps_, config.hidden_size})); |
| 1779 | positions_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, prompt_steps_); |
| 1780 | auto positions = core::wrap_tensor(positions_, core::TensorShape::from_dims({prompt_steps_}), GGML_TYPE_I32); |
| 1781 | attention_mask_ = ggml_new_tensor_4d(ctx_.get(), GGML_TYPE_F16, prompt_steps_, prompt_steps_, 1, 1); |
| 1782 | auto attention_mask = core::wrap_tensor( |
| 1783 | attention_mask_, |
| 1784 | core::TensorShape::from_dims({1, 1, prompt_steps_, prompt_steps_}), |
| 1785 | GGML_TYPE_F16); |
| 1786 | query_mask_ = ggml_new_tensor_4d(ctx_.get(), GGML_TYPE_F32, 1, 1, prompt_steps_, 1); |
| 1787 | auto query_mask = core::wrap_tensor( |
| 1788 | query_mask_, |
| 1789 | core::TensorShape::from_dims({1, prompt_steps_, 1, 1}), |
| 1790 | GGML_TYPE_F32); |
| 1791 | for (size_t layer_index = 0; layer_index < weights.layers.layers.size(); ++layer_index) { |
| 1792 | const auto & layer = weights.layers.layers[layer_index]; |
| 1793 | auto out = planner_decoder_layer_batched( |
| 1794 | ctx, |
| 1795 | x, |
| 1796 | positions, |
| 1797 | layer, |
| 1798 | config, |
| 1799 | attention_mask, |
| 1800 | query_mask, |
| 1801 | GGML_TYPE_F32); |
| 1802 | x = out.output; |
| 1803 | keys_.push_back(out.key.tensor); |
| 1804 | values_.push_back(out.value.tensor); |
| 1805 | } |
| 1806 | x = modules::SliceModule({1, prompt_steps_ - 1, 1}).build(ctx, x); |
| 1807 | x = modules::RMSNormModule({config.hidden_size, config.rms_norm_eps, true, false}) |
| 1808 | .build(ctx, x, {weights.norm, std::nullopt}); |
| 1809 | auto logits = modules::LinearModule({config.hidden_size, config.vocab_size, false}) |
| 1810 | .build(ctx, x, {weights.lm_head, std::nullopt}); |
| 1811 | logits_ = logits.tensor; |
| 1812 | ggml_set_output(logits_); |
| 1813 | graph_ = ggml_new_graph_custom(ctx_.get(), 65536, false); |
nothing calls this directly
no test coverage detected