aboutsummaryrefslogtreecommitdiff
path: root/embed_mlx.c
diff options
context:
space:
mode:
Diffstat (limited to 'embed_mlx.c')
-rw-r--r--embed_mlx.c127
1 files changed, 2 insertions, 125 deletions
diff --git a/embed_mlx.c b/embed_mlx.c
index b9b2137..de82f83 100644
--- a/embed_mlx.c
+++ b/embed_mlx.c
@@ -1,4 +1,5 @@
#include "embed_mlx.h"
+#include "embed_config.h"
#include "embed.h"
#include "qwen_safetensors.h"
@@ -322,130 +323,6 @@ static void upcast_f32_inplace(mlx_array *a, mlx_stream s) {
* Load
* ======================================================================== */
-/* We duplicate a minimal config parser here so MLX can load standalone.
- * (Same logic as embed.c:parse_config, kept inline for simplicity.) */
-
-static const char *skip_ws_m(const char *p) {
- while (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')
- p++;
- return p;
-}
-
-static const char *json_find_m(const char *json, const char *key) {
- size_t klen = strlen(key);
- const char *p = json;
- while ((p = strstr(p, key)) != NULL) {
- if (p > json && *(p - 1) == '"') {
- const char *a = p + klen;
- if (*a == '"') {
- a = skip_ws_m(a + 1);
- if (*a == ':')
- return skip_ws_m(a + 1);
- }
- }
- p += klen;
- }
- return NULL;
-}
-
-static int json_string_equals_m(const char *json, const char *key, const char *expected) {
- const char *v = json_find_m(json, key);
- if (!v || *v != '"')
- return 0;
- v++;
- size_t len = strlen(expected);
- return strncmp(v, expected, len) == 0 && v[len] == '"';
-}
-
-static int mlx_parse_config(embed_config_t *cfg, const char *model_dir) {
- char path[1024];
- int path_len = snprintf(path, sizeof(path), "%s/config.json", model_dir);
- if (path_len < 0 || (size_t)path_len >= sizeof(path)) {
- fprintf(stderr, "mlx: model path too long\n");
- return -1;
- }
- FILE *f = fopen(path, "rb");
- if (!f) {
- fprintf(stderr, "mlx: cannot open %s\n", path);
- return -1;
- }
- if (fseek(f, 0, SEEK_END) != 0) {
- fclose(f);
- return -1;
- }
- long sz = ftell(f);
- if (sz < 0 || (size_t)sz == SIZE_MAX || fseek(f, 0, SEEK_SET) != 0) {
- fclose(f);
- return -1;
- }
- char *buf = (char *)malloc((size_t)sz + 1);
- if (!buf || fread(buf, 1, (size_t)sz, f) != (size_t)sz) {
- fclose(f);
- free(buf);
- return -1;
- }
- fclose(f);
- buf[sz] = '\0';
-
-#define GI(k, fb, def) \
- do { \
- const char *v = json_find_m(buf, k); \
- cfg->fb = v ? atoi(v) : def; \
- } while (0)
-#define GF(k, fb, def) \
- do { \
- const char *v = json_find_m(buf, k); \
- cfg->fb = v ? (float)atof(v) : def; \
- } while (0)
- GI("hidden_size", hidden_size, 0);
- GI("num_hidden_layers", n_layers, 0);
- GI("num_attention_heads", n_heads, 0);
- GI("num_key_value_heads", n_kv_heads, 0);
- GI("head_dim", head_dim, EMBED_HEAD_DIM);
- GI("intermediate_size", intermediate_size, 0);
- GI("vocab_size", vocab_size, EMBED_VOCAB_SIZE);
- GF("rms_norm_eps", rms_norm_eps, 1e-6f);
- GF("rope_theta", rope_theta, 1000000.0f);
-#undef GI
-#undef GF
- cfg->attention_mode = EMBED_ATTENTION_BIDIRECTIONAL;
- cfg->pooling_mode = EMBED_POOL_MEAN;
- cfg->normalize_embeddings = 0;
- cfg->append_terminal_token = 0;
- if (json_string_equals_m(buf, "model_type", "qwen3")) {
- cfg->attention_mode = EMBED_ATTENTION_CAUSAL;
- cfg->pooling_mode = EMBED_POOL_LAST_TOKEN;
- cfg->normalize_embeddings = 1;
- cfg->append_terminal_token = 1;
- }
-
- cfg->q_dim = 0;
- cfg->kv_dim = 0;
- if (cfg->n_heads > 0 && cfg->head_dim > 0 && cfg->n_heads <= INT_MAX / cfg->head_dim)
- cfg->q_dim = cfg->n_heads * cfg->head_dim;
- if (cfg->n_kv_heads > 0 && cfg->head_dim > 0 && cfg->n_kv_heads <= INT_MAX / cfg->head_dim)
- cfg->kv_dim = cfg->n_kv_heads * cfg->head_dim;
- free(buf);
-
- if (cfg->hidden_size <= 0 || cfg->n_layers <= 0 || cfg->n_heads <= 0 || cfg->n_kv_heads <= 0 ||
- cfg->head_dim <= 0 || cfg->intermediate_size <= 0 || cfg->vocab_size <= 0 ||
- cfg->q_dim <= 0 || cfg->kv_dim <= 0 || (cfg->head_dim & 1) ||
- cfg->n_heads % cfg->n_kv_heads != 0 || !isfinite(cfg->rms_norm_eps) ||
- cfg->rms_norm_eps <= 0.0f || !isfinite(cfg->rope_theta) || cfg->rope_theta <= 0.0f) {
- fprintf(stderr,
- "mlx: invalid config in %s "
- "(hidden=%d, layers=%d, heads=%d/%d, head_dim=%d, inter=%d)\n",
- path, cfg->hidden_size, cfg->n_layers, cfg->n_heads, cfg->n_kv_heads, cfg->head_dim,
- cfg->intermediate_size);
- return -1;
- }
- if (cfg->n_layers > EMBED_MAX_LAYERS) {
- fprintf(stderr, "mlx: too many layers (%d > %d)\n", cfg->n_layers, EMBED_MAX_LAYERS);
- return -1;
- }
- return 0;
-}
-
static int mlx_model_dir_has_file(const char *model_dir, const char *rel) {
char path[1024];
int n = snprintf(path, sizeof(path), "%s/%s", model_dir, rel);
@@ -516,7 +393,7 @@ static embed_mlx_ctx_t *mlx_load_range_ex(const char *model_dir,
ctx->quantize_bits = options.quantize_bits;
ctx->quantize_group_size = options.quantize_group_size;
- if (mlx_parse_config(&ctx->config, model_dir) != 0) {
+ if (embed_config_parse(&ctx->config, model_dir) != 0) {
free(ctx);
return NULL;
}