File size: 24,764 Bytes
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
 
 
 
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
999ff90
 
 
 
 
dfb774a
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
999ff90
 
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
 
 
dfb774a
 
 
 
 
 
999ff90
 
dfb774a
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
999ff90
dfb774a
999ff90
dfb774a
 
 
999ff90
 
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
999ff90
dfb774a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
func.decl import("input") @require(%condition: i32, %diagnostic: buffer)

func.decl import("input") @encode(%text: buffer, %flags: i32, %capacity: i64) -> (buffer, i64)

func.decl import("json") @members(%json: buffer) -> (buffer, i32, i64)

func.decl import("json") @unescape(%source: buffer, %offset: i64, %length: i64, %target: buffer, %target_offset: i64) -> (i64)

func.decl import("runner") @execute_6(%stage: i32, %b0: hal.buffer, %b1: hal.buffer, %b2: hal.buffer, %b3: hal.buffer, %b4: hal.buffer, %b5: hal.buffer) -> (i64)

func.decl import("runner") @feedback(%slot: i32, %source: hal.buffer, %offset: i64, %length: i64) -> (i64)

func.def @check(%condition: i1) {
  %value = scalar.extui %condition : i1 to i32
  %error = buffer.constant utf8("text tiny accepts a/b/x messages without tools, reasoning or model options") : buffer
  func.call @require(%value, %error) : (i32, buffer)
  func.return
}

func.def @equal(%a: buffer, %b: buffer) -> (i1) {
  %a_length = buffer.length %a
  %b_length = buffer.length %b
  %same_length = index.cmp eq, %a_length, %b_length : offset
  %same = scf.if %same_length -> (i1) {
    %c0 = index.constant 0 : offset
    %compared = buffer.compare %a[%c0], %b[%c0], %a_length
    %c0_i32 = scalar.constant 0 : i32
    %matches = scalar.cmpi eq, %compared, %c0_i32 : i32
    scf.yield %matches : i1
  } else {
    %false = scalar.constant false : i1
    scf.yield %false : i1
  }
  func.return %same : i1
}

func.def @join(%a: buffer, %b: buffer) -> (buffer) {
  %c0 = index.constant 0 : offset
  %a_length = buffer.length %a
  %b_length = buffer.length %b
  %length = index.add %a_length, %b_length : offset
  %output = buffer.alloca<private> align(1) %length : buffer
  buffer.copy %a[%c0], %output[%c0], %a_length
  buffer.copy %b[%c0], %output[%a_length], %b_length
  func.return %output : buffer
}

func.def public @text_model_name_v1() -> (buffer) {
  %name = buffer.constant utf8("tiny-text") : buffer
  func.return %name : buffer
}

func.decl import("json") @parse_i64(%source: buffer, %offset: i64, %length: i64) -> (i64)

func.decl import("json") @parse_f64(%source: buffer, %offset: i64, %length: i64) -> (f64)

func.def public @text_request_defaults_v1(%options: buffer) -> (buffer) {
  %fields, %type, %count = func.call @members(%options) : (buffer) -> (buffer, i32, i64)
  %object = scalar.constant 2 : i32
  %empty_count = scalar.constant 0 : i64
  %typed = scalar.cmpi eq, %type, %object : i32
  %empty_options = scalar.cmpi eq, %count, %empty_count : i64
  %valid = scalar.andi %typed, %empty_options : i1
  func.call @check(%valid) : (i1)
  %empty = buffer.constant utf8("") : buffer
  func.return %empty : buffer
}

func.def public @text_request_options_v1(%defaults: buffer, %request: buffer, %nonce: i64) -> (buffer) {
  %origin = index.constant 0 : offset
  %c0 = index.constant 0 : index
  %c1 = index.constant 1 : index
  %zero64 = scalar.constant 0 : i64
  %one64 = scalar.constant 1 : i64
  %fields, %type, %count = func.call @members(%request) : (buffer) -> (buffer, i32, i64)
  %bounded = scalar.assume %count [range(%count, 0, 2147483647)] : i64
  %extent = index.cast %bounded : i64 to index
  %records = buffer.view %fields[%origin] : buffer -> view<[%extent]x5xi64>
  %capacity = buffer.length %request
  %storage = buffer.alloca<private> align(1) %capacity : buffer
  %temperature = buffer.constant utf8("temperature") : buffer
  %top_p = buffer.constant utf8("top_p") : buffer
  %top_k = buffer.constant utf8("top_k") : buffer
  %seed = buffer.constant utf8("seed") : buffer
  scf.for %i = [%c0 to %extent step %c1] {
    %begin = view.load %records[%i, 0] : view<[%extent]x5xi64> -> i64
    %length = view.load %records[%i, 1] : view<[%extent]x5xi64> -> i64
    %used = func.call @unescape(%request, %begin, %length, %storage, %zero64) : (buffer, i64, i64, buffer, i64) -> (i64)
    %bounded_used = scalar.assume %used [range(%used, 0, 9223372036854775807)] : i64
    %key_length = index.cast %bounded_used : i64 to offset
    %key = buffer.alloca<private> align(1) %key_length : buffer
    buffer.copy %storage[%origin], %key[%origin], %key_length
    %is_temperature = func.call @equal(%key, %temperature) : (buffer, buffer) -> (i1)
    %is_p = func.call @equal(%key, %top_p) : (buffer, buffer) -> (i1)
    %is_k = func.call @equal(%key, %top_k) : (buffer, buffer) -> (i1)
    %is_seed = func.call @equal(%key, %seed) : (buffer, buffer) -> (i1)
    %floating = scalar.ori %is_temperature, %is_p : i1
    %integer = scalar.ori %is_k, %is_seed : i1
    %value_begin = view.load %records[%i, 2] : view<[%extent]x5xi64> -> i64
    %value_length = view.load %records[%i, 3] : view<[%extent]x5xi64> -> i64
    %valid = scf.if %floating -> (i1) {
      %value = func.call @parse_f64(%request, %value_begin, %value_length) : (buffer, i64, i64) -> (f64)
      %greedy_temperature = scalar.constant 0.0 : f64
      %full_probability = scalar.constant 1.0 : f64
      %expected = scf.select %is_temperature, %greedy_temperature, %full_probability : f64
      %valid = scalar.cmpf oeq, %value, %expected : f64
      scf.yield %valid : i1
    } else {
      %valid = scf.if %integer -> (i1) {
        %value = func.call @parse_i64(%request, %value_begin, %value_length) : (buffer, i64, i64) -> (i64)
        %small = scalar.cmpi ule, %value, %one64 : i64
        %valid = scalar.ori %small, %is_seed : i1
        scf.yield %valid : i1
      } else {
        %true = scalar.constant true : i1
        scf.yield %true : i1
      }
      scf.yield %valid : i1
    }
    %condition = scalar.extui %valid : i1 to i32
    %error = buffer.constant utf8("text tiny supports temperature=0, top_p=1, top_k=0 or 1 and integer seeds") : buffer
    func.call @require(%condition, %error) : (i32, buffer)
  }
  func.return %defaults : buffer
}

func.def public @text_bind_request_v1(%state: buffer, %row: i32, %options: buffer) {
  func.return
}

func.def public @text_chat_reasoning_v1() -> (i32, i32, i32) {
  %c0 = scalar.constant 0 : i32
  func.return %c0, %c0, %c0 : i32, i32, i32
}

func.def public @text_chat_image_limits_v1() -> (i64, i64, i64, i64, i64) {
  %c0_i64 = scalar.constant 0 : i64
  func.return %c0_i64, %c0_i64, %c0_i64, %c0_i64, %c0_i64 : i64, i64, i64, i64, i64
}

