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
123 changes: 123 additions & 0 deletions .github/workflows/build-dynet38.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# SPDX-FileCopyrightText: 2026 The RISE Project
# SPDX-License-Identifier: MIT
---
# Based on: https://github.com/taishi-i/dynet/blob/3c9f35de86e98a5809e9c81394194c8f6e0aa0dc/.github/workflows/build_macos_wheels.yml
# and the test step of https://github.com/taishi-i/dynet/blob/3c9f35de86e98a5809e9c81394194c8f6e0aa0dc/.travis/test_dynet.sh
name: Build dynet38 wheels (riscv64)

on:
workflow_dispatch:
inputs:
version:
description: 'Version glob to (re)build; empty builds every version of docs/packages/dynet38.yaml not released yet'
required: false
default: ''
pull_request:
branches: [main]
paths:
- '.github/workflows/build-dynet38.yml'
- 'docs/packages/dynet38.yaml'
- 'patches/dynet38/**'
push:
branches: [main]
paths:
- '.github/workflows/build-dynet38.yml'
- 'docs/packages/dynet38.yaml'

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

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

env:
# No 2.2 tag; the build_macos_wheels branch tip is the tree the latest 2.2 wheels (cp314) were built from.
DYNET38_REF: 3c9f35de86e98a5809e9c81394194c8f6e0aa0dc
MANYLINUX_RISCV64_IMAGE: quay.io/pypa/manylinux_2_39_riscv64

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

build_wheels:
needs: [setup]
if: needs.setup.outputs.versions != '[]'
name: Build dynet38 ${{ matrix.version }} ${{ matrix.python }}-manylinux_riscv64
runs-on: ubuntu-24.04-riscv
timeout-minutes: 360
strategy:
fail-fast: false
matrix:
version: ${{ fromJSON(needs.setup.outputs.versions) }}
python: ["cp312", "cp313", "cp314"]

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

steps:
- name: Checkout dynet ${{ env.DYNET38_REF }}
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: taishi-i/dynet
ref: ${{ env.DYNET38_REF }}
persist-credentials: false

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

