Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletions .github/workflows/build-triton.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
# SPDX-FileCopyrightText: 2026 The RISE Project
# SPDX-License-Identifier: MIT
---
# Upstream triton-lang/triton is triaged not-feasible on riscv64 (gotcha 41): it is
# a GPU-only compiler with proprietary NVIDIA/AMD vendor blobs and no riscv64
# target anywhere in the stack. This workflow instead builds the
# RuyiAI-Stack/triton-riscv fork, which replaces the GPU codegen with a real
# LLVM/MLIR-native path (TTIR -> Linalg via triton-shared, Linalg -> LLVM IR via
# Buddy Compiler, then riscv64 `llc` codegen) and ships it as a `triton.backends`
# plugin, so no part of upstream's own build is patched.
#
# triton-riscv's own scripts/release.sh is reused directly rather than
# reimplemented: given a `<py-tag> <arch>` pair it does the whole job itself --
# docker-runs the riscv64 manylinux image, installs its own clang/lld toolchain,
# clones the pinned triton-lang/triton commit (triton-hash.txt), downloads
# prebuilt riscv64 LLVM and buddy-mlir toolchains (buddy-hash.txt, as GitHub
# release tarballs -- no from-source LLVM build), builds Triton with the
# triton-shared plugin against the stable Python ABI (TRITON_STABLE_ABI=ON, so
# the wheel is tagged cp312-abi3), strips the bundled LLVM/buddy tool binaries,
# and runs auditwheel repair. TRITON_RISCV_REF below pins their release tag
# independently of docs/packages/triton.yaml's version (which tracks triton's own
# __version__, not triton-riscv's release cadence).
name: Build triton wheels (riscv64)

on:
workflow_dispatch:
inputs:
version:
description: 'Version glob to (re)build; empty builds every version of docs/packages/triton.yaml not released yet'
required: false
default: ''
cancel-in-progress:
description: Cancel an in-progress run in the same concurrency group.
required: false
type: boolean
default: true
pull_request:
branches: [main]
paths:
- '.github/workflows/build-triton.yml'
- 'docs/packages/triton.yaml'
- 'patches/triton/**'
push:
branches: [main]
paths:
- '.github/workflows/build-triton.yml'
- 'docs/packages/triton.yaml'
- 'patches/triton/**'

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: ${{ github.event_name != 'workflow_dispatch' || inputs['cancel-in-progress'] }}

permissions:
contents: read # to fetch code (actions/checkout)

env:
# The triton-riscv tag this workflow builds, independent of the triton version
# matrix below (see the header comment).
TRITON_RISCV_REF: release/20261115-e99c326

jobs:
setup:
uses: $/.github/workflows/_setup.yml
with:
package: triton
version: ${{ inputs.version }}

build_wheels:
needs: [setup]
if: needs.setup.outputs.versions != '[]'
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
name: Build triton ${{ matrix.version }} cp312-abi3-manylinux_riscv64
runs-on: ubuntu-24.04-riscv
# Real riscv64 CI evidence (this PR's own two runs): the first successful
# build took ~7h end to end; a rerun of the exact same build hit the 360
# (6h) cap this comment originally guessed from upstream's x86_64 CI
# timing, with pip's own "Building editable for triton: still running..."
# heartbeat printing continuously right up to the timeout - the build was
# never hung, just genuinely this slow (and variable) on this hardware.
timeout-minutes: 600

env:
TRITON_VERSION: ${{ matrix.version }}

steps:
- name: Checkout triton-riscv ${{ env.TRITON_RISCV_REF }}
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: RuyiAI-Stack/triton-riscv
ref: ${{ env.TRITON_RISCV_REF }}
path: triton-riscv
persist-credentials: false

- name: Checkout python-wheels
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
path: python-wheels
sparse-checkout: patches/triton
persist-credentials: false

- name: Apply patches
run: git apply -v ../python-wheels/patches/triton/${{ env.TRITON_VERSION }}/*.patch
working-directory: triton-riscv

- name: Build wheel
working-directory: triton-riscv
env:
# Anonymous git from this fleet's self-hosted runners gets a 401 on
# some hosts (their own ci.yml carries the same workaround); setup.sh
# clones triton-lang/triton and Triton's own cmake clones googletest.
GITHUB_TOKEN: ${{ github.token }}
run: |
# Real riscv64 CI (run 44157994959, job 134304033843): the previous
# plain `./scripts/release.sh cp312-cp312 riscv64` invocation let
# release.sh's own docker-run wrap the build in a fresh container
# with no concurrency cap, and the "Building editable for triton"
# step was silently SIGKILLed (exit 137) after ~1h40m with no other
# error text - gotcha 421's signature (this fleet's riscv64 runners
# have ~15GB RAM and no usable swap, and release.sh's own docker-run
# forwards no MAX_JOBS/CMAKE_BUILD_PARALLEL_LEVEL into the container
# the way their own ci.yml sets on the host).
#
# release.sh already supports being invoked with IN_DOCKER pre-set
# (it then skips its own docker-run wrapping entirely and proceeds
# straight to the build), so this reproduces its exact documented
# riscv64 container invocation (same image, same volumes, same env
# vars - see the `if [ -z "${IN_DOCKER:-}" ]` branch in
# scripts/release.sh) with one addition: --cpuset-cpus, to restrict
# the affinity mask ninja/make read when picking their default -j,
# since nproc/hardware_concurrency() respect cpuset affinity but not
# a cgroup CPU quota (--cpus alone would not actually reduce the
# inferred build parallelism). Halving the visible cores (floor 2)
# is a cap, not a tune - the exact number matters far less than
# having one at all.
total_cpus="$(nproc)"
half_cpus="$(( total_cpus / 2 ))"
[ "${half_cpus}" -lt 2 ] && half_cpus=2
[ "${half_cpus}" -gt "${total_cpus}" ] && half_cpus="${total_cpus}"
docker run --rm -i --cpuset-cpus="0-$((half_cpus - 1))" \
-e IN_DOCKER=1 \
-e PY_TAG=cp312-cp312 \
-e TARGET_ARCH=riscv64 \
-e HOST_UID="$(id -u)" \
-e HOST_GID="$(id -g)" \
-e HOME=/workspace \
-e GITHUB_TOKEN \
-v "$(pwd):/workspace/triton-riscv" \
-v /tmp/triton_riscv_docker_dnf_cache:/var/cache/dnf \
-v /tmp/triton_riscv_docker_pip_cache:/workspace/.cache/pip \
-w /workspace/triton-riscv \
quay.io/pypa/manylinux_2_39_riscv64:latest \
/bin/bash ./scripts/release.sh cp312-cp312 riscv64
ls -la build/release/riscv64/cp312-cp312/

- uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: triton-${{ env.TRITON_VERSION }}-cp312-abi3-manylinux_riscv64
path: triton-riscv/build/release/riscv64/cp312-cp312/*.whl
if-no-files-found: error

- name: setup uv
uses: astral-sh/setup-uv@20cfd1bf945f4377ade1205e4dbc17946fc9a30d # v10.0.1
with:
python-version: '3.12'
activate-environment: true
enable-cache: false

- name: Test triton wheel
env:
# `uv pip install` only reads `UV_*` env vars, never `PIP_*` - a plain
# PIP_EXTRA_INDEX_URL here silently no-ops, so uv only ever sees public
# PyPI (no riscv64 torch there) and nothing in this registry at all
# (CLAUDE.md gotcha 244).
UV_EXTRA_INDEX_URL: https://pypi.riseproject.dev/simple/
UV_INDEX_STRATEGY: unsafe-best-match
# The wheel's own backend/paths.py looks up mlir-translate via
# LLVM_BINARY_DIR, a bundled backend/bin/ copy, or PATH - but
# release.sh's own packaging loop only copies opt/buddy-llc/
# llvm-objdump/llvm-readobj/buddy-opt/buddy-translate into the
# wheel, never mlir-translate, so the bundled-copy and PATH
# fallbacks both come up empty. setup.sh's download_release()
# unpacks the real LLVM toolchain (which does have mlir-translate)
# under triton-riscv/.cache/llvm, and because release.sh's docker
# run bind-mounts the checkout rather than copying it in, that
# directory is still on the runner's filesystem here, outside the
# now-exited container.
LLVM_BINARY_DIR: ${{ github.workspace }}/triton-riscv/.cache/llvm/bin
run: |
# torch is this registry's own riscv64 CPU build (docs/packages/torch.yaml);
# Triton's kernel launch path expects torch tensors, and installing it from
# the registry here (rather than triton-riscv's own external
# ruyirepo.ruyicommunity.cn index, used only by their own CI) keeps this
# entirely within this repo's normal wheel supply.
uv pip install --only-binary=:all: torch
uv pip install triton-riscv/build/release/riscv64/cp312-cp312/*.whl
# @triton.jit needs inspect.getsourcelines() to find the decorated
# function's real source, which fails for code handed to `python3
# -c` (there is no file to read lines from) - write a real .py file
# instead (unrelated to riscv64; this would fail identically on
# any platform).
cat > test_triton_kernel.py <<'EOF'
import torch
import triton
import triton.language as tl

@triton.jit
def add_kernel(x_ptr, y_ptr, out_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
pid = tl.program_id(axis=0)
offsets = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
mask = offsets < n_elements
x = tl.load(x_ptr + offsets, mask=mask)
y = tl.load(y_ptr + offsets, mask=mask)
tl.store(out_ptr + offsets, x + y, mask=mask)

x = torch.rand(98432)
y = torch.rand(98432)
out = torch.empty_like(x)
n_elements = out.numel()
grid = lambda meta: (triton.cdiv(n_elements, meta['BLOCK_SIZE']),)
add_kernel[grid](x, y, out, n_elements, BLOCK_SIZE=1024)
expected = x + y
assert torch.allclose(out, expected), (out, expected)
print('triton riscv64/triton_shared kernel OK, max abs diff',
(out - expected).abs().max().item())
EOF
python3 test_triton_kernel.py

publish:
name: Publish triton ${{ matrix.version }}
needs: [setup, build_wheels]
if: needs.setup.outputs.versions != '[]'
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
permissions:
contents: write
pull-requests: write
uses: $/.github/workflows/_publish-wheel.yml
secrets:
app-private-key: ${{ secrets.RISEPROJECT_APP_PRIVATE_KEY }}
with:
matrix: ${{ needs.setup.outputs.matrix }}
artifact-pattern: triton-${{ matrix.version }}-*-manylinux_riscv64
17 changes: 17 additions & 0 deletions docs/packages/triton.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package-name: triton
source-code: https://github.com/RuyiAI-Stack/triton-riscv
license: MIT AND Apache-2.0 WITH LLVM-exception AND Apache-2.0
comment: >-
Upstream triton-lang/triton is GPU-only with no riscv64 target (gotcha 41).
This builds the RuyiAI-Stack/triton-riscv fork instead, which
replaces the NVIDIA/AMD codegen with a CPU/riscv64 backend (triton-shared's
TTIR->Linalg lowering, then Buddy Compiler's Linalg->LLVM-MLIR lowering, then
LLVM `llc`), built against prebuilt riscv64 LLVM and buddy-mlir toolchains
(no from-source LLVM build). The wheel bundles those toolchains' `opt`,
`llc`, and `buddy-*` binaries alongside Triton itself, hence the compound
license: MIT (triton, triton-shared), Apache-2.0 WITH LLVM-exception (LLVM),
Apache-2.0 (buddy-mlir).
versions:
- version: 3.9.0+gitbbe38e57
files:
- python: '3.12-abi3:manylinux'
105 changes: 105 additions & 0 deletions patches/triton/3.9.0+gitbbe38e57/0001-riscv-safe-isa-baseline.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
Upstream-Status: Inappropriate [this fleet's riscv64 silicon implements RVV 0.7.1, not 1.0]

Real riscv64 CI (run 44206575156, job 135305237854): "Build wheel" succeeded after
~7h45m, then "Test triton wheel" ran a plain @triton.jit elementwise add kernel on
real torch tensors and died with `Illegal instruction`, exit code 132 (128+4 =
SIGILL), right after the one UserWarning print and before the kernel's own print -
i.e. inside the JIT-compiled kernel itself, on first execution, every time.

backend/riscv.py's DEFAULT_LLC_FEATURES hardcodes
"+m,+a,+f,+d,+c,+v,+zfh,+zvfh,+zba,+zbb,+zvfbfmin" (RVV + half-float-vector +
bitmanip) and backend/compiler.py's native (non-cross-compile) `_llir_to_bin`
codegen path passes that string to `llc -mattr=...` unconditionally - with no
runtime feature probe at all, i.e. gotcha 139's "compiled with RVV, no runtime
check at all" shape, except reached by a hardcoded default rather than a
build-time autodetect. The test kernel's `tl.load`/`+`/`tl.store` over
BLOCK_SIZE=1024-wide tiles lowers through MLIR's vector dialect, and with `+v`
set, `llc` is free to codegen that as real RVV (`vsetvli`, `vle32.v`, `vfadd.vv`,
`vse32.v` - see triton-riscv's own docs/06-RISCV-QEMU.md, which documents exactly
this instruction shape as the intended output of this path).

This repo's own CI fleet is already established, independently and repeatedly,
to be T-Head-class RVV-0.7.1 silicon that still advertises HWCAP_V (gotcha 272,
confirmed again for this same `ubuntu-24.04-riscv` runner pool by
gotcha 448's openvino ELF-attribute/opcode analysis: "the fleet is T-Head
(C906/C910/C920 class: TH1520, SG2042)... hence RVV 0.7.1... HWCAP advertises V;
the first RVV-1.0 `vsetvli` is still illegal"). LLVM's `llc` only ever emits the
RVV-1.0 vsetvli/vsetivli encoding (RVV 0.7.1 support was removed upstream years
ago), so any kernel whose IR actually contains a vector op and is codegen'd with
`+v` on this fleet hits exactly the symptom above: the very first `vsetvli`
traps. Gotcha 272 also already establishes QEMU cannot be used to confirm or
rule this out - TCG implements `vsetvli` correctly regardless of version, so
triton-riscv's own QEMU-based testing (docs/06-RISCV-QEMU.md,
scripts/triton-riscv-env.sh) would never have caught this; it only reproduces on
the real runner, which is what just happened.

Fix: lower DEFAULT_MARCH/DEFAULT_LLC_FEATURES to the plain rv64gc baseline
(I+M+A+F+D+C, matching this repo's established "RISCV64_GENERIC" safe baseline
for every other RVV-bearing port - see gotcha 448's "Pick RISCV64_GENERIC, not a
vector target" and gotcha 139's "capping the baseline to rv64gcv would make
every wheel require RVV hardware" framing, here with RVV-1.0-vs-0.7.1 added on
top of "require RVV at all"). +f,+d stay (lp64d is the real ABI; dropping them
causes soft-float link errors per the existing comment two lines above this
hunk) - only +v and its dependents (+zfh,+zvfh,+zvfbfmin) and the two bitmanip
extensions (+zba,+zbb, unverified on this fleet and not needed by anything this
backend currently emits) are dropped.

Also fix the native `_llir_to_bin` codegen branch to read the toolchain's
`llc_features` (TRITON_RISCV_LLC_FEATURES env var, falling back to the now-safe
DEFAULT_LLC_FEATURES) instead of the bare DEFAULT_LLC_FEATURES module constant
it had inlined directly. TRITON_RISCV_CROSS_COMPILE=1's codegen path already
reads this env var via RiscvToolchain.from_env(); the plain native
`@triton.jit` path - the one this wheel's own test, and every consumer's normal
import, actually exercises - silently did not, so the override this project
already documents (TRITON_RISCV_LLC_FEATURES) had no effect for a native run.
This keeps the safe default closed by this patch, while letting anyone who has
confirmed their own riscv64 target genuinely implements RVV 1.0 opt back in
without needing a repatch.

--- a/backend/riscv.py
+++ b/backend/riscv.py
@@ -11,9 +11,17 @@ import subprocess
from typing import Iterable, Sequence

DEFAULT_TRIPLE = "riscv64-unknown-linux-gnu"
-DEFAULT_MARCH = "rv64gcv_zfh_zvfh_zba_zbb"
+# Baseline is rv64gc (I+M+A+F+D+C), not the RVV/Zfh/Zba/Zbb superset upstream
+# defaults to. The real silicon this is JIT-compiled and executed on natively
+# can advertise HWCAP_V while actually implementing RVV 0.7.1, so any RVV-1.0
+# `vsetvli` llc emits for the superset SIGILLs at kernel-launch time; see the
+# patch header for the real-hardware evidence. TRITON_RISCV_MARCH/
+# TRITON_RISCV_LLC_FEATURES remain the override for anyone who has confirmed
+# their target genuinely implements RVV 1.0 (or the bitmanip/half-float
+# extensions) and wants them back.
+DEFAULT_MARCH = "rv64gc"
DEFAULT_MABI = "lp64d"
-DEFAULT_LLC_FEATURES = "+m,+a,+f,+d,+c,+v,+zfh,+zvfh,+zba,+zbb,+zvfbfmin"
+DEFAULT_LLC_FEATURES = "+m,+a,+f,+d,+c"


def _find_tool(
--- a/backend/compiler.py
+++ b/backend/compiler.py
@@ -835,13 +835,21 @@ def _llir_to_bin(llir: str, metadata, options=None):
# of generated Triton IR when optimized (including masked
# copies and scalar random-number kernels). Keep native
# codegen unoptimized until those backend issues are fixed.
+ #
+ # Read TRITON_RISCV_LLC_FEATURES here too (RiscvToolchain.
+ # from_env() falls back to DEFAULT_LLC_FEATURES otherwise):
+ # this native-run path previously hardcoded the module
+ # constant directly, so the env var that TRITON_RISCV_CROSS_
+ # COMPILE=1 already honors silently had no effect on a plain
+ # native `@triton.jit` call - the actual path a wheel's
+ # consumer exercises.
llc_args = [
llc_path,
src_path,
"-filetype=obj",
"-O0",
"-relocation-model=pic",
- f"-mattr={DEFAULT_LLC_FEATURES}",
+ f"-mattr={RiscvToolchain.from_env().llc_features}",
"-o",
dst_path,
]