func.def public @text_chat_begin_v1(%request: buffer, %tools: buffer) -> (buffer, i64) {
  %c0 = index.constant 0 : offset
  %c0_index = index.constant 0 : index
  %c1 = index.constant 1 : index
  %c0_i64 = scalar.constant 0 : i64
  %empty = buffer.constant utf8("") : buffer
  %tool_fields, %tool_type, %tool_count = func.call @members(%tools) : (buffer) -> (buffer, i32, i64)
  %no_tools = scalar.cmpi eq, %tool_count, %c0_i64 : i64
  func.call @check(%no_tools) : (i1)
  %fields, %type, %count64 = func.call @members(%request) : (buffer) -> (buffer, i32, i64)
  %count_limit = scalar.constant 2147483647 : i64
  %count_fits = scalar.cmpi ule, %count64, %count_limit : i64
  func.call @check(%count_fits) : (i1)
  %count_bounded = scalar.assume %count64 [range(%count64, 0, 2147483647)] : i64
  %count = index.cast %count_bounded : i64 to index
  %records = buffer.view %fields[%c0] : buffer -> view<[%count]x5xi64>
  %thinking = buffer.constant utf8("enable_thinking") : buffer
  %options = buffer.constant utf8("model_options") : buffer
  scf.for %i = [%c0_index to %count step %c1] {
    %begin64 = view.load %records[%i, 0] : view<[%count]x5xi64> -> i64
    %length64 = view.load %records[%i, 1] : view<[%count]x5xi64> -> i64
    %begin_bounded = scalar.assume %begin64 [range(%begin64, 0, 9223372036854775807)] : i64
    %length_bounded = scalar.assume %length64 [range(%length64, 0, 9223372036854775807)] : i64
    %begin = index.cast %begin_bounded : i64 to offset
    %length = index.cast %length_bounded : i64 to offset
    %has_key = index.cmp ne, %length, %c0 : offset
    %key = scf.if %has_key -> (buffer) {
      %decoded = buffer.alloca<private> align(1) %length : buffer
      %used64 = func.call @unescape(%request, %begin64, %length64, %decoded, %c0_i64) : (buffer, i64, i64, buffer, i64) -> (i64)
      %used_bounded = scalar.assume %used64 [range(%used64, 0, 9223372036854775807)] : i64
      %used = index.cast %used_bounded : i64 to offset
      %exact = buffer.alloca<private> align(1) %used : buffer
      buffer.copy %decoded[%c0], %exact[%c0], %used
      scf.yield %exact : buffer
    } else {
      scf.yield %empty : buffer
    }
    %is_thinking = func.call @equal(%key, %thinking) : (buffer, buffer) -> (i1)
    %is_options = func.call @equal(%key, %options) : (buffer, buffer) -> (i1)
    scf.if %is_thinking {
      %value_type = view.load %records[%i, 4] : view<[%count]x5xi64> -> i64
      %false_type = scalar.constant 5 : i64
      %disabled = scalar.cmpi eq, %value_type, %false_type : i64
      func.call @check(%disabled) : (i1)
    }
    scf.if %is_options {
      %value_begin64 = view.load %records[%i, 2] : view<[%count]x5xi64> -> i64
      %value_length64 = view.load %records[%i, 3] : view<[%count]x5xi64> -> i64
      %value_begin_bounded = scalar.assume %value_begin64 [range(%value_begin64, 0, 9223372036854775807)] : i64
      %value_length_bounded = scalar.assume %value_length64 [range(%value_length64, 0, 9223372036854775807)] : i64
      %value_begin = index.cast %value_begin_bounded : i64 to offset
      %value_length = index.cast %value_length_bounded : i64 to offset
      %value = buffer.alloca<private> align(1) %value_length : buffer
      buffer.copy %request[%value_begin], %value[%c0], %value_length
      %nested, %nested_type, %nested_count = func.call @members(%value) : (buffer) -> (buffer, i32, i64)
      %empty_options = scalar.cmpi eq, %nested_count, %c0_i64 : i64
      func.call @check(%empty_options) : (i1)
    }
  }
  func.return %empty, %c0_i64 : buffer, i64
}

func.def public @text_chat_message_v1(%raw: buffer, %role: buffer, %content: buffer, %parts: buffer, %reasoning: buffer, %calls: buffer, %state: i64, %ordinal: i64, %fields: i64, %part_count: i64) -> (buffer, i64, buffer) {
  %c0_i64 = scalar.constant 0 : i64
  %plain = scalar.cmpi eq, %fields, %c0_i64 : i64
  func.call @check(%plain) : (i1)
  %assistant_role = buffer.constant utf8("assistant") : buffer
  %user_role = buffer.constant utf8("user") : buffer
  %system_role = buffer.constant utf8("system") : buffer
  %assistant = func.call @equal(%role, %assistant_role) : (buffer, buffer) -> (i1)
  %user = func.call @equal(%role, %user_role) : (buffer, buffer) -> (i1)
  %system = func.call @equal(%role, %system_role) : (buffer, buffer) -> (i1)
  %ordinary = scalar.ori %user, %system : i1
  %supported = scalar.ori %ordinary, %assistant : i1
  func.call @check(%supported) : (i1)
  %terminal = buffer.constant utf8("#") : buffer
  %empty = buffer.constant utf8("") : buffer
  %suffix = scf.select %assistant, %terminal, %empty : buffer
  %fragment = func.call @join(%content, %suffix) : (buffer, buffer) -> (buffer)
  func.return %fragment, %state, %empty : buffer, i64, buffer
}

