@@ -239,22 +239,78 @@ function findMaxPossibleContextSizeForVram({gpuLayers, ggufInsights, vram, isEmb
239239} ) {
240240 const maxContextSize = getDefaultModelContextSize ( { trainContextSize : ggufInsights . trainContextSize } ) ;
241241
242- for ( let contextSize = maxContextSize ; contextSize >= minAllowedContextSizeInCalculations ; contextSize -- ) {
243- const contextVram = ggufInsights . estimateContextResourceRequirements ( {
244- contextSize,
245- batchSize : getDefaultContextBatchSize ( { contextSize, sequences : 1 } ) ,
246- modelGpuLayers : gpuLayers ,
247- sequences : 1 ,
248- isEmbeddingContext,
249- flashAttention
250- } ) . gpuVram ;
251-
252- if ( contextVram <= vram )
253- return {
242+ return findMaxValidValue ( {
243+ maxValue : maxContextSize ,
244+ minValue : minAllowedContextSizeInCalculations ,
245+ minStep : 1 ,
246+ test ( contextSize ) {
247+ const contextVram = ggufInsights . estimateContextResourceRequirements ( {
254248 contextSize,
255- vram : contextVram
256- } ;
249+ batchSize : getDefaultContextBatchSize ( { contextSize, sequences : 1 } ) ,
250+ modelGpuLayers : gpuLayers ,
251+ sequences : 1 ,
252+ isEmbeddingContext,
253+ flashAttention
254+ } ) . gpuVram ;
255+
256+ if ( contextVram <= vram )
257+ return {
258+ contextSize,
259+ vram : contextVram
260+ } ;
261+
262+ return null ;
263+ }
264+ } ) ;
265+ }
266+
267+ function findMaxValidValue < T > ( {
268+ maxValue,
269+ minValue,
270+ minStep = 1 ,
271+ test
272+ } : {
273+ maxValue : number ,
274+ minValue : number ,
275+ minStep ?: number ,
276+ test ( value : number ) : T | null
277+ } ) : T | null {
278+ let step = - Math . max ( minStep , Math . floor ( ( maxValue - minValue ) / 4 ) ) ;
279+ let bestValue : null | { value : number , result : T } = null ;
280+
281+ for ( let value = maxValue ; value >= minValue ; ) {
282+ const result = test ( value ) ;
283+ if ( result != null ) {
284+ if ( bestValue == null || value >= bestValue . value ) {
285+ bestValue = { value : value , result : result } ;
286+
287+ if ( step === - minStep )
288+ break ;
289+ else if ( step < 0 )
290+ step = Math . max ( minStep , Math . floor ( - step / 2 ) ) ;
291+ }
292+ } else if ( bestValue != null && value < bestValue . value ) {
293+ value = bestValue . value ;
294+ step = Math . max ( minStep , Math . floor ( Math . abs ( step ) / 2 ) ) ;
295+ continue ;
296+ } else if ( step > 0 )
297+ step = - Math . max ( minStep , Math . floor ( step / 2 ) ) ;
298+
299+ if ( value === minValue && step === - minStep )
300+ break ;
301+
302+ value += step ;
303+ if ( value < minValue ) {
304+ value = minValue ;
305+ step = Math . max ( minStep , Math . floor ( Math . abs ( step ) / 2 ) ) ;
306+ } else if ( value > maxValue ) {
307+ value = maxValue ;
308+ step = - Math . max ( minStep , Math . floor ( Math . abs ( step ) / 2 ) ) ;
309+ }
257310 }
258311
312+ if ( bestValue != null )
313+ return bestValue . result ;
314+
259315 return null ;
260316}
0 commit comments