携手创作,共同生长!这是我参与「日新计划 8 月更文挑战」的第9天,点击检查活动概况

一、问题描绘

由于计划做一个智能代码提示生成的东西,所以晚上就消耗大量时间在coding。。。。。。

平常都在aistudio的linux环境下干活,这次想着作为插件部署到jupyter下,所以就本地干活了,但是平常跑的很溜的代码,就呈现了难以想象的过错。详细如下:

记一次windows环境下PaddleNLP的模型下载bug排除

平常都用的好好的,为什么这次就错了呢?为此我翻开虚拟环境,挨个代码检查, 看看到底什么地方犯错,看代码都很平静,那问题出在哪儿呢?修正下paddlenlp代码打印下日志:

https://bj.bcebos.com/paddlenlp/models/community/Salesforce/codegen-350M-mono\vocab.json
https://bj.bcebos.com/paddlenlp/models/community/Salesforce/codegen-350M-mono\merges.txt
https://bj.bcebos.com/paddlenlp/models/community/Salesforce/codegen-350M-mono\added_tokens.json

二、问题剖析

发现下载模型等链接,且报404,心有不甘,手动点链接,文件下载文件成功,那这个是什么锅?

心有不甘,仔细对比发现最结尾的斜杠不一样,会不会是这个问题呢?我对比下发现:

记一次windows环境下PaddleNLP的模型下载bug排除

那,不同的斜杠,request 下载结果截然不同,那锅找到了。

三、处理办法

剖析道问题了,那么这个杠怎样来的?大胆猜想,这里的杠是采用了os.path.join来衔接 url 的。殊不知不管win还是linux等系统,url格式都是共同的,反而会导致python访问异常。那么就按部就班处理问题,把原先的path.join修正为“/”。

1.修正transformers\model_utils.py

对该文件第1539行修正

 # vocab_files["tokenizer_config_file"] = os.path.join(
            #     COMMUNITY_MODEL_PREFIX, pretrained_model_name_or_path,
            #     cls.tokenizer_config_file)
            vocab_files["tokenizer_config_file"] = COMMUNITY_MODEL_PREFIX+ '/'+ pretrained_model_name_or_path +'/'+ cls.tokenizer_config_file

2.修正transformers\tokenizer_utils_base.py

对该文件248行修正

  # Assuming from community-contributed pretrained models
            for file_id, file_name in cls.resource_files_names.items():
                # full_file_name = os.path.join(COMMUNITY_MODEL_PREFIX,
                #                               pretrained_model_name_or_path,
                #                               file_name)
                full_file_name = COMMUNITY_MODEL_PREFIX +'/'+  pretrained_model_name_or_path +'/'+file_name
                resource_files[file_id] = full_file_name
            # resource_files["model_config_file"] = os.path.join(
            #     COMMUNITY_MODEL_PREFIX, pretrained_model_name_or_path,
            #     cls.model_config_file)
            resource_files["model_config_file"] = COMMUNITY_MODEL_PREFIX +'/'+ pretrained_model_name_or_path +'/'+cls.model_config_file

这样就处理了win下模型下载犯错问题。