Skip to content

Alusus/Llama

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Llama

[العربية]

Alusus language bindings for llama.cpp, providing a complete interface for running LLM inference locally. This library supports both CPU and Vulkan GPU backends.

Installation

import "Apm";
Apm.importPackage("Alusus/Llama@0.2");

Example

import "Srl/Console";
import "Apm";
Apm.importPackage("Alusus/Llama@0.2");
use Srl;
use Llama;

// Load backends
Ggml.Backend.cpuLoad();
Ggml.Backend.vkLoad();  // Optional: for Vulkan GPU support

// Load model
def model: ref[Model](Model.load("model.gguf", Model.getDefaultParams()));
def ctx: ref[Context](Context.initFromModel(model, Context.getDefaultParams()));

// Tokenize input
def tokens: array[Token, 512];
def nTokens: Int = tokenize(model.vocab, "Hello", 5, tokens, 512, true, true);

// Decode and generate
def batch: Batch = Batch.getOne(tokens, nTokens);
ctx.decode(batch);

// Sample next token
def sampler: ref[Sampler](Sampler.initGreedy());
def nextToken: Token = sampler.sample(ctx, -1);

// Cleanup
Sampler.free(sampler);
Context.free(ctx);
Model.free(model);

GPU Support

Set the GGML_USE_VULKAN environment variable to 1 before running to enable Vulkan GPU acceleration:

GGML_USE_VULKAN=1 alusus your_script.alusus

API Reference

Basic Types

  • Token: Int - Token ID (alias for llama_token in llama.cpp)
  • Pos: Int - Token position (alias for llama_pos in llama.cpp)
  • SeqId: Int - Sequence ID (alias for llama_seq_id in llama.cpp)
  • ProgressCallback: ptr[function (progress: Float, userData: ptr): Bool] - Progress callback function pointer (alias for llama_progress_callback in llama.cpp)

Constants

  • DEFAULT_SEED (0xFFFFFFFF): Default random seed (equivalent to LLAMA_DEFAULT_SEED in llama.cpp)
  • TOKEN_NULL (-1): Null token value (equivalent to LLAMA_TOKEN_NULL in llama.cpp)

Enums

SplitMode

Controls how model layers are distributed across GPUs.

  • NONE: Single GPU only (equivalent to LLAMA_SPLIT_MODE_NONE in llama.cpp)
  • LAYER: Split layers and KV cache across GPUs (equivalent to LLAMA_SPLIT_MODE_LAYER in llama.cpp)
  • ROW: Split with tensor parallelism if supported (equivalent to LLAMA_SPLIT_MODE_ROW in llama.cpp)

RopeScalingType

RoPE (Rotary Position Embedding) scaling type.

  • UNSPECIFIED: Not specified (equivalent to LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED in llama.cpp)
  • NONE: No scaling (equivalent to LLAMA_ROPE_SCALING_TYPE_NONE in llama.cpp)
  • LINEAR: Linear scaling (equivalent to LLAMA_ROPE_SCALING_TYPE_LINEAR in llama.cpp)
  • YARN: YaRN scaling (equivalent to LLAMA_ROPE_SCALING_TYPE_YARN in llama.cpp)
  • LONGROPE: LongRoPE scaling (equivalent to LLAMA_ROPE_SCALING_TYPE_LONGROPE in llama.cpp)

PoolingType

Pooling type for embeddings.

  • UNSPECIFIED: Not specified (LLAMA_POOLING_TYPE_UNSPECIFIED in llama.cpp)
  • NONE: No pooling (LLAMA_POOLING_TYPE_NONE in llama.cpp)
  • MEAN: Mean pooling (LLAMA_POOLING_TYPE_MEAN in llama.cpp)
  • CLS: CLS token pooling (LLAMA_POOLING_TYPE_CLS in llama.cpp)
  • LAST: Last token pooling (LLAMA_POOLING_TYPE_LAST in llama.cpp)
  • RANK: Rank pooling (LLAMA_POOLING_TYPE_RANK in llama.cpp)

AttentionType

Attention mechanism type.

  • UNSPECIFIED: Not specified (LLAMA_ATTENTION_TYPE_UNSPECIFIED in llama.cpp)
  • CAUSAL: Causal attention - autoregressive (LLAMA_ATTENTION_TYPE_CAUSAL in llama.cpp)
  • NON_CAUSAL: Non-causal attention (LLAMA_ATTENTION_TYPE_NON_CAUSAL in llama.cpp)

FlashAttnType

Flash attention configuration.

  • AUTO (-1): Automatic selection
  • DISABLED (0): Flash attention disabled
  • ENABLED (1): Flash attention enabled

Data Structures

TokenData

Token data with probability information. Maps to llama_token_data.

  • id (Token): Token ID. Maps to id.
  • logit (Float): Log probability. Maps to logit.
  • p (Float): Probability. Maps to p.

TokenDataArray

Array of token data for sampling. Maps to llama_token_data_array.

  • data (ptr[TokenData]): Pointer to token data array. Maps to data.
  • size (ArchWord): Number of elements. Maps to size.
  • selected (Int[64]): Selected token index. Maps to selected.
  • sorted (Bool): Whether array is sorted. Maps to sorted.

ChatMessage

Chat message structure for chat templates. Maps to llama_chat_message.

  • role (CharsPtr): Message role ("user", "assistant", "system"). Maps to role.
  • content (CharsPtr): Message content. Maps to content.

PerfContextData

Performance data for context operations. Maps to llama_perf_context_data.

  • tStartMs (Float[64]): Start time in milliseconds. Maps to t_start_ms.
  • tLoadMs (Float[64]): Load time in milliseconds. Maps to t_load_ms.
  • tPEvalMs (Float[64]): Prompt evaluation time. Maps to t_p_eval_ms.
  • tEvalMs (Float[64]): Token evaluation time. Maps to t_eval_ms.
  • nPEval (Int): Number of prompt evaluations. Maps to n_p_eval.
  • nEval (Int): Number of token evaluations. Maps to n_eval.
  • nReused (Int): Number of reused evaluations. Maps to n_reused.

PerfSamplerData

Performance data for sampler operations. Maps to llama_perf_sampler_data.

  • tSampleMs (Float[64]): Sampling time in milliseconds. Maps to t_sample_ms.
  • nSample (Int): Number of samples. Maps to n_sample.

Classes

Model

Represents a loaded LLM model. Maps to llama_model.

Model.Params

Model loading parameters. Maps to llama_model_params.

  • devices (ptr): Device list. Maps to devices.
  • tensorBuftOverrides (ref[array[TensorBuftOverride]]): Tensor buffer type overrides. Maps to tensor_buft_overrides.
  • nGpuLayers (Int): Number of layers to offload to GPU. Maps to n_gpu_layers.
  • splitMode (SplitMode): GPU split mode. Maps to split_mode.
  • mainGpu (Int): Main GPU index. Maps to main_gpu.
  • tensorSplit (ref[array[Float]]): Tensor split ratios. Maps to tensor_split.
  • progressCallback (ProgressCallback): Progress callback function. Maps to progress_callback.
  • progressCallbackUserData (ptr): User data for callback. Maps to progress_callback_user_data.
  • kvOverrides (ptr): KV cache overrides. Maps to kv_overrides.
  • vocabOnly (Bool): Load vocabulary only. Maps to vocab_only.
  • useMmap (Bool): Use memory mapping. Maps to use_mmap.
  • useDirectIo (Bool): Use direct I/O. Maps to use_direct_io.
  • useMlock (Bool): Lock memory. Maps to use_mlock.
  • checkTensors (Bool): Validate tensor data. Maps to check_tensors.
  • useExtraBufts (Bool): Use extra buffer types. Maps to use_extra_bufts.
  • noHost (Bool): No host buffer allocation. Maps to no_host.
  • noAlloc (Bool): No memory allocation. Maps to no_alloc.

