文件最后提交记录最后更新时间
ADD ELMo Signed-off-by: sebdog <lh76997@gmail.com>3 年前
fix link validity Co-authored-by: frozenleaves<914814442@qq.com> # message auto-generated for no-merge-commit merge: !7517 merge master into master fix link validity Created-by: frozenn Commit-by: frozenleaves Merged-by: ascend-robot Description: ## Motivation Please describe the motivation of this PR and the goal you want to achieve through this PR. ## Modification Please briefly describe what modification is made in this PR. ## Self-test (Optional) If modifications to this PR may cause/fix function/accuracy/performance DTSs/issues, a self-inspection record needs to be attached. ## BC-breaking (Optional) If there are compatibility issues, such as dependencies on cann/torch_npu versions, they need to be explained in the PR. ## Checklist **Before PR**: - [ ] The new code needs to comply with the Clean Code specification. - [ ] The PR content is self-checked, and the expression can be clear and the writing standardized **After PR**: - [ ] CLA has been signed and all committers have signed the CLA in this PR. - [ ] The ci-pipeline is passed, Code Check is passed. See merge request: Ascend/ModelZoo-PyTorch!75171 个月前
add patch Signed-off-by: sebdog <lh76997@gmail.com>3 年前
!3620 Elmo模型交付件补充 * update ACL_PyTorch/contrib/nlp/ELMo/README.md. * update ACL_PyTorch/contrib/nlp/ELMo/README.md. * 删除文件 ACL_PyTorch/contrib/nlp/ELMo/opt_onnx.py * update elmo_postprocess * 修改readme描述和后处理脚本优化 * add opt_onnx.py of elmo 3 年前
update ACL_PyTorch/contrib/nlp/ELMo/elmo_preprocess.py. Signed-off-by: sebdog <lh76997@gmail.com>3 年前
update ACL_PyTorch/contrib/nlp/ELMo/elmo_pth2onnx.py. Signed-off-by: sebdog <lh76997@gmail.com>3 年前
ADD ELMo Signed-off-by: sebdog <lh76997@gmail.com>3 年前
update Signed-off-by: sebdog <lh76997@gmail.com>3 年前
README.md

ELMo模型-推理指导

概述

ELMo模型是用于训练得到单词词向量的,不同于以往常用的word2vec(CBOW、SkipGram、Hierarchical Softmax)通过大规模语料对每一个单词训练出固定的词向量,ELMo可以通过不同的语句对同一个单词训练得到不同的词向量,有效区分出同一个单词在不同语境下表示的不同含义(例如:apple可以表示苹果,也可以表示iphone)。

  • 参考实现:

    url=https://github.com/allenai/allennlp.git
    branch=main
    commit_id=b2eb036e06fbf4e293abb126552aaabc3df91aa1
    model_name=elmo
    

输入输出数据

  • 输入数据

    输入数据 数据类型 大小 数据排布格式
    input INT32 1 x 8 x 50 NWD
  • 输出数据

    输出数据 大小 数据类型 数据排布格式
    output 1 x 8 x 1024 FLOAT32 NWD

推理环境准备[所有版本]

  • 该模型需要以下插件与驱动

    表 1 版本配套表

配套 版本 环境准备指导
固件与驱动 1.0.15 Pytorch框架推理环境准备
CANN 5.1.RC2 -
Python 3.7.5 -
PyTorch 1.8.1 -
说明:Atlas 300I Duo 推理卡请以CANN版本选择实际固件与驱动版本。 \ \

快速上手

获取源码

  1. 获取源码。(解压命令参考tar –xvf *.tar与 unzip *.zip)

    git clone https://github.com/allenai/allennlp.git
    cd allennlp 
    git reset --hard b2eb036e06fbf4e293abb126552aaabc3df91aa1
    git apply ../elmo.patch
    cd ..
    mv allennlp my_allennlp
    
  2. 安装依赖。

    pip3 install -r requirements.txt
    
  3. 安装auto-optimizer工具

    安装方式参考auto-optimizer开源链接

准备数据集

  1. 获取原始数据集。

    该模型使用1 Billion Word Language Model Benchmark数据集进行推理,下载链接,该数据集包含训练集和测试集,下载后解压在当前目录下。文件夹名称为1-billion-word-language-modeling-benchmark-r13output,命令如下:

    wget https://www.statmt.org/lm-benchmark/1-billion-word-language-modeling-benchmark-r13output.tar.gz
    tar -xvf 1-billion-word-language-modeling-benchmark-r13output.tar.gz
    

    数据集目录结构如下:

     1-billion-word-language-modeling-benchmark-r13output
     ├── heldout-monolingual.tokenized.shuffled
     |   ├── news.en.heldout-00000-of-00050
     |   ├── news.en.heldout-00001-of-00050
     |   ...
     ├── t.raining-monolingual.tokenized.shuffled
     |   ├── news.en-00001-of-00100
     |   ├── news.en-00002-of-00100
     |   ...
     └── README
    
  2. 数据预处理。

    数据预处理将原始数据集转换为模型输入的数据。

    执行elmo_preprocess.py脚本,完成预处理。

    python3 elmo_preprocess.py \
        --file_path ./1-billion-word-language-modeling-benchmark-r13output/heldout-monolingual.tokenized.shuffled/ \
        --save_path ./data.txt \
        --bin_path ./bin_path \
        --file_num 50 \
        --word_len 8
    
    • 参数说明:

      • --file_path:测试数据集路径
      • --save_path:中间文件路径
      • --bin_path:预处理数据集路径
      • --file_num:测试数据集数量
      • --word_len:取句子长度

    运行成功之后产生的data.txt文件为从原始测试集中筛选的符合模型输入的数据(即句子长度小于等于8的句子);bin_path下为处理好的每个句子的二进制文件。

