| 321 | class FlashSrGraph { |
| 322 | public: |
| 323 | FlashSrGraph(const FlashSrWeights & weights, int64_t input_samples) |
| 324 | : weights_(weights), |
| 325 | input_samples_(input_samples) { |
| 326 | if (input_samples_ <= 0) { |
| 327 | throw std::runtime_error("FlashSR graph input length must be positive"); |
| 328 | } |
| 329 | ggml_init_params params{64ull * 1024ull * 1024ull, nullptr, true}; |
| 330 | ctx_.reset(ggml_init(params)); |
| 331 | if (ctx_ == nullptr) { |
| 332 | throw std::runtime_error("failed to initialize FlashSR GGML context"); |
| 333 | } |
| 334 | core::ModuleBuildContext build_ctx{ctx_.get(), "flashsr", weights.backend_type}; |
| 335 | auto input = core::make_tensor(build_ctx, GGML_TYPE_F32, core::TensorShape::from_dims({1, 1, input_samples_})); |
| 336 | input_ = input.tensor; |
| 337 | auto x = conv1d(build_ctx, input, weights.conv_pre, 3, 1); |
| 338 | x = modules::Interpolate1dModule({input_samples_ * 3, modules::Interpolate1dMode::Linear}).build(build_ctx, x); |
| 339 | auto xs = resblock(build_ctx, x, weights.resblock2, weights, 11); |
| 340 | auto xs0 = resblock(build_ctx, x, weights.resblock0, weights, 3); |
| 341 | xs = core::wrap_tensor(ggml_scale(build_ctx.ggml, modules::AddModule().build(build_ctx, xs, xs0).tensor, 0.5f), xs.shape, GGML_TYPE_F32); |
| 342 | xs = activation1d(build_ctx, xs, weights.activation_post, weights); |
| 343 | auto output = conv1d(build_ctx, xs, weights.conv_post, 3, 1); |
| 344 | output = core::wrap_tensor(ggml_tanh(build_ctx.ggml, output.tensor), output.shape, GGML_TYPE_F32); |
| 345 | output_ = output.tensor; |
| 346 | ggml_set_output(output_); |
| 347 | graph_ = ggml_new_graph_custom(ctx_.get(), 65536, false); |
| 348 | ggml_build_forward_expand(graph_, output_); |
| 349 | gallocr_ = ggml_gallocr_new(ggml_backend_get_default_buffer_type(weights.backend.get())); |
| 350 | if (gallocr_ == nullptr || |
| 351 | !ggml_gallocr_reserve(gallocr_, graph_) || |
| 352 | !ggml_gallocr_alloc_graph(gallocr_, graph_)) { |
| 353 | throw std::runtime_error("failed to allocate FlashSR GGML graph"); |
| 354 | } |
| 355 | if (core::uses_host_graph_plan(weights.backend.get())) { |
| 356 | plan_ = core::create_backend_graph_plan_if_host(weights.backend.get(), graph_); |
| 357 | if (plan_ == nullptr) { |
| 358 | throw std::runtime_error("failed to create FlashSR graph plan"); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | ~FlashSrGraph() { |
| 364 | core::release_backend_graph_resources(weights_.backend.get(), graph_); |
nothing calls this directly
no test coverage detected