| 723 | class DecodeGraph { |
| 724 | public: |
| 725 | DecodeGraph(std::shared_ptr<MioTTSCausalLMWeightsRuntime> runtime, int64_t cache_steps, size_t graph_arena_bytes) |
| 726 | : runtime_(std::move(runtime)), |
| 727 | cache_steps_(cache_steps) { |
| 728 | if (cache_steps_ <= 0) { |
| 729 | throw std::runtime_error("MioTTS lm decode requires positive cache length"); |
| 730 | } |
| 731 | const auto build_start = Clock::now(); |
| 732 | ggml_init_params params{graph_arena_bytes, nullptr, true}; |
| 733 | ctx_.reset(ggml_init(params)); |
| 734 | if (ctx_ == nullptr) { |
| 735 | throw std::runtime_error("failed to initialize MioTTS lm decode graph context"); |
| 736 | } |
| 737 | const auto & config = runtime_->assets().config; |
| 738 | const auto & weights = runtime_->weights(); |
| 739 | const int64_t dim = head_dim(config); |
| 740 | core::ModuleBuildContext ctx{ctx_.get(), "miotts.lm.decode", runtime_->backend_type()}; |
| 741 | token_id_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, 1); |
| 742 | auto token_id = core::wrap_tensor(token_id_, core::TensorShape::from_dims({1}), GGML_TYPE_I32); |
| 743 | auto x = modules::EmbeddingModule({config.vocab_size, config.hidden_size}) |
| 744 | .build(ctx, token_id, weights.token_embedding); |
| 745 | x = core::reshape_tensor(ctx, x, core::TensorShape::from_dims({1, 1, config.hidden_size})); |
| 746 | positions_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, 1); |
| 747 | auto positions = core::wrap_tensor(positions_, core::TensorShape::from_dims({1}), GGML_TYPE_I32); |
| 748 | cache_slot_ = ggml_new_tensor_1d(ctx_.get(), GGML_TYPE_I32, 1); |
| 749 | auto cache_slot = core::wrap_tensor(cache_slot_, core::TensorShape::from_dims({1}), GGML_TYPE_I32); |
| 750 | attention_mask_ = ggml_new_tensor_4d(ctx_.get(), GGML_TYPE_F16, cache_steps_, 1, 1, 1); |
| 751 | auto attention_mask = core::wrap_tensor( |
| 752 | attention_mask_, |
| 753 | core::TensorShape::from_dims({1, 1, 1, cache_steps_}), |
| 754 | GGML_TYPE_F16); |
| 755 | graph_ = ggml_new_graph_custom(ctx_.get(), 65536, false); |
| 756 | std::vector<core::TensorValue> cache_keys; |
| 757 | std::vector<core::TensorValue> cache_values; |
| 758 | for (const auto & layer : weights.layers) { |
| 759 | cache_keys.push_back(core::make_tensor( |
| 760 | ctx, |
| 761 | GGML_TYPE_F32, |
| 762 | core::TensorShape::from_dims({1, cache_steps_, config.num_key_value_heads, dim}))); |
| 763 | cache_values.push_back(core::make_tensor( |
| 764 | ctx, |
| 765 | GGML_TYPE_F32, |
| 766 | core::TensorShape::from_dims({1, cache_steps_, config.num_key_value_heads, dim}))); |
| 767 | auto out = decoder_layer_with_static_cache( |
| 768 | ctx, |
| 769 | x, |
| 770 | positions, |
| 771 | layer, |
| 772 | config, |
| 773 | cache_keys.back(), |
| 774 | cache_values.back(), |
| 775 | cache_slot, |
| 776 | attention_mask); |
| 777 | x = out.output; |
| 778 | } |
| 779 | step_cache_ = runtime::TransformerKVCache( |
| 780 | cache_steps_, |
| 781 | config.num_key_value_heads * dim, |
| 782 | std::move(cache_keys), |
nothing calls this directly
no test coverage detected