diff --git a/.github/workflows/build-triton.yml b/.github/workflows/build-triton.yml new file mode 100644 index 00000000000..928eb44a2f1 --- /dev/null +++ b/.github/workflows/build-triton.yml @@ -0,0 +1,235 @@ +# 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 ` ` 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' + push: + branches: [main] + paths: + - '.github/workflows/build-triton.yml' + - 'docs/packages/triton.yaml' + +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: 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 diff --git a/docs/packages/triton.yaml b/docs/packages/triton.yaml new file mode 100644 index 00000000000..51b35e8bfc5 --- /dev/null +++ b/docs/packages/triton.yaml @@ -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 (CLAUDE.md + 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 + files: + - python: '3.12-abi3:manylinux'