模型推理

  1. 获取权重文件

    使用PyTorch将模型权重文件.pth转换为.onnx文件,再使用ATC工具将.onnx文件转为离线推理模型文件.om文件。

下载链接为: ELMo Models中的Original模型,并下载对应的weightsoptions放在当前目录下,文件名称分别为elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5elmo_2x4096_512_2048cnn_2xhighway_options.json

  1. 模型转换

    1. 导出onnx文件。

      1. 使用elmo_pth2onnx.py导出onnx文件。

        运行elmo_pth2onnx.py脚本。

        python3 elmo_pth2onnx.py \
            --output_file elmo.onnx \
            --word_len 8 \
            --option_file ./elmo_2x4096_512_2048cnn_2xhighway_options.json \
            --weight_file ./elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5
        
      • 参数说明:

        • --output_file:导出onnx文件路径
        • --word_len:模型输出句子长度
        • --option_file: 模型option文件路径
        • --weight_file: 模型weight文件路径

        获得elmo.onnx文件。

      1. 优化ONNX文件。

        python3 -m onnxsim elmo.onnx elmo_sim.onnx
        python -m auto_optimizer optimize elmo_sim.onnx elmo_sim_opt.onnx
        

        获得elmo_sim_opt.onnx文件。

    2. 使用ATC工具将ONNX模型转OM模型。

      1. 配置环境变量。

        source /usr/local/Ascend/ascend-toolkit/set_env.sh
        

        说明: 该脚本中环境变量仅供参考,请以实际安装环境配置环境变量。详细介绍请参见《CANN 开发辅助工具指南 (推理)》。

      2. 执行命令查看芯片名称(${chip_name})。

        npu-smi info
        #该设备芯片名为Ascend310P3 (自行替换)
        回显如下:
        +-------------------+-----------------+------------------------------------------------------+
        | NPU     Name      | Health          | Power(W)     Temp(C)           Hugepages-Usage(page) |
        | Chip    Device    | Bus-Id          | AICore(%)    Memory-Usage(MB)                        |
        +===================+=================+======================================================+
        | 0       310P3     | OK              | 15.8         42                0    / 0              |
        | 0       0         | 0000:82:00.0    | 0            1074 / 21534                            |
        +===================+=================+======================================================+
        | 1       310P3     | OK              | 15.4         43                0    / 0              |
        | 0       1         | 0000:89:00.0    | 0            1070 / 21534                            |
        +===================+=================+======================================================+
        
      3. 执行ATC命令。

        atc --framework=5 \
            --model=elmo_sim_opt.onnx \
            --output=elmo_sim_opt\
            --input_format=ND \
            --input_shape="input:1,8,50" \
            --log=error --soc_version=Ascend${chip_name}
        
        • 参数说明:

          • --model:为ONNX模型文件。
          • --framework:5代表ONNX模型。
          • --output:输出的OM模型。
          • --input_format:输入数据的格式。
          • --input_shape:输入数据的shape。
          • --log:日志级别。
          • --soc_version:处理器型号,${chip_name}表示通过npu-smi info命令查询的设备名称。

          运行成功后生成elmo_sim_opt.om模型文件。

  2. 开始推理验证。

    1. 安装ais_bench推理工具。

      请访问ais_bench推理工具代码仓,根据readme文档进行工具安装。

    2. 执行推理。

      python3 -m ais_bench \
          --model ./elmo_sim_opt.om \
          --input ./bin_path \
          --output ./ \
          --batchsize 1
      
    • 参数说明:

      • --model:模型路径
      • --input:数据集文件夹路径
      • --output:输出路径
      • --batchsize: om模型的batchsize

    推理完成后在当前工作目录生成推理结果,命名格式为xxxx_xx_xx-xx_xx_xx(年_月_日-时_分_秒),如2022_08_18-06_55_19

    1. 精度对比。

      1. 调用脚本将om的推理结果与模型在线推理结果进行比对,计算余弦相似度,相似度大于99%即为达标。
      python3 elmo_postprocess.py \
          --inputs "bin_path/" \
          --om_out ${output_path} \
          --option_file ./elmo_2x4096_512_2048cnn_2xhighway_options.json \
          --weight_file ./elmo_2x4096_512_2048cnn_2xhighway_weights.hdf5
      
      • 参数说明:

        • --onnx_input:模型输入路径。
        • --om_out:om模型推理结果。
        • --option_file: 模型option文件路径
        • --weight_file: 模型weight文件路径

      ${output_path}为推理结果的保存路径,与onnx推理结果比对余弦相似度为99.60%。

    2. 性能对比。

      可使用ais_bench推理工具的纯推理模式验证om模型的性能,参考命令如下:

      python3 -m ais_bench --model ./elmo_sim_opt.om --loop 20 --batchsize 1
      
      • 参数说明:

        • --model:模型路径
        • --loop:推理次数
        • --batchsize: om模型的batchsize

模型推理性能&精度

调用ACL接口推理计算,性能参考下列数据。

芯片型号 Batch Size 数据集 精度 性能
300I Pro 1 1 Billion Word Language Model Benchmark R13 Output cosine similarity:99.60% 62.91 fps

注:该模型只支持batchsize为1。