diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-11 15:34:50 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-14 08:27:03 +0200 |
| commit | 6b3c4bb1482917e4fb24480a1d082da5f191467b (patch) | |
| tree | 6a29d58959d1c5d3a0febcc9cb06005ac5a009ed /embed_mlx.c | |
| parent | 54e6e6bbef396eb1d38c09517d8157301fb8f151 (diff) | |
| download | ffwd-6b3c4bb1482917e4fb24480a1d082da5f191467b.tar.gz ffwd-6b3c4bb1482917e4fb24480a1d082da5f191467b.zip | |
dist: remove distributed layer-sharding (protocol, CLI flags, slice APIs)
Diffstat (limited to 'embed_mlx.c')
| -rw-r--r-- | embed_mlx.c | 186 |
1 files changed, 0 insertions, 186 deletions
diff --git a/embed_mlx.c b/embed_mlx.c index 4ef283c..aa0d1ed 100644 --- a/embed_mlx.c +++ b/embed_mlx.c @@ -654,19 +654,6 @@ pplx_mlx_ctx_t *pplx_mlx_load_with_options(const char *model_dir, return mlx_load_range_ex(model_dir, 0, -1, opts, 0); } -pplx_mlx_ctx_t *pplx_mlx_load_slice(const char *model_dir, - int layer_start, int layer_end) -{ - return mlx_load_range_ex(model_dir, layer_start, layer_end, NULL, 0); -} - -pplx_mlx_ctx_t *pplx_mlx_load_slice_with_options( - const char *model_dir, int layer_start, int layer_end, - const pplx_mlx_options_t *opts) -{ - return mlx_load_range_ex(model_dir, layer_start, layer_end, opts, 0); -} - /* ======================================================================== * Free * ======================================================================== */ @@ -1674,179 +1661,6 @@ int pplx_mlx_embed_into(pplx_mlx_ctx_t *ctx, const int *token_ids, return pplx_mlx_embed_batch(ctx, &input, 1, out_embedding); } -int pplx_mlx_forward_slice_batch(pplx_mlx_ctx_t *ctx, - const pplx_input_t *inputs, int batch, - const float *input_states, - int layer_start, int layer_end, - int apply_final_norm, - float *out_states) -{ - if (!ctx || !inputs || batch <= 0 || !out_states || - layer_start < 0 || layer_start > layer_end || - layer_end > ctx->config.n_layers || - layer_start < ctx->layer_start || layer_end > ctx->layer_end || - (layer_start == 0 && input_states) || - (layer_start != 0 && !input_states) || - (layer_start == 0 && !arr_ok(ctx->embed_tokens)) || - (apply_final_norm && - (layer_end != ctx->config.n_layers || !arr_ok(ctx->norm)))) - return -1; - - const pplx_config_t *c = &ctx->config; - mlx_stream S = ctx->stream; - int hidden = c->hidden_size; - int max_seq = 0; - int total_seq = 0; - int has_padding = 0; - for (int b = 0; b < batch; b++) { - if ((layer_start == 0 && !inputs[b].ids) || inputs[b].n_tokens <= 0 || - total_seq > INT_MAX - inputs[b].n_tokens) - return -1; - total_seq += inputs[b].n_tokens; - if (inputs[b].n_tokens > max_seq) max_seq = inputs[b].n_tokens; - } - for (int b = 0; b < batch; b++) { - if (inputs[b].n_tokens != max_seq) has_padding = 1; - } - - size_t rows, ids_bytes, mask_bytes, state_bytes, row_bytes; - if (mlx_mul_size((size_t)batch, (size_t)max_seq, &rows) != 0 || - mlx_mul_size(rows, sizeof(int), &ids_bytes) != 0 || - mlx_mul_size(rows, sizeof(float), &mask_bytes) != 0 || - mlx_mul_size((size_t)hidden, sizeof(float), &row_bytes) != 0) - return -1; - if (rows == 0 || row_bytes == 0 || (has_padding && mask_bytes == 0)) - return -1; - if (layer_start != 0 && - mlx_mul_size(rows, (size_t)hidden, &state_bytes) != 0) - return -1; - if (layer_start == 0) - state_bytes = 0; - - int *padded_ids = layer_start == 0 - ? (int *)malloc(ids_bytes) : NULL; - float *padded_states = layer_start != 0 - ? (float *)calloc(1, state_bytes) : NULL; - float *attn_mask_data = has_padding - ? (float *)malloc(mask_bytes) : NULL; - if ((layer_start == 0 && !padded_ids) || - (layer_start != 0 && !padded_states) || - (has_padding && !attn_mask_data)) { - free(padded_ids); free(padded_states); free(attn_mask_data); - return -1; - } - - int packed_offset = 0; - for (int b = 0; b < batch; b++) { - for (int t = 0; t < max_seq; t++) { - size_t dense_row = (size_t)b * max_seq + t; - if (t < inputs[b].n_tokens) { - if (layer_start == 0) { - int id = inputs[b].ids[t]; - if (id < 0 || id >= c->vocab_size) { - fprintf(stderr, "mlx: invalid token id %d at input %d position %d\n", - id, b, t); - free(padded_ids); free(padded_states); - free(attn_mask_data); - return -1; - } - padded_ids[dense_row] = id; - } else { - memcpy(padded_states + dense_row * hidden, - input_states + (size_t)(packed_offset + t) * hidden, - row_bytes); - } - if (has_padding) attn_mask_data[dense_row] = 0.0f; - } else { - if (layer_start == 0) padded_ids[dense_row] = 0; - if (has_padding) attn_mask_data[dense_row] = -1.0e9f; - } - } - packed_offset += inputs[b].n_tokens; - } - - int attn_mask_shape[] = {batch, 1, 1, max_seq}; - mlx_array attn_mask = has_padding - ? mlx_array_new_data(attn_mask_data, attn_mask_shape, 4, MLX_FLOAT32) - : (mlx_array){0}; - free(attn_mask_data); - if (has_padding && !arr_ok(attn_mask)) { - free(padded_ids); free(padded_states); - return -1; - } - if (has_padding && mlx_weight_dtype(ctx) == MLX_BFLOAT16) { - mlx_array attn_mask_bf16 = mlx_array_new(); - mlx_astype(&attn_mask_bf16, attn_mask, MLX_BFLOAT16, S); - mlx_array_free(attn_mask); - attn_mask = attn_mask_bf16; - if (!arr_ok(attn_mask)) { - free(padded_ids); free(padded_states); - return -1; - } - } - - mlx_array x; - if (layer_start == 0) { - int ids_shape[] = {batch, max_seq}; - mlx_array ids = mlx_array_new_data(padded_ids, ids_shape, 2, MLX_INT32); - free(padded_ids); - if (!arr_ok(ids)) { - if (has_padding) mlx_array_free(attn_mask); - return -1; - } - x = mlx_array_new(); - mlx_take_axis(&x, ctx->embed_tokens, ids, 0, S); - mlx_array_free(ids); - } else { - int states_shape[] = {batch, max_seq, hidden}; - mlx_array states = mlx_array_new_data(padded_states, states_shape, 3, - MLX_FLOAT32); - free(padded_states); - if (!arr_ok(states)) { - if (has_padding) mlx_array_free(attn_mask); - return -1; - } - if (mlx_weight_dtype(ctx) == MLX_BFLOAT16) { - x = mlx_array_new(); - mlx_astype(&x, states, MLX_BFLOAT16, S); - mlx_array_free(states); - } else { - x = states; - } - } - - x = mlx_forward_layers(ctx, x, batch, max_seq, has_padding, attn_mask, - layer_start, layer_end); - if (has_padding) mlx_array_free(attn_mask); - - if (apply_final_norm) { - mlx_array x_normed = mlx_array_new(); - mlx_fast_rms_norm(&x_normed, x, ctx->norm, c->rms_norm_eps, S); - mlx_array_free(x); - x = x_normed; - } - - mlx_array x_f32 = mlx_array_new(); - mlx_astype(&x_f32, x, MLX_FLOAT32, S); - mlx_array_free(x); - mlx_array_eval(x_f32); - - const float *data = mlx_array_data_float32(x_f32); - if (!data) { - mlx_array_free(x_f32); - return -1; - } - packed_offset = 0; - for (int b = 0; b < batch; b++) { - memcpy(out_states + (size_t)packed_offset * hidden, - data + (size_t)b * max_seq * hidden, - (size_t)inputs[b].n_tokens * row_bytes); - packed_offset += inputs[b].n_tokens; - } - mlx_array_free(x_f32); - return 0; -} - typedef struct { int index; int span_offset; |
