Python tutorial
Character GRU language model
The public oa.core, oa.ml, and oa.runtime bindings drive the same all-position character task as the C++ tutorial.
oa.mlNative Vulkan execution300 steps · batch 64
| Contract | Value |
|---|---|
| Imports | oa.core, oa.ml, oa.runtime through the public oa package |
| Model | OaEmbedding → OaGru → OaLinear |
| Autograd | ml.GradientTape and ml.OaAdamW |
| Validation | Loss, accuracy, generation, and checkpoint round-trip |
Model
Tutorialnlpchargruag.py
class CharGruLM:def __init__(self):self.embed = ml.OaEmbedding(27, nlp.D_MODEL)self.gru = ml.OaGru(nlp.D_MODEL, nlp.HIDDEN_DIM, 1)self.head = ml.OaLinear(nlp.HIDDEN_DIM, 27)for parameter in self.parameters():parameter.Data.SetRequiresGrad(True)def forward(self, tokens):batch, sequence = tokens.Size(0), tokens.Size(1)embedded = self.embed.Forward(tokens).Reshape([batch, sequence, nlp.D_MODEL])hidden = self.gru.Forward(embedded).Reshape([batch * sequence, nlp.HIDDEN_DIM])return self.head.Forward(hidden)
Train and evaluate
Tutorialnlpchargruag.py
runtime.OaInitComputeEngine()model = CharGruLM()sampler = nlp.NlpAllPositionSampler(nlp.CORPUS, nlp.BATCH, encode=nlp.char_encode)optimizer, initial_loss, final_loss, x, y = nlp.train_all_position(model.forward,model.parameters(),sampler,lr=0.01,timer_name="char_gru_allpos_step",)accuracy = nlp.accuracy_all_positions(model.forward, x, y, 27)generated = nlp.generate_greedy(model.forward, "hello", 48, 27, encode=nlp.char_encode)
No Python tensor fork
Python objects wrap the same OaMatrix and module implementations used by C++.
Shared test scaffolding
_nlp_common.py owns the corpus, sampler, metrics, greedy generation, and training loop.
Explicit runtime lifetime
The tutorial initializes OA once, records GPU work through native calls, and synchronizes only at deliberate boundaries.
Generated reference
The Python API pages list only symbols currently registered by Source/Python.
Run
Terminal
OA_PYTHON_BUILD_DIR=Build/Release PYTHONPATH=Tutorial/Py Build/PythonVenv/bin/python Tutorial/Py/TutorialNlpCharGruAg.py