func.def public @text_chat_end_v1(%state: i64) -> (buffer, i64) {
  %empty = buffer.constant utf8("") : buffer
  func.return %empty, %state : buffer, i64
}

func.def public @text_render_tool_v1(%name: buffer, %arguments: buffer, %ordinal: i64) -> (buffer, i64) {
  %c0 = scalar.constant 0 : i32
  %c0_i64 = scalar.constant 0 : i64
  %error = buffer.constant utf8("text tiny accepts a/b/x messages without tools, reasoning or model options") : buffer
  func.call @require(%c0, %error) : (i32, buffer)
  %empty = buffer.constant utf8("") : buffer
  func.return %empty, %c0_i64 : buffer, i64
}

func.def public @text_parse_tools_v1(%schemas: buffer, %response: buffer, %truncated: i64) -> (buffer) {
  %empty = buffer.constant utf8("[]") : buffer
  func.return %empty : buffer
}

// The host supplies the previous raw prediction separately. On EOS it replaces
// the first assistant delimiter in this suffix; an open response consumes its
// last ordinary token before that delimiter. Full replay encodes both normally.
func.def public @text_prepare_input_v1(%text: buffer, %markers: buffer, %marker_count: i64, %format: i32, %boundary: i32, %capacity: i64) -> (buffer, i64, buffer) {
  %c0 = index.constant 0 : offset
  %c0_index = index.constant 0 : index
  %c1 = index.constant 1 : offset
  %c1_index = index.constant 1 : index
  %c0_i32 = scalar.constant 0 : i32
  %c1_i32 = scalar.constant 1 : i32
  %c2_i32 = scalar.constant 2 : i32
  %c1_i64 = scalar.constant 1 : i64
  %length = buffer.length %text
  %terminated = scalar.cmpi eq, %boundary, %c2_i32 : i32
  %input = scf.if %terminated -> (buffer) {
    %nonempty = index.cmp ne, %length, %c0 : offset
    func.call @check(%nonempty) : (i1)
    %first = buffer.load.i8.u %text[%c0]
    %hash = scalar.constant 35 : i32
    %delimiter = scalar.cmpi eq, %first, %hash : i32
    func.call @check(%delimiter) : (i1)
    %remaining = index.sub %length, %c1 : offset
    %suffix = buffer.alloca<private> align(1) %remaining : buffer
    buffer.copy %text[%c1], %suffix[%c0], %remaining
    scf.yield %suffix : buffer
  } else {
    scf.yield %text : buffer
  }
  %input_length = buffer.length %input
  scf.for %i = [%c0 to %input_length step %c1] {
    %byte = buffer.load.i8.u %input[%i]
    %a = scalar.constant 97 : i32
    %b = scalar.constant 98 : i32
    %x = scalar.constant 120 : i32
    %hash = scalar.constant 35 : i32
    %is_a = scalar.cmpi eq, %byte, %a : i32
    %is_b = scalar.cmpi eq, %byte, %b : i32
    %is_x = scalar.cmpi eq, %byte, %x : i32
    %is_hash = scalar.cmpi eq, %byte, %hash : i32
    %ab = scalar.ori %is_a, %is_b : i1
    %xh = scalar.ori %is_x, %is_hash : i1
    %valid = scalar.ori %ab, %xh : i1
    func.call @check(%valid) : (i1)
  }
  %probe = scalar.addi %capacity, %c1_i64 : i64
  %tokens, %count = func.call @encode(%input, %c0_i32, %probe) : (buffer, i32, i64) -> (buffer, i64)
  %fits = scalar.cmpi ule, %count, %capacity : i64
  %fits32 = scalar.extui %fits : i1 to i32
  %error = buffer.constant utf8("input exceeds the available context size") : buffer
  func.call @require(%fits32, %error) : (i32, buffer)
  %empty = buffer.constant utf8("") : buffer
  func.return %tokens, %count, %empty : buffer, i64, buffer
}

