Write C header with defines and inline accessor.
(path: str, token_count: int, dim: int, incbin_path: str)
| 292 | |
| 293 | |
| 294 | def write_vectors_h(path: str, token_count: int, dim: int, incbin_path: str): |
| 295 | """Write C header with defines and inline accessor.""" |
| 296 | with open(path, "w") as f: |
| 297 | f.write(f"""/* nomic-embed-code (nomic-ai/nomic-embed-code) token embeddings. |
| 298 | * {token_count} tokens x {dim}d int8-quantized unit vectors. |
| 299 | * Distilled from 7B model via full inference on filtered vocabulary. |
| 300 | * Simulated attention: {SIM_ATTENTION_ITERS} iterations, K={SIM_ATTENTION_K}, alpha={SIM_ATTENTION_ALPHA}. |
| 301 | * |
| 302 | * Vector blob embedded via code_vectors_blob.S (assembler .incbin). |
| 303 | * Token strings are in this header as a static array. |
| 304 | * |
| 305 | * Source: https://huggingface.co/nomic-ai/nomic-embed-code |
| 306 | * License: Apache 2.0 |
| 307 | */ |
| 308 | #ifndef CBM_NOMIC_VECTORS_H |
| 309 | #define CBM_NOMIC_VECTORS_H |
| 310 | |
| 311 | #include <stdint.h> |
| 312 | |
| 313 | #define PRETRAINED_TOKEN_COUNT {token_count} |
| 314 | #define PRETRAINED_DIM {dim} |
| 315 | |
| 316 | /* Raw vector blob: first 8 bytes = [int32 count][int32 dim], |
| 317 | * then count x dim int8 values (unit-normalized, x127 scaled). */ |
| 318 | extern const unsigned char PRETRAINED_VECTOR_BLOB[]; |
| 319 | extern const unsigned int PRETRAINED_VECTOR_BLOB_LEN; |
| 320 | |
| 321 | /* Access the int8 vector for token index i. */ |
| 322 | static inline const int8_t *pretrained_vec_at(int i) {{ |
| 323 | return (const int8_t *)(PRETRAINED_VECTOR_BLOB + 8 + (size_t)i * PRETRAINED_DIM); |
| 324 | }} |
| 325 | |
| 326 | /* Token strings (separate header to keep this file clean). */ |
| 327 | #include "code_tokens.h" |
| 328 | |
| 329 | #endif /* CBM_NOMIC_VECTORS_H */ |
| 330 | """) |
| 331 | print(f" {path}: written") |
| 332 | |
| 333 | |
| 334 | def write_blob_s(path: str, incbin_path: str): |