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

[Tokenizer] Support reading Tiktoken tokenizer.model. #9215

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from

Conversation

lvdongyi
Copy link
Contributor

@lvdongyi lvdongyi commented Sep 28, 2024

PR types

New features

PR changes

APIs

Description

  1. Support reading Tiktoken tokenizer.model.

  2. Split PretrainedTokenizerBase.from_pretrained into two separate methods: from_pretrained and _from_pretrained.

  3. Prefer not to use FastTokenizer even it is available. (When you want to load TokenizerFast through AutoTokenizer, you should explicitly set use_fast=True )

  4. Use LazyMapping to load keys and values when it is accessed.

  5. Do not allow multiple Tokenizer Classes in a tokenizer.py

    e.g.

    • Previouslypaddlenlp.transformers.albert.tokenizerwas split into paddlenlp.transformers.albert.tokenizer,paddlenlp.transformers.albert_chinese.tokenizer,paddlenlp.transformers.albert_english.tokenizer

    • Previouslypaddlenlp.transformers.mbart.tokenizerwas split into paddlenlp.transformers.mbart.tokenizer,paddlenlp.transformers.mbart50.tokenizer

  6. Modify tests/transformers/test_modeling_common.py to support LlamaTokenizerFast

TOKENIZER_MAPPING_NAMES, MODEL_NAMES_MAPPING, CONFIG_MAPPING_NAMES should be reviewed carefully

Copy link

paddle-bot bot commented Sep 28, 2024

Thanks for your contribution!

Copy link

codecov bot commented Sep 28, 2024

Codecov Report

Attention: Patch coverage is 77.03281% with 161 lines in your changes missing coverage. Please review.

Project coverage is 53.04%. Comparing base (5ad7a9c) to head (f0f4113).
Report is 8 commits behind head on develop.

Current head f0f4113 differs from pull request most recent head e84a062

Please upload reports for the commit e84a062 to get more accurate results.

Files with missing lines Patch % Lines
paddlenlp/transformers/auto/factory.py 46.06% 48 Missing ⚠️
paddlenlp/utils/import_utils.py 54.92% 32 Missing ⚠️
paddlenlp/transformers/auto/tokenizer.py 77.10% 19 Missing ⚠️
paddlenlp/transformers/mbart50/tokenizer.py 86.29% 17 Missing ⚠️
paddlenlp/transformers/albert_english/tokenizer.py 85.98% 15 Missing ⚠️
paddlenlp/transformers/auto/configuration.py 80.00% 13 Missing ⚠️
paddlenlp/transformers/convert_slow_tokenizer.py 80.59% 13 Missing ⚠️
paddlenlp/transformers/tokenizer_utils_base.py 90.62% 3 Missing ⚠️
paddlenlp/transformers/llama/tokenizer.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9215      +/-   ##
===========================================
+ Coverage    52.99%   53.04%   +0.04%     
===========================================
  Files          671      667       -4     
  Lines       109835   106886    -2949     
===========================================
- Hits         58212    56699    -1513     
+ Misses       51623    50187    -1436     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

paddlenlp/transformers/configuration_utils.py Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Show resolved Hide resolved
paddlenlp/transformers/fnet/tokenizer.py Outdated Show resolved Hide resolved
paddlenlp/transformers/mbart50/__init__.py Outdated Show resolved Hide resolved
paddlenlp/utils/download/__init__.py Outdated Show resolved Hide resolved
paddlenlp/transformers/auto/configuration.py Outdated Show resolved Hide resolved
paddlenlp/transformers/auto/configuration.py Outdated Show resolved Hide resolved
paddlenlp/transformers/auto/configuration.py Show resolved Hide resolved
paddlenlp/transformers/auto/configuration.py Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Outdated Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Outdated Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Show resolved Hide resolved
@@ -176,7 +324,7 @@ def _get_tokenizer_class_from_config(cls, pretrained_model_name_or_path, config_
return tokenizer_class

@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数修改名称需要注意,需判断其他from_pretrained方法参数是否使用相同名称

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他Tokenizer都没有override from_pretrained方法,所以应该不会有影响

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的问题是使用auto.from_pretrained()和Qwen2XXX.form_pretrained()的代码写法可能会发生变化,建议统一

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块我先改回model_args

paddlenlp/transformers/auto/tokenizer.py Outdated Show resolved Hide resolved
tests/transformers/llama/test_tokenizer.py Show resolved Hide resolved
@@ -272,7 +272,7 @@ def resolve_file_path(
f"'{log_endpoint}' for available revisions."
)
except EntryNotFoundError:
return None
raise EnvironmentError(f"Does not appear one of the {filenames} in {repo_id}.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个Error类型是不是应该是EntryNotFoundError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块在我修改之前就是这样的(

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

估计是当时就写错了,这个错误可以改

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要raise EntryNotFoundError,那前面就不需要用except捕获EntryNotFoundError了,之前这么做应该有这么做的道理(吧)。

tests/transformers/auto/test_confiugration.py Outdated Show resolved Hide resolved
DrownFish19
DrownFish19 previously approved these changes Oct 15, 2024
Copy link
Collaborator

@DrownFish19 DrownFish19 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

paddlenlp/transformers/auto/configuration.py Show resolved Hide resolved
paddlenlp/transformers/auto/tokenizer.py Show resolved Hide resolved
paddlenlp/transformers/configuration_utils.py Show resolved Hide resolved
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个层级是模型层级吧,为什么要拆开到两个文件夹?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前AutoTokenizer加载Tokenizer的方式是根据模型目录的名称进行匹配,举个例子,之前albertalbert_chinesealbert_english都在albert目录下,但是根据名称进行匹配(TOKENIZER_MAPPING_NAMES表)只允许有一个Tokenizer和一个TokenizerFast,如果不分开会导致albert_chinesealbert_english无法通过AutoTokenizer加载,因为他们三个加载时需要用不同的Tokenizer类。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants