test_xla_examples.py 2.79 KB
Newer Older
Lysandre Debut's avatar
Lysandre Debut committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# coding=utf-8
# Copyright 2018 HuggingFace Inc..
#
# 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.


17
import json
Lysandre Debut's avatar
Lysandre Debut committed
18
import logging
19
import os
Lysandre Debut's avatar
Lysandre Debut committed
20
21
22
23
import sys
from time import time
from unittest.mock import patch

24
from transformers.testing_utils import TestCasePlus, require_torch_tpu
Lysandre Debut's avatar
Lysandre Debut committed
25
26
27
28
29
30
31


logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger()


32
33
34
35
36
37
38
39
40
41
42
def get_results(output_dir):
    results = {}
    path = os.path.join(output_dir, "all_results.json")
    if os.path.exists(path):
        with open(path, "r") as f:
            results = json.load(f)
    else:
        raise ValueError(f"can't find {path}")
    return results


Lysandre Debut's avatar
Lysandre Debut committed
43
@require_torch_tpu
44
class TorchXLAExamplesTests(TestCasePlus):
Lysandre Debut's avatar
Lysandre Debut committed
45
46
47
48
49
50
    def test_run_glue(self):
        import xla_spawn

        stream_handler = logging.StreamHandler(sys.stdout)
        logger.addHandler(stream_handler)

51
        tmp_dir = self.get_auto_remove_tmp_dir()
Lysandre Debut's avatar
Lysandre Debut committed
52
        testargs = f"""
53
            ./examples/pytorch/text-classification/run_glue.py
Lysandre Debut's avatar
Lysandre Debut committed
54
            --num_cores=8
55
56
57
58
59
60
            ./examples/pytorch/text-classification/run_glue.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
Lysandre Debut's avatar
Lysandre Debut committed
61
62
            --do_train
            --do_eval
63
64
65
66
67
68
69
            --debug tpu_metrics_debug
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
Lysandre Debut's avatar
Lysandre Debut committed
70
71
            --max_seq_length=128
            """.split()
72

Lysandre Debut's avatar
Lysandre Debut committed
73
74
75
76
77
        with patch.object(sys, "argv", testargs):
            start = time()
            xla_spawn.main()
            end = time()

78
79
            result = get_results(tmp_dir)
            self.assertGreaterEqual(result["eval_accuracy"], 0.75)
Lysandre Debut's avatar
Lysandre Debut committed
80

81
            # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
Lysandre's avatar
Lysandre committed
82
            self.assertLess(end - start, 500)
83
84
85
86
87

    def test_trainer_tpu(self):
        import xla_spawn

        testargs = """
88
            ./tests/test_trainer_tpu.py
89
            --num_cores=8
90
            ./tests/test_trainer_tpu.py
91
92
93
            """.split()
        with patch.object(sys, "argv", testargs):
            xla_spawn.main()