getDefaultParams

func Model.getDefaultParams(): Params

Get default model parameters. Maps to llama_model_default_params.

load

func Model.load(path: CharsPtr, params: Params): ref[Model]

Load model from file. Maps to llama_model_load_from_file.

loadSplits

func Model.loadSplits(paths: ref[array[CharsPtr]], count: Word, params: Params): ref[Model]

Load model from split files. Maps to llama_model_load_from_splits.

free

func Model.free(model: ref[Model])

Free model resources. Maps to llama_model_free.

save

func model.save(path: CharsPtr)

Save model to file. Maps to llama_model_save_to_file.

hasEncoder

model.hasEncoder: Bool

Check if model has encoder. Maps to llama_model_has_encoder.

hasDecoder

model.hasDecoder: Bool

Check if model has decoder. Maps to llama_model_has_decoder.

nCtxTrain

model.nCtxTrain: Int

Training context size. Maps to llama_model_n_ctx_train.

nEmbd

model.nEmbd: Int

Embedding dimension. Maps to llama_model_n_embd.

nLayer

model.nLayer: Int

Number of layers. Maps to llama_model_n_layer.

nHead

model.nHead: Int

Number of attention heads. Maps to llama_model_n_head.

vocab

model.vocab: ref[Vocab]

Get vocabulary. Maps to llama_model_get_vocab.

metaValStr

func model.metaValStr(key: CharsPtr, buf: CharsPtr, bufSize: ArchWord): Int

Get metadata value as string. Maps to llama_model_meta_val_str.

metaCount

model.metaCount: Int

Get metadata count. Maps to llama_model_meta_count.

metaKeyByIndex

func model.metaKeyByIndex(idx: Int, buf: CharsPtr, bufSize: ArchWord): Int

Get metadata key by index. Maps to llama_model_meta_key_by_index.

Context

Inference context for a model. Maps to llama_context.

Context.Params

Context creation parameters. Maps to llama_context_params.

  • nCtx (Word): Context size (0 = use model default). Maps to n_ctx.
  • nBatch (Word): Logical batch size. Maps to n_batch.
  • nUbatch (Word): Physical batch size. Maps to n_ubatch.
  • nSeqMax (Word): Maximum sequences. Maps to n_seq_max.
  • nThreads (Int): Number of threads for generation. Maps to n_threads.
  • nThreadsBatch (Int): Number of threads for batch processing. Maps to n_threads_batch.
  • ropeScalingType (RopeScalingType): RoPE scaling type. Maps to rope_scaling_type.
  • poolingType (PoolingType): Pooling type. Maps to pooling_type.
  • attentionType (AttentionType): Attention type. Maps to attention_type.
  • flashAttnType (FlashAttnType): Flash attention setting. Maps to flash_attn.
  • ropeFreqBase (Float): RoPE base frequency. Maps to rope_freq_base.
  • ropeFreqScale (Float): RoPE frequency scale. Maps to rope_freq_scale.
  • yarnExtFactor (Float): YaRN extrapolation factor. Maps to yarn_ext_factor.
  • yarnAttnFactor (Float): YaRN attention factor. Maps to yarn_attn_factor.
  • yarnBetaFast (Float): YaRN beta fast. Maps to yarn_beta_fast.
  • yarnBetaSlow (Float): YaRN beta slow. Maps to yarn_beta_slow.
  • yarnOrigCtx (Word): YaRN original context size. Maps to yarn_orig_ctx.
  • defragThold (Float): Defragmentation threshold. Maps to defrag_thold.
  • cbEval (function ptr): Evaluation callback. Maps to cb_eval.
  • cbEvalData (ptr): Evaluation callback user data. Maps to cb_eval_user_data.
  • typeK (Ggml.Type): K cache data type. Maps to type_k.
  • typeV (Ggml.Type): V cache data type. Maps to type_v.
  • abortCb (function ptr): Abort callback. Maps to abort_callback.
  • abortCbData (ptr): Abort callback user data. Maps to abort_callback_data.
  • embeddings (Bool): Enable embeddings output. Maps to embeddings.
  • offloadKqv (Bool): Offload KQV to GPU. Maps to offload_kqv.
  • noPerf (Bool): Disable performance counters. Maps to no_perf.
  • opOffload (Bool): Enable operation offloading. Maps to op_offload.
  • swaFull (Bool): Full sliding window attention. Maps to swa_full.
  • kvUnified (Bool): Unified KV cache. Maps to kv_unified.
  • samplers (ref[array[SamplerSeqConfig]]): Per-sequence samplers. Maps to samplers.
  • nSamplers (ArchWord): Number of samplers. Maps to n_samplers.

getDefaultParams

func Context.getDefaultParams(): Params

Get default context parameters. Maps to llama_context_default_params.

initFromModel

func Context.initFromModel(model: ref[Model], params: Params): ref[Context]

Create context from model. Maps to llama_init_from_model.

free

func Context.free(ctx: ref[Context])

Free context resources. Maps to llama_free.

attachThreadpool

func ctx.attachThreadpool(tp: ref[Ggml.Threadpool], tpBatch: ref[Ggml.Threadpool])

Attach thread pool. Maps to llama_attach_threadpool.

detachThreadpool

func ctx.detachThreadpool()

Detach thread pool. Maps to llama_detach_threadpool.

getEmbeddingsSeq

func ctx.getEmbeddingsSeq(seqId: SeqId): ref[array[Float]]

Get sequence embeddings. Maps to llama_get_embeddings_seq.

getModel

func ctx.getModel(): ref[Model]

Get associated model. Maps to llama_get_model.

nCtx

ctx.nCtx: Word

Get context size. Maps to llama_n_ctx.

nBatch

ctx.nBatch: Word

Get logical batch size. Maps to llama_n_batch.

nUbatch

ctx.nUbatch: Word

Get physical batch size. Maps to llama_n_ubatch.

nSeqMax

ctx.nSeqMax: Word

Get max sequences. Maps to llama_n_seq_max.

getMemory

func ctx.getMemory(): ptr[Memory]

Get KV cache memory. Maps to llama_get_memory.

poolingType

ctx.poolingType: PoolingType

Get pooling type. Maps to llama_pooling_type.

encode

func ctx.encode(batch: Batch): Int

Encode batch (for encoder models). Maps to llama_encode.

decode

func ctx.decode(batch: Batch): Int

Decode batch. Maps to llama_decode.

getLogitsIth

func ctx.getLogitsIth(i: Int): ref[array[Float]]

Get logits for token at index. Maps to llama_get_logits_ith.

getEmbeddingsIth

func ctx.getEmbeddingsIth(i: Int): ref[array[Float]]

Get embeddings for token at index. Maps to llama_get_embeddings_ith.

stateSize

ctx.stateSize: ArchWord

Get state size in bytes. Maps to llama_state_get_size.

stateGetData

func ctx.stateGetData(dst: ref[array[Word[8]]], size: ArchWord): ArchWord

Get state data. Maps to llama_state_get_data.

stateSetData

func ctx.stateSetData(src: ref[array[Word[8]]], size: ArchWord): ArchWord

Set state data. Maps to llama_state_set_data.

stateLoadFile

func ctx.stateLoadFile(path: CharsPtr, tokensOut: ref[array[Token]], cap: ArchWord, outCount: ref[ArchWord]): Bool

Load state from file. Maps to llama_state_load_file.

stateSaveFile

func ctx.stateSaveFile(path: CharsPtr, tokens: ref[array[Token]], count: ArchWord): Bool

Save state to file. Maps to llama_state_save_file.

setAdapterLora

func ctx.setAdapterLora(ad: ref[Adapter], scale: Float): Int

Apply LoRA adapter. Maps to llama_set_adapter_lora.

removeAdapterLora

