Commit 
							
							·
						
						357b19c
	
1
								Parent(s):
							
							e773545
								
Update index.html
Browse files- index.html +30 -10
    	
        index.html
    CHANGED
    
    | @@ -300,17 +300,37 @@ | |
| 300 | 
             
                  const out = await model({ input_ids: enc.input_ids, attention_mask: enc.attention_mask });
         | 
| 301 | 
             
                  const dt = (performance.now() - t0) | 0;
         | 
| 302 |  | 
| 303 | 
            -
                   | 
| 304 | 
            -
                  const  | 
| 305 | 
            -
                  const  | 
| 306 | 
            -
                  const  | 
| 307 | 
            -
                   | 
| 308 | 
            -
             | 
| 309 | 
            -
                  const  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 310 | 
             
                  const rows = [];
         | 
| 311 | 
            -
                  for (const [p, i] of idx){
         | 
| 312 | 
            -
                    const tok = await tokenizer.decode([i], { skip_special_tokens:false });
         | 
| 313 | 
            -
                    rows.push({ token: tok, p, id:i });
         | 
| 314 | 
             
                  }
         | 
| 315 | 
             
                  return { rows, dt };
         | 
| 316 | 
             
                }
         | 
|  | |
| 300 | 
             
                  const out = await model({ input_ids: enc.input_ids, attention_mask: enc.attention_mask });
         | 
| 301 | 
             
                  const dt = (performance.now() - t0) | 0;
         | 
| 302 |  | 
| 303 | 
            +
                  // --- logits come as a Tensor (data: Float32Array, dims: [1, seqLen, vocab]) ---
         | 
| 304 | 
            +
                  const logitsT = out.logits;
         | 
| 305 | 
            +
                  const dims    = logitsT.dims;           // e.g., [1, seqLen, vocabSize]
         | 
| 306 | 
            +
                  const data    = logitsT.data;           // Float32Array
         | 
| 307 | 
            +
                  
         | 
| 308 | 
            +
                  const vocabSize = dims[dims.length - 1];
         | 
| 309 | 
            +
                  const seqLen    = dims[dims.length - 2];
         | 
| 310 | 
            +
                  
         | 
| 311 | 
            +
                  // Take the last time step (length = vocabSize) from the flat buffer
         | 
| 312 | 
            +
                  const start = (seqLen - 1) * vocabSize;
         | 
| 313 | 
            +
                  const last  = data.subarray(start, start + vocabSize); // typed view (no copy)
         | 
| 314 | 
            +
                  
         | 
| 315 | 
            +
                  // Softmax for probabilities
         | 
| 316 | 
            +
                  let m = -Infinity;
         | 
| 317 | 
            +
                  for (let i = 0; i < last.length; i++) if (last[i] > m) m = last[i];
         | 
| 318 | 
            +
                  
         | 
| 319 | 
            +
                  const exps = new Float32Array(last.length);
         | 
| 320 | 
            +
                  let Z = 0;
         | 
| 321 | 
            +
                  for (let i = 0; i < last.length; i++) { const e = Math.exp(last[i] - m); exps[i] = e; Z += e; }
         | 
| 322 | 
            +
                  
         | 
| 323 | 
            +
                  // Top-K
         | 
| 324 | 
            +
                  const K   = Math.min(parseInt(topkSel.value, 10) || 10, last.length);
         | 
| 325 | 
            +
                  const idx = Array.from({ length: last.length }, (_, i) => [exps[i] / Z, i])
         | 
| 326 | 
            +
                    .sort((a, b) => b[0] - a[0])
         | 
| 327 | 
            +
                    .slice(0, K);
         | 
| 328 | 
            +
                  
         | 
| 329 | 
            +
                  // Build rows
         | 
| 330 | 
             
                  const rows = [];
         | 
| 331 | 
            +
                  for (const [p, i] of idx) {
         | 
| 332 | 
            +
                    const tok = await tokenizer.decode([i], { skip_special_tokens: false });
         | 
| 333 | 
            +
                    rows.push({ token: tok, p, id: i });
         | 
| 334 | 
             
                  }
         | 
| 335 | 
             
                  return { rows, dt };
         | 
| 336 | 
             
                }
         | 
