XU XUv0.16.0

#github.com/xulang-org/packages/llm_metal

import: llm_metal #github.com/xulang-org/packages/llm_metal

counts: 15 functions, 5 methods, 7 types, 7 constants, 0 method sets

External 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 MetalKVCache

XuAPI
pub struct MetalKVCache {
    pub max_seq int
    pub pos int
}

Purpose

Structure encapsulating max_seq, pos.

Public fields

max_seq int
field (int)
pos int
field (int)

struct MetalPagedKVCache

XuAPI
pub struct MetalPagedKVCache {
    pub max_seq int
    pub block_size int
    pub max_blocks int
    pub pos int
}

Purpose

Structure encapsulating max_seq, block_size, max_blocks, pos.

Public fields

max_seq int
field (int)
block_size int
field (int)
max_blocks int
field (int)
pos int
field (int)

struct MetalLoRA

XuAPI
pub struct MetalLoRA {
    pub rank int
    pub alpha f64
    pub scale f64
    pub step int
    pub a_mat metal.F32Matrix
    pub b_mat metal.F32Matrix
}

Purpose

Structure encapsulating rank, alpha, scale, step, a_mat.

Public fields

rank int
field (int)
alpha f64
field (f64)
scale f64
field (f64)
step int
field (int)
a_mat metal.F32Matrix
field (metal.F32Matrix)
b_mat metal.F32Matrix
field (metal.F32Matrix)

new_kv_cache

XuAPI
pub fn new_kv_cache(m &MetalModel, max_seq_req int) MetalKVCache

Purpose

Construct kv cache. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
max_seq_req int
max_seq_req (int)

Returns

MetalKVCache — a value of this type

($&MetalKVCache) reset

XuAPI
pub fn (kv $&MetalKVCache) reset()

Purpose

Operation on $&MetalKVCache: clear internal state so the value can be reused.

Parameters

kv $&MetalKVCache
The MetalKVCache instance used by this call (exclusive borrow).

Returns

This call does not return a value.

($&MetalKVCache) close

XuAPI
pub fn (kv $&MetalKVCache) close() int

Purpose

Operation on $&MetalKVCache: close the resource.

Parameters

kv $&MetalKVCache
The MetalKVCache instance used by this call (exclusive borrow).

Returns

int — a value of this type

forward_token_argmax

XuAPI
pub fn forward_token_argmax(m &MetalModel, kv $&MetalKVCache, token int) int

Purpose

Forward token argmax. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
kv $&MetalKVCache
kv (exclusive borrow; may modify the original; type $&MetalKVCache)
token int
token (int)

Returns

int — a value of this type

forward_token_logits

XuAPI
pub fn forward_token_logits(m &MetalModel, kv $&MetalKVCache, token int) []f32

Purpose

Forward token logits. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
kv $&MetalKVCache
kv (exclusive borrow; may modify the original; type $&MetalKVCache)
token int
token (int)

Returns

[]f32 — slice result

prefill_tokens

XuAPI
pub fn prefill_tokens(m &MetalModel, kv $&MetalKVCache, tokens &[]int) int

Purpose

Prefill tokens. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
kv $&MetalKVCache
kv (exclusive borrow; may modify the original; type $&MetalKVCache)
tokens &[]int
tokens (read-only borrow; caller keeps the original; type &[]int)

Returns

int — a value of this type

generate

XuAPI
pub fn generate(m &MetalModel, prompt_ids &[]int, max_gen int) []int

Purpose

Execute generate. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
prompt_ids &[]int
prompt_ids (read-only borrow; caller keeps the original; type &[]int)
max_gen int
max_gen (int)

Returns

[]int — slice result

decode_tokens

XuAPI
pub fn decode_tokens(m &MetalModel, ids &[]int) string

Purpose

Decode tokens data.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
ids &[]int
ids (read-only borrow; caller keeps the original; type &[]int)

Returns

string — result string

new_paged_kv_cache

XuAPI
pub fn new_paged_kv_cache(m &MetalModel, max_seq_req int, block_size_req int) MetalPagedKVCache

Purpose

Construct paged kv cache. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
max_seq_req int
max_seq_req (int)
block_size_req int
block_size_req (int)

Returns

MetalPagedKVCache — a value of this type

($&MetalPagedKVCache) close

XuAPI
pub fn (kv $&MetalPagedKVCache) close() int

Purpose

Operation on $&MetalPagedKVCache: close the resource.

Parameters

kv $&MetalPagedKVCache
The MetalPagedKVCache instance used by this call (exclusive borrow).

Returns

int — a value of this type

forward_token_paged_argmax

XuAPI
pub fn forward_token_paged_argmax(m &MetalModel, kv $&MetalPagedKVCache, token int) int

Purpose

Forward token paged argmax. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
kv $&MetalPagedKVCache
kv (exclusive borrow; may modify the original; type $&MetalPagedKVCache)
token int
token (int)

Returns

int — a value of this type

generate_paged

XuAPI
pub fn generate_paged(m &MetalModel, prompt_ids &[]int, max_gen int, block_size int) []int

Purpose

Generate paged. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
prompt_ids &[]int
prompt_ids (read-only borrow; caller keeps the original; type &[]int)
max_gen int
max_gen (int)
block_size int
block_size (int)

Returns

[]int — slice result

new_lora

XuAPI
pub fn new_lora(m &MetalModel, rank_req int, alpha_req f64) MetalLoRA

Purpose

Construct lora. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
rank_req int
rank_req (int)
alpha_req f64
alpha_req (f64)

Returns

MetalLoRA — a value of this type

($&MetalLoRA) close

XuAPI
pub fn (l $&MetalLoRA) close() int

Purpose

Operation on $&MetalLoRA: close the resource.

Parameters

l $&MetalLoRA
The MetalLoRA instance used by this call (exclusive borrow).

Returns

int — a value of this type

forward_token_lora_argmax

XuAPI
pub fn forward_token_lora_argmax(m &MetalModel, kv $&MetalKVCache, lora &MetalLoRA, token int) int

Purpose

Forward token lora argmax. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
kv $&MetalKVCache
kv (exclusive borrow; may modify the original; type $&MetalKVCache)
lora &MetalLoRA
lora (read-only borrow; caller keeps the original; type &MetalLoRA)
token int
token (int)

Returns

int — a value of this type

train_lora_step

XuAPI
pub fn train_lora_step(m &MetalModel, lora $&MetalLoRA, input_ids &[]int, target_ids &[]int, lr f64, weight_decay f64) f64

Purpose

Train lora step. Throws on failure; handle the error or propagate it to the caller.

Parameters

m &MetalModel
m (read-only borrow; caller keeps the original; type &MetalModel)
lora $&MetalLoRA
lora (exclusive borrow; may modify the original; type $&MetalLoRA)
input_ids &[]int
input_ids (read-only borrow; caller keeps the original; type &[]int)
target_ids &[]int
target_ids (read-only borrow; caller keeps the original; type &[]int)
lr f64
lr (f64)
weight_decay f64
weight_decay (f64)

Returns

f64 — a value of this type

const WT_F32

XuAPI
pub const WT_F32 = 0

Purpose

Constant WT_F32, defining protocol or algorithm parameters.

const WT_Q4_0

XuAPI
pub const WT_Q4_0 = 2

Purpose

Constant WT_Q4_0, defining protocol or algorithm parameters.

const WT_Q5_0

XuAPI
pub const WT_Q5_0 = 6

Purpose

Constant WT_Q5_0, defining protocol or algorithm parameters.

const WT_Q8_0

XuAPI
pub const WT_Q8_0 = 8

Purpose

Constant WT_Q8_0, defining protocol or algorithm parameters.

const WT_Q4_K

XuAPI
pub const WT_Q4_K = 12

Purpose

Constant WT_Q4_K, defining protocol or algorithm parameters.

const WT_Q5_K

XuAPI
pub const WT_Q5_K = 13

Purpose

Constant WT_Q5_K, defining protocol or algorithm parameters.

const WT_Q6_K

XuAPI
pub const WT_Q6_K = 14

Purpose

Constant WT_Q6_K, defining protocol or algorithm parameters.

struct MetalConfig

XuAPI
pub 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
}

Purpose

Llm_metal Configuration parameters and options set.

Public fields

arch string
field (string)
dim int
field (int)
hidden_dim int
field (int)
n_layers int
field (int)
n_heads int
field (int)
n_kv_heads int
field (int)
vocab_size int
field (int)
max_seq_len int
field (int)
head_dim int
field (int)
kv_dim int
field (int)
rope_theta f64
field (f64)
rope_interleaved bool
field (bool)
eps f64
field (f64)
tied_output bool
field (bool)
bos_id int
field (int)
eos_id int
field (int)

struct GpuWeightRef

XuAPI
pub struct GpuWeightRef {
    pub kind int
    pub idx int
    pub rows int
    pub cols int
}

Purpose

Structure encapsulating kind, idx, rows, cols.

Public fields

kind int
kind tag
idx int
field (int)
rows int
field (int)
cols int
field (int)

struct MetalLayerWeights

XuAPI
pub 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
}

Purpose

Structure encapsulating attn_norm_idx, wq, wk, wv, wo.

Public fields

attn_norm_idx int
field (int)
wq GpuWeightRef
field (GpuWeightRef)
wk GpuWeightRef
field (GpuWeightRef)
wv GpuWeightRef
field (GpuWeightRef)
wo GpuWeightRef
field (GpuWeightRef)
bq_idx int
field (int)
bk_idx int
field (int)
bv_idx int
field (int)
q_norm_idx int
field (int)
k_norm_idx int
field (int)
ffn_norm_idx int
field (int)
wg GpuWeightRef
field (GpuWeightRef)
wu GpuWeightRef
field (GpuWeightRef)
wd GpuWeightRef
field (GpuWeightRef)

struct MetalModel

XuAPI
pub 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
}

Purpose

Structure encapsulating dev, cfg, vocab, embed_table, norm_idx.

Public fields

dev metal.Device
field (metal.Device)
cfg MetalConfig
field (MetalConfig)
vocab []string
field ([]string)
embed_table metal.F32Matrix
field (metal.F32Matrix)
norm_idx int
field (int)
lm_head GpuWeightRef
field (GpuWeightRef)
layers []MetalLayerWeights
field ([]MetalLayerWeights)
f32_mats []metal.F32Matrix
field ([]metal.F32Matrix)
q4_mats []metal.Q4Matrix
field ([]metal.Q4Matrix)
q5_mats []metal.Q5Matrix
field ([]metal.Q5Matrix)
q8_mats []metal.Q8Matrix
field ([]metal.Q8Matrix)
q4k_mats []metal.Q4KMatrix
field ([]metal.Q4KMatrix)
q5k_mats []metal.Q5KMatrix
field ([]metal.Q5KMatrix)
q6k_mats []metal.Q6KMatrix
field ([]metal.Q6KMatrix)
closed bool
field (bool)

load_gguf

XuAPI
pub fn load_gguf(path string, max_seq_override int) MetalModel

Purpose

Load gguf. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
max_seq_override int
max_seq_override (int)

Returns

MetalModel — a value of this type

load_safetensors

XuAPI
pub fn load_safetensors(path string, max_seq_override int) MetalModel

Purpose

Load safetensors. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
max_seq_override int
max_seq_override (int)

Returns

MetalModel — a value of this type

load_model

XuAPI
pub fn load_model(path string, max_seq_override int) MetalModel

Purpose

Load model. Throws on failure; handle the error or propagate it to the caller.

Parameters

path string
filesystem path (string)
max_seq_override int
max_seq_override (int)

Returns

MetalModel — a value of this type

($&MetalModel) close

XuAPI
pub fn (m $&MetalModel) close() int

Purpose

Operation on $&MetalModel: close the resource.

Parameters

m $&MetalModel
The MetalModel instance used by this call (exclusive borrow).

Returns

int — a value of this type