inference.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. #include <memory>
  5. #include <functional>
  6. // Forward declaration
  7. class BSRoformer;
  8. // Forward declaration
  9. namespace ggml { struct context; struct cgraph; }
  10. class Inference {
  11. public:
  12. using CancelCallback = std::function<bool()>;
  13. Inference(const std::string& model_path);
  14. ~Inference();
  15. // Process a full audio track (interleaved stereo float32)
  16. // Uses overlap-add chunking to handle long files
  17. // Process a full audio track (interleaved stereo float32)
  18. // Returns a vector of stems, where each stem is an interleaved stereo float vector
  19. std::vector<std::vector<float>> Process(const std::vector<float>& input_audio,
  20. int chunk_size = 352800,
  21. int num_overlap = 2,
  22. std::function<void(float)> progress_callback = nullptr,
  23. CancelCallback cancel_callback = nullptr);
  24. // Low-level chunk processing (public for testing)
  25. std::vector<std::vector<float>> ProcessChunk(const std::vector<float>& chunk_audio);
  26. // Get model's recommended inference defaults
  27. int GetDefaultChunkSize() const;
  28. int GetDefaultNumOverlap() const;
  29. int GetSampleRate() const;
  30. // Static helper for Overlap-Add logic (matches Python exactly)
  31. // model_func: input [samples], output [stems][samples] (interleaved stereo)
  32. using ModelCallback = std::function<std::vector<std::vector<float>>(const std::vector<float>&)>;
  33. static std::vector<std::vector<float>> ProcessOverlapAdd(const std::vector<float>& input_audio,
  34. int chunk_size,
  35. int num_overlap,
  36. ModelCallback model_func,
  37. std::function<void(float)> progress_callback = nullptr,
  38. CancelCallback cancel_callback = nullptr);
  39. private:
  40. // Pipelined Overlap-Add
  41. std::vector<std::vector<float>> ProcessOverlapAddPipelined(const std::vector<float>& input_audio,
  42. int chunk_size,
  43. int num_overlap,
  44. std::function<void(float)> progress_callback,
  45. CancelCallback cancel_callback);
  46. private:
  47. std::unique_ptr<BSRoformer> model_;
  48. // Persistent Graph State
  49. struct ggml_context* ctx_ = nullptr;
  50. struct ggml_cgraph* gf_ = nullptr;
  51. struct ggml_gallocr* allocr_ = nullptr;
  52. // Cached Input Tensors (owned by ctx_)
  53. struct ggml_tensor* input_tensor_ = nullptr;
  54. struct ggml_tensor* pos_time_ = nullptr;
  55. struct ggml_tensor* pos_freq_ = nullptr;
  56. struct ggml_tensor* mask_out_tensor_ = nullptr;
  57. // Cached Host Data (to avoid reallocation)
  58. std::vector<int32_t> pos_time_data_;
  59. std::vector<int32_t> pos_freq_data_;
  60. // Current config state
  61. int cached_n_frames_ = -1;
  62. // Pipelined State Data
  63. struct ChunkState {
  64. int id = -1;
  65. std::vector<float> input_audio; // Original chunk audio
  66. std::vector<float> stft_flattened; // [Prepared Input for GPU]
  67. std::vector<std::vector<float>> stft_outputs; // Kept for reconstruction
  68. int n_frames = 0;
  69. std::vector<float> mask_output; // Output from GPU
  70. std::vector<std::vector<float>> final_audio; // Result after ISTFT [stems][samples]
  71. };
  72. // Helper to ensure graph is built for specific n_frames
  73. bool EnsureGraph(int n_frames);
  74. void ComputeSTFT(const std::vector<float>& input_audio,
  75. std::vector<std::vector<float>>& stft_outputs,
  76. int& n_frames);
  77. void PrepareModelInput(const std::vector<std::vector<float>>& stft_outputs,
  78. int n_frames,
  79. std::vector<float>& model_input_rearranged);
  80. void PostProcessAndISTFT(const std::vector<float>& mask_output,
  81. const std::vector<std::vector<float>>& stft_outputs,
  82. int n_frames,
  83. std::vector<std::vector<float>>& output_audio);
  84. // Pipeline Steps
  85. std::shared_ptr<ChunkState> PreProcessChunk(const std::vector<float>& chunk_audio, int id);
  86. void RunInference(std::shared_ptr<ChunkState> state);
  87. void PostProcessChunk(std::shared_ptr<ChunkState> state);
  88. };