Skip to content

feat(cambricon): add gelu provider - #959

Open
baominghelly wants to merge 1 commit into
masterfrom
feat/cambricon-gelu
Open

feat(cambricon): add gelu provider#959
baominghelly wants to merge 1 commit into
masterfrom
feat/cambricon-gelu

Conversation

@baominghelly

Copy link
Copy Markdown
Contributor

Summary

  • Add a native Cambricon GELU provider in src/native/cambricon/ops/gelu/.
  • Use CNNL for exact GELU on float16 and float32, with a custom MLU kernel for tanh approximation and bfloat16.
  • Support contiguous and strided input/output tensors, in-place execution, scalar tensors, and empty tensors.
  • Skip Cambricon float64 cases in tests/test_gelu.py because CNNL GELU does not support that dtype.

Motivation

InfiniOps did not have a native Cambricon implementation for the canonical gelu operator. This provider enables GELU execution on Cambricon while retaining CNNL where it is reliable and using a custom fallback for the remaining supported paths.

Type of Change

  • feat — new feature / new operator / new platform
  • fix — bug fix
  • perf — performance improvement (no behavioral change)
  • refactor — code restructuring without behavior change
  • test — adding or fixing tests only
  • docs — documentation only
  • build / ci — build system or CI configuration
  • chore — tooling, formatting, or other non-code changes
  • Breaking change (requires a ! in the Conventional Commits prefix or a BREAKING CHANGE: footer)

Platforms Affected

  • CPU (WITH_CPU)
  • NVIDIA (WITH_NVIDIA)
  • Iluvatar (WITH_ILUVATAR)
  • MetaX (WITH_METAX)
  • Cambricon (WITH_CAMBRICON)
  • Moore (WITH_MOORE)
  • Ascend (WITH_ASCEND)
  • PyTorch C++ bindings (WITH_TORCH)
  • Build system / CMake / CI
  • Python bindings / user-facing API

Smoke Test Result

Targeted Cambricon operator build and test were run after rebasing onto origin/master. The repository-wide smoke suite was not run.

cmake -S . -B /workspace/build/pr-gelu-20260901 \
  -DAUTO_DETECT_DEVICES=OFF \
  -DWITH_CPU=ON \
  -DWITH_CAMBRICON=ON \
  -DWITH_TORCH=OFF \
  -DWITH_LINKED=OFF \
  -DGENERATE_PYTHON_BINDINGS=ON \
  -DINFINI_OPS_OPS=gelu \
  -DINFINI_RT_ROOT=/workspace/install/infinirt-master-test \
  -DPython_EXECUTABLE=/torch/venv3/pytorch/bin/python3.10 \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/workspace/build/pr-gelu-20260901-site/infini
cmake --build /workspace/build/pr-gelu-20260901 --parallel "$(nproc)"
cmake --install /workspace/build/pr-gelu-20260901
PYTHONNOUSERSITE=1 \
PYTHONPATH=/workspace/build/pr-gelu-20260901-site \
LD_LIBRARY_PATH=/workspace/build/pr-gelu-20260901-site/infini:/workspace/install/infinirt-master-test/lib:${LD_LIBRARY_PATH:-} \
/torch/venv3/pytorch/bin/python3.10 -m pytest -q tests/test_gelu.py --devices cambricon --maxfail=1

42 passed, 14 skipped in 0.35s

Test Results on Supported Platforms

Platform Affected Build / Smoke Result Full Result / Notes
NVIDIA No N/A - not affected N/A - not affected
Iluvatar No N/A - not affected N/A - not affected
MetaX No N/A - not affected N/A - not affected
Cambricon Yes Targeted build passed tests/test_gelu.py: 42 passed, 14 skipped; full suite not run
Moore No N/A - not affected N/A - not affected
Ascend No N/A - not affected N/A - not affected
Full `pytest` output (optional)
ssssssssssssss..........................................                 [100%]
42 passed, 14 skipped in 0.35s

Benchmark / Performance Impact

N/A — no performance benchmark was run.

Notes for Reviewers

  • Exact GELU for float16 and float32 uses CNNL.
  • Tanh-approximate GELU and bfloat16 use the custom GeluUnion kernel. The custom path computes in float32 before converting back to the output dtype.
  • The custom path supports contiguous and non-contiguous tensors and allocates workspace only for copied shape/stride metadata.
  • float64 is intentionally unsupported on Cambricon and is skipped in the MLU test cases.
  • Please review the numerical behavior of the custom tanh and bfloat16 paths. The full repository smoke/full suite and performance benchmarks were not run.

@baominghelly
baominghelly marked this pull request as ready for review September 1, 2026 04:56
@baominghelly
baominghelly requested review from a team and voltjia September 1, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant