@@ -696,6 +696,7 @@ struct Vevo2FMGraph {
696696 }
697697
698698 ~Vevo2FMGraph () {
699+ engine::core::release_backend_graph_resources (backend, graph, true );
699700 if (gallocr != nullptr ) {
700701 ggml_gallocr_free (gallocr);
701702 gallocr = nullptr ;
@@ -757,39 +758,45 @@ struct Vevo2FMStepGraph {
757758 if (config.hidden_size % config.num_heads != 0 ) {
758759 throw std::runtime_error (" Vevo2 FM hidden_size must be divisible by num_heads" );
759760 }
761+ ggml_init_params input_params{ggml_tensor_overhead () * 64 , nullptr , true };
762+ input_ctx.reset (ggml_init (input_params));
763+ if (input_ctx == nullptr ) {
764+ throw std::runtime_error (" failed to initialize Vevo2 FM step input context" );
765+ }
760766 ggml_init_params params{graph_context_bytes, nullptr , true };
761767 ctx.reset (ggml_init (params));
762768 if (ctx == nullptr ) {
763769 throw std::runtime_error (" failed to initialize Vevo2 FM step graph context" );
764770 }
765771
772+ engine::core::ModuleBuildContext input_build_ctx{input_ctx.get (), " vevo2.fm.step.input" , backend_type};
766773 engine::core::ModuleBuildContext build_ctx{ctx.get (), " vevo2.fm.step" , backend_type};
767774 prompt_input = engine::core::make_tensor (
768- build_ctx ,
775+ input_build_ctx ,
769776 GGML_TYPE_F32 ,
770777 engine::core::TensorShape::from_dims ({1 , prompt_frames, config.mel_dim })).tensor ;
771778 xt_input = engine::core::make_tensor (
772- build_ctx ,
779+ input_build_ctx ,
773780 GGML_TYPE_F32 ,
774781 engine::core::TensorShape::from_dims ({1 , target_frames, config.mel_dim })).tensor ;
775782 cond_input = engine::core::make_tensor (
776- build_ctx ,
783+ input_build_ctx ,
777784 GGML_TYPE_F32 ,
778785 engine::core::TensorShape::from_dims ({1 , cond_frames, config.hidden_size })).tensor ;
779786 uncond_cond_input = engine::core::make_tensor (
780- build_ctx ,
787+ input_build_ctx ,
781788 GGML_TYPE_F32 ,
782789 engine::core::TensorShape::from_dims ({1 , target_frames, config.hidden_size })).tensor ;
783790 timestep_input = engine::core::make_tensor (
784- build_ctx ,
791+ input_build_ctx ,
785792 GGML_TYPE_F32 ,
786793 engine::core::TensorShape::from_dims ({1 , config.hidden_size })).tensor ;
787794 cond_position_input = engine::core::make_tensor (
788- build_ctx ,
795+ input_build_ctx ,
789796 GGML_TYPE_I32 ,
790797 engine::core::TensorShape::from_dims ({cond_frames})).tensor ;
791798 uncond_position_input = engine::core::make_tensor (
792- build_ctx ,
799+ input_build_ctx ,
793800 GGML_TYPE_I32 ,
794801 engine::core::TensorShape::from_dims ({target_frames})).tensor ;
795802 ggml_set_input (prompt_input);
@@ -878,8 +885,11 @@ struct Vevo2FMStepGraph {
878885
879886 graph = ggml_new_graph_custom (ctx.get (), 524288 , false );
880887 ggml_build_forward_expand (graph, output);
881- buffer = ggml_backend_alloc_ctx_tensors (ctx.get (), backend);
882- if (buffer == nullptr ) {
888+ input_buffer = ggml_backend_alloc_ctx_tensors (input_ctx.get (), backend);
889+ gallocr = ggml_gallocr_new (ggml_backend_get_default_buffer_type (backend));
890+ if (input_buffer == nullptr || gallocr == nullptr ||
891+ !ggml_gallocr_reserve (gallocr, graph) ||
892+ !ggml_gallocr_alloc_graph (gallocr, graph)) {
883893 throw std::runtime_error (" failed to allocate Vevo2 FM step graph" );
884894 }
885895
@@ -904,9 +914,14 @@ struct Vevo2FMStepGraph {
904914 }
905915
906916 ~Vevo2FMStepGraph () {
907- if (buffer != nullptr ) {
908- ggml_backend_buffer_free (buffer);
909- buffer = nullptr ;
917+ engine::core::release_backend_graph_resources (backend, graph, true );
918+ if (gallocr != nullptr ) {
919+ ggml_gallocr_free (gallocr);
920+ gallocr = nullptr ;
921+ }
922+ if (input_buffer != nullptr ) {
923+ ggml_backend_buffer_free (input_buffer);
924+ input_buffer = nullptr ;
910925 }
911926 }
912927
@@ -966,6 +981,7 @@ struct Vevo2FMStepGraph {
966981 int64_t cond_frames = 0 ;
967982 int64_t prompt_frames = 0 ;
968983 int64_t target_frames = 0 ;
984+ std::unique_ptr<ggml_context, GgmlContextDeleter> input_ctx;
969985 std::unique_ptr<ggml_context, GgmlContextDeleter> ctx;
970986 ggml_tensor * prompt_input = nullptr ;
971987 ggml_tensor * xt_input = nullptr ;
@@ -976,7 +992,8 @@ struct Vevo2FMStepGraph {
976992 ggml_tensor * uncond_position_input = nullptr ;
977993 ggml_tensor * output = nullptr ;
978994 ggml_cgraph * graph = nullptr ;
979- ggml_backend_buffer_t buffer = nullptr ;
995+ ggml_backend_buffer_t input_buffer = nullptr ;
996+ ggml_gallocr_t gallocr = nullptr ;
980997};
981998
982999Vevo2FlowMatchingRuntime::Vevo2FlowMatchingRuntime (
@@ -1064,6 +1081,8 @@ Vevo2MelSequence Vevo2FlowMatchingRuntime::generate_mel(
10641081 const auto cond_run_start = Clock::now ();
10651082 const auto diffusion_cond = graph_->run_conditioning (diffusion_tokens, config_);
10661083 const double cond_run_ms = engine::debug::elapsed_ms (cond_run_start);
1084+ graph_.reset ();
1085+ engine::core::trim_backend_pools (execution_context_.backend ());
10671086 const int64_t cond_frames = static_cast <int64_t >(diffusion_cond.size ()) / config_.hidden_size ;
10681087 if (cond_frames * config_.hidden_size != static_cast <int64_t >(diffusion_cond.size ())) {
10691088 throw std::runtime_error (" Vevo2 FM conditioning output shape mismatch" );
@@ -1123,6 +1142,8 @@ Vevo2MelSequence Vevo2FlowMatchingRuntime::generate_mel(
11231142 const auto read_start = Clock::now ();
11241143 out.values = step_graph_->read_output (config_);
11251144 const double final_read_ms = engine::debug::elapsed_ms (read_start);
1145+ step_graph_.reset ();
1146+ engine::core::trim_backend_pools (execution_context_.backend ());
11261147 engine::debug::timing_log_scalar (" vevo2.fm.timbre_mel_ms" , timbre_mel_ms);
11271148 engine::debug::timing_log_scalar (" vevo2.fm.cond.graph.build_ms" , cond_graph_build_ms);
11281149 engine::debug::timing_log_scalar (" vevo2.fm.cond_run_ms" , cond_run_ms);
0 commit comments