cheneydon / efficient-bert

This repository contains the code for the paper in Findings of EMNLP 2021: "EfficientBERT: Progressively Searching Multilayer Perceptron via Warm-up Knowledge Distillation".
32 stars 4 forks source link

对create_pretrain_feature.sh 文件的疑问 #3

Closed QyDing02 closed 2 years ago

QyDing02 commented 2 years ago

当我运行create_pretrain_feature.sh 中的如下一段时(Wikipedia only 的那一段),即:

python create_pretrain_feature.py --lowercase --vocab_path $VOCAB_PATH --wiki_dir $WIKI_DIR

会报这个错误:

[11 07:34:01] Namespace(batch_size=64, book_dir=PosixPath('.'), concate_data_dir=PosixPath('.'), exp_dir='./exp/tmp/20220311-193401', local_rank=0, lowercase=True, merge_path='', start_epoch=1, teacher_model='bert_base', total_epochs=10, train_ratio=1, val_ratio=0, vocab_path='./pretrained_ckpt/bert-base-uncased-vocab.txt', wiki_dir=PosixPath('dataset/pretrain_data/wikipedia_nomask'))
Traceback (most recent call last):
  File "create_pretrain_feature.py", line 54, in <module>
    total_examples += int(num_epoch_examples[epoch % len(num_epoch_examples)] * args.train_ratio)
ZeroDivisionError: integer division or modulo by zero

我不知道导致len(num_epoch_examples)==0的原因是什么。 而且奇怪的是,当跳过这段代码,执行Wikipedia + BooksCorpus那一段的时候,即:

# Wikipedia + BooksCorpus
python create_pretrain_feature.py --lowercase --vocab_path $VOCAB_PATH --wiki_dir $WIKI_DIR --book_dir $BOOK_DIR --concate_data_dir $CONCATE_DATA_DIR    

一切正常,bookcorpus_nomask、wiki_book_nomask、 wikipedia_nomask这三个文件夹里各保存了5个data_epoch_x的文件。

请问是哪里出了问题?

cheneydon commented 2 years ago

https://github.com/cheneydon/efficient-bert/blob/28bca740c6be1c217e2817afd1480c1d6dc71548/create_pretrain_feature.py#L49-L52 这里你需要确定train_dir下是否有epoch_xx_metrics.json这样的文件,是通过create_pretrain_data.sh这个文件生成的,程序会读取每个json文件里的num_training_examples项值。不过在create_pretrain_feature.py这个阶段total_examples没有被使用,相关代码也可以注释掉

QyDing02 commented 2 years ago

经过调试,我找到了解决方案: https://github.com/cheneydon/efficient-bert/blob/28bca740c6be1c217e2817afd1480c1d6dc71548/create_pretrain_feature.py#L36-L39 可能是我的环境问题,这里的args.book_dirargs.concate_data_dir两个参数,当路径为空的时候并没有识别成false,而是PosixPath('.'),导致无论输入的路径来自wikipedia还是bookcorpus,条件都成立,默认进入第37行(all_train_dir = [args.wiki_dir, args.book_dir])这一分支。 当输入路径只有wikipedia的路径时, all_train_dir=[PosixPath('dataset/pretrain_data/wikipedia_nomask'),Posixpath('.')]all_train_dir[1]是空路径,引起后续的报错。 当我这样修改之后:

tmp_path=Path()
if args.book_dir!=tmp_path and args.concate_data_dir!=tmp_path:
    all_train_dir = [args.wiki_dir, args.book_dir] 
else: 
    all_train_dir = [args.wiki_dir] 

程序能正常运行了。

cheneydon commented 2 years ago

嗯,好的