All examples
Neural networks

Select strict NLP GPU numerical phases

Keep source text and host decoding explicit while placing admitted emission, feature and mask numerical phases on MPS Float32.

nlp_execution_gpu.aner
// Development runtime and an available native Metal/MPS Float32 backend required.
// Select CUDA/Float64 or CUDA/Float32 explicitly for a compatible CUDA build.
// No CPU fallback occurs if this strict backend is unavailable.
// Run in a fresh writable directory: checkpoint creation is exclusive.
import aner.nlp
import aner.nn
import aner.token

let execution = nn.execution_context(device: "mps", dtype: "Float32")
let source = nlp.corpus(List("Alice visits Paris .", "Bob visits Rome ."),
    List("gpu-train-1", "gpu-train-2"), role: "training")
let training = nlp.annotate(nlp.tokenize(source),
    List(List("PROPN", "VERB", "PROPN", "PUNCT"),
         List("PROPN", "VERB", "PROPN", "PUNCT")), task: "pos")
let model = nlp.fit_tagger(training, epochs: 3, seed: 731, execution: execution)
let held_out = nlp.corpus(List("Bob visits Paris ."), List("gpu-validation-1"), role: "validation")
let tokens = nlp.tokenize(held_out)
let prediction = model.tag(tokens, execution: execution)
print("Source-aligned labels; emission arithmetic on the strict GPU profile")
print(tokens.token_texts(0))
print(prediction.tags(0))
print(model.execution_summary())
print(prediction.execution_summary())

let first = nlp.fit_tagger(training, epochs: 1, seed: 731, execution: execution)
first.save("execution-gpu-pos-stage.anertagger")
let restored = nlp.load_tagger("execution-gpu-pos-stage.anertagger")
let continued = restored.resume_tagger(training, epochs: 2, execution: execution)
print("Same-profile GPU checkpoint continuation")
print(continued.fingerprint() == model.fingerprint())
print(continued.tag(tokens, execution: execution).fingerprint() == prediction.fingerprint())

let encoder = nlp.vectorizer(source)
let features = encoder.transform(held_out, execution: execution)
let dense = features.dense(max_cells: 1000, execution: execution)
let batch = nlp.batch(held_out, token.byte_tokenizer(), max_length: 32)
let mask = batch.mask(execution: execution)
print("Local Tensor placement matches the explicit profile")
print(nn.device(dense))
print(nn.dtype(dense))
print(nn.device(mask))
print(nn.dtype(mask))
print(features.execution_summary())
// Source token IDs never pass through floating point. Host Viterbi and ranking
// remain CPU work. Seeds do not promise Float32/Float64 or CPU/GPU equivalence.

Make it your experiment.

Use the matching development runtime. Save this program as examples/nlp_execution_gpu.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/nlp_execution_gpu.aner
aner run examples/nlp_execution_gpu.aner

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

Requires the matching unreleased native NLP execution runtime and development editor. Published Aner 0.0.5 and VS Code 0.1.48 do not provide these execution overloads. Synthetic documents are included; no data download or external service is needed. Run from a fresh writable directory because the GPU continuation case exclusively creates a checkpoint. Source checking does not establish GPU availability. Running requires a compatible native MPS Float32 build and Apple GPU, with no CPU fallback; selecting CUDA is an explicit source change and requires a compatible CUDA build/device.

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