inference.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. int GetNumStems() const;
  31. // Static helper for Overlap-Add logic (matches Python exactly)
  32. // model_func: input [samples], output [stems][samples] (interleaved stereo)
  33. using ModelCallback = std::function<std::vector<std::vector<float>>(const std::vector<float>&)>;
  34. static std::vector<std::vector<float>> ProcessOverlapAdd(const std::vector<float>& input_audio,
  35. int chunk_size,
  36. int num_overlap,
  37. ModelCallback model_func,
  38. std::function<void(float)> progress_callback = nullptr,
  39. CancelCallback cancel_callback = nullptr);
  40. private:
  41. // Pipelined Overlap-Add
  42. std::vector<std::vector<float>> ProcessOverlapAddPipelined(const std::vector<float>& input_audio,
  43. int chunk_size,
  44. int num_overlap,
  45. std::function<void(float)> progress_callback,
  46. CancelCallback cancel_callback);
  47. private:
  48. std::unique_ptr<BSRoformer> model_;
  49. // Persistent Graph State
  50. struct ggml_context* ctx_ = nullptr;
  51. struct ggml_cgraph* gf_ = nullptr;
  52. struct ggml_gallocr* allocr_ = nullptr;
  53. // Cached Input Tensors (owned by ctx_)
  54. struct ggml_tensor* input_tensor_ = nullptr;
  55. struct ggml_tensor* pos_time_ = nullptr;
  56. struct ggml_tensor* pos_freq_ = nullptr;
  57. struct ggml_tensor* mask_out_tensor_ = nullptr;
  58. // Cached Host Data (to avoid reallocation)
  59. std::vector<int32_t> pos_time_data_;
  60. std::vector<int32_t> pos_freq_data_;
  61. // Current config state
  62. int cached_n_frames_ = -1;
  63. // Pipelined State Data
  64. struct ChunkState {
  65. int id = -1;
  66. std::vector<float> input_audio; // Original chunk audio
  67. std::vector<float> stft_flattened; // [Prepared Input for GPU]
  68. std::vector<std::vector<float>> stft_outputs; // Kept for reconstruction
  69. int n_frames = 0;
  70. std::vector<float> mask_output; // Output from GPU
  71. std::vector<std::vector<float>> final_audio; // Result after ISTFT [stems][samples]
  72. };
  73. // Helper to ensure graph is built for specific n_frames
  74. bool EnsureGraph(int n_frames);
  75. void ComputeSTFT(const std::vector<float>& input_audio,
  76. std::vector<std::vector<float>>& stft_outputs,
  77. int& n_frames);
  78. void PrepareModelInput(const std::vector<std::vector<float>>& stft_outputs,
  79. int n_frames,
  80. std::vector<float>& model_input_rearranged);
  81. void PostProcessAndISTFT(const std::vector<float>& mask_output,
  82. const std::vector<std::vector<float>>& stft_outputs,
  83. int n_frames,
  84. std::vector<std::vector<float>>& output_audio);
  85. // Pipeline Steps
  86. std::shared_ptr<ChunkState> PreProcessChunk(const std::vector<float>& chunk_audio, int id);
  87. void RunInference(std::shared_ptr<ChunkState> state);
  88. void PostProcessChunk(std::shared_ptr<ChunkState> state);
  89. };