| 707 | core::TensorShape::from_dims({1, cache_steps + 1, config.num_key_value_heads, dim}))); |
| 708 | auto out = decoder_layer_with_static_cache_tail( |
| 709 | build_ctx, |
| 710 | graph, |
| 711 | x, |
| 712 | position_values, |
| 713 | layer, |
| 714 | config, |
| 715 | cache_keys.back(), |
| 716 | cache_values.back(), |
| 717 | mask); |
| 718 | x = out.output; |
| 719 | key_sources.push_back(ggml_view_1d(ctx.get(), out.key.tensor, config.num_key_value_heads * dim, 0)); |
| 720 | value_sources.push_back(ggml_view_1d(ctx.get(), out.value.tensor, config.num_key_value_heads * dim, 0)); |
| 721 | } |
| 722 | step_cache = runtime::TransformerKVCache( |
| 723 | cache_steps + 1, |
| 724 | config.num_key_value_heads * dim, |
| 725 | std::move(cache_keys), |
| 726 | std::move(cache_values)); |
| 727 | build_transfer_views(config.num_key_value_heads * dim); |
| 728 | x = modules::RMSNormModule({config.hidden_size, config.rms_norm_eps, true, false}) |
| 729 | .build(build_ctx, x, this->weights->norm); |
| 730 | logits = modules::LinearModule({config.hidden_size, config.vocab_size, false}) |
| 731 | .build(build_ctx, x, {this->weights->lm_head, std::nullopt}) |
| 732 | .tensor; |
| 733 | ggml_set_output(logits); |
| 734 | ggml_build_forward_expand(graph, logits); |
| 735 | buffer = ggml_backend_alloc_ctx_tensors(ctx.get(), backend); |
| 736 | if (buffer == nullptr) { |
| 737 | throw std::runtime_error("failed to allocate Vevo2 AR decode graph"); |
| 738 | } |
| 739 | attention_mask_values.assign(static_cast<size_t>(cache_steps + 1), ggml_fp32_to_fp16(-INFINITY)); |
| 740 | } |
| 741 | |
| 742 | ~Vevo2ARDecodeGraph() { |
| 743 | engine::core::release_backend_graph_resources(backend, graph); |
| 744 | if (buffer != nullptr) { |
| 745 | ggml_backend_buffer_free(buffer); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | bool can_run(const Vevo2ARWeights & other_weights, int64_t required_steps) const noexcept { |
| 750 | return weights.get() == &other_weights && cache_steps >= required_steps; |
| 751 | } |
| 752 | |
| 753 | void import_state(const runtime::TransformerKVState & state) { |
| 754 | step_cache.import_state(state); |
| 755 | } |
| 756 | |
| 757 | std::vector<float> run_step(int32_t token, const Vevo2ARConfig & config) { |
| 758 | if (step_cache.valid_steps() >= cache_steps) { |
| 759 | throw std::runtime_error("Vevo2 AR decode cache exhausted"); |
| 760 | } |
| 761 | ggml_backend_tensor_set(token_id, &token, 0, sizeof(int32_t)); |
| 762 | const int32_t position = static_cast<int32_t>(step_cache.current_end()); |
| 763 | ggml_backend_tensor_set(positions, &position, 0, sizeof(int32_t)); |
| 764 | const auto masked = ggml_fp32_to_fp16(-INFINITY); |
| 765 | const auto visible = ggml_fp32_to_fp16(0.0F); |
| 766 | std::fill(attention_mask_values.begin(), attention_mask_values.end(), masked); |
nothing calls this directly
no test coverage detected