instmodel
instmodel is a Python package for building instruction-based neural network models with either a PyTorch or TensorFlow/Keras backend. Build, train, and export models into a compact JSON "instruction" format for lightweight, backend-agnostic inference.
Features
- Dual Backend: Build models with PyTorch (
instmodel.torch) or TensorFlow/Keras (instmodel.tf) — both are optional dependencies. - Instruction Model Export: Convert trained models into a JSON-based instruction format that captures architecture, weights, and activations.
- Backend-Agnostic Inference: Run exported instruction models with pure NumPy via
instmodel.instruction_model— no framework required at inference time. - Validation: Verify that the instruction model produces the same outputs as the original trained model.
Installation
Install the core package (NumPy inference only):
pip install instmodel
Install with a training backend:
pip install instmodel[pytorch] # PyTorch backend
pip install instmodel[tensorflow] # TensorFlow/Keras backend
Quick Example — PyTorch
import numpy as np
from instmodel.torch import (
Dense,
InputBuffer,
ModelGraph,
ff_model,
validate_torch_model,
)
from instmodel.instruction_model import validate_instruction_model
# 1. Define a simple feed-forward model.
input_buffer = InputBuffer(4, name="simple_input")
hidden = Dense(8, activation="relu", name="hidden_relu_1")(input_buffer)
hidden = Dense(6, activation="relu", name="hidden_relu_2")(hidden)
output = Dense(1, activation="sigmoid", name="output_sigmoid")(hidden)
model_graph = ModelGraph(input_buffer, output)
model_graph.compile(optimizer="adam", loss="binary_crossentropy")
# 2. Train on dummy data.
x_data = np.random.random((10, 4))
y_data = np.random.randint(0, 2, size=(10, 1))
model_graph.fit(x_data, y_data, epochs=1, verbose=0)
# 3. Export to instruction model.
instruction_model = model_graph.create_instruction_model()
# 4. Validate.
torch_pred = model_graph.predict(x_data)
instruction_model["validation_data"] = {
"inputs": x_data.tolist(),
"expected_outputs": torch_pred.tolist(),
}
validate_instruction_model(instruction_model)
validate_torch_model(model_graph.get_torch(), instruction_model["validation_data"])
Quick Example — TensorFlow/Keras
import numpy as np
from instmodel.tf import (
Dense,
InputBuffer,
ModelGraph,
ff_model,
validate_keras_model,
)
from instmodel.instruction_model import validate_instruction_model
# 1. Define a simple feed-forward model.
input_buffer = InputBuffer(4, name="simple_input")
hidden = Dense(8, activation="relu", name="hidden_relu_1")(input_buffer)
hidden = Dense(6, activation="relu", name="hidden_relu_2")(hidden)
output = Dense(1, activation="sigmoid", name="output_sigmoid")(hidden)
model_graph = ModelGraph(input_buffer, output)
model_graph.compile(optimizer="adam", loss="binary_crossentropy")
# 2. Train on dummy data.
x_data = np.random.random((10, 4))
y_data = np.random.randint(0, 2, size=(10, 1))
model_graph.fit(x_data, y_data, epochs=1, verbose=0)
# 3. Export to instruction model.
instruction_model = model_graph.create_instruction_model()
# 4. Validate.
keras_pred = model_graph.predict(x_data, verbose=0)
instruction_model["validation_data"] = {
"inputs": x_data.tolist(),
"expected_outputs": keras_pred.tolist(),
}
validate_instruction_model(instruction_model)
validate_keras_model(model_graph.get_keras(), instruction_model["validation_data"])
API Overview
Both backends expose the same model-building API:
| Layer / Op | Description |
|---|---|
InputBuffer |
Model input |
Dense |
Fully connected layer |
Attention |
Attention mechanism |
Concatenate |
Concatenate buffers |
ReduceSum |
Sum reduction |
Add |
Element-wise addition |
Multiply |
Element-wise multiplication |
MultiplyHeads |
Head-wise broadcast multiply |
AddHeads |
Head-wise broadcast add |
ScaleVectorized |
Learnable per-element scale |
ShiftVectorized |
Learnable per-element shift |
SingleIdEmbeddings |
Single-ID embedding lookup |
MultiIdEmbeddings |
Multi-ID embedding lookup |
ModelGraph |
Compiles the computation graph for training and export |
ff_model |
Helper to build a feed-forward stack |
validate_model |
Backend-specific validator (alias) |
Backend-specific validators:
instmodel.tf.validate_keras_modelinstmodel.torch.validate_torch_model
Backend-agnostic inference:
instmodel.instruction_model.instruction_model_inferenceinstmodel.instruction_model.validate_instruction_model
GPU Testing
For running PyTorch tests on RTX 50-series GPUs (CUDA 13.1), a custom Dockerfile is provided:
custom_cuda_builds/Dockerfile.torch.cuda13
License
This project is licensed under the MIT License.
Release files for instmodel 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| instmodel-1.3.0.tar.gz | 40.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| instmodel-1.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 70.4 kB
Release files / instmodel-1.3.0.tar.gz
| Download URL | instmodel-1.3.0.tar.gz |
|---|---|
| Size | 40.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f003715eb1a472231347ac892c1948bf8c5a6f09d04540b5367fb52d84705851
|
|
BLAKE2b-256 checksum How to use checksums |
afc6f7b1d5dd0b7f0c0fd73fd28a51f5ed658fd3e0419f7d7aeb54d4eddedffd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 2, 2026.
Transparency logRelease files / instmodel-1.3.0-py3-none-any.whl
| Download URL | instmodel-1.3.0-py3-none-any.whl |
|---|---|
| Size | 29.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
15b6dbf6a137d884ba256a6cd9bc6695b3ca908e27bbb37a425fb6f96be36257
|
|
BLAKE2b-256 checksum How to use checksums |
8f7c153d066cf4a0d12e081981437edaf154bf0f4878b28ed6fb2ff9cf3a759a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 2, 2026.
Transparency log