|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "engine/framework/assets/tensor_source.h" |
| 4 | +#include "engine/framework/core/backend.h" |
| 5 | +#include "engine/framework/runtime/session.h" |
| 6 | + |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <memory> |
| 10 | +#include <string> |
| 11 | +#include <vector> |
| 12 | + |
| 13 | +namespace engine::codecs { |
| 14 | + |
| 15 | +struct FishDacCodes { |
| 16 | + std::vector<int32_t> codes; |
| 17 | + int64_t codebooks = 0; |
| 18 | + int64_t frames = 0; |
| 19 | +}; |
| 20 | + |
| 21 | +struct FishDacLatents { |
| 22 | + std::vector<float> values; |
| 23 | + int64_t frames = 0; |
| 24 | + int64_t channels = 0; |
| 25 | +}; |
| 26 | + |
| 27 | +struct FishDacCodecConfig { |
| 28 | + int sample_rate = 44100; |
| 29 | + int64_t semantic_codebook_size = 4096; |
| 30 | + int64_t residual_codebook_size = 1024; |
| 31 | + int64_t quantizer_codebooks = 9; |
| 32 | + int64_t total_codebooks = 10; |
| 33 | + int64_t codebook_dim = 8; |
| 34 | + int64_t latent_dim = 1024; |
| 35 | + int64_t frame_length = 2048; |
| 36 | +}; |
| 37 | + |
| 38 | +struct FishDacCodecWeightBinding { |
| 39 | + std::string encoder_prefix = "encoder"; |
| 40 | + std::string quantizer_prefix = "quantizer"; |
| 41 | + std::string decoder_prefix = "decoder"; |
| 42 | +}; |
| 43 | + |
| 44 | +struct FishDacCodecRuntimeOptions { |
| 45 | + size_t graph_arena_bytes = 512ull * 1024ull * 1024ull; |
| 46 | + size_t weight_context_bytes = 1024ull * 1024ull * 1024ull; |
| 47 | + assets::TensorStorageType matmul_weight_storage_type = assets::TensorStorageType::Native; |
| 48 | + assets::TensorStorageType conv_weight_storage_type = assets::TensorStorageType::Native; |
| 49 | +}; |
| 50 | + |
| 51 | +class FishDacCodecComponent { |
| 52 | +public: |
| 53 | + static std::shared_ptr<const FishDacCodecComponent> load_from_tensor_source( |
| 54 | + std::shared_ptr<const assets::TensorSource> source, |
| 55 | + FishDacCodecConfig config, |
| 56 | + FishDacCodecWeightBinding binding, |
| 57 | + ggml_backend_t backend, |
| 58 | + core::BackendType backend_type, |
| 59 | + FishDacCodecRuntimeOptions options); |
| 60 | + |
| 61 | + ~FishDacCodecComponent(); |
| 62 | + |
| 63 | + const FishDacCodecConfig & config() const noexcept; |
| 64 | + |
| 65 | +private: |
| 66 | + struct Impl; |
| 67 | + explicit FishDacCodecComponent(std::shared_ptr<const Impl> impl); |
| 68 | + |
| 69 | + std::shared_ptr<const Impl> impl_; |
| 70 | + |
| 71 | + friend class FishDacCodecRuntime; |
| 72 | +}; |
| 73 | + |
| 74 | +class FishDacCodecRuntime { |
| 75 | +public: |
| 76 | + FishDacCodecRuntime( |
| 77 | + std::shared_ptr<const FishDacCodecComponent> component, |
| 78 | + core::BackendConfig backend, |
| 79 | + int threads, |
| 80 | + size_t graph_arena_bytes); |
| 81 | + ~FishDacCodecRuntime(); |
| 82 | + |
| 83 | + FishDacCodecRuntime(const FishDacCodecRuntime &) = delete; |
| 84 | + FishDacCodecRuntime & operator=(const FishDacCodecRuntime &) = delete; |
| 85 | + |
| 86 | + FishDacCodes encode_codes(const runtime::AudioBuffer & audio); |
| 87 | + FishDacLatents encode_latents(const runtime::AudioBuffer & audio); |
| 88 | + runtime::AudioBuffer decode_codes(const FishDacCodes & codes); |
| 89 | + runtime::AudioBuffer decode_latents(const FishDacLatents & latents); |
| 90 | + runtime::AudioBuffer decode_latents(const std::vector<float> & values, int64_t frames); |
| 91 | + void release_encode_graph(); |
| 92 | + void release_runtime_graphs(); |
| 93 | + |
| 94 | +private: |
| 95 | + class Impl; |
| 96 | + std::unique_ptr<Impl> impl_; |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace engine::codecs |
0 commit comments