Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
638 commits
Select commit Hold shift + click to select a range
a856085
Update rl.py
danielhanchen Feb 16, 2025
eeac4f3
Update rl_replacements.py
danielhanchen Feb 16, 2025
6f1beb0
Update rl_replacements.py
danielhanchen Feb 16, 2025
222b1e7
llama-quantize on WINDOWS WSL error fix - edit save.py (gguf saving b…
everythingisc00l Feb 16, 2025
103cff4
Update rl_replacements.py
danielhanchen Feb 17, 2025
89a1d03
Update rl_replacements.py
danielhanchen Feb 17, 2025
c46b544
Update rl_replacements.py
danielhanchen Feb 17, 2025
ed84307
Update rl_replacements.py
danielhanchen Feb 17, 2025
93d3f16
Update rl_replacements.py
danielhanchen Feb 17, 2025
429ba6d
Update rl_replacements.py
danielhanchen Feb 17, 2025
1e42bad
Update rl_replacements.py
danielhanchen Feb 17, 2025
38a1885
Update rl_replacements.py
danielhanchen Feb 17, 2025
f0ee4f5
Update rl_replacements.py
danielhanchen Feb 17, 2025
b68dce6
Update rl_replacements.py
danielhanchen Feb 17, 2025
0827067
Update llama.py
danielhanchen Feb 17, 2025
204cd7a
Update rl_replacements.py
danielhanchen Feb 17, 2025
e141075
Update rl_replacements.py
danielhanchen Feb 17, 2025
a07a9e3
Update rl_replacements.py
danielhanchen Feb 17, 2025
cf2720d
Update llama.py
danielhanchen Feb 17, 2025
5c6f586
Update llama.py
danielhanchen Feb 18, 2025
2e07623
Update rl_replacements.py
danielhanchen Feb 18, 2025
8025cfe
Update rl_replacements.py
danielhanchen Feb 18, 2025
ba48495
Update rl_replacements.py
danielhanchen Feb 18, 2025
f0078de
Update rl.py
danielhanchen Feb 18, 2025
15e0140
Update rl.py
danielhanchen Feb 18, 2025
5f5cca4
Update rl_replacements.py
danielhanchen Feb 18, 2025
d80be70
Update rl.py
danielhanchen Feb 18, 2025
47a85eb
Update rl.py
danielhanchen Feb 18, 2025
f09478d
Update rl_replacements.py
danielhanchen Feb 18, 2025
97637c5
Update rl_replacements.py
danielhanchen Feb 18, 2025
58bd27f
Update rl_replacements.py
danielhanchen Feb 18, 2025
7c0c749
Update rl_replacements.py
danielhanchen Feb 18, 2025
97b55c1
Update rl_replacements.py
danielhanchen Feb 18, 2025
24c7a2f
Update rl_replacements.py
danielhanchen Feb 18, 2025
06b2cd3
unsloth_num_chunks
danielhanchen Feb 18, 2025
cbb16e3
Update rl.py
danielhanchen Feb 18, 2025
d16299b
Update rl_replacements.py
danielhanchen Feb 18, 2025
0c1a808
Update rl_replacements.py
danielhanchen Feb 18, 2025
6796801
Update rl_replacements.py
danielhanchen Feb 18, 2025
bd046ca
Update rl.py
danielhanchen Feb 18, 2025
ac2e814
Update rl.py
danielhanchen Feb 18, 2025
a88712f
Update rl.py
danielhanchen Feb 18, 2025
0daa328
Update rl.py
danielhanchen Feb 18, 2025
1afe3f2
Update rl.py
danielhanchen Feb 18, 2025
6732822
Update rl_replacements.py
danielhanchen Feb 18, 2025
5efe9f3
Update rl_replacements.py
danielhanchen Feb 18, 2025
15442d1
Update rl_replacements.py (#1754)
SethHWeidman Feb 19, 2025
91ab43d
Optional logits
danielhanchen Feb 19, 2025
a6a5f60
Update rl.py
danielhanchen Feb 19, 2025
83ce085
Update rl.py
danielhanchen Feb 19, 2025
8ece11f
Update rl.py
danielhanchen Feb 19, 2025
bc6bfae
Update rl.py
danielhanchen Feb 20, 2025
95fb6a4
Update rl.py
danielhanchen Feb 20, 2025
ba01cf5
Update rl.py
danielhanchen Feb 20, 2025
eb48b98
Update rl.py
danielhanchen Feb 20, 2025
3c750a1
Update rl.py
danielhanchen Feb 20, 2025
515cf5a
Update rl_replacements.py
danielhanchen Feb 20, 2025
2cf4349
Update rl.py
danielhanchen Feb 20, 2025
ae8bf68
Update rl.py
danielhanchen Feb 20, 2025
e07f4bc
Update rl.py
danielhanchen Feb 20, 2025
f11e5ab
Merge branch 'main' into nightly
danielhanchen Feb 20, 2025
3fccf5d
Update rl.py
danielhanchen Feb 20, 2025
798ad95
fix an import error (#1767)
NinoRisteski Feb 20, 2025
2957d89
SamplingParams
danielhanchen Feb 20, 2025
19d57bc
Convert mask to float (#1762)
Erland366 Feb 20, 2025
07aea40
[Windows Support] Add latest `xformers` wheels to pyproject.toml (#1753)
versipellis Feb 20, 2025
77109a4
Merge branch 'nightly' of https://github.com/unslothai/unsloth into n…
danielhanchen Feb 20, 2025
f3d9efb
vLLMSamplingParams
danielhanchen Feb 20, 2025
6d5caca
Update __init__.py
danielhanchen Feb 20, 2025
3a5610e
default num_chunks == -1
danielhanchen Feb 20, 2025
0362bd2
Versioning
danielhanchen Feb 20, 2025
2969db8
Merge branch 'main' into nightly
danielhanchen Feb 20, 2025
b5eda24
Update llama.py
danielhanchen Feb 20, 2025
7de0022
Update llama.py
danielhanchen Feb 20, 2025
d4d7694
Update llama.py
danielhanchen Feb 20, 2025
0bbfbe8
Update llama.py
danielhanchen Feb 20, 2025
ae6e2bd
Update llama.py
danielhanchen Feb 20, 2025
1792deb
Update _utils.py
danielhanchen Feb 20, 2025
5dcd079
Update rl_replacements.py
danielhanchen Feb 20, 2025
ec6e0b7
Update rl_replacements.py
danielhanchen Feb 20, 2025
bc1d2ce
Update pyproject.toml
danielhanchen Feb 20, 2025
adbe38e
Update pyproject.toml
danielhanchen Feb 20, 2025
a9b542f
Export Model to ollama.com (#1648)
gjyotin305 Feb 22, 2025
f853ac0
Merge branch 'main' into nightly
danielhanchen Mar 3, 2025
9cab347
Update cross_entropy_loss.py
danielhanchen Mar 3, 2025
0ae9082
torch_cuda_device
danielhanchen Mar 3, 2025
f21314c
Update utils.py
danielhanchen Mar 3, 2025
9215212
Update utils.py
danielhanchen Mar 3, 2025
9d95aee
Update utils.py
danielhanchen Mar 3, 2025
35e9144
device
danielhanchen Mar 3, 2025
30b6f94
device
danielhanchen Mar 3, 2025
64e2b00
Update loader.py
danielhanchen Mar 3, 2025
ffa3278
Update llama.py
danielhanchen Mar 3, 2025
748c5b5
Update README.md
danielhanchen Mar 3, 2025
469ed48
Update llama.py
danielhanchen Mar 3, 2025
bc87afd
Update llama.py
danielhanchen Mar 3, 2025
ee9d6e5
Update _utils.py
danielhanchen Mar 4, 2025
91458bb
Update utils.py
danielhanchen Mar 4, 2025
a7a5d75
Update utils.py
danielhanchen Mar 4, 2025
d93cca2
Update utils.py
danielhanchen Mar 4, 2025
6e2a3a8
Update utils.py
danielhanchen Mar 4, 2025
8f9ba99
Update utils.py
danielhanchen Mar 4, 2025
ed697da
Update llama.py
danielhanchen Mar 4, 2025
d73c34b
Update llama.py
danielhanchen Mar 4, 2025
4485da7
Update llama.py
danielhanchen Mar 4, 2025
45ea48c
Update llama.py
danielhanchen Mar 4, 2025
8c4b79c
Update llama.py
danielhanchen Mar 4, 2025
c2ae510
Update utils.py
danielhanchen Mar 4, 2025
432ea24
Update utils.py
danielhanchen Mar 4, 2025
dcff03c
Update utils.py
danielhanchen Mar 4, 2025
6ef0866
Update utils.py
danielhanchen Mar 4, 2025
8c8ce96
__version__
danielhanchen Mar 4, 2025
208971b
Update rl.py
danielhanchen Mar 4, 2025
adc6977
Bug fixes
danielhanchen Mar 4, 2025
949c298
Bug fixes
danielhanchen Mar 4, 2025
ad6d962
Merge branch 'main' into nightly
danielhanchen Mar 4, 2025
59b24ad
Update llama.py
danielhanchen Mar 5, 2025
5df3936
Update _utils.py
danielhanchen Mar 5, 2025
b8b0f9c
_wrap_fast_inference
danielhanchen Mar 5, 2025
6f0857b
Update llama.py
danielhanchen Mar 5, 2025
109364b
Update llama.py
danielhanchen Mar 5, 2025
dd4bd07
Update llama.py
danielhanchen Mar 5, 2025
b356fce
Update llama.py
danielhanchen Mar 5, 2025
e022016
Update llama.py
danielhanchen Mar 5, 2025
12094a7
Update llama.py
danielhanchen Mar 5, 2025
2836128
Update llama.py
danielhanchen Mar 5, 2025
c956616
Update llama.py
danielhanchen Mar 5, 2025
e887f43
Update llama.py
danielhanchen Mar 5, 2025
95f872d
Update llama.py
danielhanchen Mar 5, 2025
647dbb4
Update llama.py
danielhanchen Mar 5, 2025
f640c8d
Update _utils.py
danielhanchen Mar 5, 2025
91a4fce
SFT dataset prepare
danielhanchen Mar 5, 2025
4495148
Update pyproject.toml
danielhanchen Mar 5, 2025
f41dff5
Update rl_replacements.py
danielhanchen Mar 5, 2025
0a3dbfa
Update rl_replacements.py
danielhanchen Mar 5, 2025
7d8f100
Update rl_replacements.py
danielhanchen Mar 5, 2025
413ea80
Update rl.py
danielhanchen Mar 5, 2025
3f5ce93
Update llama.py
danielhanchen Mar 5, 2025
185bced
Update llama.py
danielhanchen Mar 5, 2025
fd11ad7
Update utils.py
danielhanchen Mar 5, 2025
97ed0b4
bug fix
danielhanchen Mar 5, 2025
68eca88
Update llama.py
danielhanchen Mar 5, 2025
5daf9b5
Update llama.py
danielhanchen Mar 5, 2025
858bb76
Update llama.py
danielhanchen Mar 5, 2025
daedc34
Update llama.py
danielhanchen Mar 5, 2025
95e2371
Update llama.py
danielhanchen Mar 5, 2025
fccd68a
Update __init__.py
danielhanchen Mar 5, 2025
c665e0b
Update _utils.py
danielhanchen Mar 6, 2025
d207daf
Merge branch 'main' into nightly
danielhanchen Mar 6, 2025
dbf7eac
Update _utils.py
danielhanchen Mar 6, 2025
b55f6d9
Update _utils.py
danielhanchen Mar 6, 2025
c7abf7d
Update _utils.py
danielhanchen Mar 6, 2025
98d5ab0
Update _utils.py
danielhanchen Mar 6, 2025
f72794e
Update rl.py
danielhanchen Mar 6, 2025
1ec0ee2
Update rl.py
danielhanchen Mar 6, 2025
5350c6a
Update rl.py
danielhanchen Mar 6, 2025
9009ef0
Update _utils.py
danielhanchen Mar 6, 2025
7f7899d
Update __init__.py
danielhanchen Mar 6, 2025
334bd77
Update _utils.py
danielhanchen Mar 6, 2025
ade31e2
Version
danielhanchen Mar 6, 2025
a31e45b
Merge branch 'main' into nightly
danielhanchen Mar 6, 2025
8015ff2
versioning
danielhanchen Mar 6, 2025
d8777be
Update _utils.py
danielhanchen Mar 6, 2025
132b838
Update llama.py
danielhanchen Mar 6, 2025
21faa50
Update llama.py
danielhanchen Mar 6, 2025
af5d875
Merge branch 'main' into nightly
danielhanchen Mar 6, 2025
904e1c5
Bug fixes
danielhanchen Mar 7, 2025
761bb8f
FastModel
danielhanchen Mar 8, 2025
7bf880f
__doc__
danielhanchen Mar 8, 2025
c93b51b
Update vision.py
danielhanchen Mar 8, 2025
f8867be
Update loader.py
danielhanchen Mar 8, 2025
2ab1828
Update loader.py
danielhanchen Mar 8, 2025
e05baed
Update loader.py
danielhanchen Mar 8, 2025
31012a7
version
danielhanchen Mar 8, 2025
a8bf659
Merge branch 'main' into nightly
danielhanchen Mar 9, 2025
d72e3e0
move use_modelscope to _utils (#1938)
KareemMusleh Mar 9, 2025
7e82339
Don't use revision when loading model_config and is_peft=True (#1949)
wiwu2390 Mar 9, 2025
4904c48
More syntax warnings (#1944)
KareemMusleh Mar 9, 2025
7aaa605
Update loader.py
danielhanchen Mar 9, 2025
a585536
Full finetuning and other fixes
danielhanchen Mar 10, 2025
133c0ae
UNSLOTH_ENABLE_FULL_FINETUNING
danielhanchen Mar 10, 2025
9d5aa5c
Update loader.py
danielhanchen Mar 10, 2025
934ad16
Update loader.py
danielhanchen Mar 10, 2025
76f2f2a
Update loader.py
danielhanchen Mar 10, 2025
f763ed6
Update vision.py
danielhanchen Mar 10, 2025
0df9518
Update vision.py
danielhanchen Mar 10, 2025
ced164e
full finetuning
danielhanchen Mar 10, 2025
5b45f0f
Update loader.py
danielhanchen Mar 10, 2025
23d45cf
Update loader.py
danielhanchen Mar 10, 2025
bdebea7
Update loader.py
danielhanchen Mar 10, 2025
04f1abc
Update _utils.py
danielhanchen Mar 10, 2025
4c0a8d6
max_seq_length
danielhanchen Mar 10, 2025
8f16ce0
Update rl.py
danielhanchen Mar 10, 2025
8b16a16
Update rl.py
danielhanchen Mar 10, 2025
a8c96d3
Update rl.py
danielhanchen Mar 10, 2025
739b1dd
Update pyproject.toml
danielhanchen Mar 11, 2025
c555388
AutoModelForImageTextToText
danielhanchen Mar 11, 2025
77fec99
Update mapper.py
danielhanchen Mar 11, 2025
c539fc6
Update pyproject.toml
danielhanchen Mar 11, 2025
3ddcf84
Update _utils.py
danielhanchen Mar 11, 2025
3aa2d95
Update _utils.py
danielhanchen Mar 11, 2025
a3541c0
Update _utils.py
danielhanchen Mar 11, 2025
a4faf0f
Batch samples
danielhanchen Mar 12, 2025
eb0add4
Update loader.py
danielhanchen Mar 12, 2025
b556785
Update loader.py
danielhanchen Mar 12, 2025
ead1b3b
Update loader.py
danielhanchen Mar 12, 2025
b388d8d
Update loader.py
danielhanchen Mar 12, 2025
80eac80
Update _utils.py
danielhanchen Mar 12, 2025
d6d862e
Update loader.py
danielhanchen Mar 12, 2025
ea6aae6
Update vision.py
danielhanchen Mar 12, 2025
0c4ebb3
Update loader.py
danielhanchen Mar 12, 2025
528e8f0
Update vision.py
danielhanchen Mar 12, 2025
152b376
Update vision.py
danielhanchen Mar 12, 2025
2fdeecd
Update vision.py
danielhanchen Mar 12, 2025
ceda772
Update mapper.py
danielhanchen Mar 12, 2025
0df6ad4
Merge branch 'main' into nightly
danielhanchen Mar 12, 2025
f386f0f
Update vision.py
danielhanchen Mar 12, 2025
b6187c6
Temporary patches
danielhanchen Mar 13, 2025
bb59cec
Update loader.py
danielhanchen Mar 13, 2025
3326c4f
model names
danielhanchen Mar 13, 2025
bb193e4
Gemma 3 chat template
danielhanchen Mar 13, 2025
57a5442
Bug fixes
danielhanchen Mar 13, 2025
8457c75
Update vision.py
danielhanchen Mar 13, 2025
bc735a7
Update vision.py
danielhanchen Mar 13, 2025
ed588ee
Update vision.py
danielhanchen Mar 13, 2025
a3637fa
Update vision.py
danielhanchen Mar 13, 2025
6218eae
Update vision.py
danielhanchen Mar 13, 2025
9005a57
Update llama.py
danielhanchen Mar 13, 2025
97f40bd
Update llama.py
danielhanchen Mar 13, 2025
24cd9f7
Update rl.py
danielhanchen Mar 13, 2025
b0d9ee0
Update chat_templates.py
danielhanchen Mar 13, 2025
07f47a4
Update chat_templates.py
danielhanchen Mar 13, 2025
caec8ff
Update vision.py
danielhanchen Mar 13, 2025
c96eab5
Update vision.py
danielhanchen Mar 13, 2025
6e58d97
Update vision.py
danielhanchen Mar 13, 2025
dd17676
Update loader.py
danielhanchen Mar 13, 2025
7d0893b
Update vision.py
danielhanchen Mar 13, 2025
8b51a7d
Update vision.py
danielhanchen Mar 13, 2025
833e295
Revert
danielhanchen Mar 13, 2025
20ae25a
Update _utils.py
danielhanchen Mar 13, 2025
067fb5e
forced precision
danielhanchen Mar 13, 2025
7493af8
Autocast
danielhanchen Mar 13, 2025
6dcd0bf
Update vision.py
danielhanchen Mar 13, 2025
c6eae35
Update vision.py
danielhanchen Mar 13, 2025
d1f09cf
Update rl.py
danielhanchen Mar 13, 2025
e0e31d9
Update vision.py
danielhanchen Mar 13, 2025
57576a5
Update vision.py
danielhanchen Mar 13, 2025
3b6c379
Update vision.py
danielhanchen Mar 13, 2025
b284ed5
Update vision.py
danielhanchen Mar 13, 2025
ed80c07
Update vision.py
danielhanchen Mar 13, 2025
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
173 changes: 92 additions & 81 deletions unsloth/chat_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

"to_sharegpt",
"standardize_sharegpt",
"standardize_data_formats",
"apply_chat_template",
"train_on_responses_only",

Expand All @@ -37,7 +38,9 @@
import re
from unsloth_zoo.dataset_utils import (
train_on_responses_only,
standardize_data_formats,
)
standardize_sharegpt = standardize_data_formats
CHAT_TEMPLATES = {}
DEFAULT_SYSTEM_MESSAGE = {}

Expand Down Expand Up @@ -934,6 +937,84 @@
pass


# =========================================== Gemma-3
# Obtained via
# print(tokenizer.chat_template.replace("}\n", "####").replace("\n", "\\n").replace("####", "}\n"))
gemma3_template = \
"""{{ bos_token }}
{%- if messages[0]['role'] == 'system' -%}
{%- if messages[0]['content'] is string -%}
{%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}
{%- else -%}
{%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}
{%- endif -%}
{%- set loop_messages = messages[1:] -%}
{%- else -%}
{%- set first_user_prefix = "" -%}
{%- set loop_messages = messages -%}
{%- endif -%}
{%- for message in loop_messages -%}
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
{%- endif -%}
{%- if (message['role'] == 'assistant') -%}
{%- set role = "model" -%}
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
{{ '<start_of_turn>' + role + '\n' + (first_user_prefix if loop.first else "") }}
{%- if message['content'] is string -%}
{{ message['content'] | trim }}
{%- elif message['content'] is iterable -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'image' -%}
{{ '<start_of_image>' }}
{%- elif item['type'] == 'text' -%}
{{ item['text'] | trim }}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{ raise_exception("Invalid content type") }}
{%- endif -%}
{{ '<end_of_turn>\n' }}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{ '<start_of_turn>model\n' }}
{%- endif -%}
"""

# Ollama from https://ollama.com/library/gemma3/blobs/e0a42594d802
gemma3_ollama = \
'''
FROM {__FILE_LOCATION__}
TEMPLATE """{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 }}
{{- if or (eq .Role "user") (eq .Role "system") }}<start_of_turn>user
{{ .Content }}<end_of_turn>
{{ if $last }}<start_of_turn>model
{{ end }}
{{- else if eq .Role "assistant" }}<start_of_turn>model
{{ .Content }}{{ if not $last }}<end_of_turn>
{{ end }}
{{- end }}
{{- end }}"""
PARAMETER stop "<end_of_turn>"
PARAMETER stop "<eos>"
PARAMETER temperature 0.1
PARAMETER min_p 0.0
PARAMETER top_k 64
PARAMETER top_p 0.95
PARAMETER num_predict 32768
'''

gemma3_template_eos_token = "<end_of_turn>"
CHAT_TEMPLATES["gemma-3"] = (gemma3_template, gemma3_template_eos_token, False, gemma3_ollama,)
DEFAULT_SYSTEM_MESSAGE["gemma-3"] = None # No system message in Gemma-3

CHAT_TEMPLATES["gemma3"] = (gemma3_template, gemma3_template_eos_token, False, gemma3_ollama,)
DEFAULT_SYSTEM_MESSAGE["gemma3"] = None # No system message in Gemma-3
pass

def _change_system_message(template: str, type_chat_template: str, system_message: str = None):
system_message_pattern = r"\{system_message\}"

Expand Down Expand Up @@ -1033,11 +1114,12 @@ def get_chat_template(

# Check fast tokenizer
if not is_fast_tokenizer:
print(
"Unsloth: Not a fast tokenizer, so can't process it as of yet :(\n"\
"Please log a Github issue if you want this as a new feature!\n"\
"Your chat template will still work, but it won't add or edit tokens."
)
pass
# print(
# "Unsloth: Not a fast tokenizer, so can't process it as of yet :(\n"\
# "Please log a Github issue if you want this as a new feature!\n"\
# "Your chat template will still work, but it won't add or edit tokens."
# )

elif token_mapping is not None:
# token_mapping = {"<start_of_turn>" : "<|im_start|>", "<end_of_turn>" : "<|im_end|>"}
Expand Down Expand Up @@ -1396,82 +1478,6 @@ def __convert_to_sharegpt__(examples):
pass


def standardize_sharegpt(
dataset,
aliases_for_system = ["system",],
aliases_for_user = ["user", "human", "input",],
aliases_for_assistant = ["gpt", "assistant", "output",],
):
"""
Standardizes ShareGPT and other formats to user/assistant Hugging Face format.

Get aliases for the system, user and assistant roles.
These shall map to "system", "user" and "assistant" respectively.

aliases_for_system = ["system",],
aliases_for_user = ["user", "human", "input",],
aliases_for_assistant = ["gpt", "assistant", "output",],
"""
import collections
import itertools

convos = dataset[:10]["conversations"]
uniques = collections.defaultdict(list)
for convo in convos:
for message in convo:
for key, value in message.items():
uniques[key].append(value)
pass

# Must be only 2 entries
assert(len(uniques.keys()) == 2)

keys = list(uniques.keys())
length_first = len(set(uniques[keys[0]]))
length_second = len(set(uniques[keys[1]]))

if length_first < length_second:
# Role is assigned to the first element
role_key = keys[0]
content_key = keys[1]
else:
role_key = keys[1]
content_key = keys[0]
pass

# Check roles are in aliases
all_aliases = set(aliases_for_system + aliases_for_user + aliases_for_assistant)
roles = set(uniques[role_key])
leftover_aliases = (all_aliases | roles) - all_aliases
if len(leftover_aliases) != 0:
raise TypeError(
f"Unsloth: {list(leftover_aliases)} are not in aliases. Please update aliases."
)
pass

# Mapping for aliases
aliases_mapping = {}
for x in aliases_for_system: aliases_mapping[x] = "system"
for x in aliases_for_user: aliases_mapping[x] = "user"
for x in aliases_for_assistant: aliases_mapping[x] = "assistant"

def _standardize_dataset(examples):
convos = examples["conversations"]
all_convos = []
for convo in convos:
new_convo = [
{ "role" : aliases_mapping[message[role_key]], "content" : message[content_key], }
for message in convo
]
all_convos.append(new_convo)
pass
return { "conversations" : all_convos, }
pass

return dataset.map(_standardize_dataset, batched = True, desc = "Standardizing format")
pass


def get_ollama_eos_tokens(tokenizer, extra_eos_tokens = []):
added_tokens_decoder = tokenizer.added_tokens_decoder.values()
added_tokens_decoder = [str(x) for x in added_tokens_decoder]
Expand Down Expand Up @@ -1934,6 +1940,11 @@ def formatting_prompts_func(examples):
tokenizer._ollama_modelfile = modelfile
tokenizer._unsloth_input_part = input_part
tokenizer._unsloth_output_part = output_part
if hasattr(tokenizer, "tokenizer"):
tokenizer.tokenizer.chat_template = jinja_template
tokenizer.tokenizer._ollama_modelfile = modelfile
tokenizer.tokenizer._unsloth_input_part = input_part
tokenizer.tokenizer._unsloth_output_part = output_part

return dataset.map(formatting_prompts_func, batched = True,)
pass
Expand Down
15 changes: 14 additions & 1 deletion unsloth/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
from platform import system as platform_system
platform_system = platform_system()
import numpy as np
import contextlib
import warnings, subprocess, re, inspect, psutil, os, math
from unsloth_zoo.utils import Version

Expand Down Expand Up @@ -113,6 +114,11 @@
from unsloth_zoo.training_utils import (
prepare_model_for_training,
)
from unsloth_zoo.temporary_patches import (
TEMPORARY_PATCHES,
)
for temporary_patch in TEMPORARY_PATCHES:
temporary_patch()

# =============================================
# Disable some warnings which can get annoying
Expand Down Expand Up @@ -981,7 +987,14 @@ def _unsloth_pre_compute_loss(self, model, inputs, *args, **kwargs):
"Read more on gradient accumulation issues here: https://unsloth.ai/blog/gradient"
)
pass
return self._old_compute_loss(model, inputs, *args, **kwargs)

if os.environ.get("UNSLOTH_FORCE_FLOAT32", "0") == "0":
autocaster = contextlib.nullcontext()
else:
autocaster = torch.autocast(device_type = "cuda", dtype = torch.float32)
with autocaster:
outputs = self._old_compute_loss(model, inputs, *args, **kwargs)
return outputs
pass


Expand Down
33 changes: 33 additions & 0 deletions unsloth/models/llama.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from ..tokenizer_utils import *
if HAS_FLASH_ATTENTION:
from flash_attn import flash_attn_func
from .vision import FastBaseModel

# Final patching code
from transformers.models.llama.modeling_llama import (
Expand Down Expand Up @@ -1648,6 +1649,7 @@ def from_pretrained(
disable_log_stats = False,
**kwargs,
):
os.environ["UNSLOTH_USE_NEW_MODEL"] = "0"
if trust_remote_code:
if fast_inference:
raise NotImplementedError("Unsloth: Fast inference does not support `trust_remote_code` yet.")
Expand Down Expand Up @@ -2016,6 +2018,31 @@ def get_peft_model(
temporary_location = "_unsloth_temporary_saved_buffers",
**kwargs,
):
if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1":
return FastBaseModel.get_peft_model(
model = model,
r = r,
target_modules = target_modules,
lora_alpha = lora_alpha,
lora_dropout = lora_dropout,
bias = bias,
finetune_vision_layers = False,
finetune_language_layers = True,
finetune_attention_modules = True,
finetune_mlp_modules = True,
layers_to_transform = layers_to_transform,
layers_pattern = layers_pattern,
use_gradient_checkpointing = use_gradient_checkpointing,
random_state = random_state,
max_seq_length = max_seq_length,
use_rslora = use_rslora,
modules_to_save = modules_to_save,
init_lora_weights = init_lora_weights,
loftq_config = loftq_config,
temporary_location = temporary_location,
**kwargs,
)
pass
if os.environ.get("UNSLOTH_ENABLE_FULL_FINETUNING", "0") == "1":
print("Unsloth: Full finetuning is enabled, so .get_peft_model has no effect")
return model
Expand Down Expand Up @@ -2435,6 +2462,12 @@ def patch_peft_model(
model,
use_gradient_checkpointing = True,
):
if os.environ.get("UNSLOTH_USE_NEW_MODEL", "0") == "1":
return FastBaseModel.patch_peft_model(
model = model,
use_gradient_checkpointing = use_gradient_checkpointing,
)
pass
if not isinstance(model, PeftModelForCausalLM):
raise TypeError(
"Unsloth: Your model needs to call `.get_peft_model` first!"
Expand Down
10 changes: 6 additions & 4 deletions unsloth/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class FastLanguageModel(FastLlamaModel):
@staticmethod
def from_pretrained(
model_name = "unsloth/Llama-3.2-1B-Instruct",
max_seq_length = None,
max_seq_length = 2048,
dtype = None,
load_in_4bit = True,
load_in_8bit = False,
Expand All @@ -96,7 +96,7 @@ def from_pretrained(
if load_in_8bit or full_finetuning:
return FastModel.from_pretrained(
model_name = model_name,
max_seq_length = max_seq_length, # [TODO] No effect
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
load_in_8bit = load_in_8bit,
Expand Down Expand Up @@ -295,7 +295,7 @@ def from_pretrained(
else:
return FastModel.from_pretrained(
model_name = model_name,
max_seq_length = max_seq_length, # [TODO] No effect
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
load_in_8bit = load_in_8bit,
Expand Down Expand Up @@ -442,7 +442,7 @@ class FastModel(FastBaseModel):
@staticmethod
def from_pretrained(
model_name = "unsloth/Llama-3.2-11B-Vision-Instruct-bnb-4bit",
max_seq_length = None, # [TODO] No effect
max_seq_length = 2048,
dtype = None,
load_in_4bit = True,
load_in_8bit = False,
Expand Down Expand Up @@ -500,6 +500,8 @@ def from_pretrained(
raise RuntimeError("Unsloth: Qwen 2.5 only works on transformers >= 4.49.0." + LATEST)
elif "aya-vision" in model_name.lower() and transformers_version < Version("4.50.0.dev0"):
raise RuntimeError("Unsloth: Aya Vision only works on transformers >= 4.50.0." + NIGHTLY)
elif "gemma-3" in model_name.lower() and transformers_version < Version("4.50.0.dev0"):
raise RuntimeError("Unsloth: Gemma 3 only works on transformers >= 4.50.0." + NIGHTLY)
pass

if USE_MODELSCOPE and not os.path.exists(model_name):
Expand Down
Loading