
如何用 tensorflow/models 转换 PyTorch Longformer 检查点并在 TPU 上微调 GLUE【免费下载链接】modelsModels and examples built with TensorFlow项目地址: https://gitcode.com/GitHub_Trending/mode/models在 tensorflow/models 仓库中把 AllenAI 的 PyTorch Longformer 预训练权重allenai/longformer-base-4096转成 TensorFlow 检查点然后用 GLUE 的 MNLI 数据集在 TPU 上完成一次端到端微调。整个流程由 official/projects/longformer/README.md 给出先准备 TF 格式检查点再准备 MNLI 的 TFRecord 数据可选可直接用官方云存储中的数据最后用train.py以train_and_eval模式训练。完成后训练会在验证集上持续输出 accuracyREADME 给出的参考结果是约 3 小时跑完、性能约 86。准备环境与仓库结构按 official/README.md 的说明master 分支的官方模型基于 TensorFlow 2 的 master 分支开发需要安装 nightly 版本稳定版则要求 tensorflow/models 的版本号与目标 TensorFlow 版本一致pip3 install tf-models-nightly pip3 install tensorflow-text-nightly # when model uses nlp packagesPython 要求为 3.7集成测试运行在 Python 3.7 上。Longformer 项目位于official/projects/longformer/本次流程涉及的四个文件utils/convert_pretrained_pytorch_checkpoint_to_tf.pyPyTorch 权重转 TF 检查点utils/longformer_tokenizer_to_tfrecord.py把 HuggingFace 分词后的 MNLI 数据转成 TFRecordexperiments/glue_mnli_allenai.yamlMNLI 微调的实验配置train.py训练入口通过--experiment、--config_file、--params_override组装参数。转换脚本内部会import transformers并调用transformers.AutoModel.from_pretrained(allenai/longformer-base-4096)因此运行环境需要能安装transformers并联网下载 PyTorch 权重数据脚本还会用到datasets库。第一步准备 TF 格式的预训练检查点README 给出两个选项二者都让后续训练使用INIT_CHECKPOINTlongformer-4096/longformer。选项 1下载官方已转换好的检查点需要能访问该存储桶gsutil cp -r gs://model-garden-ucsd-zihan/longformer-4096 .该命令会把longformer-4096目录完整下载到当前工作目录。选项 2从 PyTorch 权重直接转换python3 utils/convert_pretrained_pytorch_checkpoint_to_tf.py在official/projects/longformer/目录下执行。脚本逻辑是加载allenai/longformer-base-4096的全部 PyTorch 参数构建仓库内的 TFLongformerEncoder逐层把 embedding、attention 的 key/query/value 投影含 global attention 分支、FFN 与 LayerNorm 权重写入对应层最后调用tf.train.Checkpoint(encoderencoder).write(output_path)。保存路径在main()中固定为longformer-4096/longformer相对于当前目录。需要注意脚本中pretrained_lm allenai/longformer-base-4096是硬编码的这份转换逻辑只覆盖这一种规格vocab_size50265、max_position_embeddings4098换其他变体时需要自行修改脚本。第二步可选自行准备 MNLI 的 TFRecordREADME 默认的训练命令直接使用官方上传到云存储的两份数据文件如果可以直接访问本步可跳过。否则运行分词脚本把 HuggingFace 的 GLUE MNLI 转成 TFRecordpython3 longformer_tokenizer_to_tfrecord.py该脚本实际位于utils/目录README 中的命令省略了目录前缀。它会datasets.load_dataset(glue, mnli)下载数据需要联网用allenai/longformer-base-4096的 tokenizer 按max_seq_length 512分词——脚本注释明确提示 “make sure this is the same with model input size”即这里必须与模型输入尺寸一致对应配置里的seq_length: 512。task_name mnli也是写死的。输出写到脚本顶部的save_path ./即当前工作目录文件名为allenai_longformer-base-4096_train.tf_record与allenai_longformer-base-4096_eval.tf_record由模型名替换/为_生成。第三步在 TPU 上启动微调在official/projects/longformer/目录下执行 README 中的训练命令TRAIN_DATAtask.train_data.input_pathgs://model-garden-ucsd-zihan/longformer_allenai_mnli_train.tf_record,task.validation_data.input_pathgs://model-garden-ucsd-zihan/longformer_allenai_mnli_eval.tf_record INIT_CHECKPOINTlongformer-4096/longformer PYTHONPATH/path/to/model/garden \ python3 train.py \ --experimentlongformer/glue \ --config_fileexperiments/glue_mnli_allenai.yaml \ --params_override${TRAIN_DATA},runtime.distribution_strategytpu,task.init_checkpoint${INIT_CHECKPOINT} \ --tpulocal \ --model_dir/path/to/outputdir \ --modetrain_and_eval命令中两处占位符需要替换PYTHONPATH/path/to/model/garden指向本仓库根目录的本地路径保证train.py里from official.core import ...这类导入能解析到仓库内的official包--model_dir/path/to/outputdir输出目录训练会在这里写检查点并在train模式下serialize_config(params, model_dir)落一份参数快照。如果第二步生成了本地 TFRecord把TRAIN_DATA中两个input_path的值替换为本地文件的实际路径即可其余参数不变。其余参数的作用--experimentlongformer/glue使用 longformer_experiments.py 中注册的longformer/glue配置工厂即SentencePredictionConfigLongformerEncoderConfig的句子对预测任务--config_fileexperiments/glue_mnli_allenai.yaml加载 YAML 覆盖默认配置。该配置中num_classes: 3MNLI 三类标签、seq_length: 512、max_position_embeddings: 4098、attention_window为 12 层各 128、train_steps: 61359注释说明为 392,702 条训练数据 × 5 epochs、validation_interval: 2000、metric_type: accuracy其中train_data.input_path与validation_data.input_path默认值是TODO必须靠params_override填上真实路径runtime.distribution_strategytpu把分布策略切到 TPUtrain.py 会用runtime.tpu地址构建对应的 distribution strategytask.init_checkpoint${INIT_CHECKPOINT}加载第一步产出的 TF 预训练权重--tpulocal使用本机 TPU--modetrain_and_eval训练的同时持续跑验证。另外仓库里还有一份 experiments/glue_mnli.yaml其seq_length为 128、attention_window为各层 32与 allenai 版配置不同本文路径按 README 使用glue_mnli_allenai.yaml。结果验证--modetrain_and_eval下trainer 每validation_interval: 2000步在验证集上评估一次指标为accuracyYAML 中metric_type: accuracy。判断是否跑通看两点训练过程能按validation_interval持续输出验证精度检查点在checkpoint_interval: 1000步写入model_dir。README 给出的参考结果是 “This should take ~ 3 hours to run, and give a performance of ~86.”——即约 3 小时、精度约 86这是文档示例量级不是必须复现的固定数值。已知限制所有 Longformer 模型都要求配置中给出global_attention_size它对每句的前global_attention_size个 token 施加统一的 global attentionREADME 说明这是为了在 TPU 上运行时张量尺寸可以静态确定不支持对每句话使用不同的 global attention 尺寸。转换脚本与分词脚本都只针对allenai/longformer-base-4096和 MNLI 写死模型名、task_name、max_seq_length512改模型或改任务需要修改脚本本身。分词脚本会联网下载 GLUE 数据并在当前目录写两个 TFRecord 文件训练命令中的云存储路径需要能访问gs://model-garden-ucsd-zihan桶否则要用第二步的本地文件替代。【免费下载链接】modelsModels and examples built with TensorFlow项目地址: https://gitcode.com/GitHub_Trending/mode/models创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考