-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add FasterTokenizer on PPMiniLM #1542
Changes from 1 commit
2ca439e
51af868
67a9b82
60b8842
565df2d
7407caa
fc9097a
630451f
398557a
8dfbf58
78d5454
99455df
aed086a
1a4329d
baa4c1f
c95e5d8
f5d0f9d
f82f770
f037168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import os | ||
import sys | ||
import argparse | ||
import distutils.util | ||
|
||
from paddlenlp.transformers import PPMiniLMForSequenceClassification | ||
|
||
sys.path.append("../") | ||
from data import METRIC_CLASSES | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
|
||
# Required parameters | ||
parser.add_argument( | ||
"--task_name", | ||
default=None, | ||
type=str, | ||
required=True, | ||
help="The name of the task to train selected in the list: " + | ||
", ".join(METRIC_CLASSES.keys()), ) | ||
parser.add_argument( | ||
"--output_dir", | ||
default="best_clue_model", | ||
type=str, | ||
help="The output directory where the model predictions and checkpoints will be written.", | ||
) | ||
parser.add_argument( | ||
"--save_inference_model_with_tokenizer", | ||
type=distutils.util.strtobool, | ||
default=True, | ||
help="Whether to save inference model with tokenizer.") | ||
|
||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def do_export(args): | ||
save_path = os.path.join(args.output_dir, "inference") | ||
model = PPMiniLMForSequenceClassification.from_pretrained(args.output_dir) | ||
is_text_pair = True | ||
if args.task_name in ('tnews', 'iflytek', 'cluewsc2020'): | ||
is_text_pair = False | ||
model.to_static( | ||
save_path, | ||
use_faster_tokenizer=args.save_inference_model_with_tokenizer, | ||
is_text_pair=is_text_pair) | ||
|
||
|
||
def print_arguments(args): | ||
"""print arguments""" | ||
print('----------- Configuration Arguments -----------') | ||
for arg, value in sorted(vars(args).items()): | ||
print('%s: %s' % (arg, value)) | ||
print('------------------------------------------------') | ||
|
||
|
||
if __name__ == "__main__": | ||
args = parse_args() | ||
print_arguments(args) | ||
do_export(args) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,25 +173,33 @@ def to_static(self, | |
self, | ||
input_spec=[ | ||
paddle.static.InputSpec( | ||
shape=[None], dtype=core.VarDesc.VarType.STRINGS), | ||
paddle.static.InputSpec( | ||
shape=[None], dtype=core.VarDesc.VarType.STRINGS) | ||
shape=[None], | ||
dtype=core.VarDesc.VarType.STRINGS, | ||
name="text"), paddle.static.InputSpec( | ||
shape=[None], | ||
dtype=core.VarDesc.VarType.STRINGS, | ||
name="text_pair") | ||
]) | ||
else: | ||
model = paddle.jit.to_static( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 基类 FasterPretrainedModel 已经实现了 to_static 函数,此处是否直接调用基类函数即可? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 加入了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已经删除了 |
||
self, | ||
input_spec=[ | ||
paddle.static.InputSpec( | ||
shape=[None], dtype=core.VarDesc.VarType.STRINGS) | ||
shape=[None], | ||
dtype=core.VarDesc.VarType.STRINGS, | ||
name="text") | ||
]) | ||
else: | ||
model = paddle.jit.to_static( | ||
self, | ||
input_spec=[ | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64"), # input_ids | ||
shape=[None, None], dtype="int64", | ||
name="input_ids"), # input_ids | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64") # segment_ids | ||
shape=[None, None], | ||
dtype="int64", | ||
name="token_type_ids") # segment_ids | ||
]) | ||
paddle.jit.save(model, output_path) | ||
logger.info("Already save the static model to the path %s" % | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么 faster 版本的输入 shape 是 [None], 非 Faster 版本的 shape 是 [None, None]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的 to_static 逻辑是放在 FasterPretrainedModel 里还是暴露给 FasterTokenizer 的用户比较合适?@Steffy-zxf
@wawltor @LiuChiachi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dtype=core.VarDesc.VarType.STRINGS
时shape参数无效