func.def public @text_output_next_v1(%response: buffer, %previous: i64, %state: i64, %phase: i32) -> (i64, i64, i64, i64) {
  %length = buffer.length %response
  %count = index.cast %length : offset to i64
  %content = scalar.constant 1 : i64
  func.return %content, %previous, %count, %state : i64, i64, i64, i64
}

func.def public @text_complete_text_v1(%prompt: buffer, %response: buffer, %content: buffer, %reasoning: buffer, %tools: buffer, %initial_state: i64, %final_state: i64) -> (buffer, i32) {
  %c0 = index.constant 0 : offset
  %length = buffer.length %response
  %empty = index.cmp eq, %length, %c0 : offset
  %result = scf.if %empty -> (buffer) {
    scf.yield %prompt : buffer
  } else {
    %joined = func.call @join(%prompt, %response) : (buffer, buffer) -> (buffer)
    scf.yield %joined : buffer
  }
  %reusable = scalar.constant 1 : i32
  func.return %result, %reusable : buffer, i32
}

func.def public @text_encode_state_v1(%bindings: buffer, %count: i32, %origins: buffer) {
  %origin = index.constant 0 : offset
  %c0 = index.constant 0 : index
  %c1 = index.constant 1 : index
  %bounded_count = scalar.assume %count [range(%count, 1, 2147483647)] : i32
  %length = index.cast %bounded_count : i32 to index
  %origin_length = buffer.length %origins
  %origin_length64 = index.cast %origin_length : offset to i64
  %stride = scalar.constant 40 : i64
  %capacity64 = scalar.divui %origin_length64, %stride : i64
  %bounded_capacity = scalar.assume %capacity64 [range(%capacity64, 1, 2147483647)] : i64
  %capacity = index.cast %bounded_capacity : i64 to index
  %records = buffer.view %bindings[%origin] : buffer -> view<[%length]x4xi64>
  %rows = buffer.view %origins[%origin] : buffer -> view<[%capacity]x5xi64>
  scf.for %i = [%c0 to %length step %c1] {
    %row64 = view.load %records[%i, 0] : view<[%length]x4xi64> -> i64
    %row_bound = scalar.assume %row64 [range(%row64, 0, 2147483646)] : i64
    %row_value = index.cast %row_bound : i64 to index
    %row = index.assume %row_value [lt(%row_value, %capacity)] : index
    %read = view.load %records[%i, 1] : view<[%length]x4xi64> -> i64
    %write = view.load %records[%i, 2] : view<[%length]x4xi64> -> i64
    %position = view.load %records[%i, 3] : view<[%length]x4xi64> -> i64
    view.store %read, %rows[%row, 0] : i64, view<[%capacity]x5xi64>
    view.store %write, %rows[%row, 1] : i64, view<[%capacity]x5xi64>
    view.store %position, %rows[%row, 3] : i64, view<[%capacity]x5xi64>
  }
  func.return
}

func.def @plan_capacity(%plan: buffer) -> (index) {
  %bytes = buffer.length %plan
  %bytes64 = index.cast %bytes : offset to i64
  %stride = scalar.constant 36 : i64
  %records = scalar.divui %bytes64, %stride : i64
  %bounded = scalar.assume %records [range(%records, 1, 2147483647)] : i64
  %capacity = index.cast %bounded : i64 to index
  func.return %capacity : index
}

