Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Update utils.py
Daniel Han56b6c37ec040
diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.pyindex f8690a17a70..c5df015ca0a 100644--- a/unsloth/kernels/utils.py+++ b/unsloth/kernels/utils.py@@ -116,9 +116,13 @@ def get_lora_parameters_bias(proj):return W, QUANT_STATE(W), A, B, s, biaspass+global WEIGHT_BUFFER+WEIGHT_BUFFER = None+global ABSMAX_BUFFER+ABSMAX_BUFFER = Noneif HAS_CUDA_STREAM:- def fast_dequantize(W, quant_state = None, out = None):+ def fast_dequantize(W, quant_state = None, out = None, use_global_buffer = False):if quant_state is None: return Wif type(quant_state) is not list:# New quant_state as a class@@ -141,18 +145,34 @@ if HAS_CUDA_STREAM:global CUDA_STREAMif CUDA_STREAM is None: CUDA_STREAM = torch.cuda.current_stream("cuda:0")+ n_elements_absmax = absmax.numel()+# Create weight matrix- if out is None:- out = torch.empty(shape, dtype = dtype, device = "cuda:0")+ if use_global_buffer:++ # Use same buffers for faster inference+ size = shape[0]*shape[1]+ global WEIGHT_BUFFER+ global ABSMAX_BUFFER+ if WEIGHT_BUFFER is None:+ WEIGHT_BUFFER = torch.empty(size, dtype = dtype, device = "cuda:0")+ ABSMAX_BUFFER = torch.empty(n_elements_absmax, dtype = dtype, device = "cuda:0")++ if size > WEIGHT_BUFFER.numel(): WEIGHT_BUFFER.resize_(size)+ if n_elements_absmax > ABSMAX_BUFFER.numel(): WEIGHT_BUFFER.resize_(n_elements_absmax)++ out = WEIGHT_BUFFER[:size].view(shape)+ out_absmax = ABSMAX_BUFFER[:n_elements_absmax]else:- assert(out.shape == shape)- assert(out.dtype == dtype)+ if out is None:+ out = torch.empty(shape, dtype = dtype, device = "cuda:0")+ else:+ assert(out.shape == shape)+ assert(out.dtype == dtype)+ out_absmax = torch.empty(n_elements_absmax, dtype = torch.float32, device = "cuda:0")+ pass# NF4 dequantization of statistics- n_elements_absmax = absmax.numel()- out_absmax = torch.empty(n_elements_absmax, dtype = torch.float32, device = "cuda:0")-- # Do dequantizationptr_out_absmax = get_ptr(out_absmax)cdequantize_blockwise_fp32(get_ptr(code2), get_ptr(absmax), get_ptr(absmax2), ptr_out_absmax,@@ -160,6 +180,7 @@ if HAS_CUDA_STREAM:)out_absmax += offset+ # Dequantize Wfx = cdequantize_blockwise_fp16_nf4 if dtype == torch.float16 else \cdequantize_blockwise_bf16_nf4fx(get_ptr(None), get_ptr(W), ptr_out_absmax, get_ptr(out),@@ -170,7 +191,7 @@ if HAS_CUDA_STREAM:return out.t() if is_transposed else outpasselse:- def fast_dequantize(W, quant_state = None, out = None):+ def fast_dequantize(W, quant_state = None, out = None, use_global_buffer = False):if quant_state is None: return Wif type(quant_state) is not list:# New quant_state as a class@@ -191,16 +212,32 @@ else:absmax2, code2, blocksize2, _, _, _, _ = state2pass+ n_elements_absmax = absmax.numel()+# Create weight matrix- if out is None:- out = torch.empty(shape, dtype = dtype, device = "cuda:0")- else:- assert(out.shape == shape)- assert(out.dtype == dtype)+ if use_global_buffer:- # NF4 dequantization of statistics- n_elements_absmax = absmax.numel()- out_absmax = torch.empty(n_elements_absmax, dtype = torch.float32, device = "cuda:0")+ # Use same buffers for faster inference+ size = shape[0]*shape[1]+ global WEIGHT_BUFFER+ global ABSMAX_BUFFER+ if WEIGHT_BUFFER is None:+ WEIGHT_BUFFER = torch.empty(size, dtype = dtype, device = "cuda:0")+ ABSMAX_BUFFER = torch.empty(n_elements_absmax, dtype = dtype, device = "cuda:0")++ if size > WEIGHT_BUFFER.numel(): WEIGHT_BUFFER.resize_(size)+ if n_elements_absmax > ABSMAX_BUFFER.numel(): WEIGHT_BUFFER.resize_(n_elements_absmax)++ out = WEIGHT_BUFFER[:size].view(shape)+ out_absmax = ABSMAX_BUFFER[:n_elements_absmax]+ else:+ if out is None:+ out = torch.empty(shape, dtype = dtype, device = "cuda:0")+ else:+ assert(out.shape == shape)+ assert(out.dtype == dtype)+ out_absmax = torch.empty(n_elements_absmax, dtype = torch.float32, device = "cuda:0")+ pass# Do dequantizationptr_out_absmax = get_ptr(out_absmax)