@@ -591,29 +591,28 @@ class WebGpuLlamaBackend
591591 return attempts;
592592 }
593593
594- ({int ? nBatch, int ? nUbatch}) _resolveWebBatchTuning ({
594+ ({int nBatch, int nUbatch}) _resolveWebBatchSizes ({
595595 required String url,
596596 required ModelParams params,
597+ required int contextSize,
597598 }) {
598- if (params.batchSize > 0 || params.microBatchSize > 0 ) {
599- return (nBatch: null , nUbatch: null );
600- }
601-
602- if (params.preferredBackend == GpuBackend .cpu || params.gpuLayers == 0 ) {
603- return (nBatch: null , nUbatch: null );
604- }
605-
606599 final normalizedUrl = url.toLowerCase ();
607600 final isQwen35Small =
608601 normalizedUrl.contains ('qwen3.5-0.8b' ) ||
609602 normalizedUrl.contains ('qwen_qwen3.5-0.8b' );
610- if (! isQwen35Small) {
611- return (nBatch: null , nUbatch: null );
603+ final shouldUseQwen35SmallTuning =
604+ params.batchSize <= 0 &&
605+ params.microBatchSize <= 0 &&
606+ params.preferredBackend != GpuBackend .cpu &&
607+ params.gpuLayers != 0 &&
608+ isQwen35Small;
609+
610+ if (shouldUseQwen35SmallTuning) {
611+ return (nBatch: 32 , nUbatch: 8 );
612612 }
613613
614- final tunedBatch = 32 ;
615- final tunedUbatch = 8 ;
616- return (nBatch: tunedBatch, nUbatch: tunedUbatch);
614+ final resolved = resolveModelContextBatchSizes (params, contextSize);
615+ return (nBatch: resolved.batchSize, nUbatch: resolved.microBatchSize);
617616 }
618617
619618 int _webGpuFlashAttentionValue (ModelParams params) {
@@ -894,8 +893,6 @@ class WebGpuLlamaBackend
894893 requestedContextSize: params.contextSize,
895894 requestedGpuLayers: requestedGpuLayers,
896895 );
897- final batchTuning = _resolveWebBatchTuning (url: url, params: params);
898-
899896 Object ? lastError;
900897 Map <String , String > lastRuntimeHints = const < String , String > {};
901898 var retriedWithWasm32 = false ;
@@ -909,6 +906,11 @@ class WebGpuLlamaBackend
909906 for (var index = 0 ; index < loadAttempts.length; index += 1 ) {
910907 final attempt = loadAttempts[index];
911908 _lastNCtx = attempt.contextSize;
909+ final batchSizes = _resolveWebBatchSizes (
910+ url: url,
911+ params: params,
912+ contextSize: attempt.contextSize,
913+ );
912914 LlamaWebGpuBridge ? bridgeForAttempt;
913915 bool ? forceRemoteFetchBackend;
914916 final attemptThreads = switch (index) {
@@ -940,12 +942,8 @@ class WebGpuLlamaBackend
940942 nThreadsBatch: params.numberOfThreadsBatch > 0
941943 ? params.numberOfThreadsBatch
942944 : null ,
943- nBatch: params.batchSize > 0
944- ? params.batchSize
945- : batchTuning.nBatch,
946- nUbatch: params.microBatchSize > 0
947- ? params.microBatchSize
948- : batchTuning.nUbatch,
945+ nBatch: batchSizes.nBatch,
946+ nUbatch: batchSizes.nUbatch,
949947 nGpuLayers: attempt.gpuLayers,
950948 nSeqMax: math.max (1 , params.maxParallelSequences),
951949 flashAttention: _webGpuFlashAttentionValue (params),
0 commit comments