Wrapper around the Qwen3 backbone running in llama.cpp. Accepts embedding vectors as input (bypassing tok_embd) and returns hidden states (after final RMSNorm, before the built-in LM head).
| 114 | |
| 115 | |
| 116 | class LlamaCppBackbone: |
| 117 | """Wrapper around the Qwen3 backbone running in llama.cpp. |
| 118 | |
| 119 | Accepts embedding vectors as input (bypassing tok_embd) and returns |
| 120 | hidden states (after final RMSNorm, before the built-in LM head). |
| 121 | """ |
| 122 | |
| 123 | def __init__( |
| 124 | self, |
| 125 | model_path: str | Path, |
| 126 | n_ctx: int = 4096, |
| 127 | n_batch: int = 512, |
| 128 | n_threads: int = 4, |
| 129 | n_gpu_layers: int = -1, |
| 130 | type_k: str = "f16", |
| 131 | type_v: str = "f16", |
| 132 | flash_attn: str | bool = "auto", |
| 133 | ): |
| 134 | lib_path = _find_bridge_lib() |
| 135 | log.info("Loading bridge from %s", lib_path) |
| 136 | self._lib = _load_bridge(lib_path) |
| 137 | |
| 138 | ggml_type_k = _resolve_ggml_type(type_k) |
| 139 | ggml_type_v = _resolve_ggml_type(type_v) |
| 140 | fa_type = _resolve_flash_attn(flash_attn) |
| 141 | |
| 142 | model_path = str(Path(model_path).resolve()) |
| 143 | log.info( |
| 144 | "Loading GGUF model: %s (type_k=%s, type_v=%s, flash_attn=%s)", |
| 145 | model_path, type_k, type_v, flash_attn, |
| 146 | ) |
| 147 | self._handle = self._lib.bridge_create( |
| 148 | model_path.encode("utf-8"), n_ctx, n_batch, n_threads, n_gpu_layers, |
| 149 | ggml_type_k, ggml_type_v, fa_type, |
| 150 | ) |
| 151 | if not self._handle: |
| 152 | raise RuntimeError(f"Failed to load model from {model_path}") |
| 153 | |
| 154 | self.n_embd = self._lib.bridge_n_embd(self._handle) |
| 155 | self.n_vocab = self._lib.bridge_n_vocab(self._handle) |
| 156 | self.n_batch = n_batch |
| 157 | self.n_ctx = n_ctx |
| 158 | log.info( |
| 159 | "LlamaCppBackbone ready: n_embd=%d, n_vocab=%d, n_ctx=%d, n_batch=%d", |
| 160 | self.n_embd, self.n_vocab, n_ctx, n_batch, |
| 161 | ) |
| 162 | |
| 163 | def decode_single(self, embd: np.ndarray, pos: int, output: bool = True) -> None: |
| 164 | """Feed a single embedding vector at the given position.""" |
| 165 | assert embd.shape == (self.n_embd,), f"Expected ({self.n_embd},), got {embd.shape}" |
| 166 | embd = np.ascontiguousarray(embd, dtype=np.float32) |
| 167 | ptr = embd.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) |
| 168 | ret = self._lib.bridge_decode_embd(self._handle, ptr, pos, int(output)) |
| 169 | if ret != 0: |
| 170 | raise RuntimeError(f"llama_decode failed with code {ret}") |
| 171 | |
| 172 | def decode_batch( |
| 173 | self, |
no outgoing calls
no test coverage detected