Vision kernels (im2patch, patch embedding, etc.) More...
#include <string.h>#include <stddef.h>#include <stdint.h>#include <math.h>#include "bf16_utils.h"Go to the source code of this file.
Macros | |
| #define | CK_VISION_NOINLINE |
Functions | |
| void | add_stream_inplace (float *a, const float *b, size_t n) |
| void | add_stream_reorder_2d (float *main_inout, float *aux_scratch, int grid_h, int grid_w, int embed_dim, int merge_size) |
| void | feature_concat (const float *main_input, const float *branch_input, float *output, int rows, int main_dim, int branch_slice_dim, int num_branch_slices) |
| void | feature_concat_2way (const float *main_input, const float *branch_input, float *output, int rows, int main_dim, int branch_slice_dim, int num_branch_slices) |
| void | feature_slice_copy (const float *src, float *dst, int rows, int src_dim, int dst_dim, int dst_feature_offset) |
| void | gemma4_vision_projector_prep_forward (const float *input, float *output, int tokens, int dim, float scale, float eps) |
| void | im2patch (const float *image, float *patches, int C, int H, int W, int P) |
| void | patch2im (const float *d_patches, float *d_image, int C, int H, int W, int P) |
| void | position_embeddings_add (float *x, const float *position_embd, int num_tokens, int embed_dim, int num_positions) |
| void | position_embeddings_add_at_offset (float *x, const float *position_embd, int num_tokens, int embed_dim, int num_positions, int start_position) |
| void | position_embeddings_add_gemma4v_xy (float *x, const float *position_embd, int grid_h, int grid_w, int embed_dim, int source_grid_size) |
| void | position_embeddings_add_tiled_2d (float *x, const float *position_embd, int grid_h, int grid_w, int embed_dim, int merge_size, int source_grid_size) |
| void | position_embeddings_add_tiled_2d_align_corners (float *x, const float *position_embd, int grid_h, int grid_w, int embed_dim, int merge_size, int source_grid_size) |
| void | position_embeddings_add_tiled_2d_align_corners_bf16 (float *x, const float *position_embd, int grid_h, int grid_w, int embed_dim, int merge_size, int source_grid_size) |
| void | position_embeddings_add_tiled_2d_align_corners_fp32_interp_bf16 (float *x, const float *position_embd, int grid_h, int grid_w, int embed_dim, int merge_size, int source_grid_size) |
| void | rowwise_bias_add (float *x, const float *bias, int rows, int dim) |
| void | spatial_average_pool_contiguous (const float *input, float *output, int grid_h, int grid_w, int embed_dim, int merge_size) |
| void | spatial_merge_2x2 (const float *input, float *output, int grid_h, int grid_w, int embed_dim) |
| void | spatial_merge_contiguous_tiled (const float *input, float *output, int grid_h, int grid_w, int embed_dim, int merge_size) |
| static int | tile_order_index_2d (int linear_idx, int grid_h, int grid_w, int merge_size) |
| static int | tile_order_linear_index_2d (int y, int x, int grid_h, int grid_w, int merge_size) |
| void | vision_position_ids_2d_merge (int32_t *positions, int grid_h, int grid_w, int merge_size) |
Vision kernels (im2patch, patch embedding, etc.)
After changes: make test && make llamacpp-parity-full
Definition in file vision_kernels.c.
| #define CK_VISION_NOINLINE |
Definition at line 27 of file vision_kernels.c.
| void add_stream_inplace | ( | float * | a, |
| const float * | b, | ||
| size_t | n | ||
| ) |
Definition at line 713 of file vision_kernels.c.
| void add_stream_reorder_2d | ( | float * | main_inout, |
| float * | aux_scratch, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size | ||
| ) |
Definition at line 726 of file vision_kernels.c.
References tile_order_index_2d().
| void feature_concat | ( | const float * | main_input, |
| const float * | branch_input, | ||
| float * | output, | ||
| int | rows, | ||
| int | main_dim, | ||
| int | branch_slice_dim, | ||
| int | num_branch_slices | ||
| ) |
Definition at line 804 of file vision_kernels.c.
| void feature_concat_2way | ( | const float * | main_input, |
| const float * | branch_input, | ||
| float * | output, | ||
| int | rows, | ||
| int | main_dim, | ||
| int | branch_slice_dim, | ||
| int | num_branch_slices | ||
| ) |
Definition at line 756 of file vision_kernels.c.
| void feature_slice_copy | ( | const float * | src, |
| float * | dst, | ||
| int | rows, | ||
| int | src_dim, | ||
| int | dst_dim, | ||
| int | dst_feature_offset | ||
| ) |
Definition at line 783 of file vision_kernels.c.
| void gemma4_vision_projector_prep_forward | ( | const float * | input, |
| float * | output, | ||
| int | tokens, | ||
| int | dim, | ||
| float | scale, | ||
| float | eps | ||
| ) |
Definition at line 633 of file vision_kernels.c.
| void im2patch | ( | const float * | image, |
| float * | patches, | ||
| int | C, | ||
| int | H, | ||
| int | W, | ||
| int | P | ||
| ) |
im2patch: Transforms an image into a sequence of flattened patches.
Image Layout: [C, H, W] (Row-major: W is fastest moving) Output Layout: [num_patches, C * P * P]
num_patches = (H/P) * (W/P) P = patch_size
Definition at line 82 of file vision_kernels.c.
References C.
| void patch2im | ( | const float * | d_patches, |
| float * | d_image, | ||
| int | C, | ||
| int | H, | ||
| int | W, | ||
| int | P | ||
| ) |
patch2im: Accumulates gradients from patches back into the image. (Backward pass)
d_patches: [num_patches, C * P * P] d_image: [C, H, W] (Accumulated)
Definition at line 123 of file vision_kernels.c.
References C.
| void position_embeddings_add | ( | float * | x, |
| const float * | position_embd, | ||
| int | num_tokens, | ||
| int | embed_dim, | ||
| int | num_positions | ||
| ) |
Add learned absolute position embeddings in-place.
x layout: [num_tokens, embed_dim] position_embd: [num_positions, embed_dim]
This first v8 vision path intentionally assumes native resized embeddings are already materialized in the weight tensor, so token i maps directly to position_embd[i]. This is the correct contract for fixed-size bring-up.
Definition at line 168 of file vision_kernels.c.
| void position_embeddings_add_at_offset | ( | float * | x, |
| const float * | position_embd, | ||
| int | num_tokens, | ||
| int | embed_dim, | ||
| int | num_positions, | ||
| int | start_position | ||
| ) |
Definition at line 187 of file vision_kernels.c.
| void position_embeddings_add_gemma4v_xy | ( | float * | x, |
| const float * | position_embd, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | source_grid_size | ||
| ) |
Definition at line 209 of file vision_kernels.c.
| void position_embeddings_add_tiled_2d | ( | float * | x, |
| const float * | position_embd, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size, | ||
| int | source_grid_size | ||
| ) |
Definition at line 248 of file vision_kernels.c.
References tile_order_index_2d(), and tile_order_linear_index_2d().
| void position_embeddings_add_tiled_2d_align_corners | ( | float * | x, |
| const float * | position_embd, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size, | ||
| int | source_grid_size | ||
| ) |
Definition at line 342 of file vision_kernels.c.
References tile_order_index_2d().
| void position_embeddings_add_tiled_2d_align_corners_bf16 | ( | float * | x, |
| const float * | position_embd, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size, | ||
| int | source_grid_size | ||
| ) |
Definition at line 397 of file vision_kernels.c.
References bf16_to_float(), float_to_bf16(), and tile_order_index_2d().
| void position_embeddings_add_tiled_2d_align_corners_fp32_interp_bf16 | ( | float * | x, |
| const float * | position_embd, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size, | ||
| int | source_grid_size | ||
| ) |
Definition at line 458 of file vision_kernels.c.
References bf16_to_float(), float_to_bf16(), and tile_order_index_2d().
| void rowwise_bias_add | ( | float * | x, |
| const float * | bias, | ||
| int | rows, | ||
| int | dim | ||
| ) |
Definition at line 696 of file vision_kernels.c.
| void spatial_average_pool_contiguous | ( | const float * | input, |
| float * | output, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size | ||
| ) |
Definition at line 657 of file vision_kernels.c.
| void spatial_merge_2x2 | ( | const float * | input, |
| float * | output, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim | ||
| ) |
Merge 2x2 neighboring tokens into a single wider token.
Input layout: [grid_h * grid_w, embed_dim] Output layout: [(grid_h/2) * (grid_w/2), embed_dim * 4]
Pack order within each merged token: top-left, top-right, bottom-left, bottom-right
Definition at line 577 of file vision_kernels.c.
| void spatial_merge_contiguous_tiled | ( | const float * | input, |
| float * | output, | ||
| int | grid_h, | ||
| int | grid_w, | ||
| int | embed_dim, | ||
| int | merge_size | ||
| ) |
Definition at line 616 of file vision_kernels.c.
|
static |
Definition at line 30 of file vision_kernels.c.
Referenced by add_stream_reorder_2d(), position_embeddings_add_tiled_2d(), position_embeddings_add_tiled_2d_align_corners(), position_embeddings_add_tiled_2d_align_corners_bf16(), and position_embeddings_add_tiled_2d_align_corners_fp32_interp_bf16().
|
static |
Definition at line 55 of file vision_kernels.c.
Referenced by position_embeddings_add_tiled_2d().
| void vision_position_ids_2d_merge | ( | int32_t * | positions, |
| int | grid_h, | ||
| int | grid_w, | ||
| int | merge_size | ||
| ) |
Build merged 2D vision position IDs in the layout expected by vision M-RoPE.
Output layout: [4, grid_h * grid_w] flattened as [y_stream | x_stream | y_stream_dup | x_stream_dup]
Tokens are emitted in merged-tile traversal order so the position buffer matches the same 2x2 grouping used by Qwen-style vision encoders.
Definition at line 529 of file vision_kernels.c.