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

Skip to content

Commit e5a4503

Browse files
authored
Unify the launch way of lexical analysis (PaddlePaddle#234)
* lac and waybill use int64 for windows compatibility * replace spawn * optimize readme * use --device gpu
1 parent 46f64af commit e5a4503

16 files changed

Lines changed: 111 additions & 77 deletions

File tree

examples/benchmark/glue/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ python -u ./run_glue.py \
2525
--logging_steps 1 \
2626
--save_steps 100 \
2727
--output_dir ./tmp/$TASK_NAME/ \
28-
--device "gpu"
28+
--device gpu
2929

3030
```
3131

@@ -45,7 +45,7 @@ python -m paddle.distributed.launch --gpus "0,1" run_glue.py \
4545
--logging_steps 1 \
4646
--save_steps 100 \
4747
--output_dir ./tmp/$TASK_NAME/ \
48-
--device "gpu"
48+
--device gpu
4949

5050
```
5151
其中参数释义如下:

examples/information_extraction/msra_ner/README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ PaddleNLP集成的数据集MSRA-NER数据集对文件格式做了调整:每一
1515

1616
### 模型训练
1717

18+
#### 单卡训练
19+
1820
```shell
1921
export CUDA_VISIBLE_DEVICES=0
2022

@@ -27,7 +29,7 @@ python -u ./train.py \
2729
--logging_steps 1 \
2830
--save_steps 500 \
2931
--output_dir ./tmp/msra_ner/ \
30-
--n_gpu 1
32+
--device gpu
3133
```
3234

3335
其中参数释义如下:
@@ -39,7 +41,22 @@ python -u ./train.py \
3941
- `logging_steps`: 表示日志打印间隔。
4042
- `save_steps`: 表示模型保存及评估间隔。
4143
- `output_dir`: 表示模型保存路径。
42-
- `n_gpu`: 表示使用的 GPU 卡数。若希望使用多卡训练,将其设置为指定数目即可;若为0,则使用CPU。
44+
- `device`: 训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU。
45+
46+
#### 多卡训练
47+
```shell
48+
python -m paddle.distributed.launch --gpus "0,1" ./train.py \
49+
--model_name_or_path bert-base-multilingual-uncased \
50+
--max_seq_length 128 \
51+
--batch_size 32 \
52+
--learning_rate 2e-5 \
53+
--num_train_epochs 3 \
54+
--logging_steps 1 \
55+
--save_steps 500 \
56+
--output_dir ./tmp/msra_ner/ \
57+
--device gpu
58+
```
59+
4360

4461
训练过程将按照 `logging_steps``save_steps` 的设置打印如下日志:
4562

@@ -68,7 +85,7 @@ python -u ./eval.py \
6885
--model_name_or_path bert-base-multilingual-uncased \
6986
--max_seq_length 128 \
7087
--batch_size 32 \
71-
--use_gpu True \
88+
--device gpu \
7289
--init_checkpoint_path tmp/msra_ner/model_500.pdparams
7390
```
7491

@@ -88,7 +105,7 @@ python -u ./predict.py \
88105
--model_name_or_path bert-base-multilingual-uncased \
89106
--max_seq_length 128 \
90107
--batch_size 32 \
91-
--use_gpu True \
108+
--device gpu \
92109
--init_checkpoint_path tmp/msra_ner/model_500.pdparams
93110
```
94111

examples/information_extraction/msra_ner/eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
parser.add_argument("--init_checkpoint_path", default=None, type=str, required=True, help="The model checkpoint path.", )
3838
parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.", )
3939
parser.add_argument("--batch_size", default=8, type=int, help="Batch size per GPU/CPU for training.", )
40-
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="If set, use GPU for training.")
40+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
4141
# yapf: enable
4242

4343

@@ -62,7 +62,7 @@ def tokenize_and_align_labels(example, tokenizer, no_entity_id,
6262

6363

6464
def do_eval(args):
65-
paddle.set_device("gpu" if args.use_gpu else "cpu")
65+
paddle.set_device(args.device)
6666

6767
# Create dataset, tokenizer and dataloader.
6868
train_ds, eval_ds = load_dataset(

examples/information_extraction/msra_ner/predict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
parser.add_argument("--init_checkpoint_path", default=None, type=str, required=True, help="The model checkpoint path.", )
3737
parser.add_argument("--max_seq_length", default=128, type=int, help="The maximum total input sequence length after tokenization. Sequences longer " "than this will be truncated, sequences shorter will be padded.", )
3838
parser.add_argument("--batch_size", default=8, type=int, help="Batch size per GPU/CPU for training.", )
39-
parser.add_argument("--use_gpu", type=ast.literal_eval, default=True, help="If set, use GPU for training.")
39+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
4040
# yapf: enable
4141

4242

@@ -90,7 +90,7 @@ def parse_decodes(input_words, id2label, decodes, lens):
9090

9191

9292
def do_predict(args):
93-
paddle.set_device("gpu" if args.use_gpu else "cpu")
93+
paddle.set_device(args.device)
9494

9595
# Create dataset, tokenizer and dataloader.
9696
train_ds, predict_ds = load_dataset(

examples/information_extraction/msra_ner/train.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
parser.add_argument("--logging_steps", type=int, default=1, help="Log every X updates steps.")
4848
parser.add_argument("--save_steps", type=int, default=100, help="Save checkpoint every X updates steps.")
4949
parser.add_argument("--seed", type=int, default=42, help="random seed for initialization")
50-
parser.add_argument("--n_gpu", type=int, default=1, help="number of gpus to use, 0 for cpu.")
50+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
5151
# yapf: enable
5252

5353

@@ -91,7 +91,7 @@ def tokenize_and_align_labels(example, tokenizer, no_entity_id,
9191

9292

9393
def do_train(args):
94-
paddle.set_device("gpu" if args.n_gpu else "cpu")
94+
paddle.set_device(args.device)
9595
if paddle.distributed.get_world_size() > 1:
9696
paddle.distributed.init_parallel_env()
9797

@@ -191,7 +191,7 @@ def do_train(args):
191191
lr_scheduler.step()
192192
optimizer.clear_grad()
193193
if global_step % args.save_steps == 0 or global_step == last_step:
194-
if (not args.n_gpu > 1) or paddle.distributed.get_rank() == 0:
194+
if paddle.distributed.get_rank() == 0:
195195
evaluate(model, loss_fct, metric, test_data_loader,
196196
label_num)
197197
paddle.save(model.state_dict(),
@@ -201,7 +201,4 @@ def do_train(args):
201201

202202
if __name__ == "__main__":
203203
args = parser.parse_args()
204-
if args.n_gpu > 1:
205-
paddle.distributed.spawn(do_train, args=(args, ), nprocs=args.n_gpu)
206-
else:
207-
do_train(args)
204+
do_train(args)

examples/information_extraction/waybill_ie/run_bigru_crf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ def convert_example(example):
134134
test_ds.map(convert_example)
135135

136136
batchify_fn = lambda samples, fn=Tuple(
137-
Pad(axis=0, pad_val=word_vocab.get('OOV')), # token_ids
138-
Stack(), # seq_len
139-
Pad(axis=0, pad_val=label_vocab.get('O')) # label_ids
137+
Pad(axis=0, pad_val=word_vocab.get('OOV'), dtype='int64'), # token_ids
138+
Stack(dtype='int64'), # seq_len
139+
Pad(axis=0, pad_val=label_vocab.get('O'), dtype='int64') # label_ids
140140
): fn(samples)
141141

142142
train_loader = paddle.io.DataLoader(

examples/lexical_analysis/README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,42 @@ python download.py --data_dir ./
4242

4343
### 模型训练
4444

45-
模型训练支持 CPU 和 GPU,使用 GPU 之前应指定使用的显卡卡号:
45+
#### 单卡训练
46+
47+
启动方式如下:
4648

4749
```bash
48-
export CUDA_VISIBLE_DEVICES=0 # 支持多卡训练,如使用双卡,可以设置为0,1
50+
python train.py \
51+
--data_dir ./lexical_analysis_dataset_tiny \
52+
--model_save_dir ./save_dir \
53+
--epochs 10 \
54+
--batch_size 32 \
55+
--device gpu \
56+
# --init_checkpoint ./save_dir/final
4957
```
5058

51-
训练启动方式如下:
59+
其中参数释义如下:
60+
- `data_dir`: 数据集所在文件夹路径.
61+
- `model_save_dir`: 训练期间模型保存路径。
62+
- `epochs`: 模型训练迭代轮数。
63+
- `batch_size`: 表示每次迭代**每张卡**上的样本数目。
64+
- `device`: 训练使用的设备, 'gpu'表示使用GPU, 'xpu'表示使用百度昆仑卡, 'cpu'表示使用CPU。
65+
- `init_checkpoint`: 模型加载路径,通过设置init_checkpoint可以启动增量训练。
66+
67+
#### 多卡训练
68+
69+
启动方式如下:
5270

5371
```bash
54-
python train.py \
72+
python -m paddle.distributed.launch --gpus "0,1" train.py \
5573
--data_dir ./lexical_analysis_dataset_tiny \
5674
--model_save_dir ./save_dir \
5775
--epochs 10 \
5876
--batch_size 32 \
59-
--n_gpu 1 \
77+
--device gpu \
6078
# --init_checkpoint ./save_dir/final
6179
```
6280

63-
其中 data_dir 是数据集所在文件夹路径,init_checkpoint 是模型加载路径,通过设置init_checkpoint可以启动增量训练。
64-
6581
### 模型评估
6682

6783
通过加载训练保存的模型,可以对测试集数据进行验证,启动方式如下:
@@ -70,7 +86,7 @@ python train.py \
7086
python eval.py --data_dir ./lexical_analysis_dataset_tiny \
7187
--init_checkpoint ./save_dir/final \
7288
--batch_size 32 \
73-
--use_gpu
89+
--device gpu
7490
```
7591

7692
### 模型预测
@@ -81,7 +97,7 @@ python eval.py --data_dir ./lexical_analysis_dataset_tiny \
8197
python predict.py --data_dir ./lexical_analysis_dataset_tiny \
8298
--init_checkpoint ./save_dir/final \
8399
--batch_size 32 \
84-
--use_gpu
100+
--device gpu
85101
```
86102

87103
得到类似以下输出:

examples/lexical_analysis/eval.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,22 @@
3232
parser.add_argument("--init_checkpoint", type=str, default=None, help="Path to init model.")
3333
parser.add_argument("--batch_size", type=int, default=300, help="The number of sequences contained in a mini-batch.")
3434
parser.add_argument("--max_seq_len", type=int, default=64, help="Number of words of the longest seqence.")
35-
parser.add_argument('--use_gpu', action='store_true', help='If set, use gpu to excute')
35+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
3636
parser.add_argument("--emb_dim", type=int, default=128, help="The dimension in which a word is embedded.")
3737
parser.add_argument("--hidden_size", type=int, default=128, help="The number of hidden nodes in the GRU layer.")
3838
args = parser.parse_args()
3939
# yapf: enable
4040

4141

4242
def evaluate(args):
43-
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
44-
paddle.set_device("gpu" if args.use_gpu else "cpu")
43+
paddle.set_device(args.device)
4544

4645
# create dataset.
4746
test_dataset = LacDataset(args.data_dir, mode='test')
4847
batchify_fn = lambda samples, fn=Tuple(
49-
Pad(axis=0, pad_val=0), # word_ids
50-
Stack(), # length
51-
Pad(axis=0, pad_val=0), # label_ids
48+
Pad(axis=0, pad_val=0, dtype='int64'), # word_ids
49+
Stack(dtype='int64'), # length
50+
Pad(axis=0, pad_val=0, dtype='int64'), # label_ids
5251
): fn(samples)
5352

5453
# Create sampler for dataloader
@@ -60,7 +59,6 @@ def evaluate(args):
6059
test_loader = paddle.io.DataLoader(
6160
dataset=test_dataset,
6261
batch_sampler=test_sampler,
63-
places=place,
6462
return_list=True,
6563
collate_fn=batchify_fn)
6664

examples/lexical_analysis/predict.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,22 @@
3131
parser.add_argument("--init_checkpoint", type=str, default=None, help="Path to init model.")
3232
parser.add_argument("--batch_size", type=int, default=300, help="The number of sequences contained in a mini-batch.")
3333
parser.add_argument("--max_seq_len", type=int, default=64, help="Number of words of the longest seqence.")
34-
parser.add_argument('--use_gpu', action='store_true', help='If set, use gpu to excute')
34+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
3535
parser.add_argument("--emb_dim", type=int, default=128, help="The dimension in which a word is embedded.")
3636
parser.add_argument("--hidden_size", type=int, default=128, help="The number of hidden nodes in the GRU layer.")
3737
args = parser.parse_args()
3838
# yapf: enable
3939

4040

4141
def infer(args):
42-
place = paddle.CUDAPlace(0) if args.use_gpu else paddle.CPUPlace()
43-
paddle.set_device("gpu" if args.use_gpu else "cpu")
42+
paddle.set_device(args.device)
4443

4544
# create dataset.
4645
infer_dataset = LacDataset(args.data_dir, mode='infer')
4746

4847
batchify_fn = lambda samples, fn=Tuple(
49-
Pad(axis=0, pad_val=0), # word_ids
50-
Stack(), # length
48+
Pad(axis=0, pad_val=0, dtype='int64'), # word_ids
49+
Stack(dtype='int64'), # length
5150
): fn(samples)
5251

5352
# Create sampler for dataloader
@@ -59,7 +58,6 @@ def infer(args):
5958
infer_loader = paddle.io.DataLoader(
6059
dataset=infer_dataset,
6160
batch_sampler=infer_sampler,
62-
places=place,
6361
return_list=True,
6462
collate_fn=batchify_fn)
6563

examples/lexical_analysis/train.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
parser.add_argument("--epochs", type=int, default=10, help="Corpus iteration num.")
3737
parser.add_argument("--batch_size", type=int, default=300, help="The number of sequences contained in a mini-batch.")
3838
parser.add_argument("--max_seq_len", type=int, default=64, help="Number of words of the longest seqence.")
39-
parser.add_argument("--n_gpu", type=int, default=1, help="Number of GPUs to use, 0 for CPU.")
39+
parser.add_argument("--device", default="gpu", type=str, choices=["cpu", "gpu", "xpu"] ,help="The device to select to train the model, is must be cpu/gpu/xpu.")
4040
parser.add_argument("--base_lr", type=float, default=0.001, help="The basic learning rate that affects the entire network.")
4141
parser.add_argument("--emb_dim", type=int, default=128, help="The dimension in which a word is embedded.")
4242
parser.add_argument("--hidden_size", type=int, default=128, help="The number of hidden nodes in the GRU layer.")
@@ -46,16 +46,16 @@
4646

4747

4848
def train(args):
49-
paddle.set_device("gpu" if args.n_gpu else "cpu")
49+
paddle.set_device(args.device)
5050

5151
# Create dataset.
5252
train_dataset = LacDataset(args.data_dir, mode='train')
5353
test_dataset = LacDataset(args.data_dir, mode='test')
5454

5555
batchify_fn = lambda samples, fn=Tuple(
56-
Pad(axis=0, pad_val=0), # word_ids
57-
Stack(), # length
58-
Pad(axis=0, pad_val=0), # label_ids
56+
Pad(axis=0, pad_val=0, dtype='int64'), # word_ids
57+
Stack(dtype='int64'), # length
58+
Pad(axis=0, pad_val=0, dtype='int64'), # label_ids
5959
): fn(samples)
6060

6161
# Create sampler for dataloader
@@ -116,7 +116,4 @@ def train(args):
116116

117117
if __name__ == "__main__":
118118
args = parser.parse_args()
119-
if args.n_gpu > 1:
120-
paddle.distributed.spawn(train, args=(args, ), nprocs=args.n_gpu)
121-
else:
122-
train(args)
119+
train(args)

0 commit comments

Comments
 (0)