Skip to content

[KernelGen][Kunlunxin] Add bernoulli operator - #289

Merged
Dongxu-H merged 2 commits into
flagos-ai:infra-cifrom
NineAnnAnn:kunlunxin-bernoulli-opt
Aug 18, 2026
Merged

[KernelGen][Kunlunxin] Add bernoulli operator#289
Dongxu-H merged 2 commits into
flagos-ai:infra-cifrom
NineAnnAnn:kunlunxin-bernoulli-opt

Conversation

@NineAnnAnn

Copy link
Copy Markdown
Contributor

Summary

Adds a Kunlunxin (P800) backend specialization for bernoulli. Draws binary random numbers (0 or 1) from a Bernoulli distribution parameterized by the input probability tensor. Adds a dedicated Triton kernel for this backend and registers it in the backend ops module.

Testing

  • Validated against reference on device (Kunlunxin (P800))
  • Accuracy: PASS (6 cases) over 3 dtypes x 6 shapes

Performance

Test command: pytest benchmark/test_bernoulli.py --level core (Kunlunxin (P800))

bernoulli

dtype Size Torch Latency (ms) Gems Latency (ms) Speedup
float16 [64, 64] 0.011379 0.006026 1.888
float16 [4096, 4096] 3.374843 0.209165 16.135
float16 [64, 512, 512] 3.374927 0.208959 16.151
float32 [64, 64] 0.011972 0.005828 2.054
float32 [4096, 4096] 3.345646 0.211519 15.817
float32 [64, 512, 512] 3.345941 0.211849 15.794
bfloat16 [64, 64] 0.011650 0.005710 2.040
bfloat16 [4096, 4096] 3.269944 0.315181 10.375
bfloat16 [64, 512, 512] 3.271776 0.315200 10.380
Operator Geometric Mean Speedup
bernoulli 7.252

Multi-backend Testing

Backend Accuracy Test Speedup (mean) Notes
Kunlunxin (P800) PASS (6 cases) 7.252 Primary

Files Changed

  • src/flag_gems/runtime/backend/_kunlunxin/ops/bernoulli.py: New Kunlunxin (P800) kernel implementation
  • src/flag_gems/runtime/backend/_kunlunxin/ops/__init__.py: Register import and __all__ entry

Add a kunlunxin backend specialization for bernoulli with a tuned Triton kernel and register it in the backend ops module. Accuracy validated on device; benchmark measured on Kunlunxin (P800).
@Dongxu-H

Copy link
Copy Markdown
Collaborator

/test | bernoulli:kunlunxin

@github-actions

Copy link
Copy Markdown

❌ On-demand test failed

Operator: bernoulli
Runner: kunlunxin
Backend: kunlunxin

The test failed to complete. Check the workflow run for details.

@Dongxu-H

Copy link
Copy Markdown
Collaborator

init.py 中 bernoulli 的导入位置应该按字母顺序放在 batch_norm 和 bernoulli_ 之间
all 列表中 bernoulli 应该按字母顺序排列而不是放在末尾

@Dongxu-H
Dongxu-H merged commit 3b2e850 into flagos-ai:infra-ci Aug 18, 2026
9 checks passed
103yiran pushed a commit that referenced this pull request Aug 18, 2026
Co-authored-by: NineAnnAnn <NineAnnAnn@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants