-
Notifications
You must be signed in to change notification settings - Fork 6
/
cli_singletask.py
137 lines (116 loc) · 5.92 KB
/
cli_singletask.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors and The HugginFace Inc. team.
#
# 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.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
import argparse
import logging
import random
import numpy as np
import torch
from run_singletask import run
def main():
parser = argparse.ArgumentParser()
## Basic parameters
parser.add_argument("--train_file", default="data", required=True)
parser.add_argument("--dev_file", default="data", required=True)
parser.add_argument("--test_file", default="data", required=False)
parser.add_argument("--dataset", default="nlp_forest_single", required=False)
parser.add_argument("--model", default="facebook/bart-base", required=False)
parser.add_argument("--output_dir", default=None, type=str, required=True)
parser.add_argument("--do_train", action='store_true')
parser.add_argument("--do_predict", action='store_true')
parser.add_argument("--predict_checkpoint", type=str, default="best-model.pt")
## Model parameters
parser.add_argument("--checkpoint", type=str)
parser.add_argument("--do_lowercase", action='store_true', default=False)
parser.add_argument("--freeze_embeds", action='store_true', default=False)
# Preprocessing/decoding-related parameters
parser.add_argument('--max_input_length', type=int, default=512)
parser.add_argument('--max_output_length', type=int, default=64)
parser.add_argument('--num_beams', type=int, default=4)
parser.add_argument("--append_another_bos", action='store_true', default=False)
# Training-related parameters
parser.add_argument("--train_batch_size", default=64, type=int,
help="Batch size per GPU/CPU for training.")
parser.add_argument("--predict_batch_size", default=32, type=int,
help="Batch size per GPU/CPU for evaluation.")
parser.add_argument("--learning_rate", default=3e-5, type=float,
help="The initial learning rate for Adam.")
parser.add_argument("--warmup_proportion", default=0.01, type=float,
help="Weight decay if we apply some.")
parser.add_argument("--weight_decay", default=0.01, type=float,
help="Weight deay if we apply some.")
parser.add_argument("--adam_epsilon", default=1e-8, type=float,
help="Epsilon for Adam optimizer.")
parser.add_argument("--max_grad_norm", default=0.1, type=float,
help="Max gradient norm.")
parser.add_argument("--gradient_accumulation_steps", default=1, type=int,
help="Max gradient norm.")
parser.add_argument("--num_train_epochs", default=1000.0, type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--warmup_steps", default=500, type=int,
help="Linear warmup over warmup_steps.")
parser.add_argument("--total_steps", default=100000, type=int,
help="Linear warmup over warmup_steps.")
parser.add_argument('--wait_step', type=int, default=10000000000)
# Other parameters
parser.add_argument("--quiet", action='store_true',
help="If true, tqdm will not show progress bar")
parser.add_argument('--eval_period', type=int, default=2000,
help="Evaluate & save model")
parser.add_argument('--prefix', type=str, default='',
help="Prefix for saving predictions")
parser.add_argument('--debug', action='store_true',
help="Use a subset of data for debugging")
parser.add_argument('--seed', type=int, default=42,
help="random seed for initialization")
args = parser.parse_args()
if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
print("Output directory () already exists and is not empty.")
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir, exist_ok=True)
##### Start writing logs
log_filename = "{}log.txt".format("" if args.do_train else "eval_")
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO,
handlers=[logging.FileHandler(os.path.join(args.output_dir, log_filename)),
logging.StreamHandler()])
logger = logging.getLogger(__name__)
logger.info(args)
logger.info(args.output_dir)
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
args.n_gpu = torch.cuda.device_count()
if args.n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
if not args.do_train and not args.do_predict:
raise ValueError("At least one of `do_train` or `do_predict` must be True.")
if args.do_train:
if not args.train_file:
raise ValueError("If `do_train` is True, then `train_dir` must be specified.")
if not args.dev_file:
raise ValueError("If `do_train` is True, then `predict_dir` must be specified.")
if args.do_predict:
if not args.test_file:
raise ValueError("If `do_predict` is True, then `predict_dir` must be specified.")
logger.info("Using {} gpus".format(args.n_gpu))
run(args, logger)
if __name__=='__main__':
main()