func ctx.removeAdapterLora(ad: ref[Adapter]): Int

Remove LoRA adapter. Maps to llama_rm_adapter_lora.

clearAdapterLora

func ctx.clearAdapterLora()

Clear all LoRA adapters. Maps to llama_clear_adapter_lora.

perfContext

ctx.perfContext: PerfContextData

Get performance data. Maps to llama_perf_context.

perfContextPrint

func ctx.perfContextPrint()

Print performance data. Maps to llama_perf_context_print.

perfContextReset

func ctx.perfContextReset()

Reset performance counters. Maps to llama_perf_context_reset.

Batch

Token batch for processing. Maps to llama_batch.

  • nTokens (Int): Number of tokens. Maps to n_tokens.
  • token (ref[array[Token]]): Token IDs. Maps to token.
  • embd (ref[array[Float]]): Embeddings (alternative to tokens). Maps to embd.
  • pos (ref[array[Pos]]): Token positions. Maps to pos.
  • nSeqId (ref[array[Int]]): Number of sequence IDs per token. Maps to n_seq_id.
  • seqId (ref[array[ref[array[SeqId]]]]): Sequence IDs. Maps to seq_id.
  • output (ref[array[Int[8]]]): Output flags. Maps to logits.

getOne

func Batch.getOne(tokens: ref[array[Token]], nTokens: Int): Batch

Create batch from token array. Maps to llama_batch_get_one.

init

func Batch.init(nTokens: Int, embd: Int, nSeqMax: Int): Batch

Initialize empty batch. Maps to llama_batch_init.

free

func Batch.free(batch: Batch)

Free batch resources. Maps to llama_batch_free.

Sampler

Token sampler for generation. Maps to llama_sampler.

Sampler.ChainParams

Sampler chain parameters. Maps to llama_sampler_chain_params.

  • noPerf (Bool): Disable performance counters. Maps to no_perf.

init

func Sampler.init(iface: ptr, ctx: ptr): ref[Sampler]

Initialize custom sampler. Maps to llama_sampler_init.

chainInit

func Sampler.chainInit(params: ChainParams): ref[Sampler]

Create sampler chain. Maps to llama_sampler_chain_init.

initGreedy

func Sampler.initGreedy(): ref[Sampler]

Create greedy sampler. Maps to llama_sampler_init_greedy.

initDist

func Sampler.initDist(seed: Word): ref[Sampler]

Create distribution sampler. Maps to llama_sampler_init_dist.

initTopK

func Sampler.initTopK(k: Int): ref[Sampler]

Create top-k sampler. Maps to llama_sampler_init_top_k.

initTopP

func Sampler.initTopP(p: Float, minKeep: ArchWord): ref[Sampler]

Create top-p (nucleus) sampler. Maps to llama_sampler_init_top_p.

initPenalties

func Sampler.initPenalties(penaltyLastN: Int, penaltyRepeat: Float, penaltyFreq: Float, penaltyPresent: Float): ref[Sampler]

Create penalty sampler. Maps to llama_sampler_init_penalties.

  • penaltyLastN: Last n tokens to penalize (0 = disable, -1 = context size)
  • penaltyRepeat: Repeat penalty (1.0 = disabled)
  • penaltyFreq: Frequency penalty (0.0 = disabled)
  • penaltyPresent: Presence penalty (0.0 = disabled)

clone

func Sampler.clone(s: ref[Sampler]): ref[Sampler]

Clone sampler. Maps to llama_sampler_clone.

free

func Sampler.free(s: ref[Sampler])

Free sampler resources. Maps to llama_sampler_free.

reset

func sampler.reset()

Reset sampler state. Maps to llama_sampler_reset.

sample

func sampler.sample(ctx: ref[Context], idx: Int): Token

Sample next token. Maps to llama_sampler_sample.

name

sampler.name: CharsPtr

Get sampler name. Maps to llama_sampler_name.

accept

func sampler.accept(token: Token)

Accept token for tracking. Maps to llama_sampler_accept.

