~/bend-docscommunity

gen.bend checks

raw source on the hub · import bend-ml-llama@0.3.0.0/gen.bend as Gen

bend-ml-llama, generation: the ids of a prompt run through main.bend's forward pass position by position (one key/value cache per layer), and then each new token is chosen from the logits and fed back in: the argmax (greedy), or a draw with temperature, top-k and top-p from a seed (main.bend's sampler). Generation stops after n tokens, or after the end-of-text token (id 0), as transformers' generate does; in a chat after <|im_end|> (id 2) too.

import ./main.bend as M import ./gen.bend as G G.generate(c, model, G.Greedy{}, False{}, ids, n) # G.Gen{toks, m}: the new tokens and the model G.tokens(c, model, G.Draw{0.8, 50n, 0.95, 42}, False{}, ids, n) # the same loop, one token at a time (G.Toks) G.turn(c, G.Greedy{}, cap, G.Chat.start(c, cap), model, ids, n) # a conversation's next answer (G.Turn); it # hands the conversation back, caches and all

The model is a parameter, so the caller chooses its loader. Nothing here prints or reads a clock: a program that shows each token as it comes walks G.Toks, as llama.bend's demo/gen.bend does.

A conversation (the last section) keeps its caches between the turns: a turn's ids go through the layers in one pass on top of the positions already there, and the answer is generated after them, so no turn runs the ones before it again. Its answers are the tokens that tokens gives for the whole conversation up to each of them.

4 imports
import Base
import bend-ml-tensor-array@0.1.20.0/main.bend as TA
import ./main.bend as M
import ./prompt.bend as P

Types

type Pick source · line 28 · raw

Data

how each new token is chosen: the argmax (greedy, transformers' do_sample = False), or a draw (main.bend's sample) with temperature temp > 0, the k >= 1 largest logits, top-p p in (0, 1], and draw j of the seed for the j-th new token

type Chosen source · line 33 · raw

Data

a chosen token: its id and logit (am), and the candidates it was drawn from, largest first (cs; none when greedy)

type Toks source · line 78 · raw

@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> Type

The new tokens, one at a time, as a generator yields them. Tok: a new token (am: its id and logit; cs: the candidates of a draw), the model handed back, and the rest of the generation, which runs when it is given that model again (rest(m)): the next forward pass happens then, not before. End: no more tokens, and the model. A program may stop at any Tok and keep the model; the caches go with the rest it does not ask for. (A closure can be called once in Bend, so a callback per token is not an option: the caller walks the tokens instead.)

type Gen source · line 145 · raw

@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> Type

the new tokens (the id and logit of each, in order) and the model handed back

type Fit source · line 194 · raw

@-k:Nat -> @-room:Nat -> Type

k positions out of room: what is left, with the equation that says so (room = k + rest), or Short when k is more than the room. The equation is what turns a count made at run time into the proofs the caches ask for

type Chat source · line 239 · raw

@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> Type

A conversation between two answers: its t positions are in the caches (one per layer, with room for cap), and pend is the last answer's last token when it still has to go through the model: it does not after a stop token, and there is none before the first turn

type Why source · line 250 · raw

Data

why an answer ended. Stop: after <|im_end|> (id 2) or the end-of-text token (id 0). Count: the tokens asked for were generated. Full: the caches are full, so the last token has no position to run at. Unfit: nothing ran, since the turn's ids (need of them, the pending token among them) are more than the room. Idle: nothing ran, since the turn had no id or no token was asked for

type Turn source · line 260 · raw

@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @-cap:Nat -> Type

An answer, one token at a time as Toks hands them: Said is a new token, the model handed back and the rest of the answer, which runs when it is given that model again. Over: no more tokens, why, the model, the positions the caches still have room for, and the conversation for the next turn

Definitions

def Chosen.am source · line 36 · raw

@x:Chosen -> 0xf3d14d7bad8726b73c5072bce72b710c/main.AM

def Chosen.cs source · line 41 · raw

@x:Chosen -> List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.Cand>

def drawn source · line 46 · raw

@cs:List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.Cand> -> @s:0xf3d14d7bad8726b73c5072bce72b710c/main.Cand -> Chosen

def choose source · line 53 · raw

@pk:Pick -> @lg:List<&2, F32> -> @+j:Nat -> Chosen

the j-th new token, from the logits lg. Greedy is the argmax alone. A draw is main.bend's sample in its two halves (topk, then sample.cs), so that the candidates are computed once and handed out with the token.

def left_after source · line 62 · raw

@+id:Nat -> @+left:Nat -> Nat

the tokens left to generate after the token id: none after the end-of-text token (id 0)

def left_chat source · line 66 · raw

@chat:Bool -> @+id:Nat -> @+left:Nat -> Nat

the same in a chat, where nothing more is generated after <|im_end|> (id 2) either

def loop source · line 85 · raw

@fuel:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @+chat:Bool -> @-cap:Nat -> @+t:Nat -> @-inv:{Nat.add(t, fuel) == cap : Nat} -> @+tok:Nat -> @queue:List<&2, Nat> -> @left:Nat -> @+j:Nat -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> Toks<c>

tok runs at position t; queue: the prompt ids still to feed; left: tokens still to generate, j generated so far. The caches have room for cap positions, and fuel counts the steps left with t + fuel = cap (inv), which gives each step its proof that t < cap. The forward pass is matched by Out.then, so the loop recurses inside its closure.

def tokens.seq source · line 104 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+pk:Pick -> @+chat:Bool -> @+ids:List<&2, Nat> -> @+n:Nat -> Toks<c>

the prompt's positions one after another (main.bend's forward_pre), then the new tokens: the path of a prompt of one id, where there is nothing to share

def after source · line 113 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @+chat:Bool -> @-cap:Nat -> @+plen:Nat -> @+lp:Nat -> @-inv:{Nat.add(plen, 1n+lp) == cap : Nat} -> @o:0xf3d14d7bad8726b73c5072bce72b710c/prompt.OutP<c, cap, plen> -> Toks<c>

after a prompt of plen ids that ran in one pass (prompt.bend): its last position's logits choose the first new token, and the loop goes on at position plen with the caches the pass grew; 1 + lp tokens were asked for

def tokens.pick source · line 121 · raw

@pp:Nat -> @+lp:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+pk:Pick -> @+chat:Bool -> @+ids:List<&2, Nat> -> Toks<c>

pp: the prompt's ids but one. One id runs position by position; two or more go through each layer together, with room for the prompt and the 1 + lp new tokens

def tokens.by source · line 130 · raw

@n:Nat -> @plen:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+pk:Pick -> @+chat:Bool -> @+ids:List<&2, Nat> -> Toks<c>

n: the tokens asked for; plen: the prompt's ids. No token asked for, or an empty prompt, gives none (a Llama needs one position to start from)

def tokens source · line 141 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+pk:Pick -> @+chat:Bool -> @+ids:List<&2, Nat> -> @+n:Nat -> Toks<c>

the new tokens after the prompt ids, n at most, one at a time: the prompt runs first, then the first token is chosen

def Gen.then source · line 149 · raw

@-R:Type -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @g:Gen<c> -> @k:(@_:List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.AM> -> @_:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> R) -> R

continues with the tokens and the model

def Gen.toks source · line 155 · raw

@-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @g:Gen<c> -> List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.AM>

the tokens alone (the model is dropped)

def take.go source · line 161 · raw

@n:Nat -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @s:Toks<c> -> @acc:List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.AM> -> Gen<c>

acc: the tokens taken so far, newest first. The n-th token is taken without asking for the rest.

def take source · line 173 · raw

@n:Nat -> @-c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @s:Toks<c> -> Gen<c>

the first n tokens of s, and the model: all of s if it ends by then, and no forward pass after the n-th

def generate source · line 177 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+pk:Pick -> @+chat:Bool -> @+ids:List<&2, Nat> -> @+n:Nat -> Gen<c>

n new tokens after the prompt ids, all at once (fewer after a stop token)

def ids_of source · line 181 · raw

@toks:List<&2, 0xf3d14d7bad8726b73c5072bce72b710c/main.AM> -> List<&2, Nat>

the ids of tokens

def succ_eq source · line 199 · raw

@-a:Nat -> @-b:Nat -> @e:{a == b : Nat} -> {1n+a == 1n+b : Nat}

a = b gives 1 + a = 1 + b

def fit.up source · line 204 · raw

@-p:Nat -> @-q:Nat -> @f:Fit<p, q> -> Fit<1n+p, 1n+q>

one more on both sides: when k out of room leave rest, 1 + k out of 1 + room leave the same rest

def fit source · line 214 · raw

@k:Nat -> @room:Nat -> Fit<k, room>

k and room are counted down together. When k ends first, what is left of the room is the rest, and 0 + rest = rest holds by computing; when the room ends first, k was more than the room. On the way back every step adds one to both sides of the equation (fit.up)

def room_inv source · line 225 · raw

@+t:Nat -> @-k:Nat -> @-rest:Nat -> @-room:Nat -> @-cap:Nat -> @e:{Nat.add(k, rest) == room : Nat} -> @inv:{Nat.add(t, room) == cap : Nat} -> {Nat.add(Nat.add(t, k), rest) == cap : Nat}

