unsloth-Inkling-2-Mini-loom / program /vision_control.loom
sawansri's picture
t68: faster decode and prefill, exact drafted sampling, re-measured card
73b6d82 verified
Raw History Blame Contribute Delete
8.54 kB
// Image component control (generated by gen_component.py).
type.decl import("finite", "batch") @FiniteBatch
func.decl import("finite", "batch_pack") @finite_batch_pack(%batch: ptr<@FiniteBatch>) -> (buffer, buffer)
func.decl import("finite", "batch_finish") @finite_batch_finish(%batch: ptr<@FiniteBatch>)
func.decl import("input") @require(%condition: i32, %diagnostic: buffer)
func.decl import("runner") @execute_2(%stage: i32, %input: hal.buffer, %output: hal.buffer) -> (i64)
func.decl @vision_stage(%height: i64, %width: i64) -> (i32)
global.rodata.def @vision_bad_rows = bytes("696d61676520726f7720636f756e7420646f6573206e6f74206d61746368207468652070726f6365737365642073697a65")
// Memory: private = largest packet + encoder scratch at the largest size; retained = output rows.
func.def public @vision_forecast_memory(%state: buffer, %ranges: buffer, %descriptors: buffer) -> (i64, i64, i64) {
%origin = index.constant 0 : offset
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
%c0_i64 = scalar.constant 0 : i64
%c16 = scalar.constant 16 : i64
%range_bytes = buffer.length %ranges
%range_length = index.cast %range_bytes : offset to i64
%count_value = scalar.divui %range_length, %c16 : i64
%bounded = scalar.assume %count_value [range(%count_value, 1, 2147483647)] : i64
%count = index.cast %bounded : i64 to index
%records = buffer.view %ranges[%origin] : buffer -> view<[%count]x2xi64>
%peak, %rows = scf.for %i = [%c0 to %count step %c1](%p = %c0_i64 : i64, %r = %c0_i64 : i64) -> (i64, i64) {
%off = view.load %records[%i, 0] : view<[%count]x2xi64> -> i64
%ob = scalar.assume %off [range(%off, 0, 9223372036854775807)] : i64
%o = index.cast %ob : i64 to offset
%header = buffer.view %descriptors[%o] : buffer -> view<8xi32>
%len32 = view.load %header[7] : view<8xi32> -> i32
%rows32 = view.load %header[2] : view<8xi32> -> i32
%len = scalar.extui %len32 : i32 to i64
%rr = scalar.extui %rows32 : i32 to i64
%bigger = scalar.cmpi ugt, %len, %p : i64
%np = scf.select %bigger, %len, %p : i64
%nr = scalar.addi %r, %rr : i64
scf.yield %np, %nr : i64, i64
}
%scratch = scalar.constant 1847197696 : i64
%private = scalar.addi %peak, %scratch : i64
%row_bytes = scalar.constant 9216 : i64
%retained = scalar.muli %rows, %row_bytes : i64
func.return %private, %retained, %retained : i64, i64, i64
}
// One image per run: the tag is the processed size (metadata [width, height]).
func.def public @text_image_prepare_request_v1(%state: buffer, %request: buffer) -> (buffer, i64, i64) {
%origin = index.constant 0 : offset
%header = buffer.view %request[%origin] : buffer -> view<8xi32>
%meta32 = view.load %header[3] : view<8xi32> -> i32
%meta64 = scalar.extui %meta32 : i32 to i64
%mb = scalar.assume %meta64 [range(%meta64, 0, 4294967295)] : i64
%mo = index.cast %mb : i64 to offset
%meta = buffer.view %request[%mo] : buffer -> view<2xi32>
%w = view.load %meta[0] : view<2xi32> -> i32
%h = view.load %meta[1] : view<2xi32> -> i32
%w64 = scalar.extui %w : i32 to i64
%h64 = scalar.extui %h : i32 to i64
%shift = scalar.constant 32 : i64
%wbits = scalar.shli %w64, %shift : i64
%tag = scalar.ori %wbits, %h64 : i64
%units = scalar.constant 1 : i64
func.return %request, %units, %tag : buffer, i64, i64
}
func.def public @text_image_prepare_batch_v1(%state: buffer, %tags: buffer, %batch: ptr<@FiniteBatch>) -> (i32, buffer, buffer) {
%ranges, %packed = func.call @finite_batch_pack(%batch) : (ptr<@FiniteBatch>) -> (buffer, buffer)
%origin = index.constant 0 : offset
%range = buffer.view %ranges[%origin] : buffer -> view<1x2xi64>
%source64 = view.load %range[0, 0] : view<1x2xi64> -> i64
%sb = scalar.assume %source64 [range(%source64, 0, 4294967295)] : i64
%source = index.cast %sb : i64 to offset
%header = buffer.view %packed[%source] : buffer -> view<8xi32>
%rows32 = view.load %header[2] : view<8xi32> -> i32
%meta32 = view.load %header[3] : view<8xi32> -> i32
%planes32 = view.load %header[5] : view<8xi32> -> i32
%meta64 = scalar.extui %meta32 : i32 to i64
%planes64 = scalar.extui %planes32 : i32 to i64
%mb = scalar.assume %meta64 [range(%meta64, 0, 4294967295)] : i64
%pb = scalar.assume %planes64 [range(%planes64, 0, 4294967295)] : i64
%mo0 = index.cast %mb : i64 to offset
%po0 = index.cast %pb : i64 to offset
%mo = index.add %source, %mo0 : offset
%po = index.add %source, %po0 : offset
%meta = buffer.view %packed[%mo] : buffer -> view<2xi32>
%dst_w = view.load %meta[0] : view<2xi32> -> i32
%dst_h = view.load %meta[1] : view<2xi32> -> i32
%plane = buffer.view %packed[%po] : buffer -> view<12xi32>
%src_w = view.load %plane[1] : view<12xi32> -> i32
%src_h = view.load %plane[2] : view<12xi32> -> i32
%pix_rel = view.load %plane[6] : view<12xi32> -> i32
%channels = view.load %plane[8] : view<12xi32> -> i32
// rows check: the chat program declared rows from the same size rule
%pf = scalar.constant 40 : i32
%gw = scalar.divui %dst_w, %pf : i32
%gh = scalar.divui %dst_h, %pf : i32
%expect = scalar.muli %gw, %gh : i32
%ok = scalar.cmpi eq, %expect, %rows32 : i32
%ok32 = scalar.extui %ok : i1 to i32
%bad = global.load @vision_bad_rows : buffer
func.call @require(%ok32, %bad) : (i32, buffer)
// payload: 64-byte device header + the packed cohort
%hdr_bytes = index.constant 64 : offset
%packed_bytes = buffer.length %packed
%payload_bytes = index.add %hdr_bytes, %packed_bytes : offset
%payload = buffer.alloca<private> align(16) %payload_bytes : buffer
buffer.copy %packed[%origin], %payload[%hdr_bytes], %packed_bytes
%hdr = buffer.view %payload[%origin] : buffer -> view<16xi32>
%source64b = index.cast %source : offset to i64
%source32 = scalar.trunci %source64b : i64 to i32
%sixty4 = scalar.constant 64 : i32
%pix0 = scalar.addi %pix_rel, %source32 : i32
%pix_abs = scalar.addi %pix0, %sixty4 : i32
%zero32 = scalar.constant 0 : i32
view.store %src_w, %hdr[0] : i32, view<16xi32>
view.store %src_h, %hdr[1] : i32, view<16xi32>
view.store %channels, %hdr[2] : i32, view<16xi32>
view.store %pix_abs, %hdr[3] : i32, view<16xi32>
view.store %dst_w, %hdr[4] : i32, view<16xi32>
view.store %dst_h, %hdr[5] : i32, view<16xi32>
// plan: [output bytes, result bytes, 1 request, rows] [first row, rows, 0] then [byte offset, width] per row
%rows64 = scalar.extui %rows32 : i32 to i64
%rb = scalar.assume %rows64 [range(%rows64, 1, 4096)] : i64
%rows = index.cast %rb : i64 to index
%c16 = index.constant 16 : offset
%c56 = index.constant 56 : offset
%row_bytes_total = index.scale %rows, %c16 : index, offset -> offset
%plan_bytes = index.add %c56, %row_bytes_total : offset
%plan = buffer.alloca<private> align(8) %plan_bytes : buffer
%ph = buffer.view %plan[%origin] : buffer -> view<7xi64>
%width = scalar.constant 2304 : i64
%row_bytes = scalar.constant 9216 : i64
%out_bytes = scalar.muli %rb, %row_bytes : i64
%one = scalar.constant 1 : i64
%zero = scalar.constant 0 : i64
view.store %out_bytes, %ph[0] : i64, view<7xi64>
view.store %out_bytes, %ph[1] : i64, view<7xi64>
view.store %one, %ph[2] : i64, view<7xi64>
view.store %rb, %ph[3] : i64, view<7xi64>
view.store %zero, %ph[4] : i64, view<7xi64>
view.store %rb, %ph[5] : i64, view<7xi64>
view.store %zero, %ph[6] : i64, view<7xi64>
%records = buffer.view %plan[%c56] : buffer -> view<[%rows]x2xi64>
%c0 = index.constant 0 : index
%c1 = index.constant 1 : index
scf.for %i = [%c0 to %rows step %c1] {
%i64 = index.cast %i : index to i64
%offset = scalar.muli %i64, %row_bytes : i64
view.store %offset, %records[%i, 0] : i64, view<[%rows]x2xi64>
view.store %width, %records[%i, 1] : i64, view<[%rows]x2xi64>
}
%h64 = scalar.extui %dst_h : i32 to i64
%w64 = scalar.extui %dst_w : i32 to i64
%stage = func.call @vision_stage(%h64, %w64) : (i64, i64) -> (i32)
func.return %stage, %payload, %plan : i32, buffer, buffer
}
func.def public @text_image_run_v1(%state: buffer, %stage: i32, %input: hal.buffer, %output: hal.buffer, %batch: ptr<@FiniteBatch>) {
%work = func.call @execute_2(%stage, %input, %output) : (i32, hal.buffer, hal.buffer) -> (i64)
func.return
}
// One run encodes the whole cohort (one image per run): every member is done when the run is collected.
func.def public @text_image_collect_batch_v1(%state: buffer, %batch: ptr<@FiniteBatch>) {
func.call @finite_batch_finish(%batch) : (ptr<@FiniteBatch>)
func.return
}