Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82a303f

Browse files
authored
[utc] fix loading local model in taskflow (PaddlePaddle#4505)
1 parent b1b8908 commit 82a303f

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

โ€Žapplications/zero_shot_text_classification/README.mdโ€Ž

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ python run_train.py \
116116
--disable_tqdm True \
117117
--metric_for_best_model macro_f1 \
118118
--load_best_model_at_end True \
119-
--save_total_limit 1
119+
--save_total_limit 1 \
120+
--save_plm
120121
```
121122

122123
ๅฆ‚ๆžœๅœจGPU็Žฏๅขƒไธญไฝฟ็”จ๏ผŒๅฏไปฅๆŒ‡ๅฎšgpusๅ‚ๆ•ฐ่ฟ›่กŒๅคšๅก่ฎญ็ปƒ๏ผš
@@ -145,7 +146,8 @@ python -u -m paddle.distributed.launch --gpus "0,1" run_train.py \
145146
--disable_tqdm True \
146147
--metric_for_best_model macro_f1 \
147148
--load_best_model_at_end True \
148-
--save_total_limit 1
149+
--save_total_limit 1 \
150+
--save_plm
149151
```
150152

151153
่ฏฅ็คบไพ‹ไปฃ็ ไธญ็”ฑไบŽ่ฎพ็ฝฎไบ†ๅ‚ๆ•ฐ `--do_eval`๏ผŒๅ› ๆญคๅœจ่ฎญ็ปƒๅฎŒไผš่‡ชๅŠจ่ฟ›่กŒ่ฏ„ไผฐใ€‚
@@ -208,7 +210,7 @@ python run_eval.py \
208210
>>> from pprint import pprint
209211
>>> from paddlenlp import Taskflow
210212
>>> schema = ["็—…ๆƒ…่ฏŠๆ–ญ", "ๆฒป็–—ๆ–นๆกˆ", "็—…ๅ› ๅˆ†ๆž", "ๆŒ‡ๆ ‡่งฃ่ฏป", "ๅฐฑๅŒปๅปบ่ฎฎ", "็–พ็—…่กจ่ฟฐ", "ๅŽๆžœ่กจ่ฟฐ", "ๆณจๆ„ไบ‹้กน", "ๅŠŸๆ•ˆไฝœ็”จ", "ๅŒป็–—่ดน็”จ", "ๅ…ถไป–"]
211-
>>> my_cls = Taskflow("zero_shot_text_classification", schema=schema, task_path='./checkpoint/model_best', precision="fp16")
213+
>>> my_cls = Taskflow("zero_shot_text_classification", schema=schema, task_path='./checkpoint/model_best/plm', precision="fp16")
212214
>>> pprint(my_cls("ไธญๆ€ง็ฒ’็ป†่ƒžๆฏ”็އๅไฝŽ"))
213215
```
214216

@@ -225,7 +227,7 @@ from paddlenlp import SimpleServer, Taskflow
225227
schema = ["็—…ๆƒ…่ฏŠๆ–ญ", "ๆฒป็–—ๆ–นๆกˆ", "็—…ๅ› ๅˆ†ๆž", "ๆŒ‡ๆ ‡่งฃ่ฏป", "ๅฐฑๅŒปๅปบ่ฎฎ"]
226228
utc = Taskflow("zero_shot_text_classification",
227229
schema=schema,
228-
task_path="../../checkpoint/model_best/",
230+
task_path="../../checkpoint/model_best/plm",
229231
precision="fp32")
230232
app = SimpleServer()
231233
app.register_taskflow("taskflow/utc", utc)

โ€Žapplications/zero_shot_text_classification/deploy/simple_serving/README.mdโ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ schema = ["็—…ๆƒ…่ฏŠๆ–ญ", "ๆฒป็–—ๆ–นๆกˆ", "็—…ๅ› ๅˆ†ๆž", "ๆŒ‡ๆ ‡่งฃ่ฏป", "ๅฐฑ
4141

4242
```python
4343
# Default task_path
44-
utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema)
44+
utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema)
4545
```
4646

4747
#### ๅคšๅกๆœๅŠกๅŒ–้ข„ๆต‹
4848
PaddleNLP SimpleServing ๆ”ฏๆŒๅคšๅก่ดŸ่ฝฝๅ‡่กก้ข„ๆต‹๏ผŒไธป่ฆๅœจๆœๅŠกๅŒ–ๆณจๅ†Œ็š„ๆ—ถๅ€™๏ผŒๆณจๅ†ŒไธคไธชTaskflow็š„taskๅณๅฏ๏ผŒไธ‹้ขๆ˜ฏ็คบไพ‹ไปฃ็ 
4949

5050
```python
51-
utc1 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema)
52-
utc2 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema)
51+
utc1 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema)
52+
utc2 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema)
5353
service.register_taskflow("taskflow/utc", [utc1, utc2])
5454
```
5555

โ€Žapplications/zero_shot_text_classification/deploy/simple_serving/server.pyโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
# The schema changed to your defined schema
1818
schema = ["็—…ๆƒ…่ฏŠๆ–ญ", "ๆฒป็–—ๆ–นๆกˆ", "็—…ๅ› ๅˆ†ๆž", "ๆŒ‡ๆ ‡่งฃ่ฏป", "ๅฐฑๅŒปๅปบ่ฎฎ", "็–พ็—…่กจ่ฟฐ", "ๅŽๆžœ่กจ่ฟฐ", "ๆณจๆ„ไบ‹้กน", "ๅŠŸๆ•ˆไฝœ็”จ", "ๅŒป็–—่ดน็”จ", "ๅ…ถไป–"]
1919
# The task path changed to your best model path
20-
utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema)
20+
utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema)
2121
# If you want to define the finetuned utc service
2222
app = SimpleServer()
2323
app.register_taskflow("taskflow/utc", utc)

โ€Žpaddlenlp/taskflow/zero_shot_text_classification.pyโ€Ž

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,30 @@ class ZeroShotTextClassificationTask(Task):
5454
"special_tokens_map": "special_tokens_map.json",
5555
"tokenizer_config": "tokenizer_config.json",
5656
}
57+
resource_files_urls = {
58+
"utc-large": {
59+
"model_state": [
60+
"https://bj.bcebos.com/paddlenlp/taskflow/zero_shot_text_classification/utc-large/model_state.pdparams",
61+
"71eb9a732c743a513b84ca048dc4945b",
62+
],
63+
"config": [
64+
"https://bj.bcebos.com/paddlenlp/taskflow/zero_shot_text_classification/utc-large/config.json",
65+
"9496be2cc99f7e6adf29280320274142",
66+
],
67+
"vocab_file": [
68+
"https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/vocab.txt",
69+
"afc01b5680a53525df5afd7518b42b48",
70+
],
71+
"special_tokens_map": [
72+
"https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/special_tokens_map.json",
73+
"2458e2131219fc1f84a6e4843ae07008",
74+
],
75+
"tokenizer_config": [
76+
"https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/tokenizer_config.json",
77+
"dcb0f3257830c0eb1f2de47f2d86f89a",
78+
],
79+
},
80+
}
5781

5882
def __init__(self, task: str, model: str = "utc-large", schema: list = None, **kwargs):
5983
super().__init__(task=task, model=model, **kwargs)
@@ -64,6 +88,7 @@ def __init__(self, task: str, model: str = "utc-large", schema: list = None, **k
6488
self._pred_threshold = kwargs.get("pred_threshold", 0.5)
6589
self._num_workers = kwargs.get("num_workers", 0)
6690

91+
self._check_task_files()
6792
self._construct_tokenizer()
6893
self._check_predictor_type()
6994
self._get_inference_model()
@@ -102,10 +127,7 @@ def _construct_model(self, model):
102127
"""
103128
Construct the inference model for the predictor.
104129
"""
105-
if self.from_hf_hub:
106-
model_instance = UTC.from_pretrained(self._task_path, from_hf_hub=self.from_hf_hub)
107-
else:
108-
model_instance = UTC.from_pretrained(model)
130+
model_instance = UTC.from_pretrained(self._task_path, from_hf_hub=self.from_hf_hub)
109131
self._model = model_instance
110132
self._model.eval()
111133

0 commit comments

Comments
ย (0)