// Device packets retain the first cohort's nine-field plan records. Feedback
// slots are span-indexed, including unselected input-only spans.
func.def public @text_encode_epoch_v1(%control_state: buffer, %plan: buffer, %inputs: buffer, %media_plan: buffer, %first_count: i32, %next_count: i32, %input_count: i32, %first_media_count: i32, %next_media_count: i32, %first_packet_capacity: i32, %next_packet_capacity: i32, %eos: i32, %metadata: buffer, %tokens: buffer) -> (i32, buffer, i64, i64) {
  %origin = index.constant 0 : offset
  %header_bytes = index.constant 16 : offset
  %count64 = scalar.extui %first_count : i32 to i64
  %record_bytes = scalar.constant 36 : i64
  %plan_length = scalar.muli %count64, %record_bytes : i64
  %plan_bytes = index.cast %plan_length : i64 to offset
  %header = buffer.view %metadata[%origin] : buffer -> view<4xi32>
  %c0 = scalar.constant 0 : i32
  view.store %first_count, %header[0] : i32, view<4xi32>
  view.store %eos, %header[1] : i32, view<4xi32>
  view.store %c0, %header[2] : i32, view<4xi32>
  view.store %c0, %header[3] : i32, view<4xi32>
  buffer.copy %plan[%origin], %metadata[%header_bytes], %plan_bytes
  %input_count64 = scalar.extui %input_count : i32 to i64
  %token_stride = scalar.constant 4 : i64
  %input_length = scalar.muli %input_count64, %token_stride : i64
  %input_bytes = index.cast %input_length : i64 to offset
  buffer.copy %inputs[%origin], %tokens[%origin], %input_bytes
  %transfer_offset = index.constant 8 : offset
  %transfers = buffer.view %control_state[%transfer_offset] : buffer -> view<2x5xi64>
  %zero64 = scalar.constant 0 : i64
  %one64 = scalar.constant 1 : i64
  %header_length = scalar.constant 16 : i64
  %metadata_length = scalar.addi %header_length, %plan_length : i64
  view.store %zero64, %transfers[0, 0] : i64, view<2x5xi64>
  view.store %zero64, %transfers[0, 1] : i64, view<2x5xi64>
  view.store %zero64, %transfers[0, 2] : i64, view<2x5xi64>
  view.store %zero64, %transfers[0, 3] : i64, view<2x5xi64>
  view.store %metadata_length, %transfers[0, 4] : i64, view<2x5xi64>
  view.store %one64, %transfers[1, 0] : i64, view<2x5xi64>
  view.store %zero64, %transfers[1, 1] : i64, view<2x5xi64>
  view.store %one64, %transfers[1, 2] : i64, view<2x5xi64>
  view.store %zero64, %transfers[1, 3] : i64, view<2x5xi64>
  view.store %input_length, %transfers[1, 4] : i64, view<2x5xi64>
  %transfer_begin = scalar.constant 8 : i64
  %transfer_length = scalar.constant 80 : i64
  func.return %first_count, %control_state, %transfer_begin, %transfer_length : i32, buffer, i64, i64
}

func.def public @text_publish_epoch_v1(%plan: buffer, %media_plan: buffer, %count: i32, %first_media_count: i32, %next_media_count: i32, %verified: i32, %continues: i32, %next_speculative: buffer, %ordinary: buffer, %first_results: buffer, %next_results: buffer, %progress: buffer) {
  %origin = index.constant 0 : offset
  %c0 = index.constant 0 : index
  %c1 = index.constant 1 : index
  %c0_i32 = scalar.constant 0 : i32
  %c1_i32 = scalar.constant 1 : i32
  %c2_i32 = scalar.constant 2 : i32
  %progress_bytes = buffer.length %progress
  buffer.fill %c0_i32, %progress[%origin], %progress_bytes : i32
  %bounded_count = scalar.assume %count [range(%count, 1, 2147483647)] : i32
  %length = index.cast %bounded_count : i32 to index
  %plan_extent = func.call @plan_capacity(%plan) : (buffer) -> (index)
  %requests = buffer.view %plan[%origin] : buffer -> view<[%plan_extent]x9xi32>
  %outputs = buffer.view %ordinary[%origin] : buffer -> view<[%length]xi32>
  %published = buffer.view %progress[%origin] : buffer -> view<[%length]x21xi32>
  scf.for %i = [%c0 to %length step %c1] {
    %request = index.assume %i [lt(%i, %plan_extent)] : index
    %consumed = view.load %requests[%request, 0] : view<[%plan_extent]x9xi32> -> i32
    %source_position = view.load %requests[%request, 2] : view<[%plan_extent]x9xi32> -> i32
    %source_advance = scalar.muli %consumed, %c2_i32 : i32
    %next_source_position = scalar.addi %source_position, %source_advance : i32
    %flags = view.load %requests[%request, 6] : view<[%plan_extent]x9xi32> -> i32
    %bits = scalar.andi %flags, %c1_i32 : i32
    %selected = scalar.cmpi ne, %bits, %c0_i32 : i32
    %selected_count = scalar.extui %selected : i1 to i32
    view.store %consumed, %published[%i, 0] : i32, view<[%length]x21xi32>
    view.store %consumed, %published[%i, 1] : i32, view<[%length]x21xi32>
    view.store %selected_count, %published[%i, 2] : i32, view<[%length]x21xi32>
    view.store %next_source_position, %published[%i, 4] : i32, view<[%length]x21xi32>
    scf.if %selected {
      %token = view.load %outputs[%i] : view<[%length]xi32> -> i32
      view.store %token, %published[%i, 5] : i32, view<[%length]x21xi32>
    }
  }
  func.return
}

