Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
coli chat: the live preview is a picture forming, not a mosaic
JustVugg0fd3070eed6d
diff --git a/c/coli b/c/coliindex af4f0ef5..b707109b 100755--- a/c/coli+++ b/c/coli@@ -2146,8 +2146,15 @@ class ImageProgress:FRAMES = Spinner.FRAMESSTAGES = {"encode": "encoding the prompt", "denoise": "denoising", "decode": "decoding"}- def __init__(self, stream, seed, steps, previews=True):+ def __init__(self, stream, seed, steps, previews=True, protocol="blocks"):self.out = stream+ # With sixel the preview is drawn as a picture, smoothly enlarged, in an+ # area reserved once: the cursor is saved at its top (DECSC) and every+ # step is drawn over the last one from there. kitty and iTerm2 place+ # images by their own rules, so they keep the half blocks for previews+ # (the finished picture is full resolution in every graphics protocol).+ self.protocol = protocol+ self.area = 0self.live = bool(stream) and (stream.isatty() or os.environ.get("COLI_COLOR") == "1")self.previews = previews and self.liveself.seed, self.steps = seed, steps@@ -2197,6 +2204,9 @@ class ImageProgress:if not self.previews or not width or not height:returnimport term_image+ if self.protocol == "sixel":+ self._preview_sixel(term_image, width, height, channels, pixels)+ returntry:text = term_image.render_blocks(pixels, width, height, channels,max_columns=min(64, term_w() - 6), max_rows=16,@@ -2209,6 +2219,31 @@ class ImageProgress:self.rows = text.count("\n")self._draw()+ def _preview_sixel(self, term_image, width, height, channels, pixels):+ columns, rows, cell_w, cell_h = term_image.terminal_geometry(self.out)+ w_px = max(64, min(512, (columns - 6) * cell_w))+ h_px = round(height * w_px / width)+ cap = max(4, min(18, rows - 8)) * cell_h+ if h_px > cap:+ h_px, w_px = cap, max(64, round(width * cap / height))+ try:+ big = term_image.upscale_smooth(pixels, width, height, channels, w_px, h_px)+ text = term_image.render_sixel(big, w_px, h_px, channels, w_px, h_px, " ")+ except (ValueError, IndexError):+ return+ # rows the picture covers, with a margin for a terminal whose real cells+ # are smaller than the ones it reported (or than the 8x16 guess)+ lines = -(-h_px // max(8, cell_h - 2)) + 1+ with self.lock:+ if not self.area:+ # reserve the lines now, so the terminal scrolls here and never+ # under a saved cursor+ self.out.write("\r\033[K" + "\n" * lines + f"\033[{lines}A\r\0337")+ self.area = lines+ self.out.write("\0338" + text)+ self.out.write(f"\0338\033[{self.area}B")+ self._draw()+def interrupted(self):with self.lock:self.note = "stopping… (Ctrl-C again to quit)"@@ -2223,10 +2258,13 @@ class ImageProgress:if self.live:with self.lock:self.out.write("\r\033[K")- if self.rows:+ if self.area:+ self.out.write("\0338\033[J") # the preview area, image and all+ elif self.rows:self.out.write(f"\033[{self.rows}A\r\033[J")self.out.flush()self.rows = 0+ self.area = 0def image_settings(a, info):"""Size, steps and seed for the first image: the flags when given, the@@ -2257,7 +2295,7 @@ def generate_image(backend, prompt, settings, protocol, out=None, stream=None, lstream = stream or sys.stdoutseed = settings["seed"] if settings["seed"] is not None else random_seed()previews = protocol != "none" and os.environ.get("COLI_IMAGE_PREVIEW", "1") != "0"- progress = ImageProgress(stream, seed, settings["steps"], previews)+ progress = ImageProgress(stream, seed, settings["steps"], previews, protocol)progress.start()t0 = time.time()try:diff --git a/c/term_image.py b/c/term_image.pyindex 4ade1a0d..965871b1 100644--- a/c/term_image.py+++ b/c/term_image.py@@ -250,6 +250,35 @@ def resize(pixels, width, height, channels, new_width, new_height):return bytes(out)+def upscale_smooth(pixels, width, height, channels, new_width, new_height):+ """Bilinear enlargement. The engine's previews carry one pixel per latent+ token (48x32 for a 768x512 picture): enlarged with resize() they come out as+ visible squares, and a preview is meant to look like a picture forming, as+ the web UI shows it (smoothly scaled and blurred), not like a mosaic."""+ new_width, new_height = max(1, new_width), max(1, new_height)+ out = bytearray(new_width * new_height * channels)+ columns = []+ for x in range(new_width):+ fx = min(max((x + 0.5) * width / new_width - 0.5, 0.0), width - 1.0)+ x0 = int(fx)+ x1 = min(width - 1, x0 + 1)+ columns.append((x0 * channels, x1 * channels, fx - x0))+ o = 0+ for y in range(new_height):+ fy = min(max((y + 0.5) * height / new_height - 0.5, 0.0), height - 1.0)+ y0 = int(fy)+ u = fy - y0+ r0 = y0 * width * channels+ r1 = min(height - 1, y0 + 1) * width * channels+ for a0, a1, t in columns:+ for c in range(channels):+ top = pixels[r0 + a0 + c] + (pixels[r0 + a1 + c] - pixels[r0 + a0 + c]) * t+ bottom = pixels[r1 + a0 + c] + (pixels[r1 + a1 + c] - pixels[r1 + a0 + c]) * t+ out[o] = int(top + (bottom - top) * u + 0.5)+ o += 1+ return bytes(out)++def flatten(pixels, width, height, channels, checker=8):"""RGB for renderers that have no alpha: transparent areas over a greycheckerboard, the convention that reads as "transparent" at a glance."""diff --git a/c/tests/test_term_image.py b/c/tests/test_term_image.pyindex 323cf9f6..b67736cf 100644--- a/c/tests/test_term_image.py+++ b/c/tests/test_term_image.py@@ -284,5 +284,24 @@ class RenderDispatchTest(unittest.TestCase):ti.render(pixels, 64, 32, 4, "hologram", env={}, stream=Tty())++class UpscaleSmoothTest(unittest.TestCase):+ """The TUI enlarges the engine's one-pixel-per-token previews with this, so+ they look like a picture forming and not like a mosaic of squares."""++ def test_flat_colour_stays_exact(self):+ big = ti.upscale_smooth(bytes([10, 200, 30]) * 4, 2, 2, 3, 16, 12)+ self.assertEqual(big, bytes([10, 200, 30]) * (16 * 12))++ def test_corners_kept_and_middle_interpolated(self):+ # a black-to-white ramp, 2 pixels wide, enlarged 8x+ small = bytes([0, 0, 0, 255, 255, 255])+ big = ti.upscale_smooth(small, 2, 1, 3, 16, 1)+ row = [big[i * 3] for i in range(16)]+ self.assertEqual(row[0], 0)+ self.assertEqual(row[-1], 255)+ self.assertEqual(row, sorted(row)) # monotone, no steps back+ self.assertGreater(len(set(row)), 8) # a ramp, not two blocks+if __name__ == "__main__":unittest.main()