|
| 1 | +# codec.cpp |
| 2 | + |
| 3 | +This repository is a C/C++ library + CLI that runs several neural audio codecs (currently WavTokenizer-Large, DAC, Mimi) using **ggml** graphs so execution can be offloaded via **ggml backends** (CPU/CUDA/Vulkan/Metal/etc.). |
| 4 | + |
| 5 | +The intended architecture is **llama.cpp-style**: |
| 6 | +- Build model forward passes as **ggml graphs (ops)**. |
| 7 | +- Execute via **ggml_backend + ggml_backend_sched** so backends can offload. |
| 8 | +- Avoid bespoke CPU-side tensor math buffers when possible. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## High-level layout |
| 13 | + |
| 14 | +- `include/codec.h` — public C API (model load/init, encode/decode, batch decode) |
| 15 | +- `src/codec.cpp` — top-level dispatch + model loading + backend selection |
| 16 | +- `src/models/` — per-architecture graph builders and glue |
| 17 | + - `wavtokenizer.cpp/.h` |
| 18 | + - `dac.cpp/.h` |
| 19 | + - `mimi.cpp/.h` |
| 20 | +- `src/runtime/` — graph cache + execution runtime |
| 21 | + - `graph.cpp/.h` — graph cache keyed by (kind, n_frames, n_q, hop, etc.) |
| 22 | + - `graph_exec.cpp` — ggml_backend scheduler init + graph compute |
| 23 | + - `tensor_utils.*`, `gguf_kv.*` — tensor helpers / metadata |
| 24 | +- `src/ops/` — small wrappers around ggml ops + a few custom compositions |
| 25 | + - `ggml_ops.*` — layernorm/groupnorm/linear/unary/snake/pad/crop helpers |
| 26 | + - `conv1d.*`, `convtr1d.*` — conv wrappers (keep only if ggml lacks needed variant) |
| 27 | +- `src/batch/` — sequence-level batch container + decode loop (MVP) |
| 28 | +- `examples/` — demo/inspection binaries (e.g. batch decode) |
| 29 | +- `ggml/` — ggml submodule/subproject |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## Build |
| 34 | + |
| 35 | +CMake project with ggml as a subdirectory. |
| 36 | + |
| 37 | +Typical CPU build: |
| 38 | +```bash |
| 39 | +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release |
| 40 | +cmake --build build -j |
| 41 | +``` |
| 42 | + |
| 43 | +Enable GPU backend (example: CUDA): |
| 44 | +```bash |
| 45 | +cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DGGML_CUDA=ON |
| 46 | +cmake --build build -j |
| 47 | +``` |
| 48 | + |
| 49 | +Backends are intended to be selected via ggml backend selection logic. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## Runtime / backend philosophy |
| 54 | + |
| 55 | +### Backend selection |
| 56 | +In `src/codec.cpp` the backend is selected roughly as: |
| 57 | +- if `codec_model_params.use_gpu = true`: call `ggml_backend_load_all()` then `ggml_backend_init_best()` |
| 58 | +- else: CPU backend |
| 59 | + |
| 60 | +### Scheduler-based execution (important) |
| 61 | +`src/runtime/graph_exec.cpp` uses: |
| 62 | +- `ggml_backend_sched_new(...)` |
| 63 | +- `ggml_backend_sched_graph_compute(...)` |
| 64 | + |
| 65 | +This is the core mechanism enabling CPU/GPU split + offload. |
| 66 | + |
| 67 | +**Key rule:** graphs should be constructed in a way that ggml can place tensors on supported backends; avoid pulling intermediate tensors out to CPU buffers. |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Graph caching |
| 72 | + |
| 73 | +Graphs are cached by a small key (see `codec_graph_cache_key` in internal headers), typically including: |
| 74 | +- graph kind (encode/decode per model) |
| 75 | +- `n_frames`, `n_q`, `hop`, input sizes, latent_dim, etc. |
| 76 | + |
| 77 | +Flow: |
| 78 | +1. `codec_graph_cache_get_or_build(...)` builds graph in an eval arena (`ggml_init(no_alloc=true)`). |
| 79 | +2. `codec_graph_prepare_io(...)` allocates tensors for the graph in a backend buffer. |
| 80 | +3. `codec_graph_compute(...)` runs scheduler compute. |
| 81 | + |
| 82 | +Important constraints: |
| 83 | +- When switching to a different graph allocation, scheduler reset may be required to avoid dangling allocations (see comments in `codec_graph_prepare_io`). |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## ggml op usage |
| 88 | + |
| 89 | +Prefer directly using ggml ops when available. |
| 90 | + |
| 91 | +`src/ops/ggml_ops.cpp` provides small helpers that are either: |
| 92 | +- thin wrappers over ggml primitives (`ggml_norm`, `ggml_group_norm`, `ggml_mul_mat`, activations) |
| 93 | +- composed ops built from primitives (e.g. DAC `snake` implemented as `x + sin(ax)^2 / a`) |
| 94 | + |
| 95 | +If a needed op is missing in ggml: |
| 96 | +1. First try composing from existing ops. |
| 97 | +2. If impossible/perf critical, add a custom op (CPU SIMD first) and keep a path to backend support later. |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Model files / GGUF |
| 102 | + |
| 103 | +Models are loaded from `.gguf`. |
| 104 | + |
| 105 | +Some tensors that could be generated at runtime should instead be baked into GGUF during conversion (for reproducibility + avoiding runtime FP32→FP16 conversions). |
| 106 | + |
| 107 | +If conversion scripts are involved, regenerate gguf after changes (stale gguf is a common source of “missing tensor” errors). |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## Conventions / guardrails for changes |
| 112 | + |
| 113 | +- Keep encode/decode numerics stable (unit/regression tests where possible). |
| 114 | +- Avoid introducing new CPU-only intermediate buffers; build everything as ggml tensors. |
| 115 | +- When touching graph execution / backend scheduler: be careful with allocation lifetimes (`eval_ctx`, scheduler reset semantics). |
| 116 | +- Prefer small, reviewable commits. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Useful entry points for Codex |
| 121 | + |
| 122 | +If you need to understand execution: |
| 123 | +- `src/runtime/graph_exec.cpp` (scheduler + compute) |
| 124 | +- `src/runtime/graph.cpp` (cache + arena) |
| 125 | + |
| 126 | +If you need to understand a model forward: |
| 127 | +- `src/models/mimi.cpp` / `dac.cpp` / `wavtokenizer.cpp` |
| 128 | + |
| 129 | +If you need to add/replace an op: |
| 130 | +- `src/ops/ggml_ops.cpp` (+ possibly ggml upstream) |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +## Mimi Encoder Migration Status |
| 135 | + |
| 136 | +- Mimi encode path is consolidated into one canonical graph kind: `CODEC_GRAPH_MIMI_ENCODE`. |
| 137 | +- The unified graph builder is the only Mimi encode graph path (`frontend -> transformer -> downsample -> unrolled RVQ`). |
| 138 | +- Split/legacy graph kinds for Mimi encode stages are removed from runtime graph enums. |
| 139 | +- Mimi encode weight writing now targets only the canonical encode graph path. |
0 commit comments