t + room = cap and k + rest = room give (t + k) + rest = cap: after k more positions the room is rest. Proof: cap is t + room (inv) and room is k + rest (e), so the claim is (t + k) + rest = t + (k + rest), which is add_assoc

def room_le source · line 232 · raw

@+t:Nat -> @+k:Nat -> @+rest:Nat -> @-room:Nat -> @-cap:Nat -> @e:{Nat.add(k, rest) == room : Nat} -> @inv:{Nat.add(t, room) == cap : Nat} -> {True{} == 0x5c5507d77be847e1e454ec33ab54e087/main.leb(Nat.add(t, k), cap) : Bool}

and so the k positions fit: t + k <= cap. Proof: cap is (t + k) + rest (room_inv), and a number is at most itself plus another (tensor-array's leb_up over leb_refl)

def Chat.start source · line 243 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+cap:Nat -> Chat<c, cap>

a conversation with no turn yet: empty caches with room for cap positions

def is_stop source · line 264 · raw

@+id:Nat -> Bool

def pend_of source · line 268 · raw

@+tok:Nat -> Maybe<&2, Nat>

an answer's last token as the conversation keeps it: a stop token is not kept

def turn.go source · line 274 · raw

@room:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @-cap:Nat -> @+t:Nat -> @-inv:{Nat.add(t, room) == cap : Nat} -> @+tok:Nat -> @left:Nat -> @+j:Nat -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> Turn<c, cap>

tok: the answer's last token so far, which runs at position t when another is asked for (left of them; j so far). room counts the positions left, with t + room = cap (inv), as loop's fuel does. The answer is over when no token is left to generate, or when no position is left to run tok at

def turn.first source · line 289 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @-cap:Nat -> @+t:Nat -> @+room:Nat -> @-inv:{Nat.add(t, room) == cap : Nat} -> @+lp:Nat -> @o:0xf3d14d7bad8726b73c5072bce72b710c/prompt.OutP<c, cap, t> -> Turn<c, cap>

after the turn's ids ran in one pass: the last position's logits choose the answer's first token, and the answer goes on at position t with the caches the pass grew; 1 + lp tokens were asked for

def turn.fit source · line 297 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @-cap:Nat -> @+t:Nat -> @+k:Nat -> @+room:Nat -> @f:Fit<k, room> -> @-inv:{Nat.add(t, room) == cap : Nat} -> @+pend:Maybe<&2, Nat> -> @+ids:List<&2, Nat> -> @+lp:Nat -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> Turn<c, cap>

ids: the k ids the turn feeds, at positions t .. t + k - 1. When they fit in the room (f), they go through each layer together (prompt.bend); when they do not, nothing runs and the conversation is handed back as it was

def turn.by source · line 305 · raw

@n:Nat -> @k:Nat -> @+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @-cap:Nat -> @+t:Nat -> @+room:Nat -> @-inv:{Nat.add(t, room) == cap : Nat} -> @+pend:Maybe<&2, Nat> -> @+ids:List<&2, Nat> -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> Turn<c, cap>

n: the tokens asked for; k: the ids the turn feeds

def turn.room source · line 316 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @-cap:Nat -> @+t:Nat -> @f:Fit<t, cap> -> @n:Nat -> @+pend:Maybe<&2, Nat> -> @+ids:List<&2, Nat> -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @cs:List<&1, 0xf3d14d7bad8726b73c5072bce72b710c/main.KV<cap, t>> -> Turn<c, cap>

f: the room the caches have left after their t positions (cap = t + room). A conversation never holds more than cap positions, so Short does not happen; it is answered as a full conversation

def feed source · line 324 · raw

@pend:Maybe<&2, Nat> -> @ids:List<&2, Nat> -> List<&2, Nat>

the ids a turn feeds: the last answer's pending token, when there is one, then the turn's own

def turn source · line 338 · raw

@+c:0xf3d14d7bad8726b73c5072bce72b710c/main.Cfg -> @+pk:Pick -> @+cap:Nat -> @ch:Chat<c, cap> -> @m:0xf3d14d7bad8726b73c5072bce72b710c/main.Model<c> -> @+ids:List<&2, Nat> -> @n:Nat -> Turn<c, cap>

The conversation's next answer, n tokens at most, one at a time. ids continue the conversation after the last answer: for the first turn the conversation's ids in the chat template (tok.bend's chat), and after an answer <|im_end|> and what follows it (tok.bend's chat_next); the answer's last token goes before them when it was not a stop token. They run through each layer in one pass, on top of the caches, and each new token then runs at its position, until a stop token, the count, or the end of the room (cap positions: Chat.start's). The draws of a Pick count from the answer's first token, so an answer is the tokens that tokens gives for the whole conversation up to it.