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

Merged
merged 28 commits into from
Oct 30, 2024

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. 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 66.66667% with 156 lines in your changes missing coverage. Please review.

Project coverage is 52.80%. Comparing base (78f911a) to head (5579695).
Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
paddlenlp/transformers/auto/factory.py 46.06% 48 Missing ⚠️
paddlenlp/utils/import_utils.py 53.52% 33 Missing ⚠️
paddlenlp/transformers/auto/tokenizer.py 76.47% 24 Missing ⚠️
paddlenlp/transformers/auto/configuration.py 72.30% 18 Missing ⚠️
paddlenlp/transformers/convert_slow_tokenizer.py 76.11% 16 Missing ⚠️
paddlenlp/transformers/llama/tokenizer.py 41.17% 10 Missing ⚠️
paddlenlp/transformers/tokenizer_utils_base.py 84.84% 5 Missing ⚠️
paddlenlp/transformers/configuration_utils.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9215      +/-   ##
===========================================
- Coverage    53.19%   52.80%   -0.40%     
===========================================
  Files          673      673              
  Lines       108855   107657    -1198     
===========================================
- Hits         57909    56849    -1060     
+ Misses       50946    50808     -138     

☔ 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类。

@lvdongyi lvdongyi force-pushed the dev-20240927-support-tiktoken branch from e84a062 to a422932 Compare October 18, 2024 01:12
tokenizer.Load(self.vocab_file)
return tokenizer

with open(self.vocab_file, "rb") as f:
Copy link
Collaborator

Choose a reason for hiding this comment

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

现在默认全面切换到faster了?

Copy link
Collaborator

Choose a reason for hiding this comment

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

应该是没有的,只有当use_fast=True时才会使用tokenizer_fast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

现在默认全面切换到faster了?

已经修改了默认值,默认使用以前的Load方式

ZHUI
ZHUI previously approved these changes Oct 21, 2024
Copy link
Collaborator

@ZHUI ZHUI left a comment

Choose a reason for hiding this comment

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

LGTM

@lvdongyi lvdongyi force-pushed the dev-20240927-support-tiktoken branch 2 times, most recently from 376b3ad to fe19531 Compare October 21, 2024 13:37
@lvdongyi lvdongyi force-pushed the dev-20240927-support-tiktoken branch from fe19531 to 3412f50 Compare October 22, 2024 07:57
@ZHUI
Copy link
Collaborator

ZHUI commented Oct 23, 2024

需要合入的话,可以 @ 我

@lvdongyi
Copy link
Contributor Author

需要合入的话,可以 @ 我

目前不知道什么原因,PaddleNLP-CI会卡在running P0case 2/4: albert

@ZHUI
Copy link
Collaborator

ZHUI commented Oct 23, 2024

好的,等一会儿 CI 吧,有个 Conflicting 可以处理一下

@lvdongyi lvdongyi force-pushed the dev-20240927-support-tiktoken branch from 2b3fb62 to 2ceeccb Compare October 23, 2024 09:44
@lvdongyi lvdongyi force-pushed the dev-20240927-support-tiktoken branch from 2ceeccb to 19521f9 Compare October 23, 2024 09:46
@lvdongyi
Copy link
Contributor Author

好的,等一会儿 CI 吧,有个 Conflicting 可以处理一下

处理了

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