1
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
15
from __future__ import annotations
19
from unittest import TestCase
21
from paddlenlp.utils.downloader import get_path_from_url_with_filelock
22
from paddlenlp.utils.log import logger
23
from tests.testing_utils import argv_context_guard, load_test_config
26
class BERT_Test(TestCase):
27
def download_corpus(self, input_dir):
28
os.makedirs(input_dir, exist_ok=True)
30
"https://bj.bcebos.com/paddlenlp/models/transformers/bert/data/training_data.hdf5",
34
file_name = file.split("/")[-1]
35
file_path = os.path.join(input_dir, file_name)
36
if not os.path.exists(file_path):
37
logger.info(f"start to download corpus: <{file_name}> into <{input_dir}>")
38
get_path_from_url_with_filelock(file, root_dir=input_dir)
40
def setUp(self) -> None:
41
self.path = "./model_zoo/bert"
42
self.config_path = "./tests/fixtures/model_zoo/bert.yaml"
43
sys.path.insert(0, self.path)
45
def tearDown(self) -> None:
46
sys.path.remove(self.path)
48
def test_pretrain(self):
51
pretrain_config = load_test_config(self.config_path, "pretrain")
52
self.download_corpus(pretrain_config["input_dir"])
53
with argv_context_guard(pretrain_config):
54
from run_pretrain_trainer import do_train
60
"model_type": pretrain_config["model_type"],
61
"model_path": pretrain_config["output_dir"],
62
"output_path": "infer_model/model",
64
with argv_context_guard(export_config):
65
from export_model import main
69
# 3. infer model of glue
70
glue_config = load_test_config(self.config_path, "glue")
72
"model_type": export_config["model_type"],
73
"model_path": export_config["output_path"],
74
"task_name": glue_config["task_name"],
76
with argv_context_guard(infer_config):
77
from predict_glue import main
81
# infer model of samples
83
"model_path": export_config["output_path"],
84
"device": pretrain_config["device"],
86
with argv_context_guard(infer_config):
87
from predict import main
93
glue_config = load_test_config(self.config_path, "glue")
94
with argv_context_guard(glue_config):
95
from run_glue_trainer import do_train