MCPcopy
hub / github.com/bbycroft/llm-viz / renderOutputBoxes

Function renderOutputBoxes

src/llm/components/ModelCard.ts:157–223  ·  view source on GitHub ↗
(state: IProgramState, layout: IGptModelLayout, tl: Vec3, br: Vec3, cellW: number, fontSize: number, lineOpts: ILineOpts, opts?: IOutputBoxOpts)

Source from the content-addressed store, hash-verified

155}
156
157export function renderOutputBoxes(state: IProgramState, layout: IGptModelLayout, tl: Vec3, br: Vec3, cellW: number, fontSize: number, lineOpts: ILineOpts, opts?: IOutputBoxOpts) {
158 let render = state.render;
159 let { T, vocabSize } = layout.shape;
160 let outCellH = br.y - tl.y;
161
162 let opacity = opts?.opacity ?? 1.0;
163 let boldLast = opts?.boldLast ?? true;
164
165 lineOpts = { ...lineOpts, color: lineOpts.color.mul(opacity ?? 1.0) };
166 let tokTextOpts: IFontOpts = { color: Vec4.fromHexColor("#000000", opacity), mtx: lineOpts.mtx, size: fontSize };
167 let idxTextOpts: IFontOpts = { color: Vec4.fromHexColor("#666666", opacity), mtx: lineOpts.mtx, size: fontSize * 0.6 };
168
169 let dimmedTokTextOpts: IFontOpts = { ...tokTextOpts, color: tokTextOpts.color.mul(0.3) };
170 let dimmedIdxTextOpts: IFontOpts = { ...idxTextOpts, color: idxTextOpts.color.mul(0.3) };
171
172 drawLineRect(render, tl, br, lineOpts);
173
174 let sortedOutput = layout.model?.sortedBuf;
175
176 for (let i = 0; i < T; i++) {
177 if (i > 0) {
178 let lineX = tl.x + i * cellW;
179 drawLine(render.lineRender, new Vec3(lineX, tl.y, 0), new Vec3(lineX, br.y, 0), lineOpts);
180 }
181
182 if (sortedOutput && i < layout.model!.inputLen) {
183 let usedSoFar = 0.0;
184 let cx = tl.x + (i + 0.5) * cellW;
185
186 for (let j = 0; j < vocabSize; j++) {
187 let tokIdx = sortedOutput[(i * vocabSize + j) * 2 + 0];
188 let tokProb = sortedOutput[(i * vocabSize + j) * 2 + 1];
189
190 let partTop = tl.y + usedSoFar * outCellH;
191 let partH = tokProb * outCellH;
192
193 let dimmed = i < layout.model!.inputLen - 1 || !boldLast;
194
195 let color = mixColorValues(opts?.tokMixes ?? null, tokTextOpts.color, i);
196 if (dimmed) {
197 color = color.mul(0.3);
198 }
199
200 let tokOpts = { ...tokTextOpts, color };
201 let idxOpts = { ...idxTextOpts, color: color.mul(0.6) };
202
203 let tokStr = sortABCInputTokenToString(tokIdx);
204 let tokW = measureText(render.modelFontBuf, tokStr, tokOpts);
205 let idxW = measureText(render.modelFontBuf, tokIdx.toString(), idxOpts);
206 let textH = tokOpts.size + idxOpts.size;
207 let top = partTop + (partH - textH) / 2;
208
209 if (partH > textH) {
210 drawText(render.modelFontBuf, tokStr, cx - tokW / 2, top, tokOpts);
211 drawText(render.modelFontBuf, tokIdx.toString(), cx - idxW / 2, top + tokOpts.size, idxOpts);
212 }
213
214 usedSoFar += tokProb;

Callers 2

renderInputAtTopFunction · 0.85
renderOutputAtBottomFunction · 0.85

Calls 8

measureTextFunction · 0.90
drawTextFunction · 0.90
drawLineRectFunction · 0.85
mixColorValuesFunction · 0.85
fromHexColorMethod · 0.80
mulMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected