GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix tokenizer + docs by danielhanchen · Pull Request #62 · unslothai/unsloth

Fix tokenizer + docs #62

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.

Sign up for GitHub

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

Merged
danielhanchen merged 11 commits into main from nightly
Jan 4, 2024

Uh oh!

There was an error while loading. Please reload this page.

Merged

Fix tokenizer + docs #62

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
03b2771
Patch tokenizer
danielhanchen Jan 4, 2024
d274d1d
Update _utils.py
danielhanchen Jan 4, 2024
7569c09
Update _utils.py
danielhanchen Jan 4, 2024
f2b39f3
Update _utils.py
danielhanchen Jan 4, 2024
655e8dd
Cleanup
danielhanchen Jan 4, 2024
aa1daa3
Add comments to functions
danielhanchen Jan 4, 2024
5940579
Update rope_embedding.py
danielhanchen Jan 4, 2024
6e5895e
Update rope_embedding.py
danielhanchen Jan 4, 2024
09c955b
Update llama.py
danielhanchen Jan 4, 2024
25081c8
New logos!
danielhanchen Jan 4, 2024
eabdfa5
Update README.md
danielhanchen Jan 4, 2024
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
Patch tokenizer
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@danielhanchen
danielhanchen committed Jan 4, 2024
commit 03b277107e5a9e29a76c2929bc8d310bfe651a8e

There are no files selected for viewing

62 changes: 49 additions & 13 deletions 62 unsloth/models/_utils.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,60 @@ def patch_tokenizer(model, tokenizer):
pass


def check_tokenizer(model, tokenizer):
def check_tokenizer(
model,
tokenizer,
model_name = "unsloth/llama-2-7b-bnb-4bit",
model_max_length = 4096,
padding_side = "right",
token = None,
_reload = True,
):
# Checks tokenizer for out of bounds ids.
# Mainly a fix for https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha
# where <sep> had token id=32002.
# See https://huggingface.co/berkeley-nest/Starling-LM-7B-alpha/discussions/25
special_tokens_map = tokenizer.special_tokens_map
# Seems like the Fast tokenizer in Rust breaks things!

max_embedding_size = model.model.embed_tokens.weight.shape[0]

for token_name, token_content in special_tokens_map.items():
if type(token_content) is not str: continue
token_ids = tokenizer([token_content], add_special_tokens = False, return_attention_mask = False)
token_ids = token_ids.input_ids[0][0]
if token_ids < 0 or token_ids >= max_embedding_size:
raise RuntimeError(
f"Unsloth: Extra special token `{token_content}` with id={token_ids} exceeds "\
f"the maximum vocabulary size of {max_embedding_size}. You must fix the tokenizer "\
"or else out of bounds memory accesses will occur."
added_tokens_fast = tokenizer.added_tokens_decoder
added_tokens_fast = {index : str(value) for index, value in added_tokens_fast.items()}
sorted_keys = sorted(added_tokens_fast)
added_tokens_fast = {key : added_tokens_fast[key] for key in sorted_keys}

for j, index in enumerate(added_tokens_fast.keys()):
if index >= max_embedding_size:
bad_indices = list(added_tokens_fast.keys ())[j:]
bad_tokens = list(added_tokens_fast.values())[j:]
logger.warning_once(
f"Unsloth: Tokenizer for `{model_name}` was loaded as Rust fast tokenizer, but\n"\
f"will sadly do out of bounds memory accesses since the tokens={bad_tokens} with\n"\
f"token ids={bad_indices} exceeds the maximum vocab size of {max_embedding_size}.\n"\
f"Unsloth will attempt to load the tokenizer using a slow tokenizer instead."
)
if not _reload:
raise RuntimeError(
"Unsloth tried a slow tokenizer, but cannot succeed. Your tokenizer is broken."
)
# Try slow tokenizer which can fix things!
tokenizer = AutoTokenizer.from_pretrained(
model_name,
model_max_length = model_max_length,
padding_side = padding_side,
token = token,
use_fast = False,
)
return check_tokenizer(
model = model,
tokenizer = tokenizer,
model_name = model_name,
model_max_length = 4096,
padding_side = padding_side,
token = token,
_reload = False,
)
break
pass
pass
pass
return tokenizer
pass
9 changes: 8 additions & 1 deletion 9 unsloth/models/llama.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,14 @@ def from_pretrained(
internal_model.max_seq_length = max_position_embeddings

# We check the tokenizer first for errors
check_tokenizer(model, tokenizer)
tokenizer = check_tokenizer(
model = model,
tokenizer = tokenizer,
model_name = model_name,
model_max_length = max_seq_length,
padding_side = "right",
token = token,
)
return model, tokenizer
pass

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.