mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-28 11:10:41 +00:00
Some checks failed
CI (self-hosted CUDA backend) / gpu-cuda (push) Has been cancelled
Behavior Check / ubuntu (push) Has been cancelled
Behavior Check / windows (push) Has been cancelled
CI (3rd-party) / ubuntu-26-llguidance (push) Has been cancelled
CI (android) / default (push) Has been cancelled
CI (android) / ndk (push) Has been cancelled
CI (android) / arm64 (push) Has been cancelled
CI (apple) / macos-latest-arm64 (push) Has been cancelled
CI (cpu) / ubuntu (arm64, ubuntu-26.04-arm) (push) Has been cancelled
CI (cpu) / ubuntu (x64, ubuntu-26.04) (push) Has been cancelled
CI (cpu) / windows / arm64 (push) Has been cancelled
CI (cpu) / windows / x64-cpu-static (push) Has been cancelled
CI (cpu) / windows / x64-openblas (push) Has been cancelled
CI (cpu) / build-cmake-pkg (push) Has been cancelled
CI (CUDA, ubuntu) / cuda (push) Has been cancelled
CI (CUDA, ubuntu) / hip (push) Has been cancelled
CI (CUDA, ubuntu) / musa (push) Has been cancelled
CI (ibm) / ubuntu-26-s390x (VXE OFF) (push) Has been cancelled
CI (ibm) / ubuntu-26-s390x (VXE ON) (push) Has been cancelled
CI (ibm) / ubuntu-26-zdnn-s390x (push) Has been cancelled
CI (ibm) / ubuntu-24-ppc64le (push) Has been cancelled
CI (opencl) / windows-2025-opencl-adreno (push) Has been cancelled
CI (sanitize) / ctest (ubuntu-26.04, THREAD) (push) Has been cancelled
CI (openvino) / ubuntu-24-openvino (push) Has been cancelled
CI (openvino) / openvino-windows-2022 (push) Has been cancelled
CI (riscv) / ubuntu-cpu-riscv64-native (push) Has been cancelled
CI (sanitize) / ctest (ubuntu-26.04, ADDRESS) (push) Has been cancelled
CI (sanitize) / ctest (ubuntu-26.04, UNDEFINED) (push) Has been cancelled
CI (sycl) / ubuntu-26-sycl (fp16, ON) (push) Has been cancelled
CI (sycl) / ubuntu-26-sycl (fp32, OFF) (push) Has been cancelled
CI (sycl) / windows-latest-sycl (push) Has been cancelled
CI (virtgpu) / ubuntu-26-virtgpu (push) Has been cancelled
CI (self-hosted CPU backend) / cpu-arm64-high-perf-graviton4 (push) Has been cancelled
CI (vulkan) / ubuntu-arm64 (push) Has been cancelled
CI (vulkan) / ubuntu-llvmpipe (push) Has been cancelled
CI (vulkan) / windows / arm64 (push) Has been cancelled
CI (vulkan) / windows / x64 (push) Has been cancelled
CI (wasi) / ubuntu-webgpu (push) Has been cancelled
CI (wasm) / ubuntu-webgpu (push) Has been cancelled
CI (self-hosted CPU backend) / cpu-x64-high-perf (push) Has been cancelled
CI (self-hosted CUDA backend) / gpu-rocm (push) Has been cancelled
CI (self-hosted KleidiAI backend) / cpu-arm64-graviton4-kleidiai (push) Has been cancelled
CI (self-hosted Metal backend) / gpu-metal (push) Has been cancelled
CI (self-hosted OpenVINO backend) / gpu-openvino-low-perf (push) Has been cancelled
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm (push) Has been cancelled
CI (self-hosted Vulkan backend) / gpu-vulkan-nvidia-cm2 (push) Has been cancelled
CI (self-hosted Vulkan backend) / gpu-vulkan-apple (push) Has been cancelled
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-linux (push) Has been cancelled
CI (self-hosted Vulkan backend) / gpu-vulkan-intel-windows (push) Has been cancelled
CI (self-hosted WebGPU backend) / gpu-webgpu-nvidia (push) Has been cancelled
CI (self-hosted WebGPU backend) / gpu-webgpu-apple (push) Has been cancelled
Code Style Checker / model-naming (push) Has been cancelled
EditorConfig Checker / editorconfig (push) Has been cancelled
HIP quality check / ubuntu-22-hip-quality-check (push) Has been cancelled
Models Backend Check / cuda (push) Has been cancelled
Models Backend Check / metal (push) Has been cancelled
Models Backend Check / rocm (push) Has been cancelled
Models Backend Check / vulkan-nvidia (push) Has been cancelled
Models Backend Check / vulkan-amd (push) Has been cancelled
Models Backend Check / webgpu-nvidia (push) Has been cancelled
flake8 Lint / Lint (push) Has been cancelled
Release / check-release (push) Has been cancelled
Server (sanitize) / server (RelWithDebInfo, ADDRESS) (push) Has been cancelled
Server (sanitize) / server (RelWithDebInfo, UNDEFINED) (push) Has been cancelled
Server (self-hosted) / server-metal (push) Has been cancelled
Server (self-hosted) / server-cuda (push) Has been cancelled
Server (self-hosted) / server-kleidiai (push) Has been cancelled
Server / ubuntu (push) Has been cancelled
Server / windows (push) Has been cancelled
Release / macos-cpu (arm64, arm64, -DGGML_METAL_EMBED_LIBRARY=ON -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-26) (push) Has been cancelled
Release / macos-cpu (x64, x64, -DGGML_METAL=OFF -DCMAKE_OSX_DEPLOYMENT_TARGET=13.3, macos-15-intel) (push) Has been cancelled
Release / linux-arm64-snapdragon (push) Has been cancelled
Release / ubuntu-cpu (26.04-s390x, ubuntu-26.04-s390x) (push) Has been cancelled
Release / ubuntu-cpu (arm64, ubuntu-24.04-arm) (push) Has been cancelled
Release / ubuntu-cpu (s390x, ubuntu-24.04-s390x) (push) Has been cancelled
Release / ubuntu-cpu (x64, ubuntu-22.04) (push) Has been cancelled
Release / ubuntu-24-openvino (push) Has been cancelled
Release / windows-openvino (push) Has been cancelled
Release / ubuntu-vulkan (arm64, ubuntu-24.04-arm) (push) Has been cancelled
Release / ubuntu-vulkan (x64, ubuntu-22.04) (push) Has been cancelled
Release / ubuntu-cuda (13.5, arm64) (push) Has been cancelled
Release / ubuntu-cuda (12.8, x64) (push) Has been cancelled
Release / ubuntu-cuda (13.5, x64) (push) Has been cancelled
Release / android-arm64 (push) Has been cancelled
Release / android-arm64-snapdragon (push) Has been cancelled
Release / windows-cpu / arm64 (push) Has been cancelled
Release / windows-cpu / x64 (push) Has been cancelled
Release / windows-rocm (10.0.0, x64, gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1153;gfx1200;gfx1201) (push) Has been cancelled
Release / windows (arm64, opencl-adreno, -G "Ninja Multi-Config" -D CMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-llvm.cmake -DCMAKE_PREFIX_PATH="$env:RUNNER_TEMP/opencl-arm64-release" -DGGML_OPENCL=ON -DGGML_OPENCL_USE_ADRENO_KERNELS=ON, ggml-opencl) (push) Has been cancelled
Release / windows (arm64, vulkan, -G "Ninja Multi-Config" -DGGML_VULKAN=ON -D CMAKE_TOOLCHAIN_FILE=cmake/arm64-windows-llvm.cmake, ggml-vulkan) (push) Has been cancelled
Release / windows (x64, vulkan, -DGGML_VULKAN=ON, ggml-vulkan) (push) Has been cancelled
Release / ubuntu-26-zdnn (push) Has been cancelled
Release / windows-cuda (13.5, arm64) (push) Has been cancelled
Release / windows-cuda (12.4, x64) (push) Has been cancelled
Release / windows-cuda (13.5, x64) (push) Has been cancelled
Release / windows-sycl (push) Has been cancelled
Release / ubuntu-24-sycl (fp16, ON) (push) Has been cancelled
Release / ubuntu-24-sycl (fp32, OFF) (push) Has been cancelled
Release / ubuntu-24-rocm (10.0.0, x64, gfx908;gfx90a;gfx942;gfx950;gfx1010;gfx1011;gfx1012;gfx1030;gfx1031;gfx1032;gfx1033;gfx1034;gfx1035;gfx1036;gfx1100;gfx1101;gfx1102;gfx1103;gfx1150;gfx1151;gfx1152;gfx1200;gfx1201) (push) Has been cancelled
Release / ios-xcode (push) Has been cancelled
Release / ui-build (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Check Pre-Tokenizer Hashes / pre-tokenizer-hashes (push) Has been cancelled
Python check requirements.txt / check-requirements (push) Has been cancelled
Python Type-Check / python type-check (push) Has been cancelled
Update Operations Documentation / update-ops-docs (push) Has been cancelled
184 lines
6.3 KiB
C++
184 lines
6.3 KiB
C++
#pragma once
|
|
|
|
#include "ggml.h"
|
|
#include "ggml-cpp.h"
|
|
#include "mtmd-subcontext.h"
|
|
#include "mtmd-impl.h"
|
|
#include "mtmd-model.h"
|
|
|
|
#include <vector>
|
|
#include <functional>
|
|
|
|
#define DEFAULT_INTERPOLATION_MODE (GGML_SCALE_MODE_BILINEAR | GGML_SCALE_FLAG_ANTIALIAS)
|
|
|
|
struct build_vit_opts {
|
|
ggml_tensor * attn_mask = nullptr;
|
|
// TODO @ngxson : merge attn_mask and attn_mask_layers into one call
|
|
std::vector<ggml_tensor *> attn_mask_layers; // one per layer
|
|
|
|
// hook at layer output embeddings
|
|
std::function<void(ggml_tensor * cur, int il)> callback_layer_out = nullptr;
|
|
|
|
// whether to skip the automatic post-layernorm (model.post_ln_w) applied at the end
|
|
bool skip_post_ln = false;
|
|
};
|
|
|
|
struct mtmd_graph {
|
|
const mtmd_model & model;
|
|
const mtmd_hparams & hparams;
|
|
projector_type proj_type;
|
|
|
|
const mtmd_image_f32 & img; // for backward compat
|
|
const mtmd_image_f32_batch * img_batch = nullptr;
|
|
|
|
const int patch_size;
|
|
const int n_patches_x;
|
|
const int n_patches_y;
|
|
const int n_patches;
|
|
const int n_embd;
|
|
const int n_head;
|
|
const int n_head_kv;
|
|
const int d_head;
|
|
const int n_layer;
|
|
const int n_mmproj_embd;
|
|
const float eps;
|
|
float kq_scale; // TODO: maybe move this to hparams
|
|
const mtmd_flash_attn_type flash_attn_type;
|
|
|
|
// TODO [QWEN_VIDEO]: improve this in the future
|
|
int n_batch = 1;
|
|
|
|
ggml_context_ptr ctx0_ptr;
|
|
ggml_context * ctx0;
|
|
ggml_cgraph * gf;
|
|
|
|
mtmd_graph(mtmd_subcontext * ctx, const mtmd_image_f32 & img);
|
|
|
|
// build sub-graph, reuse buf from parent
|
|
mtmd_graph(const mtmd_graph & parent);
|
|
|
|
virtual ~mtmd_graph() = default;
|
|
virtual ggml_cgraph * build() = 0;
|
|
|
|
// wrapper around ggml_mul_mat, allow hooking (e.g. LoRA, clamping) depending on the model
|
|
// tensor w should be the weight matrix, and tensor x should be the input
|
|
virtual ggml_tensor * build_mm(ggml_tensor * w, ggml_tensor * x) const;
|
|
// TODO: build_mm(w, b, x) to support bias
|
|
|
|
virtual bool support_batch() const {
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// utility functions
|
|
//
|
|
void cb(ggml_tensor * cur0, const char * name, int il) const;
|
|
|
|
const mtmd_image_f32 & get_img(size_t idx) const {
|
|
GGML_ASSERT(img_batch);
|
|
GGML_ASSERT(idx < img_batch->entries.size());
|
|
return img_batch->entries[idx];
|
|
}
|
|
|
|
// siglip2 naflex
|
|
ggml_tensor * resize_position_embeddings(uint32_t interpolation_mode = DEFAULT_INTERPOLATION_MODE);
|
|
|
|
// build vision transformer (ViT) cgraph
|
|
// this function should cover most of the models
|
|
// if your model has specific features, you should probably duplicate this function
|
|
ggml_tensor * build_vit(
|
|
ggml_tensor * inp,
|
|
int64_t n_pos,
|
|
norm_type norm_t,
|
|
ffn_op_type ffn_t,
|
|
ggml_tensor * learned_pos_embd,
|
|
std::function<ggml_tensor *(ggml_tensor *, const mtmd_layer &)> add_pos,
|
|
const build_vit_opts & opts = {});
|
|
|
|
// build the input after conv2d (inp_raw --> patches)
|
|
// returns tensor with shape [n_embd, n_patches]
|
|
ggml_tensor * build_inp();
|
|
|
|
ggml_tensor * build_inp_raw(int channels = 3);
|
|
|
|
// qwen-vl style temporal merge, handles n_batch == 1 (image) or n_batch == 2 (2 video frames)
|
|
// requires patch_embeddings_0 and patch_embeddings_1; returns tensor with shape [w, h, n_embd, 1]
|
|
ggml_tensor * build_inp_temporal_merge();
|
|
|
|
// f16 if flash attn is enabled, set it with set_input_attn_mask()
|
|
// idx is only needed when the graph has more than one mask
|
|
ggml_tensor * build_inp_attn_mask(int64_t n_kv, int64_t n_q, int idx = 0);
|
|
|
|
ggml_tensor * build_norm(
|
|
ggml_tensor * cur,
|
|
ggml_tensor * mw,
|
|
ggml_tensor * mb,
|
|
norm_type type,
|
|
float norm_eps,
|
|
int il) const;
|
|
|
|
ggml_tensor * build_ffn(
|
|
ggml_tensor * cur,
|
|
ggml_tensor * up,
|
|
ggml_tensor * up_b,
|
|
ggml_tensor * gate,
|
|
ggml_tensor * gate_b,
|
|
ggml_tensor * down,
|
|
ggml_tensor * down_b,
|
|
ffn_op_type type_op,
|
|
int il) const;
|
|
|
|
ggml_tensor * build_moe_ffn(
|
|
ggml_tensor * cur,
|
|
const mtmd_layer & layer,
|
|
ffn_op_type type_op,
|
|
int il) const;
|
|
|
|
ggml_tensor * build_attn(
|
|
ggml_tensor * wo,
|
|
ggml_tensor * wo_b,
|
|
ggml_tensor * q_cur,
|
|
ggml_tensor * k_cur,
|
|
ggml_tensor * v_cur,
|
|
ggml_tensor * kq_mask,
|
|
float kq_scale,
|
|
int il,
|
|
ggml_tensor * sinks = nullptr) const;
|
|
|
|
// implementation of the 2D RoPE using two ggml_rope_ext calls
|
|
//
|
|
// unlike GGML_ROPE_TYPE_VISION which forces NEOX ordering, this rotates adjacent pairs (normal ordering)
|
|
//
|
|
// example:
|
|
// given a single head with size = 8 --> [00000000]
|
|
// dims [0, 4) rotate with pos_a, dims [4, 8) rotate with pos_b --> [aaaabbbb]
|
|
// interleave_freq = false --> both halves use the same inv_freq set (like GGML_ROPE_TYPE_VISION)
|
|
// interleave_freq = true --> first half uses even inv_freq, second half uses odd inv_freq (used by pixtral)
|
|
ggml_tensor * build_rope_2d(
|
|
ggml_context * ctx0,
|
|
ggml_tensor * cur,
|
|
ggml_tensor * pos_a, // first half
|
|
ggml_tensor * pos_b, // second half
|
|
const float freq_base,
|
|
const bool interleave_freq
|
|
);
|
|
|
|
// aka pixel_shuffle / pixel_unshuffle / patch_merger (Kimi-VL)
|
|
// support dynamic resolution
|
|
ggml_tensor * build_patch_merge_permute(ggml_tensor * cur, int scale_factor);
|
|
|
|
// Generic function to stack frames for audio processing
|
|
// Abstracts out the StackAudioFrames logic used by ultravox
|
|
ggml_tensor * build_stack(ggml_tensor * cur, int32_t stack_factor, int32_t n_embed);
|
|
|
|
// append the separators of img.suffix_type after the image tokens
|
|
ggml_tensor * build_suffix(ggml_tensor * cur);
|
|
};
|
|
|
|
static inline std::string get_attn_mask_name(int idx) {
|
|
return idx == 0 ? "attn_mask" : "attn_mask_" + std::to_string(idx);
|
|
}
|
|
|
|
// create the graph builder of the subcontext's projector, params is only needed by encode()
|
|
std::unique_ptr<mtmd_graph> mtmd_get_graph_builder(mtmd_subcontext * ctx, const mtmd_image_f32_batch & imgs,
|
|
const mtmd_subctx_encode_params * params = nullptr);
|