apply

func sampler.apply(cand: ref[TokenDataArray])

Apply sampler to candidates. Maps to llama_sampler_apply.

chainAdd

func sampler.chainAdd(s: ref[Sampler])

Add sampler to chain. Maps to llama_sampler_chain_add.

chainGet

func sampler.chainGet(idx: Int): ref[Sampler]

Get sampler from chain. Maps to llama_sampler_chain_get.

chainCount

sampler.chainCount: Int

Get chain length. Maps to llama_sampler_chain_n.

chainRemove

func sampler.chainRemove(idx: Int): ref[Sampler]

Remove sampler from chain. Maps to llama_sampler_chain_remove.

perfSampler

sampler.perfSampler: PerfSamplerData

Get performance data. Maps to llama_perf_sampler.

perfSamplerPrint

func sampler.perfSamplerPrint()

Print performance data. Maps to llama_perf_sampler_print.

perfSamplerReset

func sampler.perfSamplerReset()

Reset performance counters. Maps to llama_perf_sampler_reset.

Vocab

Model vocabulary. Maps to llama_vocab.

tokenToPiece

func vocab.tokenToPiece(token: Token, buf: CharsPtr, length: Int, lstrip: Int, special: Bool): Int

Convert token to text. Maps to llama_token_to_piece.

eos

vocab.eos: Token

Get end-of-sequence token. Maps to llama_vocab_eos.

isEog

func vocab.isEog(token: Token): Bool

Check if token is end-of-generation. Maps to llama_vocab_is_eog.

Memory

KV cache memory management. Maps to llama_memory.

clear

func memory.clear(data: Bool)

Clear KV cache. Maps to llama_memory_clear.

seqRemove

func memory.seqRemove(seq: SeqId, p0: Pos, p1: Pos): Bool

Remove sequence range. Maps to llama_memory_seq_rm.

seqCopy

func memory.seqCopy(src: SeqId, dst: SeqId, p0: Pos, p1: Pos)

Copy sequence range. Maps to llama_memory_seq_cp.

seqKeep

func memory.seqKeep(seq: SeqId)

Keep only specified sequence. Maps to llama_memory_seq_keep.

seqAdd

func memory.seqAdd(seq: SeqId, p0: Pos, p1: Pos, delta: Pos)

Add position delta to sequence. Maps to llama_memory_seq_add.

seqDiv

func memory.seqDiv(seq: SeqId, p0: Pos, p1: Pos, d: Int)

Divide positions in sequence. Maps to llama_memory_seq_div.

Adapter

LoRA adapter support. Maps to llama_adapter_lora.

loraInit

func Adapter.loraInit(model: ref[Model], path: CharsPtr): ref[Adapter]

Load LoRA adapter. Maps to llama_adapter_lora_init.

loraFree

func Adapter.loraFree(ad: ref[Adapter])

Free LoRA adapter. Maps to llama_adapter_lora_free.

Global Functions

Backend Management

backendInit

func backendInit()

Initialize llama backend. Maps to llama_backend_init.

backendFree

func backendFree()

Free llama backend. Maps to llama_backend_free.

numaInit

func numaInit(strategy: Ggml.NumaStrategy)

Initialize NUMA. Maps to llama_numa_init.

System Information

timeUs

func timeUs(): Int[64]

Get current time in microseconds. Maps to llama_time_us.

maxDevices

func maxDevices(): ArchWord

Get maximum number of devices. Maps to llama_max_devices.

maxParallelSequences

func maxParallelSequences(): ArchWord

Get max parallel sequences. Maps to llama_max_parallel_sequences.

supportsMmap

func supportsMmap(): Bool

Check mmap support. Maps to llama_supports_mmap.

supportsMlock

func supportsMlock(): Bool

Check mlock support. Maps to llama_supports_mlock.

supportsGpuOffload

func supportsGpuOffload(): Bool

Check GPU offload support. Maps to llama_supports_gpu_offload.

supportsRpc

