From 38a3e34bbf4cbb2e2e6f4eb4c1ec72c3164a3b55 Mon Sep 17 00:00:00 2001 From: jiqing-feng Date: Mon, 24 Mar 2025 12:58:12 +0000 Subject: [PATCH 1/2] fix xpu to cpu Signed-off-by: jiqing-feng --- bitsandbytes/nn/modules.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/bitsandbytes/nn/modules.py b/bitsandbytes/nn/modules.py index 961f746ba..ff78a8dc0 100755 --- a/bitsandbytes/nn/modules.py +++ b/bitsandbytes/nn/modules.py @@ -694,7 +694,7 @@ def to(self: T, tensor: Tensor, non_blocking: bool = ...) -> T: ... def to(self, *args, **kwargs): device, dtype, non_blocking, convert_to_format = torch._C._nn._parse_to(*args, **kwargs) - if device is not None and device.type in ("cuda", "xpu", "cpu"): + if device is not None: if device.type == "cuda" and self.data.device.type == "cpu": return self.cuda(device) elif device.type == "cpu": @@ -705,21 +705,18 @@ def to(self, *args, **kwargs): return self.cpu() elif device.type == "xpu": if self.data.dtype == torch.int8: - self.data = self.data.contiguous().xpu(device) + self.data = self.data.contiguous() self.CB = self.data - return self - else: - return self.xpu(device) - else: - new_param = Int8Params( - super().to(device=device, dtype=dtype, non_blocking=non_blocking), - requires_grad=self.requires_grad, - has_fp16_weights=self.has_fp16_weights, - ) - new_param.CB = self.CB - new_param.SCB = self.SCB - return new_param + new_param = Int8Params( + super().to(device=device, dtype=dtype, non_blocking=non_blocking), + requires_grad=self.requires_grad, + has_fp16_weights=self.has_fp16_weights, + ) + new_param.CB = self.CB + new_param.SCB = self.SCB + + return new_param def maybe_rearrange_weight(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs): From f30c9d118d71361c1b35d31e8a2092c3b1bea05d Mon Sep 17 00:00:00 2001 From: jiqing-feng Date: Mon, 24 Mar 2025 13:05:05 +0000 Subject: [PATCH 2/2] fix xpu cpu data device Signed-off-by: jiqing-feng --- bitsandbytes/nn/modules.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bitsandbytes/nn/modules.py b/bitsandbytes/nn/modules.py index ff78a8dc0..eb528576d 100755 --- a/bitsandbytes/nn/modules.py +++ b/bitsandbytes/nn/modules.py @@ -700,13 +700,14 @@ def to(self, *args, **kwargs): elif device.type == "cpu": if self.data.dtype == torch.int8: self.CB = self.data - return self else: return self.cpu() elif device.type == "xpu": if self.data.dtype == torch.int8: self.data = self.data.contiguous() self.CB = self.data + if self.data.device.type == "cpu": + return self.xpu(device) new_param = Int8Params( super().to(device=device, dtype=dtype, non_blocking=non_blocking),