Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nightly #673

Merged
merged 234 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
234 commits
Select commit Hold shift + click to select a range
7df08c4
Update llama.py
danielhanchen May 19, 2024
ba5b6ce
offload
danielhanchen May 19, 2024
a07057e
Update llama.py
danielhanchen May 19, 2024
4be9063
Update llama.py
danielhanchen May 19, 2024
3dc3d3f
Update llama.py
danielhanchen May 19, 2024
f1cc1e8
Update llama.py
danielhanchen May 19, 2024
5cb531a
Update llama.py
danielhanchen May 19, 2024
6bd8e60
Update llama.py
danielhanchen May 19, 2024
d1d57ff
Update llama.py
danielhanchen May 19, 2024
7470f67
continued pretraining trainer
danielhanchen May 20, 2024
da9c1a6
Update trainer.py
danielhanchen May 20, 2024
2c68f56
Update trainer.py
danielhanchen May 20, 2024
217bf9d
Update trainer.py
danielhanchen May 20, 2024
6e85384
Update trainer.py
danielhanchen May 21, 2024
77f9c51
is_bfloat16_supported
danielhanchen May 21, 2024
c0e1d27
Update __init__.py
danielhanchen May 21, 2024
2b23b93
Update README.md
danielhanchen May 21, 2024
902e23a
Update llama.py
danielhanchen May 21, 2024
98f41ce
Merge branch 'main' into nightly
danielhanchen May 22, 2024
3193cac
is_bfloat16_supported
danielhanchen May 22, 2024
dfeaf4b
Update __init__.py
danielhanchen May 22, 2024
1e84090
Mistral v3
danielhanchen May 22, 2024
f63f32b
Merge branch 'main' into nightly
danielhanchen May 23, 2024
57ad8e7
Phi 3 medium
danielhanchen May 23, 2024
2b994b2
Update chat_templates.py
danielhanchen May 23, 2024
ff8171f
Update chat_templates.py
danielhanchen May 23, 2024
5ca8b58
Phi-3
danielhanchen May 23, 2024
98c2e81
Merge branch 'main' into nightly
danielhanchen May 23, 2024
3817660
Merge branch 'main' into nightly
danielhanchen May 23, 2024
f858145
Merge branch 'main' into nightly
danielhanchen May 24, 2024
a1328f6
Update save.py
danielhanchen May 24, 2024
fb29673
Update README.md
shimmyshimmer May 25, 2024
fa85556
Untrained tokens
danielhanchen May 26, 2024
c511aca
Update tokenizer_utils.py
danielhanchen May 26, 2024
35e7355
Update tokenizer_utils.py
danielhanchen May 26, 2024
cc0bf44
Update tokenizer_utils.py
danielhanchen May 26, 2024
674ba66
Update tokenizer_utils.py
danielhanchen May 26, 2024
9823f52
Update tokenizer_utils.py
danielhanchen May 26, 2024
c0c761b
Update tokenizer_utils.py
danielhanchen May 26, 2024
e2850c0
Update tokenizer_utils.py
danielhanchen May 26, 2024
8e12780
Update tokenizer_utils.py
danielhanchen May 26, 2024
6f1855e
Update tokenizer_utils.py
danielhanchen May 26, 2024
d27b173
Update tokenizer_utils.py
danielhanchen May 26, 2024
7bf7399
Update tokenizer_utils.py
danielhanchen May 26, 2024
31ecef9
Update tokenizer_utils.py
danielhanchen May 26, 2024
b67d93f
Update tokenizer_utils.py
danielhanchen May 26, 2024
e874ccd
Update tokenizer_utils.py
danielhanchen May 26, 2024
d7b54ff
Update tokenizer_utils.py
danielhanchen May 27, 2024
5a4a512
Update tokenizer_utils.py
danielhanchen May 27, 2024
82c040e
Update tokenizer_utils.py
danielhanchen May 27, 2024
8e227b2
Update tokenizer_utils.py
danielhanchen May 27, 2024
250d386
Update tokenizer_utils.py
danielhanchen May 27, 2024
e6db3ba
Update llama.py
danielhanchen May 27, 2024
e673fa2
Update tokenizer_utils.py
danielhanchen May 27, 2024
222b835
Update tokenizer_utils.py
danielhanchen May 27, 2024
6404aa5
Update tokenizer_utils.py
danielhanchen May 27, 2024
cfea7b2
Update tokenizer_utils.py
danielhanchen May 27, 2024
083e5ba
Update save.py
danielhanchen May 27, 2024
6f2565c
Update save.py
danielhanchen May 27, 2024
c19b04e
Update save.py
danielhanchen May 27, 2024
64b12a2
checkpoint
danielhanchen May 28, 2024
4cd5a8a
Merge branch 'main' into nightly
danielhanchen May 28, 2024
196faec
Update _utils.py
danielhanchen May 28, 2024
235be40
Update tokenizer_utils.py
danielhanchen May 29, 2024
cf9090a
Update tokenizer_utils.py
danielhanchen May 29, 2024
1fb1110
Update tokenizer_utils.py
danielhanchen May 29, 2024
d1bd60c
Update llama.py
danielhanchen May 30, 2024
732ead0
accelerate
danielhanchen May 30, 2024
359ae5c
Update _utils.py
danielhanchen May 30, 2024
8dcfad3
Update _utils.py
danielhanchen May 30, 2024
2bafc57
Update _utils.py
danielhanchen May 30, 2024
90f6311
Update _utils.py
danielhanchen May 30, 2024
7b84ff7
Update _utils.py
danielhanchen May 30, 2024
60f4b9a
Update _utils.py
danielhanchen May 30, 2024
3ebe5a5
Update _utils.py
danielhanchen May 30, 2024
7bbc8ce
Update tokenizer_utils.py
danielhanchen May 30, 2024
6f5c84c
train_dataloader
danielhanchen May 30, 2024
0d269ca
Update llama.py
danielhanchen May 30, 2024
6b7c142
Update llama.py
danielhanchen May 30, 2024
54f3a74
Update llama.py
danielhanchen May 30, 2024
0bc96c5
use_fast_convert
danielhanchen May 30, 2024
02c91b0
Merge branch 'main' into nightly
danielhanchen May 30, 2024
b384ff0
Merge branch 'main' into nightly
danielhanchen May 30, 2024
a8b5d89
Update save.py
danielhanchen May 30, 2024
872d569
Update save.py
danielhanchen May 30, 2024
3a1f5f2
Update save.py
danielhanchen May 30, 2024
bcadc8c
Update save.py
danielhanchen Jun 2, 2024
1381820
remove_special_tokens
danielhanchen Jun 2, 2024
e01b87d
Ollama
danielhanchen Jun 2, 2024
b3479c7
Update chat_templates.py
danielhanchen Jun 3, 2024
86804dc
Update chat_templates.py
danielhanchen Jun 3, 2024
87fdd3a
Update chat_templates.py
danielhanchen Jun 3, 2024
5c5df69
Merge branch 'main' into nightly
danielhanchen Jun 7, 2024
6386d94
Update llama.py
danielhanchen Jun 7, 2024
b1a9551
Update chat_templates.py
danielhanchen Jun 9, 2024
344a05d
Support bfloat16 GGUF
danielhanchen Jun 9, 2024
6b11e0d
Update save.py
danielhanchen Jun 9, 2024
c6e4b5b
Update llama.py
danielhanchen Jun 9, 2024
57f29ab
fast_forward_inference
danielhanchen Jun 9, 2024
d32e972
Update mapper.py
danielhanchen Jun 9, 2024
e121fa5
Update loader.py
danielhanchen Jun 9, 2024
5eaa10f
Update llama.py
danielhanchen Jun 9, 2024
f57d28d
Update tokenizer_utils.py
danielhanchen Jun 10, 2024
8937507
info
danielhanchen Jun 11, 2024
8982edb
edits
danielhanchen Jun 11, 2024
8904605
Create chat template
danielhanchen Jun 11, 2024
2a374c2
Fix tokenizer
danielhanchen Jun 12, 2024
d704b73
Merge branch 'main' into nightly
danielhanchen Jun 13, 2024
8176155
Update tokenizer_utils.py
danielhanchen Jun 13, 2024
21a99f1
fix case where gguf saving fails due to first_conversion dtype (#630)
chrehall68 Jun 13, 2024
dbf2dcf
Support revision parameter in FastLanguageModel.from_pretrained (#629)
chrehall68 Jun 13, 2024
9016171
clears any selected_adapters before calling internal_model.save_pretr…
neph1 Jun 13, 2024
0428920
Update __init__.py (#602)
xyangk Jun 13, 2024
9fdd847
Fixed unsloth/tokenizer_utils.py for chat training (#604)
Oseltamivir Jun 13, 2024
b5fc6aa
Add GGML saving option to Unsloth for easier Ollama model creation an…
mahiatlinux Jun 13, 2024
3fafbf7
docs: Add LoraConfig parameters documentation (#619)
sebdg Jun 13, 2024
273a871
llama.cpp failing (#371)
bet0x Jun 13, 2024
b312b3f
fix libcuda_dirs import for triton 3.0 (#227)
t-vi Jun 13, 2024
1601dca
Update save.py
danielhanchen Jun 13, 2024
26dc502
Update __init__.py
danielhanchen Jun 13, 2024
6a51657
Update fast_lora.py
danielhanchen Jun 13, 2024
4a8ba90
Update save.py
danielhanchen Jun 13, 2024
0abb5ba
Update save.py
danielhanchen Jun 13, 2024
b24dd05
Update save.py
danielhanchen Jun 13, 2024
48c6d6d
Update loader.py
danielhanchen Jun 13, 2024
e35f608
Update save.py
danielhanchen Jun 13, 2024
4822eae
Update save.py
danielhanchen Jun 13, 2024
7d847ed
quantize now llama-quantize
danielhanchen Jun 13, 2024
82f10cb
Update chat_templates.py
danielhanchen Jun 13, 2024
08424f0
Update loader.py
danielhanchen Jun 13, 2024
eb906d0
Update mapper.py
danielhanchen Jun 13, 2024
0a304ae
Update __init__.py
danielhanchen Jun 13, 2024
71edc42
embedding size
danielhanchen Jun 13, 2024
411b881
Merge branch 'main' into nightly
danielhanchen Jun 13, 2024
b74e321
Update qwen2.py
danielhanchen Jun 13, 2024
9c6d415
Merge branch 'main' into nightly
danielhanchen Jun 14, 2024
b82277f
docs
danielhanchen Jun 14, 2024
d98e45e
Update README.md
danielhanchen Jun 14, 2024
b6f0fdb
Update qwen2.py
danielhanchen Jun 14, 2024
6c031e4
README: Fix minor typo. (#559)
shaper Jun 14, 2024
2401dee
Update mistral.py
danielhanchen Jun 14, 2024
1b93d7e
Update qwen2.py
danielhanchen Jun 14, 2024
3581037
Update qwen2.py
danielhanchen Jun 14, 2024
b56b8b8
Update qwen2.py
danielhanchen Jun 14, 2024
fe8c064
Update llama.py
danielhanchen Jun 14, 2024
d8d332a
Update llama.py
danielhanchen Jun 14, 2024
cdb1dbb
Update llama.py
danielhanchen Jun 14, 2024
e8b3cf0
Update README.md
danielhanchen Jun 14, 2024
7e6f000
FastMistralModel
danielhanchen Jun 14, 2024
28995ab
Update mistral.py
danielhanchen Jun 14, 2024
515b1ae
Update mistral.py
danielhanchen Jun 14, 2024
7f28209
Update mistral.py
danielhanchen Jun 14, 2024
453cc48
Update mistral.py
danielhanchen Jun 14, 2024
6633d4a
Update mistral.py
danielhanchen Jun 14, 2024
e5bf125
Auto check rope scaling
danielhanchen Jun 14, 2024
d4f4bce
Merge branch 'main' into nightly
danielhanchen Jun 14, 2024
341565b
Update llama.py
danielhanchen Jun 14, 2024
dd3c6b1
Update llama.py
danielhanchen Jun 15, 2024
6d1ae23
Update llama.py
danielhanchen Jun 15, 2024
d855ef9
GPU support
danielhanchen Jun 15, 2024
da1fe76
Merge branch 'main' into nightly
danielhanchen Jun 15, 2024
6656446
Typo
danielhanchen Jun 15, 2024
9bd5fad
Update gemma.py
danielhanchen Jun 15, 2024
a3061b6
gpu
danielhanchen Jun 15, 2024
7e5155d
Merge branch 'main' into nightly
danielhanchen Jun 15, 2024
513bd4d
Multiple GGUF saving
danielhanchen Jun 15, 2024
fb54fbb
Update save.py
danielhanchen Jun 15, 2024
4cba3e2
Update save.py
danielhanchen Jun 15, 2024
979bb22
Merge branch 'main' into nightly
danielhanchen Jun 15, 2024
31811cf
check PEFT and base
danielhanchen Jun 15, 2024
a0232a7
Update llama.py
danielhanchen Jun 15, 2024
c4c4ff4
Update llama.py
danielhanchen Jun 15, 2024
80e82a2
Update llama.py
danielhanchen Jun 15, 2024
f62237d
Update llama.py
danielhanchen Jun 15, 2024
7f864dc
Update llama.py
danielhanchen Jun 15, 2024
4dda039
Update chat_templates.py
danielhanchen Jun 15, 2024
30605de
Fix breaking bug in save.py with interpreting quantization_method as …
ArcadaLabs-Jason Jun 16, 2024
1ba18ac
Merge branch 'main' into nightly
danielhanchen Jun 16, 2024
e2b2083
Revert "Fix breaking bug in save.py with interpreting quantization_me…
danielhanchen Jun 16, 2024
aeda849
Merge branch 'nightly' of https://github.com/unslothai/unsloth into n…
danielhanchen Jun 16, 2024
0938ab8
Revert "Revert "Fix breaking bug in save.py with interpreting quantiz…
danielhanchen Jun 16, 2024
46517a9
Merge branch 'main' into nightly
danielhanchen Jun 16, 2024
99f0397
Update llama.py
danielhanchen Jun 16, 2024
b824627
peft
danielhanchen Jun 16, 2024
e4f2263
patch
danielhanchen Jun 16, 2024
7aadceb
Update loader.py
danielhanchen Jun 16, 2024
17a9fb3
retrain
danielhanchen Jun 16, 2024
25cb17e
Update llama.py
danielhanchen Jun 16, 2024
3fba0f5
Update llama.py
danielhanchen Jun 16, 2024
798aa1e
Update llama.py
danielhanchen Jun 16, 2024
c6142d0
Update llama.py
danielhanchen Jun 16, 2024
7236dfc
Update llama.py
danielhanchen Jun 16, 2024
37f9abd
Update llama.py
danielhanchen Jun 16, 2024
7618197
Update llama.py
danielhanchen Jun 16, 2024
b4907f3
Update llama.py
danielhanchen Jun 16, 2024
2714a8b
Update llama.py
danielhanchen Jun 16, 2024
771a0d0
Update llama.py
danielhanchen Jun 16, 2024
af88eda
Merge branch 'main' into nightly
danielhanchen Jun 17, 2024
92c7d58
offload
danielhanchen Jun 17, 2024
2eacd2d
Update llama.py
danielhanchen Jun 17, 2024
b957061
Create a starter script for command-line training to integrate in ML …
sebdg Jun 18, 2024
4dba6c5
Update chat_templates.py
danielhanchen Jun 18, 2024
f2e4b83
Ollama
danielhanchen Jun 18, 2024
a5367a1
Update chat_templates.py
danielhanchen Jun 18, 2024
85062ff
Update chat_templates.py
danielhanchen Jun 18, 2024
12bd3cf
Update chat_templates.py
danielhanchen Jun 18, 2024
4417417
Update chat_templates.py
danielhanchen Jun 18, 2024
be02d97
Update chat_templates.py
danielhanchen Jun 18, 2024
0a1ee7a
Update chat_templates.py
danielhanchen Jun 18, 2024
7195152
Update chat_templates.py
danielhanchen Jun 18, 2024
676b20b
Update chat_templates.py
danielhanchen Jun 18, 2024
89b7807
Update chat_templates.py
danielhanchen Jun 18, 2024
563afa9
Update chat_templates.py
danielhanchen Jun 18, 2024
7bcaa23
Merge branch 'main' into nightly
danielhanchen Jun 19, 2024
bbfa77c
Ollama
danielhanchen Jun 19, 2024
9a88ac1
Update chat_templates.py
danielhanchen Jun 19, 2024
89e3027
ollama
danielhanchen Jun 19, 2024
412a7c9
Update mapper.py
danielhanchen Jun 19, 2024
44039e5
Update chat_templates.py
danielhanchen Jun 19, 2024
fc9ec40
Update save.py
danielhanchen Jun 19, 2024
7be6d88
Update save.py
danielhanchen Jun 19, 2024
5530710
Update save.py
danielhanchen Jun 19, 2024
d5b8d41
Update save.py
danielhanchen Jun 19, 2024
d1b3fac
Update save.py
danielhanchen Jun 19, 2024
99b68ff
Update save.py
danielhanchen Jun 19, 2024
a25dea9
Update save.py
danielhanchen Jun 19, 2024
bee81d1
Merge branch 'main' into nightly
danielhanchen Jun 20, 2024
e544c97
Update chat_templates.py
danielhanchen Jun 20, 2024
3f27de2
Update chat_templates.py
danielhanchen Jun 20, 2024
34c71b5
Update chat_templates.py
danielhanchen Jun 20, 2024
ae2924f
Update chat_templates.py
danielhanchen Jun 20, 2024
15e4e10
Merge branch 'main' into nightly
danielhanchen Jun 20, 2024
a5cbf3e
Update llama.py
danielhanchen Jun 20, 2024
b5888ba
Fixes
danielhanchen Jun 20, 2024
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
7 changes: 1 addition & 6 deletions unsloth/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,6 @@ def prepare_n_gradient_checkpoints(
pass


# Unsloth only works on NVIDIA GPUs for now
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = device_ids[:device_ids.find(',')] # Unsloth only works on NVIDIA GPUs for now
device = f"cuda:{device if device.isdigit() else '0'}"

class Unsloth_Offloaded_Gradient_Checkpointer(torch.autograd.Function):
"""
Saves VRAM by smartly offloading to RAM.
Expand All @@ -398,7 +393,7 @@ def forward(ctx, forward_function, hidden_states, *args):
@torch.cuda.amp.custom_bwd
def backward(ctx, dY):
(hidden_states,) = ctx.saved_tensors
hidden_states = hidden_states.to(device, non_blocking = True).detach()
hidden_states = hidden_states.to("cuda:0", non_blocking = True).detach()
hidden_states.requires_grad = True
with torch.enable_grad():
(output,) = ctx.forward_function(hidden_states, *ctx.args)
Expand Down
13 changes: 5 additions & 8 deletions unsloth/models/gemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@
GemmaFlashAttention2 = GemmaAttention
pass

import os
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = f"cuda:{device_ids[:device_ids.find(',')]}" # Unsloth only works on NVIDIA GPUs for now

torch_nn_functional_gelu = torch.nn.functional.gelu
def fast_geglu_inference(self, X):
# gate = self.gate_proj(X)
# up = self.up_proj(X)
bsz, _, hd = X.shape
# mlp_size = self.config.intermediate_size
# temp = torch.empty((2, bsz, 1, mlp_size), dtype = X.dtype, device = device)
# temp = torch.empty((2, bsz, 1, mlp_size), dtype = X.dtype, device = "cuda:0")

gate = fast_linear_forward(self.gate_proj, X)#, out = temp[0])
up = fast_linear_forward(self. up_proj, X)#, out = temp[1])
Expand All @@ -75,7 +72,7 @@ def GemmaDecoderLayer_fast_forward(
*args, **kwargs,
):
if use_cache and hasattr(self, "_flag_for_generation"): #past_key_value is not None:
out_weight = torch.empty(self.input_layernorm.weight.shape, dtype = torch.float32, device = device)
out_weight = torch.empty(self.input_layernorm.weight.shape, dtype = torch.float32, device = "cuda:0")

# Self Attention
residual = hidden_states
Expand Down Expand Up @@ -137,7 +134,7 @@ def GemmaModel_fast_forward_inference(
position_ids,
attention_mask = None,
):
out_weight = torch.empty_like(self.model.layers[0].input_layernorm.weight, dtype = torch.float32, device = device)
out_weight = torch.empty_like(self.model.layers[0].input_layernorm.weight, dtype = torch.float32, device = "cuda:0")
input_ids = input_ids[:,:self.max_seq_length]
hidden_states = self.model.embed_tokens(input_ids)
hidden_states = hidden_states.to(self.config.torch_dtype)
Expand Down Expand Up @@ -220,8 +217,8 @@ def _set_cos_sin_cache(self, seq_len, device, dtype):

emb = torch.cat((radians_new, radians_new), dim = -1)
# We must do RoPE in float32!
cos = emb.cos().to(device = device, non_blocking = True)#, dtype = dtype)
sin = emb.sin().to(device = device, non_blocking = True)#, dtype = dtype)
cos = emb.cos().to(device = "cuda:0", non_blocking = True)#, dtype = dtype)
sin = emb.sin().to(device = "cuda:0", non_blocking = True)#, dtype = dtype)
self.register_buffer("cos_cached", cos, persistent = False)
self.register_buffer("sin_cached", sin, persistent = False)
pass
Expand Down
39 changes: 14 additions & 25 deletions unsloth/models/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ def original_apply_o(self, X):
return O
pass

import os # Unsloth only works on NVIDIA GPUs for now
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = device_ids[:device_ids.find(',')] # Unsloth only works on NVIDIA GPUs for now
device = f"cuda:{device if device.isdigit() else '0'}"

from math import sqrt as math_sqrt
KV_CACHE_INCREMENT = 256 # KV Cache update size
torch_nn_functional_softmax = torch.nn.functional.softmax
Expand Down Expand Up @@ -136,15 +131,15 @@ def LlamaAttention_fast_forward_inference(
# Prefill phase
# if not hasattr(self, "paged_attention"):
if do_prefill:
self.paged_attention = torch.empty((KV_CACHE_INCREMENT+seq_len+1, 2, bsz, n_kv_heads, head_dim), dtype = dtype, device = device)
self.paged_attention = torch.empty((KV_CACHE_INCREMENT+seq_len+1, 2, bsz, n_kv_heads, head_dim), dtype = dtype, device = "cuda:0")
self.paged_attention_K = self.paged_attention[:,0]
self.paged_attention_V = self.paged_attention[:,1]
self.paged_attention_K[:seq_len] = K1.permute(2, 0, 1, 3)
self.paged_attention_V[:seq_len] = V1.permute(2, 0, 1, 3)
self.temp_QA = torch.empty((2, bsz, 1, attention_size), dtype = dtype, device = device)
self.temp_KV = torch.empty((2, bsz, 1, n_kv_heads*head_dim), dtype = dtype, device = device)
self.RH_Q = torch.empty((bsz, n_heads, 1, head_dim), dtype = dtype, device = device)
self.attention = torch.empty((bsz, n_heads, 1, KV_CACHE_INCREMENT+seq_len), dtype = dtype, device = device)
self.temp_QA = torch.empty((2, bsz, 1, attention_size), dtype = dtype, device = "cuda:0")
self.temp_KV = torch.empty((2, bsz, 1, n_kv_heads*head_dim), dtype = dtype, device = "cuda:0")
self.RH_Q = torch.empty((bsz, n_heads, 1, head_dim), dtype = dtype, device = "cuda:0")
self.attention = torch.empty((bsz, n_heads, 1, KV_CACHE_INCREMENT+seq_len), dtype = dtype, device = "cuda:0")
self.scalar = 1.0 / math_sqrt(self.head_dim)
self.half_head_dim = head_dim // 2
elif kv_seq_len >= self.paged_attention.shape[0]:
Expand Down Expand Up @@ -174,7 +169,7 @@ def LlamaAttention_fast_forward_inference(
Qn *= cos
Qn.addcmul_(RH_Q, sin)

RH_K = RH_Q[:,:n_kv_heads,:,:] # torch.empty((n_kv_heads, 1, head_dim), dtype = dtype, device = device)
RH_K = RH_Q[:,:n_kv_heads,:,:] # torch.empty((n_kv_heads, 1, head_dim), dtype = dtype, device = "cuda:0")
RH_K[:,:,:,:h] = Kn[:,:,:,h:]
RH_K[:,:,:,h:] = Kn[:,:,:,:h]
torch.neg(RH_K[:,:,:,:h], out = RH_K[:,:,:,:h])
Expand Down Expand Up @@ -236,7 +231,7 @@ def fast_swiglu_inference(self, X):
# up = self.up_proj(X)
bsz, _, hd = X.shape
# mlp_size = self.config.intermediate_size
# temp = torch.empty((2, bsz, 1, mlp_size), dtype = X.dtype, device = device)
# temp = torch.empty((2, bsz, 1, mlp_size), dtype = X.dtype, device = "cuda:0")

gate = fast_linear_forward(self.gate_proj, X)#, out = temp[0])
up = fast_linear_forward(self. up_proj, X)#, out = temp[1])
Expand Down Expand Up @@ -526,7 +521,7 @@ def LlamaModel_fast_forward(
position_ids = torch.arange(
past_key_values_length, seq_length + past_key_values_length,
dtype = torch.int32,
device = device,
device = "cuda:0",
)
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
elif position_ids is not None:
Expand Down Expand Up @@ -846,11 +841,8 @@ def _CausalLM_fast_forward(
if labels is not None:
shift_logits = logits
if not hasattr(self, "extra_ignored_labels"):
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = device_ids[:device_ids.find(',')] # Unsloth only works on NVIDIA GPUs for now
device = f"cuda:{device if device.isdigit() else '0'}"
# Fixes https://github.com/unslothai/unsloth/issues/10
self.extra_ignored_labels = torch.full((self.max_seq_length, 1), -100, device = device)
self.extra_ignored_labels = torch.full((self.max_seq_length, 1), -100, device = "cuda:0")
pass

shift_labels = torch.hstack((labels[..., 1:], self.extra_ignored_labels[:labels.shape[0]]))
Expand Down Expand Up @@ -1471,7 +1463,7 @@ def get_peft_model(
print("Unsloth: Casting embed_tokens to float32")

model.model.model.embed_tokens.modules_to_save.default\
.to(device = device, dtype = torch.float32, non_blocking = True)
.to(device = "cuda:0", dtype = torch.float32, non_blocking = True)
model.model.model.embed_tokens.modules_to_save.default.requires_grad_(True)

# [TODO] Move old embed_tokens to CPU - should be disk!
Expand All @@ -1484,7 +1476,7 @@ def get_peft_model(
print("Unsloth: Casting lm_head to float32")

model.model.lm_head.modules_to_save.default\
.to(device = device, dtype = torch.float32, non_blocking = True)
.to(device = "cuda:0", dtype = torch.float32, non_blocking = True)
model.model.lm_head.modules_to_save.default.requires_grad_(True)

# [TODO] Move old lm_head to CPU - should be disk!
Expand Down Expand Up @@ -1713,15 +1705,15 @@ def get_peft_model(
print("Unsloth: Casting embed_tokens to float32")
assert(hasattr(model.model.model.embed_tokens, "modules_to_save"))
model.model.model.embed_tokens.modules_to_save.default\
.to(device = device, dtype = torch.float32, non_blocking = True)
.to(device = "cuda:0", dtype = torch.float32, non_blocking = True)
model.model.model.embed_tokens.modules_to_save.default.requires_grad_(True)
pass

if train_lm_head:
print("Unsloth: Casting lm_head to float32")
assert(hasattr(model.model.lm_head, "modules_to_save"))
model.model.lm_head.modules_to_save.default\
.to(device = device, dtype = torch.float32, non_blocking = True)
.to(device = "cuda:0", dtype = torch.float32, non_blocking = True)
model.model.lm_head.modules_to_save.default.requires_grad_(True)
pass

Expand Down Expand Up @@ -1902,10 +1894,7 @@ def patch_peft_model(
# Patch cross entropy loss labels
# Fixes https://github.com/unslothai/unsloth/issues/10
max_seq_length = model.max_seq_length
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = device_ids[:device_ids.find(',')] # Unsloth only works on NVIDIA GPUs for now
device = f"cuda:{device if device.isdigit() else '0'}"
extra_ignored_labels = torch.full((max_seq_length, 1), -100, device = device)
extra_ignored_labels = torch.full((max_seq_length, 1), -100, device = "cuda:0")
model.model.extra_ignored_labels = extra_ignored_labels
internal_model = model
while hasattr(internal_model, "model"):
Expand Down
5 changes: 1 addition & 4 deletions unsloth/models/mistral.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,8 @@ def MistralForCausalLM_fast_forward(
if labels is not None:
shift_logits = logits
if not hasattr(self, "extra_ignored_labels"):
device_ids = os.environ.get("CUDA_VISIBLE_DEVICES", "0") + ","
device = device_ids[:device_ids.find(',')] # Unsloth only works on NVIDIA GPUs for now
device = f"cuda:{device if device.isdigit() else '0'}"
# Fixes https://github.com/unslothai/unsloth/issues/10
self.extra_ignored_labels = torch.full((self.max_seq_length, 1), -100, device = device)
self.extra_ignored_labels = torch.full((self.max_seq_length, 1), -100, device = "cuda:0")
pass

shift_labels = torch.hstack((labels[..., 1:], self.extra_ignored_labels[:labels.shape[0]]))
Expand Down