| 448 | public: |
| 449 | FixedShapeGraph(ggml_backend_t backend, int compute_threads) |
| 450 | : backend_(backend), |
| 451 | compute_threads_(compute_threads) { |
| 452 | } |
| 453 | |
| 454 | virtual ~FixedShapeGraph() { |
| 455 | if (graph_ != nullptr) { |
| 456 | engine::core::release_backend_graph_resources(backend_, graph_); |
| 457 | } |
| 458 | if (gallocr_ != nullptr) { |
| 459 | ggml_gallocr_free(gallocr_); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | const std::vector<float> & run(const std::vector<float> & input_values) { |
| 464 | if (static_cast<int64_t>(input_values.size()) != input_shape_.num_elements()) { |
| 465 | throw std::runtime_error("roformer graph input size mismatch"); |
| 466 | } |
| 467 | const auto upload_start = Clock::now(); |
| 468 | ggml_backend_tensor_set(input_, input_values.data(), 0, input_values.size() * sizeof(float)); |
| 469 | engine::debug::timing_log_scalar("mel_band_roformer.graph.input_upload_ms", engine::debug::elapsed_ms(upload_start)); |
| 470 | core::set_backend_threads(backend_, compute_threads_); |
| 471 | const auto compute_start = Clock::now(); |
| 472 | const ggml_status status = engine::core::compute_backend_graph(backend_, graph_); |
| 473 | ggml_backend_synchronize(backend_); |
| 474 | engine::debug::timing_log_scalar("mel_band_roformer.graph.compute_ms", engine::debug::elapsed_ms(compute_start)); |
| 475 | if (status != GGML_STATUS_SUCCESS) { |
| 476 | throw std::runtime_error("roformer graph compute failed"); |
| 477 | } |
| 478 | const auto read_start = Clock::now(); |
| 479 | output_host_.resize(static_cast<size_t>(output_shape_.num_elements())); |
| 480 | ggml_backend_tensor_get(output_, output_host_.data(), 0, output_host_.size() * sizeof(float)); |
| 481 | engine::debug::timing_log_scalar("mel_band_roformer.graph.output_read_ms", engine::debug::elapsed_ms(read_start)); |
| 482 | return output_host_; |
| 483 | } |
| 484 | |
| 485 | protected: |
| 486 | void init_context(size_t arena_bytes) { |
| 487 | ggml_init_params params{arena_bytes, nullptr, true}; |
| 488 | ctx_.reset(ggml_init(params)); |
| 489 | if (ctx_ == nullptr) { |
| 490 | throw std::runtime_error("failed to initialize roformer ggml context"); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | core::ModuleBuildContext make_build_context(core::ExecutionContext & execution_context, const char * name) { |
| 495 | return core::ModuleBuildContext{ |
| 496 | ctx_.get(), |
| 497 | name, |
| 498 | execution_context.backend_type(), |
| 499 | }; |
| 500 | } |
| 501 | |
| 502 | void finalize_graph(size_t max_nodes) { |
| 503 | graph_ = ggml_new_graph_custom(ctx_.get(), max_nodes, false); |
| 504 | ggml_build_forward_expand(graph_, output_); |
| 505 | gallocr_ = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend_)); |
| 506 | if (gallocr_ == nullptr || !ggml_gallocr_alloc_graph(gallocr_, graph_)) { |
| 507 | throw std::runtime_error("failed to allocate roformer graph buffer"); |
nothing calls this directly
no test coverage detected