復現ACL2020論文:RAT-SQL(paper|code)時遇到的bug總結以及自己的復現流程。嘗試了docker之后遇到一堆坑,最后決定直接使用conda環境。需要知道root密碼。liunx,Ubuntu20.04,RTX3090。"/path/to/"表示該文件或目錄的所在路徑,比如"/path/to/rat-sql",在具體路徑"/home/ps/rat-sql"中,"/path/to/"等於"/home/ps/"。
1 BUG總結
1.1 ValueError: Unsupported kind for param args: VAR_POSITIONAL
發生在preprocess時。原因在於pytorch版本過高或者python版本過高,可用如下命令安裝pytorch
conda install pytorch==1.3.1 cudatoolkit=10.1
preprocess階段還有類似bug,都可用這個方法解決,請使用python 3.7。
1.2 no space left on device
發生在train時。微軟給的代碼中保存的模型檢查點過多,非bert訓練大約需要幾十G,bert則需要幾百G。
需要指定 --logdir 到足夠大的硬盤中,或減少檢查點數量。
1.3 找不到__LOGDIR__路徑
發生在eval時。在infer.py和eval.py中的__LOGDIR__都被替換為了實際的log路徑,但是在run.py中,沒有被替換,可以把run.py中104行開始的如下兩行代碼
res_json = json.load(open(eval_output_path))
print(step, res_json['total_scores']['all']['exact'])
替換成如下代碼
model_config = json.loads(_jsonnet.evaluate_file(
eval_config.config,
tla_codes={'args': eval_config.config_args}))
if 'model_name' in model_config:
specific_logdir = os.path.join(logdir, model_config['model_name'])
eval_output_path = eval_output_path.replace('__LOGDIR__', specific_logdir)
res_json = json.load(open(eval_output_path))
print(step, res_json['total_scores']['all']['exact'])
else:
specific_logdir = logdir
eval_output_path = eval_output_path.replace('__LOGDIR__', specific_logdir)
res_json = json.load(open(eval_output_path))
print(step, res_json['total_scores'])
1.4 assert next_choices is not None
發生在eval wikisql時,需要把experiments/wikisql-glove-run.jsonnet中第12行的
eval_use_heuristic: true
改為
eval_use_heuristic: false
1.5 AttributeError: 'RMKeyView' object has no attribute 'index'
依舊發生在eval wikisql時,是records包本身的bug。找到path/to/anaconda3/envs/ratsql/lib/python3.7/site-packages/records.py(ratsql是conda環境名;python3.7是python版本)
找到第40行keys函數中
return self._keys
改為
return list(self._keys)
1.6 把自定義的包路徑加入conda環境中
遇到不能pip install或conda install的包時,比如third_party中的wikisql。用PYTHONPATH=""等方法加到當前終端(或類似方法加到linux當前用戶,linux所有用戶)感覺相當麻煩,我就想加到我的conda環境中,也不影響其他的項目也不影響別人。使用如下命令一行解決。
conda develop /path/to/rat-sql/third_party/wikisql/
具體原理是在"/path/to/anaconda3/envs/ratsql/lib/python3.7/site-packages"(ratsql是conda環境名;python3.7是python版本)目錄下生成一個conda.pth文件,conda環境會把conda.pth文件中的路徑加到sys.path中,因此只在該conda環境中有效。
2 復現流程
2.1 安裝linux包
sudo su
mkdir -p /usr/share/man/man1 && \
apt-get update && apt-get install -y \
build-essential \
cifs-utils \
curl \
default-jdk \
dialog \
dos2unix \
git \
sudo
exit
2.2 創建conda環境,安裝python包
conda create -n ratsql python=3.7
conda activate ratsql
pip install asdl==0.1.5
pip install astor==0.7.1
pip install attrs==18.2.0
pip install babel==2.7.0
pip install bpemb==0.2.11
pip install cython==0.29.1
pip install jsonnet==0.14.0
pip install networkx==2.2
pip install nltk==3.4
pip install pyrsistent==0.14.9
pip install pytest==5.3.2
pip install records==0.5.3
pip install stanford-corenlp==3.9.2
pip install tabulate==0.8.6
conda install pytorch==1.3.1 cudatoolkit=10.1
pip install torchtext==0.3.1
pip install tqdm==4.36.1
pip install transformers==2.3.0
pip install entmax
pip install scikit-learn
2.3 下載nltk_data和bert
python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt')"
python -c "from transformers import BertModel; BertModel.from_pretrained('bert-large-uncased-whole-word-masking')"
2.4 下載stanford-corenlp和wikisql官方腳本
sudo su
mkdir -p third_party && \
cd third_party && \
curl https://download.cs.stanford.edu/nlp/software/stanford-corenlp-full-2018-10-05.zip | jar xv && \
cd .. && \
git clone https://github.com/salesforce/WikiSQL third_party/wikisql
exit
連不上github可以把https://換成git://
2.5 把下載下來並組織好的data(參照rat-sql的readme)復制到項目中
mkdir -p data && \
cd data && \
cp -r /path/to/data ./ && \
cd ..
2.6 將所有 shell 腳本轉換為 Unix 行尾
/bin/bash -c 'if compgen -G "/path/to/rat-sql/**/*.sh" > /dev/null; then dos2unix /app/**/*.sh; fi'
2.7 把wikisql官方腳本加到conda環境
conda develop /path/to/rat-sql/third_party/wikisql
3 運行
3.1 運行命令
3.1.1 spider-glove
python run.py preprocess experiments/spider-glove-run.jsonnet
python run.py train experiments/spider-glove-run.jsonnet
python run.py eval experiments/spider-glove-run.jsonnet
3.1.2 spider-bert
python run.py preprocess experiments/spider-bert-run.jsonnet
python run.py train experiments/spider-bert-run.jsonnet
python run.py eval experiments/spider-bert-run.jsonnet
3.1.3 wikisql-glove
python run.py preprocess experiments/wikisql-glove-run.jsonnet
python run.py train experiments/wikisql-glove-run.jsonnet
python run.py eval experiments/wikisql-glove-run.jsonnet