struct MetalKVCache
pub struct MetalKVCache {
pub max_seq int
pub pos int
}用途
承载 max_seq、pos 等属性的结构体。
公开字段
max_seqint- 字段(int)
posint- 字段(int)
#github.com/xulang-org/packages/llm_metal扩展包 `llm_metal #github.com/xulang-org/packages/llm_metal`,由模块 github.com/xulang-org/packages 提供。
github.com/xulang-org/packages · 1.17.2 · 安装与使用
struct MetalKVCachepub struct MetalKVCache {
pub max_seq int
pub pos int
}承载 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
}承载 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
}承载 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) MetalKVCache构造 kv cache。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelmax_seq_req intMetalKVCache — 该类型的值
($&MetalKVCache) resetpub fn (kv $&MetalKVCache) reset()$&MetalKVCache 上的操作:清空内部状态,可再写入或读取。
kv $&MetalKVCacheMetalKVCache 实例;以独占借用传入,方法可修改它,调用方仍持有实例。不返回值。
($&MetalKVCache) closepub fn (kv $&MetalKVCache) close() int$&MetalKVCache 上的操作:关闭资源。
kv $&MetalKVCacheMetalKVCache 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
forward_token_argmaxpub fn forward_token_argmax(m &MetalModel, kv $&MetalKVCache, token int) int执行 forward token argmax 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelkv $&MetalKVCachetoken intint — 该类型的值
forward_token_logitspub fn forward_token_logits(m &MetalModel, kv $&MetalKVCache, token int) []f32执行 forward token logits 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelkv $&MetalKVCachetoken int[]f32 — 切片结果
prefill_tokenspub fn prefill_tokens(m &MetalModel, kv $&MetalKVCache, tokens &[]int) int执行 prefill tokens 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelkv $&MetalKVCachetokens &[]intint — 该类型的值
generatepub fn generate(m &MetalModel, prompt_ids &[]int, max_gen int) []int生成。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelprompt_ids &[]intmax_gen int[]int — 切片结果
decode_tokenspub fn decode_tokens(m &MetalModel, ids &[]int) string解码 tokens 数据。
m &MetalModelids &[]intstring — 结果字符串
new_paged_kv_cachepub fn new_paged_kv_cache(m &MetalModel, max_seq_req int, block_size_req int) MetalPagedKVCache构造 paged kv cache。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelmax_seq_req intblock_size_req intMetalPagedKVCache — 该类型的值
($&MetalPagedKVCache) closepub fn (kv $&MetalPagedKVCache) close() int$&MetalPagedKVCache 上的操作:关闭资源。
kv $&MetalPagedKVCacheMetalPagedKVCache 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
forward_token_paged_argmaxpub fn forward_token_paged_argmax(m &MetalModel, kv $&MetalPagedKVCache, token int) int执行 forward token paged argmax 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelkv $&MetalPagedKVCachetoken intint — 该类型的值
generate_pagedpub fn generate_paged(m &MetalModel, prompt_ids &[]int, max_gen int, block_size int) []int执行 generate paged 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelprompt_ids &[]intmax_gen intblock_size int[]int — 切片结果
new_lorapub fn new_lora(m &MetalModel, rank_req int, alpha_req f64) MetalLoRA构造 lora。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelrank_req intalpha_req f64MetalLoRA — 该类型的值
($&MetalLoRA) closepub fn (l $&MetalLoRA) close() int$&MetalLoRA 上的操作:关闭资源。
l $&MetalLoRAMetalLoRA 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值
forward_token_lora_argmaxpub fn forward_token_lora_argmax(m &MetalModel, kv $&MetalKVCache, lora &MetalLoRA, token int) int执行 forward token lora argmax 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModelkv $&MetalKVCachelora &MetalLoRAtoken intint — 该类型的值
train_lora_steppub fn train_lora_step(m &MetalModel, lora $&MetalLoRA, input_ids &[]int, target_ids &[]int, lr f64, weight_decay f64) f64执行 train lora step 操作。失败时抛出错误,调用方需处理或继续传播。
m &MetalModellora $&MetalLoRAinput_ids &[]inttarget_ids &[]intlr f64weight_decay f64f64 — 该类型的值
const WT_F32pub const WT_F32 = 0常量 WT_F32,定义了相关的协议或算法配置参数。
const WT_Q4_0pub const WT_Q4_0 = 2常量 WT_Q4_0,定义了相关的协议或算法配置参数。
const WT_Q5_0pub const WT_Q5_0 = 6常量 WT_Q5_0,定义了相关的协议或算法配置参数。
const WT_Q8_0pub const WT_Q8_0 = 8常量 WT_Q8_0,定义了相关的协议或算法配置参数。
const WT_Q4_Kpub const WT_Q4_K = 12常量 WT_Q4_K,定义了相关的协议或算法配置参数。
const WT_Q5_Kpub const WT_Q5_K = 13常量 WT_Q5_K,定义了相关的协议或算法配置参数。
const WT_Q6_Kpub const WT_Q6_K = 14常量 WT_Q6_K,定义了相关的协议或算法配置参数。
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 配置参数与选项集合。
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
}承载 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
}承载 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
}承载 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) MetalModel执行 load gguf 操作。失败时抛出错误,调用方需处理或继续传播。
path stringmax_seq_override intMetalModel — 该类型的值
load_safetensorspub fn load_safetensors(path string, max_seq_override int) MetalModel执行 load safetensors 操作。失败时抛出错误,调用方需处理或继续传播。
path stringmax_seq_override intMetalModel — 该类型的值
load_modelpub fn load_model(path string, max_seq_override int) MetalModel执行 load model 操作。失败时抛出错误,调用方需处理或继续传播。
path stringmax_seq_override intMetalModel — 该类型的值
($&MetalModel) closepub fn (m $&MetalModel) close() int$&MetalModel 上的操作:关闭资源。
m $&MetalModelMetalModel 实例;以独占借用传入,方法可修改它,调用方仍持有实例。int — 该类型的值