Add a New NKIPy Operation
Add the operation $0 to the $1 category file.
Steps
1. Identify the correct category file
Operations live in nkipy/src/nkipy/core/ops/<category>.py. Choose from:
| Category |
File |
Examples |
| unary |
unary.py |
exp, log, sqrt, abs, neg |
| binary |
binary.py |
add, mul, sub, div, pow |
| reduce |
reduce.py |
sum, mean, max, min, prod |
| transform |
transform.py |
reshape, transpose, concat, split |
| indexing |
indexing.py |
getitem, setitem, slice |
| creation |
creation.py |
zeros, ones, arange, full |
| conv |
conv.py |
conv2d operations |
| nn |
nn.py |
relu, softmax, gelu |
| collectives |
collectives.py |
all_reduce, all_gather |
| linalg |
linalg.py |
matmul, dot |
2. Create the Op instance and register implementations
In the chosen category file, follow this pattern:
from nkipy.core.backend.hlo import get_hlo_context
from nkipy.core.ops._registry import Op
# Create the dispatcher
my_op = Op('my_op')
@my_op.impl('hlo')
def _my_op_hlo(arg1, arg2, ...):
"""HLO tracing implementation."""
ctx = get_hlo_context()
# Build HLO operation using ctx
# Return NKIPyTensorRef
...
@my_op.impl('cpu')
def _my_op_cpu(arg1, arg2, ...):
"""CPU eager implementation using NumPy."""
import numpy as np
# Return numpy array
...
Key rules:
- The
hlo impl builds HLO IR nodes via the trace context (get_hlo_context() from nkipy.core.backend.hlo)
- The
cpu impl uses pure NumPy and returns numpy arrays
- Both implementations must accept the same signature and produce equivalent results
- Both backends are required — every op needs
hlo and cpu
3. Export from __init__.py
Add the new op to nkipy/src/nkipy/core/ops/__init__.py:
- Import it from the category module
- Add it to the
__all__ list if one exists
- Follow the existing grouping/ordering in the imports
4. Wire into tensor API (if applicable)
If the op should be callable as a method on tensors (e.g., tensor.sum()), or via np.* dispatch:
- Check
nkipy/src/nkipy/core/tensor.py for TensorArithmeticMixin
- Check
nkipy/src/nkipy/core/tensor_apis.py for high-level API wrappers
- Check
nkipy/src/nkipy/core/_numpy_dispatch.py for NumPy protocol integration
5. Add tests
Create or extend tests in tests/unit/. Follow existing patterns:
- Test both CPU and HLO backends
- Test edge cases (empty tensors, broadcasting, dtype promotion)
- Run with:
uv run pytest tests/unit/test_tensor_api.py -k "test_my_op" -v
6. Verify
uv run ruff check nkipy/src/nkipy/core/ops/
uv run pytest tests/unit/ -k "$0" -v
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: add-op3description: Add a new operation to the NKIPy ops registry with hlo and cpu backend implementations Use when this capability is needed.4---56# Add a New NKIPy Operation78Add the operation `$0` to the `$1` category file.910## Steps1112### 1. Identify the correct category file1314Operations live in `nkipy/src/nkipy/core/ops/<category>.py`. Choose from:1516| Category | File | Examples |17|----------|------|----------|18| unary | `unary.py` | exp, log, sqrt, abs, neg |19| binary | `binary.py` | add, mul, sub, div, pow |20| reduce | `reduce.py` | sum, mean, max, min, prod |21| transform | `transform.py` | reshape, transpose, concat, split |22| indexing | `indexing.py` | getitem, setitem, slice |23| creation | `creation.py` | zeros, ones, arange, full |24| conv | `conv.py` | conv2d operations |25| nn | `nn.py` | relu, softmax, gelu |26| collectives | `collectives.py` | all_reduce, all_gather |27| linalg | `linalg.py` | matmul, dot |2829### 2. Create the Op instance and register implementations3031In the chosen category file, follow this pattern:3233```python34from nkipy.core.backend.hlo import get_hlo_context35from nkipy.core.ops._registry import Op3637# Create the dispatcher38my_op = Op('my_op')3940@my_op.impl('hlo')41def _my_op_hlo(arg1, arg2, ...):42 """HLO tracing implementation."""43 ctx = get_hlo_context()44 # Build HLO operation using ctx45 # Return NKIPyTensorRef46 ...4748@my_op.impl('cpu')49def _my_op_cpu(arg1, arg2, ...):50 """CPU eager implementation using NumPy."""51 import numpy as np52 # Return numpy array53 ...54```5556Key rules:57- The `hlo` impl builds HLO IR nodes via the trace context (`get_hlo_context()` from `nkipy.core.backend.hlo`)58- The `cpu` impl uses pure NumPy and returns numpy arrays59- Both implementations must accept the same signature and produce equivalent results60- Both backends are **required** — every op needs `hlo` and `cpu`6162### 3. Export from `__init__.py`6364Add the new op to `nkipy/src/nkipy/core/ops/__init__.py`:65- Import it from the category module66- Add it to the `__all__` list if one exists67- Follow the existing grouping/ordering in the imports6869### 4. Wire into tensor API (if applicable)7071If the op should be callable as a method on tensors (e.g., `tensor.sum()`), or via `np.*` dispatch:72- Check `nkipy/src/nkipy/core/tensor.py` for `TensorArithmeticMixin`73- Check `nkipy/src/nkipy/core/tensor_apis.py` for high-level API wrappers74- Check `nkipy/src/nkipy/core/_numpy_dispatch.py` for NumPy protocol integration7576### 5. Add tests7778Create or extend tests in `tests/unit/`. Follow existing patterns:79- Test both CPU and HLO backends80- Test edge cases (empty tensors, broadcasting, dtype promotion)81- Run with: `uv run pytest tests/unit/test_tensor_api.py -k "test_my_op" -v`8283### 6. Verify8485```bash86uv run ruff check nkipy/src/nkipy/core/ops/87uv run pytest tests/unit/ -k "$0" -v88```8990---91> Converted and distributed by [TomeVault](https://tomevault.io/claim/aws-neuron) — claim your Tome and manage your conversions.92<!-- tomevault:4.0:skill_md:2026-04-11 -->