-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 PP-MiniLM #1403
Merged
Merged
Add PP-MiniLM #1403
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ca13671
# This is a combination of 2 commits.
LiuChiachi 8d0af8b
solve conflicts
LiuChiachi 63b108c
update readme
LiuChiachi d644175
Merge branch 'develop' of https://github.com/PaddlePaddle/PaddleNLP i…
LiuChiachi af7a461
update readme
LiuChiachi b82ab76
delete useless char
LiuChiachi 581625c
update reamde
LiuChiachi 90df17e
update reamde
LiuChiachi d98af38
update readme, add general readme, remove 'ofa'
LiuChiachi 1ccb536
remove infe_perf
LiuChiachi 7e4b169
Update README
20fc343
Update README
0055c61
remove blank space between Chinese characters and numbers
LiuChiachi 97c028a
solve conflicts
LiuChiachi 75346d1
add blank space
LiuChiachi 9b67b42
update data in readme
LiuChiachi cd7ce83
add blank space
LiuChiachi e900e03
update readme
LiuChiachi 067dd3a
Merge branch 'develop' of https://github.com/PaddlePaddle/PaddleNLP i…
LiuChiachi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 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 numpy as np | ||
|
||
from paddle.metric import Metric, Accuracy | ||
from paddlenlp.transformers import ErnieForSequenceClassification, ErnieTokenizer | ||
from paddlenlp.transformers import BertForSequenceClassification, BertTokenizer | ||
|
||
MODEL_CLASSES = { | ||
"ernie": (ErnieForSequenceClassification, ErnieTokenizer), | ||
"bert": (BertForSequenceClassification, BertTokenizer) | ||
} | ||
|
||
METRIC_CLASSES = { | ||
"afqmc": Accuracy, | ||
"tnews": Accuracy, | ||
"iflytek": Accuracy, | ||
"ocnli": Accuracy, | ||
"cmnli": Accuracy, | ||
"cluewsc2020": Accuracy, | ||
"csl": Accuracy, | ||
} | ||
|
||
|
||
def convert_example(example, | ||
tokenizer, | ||
label_list, | ||
max_seq_length=512, | ||
is_test=False): | ||
"""convert a glue example into necessary features""" | ||
if not is_test: | ||
# `label_list == None` is for regression task | ||
label_dtype = "int64" if label_list else "float32" | ||
# Get the label | ||
label = example['label'] | ||
label = np.array([label], dtype=label_dtype) | ||
# Convert raw text to feature | ||
if 'sentence' in example: | ||
example = tokenizer(example['sentence'], max_seq_len=max_seq_length) | ||
elif 'sentence1' in example: | ||
example = tokenizer( | ||
example['sentence1'], | ||
text_pair=example['sentence2'], | ||
max_seq_len=max_seq_length) | ||
elif 'keyword' in example: # CSL | ||
sentence1 = " ".join(example['keyword']) | ||
example = tokenizer( | ||
sentence1, text_pair=example['abst'], max_seq_len=max_seq_length) | ||
elif 'target' in example: # wsc | ||
text, query, pronoun, query_idx, pronoun_idx = example['text'], example[ | ||
'target']['span1_text'], example['target']['span2_text'], example[ | ||
'target']['span1_index'], example['target']['span2_index'] | ||
text_list = list(text) | ||
assert text[pronoun_idx:(pronoun_idx + len(pronoun) | ||
)] == pronoun, "pronoun: {}".format(pronoun) | ||
assert text[query_idx:(query_idx + len(query) | ||
)] == query, "query: {}".format(query) | ||
if pronoun_idx > query_idx: | ||
text_list.insert(query_idx, "_") | ||
text_list.insert(query_idx + len(query) + 1, "_") | ||
text_list.insert(pronoun_idx + 2, "[") | ||
text_list.insert(pronoun_idx + len(pronoun) + 2 + 1, "]") | ||
else: | ||
text_list.insert(pronoun_idx, "[") | ||
text_list.insert(pronoun_idx + len(pronoun) + 1, "]") | ||
text_list.insert(query_idx + 2, "_") | ||
text_list.insert(query_idx + len(query) + 2 + 1, "_") | ||
text = "".join(text_list) | ||
example = tokenizer(text, max_seq_len=max_seq_length) | ||
|
||
if not is_test: | ||
return example['input_ids'], example['token_type_ids'], label | ||
else: | ||
return example['input_ids'], example['token_type_ids'] |
78 changes: 78 additions & 0 deletions
78
examples/model_compression/PP-MiniLM/finetuning/export_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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 argparse | ||
import os | ||
|
||
import paddle | ||
|
||
from run_clue import MODEL_CLASSES | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
|
||
# Required parameters | ||
parser.add_argument( | ||
"--model_type", | ||
default=None, | ||
type=str, | ||
required=True, | ||
help="Model type selected in the list: " + | ||
", ".join(MODEL_CLASSES.keys()), ) | ||
parser.add_argument( | ||
"--model_path", | ||
default=None, | ||
type=str, | ||
required=True, | ||
help="Path of the trained model to be exported.", ) | ||
parser.add_argument( | ||
"--output_path", | ||
default=None, | ||
type=str, | ||
required=True, | ||
help="The output file prefix used to save the exported inference model.", | ||
) | ||
args = parser.parse_args() | ||
return args | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
args.model_type = args.model_type.lower() | ||
model_class, tokenizer_class = MODEL_CLASSES[args.model_type] | ||
|
||
# build model and load trained parameters | ||
model = model_class.from_pretrained(args.model_path) | ||
# switch to eval model | ||
model.eval() | ||
# convert to static graph with specific input description | ||
model = paddle.jit.to_static( | ||
model, | ||
input_spec=[ | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64"), # input_ids | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64") # segment_ids | ||
]) | ||
# save converted static graph model | ||
paddle.jit.save(model, args.output_path) | ||
# also save tokenizer for inference usage | ||
tokenizer = tokenizer_class.from_pretrained(args.model_path) | ||
tokenizer.save_pretrained(os.path.dirname(args.output_path)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
35 changes: 35 additions & 0 deletions
35
examples/model_compression/PP-MiniLM/finetuning/run_all_search.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# $1 means GENERAL_DIR | ||
|
||
# The penultimate parameter is the card id, this script can be changed if necessary | ||
bash run_one_search.sh $1 afqmc 0 & | ||
bash run_one_search.sh $1 tnews 1 & | ||
bash run_one_search.sh $1 ifly 2 & | ||
bash run_one_search.sh $1 ocnli 3 & | ||
bash run_one_search.sh $1 csl 4 & | ||
bash run_one_search.sh $1 wsc 5 & | ||
|
||
# Because the CMNLI data set is significantly larger than other data sets, | ||
# it needs to be placed on different cards. | ||
lr=1e-4 | ||
bs=16 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 0 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=32 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 1 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=64 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 2 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
|
||
lr=5e-5 | ||
bs=16 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 3 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=32 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 4 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=64 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 5 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
|
||
lr=3e-5 | ||
bs=16 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 6 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=32 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 5 $1 > $1/cmnli/${lr}_${bs}_3_128.log & | ||
bs=64 | ||
sh run_clue.sh CMNLI $lr $bs 3 128 7 $1 > $1/cmnli/${lr}_${bs}_3_128.log & |
Oops, something went wrong.
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.
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.
删除空行