func supportsRpc(): Bool

Check RPC support. Maps to llama_supports_rpc.

printSystemInfo

func printSystemInfo(): CharsPtr

Get system info string. Maps to llama_print_system_info.

Tokenization

tokenize

func tokenize(
    vocab: ref[Vocab], text: CharsPtr, textLen: Int, tokens: ref[array[Token]],
    nTokensMax: Int, addSpecial: Bool, parseSpecial: Bool
): Int

Convert text to tokens. Returns the number of tokens written. Maps to llama_tokenize.

detokenize

func detokenize(
    vocab: ref[Vocab], tokens: ref[array[Token]], nTokens: Int, text: CharsPtr,
    textLenMax: Int, removeSpecial: Bool, unparseSpecial: Bool
): Int

Convert tokens to text. Returns the number of characters written. Maps to llama_detokenize.

Chat Templates

chatApplyTemplate

func chatApplyTemplate(
    tmpl: CharsPtr, chat: ref[array[ChatMessage]], nMsg: ArchWord,
    addAssistant: Bool, buf: CharsPtr, bufLen: Int
): Int

Apply chat template to messages. Pass 0 for tmpl to use the model's default template. Returns the number of characters written. Maps to llama_chat_apply_template.

chatBuiltinTemplates

func chatBuiltinTemplates(out: ref[CharsPtr], len: ArchWord): Int

Get built-in template names. Maps to llama_chat_builtin_templates.

Logging

logSet

func logSet(cb: ptr[function(level: Int, text: CharsPtr, userData: ptr)], userData: ptr)

Set logging callback. Maps to llama_log_set.

More Examples

Text Completion

See Examples/completion.alusus for a complete text completion example.

Chat

See Examples/chat.alusus for a multi-turn chat example with chat templates.

Basic Usage Pattern

import "Apm";
Apm.importPackage("Alusus/Llama@0.2");
use Llama;

// 1. Initialize backend
Ggml.Backend.cpuLoad();

// 2. Load model
def modelParams: Model.Params = Model.getDefaultParams();
modelParams.nGpuLayers = 0;  // CPU only
def model: ref[Model](Model.load("model.gguf", modelParams));

// 3. Create context
def ctxParams: Context.Params = Context.getDefaultParams();
ctxParams.nCtx = 2048;
def ctx: ref[Context](Context.initFromModel(model, ctxParams));

// 4. Create sampler chain
def chainParams: Sampler.ChainParams;
chainParams.noPerf = true;
def sampler: ref[Sampler](Sampler.chainInit(chainParams));
sampler.chainAdd(Sampler.initPenalties(64, 1.1, 0.0, 0.0));
sampler.chainAdd(Sampler.initTopK(40));
sampler.chainAdd(Sampler.initTopP(0.9, 1));
sampler.chainAdd(Sampler.initDist(0));

// 5. Tokenize and decode prompt
def tokens: array[Token, 512];
def nTokens: Int = tokenize(model.vocab, "Hello world", 11, tokens, 512, true, true);
def batch: Batch = Batch.getOne(tokens, nTokens);
ctx.decode(batch);

// 6. Generate tokens
while true {
    def id: Token = sampler.sample(ctx, -1);
    if model.vocab.isEog(id) break;

    // Convert token to text and print
    def buf: array[Char, 64];
    detokenize(model.vocab, id~ptr~cast[ref[array[Token]]], 1, buf~ptr, 64, 0, 0);
    // ... print buf

    // Decode next token
    def nextBatch: Batch = Batch.getOne(id~ptr~cast[ref[array[Token]]], 1);
    ctx.decode(nextBatch);
}

// 7. Cleanup
Sampler.free(sampler);
Context.free(ctx);
Model.free(model);

License

Copyright (c) 2023-2024 The ggml authors Copyright (c) 2026 Alusus Software Ltd. for the Alusus language bindings.

This library follows the same license as llama.cpp (MIT License).

About

Alusus binding for llama.cpp

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors