@@ -65,6 +65,7 @@ export function createLLMTextRunner(
6565 // Generation methods
6666 // ======================================================
6767 const isCancelled = createSynchronizable ( false ) ;
68+
6869 const stop = ( ) : void => {
6970 'worklet' ;
7071 isCancelled . setBlocking ( true ) ;
@@ -83,6 +84,7 @@ export function createLLMTextRunner(
8384
8485 const startMs = Date . now ( ) ;
8586 const startPos = pos . getBlocking ( ) ;
87+
8688 const tokens = tokenizer . encode ( prompt ) ;
8789
8890 let offset = 0 ;
@@ -143,52 +145,33 @@ export function createLLMTextRunner(
143145 const generatedTokens : number [ ] = [ ] ;
144146 const maxNewTokens = config ?. maxNewTokens ?? Infinity ;
145147
146- let numTokens = 0 ;
147- // TO-REMOVE: Timing breakdown for benchmarking
148- let totalModelExecuteMs = 0 ;
149- let totalSampleMs = 0 ;
150- let totalOnTokenMs = 0 ;
151-
152148 const tToken = tensor ( 'int64' , [ 1 , 1 ] ) ;
153149 const tCurPos = tensor ( 'int64' , [ 1 ] ) ;
154150
151+ let numTokens = 0 ;
155152 let curPos = pos . getBlocking ( ) ;
156153
157154 try {
158155 while ( numTokens < maxNewTokens && ! isCancelled . getBlocking ( ) ) {
159156 if ( curPos >= maxContextLen ) break ;
160157
161- // TO-REMOVE: Sample timing
162- const tSample0 = Date . now ( ) ;
163- const nextToken = sample (
164- tLogits ,
165- { generatedTokens : Int32Array . from ( generatedTokens ) } ,
166- config
167- ) ;
168- totalSampleMs += Date . now ( ) - tSample0 ;
158+ const genTokArray = Int32Array . from ( generatedTokens ) ;
159+ const nextToken = sample ( tLogits , { generatedTokens : genTokArray } , config ) ;
169160
170- // TO-REMOVE: onToken timing
171- const tOnToken0 = Date . now ( ) ;
172161 onToken ?.( tokenizer . decode ( Int32Array . of ( nextToken ) ) ) ;
173- totalOnTokenMs += Date . now ( ) - tOnToken0 ;
174-
175162 generatedTokens . push ( nextToken ) ;
176163
177- if ( ! config ?. ignoreEos && eosIds . includes ( nextToken ) ) {
178- break ;
179- }
164+ if ( ! config ?. ignoreEos && eosIds . includes ( nextToken ) ) break ;
180165
181166 tToken . setData ( BigInt64Array . of ( BigInt ( nextToken ) ) ) ;
182167 tCurPos . setData ( BigInt64Array . of ( BigInt ( curPos ) ) ) ;
183168
184- // TO-REMOVE: model.execute timing
185- const tExec0 = Date . now ( ) ;
186169 model . execute ( 'forward' , [ tToken , tCurPos ] , [ tLogits ] ) ;
187- totalModelExecuteMs += Date . now ( ) - tExec0 ;
188170
171+ numTokens += 1 ;
189172 curPos += 1 ;
173+
190174 pos . setBlocking ( curPos ) ;
191- numTokens += 1 ;
192175 }
193176 } finally {
194177 tToken . dispose ( ) ;
@@ -198,15 +181,6 @@ export function createLLMTextRunner(
198181 const durationMs = Date . now ( ) - generateStartMs ;
199182 const tokensPerSecond = durationMs > 0 ? ( numTokens / durationMs ) * 1000 : 0 ;
200183
201- // TO-REMOVE: Console log breakdown
202- // eslint-disable-next-line no-console
203- console . log (
204- `[llmTextRunner] Generated ${ numTokens } tokens in ${ durationMs } ms (${ tokensPerSecond . toFixed ( 1 ) } tok/s) | ` +
205- `model.execute: ${ totalModelExecuteMs } ms (${ durationMs > 0 ? ( ( totalModelExecuteMs / durationMs ) * 100 ) . toFixed ( 1 ) : 0 } %) | ` +
206- `sample: ${ totalSampleMs } ms (${ durationMs > 0 ? ( ( totalSampleMs / durationMs ) * 100 ) . toFixed ( 1 ) : 0 } %) | ` +
207- `onToken: ${ totalOnTokenMs } ms (${ durationMs > 0 ? ( ( totalOnTokenMs / durationMs ) * 100 ) . toFixed ( 1 ) : 0 } %)`
208- ) ;
209-
210184 return { numTokens, durationMs, tokensPerSecond, prefill : prefillStats } ;
211185 } ;
212186
0 commit comments