Skip to content

Navigation Menu

Sign in
Sign up

OpenCL 2024 冬季作业 #8

Discussion options

本作业旨在扩展 operators-rs 项目中算子的浮点类型支持,通过添加 f16 类型实现增强模型的适配能力和性能优化。项目中已有完善的 f32 算子支持,并提供了 f16 算子实现的示例。作业的最终目标是:

  • 为所有算子添加 f16 类型支持。
  • 在 InfiniLM 项目中完成 Llama 端到端推理测试,验证性能和功能。

作业任务

  1. 下载模型;

  2. 配置 InfiniLM 并验证 CPU 推理

    NOTICE 关于 InfiniLM 的配置和细节参阅项目自述文件。

    git clone https://github.com/InfiniTensor/InfiniLM
    • PowerShell

      $env:TEST_MODEL=`model path`
      cargo test --release --package llama-cpu -- infer::test_infer --exact --nocapture
    • Bash

      export TEST_MODEL=`model path`
      cargo test --release --package llama-cpu -- infer::test_infer --exact --nocapture
  3. 验证 OpenCL 单精度浮点推理

    1. 将使用的算子库替换为 作业分支

      修改 InfiniLM/Cargo.toml:L37:

      operators = { git = "https://github.com/ma-hang/operators-rs", rev = "f9883a3", default-features = false }
    2. 验证推理(F32)

      • NOTICE 当前使用 f32 也有一些精度问题,只看前 100 个 token 大概是人话即可;
      • NOTICE 相比 CPU 需要额外设置 OpenCL 设备。只需要设置设备名字的一部分,例如,Intel 核显可能为 Arc;
      • PowerShell

        $env:TEST_MODEL=`model path`
        $env:DEVICES=`your opencl device name`
        cargo test --release --package llama-cpu -- infer::test_infer --exact --nocapture
      • Bash

        export TEST_MODEL=`model path`
        export DEVICES=`your opencl device name`
        cargo test --release --package llama-cpu -- infer::test_infer --exact --nocapture
  4. 适配 f16 精度

    1. fork from 作业分支;
    2. 阅读 Operators 项目源码,重点关注 OpenCL 算子的实现方式和结构。理解现有 f32 算子的实现逻辑,并分析 f16 示例算子的代码;
    3. 为以下算子添加 f16 支持:
      • rearrange
      • rms_norm(混合精度,权重为 f32)
      • mat_mul
      • rope
      • fused_softmax
      • swiglu
      • random_sample
    4. 编写测试用例
      • 为每个算子的 f16 版本编写单元测试,参考 f32 单元测试,确保计算结果与 f64 的一致性;
      • 测试样例需覆盖多种边界情况(如不同大小的矩阵或张量)。
    5. 验证端到端推理
      1. 将 InfiniLM 使用的算子库替换为你的仓库;
      2. 配置 f16 模型路径完成推理,确保模型能够正常生成文本。

通过标准

  • 算子正确性:算子 f16 精度通过单元测试;
  • 推理验证:InfiniLM 推理正常,输出对话内容正确;
  • 代码质量:确保代码包含注释,并通过格式检查;

作业提交

在本项目种发起名为"提交作业"的 issue,内容包括:

  • operators-rs fork 仓库路径;
  • 通过单测和文本生成的截图;
  • 实验报告(可选):描述实现细节、测试结果、优化过程,遇到的问题及其解决方案等;

附录

OpenCL 环境配置

课件;

Android 手机 adb 推理调试

  1. 编译环境

    export BINDGEN_EXTRA_CLANG_ARGS="--sysroot=$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/sysroot"
  2. operators-rs 调试

    NOTICE 根据实际情况修改 hash

    RUSTFLAGS="-C target-feature=+fullfp16" cargo test --target aarch64-linux-android --features opencl --no-run
    adb push target/aarch64-linux-android/debug/deps/operators-`hash` /data/local/tmp/
    adb shell chmod +x /data/local/tmp/operators-`hash`
    adb shell /data/local/tmp/operators-`hash`
    adb shell rm /data/local/tmp/operators-`hash`
  3. InfiniLM 调试

    cargo test --target aarch64-linux-android --release --package llama-cl --no-run
    adb push target/aarch64-linux-android/release/deps/llama_cl-`hash` /data/local/tmp/
    adb shell chmod +x /data/local/tmp/llama_cl-`hash`
    adb shell
    TEST_MODEL="/data/local/tmp/`model path`" MAX_STEPS="200" /data/local/tmp/llama_cl-`hash`
    adb shell rm /data/local/tmp/llama_cl-`hash`
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 participant

AltStyle によって変換されたページ (->オリジナル) /