All examples
Language models

Replay native CUDA batch graphs

Compare explicit graph replay with eager cached and full prefix generation, verify independent row semantics and retain unchanged model state.

generative_cuda_graph.aner
// Native CUDA graph replay on small seeded research weights.
// This exercises inference mechanisms, not trained language quality.
import aner.token
import aner.gen
import aner.nn

fn verify(label: String, condition: Bool) -> Unit {
    print("check_" + label)
    if !condition {
        let failure: Result<Bool, String> = Result.err(label)
        print(failure.unwrap())
    }
    print(condition)
}
fn same_ids(left: List, right: List) -> Bool {
    if left.len() != right.len() { return false }
    var index = 0
    while index < left.len() {
        if cast<Int64>(left.get(index)) != cast<Int64>(right.get(index)) { return false }
        index = index + 1
    }
    return true
}
fn same_rows(left: List, right: List) -> Bool {
    if left.len() != right.len() { return false }
    var row = 0
    while row < left.len() {
        if !same_ids(cast<List>(left.get(row)), cast<List>(right.get(row))) { return false }
        row = row + 1
    }
    return true
}

// Graph replay is explicit and requires cached CUDA execution.
// Change only Float32 to Float64 to run the other supported precision.
nn.configure("cuda", "Float32", "preserve")
let codec = token.byte_tokenizer()
let model = gen.decoder(codec, context: 96, width: 32,
    heads: 4, layers: 2, seed: 63, dropout: 0.0)
let identity = model.fingerprint()
let prompt = codec.encode("research", bos: true, eos: false)
let prompts = List(prompt, List(), List(120), prompt)
let budgets = List(24, 16, 0, 8)
let seeds = List(71, 19, 23, 97)
let graph = model.generate_batch(prompts, max_new_tokens: budgets,
    temperature: 0.8, top_k: 20, top_p: 0.95, seeds: seeds, cuda_graph: true)
let eager = model.generate_batch(prompts, budgets, 0.8, 20, 0.95, seeds, true, false)
let oracle = model.generate_batch(prompts, budgets, 0.8, 20, 0.95, seeds, false)
verify("graph_eager", same_rows(graph, eager))
verify("graph_uncached", same_rows(graph, oracle))
verify("empty_prompt_bos", same_ids(cast<List>(graph.get(1)),
    model.generate(List(), 16, 0.8, 20, 0.95, 19)))
verify("zero_budget", cast<List>(graph.get(2)).len() == 0)
verify("independent_seed", same_ids(cast<List>(graph.get(0)),
    model.generate(prompt, 24, 0.8, 20, 0.95, 71)))
let shared = model.generate_batch(List(prompt, prompt), 8, 0.0, 0, 1.0,
    List(7, 99), use_cache: true, cuda_graph: true)
verify("scalar_budget", same_ids(cast<List>(shared.get(0)), cast<List>(shared.get(1))))
let zero = model.generate_batch(prompts, List(0, 0, 0, 0), 0.0, 0, 1.0,
    seeds, cuda_graph: true)
verify("all_zero", zero.len() == 4 && cast<List>(zero.get(0)).len() == 0)
verify("model_unchanged", model.fingerprint() == identity && model.steps() == 0)
print("cuda_graph_checks_passed")

Make it your experiment.

With a compatible development runtime installed, save this program as examples/generative_cuda_graph.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/generative_cuda_graph.aner
aner run examples/generative_cuda_graph.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Use a compatible current native Aner development runtime with a CUDA enabled build and native batched graph replay. The source explicitly requires cached CUDA Float32 with no CPU fallback; change only the precision literal to Float64 for the other supported precision. The cuda_graph Bool defaults to false, and true requires cached CUDA execution. Optional capture resource failure continues only with native eager CUDA. This example creates no files and needs no pretrained weights, provider, credentials, external data or network connection. Older packaged runtimes may predate this development capability.

Bundled datasets retain their own source attribution and license notices. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner