Model structure
Upload a model's layers and weights, read them back, replay a prediction through them and see what every layer computes.
The architecture of a model: its layers, weights and biases as a kaimei.structure.v1 document, the scene the dashboard draws, a replay of one prediction through the graph, and what every layer computed for an input. The Python client builds the spec from a fitted estimator; these endpoints are what it calls, and what you call to write a converter of your own.
Executable families (networks of supported layers, trees and tree ensembles of any size, models made of those, and general model graphs of ONNX operators) are run by the API for explanations, predictions and analyses. Anything else is drawable, and is queried through a remote inference URL or the model's agent.
#The structure spec
A spec is a JSON object with format: "kaimei.structure.v1", a family and the layers or nodes. Tensors are nested lists, or references: {"$tensor": "key"} for a binary tensor uploaded separately, {"$b64": …} for an inline buffer, or {"$shape": [...], "dtype", "stats": {...}} for a tensor described without its values (drawable, not executable), which may carry a view: a picture of it of at most 64 by 64 cells (see models of any size).
| family | Content | Executable |
|---|---|---|
network | layers: an ordered list. Each layer has an id, a type, its tensors and optional inputs naming the layers it reads (defaults to the previous one). Dense weights are (out, in): y = W x + b. A dotted path groups layers into blocks. | Yes, unless approximate |
tree | nodes: {id, feature, threshold, left, right} for splits, {id, leaf: true, output} for leaves (a class distribution or a value); root names the first node. Up to 8,000 nodes. | Yes |
ensemble | trees: a list of tree specs, averaged. Or, at any size, forest: flat arrays roots, feature, threshold, left, right, value and optional missing_left, each inline or as a binary tensor, with children after their parents, plus combine: method (mean, sum or median), weights, tree_class, base, scale and link (identity, sigmoid, softmax or exp). | Yes |
composite | members: full specs, plus combine with an op: mean, vote, ovr, calibrated, stack or target. The scene draws the first member; a prediction uses all of them. | When every member is |
ensemble_summary | importances per feature, for an ensemble Kaimei can't run itself. | No |
graph | graph: the model operator by operator, with ONNX's operators, plus head, tokenizers and sampler. See the graph spec. | Yes, unless a step has no kernel or a tensor was sent as its shape |
Two shorthands are accepted and normalised to a network: format: "linear" (weight, bias, feature_names, optional activation) and format: "mlp" (a list of dense layers).
| Layer type | Fields |
|---|---|
input | names (feature names) or shape [h, w, c] for an image input |
dense / linear | units, weight (out, in), bias, optional activation |
conv2d | weight (out, in/groups, kh, kw), bias, stride, padding (same, valid or explicit), dilation, groups, optional activation |
maxpool / avgpool | size, stride, padding, ceil_mode, count_include_pad |
adaptive_avgpool / adaptive_maxpool / global_avgpool / global_maxpool | output_size for adaptive pools |
batchnorm | gamma, beta, mean, var, eps |
normalize / rescaling | per-channel scale, offset, or mean and std |
standardize | center, scale per feature (a folded StandardScaler) |
layernorm | gamma, beta, eps |
embedding | weight (vocab, dim) |
activation | activation: relu, relu6, leaky_relu, elu, gelu, silu, hardswish, hardsigmoid, sigmoid, tanh, softmax; alpha for leaky_relu and elu |
add / concat / multiply | inputs: the layers to combine (residual add, channel concat, broadcasting multiply for squeeze-excitation) |
flatten | optional order: "chw" for PyTorch heads |
pad2d | padding per side |
dropout | no-op at inference; kept so the drawing matches the code |
opaque | a step Kaimei draws but does not run: op (the framework's own name), attrs (plain JSON), shape (the output without its batch axis, any rank; [h, w, c] for pictures), optional params ({name: tensor}, counted and served like any other tensor), reason, and inputs ([] for a constant). Any opaque step makes the whole model drawn, not run. |
Top-level fields: feature_names, task, labels or outputs, framework, name, summary, architecture, approximate: true when a layer was passed through unmodelled, and metadata: a plain JSON object (up to 8 KB) saying where the spec came from, kept on the document as sent. kaimei.extract_structure(estimator) in the Python client produces this document from scikit-learn, Keras and PyTorch objects, fills metadata with the model's class, the framework and its version, the Python and client versions and the time (add your own with metadata=), and turns operations it cannot replay into opaque steps, or raises unsupported_layers with on_unsupported="error". The compiled document's stats.opaque counts them.
{
"format": "kaimei.structure.v1",
"family": "network",
"framework": "sklearn",
"task": "binary_classification",
"feature_names": [
"tenure_months", "monthly_spend", "support_tickets"
],
"labels": ["stay", "churn"],
"layers": [
{
"id": "input",
"type": "input",
"names": [
"tenure_months", "monthly_spend", "support_tickets"
]
},
{
"id": "standardize",
"type": "standardize",
"center": [24.0, 62.0, 1.2],
"scale": [18.0, 30.0, 1.6]
},
{
"id": "hidden",
"type": "dense",
"units": 4,
"activation": "relu",
"weight": [
[0.8, -0.3, 0.5],
[-0.6, 0.9, 0.2],
[0.1, 0.4, -0.7],
[0.3, -0.2, 0.9]
],
"bias": [0.1, -0.2, 0.05, 0.0]
},
{
"id": "output",
"type": "dense",
"units": 1,
"activation": "sigmoid",
"weight": [[-1.2, 0.9, 0.7, 1.1]],
"bias": [-0.3]
}
]
}{
"format": "kaimei.structure.v1",
"family": "network",
"framework": "pytorch",
"task": "multiclass_classification",
"layers": [
{
"id": "input",
"type": "input",
"shape": [224, 224, 3]
},
{
"id": "normalize",
"type": "normalize",
"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225]
},
{
"id": "features.0.conv",
"path": "features.0",
"type": "conv2d",
"stride": 2,
"padding": "same",
"activation": "relu6",
"weight": {"$tensor": "features.0.conv.weight"},
"bias": {"$tensor": "features.0.conv.bias"}
},
{
"id": "features.1.dw",
"path": "features.1",
"type": "conv2d",
"groups": 32,
"padding": "same",
"weight": {"$tensor": "features.1.dw.weight"}
},
…,
{"id": "pool", "type": "global_avgpool"},
{
"id": "classifier",
"type": "dense",
"units": 1000,
"activation": "softmax",
"weight": {"$tensor": "classifier.weight"},
"bias": {"$tensor": "classifier.bias"}
}
]
}#The graph spec
A structure of family: "graph" is a model written down operator by operator, the way ONNX writes models down. The Python client produces it from PyTorch, scikit-learn, Keras or an ONNX file (models of any kind); this is the form, for a converter of your own.
| Key | What it holds |
|---|---|
graph.opsets | Operator set versions by domain, {"": 17}. A node may carry its own opset and domain (ai.onnx.ml, com.microsoft, and ai.kaimei for Kaimei's Linear and LastToken). |
graph.inputs | {name, dtype, shape, from}: shape for one row, and from the features that make the tensor (below). batch_axis (0 when that axis has size 1) is where rows stack; null takes one row at a time. |
graph.constants | By name: a list of numbers (whole numbers are int64, others float32), {dtype, shape, data}, {"$b64", dtype, shape} (raw little-endian bytes, which is how infinity travels), {"$tensor": key} or {"$shape", dtype, stats, view}. |
graph.nodes | {id, op, inputs, outputs, attrs, path}, by ONNX's operator names and attributes. A tensor is produced once and before it is read. Bodies of If, Loop and Scan are nested {"graph": {...}} attributes. |
graph.outputs | The graph's outputs, by name. |
graph.values | The shape and number type of the tensors a person is shown, and optionally hidden: true (run, never drawn), label, role: "attention", view: {perm, shape} (how its values are laid out for display) and source (what the original model file calls it). Shapes left out are found by one pass on zeros, which runs under the limits of any pass: a graph whose constants are larger than a pass may hold, or whose inputs or shown tensors are, declares its shapes here. With a check, every declared shape must be the one computed on its first row. |
head | output (default: the first graph output) and link: none, sigmoid or softmax, applied when the model ends in scores. One number from a binary model is the probability of its second label, whichever label is the positive one. A detector's head says which columns of its rows are a box and which are scores, a segmenter's its layout (chw, hwc or indices) and a clustering's whether the nearest or the likeliest group is the answer (reading the answers); every such field is checked at upload, and one that cannot be read is 400 invalid_structure with param naming it. |
tokenizers | By feature name: the tokenizer of each text feature. |
sampler | A diffusion model's loop: {type: "diffusion", sample, timestep, prediction, train_steps, schedule, beta_start, beta_end, steps, eta, clip, timestep_scale, layout}, or alphas_cumprod in full. prediction is epsilon, sample or v; schedule linear, scaled_linear or cosine. |
check | {inputs, outputs}: rows, feature by feature, and the original library's answers to them (a table may send {rows, outputs}). The API scores them before it runs the model, and does not store them. groups (how many rows of each kind, in order) has each group compared at its own scale, and tolerance is how far an output may be from the library's, relative to their size (1e-5 unless said, never looser than 1e-3). A check this server runs out of time or room for refuses the upload with that reason and param: "check". |
| from | The input tensor is |
|---|---|
{"features": ["age", "income"]} | Those scalar features, in that order. |
{"feature": "photo", "layout": "nchw"} | An image feature, laid out nchw, nhwc, chw or hwc. |
{"feature": "review", "part": "ids"} | A text feature's token ids; part may also be mask (1 on real tokens), type_ids (zeros), positions or length. pad_id (0 unless said) fills the ids after the last token. A text or tokens feature bound with no part is read as its ids. |
{"feature": "load"} | A sequence or tensor feature, row-major. |
{"value": …} / {"zeros": true} | The same value on every pass, such as a starting state. A value sent as its shape, as a summary upload sends it, leaves the graph drawn, not run. |
task takes every task, the generative ones included, and outputs may say kind, value_range, layout and, for a model run elsewhere, shape. What runs, what is only drawn and how each tensor is shown are on models of any kind.
{
"format": "kaimei.structure.v1",
"family": "graph",
"task": "generation",
"framework": "pytorch",
"features": [
{
"name": "noise",
"type": "tensor",
"shape": [3, 32, 32]
},
{"name": "t", "type": "numeric"}
],
"graph": {
"opsets": {"": 17},
"inputs": [
{
"name": "x",
"dtype": "float32",
"shape": [1, 3, 32, 32],
"from": {"feature": "noise"}
},
{
"name": "t",
"dtype": "float32",
"shape": [1],
"from": {
"features": ["t"]
}
}
],
"constants": …,
"nodes": …,
"outputs": ["eps"]
},
"outputs": {
"kind": "image",
"value_range": [-1, 1]
},
"sampler": {
"type": "diffusion",
"sample": "noise",
"timestep": "t",
"prediction": "epsilon",
"train_steps": 1000,
"schedule": "linear",
"steps": 50,
"eta": 0,
"clip": [-1, 1],
"layout": "chw"
}
}#Retrieve the structure document
/v1/models/{model}/structureAPI key or sessionThe architecture document the dashboard renders: stats and the rendering scale (tier, whether the browser can replay the forward pass), layers grouped from the spec, the blocks tree implied by node paths, a colour legend, the output head, the 3-D scene (nodes with position, kind, formula, inputs and args; edges; camera), the parameters with their stored dtype (f16, f32, f64, or i8, i16, i32, i64, u8 and bool for a graph's whole numbers and flags), statistics, previews and, when small enough, values, and the spec's metadata. A parameter sent as its shape with a picture says pictured: true. A general model graph's scene nodes carry the operator, its attributes and the tensor as the model keeps it in args (how each step is shown).
Models with an uploaded structure return it; built-in models return the reference architecture. Any other model gets a schematic: its inputs and output only, with architecture, fidelity and source set to schematic, schematic and derived, and a summary inviting a structure upload.
verification says what the numbers rest on: verified (its library check ran on the API and agreed, or a reader that runs one wrote it), unchecked (stored with no check, or with one that could not be run here because a step is only drawn or the model is too large; an upload that only says it was verified is unchecked too), reupload (a client before 0.7.2 may have saved one tree of a forest as the whole model), drawn, builtin or schematic.
Path parameters
Model id or slug.
Query parameters
Inline parameter values when they are small enough.
Values per tensor to inline, 0 to 200,000.
Which member of a forest the scene draws.
Only this block of the model (a node path).
Block levels below root to include, 0 to 64.
full or summary; summary omits tensor previews.Returns
model.structure document.doc = client.models.structure("churn-net-docs", detail="summary")
print(doc.architecture, doc.fidelity, doc.runnable, doc.scale["tier"])
for layer in doc.layers:
print(layer["label"], layer["units"], layer["parameters"], layer["kinds"])
block = client.models.structure("encoder", root="encoder.layers.17", depth=2)curl https://api.kaimei.ai/v1/models/churn-net-docs/structure?detail=summary \
-H "Authorization: Bearer $KAIMEI_API_KEY"{
"object": "model.structure",
"version": 2,
"model_id": "mdl_ec2f5d3a8098280a8964",
"name": "Churn net (docs)",
"framework": "sklearn",
"task": "binary_classification",
"architecture": "network",
"fidelity": "exact",
"source": "upload",
"summary": "Uploaded architecture. Every dense weight and bias below is the tensor used to compute the prediction.",
"runnable": true,
"focus_node": "output",
"stats": {
"nodes": 11,
"scene_nodes": 13,
"edges": 12,
"parameters": 27,
"tensors": 10,
"layers": 4,
"depth": 6,
"blocks": 0,
"trees": 0
},
"scale": {
"tier": "small",
"parameters": 27,
"nodes": 11,
"tensors": 10,
"blocks": 0,
"browser_replay": true,
"weights_available": true,
"previews": false,
"collapse": false,
"truncated": false,
"root": "",
"depth": null
},
"layers": [
{
"id": "inputs",
"label": "Inputs",
"node_ids": [
"tenure_months", "monthly_spend", "support_tickets"
],
"block": "",
"units": 1,
"parameters": 0,
"position": 0,
"kinds": ["input"],
"repeat": null
},
{
"id": "hidden",
"label": "hidden",
"node_ids": ["hidden", "hidden__relu"],
"block": "",
"units": 4,
"parameters": 16,
"position": 6,
"kinds": ["activation", "linear"],
"repeat": null
},
…
],
"blocks": [],
"legend": [
{"kind": "input", "label": "Input", "color": "#3ec8ff"},
{
"kind": "linear",
"label": "Linear layer",
"color": "#8aa4ff"
},
{
"kind": "activation",
"label": "Activation",
"color": "#ff6bb5"
},
…
],
"head": {
"kind": "binary_classification",
"labels": ["stay", "churn"],
"positive_label": "churn",
"positive_index": 1,
"threshold": 0.5,
"vector_node": "probabilities",
"explanation_node": "output"
},
"scene": {
"nodes": [
{
"id": "hidden",
"kind": "linear",
"op": "dense",
"mesh": "slab",
"label": "hidden",
"formula": "W x + b",
"stage": 2.0,
"position": [6.1, 0.0, 0.0],
"units": 4,
"parameter_ids": ["hidden.weight", "hidden.bias"],
"executable": true,
"activation": "relu",
"inputs": [
"standardize__tenure_months",
"standardize__monthly_spend",
"standardize__support_tickets"
],
"args": {}
},
…
],
"edges": [
{
"id": "tenure_months>standardize__tenure_months",
"source": "tenure_months",
"target": "standardize__tenure_months",
"kind": "data"
},
…
],
"camera": {
"position": [9.02, 3.42, 20.28],
"target": [7.78, 0.0, 0.0]
}
},
"parameters": [
{
"id": "hidden.weight",
"node_id": "hidden",
"name": "weight",
"role": "weight",
"label": "hidden weight",
"learned": true,
"shape": [4, 3],
"numel": 12,
"stats": {
"min": -0.7,
"max": 0.9,
"mean": 0.191667,
"std": 0.528297,
"l2": 1.946792,
"abs_mean": 0.491667,
"sparsity": 0.0,
"numel": 12
},
"values_included": true,
"available": true,
"values": [0.8, -0.3, 0.5, …]
},
…
]
}#Upload a structure
/v1/models/{model}/structureAPI key or sessionStore a kaimei.structure.v1 spec on a registered model, replacing any previous one. The spec is compiled and validated; an unusable spec returns 400 with the code and param that failed (invalid_structure, unsupported_layers, tensor_missing).
A spec can hold check: rows and your library's answers to them. The API scores them with the engine that will run the model, and if they don't match, the model is drawn but not run. The rows aren't stored. A check the API runs out of time or room for has not shown the reading wrong, so the upload is refused with that reason (400 request_time_exceeded, run_time_exceeded or structure_too_large, with param: "check"): send a check of fewer rows, or none. A model too large for the server to hold in memory is also drawn but not run: its document says runnable: false and its summary says why, and running it returns model_too_large_to_run.
Tensors referenced as {"$tensor": key} must already be uploaded with PUT …/structure/tensors/{key}. The Python client's upload_structure does both from a fitted estimator.
Path parameters
Model id or slug.
Body parameters
The spec itself. See the structure spec.
Returns
spec = {
"format": "kaimei.structure.v1", "family": "network", "task": "binary_classification",
"feature_names": ["tenure_months", "monthly_spend", "support_tickets"], "labels": ["stay", "churn"],
"layers": [
{"id": "input", "type": "input", "names": ["tenure_months", "monthly_spend", "support_tickets"]},
{"id": "standardize", "type": "standardize", "center": [24.0, 62.0, 1.2], "scale": [18.0, 30.0, 1.6]},
{"id": "hidden", "type": "dense", "units": 4, "activation": "relu", "weight": W1.tolist(), "bias": b1.tolist()},
{"id": "output", "type": "dense", "units": 1, "activation": "sigmoid", "weight": W2.tolist(), "bias": b2.tolist()},
],
}
client.models.upload_structure("churn-net-docs", spec)
# or straight from the estimator (extracts, uploads large tensors as binary, then PUTs the spec)
client.models.upload_structure("churn-net-docs", clf)curl -X PUT https://api.kaimei.ai/v1/models/churn-net-docs/structure \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"format": "kaimei.structure.v1", "family": "network", "task": "binary_classification",
"feature_names": ["tenure_months", "monthly_spend", "support_tickets"], "labels": ["stay", "churn"],
"layers": [
{"id": "input", "type": "input", "names": ["tenure_months", "monthly_spend", "support_tickets"]},
{"id": "hidden", "type": "dense", "units": 4, "activation": "relu", "weight": {"$tensor": "hidden.weight"}, "bias": [0.1, -0.2, 0.05, 0.0]},
{"id": "output", "type": "dense", "units": 1, "activation": "sigmoid", "weight": [[-1.2, 0.9, 0.7, 1.1]], "bias": [-0.3]}
]
}'{
"object": "model.structure",
"model_id": "mdl_ec2f5d3a8098280a8964",
"architecture": "network",
"fidelity": "exact",
"runnable": true,
"stats": {
"nodes": 11,
"parameters": 27,
"tensors": 10,
"layers": 4
},
…: …
}#Remove a structure
/v1/models/{model}/structureAPI key or sessionDelete the uploaded spec and its binary tensors. The model stays registered; it is no longer executable unless it has a remote or built-in inference.
Path parameters
Model id or slug.
Returns
object: "model.structure".client.models.delete_structure("churn-net-docs")curl -X DELETE https://api.kaimei.ai/v1/models/churn-net-docs/structure \
-H "Authorization: Bearer $KAIMEI_API_KEY"{
"id": "mdl_ec2f5d3a8098280a8964",
"object": "model.structure",
"deleted": true
}#Trace one prediction
/v1/models/{model}/structure/traceAPI key or sessionReplay one input through the architecture and narrate it: the output, the drivers of the final score (which incoming node moved it, and by how much), a narrative sentence per node, and a frame per node with its activation vector, a summary (size, max, mean, L2, active units, argmax), an intensity in [0, 1] for colouring and, for linear layers, the contributions of each input node and the bias. For a forest, tree picks the tree to walk; a trace still averages every tree for the output.
For large models pass detail: "summary" and nodes: frames then carry statistics and a sampled vector for the chosen nodes only; the output head is always included.
When the API cannot run the structure and the model's agent runs the real model, the agent answers and the trace says source: "agent": frames for the steps it reported, with no narrative or drivers. A language model's text longer than its max_length is 400 invalid_input; any other model's is cut, and output.input_truncated says so.
Path parameters
Model id or slug.
Body parameters
A feature row.
full or summary.Node ids to return frames for (up to 2,000). Default: every node.
Member of a forest to walk.
Returns
model.structure.trace.trace = client.models.trace_structure("churn-net-docs",
input={"tenure_months": 3, "monthly_spend": 91.5, "support_tickets": 4})
print(trace.output) # {'label': 'churn', 'probability': 0.9163, ...}
for line in trace.narrative:
print(line["text"])
# tenure months enters as 3.0.
# tenure months: (3 − 24) / 18 = -1.167.
# hidden = -0.2533. relu: relu(-0.2533) = 0. ...curl -X POST https://api.kaimei.ai/v1/models/churn-net-docs/structure/trace \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"input": {"tenure_months": 3, "monthly_spend": 91.5, "support_tickets": 4}}'{
"object": "model.structure.trace",
"model_id": "mdl_ec2f5d3a8098280a8964",
"runnable": true,
"detail": "full",
"output": {
"label": "churn",
"probability": 0.91626639,
"probabilities": {"stay": 0.08373361, "churn": 0.91626639},
"value": 0.91626639,
"threshold": 0.5
},
"drivers": [
{
"from": "hidden__relu",
"label": "relu",
"input": [0.0, 1.735, 0.0, 1.0283],
"delta": 2.6927,
"share": 0.8998
},
{
"from": "bias",
"label": "Bias",
"input": -0.3,
"delta": -0.3,
"share": 0.1002
}
],
"narrative": [
{
"node_id": "tenure_months",
"label": "tenure months",
"text": "tenure months enters as 3.0."
},
{
"node_id": "standardize__tenure_months",
"label": "tenure months",
"text": "tenure months: (3 − 24) / 18 = -1.167."
},
…
],
"frames": [
{
"index": 6,
"node_id": "hidden",
"op": "dense",
"label": "hidden",
"intensity": 0.5611,
"value": [-0.2533, 1.735, -0.8983, 1.0283],
"summary": {
"size": 4,
"max_abs": 1.735,
"mean_abs": 0.97875,
"l2": 2.2224,
"active": 4,
"argmax": 1
},
"text": "hidden = -0.2533.",
"contributions": [
{
"from": "standardize__tenure_months",
"input": -1.1667,
"delta": [-0.9333, 0.7, -0.1167, -0.35],
"label": "tenure months"
},
…,
{
"from": "bias",
"input": [0.1, -0.2, 0.05, 0.0],
"delta": [0.1, -0.2, 0.05, 0.0],
"label": "Bias"
}
]
},
…
],
"path": [
"tenure_months", "monthly_spend", "support_tickets",
"standardize__tenure_months", …, "output__sigmoid"
]
}#Silence a stage
/v1/models/{model}/structure/responsibilityAPI key or sessionRun one input, then set each named stage to zero and report how far the output moves. Omitted nodes means one probe per layer, up to 24. Each probe carries node_id, delta, share, flipped and the output after that stage was silenced. Probes are ordered by delta, largest first. For a classifier a delta of 1 is a certain answer turned into another; a language model is measured the same way, over the probabilities of its next token. A stage without which the model has no finite answer at all (a text with every token gone has nothing to average) is listed in skipped with reason: "non_finite_output", and the other stages are still measured. Needs the API to run the structure.
Path parameters
Model id or slug.
Body parameters
A feature row.
Stage ids to silence, up to 24. Default: one per layer.
Member of a forest. Ignored for other families.
Returns
model.structure.responsibility record: baseline, probes and skipped (each {node_id, label, op, layer, position, reason}).curl -X POST https://api.kaimei.ai/v1/models/churn-net-docs/structure/responsibility \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"input": {"tenure_months": 3, "monthly_spend": 91.5, "support_tickets": 4}}'{
"object": "model.structure.responsibility",
"baseline": {"label": "churn", "probability": 0.9163},
"probes": [
{
"node_id": "hidden",
"label": "hidden",
"op": "dense",
"delta": 0.41,
"share": 0.62,
"flipped": false,
"output": {"label": "churn"}
},
…
],
"skipped": []
}#How a forest voted
/v1/models/{model}/structure/votesAPI key or sessionEvery tree's leaf for one input: its value, label, weight and the features on the path, plus whether the forest agrees. A large forest returns a sample of ballots (ballots_truncated) and, for a score, bins instead of every number.
Path parameters
Model id or slug.
Body parameters
A feature row.
Which tree the scene draws. The votes are still every tree's.
Returns
model.structure.votes record: output, agreement, trees, features and ballots.curl -X POST https://api.kaimei.ai/v1/models/churn-net-docs/structure/votes \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"input": {"tenure_months": 3, "monthly_spend": 91.5, "support_tickets": 4}}'{
"object": "model.structure.votes",
"trees": 100,
"agreement": 0.91,
"output": {"label": "churn"},
"features": [
{"feature": "tenure_months", "trees": 40, "share": 0.4}
],
"ballots": [
{
"index": 0,
"weight": 1.0,
"value": [0.12],
"label": "churn",
"features": ["tenure_months"]
}
],
"ballots_truncated": true
}#What every layer computes
/v1/models/{model}/structure/activationsAPI key or sessionRun one input through an uploaded network and return what each layer computed, the data behind the dashboard's Sight view. Every stage reports its intensity, active_fraction and top_units (strongest channels or units). The detailed stages (nodes, or by default up to 24 feature maps plus the output) add the Σ|a| energy of every channel, the maps of the channels strongest ones, a saliency map (mean |a| over channels) and, for vectors up to 4,096 units, every value.
Maps are area-averaged until their long side is at most max_side cells and travel as base64 uint8 with their value range; kaimei.activations.decode restores them. cam adds class attention (Grad-CAM) toward target at the named layers when it fits a time budget estimated from the forward pass; otherwise the stage says why.
A feature map's channels are ranked by energy, the sum of their values. The heads of an attention layer (a stage with role: "attention" and shape queries × keys × heads) cannot be: every query's weights add up to one. They are ranked by focus instead, the largest weight each query gives, averaged over the queries (1 when every query looks at one key), which the stage lists for every head and each map says of itself. channel_start reads the maps in their own order, a page at a time, which is how heads are usually wanted; the stage then says channel_start and channel_count.
nodes, channels and max_side can each be large, and not all at once: stages × max_side² × the larger of channels and 16 may come to 25,690,112 cells (64 stages of 32 maps at 112 cells a side); past that the request is 422 validation_error. When the model's agent runs it, the stages come from the agent and the record says source: "agent".
Path parameters
Model id or slug.
Body parameters
A feature row (an image for a CNN).
Stages to detail, up to 512. Default: the API picks the feature maps worth seeing (at most 24) plus the output.
Maps per detailed spatial stage, 1 to 64: the strongest, or from channel_start on.
Read the maps in their own order from this one, instead of the strongest. The way to page through every head of an attention layer.
Longest side of a shipped map, 4 to 256 cells.
Strongest units or channels listed for every stage, 1 to 256.
Up to 4 layer ids to compute class attention for.
Class index or label the attention explains. Default: the predicted class.
Returns
model.structure.activations record: output, target, stages, the ids of the detailed stages and forward_ms.A spatial stage of a CNN adds shape [h, w, c], energy per channel, maps (the strongest channels, each {channel, range, data}), a saliency map and, when requested, cam with the attention map or a skipped reason. An attention stage adds role, focus per head, and focus and peak on each map. Forests have no activations.
result = client.models.activations("cats-dogs-photos", input={"photo": photo}, channels=8)
print(result.output["label"], result.forward_ms)
for stage in result.stages:
if stage["detailed"]:
print(stage["node_id"], stage["op"], stage["intensity"], stage["top_units"][:3])
focused = client.models.activations("cats-dogs-photos", input={"photo": photo},
nodes=["features.17"], cam=["features.17"], target="dog")
# every head of an attention layer, in their own order, a page at a time
heads = client.models.activations("review-sentiment-v1", input={"review": "the plot was not good."},
nodes=["blocks.0.Softmax"], channel_start=0, channels=2)
stage = next(s for s in heads.stages if s["detailed"])
print(stage["role"], stage["shape"], stage["focus"]) # attention [16, 16, 2] [...]curl -X POST https://api.kaimei.ai/v1/models/churn-net-docs/structure/activations \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"input": {"tenure_months": 3, "monthly_spend": 91.5, "support_tickets": 4}, "units": 4}'{
"object": "model.structure.activations",
"model_id": "mdl_ec2f5d3a8098280a8964",
"runnable": true,
"output": {
"label": "churn",
"probability": 0.91626639,
"probabilities": {"stay": 0.08373361, "churn": 0.91626639},
"value": 0.91626639,
"threshold": 0.5
},
"target": {"index": 1, "label": "churn"},
"input": null,
"stages": [
{
"index": 0,
"node_id": "tenure_months",
"label": "tenure months",
"op": "input",
"kind": "input",
"units": 1,
"intensity": 0.6351,
"summary": {
"size": 1,
"max_abs": 3.0,
"mean_abs": 3.0,
"l2": 3.0,
"active": 1,
"argmax": 0
},
"active_fraction": 1.0,
"detailed": false,
"top_units": [
{"index": 0, "value": 3.0}
]
},
…,
{
"index": 6,
"node_id": "hidden",
"label": "hidden",
"op": "dense",
"kind": "linear",
"units": 4,
"intensity": 0.5611,
"summary": {
"size": 4,
"max_abs": 1.735,
"mean_abs": 0.97875,
"l2": 2.2224,
"active": 4,
"argmax": 1
},
"active_fraction": 1.0,
"detailed": true,
"top_units": [
{"index": 1, "value": 1.735},
{"index": 3, "value": 1.028333},
{"index": 2, "value": -0.898333},
{"index": 0, "value": -0.253333}
],
"values": {
"shape": [4],
"range": [-0.898333, 1.735],
"encoding": "u8",
"data": "PwD/Qw=="
}
},
…
],
"detailed": [
"standardize__tenure_months", "standardize__monthly_spend",
"standardize__support_tickets", "hidden", …
],
"channels": 8,
"max_side": 56,
"forward_ms": 0.47
}{
"index": 23,
"node_id": "blocks.0.Softmax",
"label": "Softmax",
"op": "Softmax",
"kind": "activation",
"units": 512,
"intensity": 0.7771,
"detailed": true,
"shape": [16, 16, 2],
"role": "attention",
"top_channels": [0, 1],
"energy": [16.0, 15.999999],
"focus": [0.398957, 0.395222],
"channel_start": 0,
"channel_count": 2,
"maps": [
{
"channel": 0,
"energy": 16.0,
"peak": 0.557427,
"focus": 0.398957,
"shape": [16, 16],
"range": [0.0, 0.55742669],
"encoding": "u8",
"data": "KTANd0QnFRItLgAA…"
},
…
],
"saliency": {
"shape": [16, 16],
"range": [0.0, 0.41145987],
"encoding": "u8",
"data": "Iz0RUkEmGRbeNAAA…"
}
}#List binary tensors
/v1/models/{model}/structure/tensorsAPI key or sessionThe binary tensors uploaded for this model with their shapes, sizes, statistics and whether the current spec referenced them.
Path parameters
Model id or slug.
Returns
total_bytes.tensors = client.models.tensors("churn-net-docs")
print(tensors.total_bytes)
for item in tensors.data:
print(item["key"], item["shape"], item["dtype"], item["bytes"], item["referenced"])curl https://api.kaimei.ai/v1/models/churn-net-docs/structure/tensors \
-H "Authorization: Bearer $KAIMEI_API_KEY"{
"object": "list",
"data": [
{
"object": "model.structure.tensor",
"model_id": "mdl_ec2f5d3a8098280a8964",
"key": "hidden.weight",
"shape": [4, 3],
"dtype": "f32",
"numel": 12,
"bytes": 50,
"compression": "zlib",
"stats": {
"min": -0.7,
"max": 0.9,
"mean": 0.191667,
"std": 0.528297,
"l2": 1.946792,
"abs_mean": 0.491667,
"sparsity": 0.0,
"numel": 12,
"histogram": {
"counts": [2, 0, …],
"edges": [-0.7, -0.566667, …]
}
},
"referenced": true
}
],
"total_bytes": 50
}#Upload a binary tensor
/v1/models/{model}/structure/tensors/{key}API key or sessionUpload one weight tensor as raw little-endian bytes, zlib-compressed by default, and reference it from the spec as {"$tensor": "key"}. Keys are dotted module paths with an optional parameter name (encoder.layers.0.q.weight): letters, digits, . _ - : /, at most 200 characters.
Up to 250 million values per tensor and 512 MB compressed per upload; 20,000 uploads per hour per organization. Uploading the same key again replaces the tensor.
Path parameters
Model id or slug.
Tensor key.
Query parameters
Comma-separated dimensions, for example 768,3072.
f16, f32 or f64, little-endian; for a general model graph also i8, i16, i32, i64, u8 and bool.zlib or none.Body parameters
The buffer, with Content-Type: application/octet-stream.
Returns
model.structure.tensor with statistics.import numpy as np
W1 = np.array([[0.8, -0.3, 0.5], [-0.6, 0.9, 0.2], [0.1, 0.4, -0.7], [0.3, -0.2, 0.9]], dtype=np.float32)
client.models.upload_tensors("churn-net-docs", {"hidden.weight": W1})
# then PUT a spec that says "weight": {"$tensor": "hidden.weight"}python -c "import numpy as np, zlib, sys; sys.stdout.buffer.write(zlib.compress(np.load('w1.npy').astype('<f4').tobytes()))" > w1.z
curl -X PUT "https://api.kaimei.ai/v1/models/churn-net-docs/structure/tensors/hidden.weight?shape=4,3&dtype=f32&compression=zlib" \
-H "Authorization: Bearer $KAIMEI_API_KEY" \
-H "Content-Type: application/octet-stream" \
--data-binary @w1.z{
"object": "model.structure.tensor",
"model_id": "mdl_ec2f5d3a8098280a8964",
"key": "hidden.weight",
"shape": [4, 3],
"dtype": "f32",
"numel": 12,
"bytes": 50,
"compression": "zlib",
"stats": {
"min": -0.7,
"max": 0.9,
"mean": 0.191667,
"std": 0.528297,
"l2": 1.946792,
"abs_mean": 0.491667,
"sparsity": 0.0,
"numel": 12
},
"referenced": null
}#Delete a tensor
/v1/models/{model}/structure/tensors/{key}API key or sessionRemove one uploaded tensor. A spec that still references it becomes unusable until the tensor is re-uploaded.
Path parameters
Model id or slug.
Tensor key.
Returns
{model_id}:{key}.client.models.delete_tensor("churn-net-docs", "hidden.weight")curl -X DELETE https://api.kaimei.ai/v1/models/churn-net-docs/structure/tensors/hidden.weight \
-H "Authorization: Bearer $KAIMEI_API_KEY"{
"id": "mdl_ec2f5d3a8098280a8964:hidden.weight",
"object": "model.structure.tensor",
"deleted": true
}#Read a parameter
/v1/models/{model}/structure/parameters/{parameter_id}API key or sessionA window of one weight or bias tensor. Parameter ids come from the structure document (hidden.weight, standardize__tenure_months.center). JSON windows are limited to 20,000 values; format=f32 streams raw little-endian float32 bytes for windows up to 4 million values, with the geometry in X-Kaimei-* headers.
A picture of any tensor. With side, the tensor comes back as a matrix (its first axis down, the rest across, leading axes of one not counted) of at most side cells a side, each cell standing for a block of values by the strongest of them, sign kept, or their mean. rows and cols show a window, down to one value a cell. The values stay on the server (source: "values"). For a tensor sent as its shape, it returns the picture its owner sent with it, if there is one: the whole tensor, as fine as it was made, with no window (source: "summary", available: false).
Path parameters
Model id or slug.
Parameter id from the structure document.
Query parameters
First value, in flattened row-major order.
Values to return. At most 20,000 for JSON, 4,000,000 for f32.
json or f32.Include summary statistics (JSON only).
Side of a downsampled 2-D preview to include, 0 to 256 (JSON only).
Return a picture of the tensor of at most this many cells a side, 1 to 512, instead of its values.
With side: the window to show, first:last in the matrix's rows and columns (last may be left out). 400 invalid_window otherwise.
With side: strongest or mean.
Member of a forest.
Returns
model.structure.parameter, or application/octet-stream with headers X-Kaimei-Offset, X-Kaimei-Count, X-Kaimei-Total, X-Kaimei-Shape, X-Kaimei-Dtype and X-Kaimei-Available. With side, a model.structure.parameter.view: shape, dtype, matrix (rows and columns), available, stats, source, window, cell (values a cell stands for, and pool) and view ({shape, range, encoding, data}, decoded by kaimei.activations.decode).window = client.models.structure_parameter("churn-net-docs", "hidden.weight", limit=6, stats=True)
print(window.shape, window.total, window.values) # [4, 3] 12 [0.8, -0.3, 0.5, -0.6, 0.9, 0.2]
# the whole tensor as float32, fetched as binary
weights = client.models.structure_parameter_values("churn-net-docs", "hidden.weight")
weights.reshape(window.shape)
# a picture of a tensor of any size, at most side cells a side
view = client.models.parameter_view("review-sentiment-v1", "blocks.0.up.MatMul.weight", side=8)curl "https://api.kaimei.ai/v1/models/churn-net-docs/structure/parameters/hidden.weight?limit=6&stats=true" \
-H "Authorization: Bearer $KAIMEI_API_KEY"
# raw float32
curl "https://api.kaimei.ai/v1/models/churn-net-docs/structure/parameters/hidden.weight?format=f32&limit=1000000" \
-H "Authorization: Bearer $KAIMEI_API_KEY" -D - --output hidden.weight.f32
# X-Kaimei-Offset: 0 X-Kaimei-Count: 12 X-Kaimei-Total: 12 X-Kaimei-Shape: 4,3 X-Kaimei-Dtype: f32{
"object": "model.structure.parameter",
"id": "hidden.weight",
"model_id": "mdl_ec2f5d3a8098280a8964",
"node_id": "hidden",
"name": "weight",
"role": "weight",
"label": "hidden weight",
"shape": [4, 3],
"offset": 0,
"count": 6,
"total": 12,
"available": true,
"values": [0.8, -0.3, 0.5, -0.6, 0.9, 0.2],
"stats": {
"min": -0.7,
"max": 0.9,
"mean": 0.191667,
"std": 0.528297,
"l2": 1.946792,
"abs_mean": 0.491667,
"sparsity": 0.0,
"numel": 12
}
}{
"object": "model.structure.parameter.view",
"id": "blocks.0.up.MatMul.weight",
"model_id": "mdl_35636a1ada02058bf5fd",
"node_id": "blocks.0.up.MatMul",
"shape": [32, 64],
"dtype": "f16",
"matrix": [32, 64],
"available": true,
"stats": {
"min": -0.324219,
"max": 0.335449,
"mean": -0.00401584,
…: …
},
"source": "values",
"window": {
"rows": [0, 32],
"cols": [0, 64]
},
"cell": {"rows": 4, "cols": 8, "pool": "strongest"},
"view": {
"shape": [8, 8],
"range": [-0.32421875, 0.33544922],
"encoding": "u8",
"data": "ISYZ8xXoEf4bLhwm8t/O0wDY…"
}
}