From 7d6d0085919c9b9416f651891a507a498e50a980 Mon Sep 17 00:00:00 2001 From: Huijuan Wang Date: Tue, 17 Jan 2023 14:21:44 +0800 Subject: [PATCH 1/4] [utc] fix loading local model in taskflow --- applications/zero_shot_text_classification/README.md | 10 ++++++---- paddlenlp/taskflow/zero_shot_text_classification.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/applications/zero_shot_text_classification/README.md b/applications/zero_shot_text_classification/README.md index 38d8fd89b912..d6f09159ed23 100644 --- a/applications/zero_shot_text_classification/README.md +++ b/applications/zero_shot_text_classification/README.md @@ -114,7 +114,8 @@ python run_train.py \ --disable_tqdm True \ --metric_for_best_model macro_f1 \ --load_best_model_at_end True \ - --save_total_limit 1 + --save_total_limit 1 \ + --save_plm ``` 如果在GPU环境中使用,可以指定gpus参数进行多卡训练: @@ -143,7 +144,8 @@ python -u -m paddle.distributed.launch --gpus "0,1" run_train.py \ --disable_tqdm True \ --metric_for_best_model macro_f1 \ --load_best_model_at_end True \ - --save_total_limit 1 + --save_total_limit 1 \ + --save_plm ``` 该示例代码中由于设置了参数 `--do_eval`,因此在训练完会自动进行评估。 @@ -204,7 +206,7 @@ python run_eval.py \ >>> from pprint import pprint >>> from paddlenlp import Taskflow >>> schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议", "疾病表述", "后果表述", "注意事项", "功效作用", "医疗费用", "其他"] ->>> my_cls = Taskflow("zero_shot_text_classification", schema=schema, task_path='./checkpoint/model_best', precision="fp16") +>>> my_cls = Taskflow("zero_shot_text_classification", schema=schema, task_path='./checkpoint/model_best/plm', precision="fp16") >>> pprint(my_cls("中性粒细胞比率偏低")) ``` @@ -221,7 +223,7 @@ from paddlenlp import SimpleServer, Taskflow schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议"] utc = Taskflow("zero_shot_text_classification", schema=schema, - task_path="../../checkpoint/model_best/", + task_path="../../checkpoint/model_best/plm", precision="fp32") app = SimpleServer() app.register_taskflow("taskflow/utc", utc) diff --git a/paddlenlp/taskflow/zero_shot_text_classification.py b/paddlenlp/taskflow/zero_shot_text_classification.py index 605af47a2c9d..f3a7392af633 100644 --- a/paddlenlp/taskflow/zero_shot_text_classification.py +++ b/paddlenlp/taskflow/zero_shot_text_classification.py @@ -105,7 +105,7 @@ def _construct_model(self, model): if self.from_hf_hub: model_instance = UTC.from_pretrained(self._task_path, from_hf_hub=self.from_hf_hub) else: - model_instance = UTC.from_pretrained(model) + model_instance = UTC.from_pretrained(self._task_path) self._model = model_instance self._model.eval() From 7134f4cc63e83cdf409f06d2b7d5029730dfa45b Mon Sep 17 00:00:00 2001 From: Huijuan Wang Date: Tue, 17 Jan 2023 14:25:36 +0800 Subject: [PATCH 2/4] [utc] fix task_path in deployment --- .../deploy/simple_serving/README.md | 6 +++--- .../deploy/simple_serving/server.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/applications/zero_shot_text_classification/deploy/simple_serving/README.md b/applications/zero_shot_text_classification/deploy/simple_serving/README.md index 83602e66dc18..b7b60e4d46f0 100644 --- a/applications/zero_shot_text_classification/deploy/simple_serving/README.md +++ b/applications/zero_shot_text_classification/deploy/simple_serving/README.md @@ -41,15 +41,15 @@ schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就 ```python # Default task_path -utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema) +utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema) ``` #### 多卡服务化预测 PaddleNLP SimpleServing 支持多卡负载均衡预测,主要在服务化注册的时候,注册两个Taskflow的task即可,下面是示例代码 ```python -utc1 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema) -utc2 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema) +utc1 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema) +utc2 = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema) service.register_taskflow("taskflow/utc", [utc1, utc2]) ``` diff --git a/applications/zero_shot_text_classification/deploy/simple_serving/server.py b/applications/zero_shot_text_classification/deploy/simple_serving/server.py index fcffaa763325..c232b2a71e25 100644 --- a/applications/zero_shot_text_classification/deploy/simple_serving/server.py +++ b/applications/zero_shot_text_classification/deploy/simple_serving/server.py @@ -17,7 +17,7 @@ # The schema changed to your defined schema schema = ["病情诊断", "治疗方案", "病因分析", "指标解读", "就医建议", "疾病表述", "后果表述", "注意事项", "功效作用", "医疗费用", "其他"] # The task path changed to your best model path -utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/", schema=schema) +utc = Taskflow("zero_shot_text_classification", task_path="../../checkpoint/model_best/plm", schema=schema) # If you want to define the finetuned utc service app = SimpleServer() app.register_taskflow("taskflow/utc", utc) From afbaaa79d827090a126f4942e4768e7fca848e93 Mon Sep 17 00:00:00 2001 From: Huijuan Wang Date: Tue, 17 Jan 2023 16:17:49 +0800 Subject: [PATCH 3/4] [utc] add resource_urls for default model --- .../taskflow/zero_shot_text_classification.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/paddlenlp/taskflow/zero_shot_text_classification.py b/paddlenlp/taskflow/zero_shot_text_classification.py index f3a7392af633..5991ca8d3fd8 100644 --- a/paddlenlp/taskflow/zero_shot_text_classification.py +++ b/paddlenlp/taskflow/zero_shot_text_classification.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json +import os from typing import Any, Dict, List, Union import numpy as np @@ -21,6 +23,7 @@ from paddlenlp.prompt import PromptDataCollatorWithPadding, UTCTemplate from paddlenlp.transformers import UTC, AutoTokenizer +from ..utils.env import CONFIG_NAME, LEGACY_CONFIG_NAME from .task import Task from .utils import static_mode_guard @@ -54,6 +57,30 @@ class ZeroShotTextClassificationTask(Task): "special_tokens_map": "special_tokens_map.json", "tokenizer_config": "tokenizer_config.json", } + resource_files_urls = { + "utc-large": { + "model_state": [ + "https://bj.bcebos.com/paddlenlp/taskflow/zero_shot_text_classification/utc-large/model_state.pdparams", + "71eb9a732c743a513b84ca048dc4945b", + ], + "config": [ + "https://bj.bcebos.com/paddlenlp/taskflow/zero_shot_text_classification/utc-large/config.json", + "9496be2cc99f7e6adf29280320274142", + ], + "vocab_file": [ + "https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/vocab.txt", + "afc01b5680a53525df5afd7518b42b48", + ], + "special_tokens_map": [ + "https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/special_tokens_map.json", + "2458e2131219fc1f84a6e4843ae07008", + ], + "tokenizer_config": [ + "https://bj.bcebos.com/paddlenlp/taskflow/zero_text_classification/utc-large/tokenizer_config.json", + "dcb0f3257830c0eb1f2de47f2d86f89a", + ], + }, + } def __init__(self, task: str, model: str = "utc-large", schema: list = None, **kwargs): super().__init__(task=task, model=model, **kwargs) @@ -64,6 +91,17 @@ def __init__(self, task: str, model: str = "utc-large", schema: list = None, **k self._pred_threshold = kwargs.get("pred_threshold", 0.5) self._num_workers = kwargs.get("num_workers", 0) + if os.path.exists(os.path.join(self._task_path, LEGACY_CONFIG_NAME)): + if "config" in self.resource_files_names.keys(): + del self.resource_files_names["config"] + with open(os.path.join(self._task_path, LEGACY_CONFIG_NAME)) as f: + self._init_class = json.load(f)["init_class"] + self._check_task_files() + else: + self._check_task_files() + with open(os.path.join(self._task_path, CONFIG_NAME)) as f: + self._init_class = json.load(f)["architectures"].pop() + self._construct_tokenizer() self._check_predictor_type() self._get_inference_model() From 7f51676e5e36f5dacf60c5f5c3f18e73194bb3ec Mon Sep 17 00:00:00 2001 From: Huijuan Wang Date: Tue, 17 Jan 2023 16:55:49 +0800 Subject: [PATCH 4/4] [utc] update taskflow --- .../taskflow/zero_shot_text_classification.py | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/paddlenlp/taskflow/zero_shot_text_classification.py b/paddlenlp/taskflow/zero_shot_text_classification.py index 5991ca8d3fd8..50f346e21313 100644 --- a/paddlenlp/taskflow/zero_shot_text_classification.py +++ b/paddlenlp/taskflow/zero_shot_text_classification.py @@ -13,8 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json -import os from typing import Any, Dict, List, Union import numpy as np @@ -23,7 +21,6 @@ from paddlenlp.prompt import PromptDataCollatorWithPadding, UTCTemplate from paddlenlp.transformers import UTC, AutoTokenizer -from ..utils.env import CONFIG_NAME, LEGACY_CONFIG_NAME from .task import Task from .utils import static_mode_guard @@ -91,17 +88,7 @@ def __init__(self, task: str, model: str = "utc-large", schema: list = None, **k self._pred_threshold = kwargs.get("pred_threshold", 0.5) self._num_workers = kwargs.get("num_workers", 0) - if os.path.exists(os.path.join(self._task_path, LEGACY_CONFIG_NAME)): - if "config" in self.resource_files_names.keys(): - del self.resource_files_names["config"] - with open(os.path.join(self._task_path, LEGACY_CONFIG_NAME)) as f: - self._init_class = json.load(f)["init_class"] - self._check_task_files() - else: - self._check_task_files() - with open(os.path.join(self._task_path, CONFIG_NAME)) as f: - self._init_class = json.load(f)["architectures"].pop() - + self._check_task_files() self._construct_tokenizer() self._check_predictor_type() self._get_inference_model() @@ -140,10 +127,7 @@ def _construct_model(self, model): """ Construct the inference model for the predictor. """ - if self.from_hf_hub: - model_instance = UTC.from_pretrained(self._task_path, from_hf_hub=self.from_hf_hub) - else: - model_instance = UTC.from_pretrained(self._task_path) + model_instance = UTC.from_pretrained(self._task_path, from_hf_hub=self.from_hf_hub) self._model = model_instance self._model.eval()