- name: Apply patches
run: git apply python-wheels/patches/dynet38/${{ env.DYNET38_VERSION }}/*.patch

- uses: pypa/cibuildwheel@1828c10ab37f080699c7b81cea34097c684a7074 # v4.2.0
with:
output-dir: wheelhouse/
only: ${{ matrix.python }}-manylinux_riscv64
env:
CIBW_MANYLINUX_RISCV64_IMAGE: ${{ env.MANYLINUX_RISCV64_IMAGE }}
CIBW_ENVIRONMENT: >-
PIP_USE_PEP517=1
PIP_EXTRA_INDEX_URL=https://pypi.riseproject.dev/simple/
PIP_ONLY_BINARY=numpy
CIBW_TEST_COMMAND: cd {project}/tests/python && python test.py

- name: Check wheel contents
run: |
python3 - wheelhouse/*.whl <<'EOF'
import sys, zipfile
names = zipfile.ZipFile(sys.argv[1]).namelist()
assert any(n.startswith("_dynet.") and n.endswith(".so") for n in names), names
assert any(n.startswith("dynet38.libs/libdynet") for n in names), names
licences = {n.rsplit("/", 1)[-1] for n in names if ".dist-info/licenses/" in n} - {""}
assert licences == {"LICENSE.txt", "LICENSE.eigen-mpl2"}, licences
EOF

- name: Store wheels
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: dynet38-${{ env.DYNET38_VERSION }}-${{ matrix.python }}-manylinux_riscv64
path: ./wheelhouse/*.whl
if-no-files-found: error

publish:
name: Publish dynet38 ${{ 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:
artifact-pattern: dynet38-${{ matrix.version }}-*-manylinux_riscv64
6 changes: 6 additions & 0 deletions docs/packages/dynet38.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package-name: dynet38
source-code: https://github.com/taishi-i/dynet
license: Apache-2.0 / MPL-2.0 (eigen)
versions:
- version: '2.2'
patched: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
From 578b42fd151547869f5353ef3db5c8867a7a6f5f Mon Sep 17 00:00:00 2001
From: Ludovic Henry <git@ludovic.dev>
Date: Fri, 25 Sep 2026 10:46:55 +0000
Subject: [PATCH 1/3] cmake: don't pass -march=native on RISC-V

The non-Windows, non-Apple-ARM branch of CMakeLists.txt unconditionally
appends -march=native to CMAKE_CXX_FLAGS. GCC on riscv64 has no "native"
value for -march and rejects it outright:

g++: error: '-march=native': ISA string must begin with rv32 or rv64

so setup.py's CMake build fails on the first object file. Skip the flag
when CMAKE_SYSTEM_PROCESSOR is RISC-V, mirroring the existing carve-out
for Apple Silicon. DyNet has no ISA-specific code of its own and Eigen
falls back to its scalar path when no vector ISA is enabled, so the
build needs no replacement flag; leaving -march at the toolchain default
(rv64gc) also keeps the wheel portable across riscv64 hardware.

Upstream-Status: To upstream [not yet submitted; clab/dynet has had no commit since 2022, so this belongs in the taishi-i/dynet fork that publishes the dynet38 wheels]

Signed-off-by: Ludovic Henry <git@ludovic.dev>
---
CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2acd4211..eda2e11f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -76,7 +76,7 @@ if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNOMINMAX") # Disable min/max macros in windef.h
elseif(APPLE AND (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm.*" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64.*"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=apple-m1") # Clang on ARM-based Macs does not support -march=native
-else()
+elseif(NOT ${CMAKE_SYSTEM_PROCESSOR} MATCHES "riscv.*") # GCC on RISC-V does not support -march=native
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
endif()

--
2.43.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
From 868a9985403b2c1d24944e1280b2c65e6cbbd66b Mon Sep 17 00:00:00 2001
From: Ludovic Henry <git@ludovic.dev>
Date: Fri, 25 Sep 2026 10:51:26 +0000
Subject: [PATCH 2/3] mem: fall back to posix_memalign when <mm_malloc.h> is
missing

CPUAllocator gets its 32-byte-aligned blocks from _mm_malloc()/_mm_free(),
declared in <mm_malloc.h>. GCC installs that header for x86 targets only;
riscv64 GCC has none, so mem.cc fails to compile:

dynet/mem.cc:13:10: fatal error: mm_malloc.h: No such file or directory

Keep including the header wherever it exists (or wherever __has_include
cannot tell), and otherwise define _mm_malloc()/_mm_free() in terms of
posix_memalign()/free() -- the same pair GCC's own x86 <mm_malloc.h>
wraps -- so the allocator behaves identically on every target.

Upstream-Status: To upstream [not yet submitted; clab/dynet has had no commit since 2022, so this belongs in the taishi-i/dynet fork that publishes the dynet38 wheels]

Signed-off-by: Ludovic Henry <git@ludovic.dev>
---
dynet/mem.cc | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/dynet/mem.cc b/dynet/mem.cc
index 700da14c..10460cd3 100644
--- a/dynet/mem.cc
+++ b/dynet/mem.cc
@@ -10,7 +10,25 @@

#include <fcntl.h>
#if !_WINDOWS
+#if defined(__has_include)
+#if __has_include(<mm_malloc.h>)
+#define DYNET_HAVE_MM_MALLOC_H 1
+#endif
+#else
+#define DYNET_HAVE_MM_MALLOC_H 1
+#endif
+#if DYNET_HAVE_MM_MALLOC_H
#include <mm_malloc.h>
+#else
+// No <mm_malloc.h> on this target (GCC ships it for x86 only): use the same
+// posix_memalign()/free() pair that GCC's x86 <mm_malloc.h> wraps.
+static inline void* _mm_malloc(size_t size, size_t align) {
+ void* ptr;
+ if (align < sizeof(void*)) align = sizeof(void*);
+ return posix_memalign(&ptr, align, size) == 0 ? ptr : nullptr;
+}
+static inline void _mm_free(void* ptr) { free(ptr); }
+#endif
#endif
#include "dynet/except.h"
#include "dynet/devices.h"
--
2.43.0

Loading
Loading