func.def public @text_step_v1(%state: buffer, %initialize: i32, %source_position: i32, %input_count: i32, %residual: hal.buffer, %control: hal.buffer, %recurrent: hal.buffer, %attention: hal.buffer, %tokens: hal.buffer, %progress: hal.buffer) -> (i64, i32) {
  %c0 = scalar.constant 0 : i32
  %c2 = scalar.constant 2 : i32
  %c0_i64 = scalar.constant 0 : i64
  %source_advance = scalar.muli %input_count, %c2 : i32
  %next_source_position = scalar.addi %source_position, %source_advance : i32
  %error = buffer.constant utf8("text tiny requires pooled epochs without MTP") : buffer
  func.call @require(%c0, %error) : (i32, buffer)
  func.return %c0_i64, %next_source_position : i64, i32
}

func.def public @text_epoch_v1(%control_state: buffer, %shape: i32, %proposes: i32, %verifies: i32, %span_count: i32, %output_count: i32, %epoch_count: i32, %continuation_shape: i32, %continuation_proposes: i32, %first_media_count: i32, %next_media_count: i32, %residual: hal.buffer, %metadata: hal.buffer, %origins: hal.buffer, %state: hal.buffer, %tokens: hal.buffer, %outputs: hal.buffer, %carry: hal.buffer, %committed: hal.buffer, %results: hal.buffer, %draft_cache: hal.buffer, %draft_origins: hal.buffer, %next_results: hal.buffer) -> (i64) {
  %origin = index.constant 0 : offset
  %view = buffer.view %control_state[%origin] : buffer -> view<1xi32>
  %stage = view.load %view[0] : view<1xi32> -> i32
  %c0 = scalar.constant 0 : i32
  %completion = func.call @execute_6(%stage, %residual, %metadata, %origins, %state, %tokens, %outputs) : (i32, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer, hal.buffer) -> (i64)
  %c0_i64 = scalar.constant 0 : i64
  %c4_i64 = scalar.constant 4 : i64
  %count64 = scalar.extui %span_count : i32 to i64
  %length = scalar.muli %count64, %c4_i64 : i64
  %feedback = func.call @feedback(%c0, %outputs, %c0_i64, %length) : (i32, hal.buffer, i64, i64) -> (i64)
  func.return %completion : i64
}

func.decl @tiny_storage(%maximum: i64, %span_capacity: i64, %context: i64, %pool: i64, %rows: i64, %pins: i64) -> (buffer, buffer, buffer, buffer, buffer)

func.def public @text_resize_state_v1(%state: buffer, %rows: i64) -> (buffer, buffer, buffer, buffer, buffer, buffer) {
  %layout_origin = index.constant 88 : offset
  %layout = buffer.view %state[%layout_origin] : buffer -> view<5xi64>
  %maximum = view.load %layout[0] : view<5xi64> -> i64
  %span_capacity = view.load %layout[1] : view<5xi64> -> i64
  %context = view.load %layout[2] : view<5xi64> -> i64
  %pool = view.load %layout[3] : view<5xi64> -> i64
  %pins = view.load %layout[4] : view<5xi64> -> i64
  %allocations, %views, %origins, %draft, %geometry = func.call @tiny_storage(%maximum, %span_capacity, %context, %pool, %rows, %pins) : (i64, i64, i64, i64, i64, i64) -> (buffer, buffer, buffer, buffer, buffer)
  func.return %state, %allocations, %views, %origins, %draft, %geometry : buffer, buffer, buffer, buffer, buffer, buffer
}