Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions unsloth/models/gemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,16 @@ def __init__(
for device in range(DEVICE_COUNT):
self._set_cos_sin_cache(
seq_len = self.current_rope_size,
device = torch.device(device),
device = torch.device(DEVICE_TYPE_TORCH, device),
dtype = torch.get_default_dtype(),
)

# dummy so that patch_utils doesn't fail for now
self.cos_cached = torch.empty(
1, device = torch.cuda.current_device(), dtype = torch.get_default_dtype()
1, device = get_current_device(), dtype = torch.get_default_dtype()
)
self.sin_cached = torch.empty(
1, device = torch.cuda.current_device(), dtype = torch.get_default_dtype()
1, device = get_current_device(), dtype = torch.get_default_dtype()
)

def _set_cos_sin_cache(self, seq_len, device, dtype):
Expand Down Expand Up @@ -350,7 +350,7 @@ def forward(self, x, position_ids = None, seq_len = None):

def get_cached(self, seq_len = None, device_index = None):
if device_index is None:
device_index = torch.cuda.current_device()
device_index = get_current_device()
return self.multi_gpu_cos_cached[device_index], self.multi_gpu_sin_cached[
device_index
]
Expand All @@ -362,7 +362,9 @@ def extend_rope_embedding(self, x, seq_len):
self.current_rope_size = math.ceil(seq_len / 8192) * 8192
for device in range(DEVICE_COUNT):
self._set_cos_sin_cache(
self.current_rope_size, device = torch.device(device), dtype = x.dtype
self.current_rope_size,
device = torch.device(DEVICE_TYPE_TORCH, device),
dtype = x.dtype,
)


Expand Down Expand Up @@ -489,5 +491,5 @@ def post_patch(model, tokenizer, correct_dtype = None):

for _ in range(3):
gc.collect()
torch.cuda.empty_cache()
clean_gpu_cache()
return model, tokenizer