NJUNLP / knn-box

an easy-to-use knn-mt toolkit
MIT License
104 stars 12 forks source link

KeyError: 'keys' #17

Closed AIikai closed 11 months ago

AIikai commented 1 year ago

请问出现这个问题是什么原因呢?

2023-02-09 08:23:10 | INFO | fairseq.tasks.translation | /data/home/likai/NMT-offline/knn-box/knnbox-scripts/vanilla-knn-mt/../../data-bin/zh2en-ziyan-03 train zh-en 721 examples
2023-02-09 08:23:11 | INFO | train |  | valid on 'train' subset | loss 2.994 | nll_loss 1.346 | ppl 2.54 | wps 0 | wpb 16975 | bsz 721                                                                                                     
[vals.npy: (16975,) saved successfully ^_^ ]
|||  {'vals': <knnbox.common_utils.memmap.Memmap object at 0x7f9b55f62990>} <class 'knnbox.common_utils.memmap.Memmap'>
Traceback (most recent call last):
  File "/data/home/likai/NMT-offline/knn-box/knnbox-scripts/vanilla-knn-mt/../../knnbox-scripts/common/validate.py", line 252, in <module>
    cli_main()
  File "/data/home/likai/NMT-offline/knn-box/knnbox-scripts/vanilla-knn-mt/../../knnbox-scripts/common/validate.py", line 246, in cli_main
    distributed_utils.call_main(args, main, override_args=override_args)
  File "/data/home/likai/NMT-offline/knn-box/fairseq/distributed_utils.py", line 301, in call_main
    main(args, **kwargs)
  File "/data/home/likai/NMT-offline/knn-box/knnbox-scripts/vanilla-knn-mt/../../knnbox-scripts/common/validate.py", line 192, in main
    datastore.build_faiss_index("keys", use_gpu=(not args.build_faiss_index_with_cpu))   # build faiss index
  File "/data/home/likai/NMT-offline/knn-box/knnbox/datastore/datastore.py", line 177, in build_faiss_index
    if not isinstance(self.datas[name], Memmap):
KeyError: 'keys'
Maxwell-Lyu commented 1 year ago

能否提供一下运行的脚本内容?从输出来看,我猜测您是在运行保存datastore的脚本,但执行完对数据的forward之后,却没有保存下来对应的keys.npy,因此无法继续进行index的建立操作。 导致这一问题的最大可能,是您脚本当中--knn-mode build_datastore这部分设置有问题,模型没有运行在保存key的执行模式下

ZhaoQianfeng commented 1 year ago

我猜你不需要key所以把保存key的代码注释掉了? 如果是这样,那报这个错是因为工具包在build_datastore的时候会顺便为key建立faiss索引,如果你没保存key,建索引就会报错。解决办法就是不建立索引,把knnbox-scripts/common/validate.py的192行注释掉即可

 if knn_type in ["vanilla_knn_mt", "adaptive_knn_mt", "kernel_smoothed_knn_mt", "vanilla_knn_mt_visual", "plac_knn_mt", "robust_knn_mt"]:
        datastore.dump()    # dump to disk
        # 注释掉下一行
        datastore.build_faiss_index("keys", use_gpu=(not args.build_faiss_index_with_cpu))   # build faiss index
AIikai commented 1 year ago

能否提供一下运行的脚本内容?从输出来看,我猜测您是在运行保存datastore的脚本,但执行完对数据的forward之后,却没有保存下来对应的keys.npy,因此无法继续进行index的建立操作。 导致这一问题的最大可能,是您脚本当中--knn-mode build_datastore这部分设置有问题,模型没有运行在保存key的执行模式下

谢谢回复!脚本如下,没怎么改动,只是换用了自己的翻译模型。对的,keys.npy未保存下来,请问该部分代码是在哪个位置?可能是我看遗漏了吧,只保存下来了vals.npy和config.json

`export OMP_WAIT_POLICY=PASSIVE

PROJECT_PATH=$( cd -- "$( dirname -- "$ BASH_SOURCE[0]}" )" &> /dev/null && pwd )/../.. DATA_PATH=$PROJECT_PATH/data-bin/zh2en-ziyan-03 BASE_MODEL=$PROJECT_PATH/pretrain-models/zh-en-old/checkpoint.pt DATASTORE_SAVE_PATH=$PROJECT_PATH/datastore/vanilla/it

CUDA_VISIBLE_DEVICES=5 python $PROJECT_PATH/knnbox-scripts/common/validate.py $DATA_PATH \ --task translation \ --path $BASE_MODEL \ --model-overrides "{'eval_bleu': False, 'required_seq_len_multiple':1, 'load_alignments': False}" \ --dataset-impl mmap \ --valid-subset train \ --skip-invalid-size-inputs-valid-test \ --max-tokens 4096 \ --bpe fastbpe \ --user-dir $PROJECT_PATH/knnbox/models \ --arch vanilla_knn_mt@transformer_zh_en_lk \ --knn-mode build_datastore \ --knn-datastore-path $DATASTORE_SAVE_PATH `

Maxwell-Lyu commented 1 year ago

保存key的代码位于knnbox/models/当中,以vanilla_knn_mt.py所对应的vanilla_knn_mt@transformer_zh_en模型为例,您可以注意该文件的第112行

        if self.args.knn_mode == "build_datastore":
            keys = select_keys_with_pad_mask(x, self.datastore.get_pad_mask())
            # save half precision keys
            self.datastore["keys"].add(keys.half())