Generative models
Language models, generators and embedding models: what a prediction of each looks like, and how POST …/generate runs the loop they are used in.
Four tasks sit beside classification and regression. Register a model with one of them, or pass it as task= to client.models.upload; a Hugging Face model says its own.
| task | The model's output | A prediction is |
|---|---|---|
text_generation | Scores for every token of the vocabulary, for each position or for the last one. Read at the last real token, through a softmax unless head.link says otherwise. | The next token: {kind: "token", token, token_id, probability, top, classes} |
sequence_to_sequence | An encoder-decoder (T5, BART, Marian and their kind): the encoder reads the input, and the decoder scores the next token of the answer it is writing, held in a feature of its own (generated: true) that no caller gives. | The first token of the answer, as for text_generation; generate writes the whole answer |
generation | Any tensor: a picture, a series, an array. outputs.kind (image, sequence, tensor) and outputs.value_range say what it is. | {kind, shape, size, summary, preview}, and values when it has 64 or fewer |
embedding | A vector. | {kind: "embedding", size, norm, summary, preview}, and values when it has 64 or fewer |
- Many classes. A classifier or a language model with more than 200 classes lists its 20 most likely in
topandprobabilities, with the whole count inclassesandtruncated: true. - Pictures and previews. A picture is reported height × width × channels, whatever order the model keeps it in.
previewis bytes with the range they were quantised from:value = range[0] + byte / 255 × (range[1] − range[0]); a picture's is at most 96 cells a side.kaimei.activations.decodereads it. - Run elsewhere. A generator run by an endpoint or an agent says the shape of what it returns in
outputs.shape, andoutputs.layout: "chw"when that is a picture kept channels first. - What applies. A language model's next token, and an encoder-decoder's, can be explained like a class. Analyses, insights and performance against outcomes need one class or one number for each row, and answer
422 not_applicable_for_taskfor these four tasks. Agenerationorembeddingmodel has no explanation methods. A language model's text longer than itsmax_lengthis400 invalid_input, never cut.
prediction = client.models.predict("review-writer-v1", input={"prompt": "the film was"})
print(prediction.output["token"], prediction.output["probability"]) # never 0.155curl https://api.kaimei.ai/v1/models/review-writer-v1/predict \
-H "Authorization: Bearer $KAIMEI_API_KEY" -H "Content-Type: application/json" \
-d '{"input": {"prompt": "the film was"}}'{
"kind": "token",
"label": "never",
"token": "never",
"token_id": 37,
"class_index": 37,
"probability": 0.155167,
"top": [
{"token_id": 37, "token": "never", "probability": 0.155167},
{"token_id": 25, "token": "hardly", "probability": 0.11253},
{"token_id": 38, "token": "not", "probability": 0.112161},
…
],
"probabilities": {"never": 0.155167, "hardly": 0.11253, …: …},
"classes": 62,
"truncated": true,
"target_label": "never",
"target_index": 37,
"value": 0.155167,
"name": "next token"
}{
"kind": "image",
"shape": [16, 16, 1],
"size": 256,
"summary": {
"min": 0.00011185,
"max": 0.91657668,
"mean": 0.26136955,
"std": 0.2985545
},
"preview": {
"shape": [16, 16, 1],
"range": [0.00011185, 0.91657668],
"encoding": "u8",
"data": "AAAAAAAAAAAAAAAAAAAAAAAA…"
},
"value": 0.26136955,
"value_range": [0.0, 1.0],
"name": "picture"
}#Generate
One pass of a generative model is not what it is used for. POST /v1/models/{model}/generate runs the loop around the passes and reports every step. Each step is a real pass of the model wherever it runs (this server's processors, its endpoint, its agent), so any step can be opened in the model's trace and activations by sending that step's input.
- A language model writes token by token. Each step reports the token chosen, its probability, the 5 likeliest tokens at that step (
top), the time it took and thetextwritten so far.temperature0 always takes the likeliest token; above it tokens are drawn at random, reproducibly fromseed, among thetop_klikeliest and those that together holdtop_pof the probability. It writesstepstokens (32 when left out, at most 512).stoppedsays why it ended:steps,stop_token(one ofstop_tokens, or the tokenizer's end-of-text token),length(the prompt and what was written filledmax_length) ortime. The token that ends a text is a step and is named instop_token, but is not part ofgenerated. - A one-pass generator (a decoder, a GAN's generator, an autoencoder) is run once. A tensor or sequence feature left out of
inputis given its baseline, or drawn fromseedwhen it has none;draw: ["latent"]draws it although it has one. Drawn values stay inside the feature'sminandmax, and what was filled in comes back asinput(up to 4,096 values), so the same picture can be asked for again or moved a little.seeds(up to 16) draws once for each seed in the same request and returnsvariations. - An encoder-decoder writes an answer of its own. The input is held as it was given, and the decoder writes token by token from its first token, as a language model does, until its end-of-text token,
stepstokens, or itsmax_length. The answer isgenerated(alsotext), withinput(what was read) andsource(the text feature it was read from) in place ofprompt; the profile'sgeneration.typeisseq2seq. - A diffusion model names its loop in the structure's
sampler(see the graph spec), which the client fills from adiffusersscheduler (sampler=, or the pipeline itself) with the scheduler's own noise schedule. Kaimei samples with DDIM and returns up to 16 pictures along the way, each with itstimestepandnoise_level.
Time. The whole loop stays within 45 seconds. A language model too slow for the tokens asked for stops early. A diffusion model plans fewer, evenly spaced steps from the time its first pass took, so that it still goes all the way from noise to a picture, and stops when the time is up. Either says stopped: "time". The loop is the structure's whoever runs the passes: a diffusion model's sampler and an encoder-decoder's end-of-text token are read from the structure the model was registered with, also for a model its endpoint or its agent runs. A one-pass generator or a denoiser given a text longer than the model reads says so in output.input_truncated, as a prediction does.
A generation is a prediction. It is logged as one, with what was made rather than every step, and the answer carries its prediction_id. It is one call against the hourly predictions limit and one prediction toward the month's predictions_per_month, however many steps or seeds it takes; past the quota, 403 quota_exceeded. Other tasks, and seeds for a language model, an encoder-decoder or a denoiser, are 422 not_applicable_for_task.
run = client.models.generate("review-writer-v1", input={"prompt": "the film was"},
steps=6, temperature=0.7, top_k=20, seed=3)
for step in run.steps:
print(step["token"], step["probability"], [alt["token"] for alt in step["top"]])
print(run.text, run.stopped) # the film was hardly messy stop_token
print(run.prediction_id) # the generation, logged as one prediction
# three pictures from one generator, one for each seed; the latent drawn although it has a baseline
strip = client.models.generate("shape-painter-v1", seeds=[1, 2, 3], draw=["latent"])
for variation in strip.variations:
print(variation["seed"], variation["input"]["latent"][:2], variation["output"]["shape"])curl https://api.kaimei.ai/v1/models/review-writer-v1/generate \
-H "Authorization: Bearer $KAIMEI_API_KEY" -H "Content-Type: application/json" \
-d '{"input": {"prompt": "the film was"}, "steps": 6, "temperature": 0.7, "top_k": 20, "seed": 3}'
# three pictures from one generator, one for each seed
curl https://api.kaimei.ai/v1/models/shape-painter-v1/generate \
-H "Authorization: Bearer $KAIMEI_API_KEY" -H "Content-Type: application/json" -d '{"seeds": [1, 2, 3], "draw": ["latent"]}'{
"object": "model.generation",
"model_id": "mdl_eaf0c1ca3d54c3f337ff",
"kind": "text",
"feature": "prompt",
"prompt": {
"tokens": [
{"token_id": 54, "token": "the"},
{"token_id": 20, "token": "film"},
{"token_id": 58, "token": "was"}
],
"text": "the film was"
},
"steps": [
{
"index": 0,
"position": 3,
"token_id": 25,
"token": "hardly",
"probability": 0.11253,
"top": [
{
"token_id": 37,
"token": "never",
"probability": 0.155167
},
{
"token_id": 25,
"token": "hardly",
"probability": 0.11253
},
…
],
"ms": 1.44,
"text": "hardly"
},
{
"index": 1,
"position": 4,
"token_id": 33,
"token": "messy",
"probability": 0.043593,
"top": …,
"ms": 1.09,
"text": "hardly messy"
},
{
"index": 2,
"position": 5,
"token_id": 61,
"token": ".",
"probability": 0.437722,
"top": …,
"ms": 1.0
}
],
"tokens": [54, 20, 58, 25, 33],
"generated": "hardly messy",
"text": "the film was hardly messy",
"stop_token": {"token_id": 61, "token": "."},
"stopped": "stop_token",
"requested_steps": 6,
"ms": 2.54,
"seed": 3,
"prediction_id": "pred_fc3c1be4068980b28307"
}