Before You Start
- Read
.agents/knowledge/constraints.md, especially device guards and multi-backendmodeling_<m>.pyrules. - Read
veomni/ops/README.mdanddocs/design/op_selection.md. - Inspect the closest family under
veomni/ops/kernels/and its tests undertests/ops/.
Ops Architecture
veomni.ops.OP_REGISTRY is the tensor-kernel source of truth. Each
row has the identity (op, variant, implementation, device). Callers
select the public (op, variant, implementation) triple; the registry
derives the device from the row's KernelRequirement and resolves the current
device before a device-agnostic row.
A row provides exactly one of these forms:
- raw
forwardandbackwardfunctions, from which the registry generates a
torch.autograd.Function wrapper; or
- an opaque
wrapperfor eager PyTorch or a library API that already owns its
autograd behavior.
For a raw pair, forward returns (output, SavedState) and backward returns
one gradient entry per positional tensor input. Pass tensors positionally and
non-tensor attributes by keyword.
Model classes construct an instance-local VeomniOp handle and call it
directly. Input normalization, HuggingFace-compatible signatures, and loss
policy belong in veomni/models/; do not add consumer-specific adapters
to the registry. The public CLI/YAML field remains
model.ops_implementation, while model builders receive it through the
ops_implementation keyword.
Use these separate mechanisms only when their semantics require them:
veomni/ops/batch_invariant/for the opt-in ATen patch controlled by
set_batch_invariant_mode(...);
veomni/ops/install.pyfor idempotent process-wide integrations such as
registration with a third-party framework;
veomni/distributed/hccl_premul_sum.pyfor the NPU collective compatibility
patch used by distributed ExtraParallel code.
Phase 1: Design
- Define the stable tensor contract and decide whether consumer-specific
preprocessing belongs in models.
- Choose the op name, semantic variant, implementation name, and device
requirement. A variant changes the tensor contract; an implementation keeps
that contract and changes how it is computed.
- Decide whether the implementation is a raw pair or an opaque wrapper. Never
provide both forms for one row.
- If users must select it, add or extend the appropriate field in
OpsImplementationConfig. Preserve existing CLI/YAML field names.
Phase 2: Implement
- Create or extend a family under
veomni/ops/kernels/<kernel_name>/.
- Keep implementations in variant/device-oriented modules consistent with the
neighboring families.
- Register every row through
register_op:
```python
from veomni.ops import register_op
from veomni.ops.platform import GpuKernelRequirement, NvidiaGpuPlatform
register_op("example", "standard", "eager", wrapper=eager_example)
register_op(
"example",
"standard",
"triton",
forward=triton_forward,
backward=triton_backward,
requirement=GpuKernelRequirement(platforms=(NvidiaGpuPlatform(min_cc=80),)),
)
```
- Import the family from
veomni/ops/kernels/__init__.pyso registration
happens when veomni.ops is imported.
- In each consuming model, construct a
VeomniOpfrom
config.ops_config.<field> and store it on the model/module instance.
- For a compound raw kernel, call the nested row's raw
forward/backward
and use the saved-state helpers in veomni/ops/compound.py; do not call
the nested autograd wrapper.
- Guard optional device packages and attach an explicit requirement. A
registered implementation must fail clearly when its requirement is not
satisfied; it must not silently fall back to eager.
- Add English module, module-level class, and public function docstrings.
Nested closures and ordinary _private helpers are not gated; forward /
backward / wrapper / __call__ still are. Checked by
tests/ops/base/test_op_documentation.py.
Phase 3: Test
- Add tests under
tests/ops/<kernel_name>/for:
- forward parity against an independent eager reference;
- backward parity for every differentiable input;
- dtype/shape/edge contracts;
- registration and device requirements;
- explicit failure for unknown or unavailable implementations.
- Put consumer-specific normalization and model wiring tests under
tests/models/ instead of duplicating them in the raw-kernel suite.
Cover distinct variants and custom backward implementations separately,
including EP/non-EP and split/merged layouts when implemented. Standard
Quack and GPT-OSS Quack share GEMM but not the expert computation. An
EP-local compute test does not qualify token all-to-all; add a model/backend
E2E representative when that integrated path is new. Follow the
for representative selection and capability exceptions.
- Run the family tests plus the registry and documentation guards:
GPU and NPU unit manifests select tests/ops/, with hardware prerequisites
and selection filters controlling individual cases. Check both manifests
before adding an entry; an already-collected case needs no workflow edit.
```bash
pytest -q tests/ops/<kernel_name>/ tests/ops/base/
```
- Add a benchmark only when performance is part of the acceptance criteria;
compare it with the canonical eager contract.
Phase 4: Document
- Update
veomni/ops/README.mdwith the family, variants, and supported
implementations.
- Update
docs/design/op_selection.mdwhen selection behavior changes. - Update
.agents/knowledge/architecture.mdwhen the layout or call chain
changes.
Phase 5: Finalize
- Run
/veomni-review. - Run
make qualityand the relevant kernel/model integration tests. - Verify
OP_REGISTRY.list_registered(...)and
list_available(...) report the expected rows.
Common Pitfalls
- Confusing a semantic variant with an implementation backend.
- Adding model-specific reshaping or reduction policy to the tensor registry.
- Calling a nested autograd wrapper from a compound custom-autograd function.
- Importing NPU/CUDA-only libraries without guards or requirements.
- Registering a row without importing its family from
kernels/__init__.py. - Letting an unavailable optimized implementation fall back silently.
- Importing a specific kernel implementation module directly inside
modeling_<m>.pyinstead of dispatching viaVeomniOp. - Renaming
ops_implementationwhile changing the internal kernel plumbing.
Configuration ownership
Model ops selections belong to VeOmniPretrainedConfig.ops_implementation.
Read config.ops_config.<field> when binding a handle. HF config methods are
never patched globally; standalone module tests must construct an adapted config
with get_veomni_config_class or opt in with as_veomni_config.
Attention's HF accessors alias the same dictionary; do not add synchronization
fields or repair attention state in builders. See docs/design/module_config.md.
Merge all load-time ops layers as dictionaries before validation. The owning
config caches a standard OpsImplementationConfig instance after the final
selection is known; do not bypass its dataclass constructor or introduce an
ambient validation context. Caches stay out of saved config dictionaries.
Pass selections directly to the owning variant. On NPU, unweighted RMSNorm
supports only eager; explicit torch_npu/npu must raise. The attention
eager row preserves model-local eager math when present and otherwise
forwards to veomni_sdpa; consumers do not remap eager themselves.