CANN: add CROSS_ENTROPY_LOSS operator #3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
描述 (Description)
本 PR 在 ggml 的 CANN 后端中添加对 CROSS_ENTROPY_LOSS的支持。
变更摘要:
在 ggml/src/ggml-cann/aclnn_ops.h 中增加函数声明。
在 ggml/src/ggml-cann/aclnn_ops.cpp 中增加函数实现。
在 ggml/src/ggml-cann/ggml-cann.cpp 中:
在 ggml_cann_compute_forward 的 switch-case 中注册算子;
在 ggml_backend_cann_supports_op 中使算子返回 true。
测试 (Testing)
测试步骤:
执行编译:
cmake -B build -DGGML_CANN=on -DCMAKE_BUILD_TYPE=release
cmake --build build --config release -j
运行测试
./bin/test-backend-ops test -b CANN0 -o CROSS_ENTROPY_LOSS
测试结果:

备注 (Notes)
当前仅实现并测试 F32。若需要支持 F16、BF16 或量化类型,需增加转换/降精逻辑或在后端实现相应 kernel。
经测试,cninfer的环境似乎不支持cross entropy loss算子的运行,只能使用cntrain集群的节点进行实验。