struct MetalKVCache
pub struct MetalKVCache {
pub max_seq int
pub pos int
}Purpose
Structure encapsulating max_seq, pos.
Public fields
max_seqint- field (int)
posint- field (int)
#github.com/xulang-org/packages/llm_metalExternal package `llm_metal #github.com/xulang-org/packages/llm_metal`, provided by github.com/xulang-org/packages.
github.com/xulang-org/packages · 1.17.2 · Install and use
struct MetalKVCachepub struct MetalKVCache {
pub max_seq int
pub pos int
}Structure encapsulating max_seq, pos.
max_seq intpos intstruct MetalPagedKVCachepub struct MetalPagedKVCache {
pub max_seq int
pub block_size int
pub max_blocks int
pub pos int
}Structure encapsulating max_seq, block_size, max_blocks, pos.
max_seq intblock_size intmax_blocks intpos intstruct MetalLoRApub struct MetalLoRA {
pub rank int
pub alpha f64
pub scale f64
pub step int
pub a_mat metal.F32Matrix
pub b_mat metal.F32Matrix
}Structure encapsulating rank, alpha, scale, step, a_mat.
rank intalpha f64scale f64step inta_mat metal.F32Matrixb_mat metal.F32Matrixnew_kv_cachepub fn new_kv_cache(m &MetalModel, max_seq_req int) MetalKVCacheConstruct kv cache. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelmax_seq_req intMetalKVCache — a value of this type
($&MetalKVCache) resetpub fn (kv $&MetalKVCache) reset()Operation on $&MetalKVCache: clear internal state so the value can be reused.
kv $&MetalKVCacheMetalKVCache instance used by this call (exclusive borrow).This call does not return a value.
($&MetalKVCache) closepub fn (kv $&MetalKVCache) close() intOperation on $&MetalKVCache: close the resource.
kv $&MetalKVCacheMetalKVCache instance used by this call (exclusive borrow).int — a value of this type
forward_token_argmaxpub fn forward_token_argmax(m &MetalModel, kv $&MetalKVCache, token int) intForward token argmax. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelkv $&MetalKVCachetoken intint — a value of this type
forward_token_logitspub fn forward_token_logits(m &MetalModel, kv $&MetalKVCache, token int) []f32Forward token logits. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelkv $&MetalKVCachetoken int[]f32 — slice result
prefill_tokenspub fn prefill_tokens(m &MetalModel, kv $&MetalKVCache, tokens &[]int) intPrefill tokens. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelkv $&MetalKVCachetokens &[]intint — a value of this type
generatepub fn generate(m &MetalModel, prompt_ids &[]int, max_gen int) []intExecute generate. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelprompt_ids &[]intmax_gen int[]int — slice result
decode_tokenspub fn decode_tokens(m &MetalModel, ids &[]int) stringDecode tokens data.
m &MetalModelids &[]intstring — result string
new_paged_kv_cachepub fn new_paged_kv_cache(m &MetalModel, max_seq_req int, block_size_req int) MetalPagedKVCacheConstruct paged kv cache. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelmax_seq_req intblock_size_req intMetalPagedKVCache — a value of this type
($&MetalPagedKVCache) closepub fn (kv $&MetalPagedKVCache) close() intOperation on $&MetalPagedKVCache: close the resource.
kv $&MetalPagedKVCacheMetalPagedKVCache instance used by this call (exclusive borrow).int — a value of this type
forward_token_paged_argmaxpub fn forward_token_paged_argmax(m &MetalModel, kv $&MetalPagedKVCache, token int) intForward token paged argmax. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelkv $&MetalPagedKVCachetoken intint — a value of this type
generate_pagedpub fn generate_paged(m &MetalModel, prompt_ids &[]int, max_gen int, block_size int) []intGenerate paged. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelprompt_ids &[]intmax_gen intblock_size int[]int — slice result
new_lorapub fn new_lora(m &MetalModel, rank_req int, alpha_req f64) MetalLoRAConstruct lora. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelrank_req intalpha_req f64MetalLoRA — a value of this type
($&MetalLoRA) closepub fn (l $&MetalLoRA) close() intOperation on $&MetalLoRA: close the resource.
l $&MetalLoRAMetalLoRA instance used by this call (exclusive borrow).int — a value of this type
forward_token_lora_argmaxpub fn forward_token_lora_argmax(m &MetalModel, kv $&MetalKVCache, lora &MetalLoRA, token int) intForward token lora argmax. Throws on failure; handle the error or propagate it to the caller.
m &MetalModelkv $&MetalKVCachelora &MetalLoRAtoken intint — a value of this type
train_lora_steppub fn train_lora_step(m &MetalModel, lora $&MetalLoRA, input_ids &[]int, target_ids &[]int, lr f64, weight_decay f64) f64Train lora step. Throws on failure; handle the error or propagate it to the caller.
m &MetalModellora $&MetalLoRAinput_ids &[]inttarget_ids &[]intlr f64weight_decay f64f64 — a value of this type
const WT_F32pub const WT_F32 = 0Constant WT_F32, defining protocol or algorithm parameters.
const WT_Q4_0pub const WT_Q4_0 = 2Constant WT_Q4_0, defining protocol or algorithm parameters.
const WT_Q5_0pub const WT_Q5_0 = 6Constant WT_Q5_0, defining protocol or algorithm parameters.
const WT_Q8_0pub const WT_Q8_0 = 8Constant WT_Q8_0, defining protocol or algorithm parameters.
const WT_Q4_Kpub const WT_Q4_K = 12Constant WT_Q4_K, defining protocol or algorithm parameters.
const WT_Q5_Kpub const WT_Q5_K = 13Constant WT_Q5_K, defining protocol or algorithm parameters.
const WT_Q6_Kpub const WT_Q6_K = 14Constant WT_Q6_K, defining protocol or algorithm parameters.
struct MetalConfigpub struct MetalConfig {
pub arch string
pub dim int
pub hidden_dim int
pub n_layers int
pub n_heads int
pub n_kv_heads int
pub vocab_size int
pub max_seq_len int
pub head_dim int
pub kv_dim int
pub rope_theta f64
pub rope_interleaved bool
pub eps f64
pub tied_output bool
pub bos_id int
pub eos_id int
}Llm_metal Configuration parameters and options set.
arch stringdim inthidden_dim intn_layers intn_heads intn_kv_heads intvocab_size intmax_seq_len inthead_dim intkv_dim intrope_theta f64rope_interleaved booleps f64tied_output boolbos_id inteos_id intstruct GpuWeightRefpub struct GpuWeightRef {
pub kind int
pub idx int
pub rows int
pub cols int
}Structure encapsulating kind, idx, rows, cols.
kind intidx introws intcols intstruct MetalLayerWeightspub struct MetalLayerWeights {
pub attn_norm_idx int
pub wq GpuWeightRef
pub wk GpuWeightRef
pub wv GpuWeightRef
pub wo GpuWeightRef
pub bq_idx int
pub bk_idx int
pub bv_idx int
pub q_norm_idx int
pub k_norm_idx int
pub ffn_norm_idx int
pub wg GpuWeightRef
pub wu GpuWeightRef
pub wd GpuWeightRef
}Structure encapsulating attn_norm_idx, wq, wk, wv, wo.
attn_norm_idx intwq GpuWeightRefwk GpuWeightRefwv GpuWeightRefwo GpuWeightRefbq_idx intbk_idx intbv_idx intq_norm_idx intk_norm_idx intffn_norm_idx intwg GpuWeightRefwu GpuWeightRefwd GpuWeightRefstruct MetalModelpub struct MetalModel {
pub dev metal.Device
pub cfg MetalConfig
pub vocab []string
pub embed_table metal.F32Matrix
pub norm_idx int
pub lm_head GpuWeightRef
pub layers []MetalLayerWeights
pub f32_mats []metal.F32Matrix
pub q4_mats []metal.Q4Matrix
pub q5_mats []metal.Q5Matrix
pub q8_mats []metal.Q8Matrix
pub q4k_mats []metal.Q4KMatrix
pub q5k_mats []metal.Q5KMatrix
pub q6k_mats []metal.Q6KMatrix
pub closed bool
}Structure encapsulating dev, cfg, vocab, embed_table, norm_idx.
dev metal.Devicecfg MetalConfigvocab []stringembed_table metal.F32Matrixnorm_idx intlm_head GpuWeightReflayers []MetalLayerWeightsf32_mats []metal.F32Matrixq4_mats []metal.Q4Matrixq5_mats []metal.Q5Matrixq8_mats []metal.Q8Matrixq4k_mats []metal.Q4KMatrixq5k_mats []metal.Q5KMatrixq6k_mats []metal.Q6KMatrixclosed boolload_ggufpub fn load_gguf(path string, max_seq_override int) MetalModelLoad gguf. Throws on failure; handle the error or propagate it to the caller.
path stringmax_seq_override intMetalModel — a value of this type
load_safetensorspub fn load_safetensors(path string, max_seq_override int) MetalModelLoad safetensors. Throws on failure; handle the error or propagate it to the caller.
path stringmax_seq_override intMetalModel — a value of this type
load_modelpub fn load_model(path string, max_seq_override int) MetalModelLoad model. Throws on failure; handle the error or propagate it to the caller.
path stringmax_seq_override intMetalModel — a value of this type
($&MetalModel) closepub fn (m $&MetalModel) close() intOperation on $&MetalModel: close the resource.
m $&MetalModelMetalModel instance used by this call (exclusive borrow).int — a value of this type