Skip to content

Commit

Permalink
override .cuda() to check if model is already quantized (#25166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranchlai authored Jul 28, 2023
1 parent c1dba11 commit 2a78720
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/transformers/modeling_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,16 @@ def get_memory_footprint(self, return_buffers=True):
mem = mem + mem_bufs
return mem

def cuda(self, *args, **kwargs):
# Checks if the model has been loaded in 8-bit
if getattr(self, "is_quantized", False):
raise ValueError(
"Calling `cuda()` is not supported for `4-bit` or `8-bit` quantized models. Please use the model as it is, since the"
" model has already been set to the correct devices and casted to the correct `dtype`."
)
else:
return super().cuda(*args, **kwargs)

def to(self, *args, **kwargs):
# Checks if the model has been loaded in 8-bit
if getattr(self, "is_quantized", False):
Expand Down

0 comments on commit 2a78720

Please sign in to comment.