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
31 changes: 28 additions & 3 deletions GGMLSharp/GGMLSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,35 @@
<Compile Include="SafeGGmlOptContext.cs" />
<Compile Include="Structs.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="ggml.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

<ItemGroup Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">
<Content Include="runtimes\win-x64\native\*.dll" Pack="false" CopyToOutputDirectory="PreserveNewest">
<Link>%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsOSPlatform('Windows')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">
<Content Include="runtimes\win-arm64\native\*.dll" Pack="false" CopyToOutputDirectory="PreserveNewest">
<Link>%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsOSPlatform('Linux')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">
<Content Include="runtimes\linux-x64\native\*.so" Pack="false" CopyToOutputDirectory="PreserveNewest">
<Link>%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsOSPlatform('Linux')) and '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">
<Content Include="runtimes\linux-arm64\native\*.so" Pack="false" CopyToOutputDirectory="PreserveNewest">
<Link>%(Filename)%(Extension)</Link>
</Content>
</ItemGroup>

<!-- For NuGet packaging we package all runtimes -->
<ItemGroup>
<None Include="runtimes\**\*" Pack="true" PackagePath="runtimes\" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion GGMLSharp/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace GGMLSharp
{
internal unsafe class Native
{
public const string DllName = "ggml";
public const string DllName = "libggml";

#region ggml.h

Expand Down
109 changes: 109 additions & 0 deletions GGMLSharp/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
FROM ubuntu:25.04 AS base

ARG GGML_COMMIT=master

RUN apt-get update && \
apt-get install -y git cmake build-essential ccache wget tar

WORKDIR /src

RUN git clone --filter=blob:none --no-checkout https://github.com/ggml-org/ggml.git && \
cd ggml && \
git checkout ${GGML_COMMIT} && \
sed -i '4598s/$/;/' src/ggml-quants.c # This is a quick patch needed for older GGML versions to get it to compile for windows

WORKDIR /src/ggml/build

FROM base AS win-base

ENV LLVM_MINGW_VERSION=20250717
ENV LLVM_MINGW_BASE=llvm-mingw-${LLVM_MINGW_VERSION}-ucrt-ubuntu-22.04-x86_64
ENV LLVM_MINGW_ARCHIVE=${LLVM_MINGW_BASE}.tar.xz
ENV LLVM_MINGW_PATH=/src/${LLVM_MINGW_BASE}

WORKDIR /src

RUN wget https://github.com/mstorsjo/llvm-mingw/releases/download/${LLVM_MINGW_VERSION}/${LLVM_MINGW_ARCHIVE} && \
tar -xf ${LLVM_MINGW_ARCHIVE}


WORKDIR /src/ggml/build

FROM win-base AS win-x64-builder

ENV TOOLCHAIN_FILE=/src/ggml/build/toolchain-windows-x64.cmake

RUN echo "set(CMAKE_SYSTEM_NAME Windows)" > ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_C_COMPILER ${LLVM_MINGW_PATH}/bin/x86_64-w64-mingw32-clang)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_CXX_COMPILER ${LLVM_MINGW_PATH}/bin/x86_64-w64-mingw32-clang++)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH ${LLVM_MINGW_PATH}/x86_64-w64-mingw32)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)" >> ${TOOLCHAIN_FILE}

RUN cmake .. \
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \
-DGGML_BUILD_TESTS=OFF \
-DGGML_OPENMP=OFF \
-DBUILD_SHARED_LIBS=ON

RUN cmake --build . --config Release -j $(nproc)

FROM win-base AS win-arm64-builder

ENV TOOLCHAIN_FILE=/src/ggml/build/toolchain-windows-arm64.cmake

RUN echo "set(CMAKE_SYSTEM_NAME Windows)" > ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_C_COMPILER ${LLVM_MINGW_PATH}/bin/aarch64-w64-mingw32-clang)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_CXX_COMPILER ${LLVM_MINGW_PATH}/bin/aarch64-w64-mingw32-clang++)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH ${LLVM_MINGW_PATH}/aarch64-w64-mingw32)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)" >> ${TOOLCHAIN_FILE}

RUN cmake .. \
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \
-DBUILD_SHARED_LIBS=ON \
-DGGML_OPENMP=OFF \
-DGGML_BUILD_TESTS=OFF

RUN cmake --build . --config Release -j $(nproc)

FROM base AS linux-x64-builder

RUN cmake .. \
-DBUILD_SHARED_LIBS=ON \
-DGGML_OPENMP=OFF \
-DGGML_BUILD_TESTS=OFF

RUN cmake --build . --config Release -j $(nproc)

FROM base AS linux-arm64-builder

ENV TOOLCHAIN_FILE=/src/ggml/build/toolchain-linux-arm64.cmake

RUN apt-get update && \
apt-get install -y g++-aarch64-linux-gnu

RUN echo "set(CMAKE_SYSTEM_NAME Linux)" > ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_SYSTEM_PROCESSOR aarch64)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)" >> ${TOOLCHAIN_FILE} && \
echo "set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)" >> ${TOOLCHAIN_FILE}

RUN cmake .. \
-DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \
-DBUILD_SHARED_LIBS=ON \
-DGGML_BUILD_TESTS=OFF

RUN cmake --build . --config Release -j $(nproc)

FROM base

WORKDIR /out

ENV BASE=/src/ggml/build

COPY --from=linux-x64-builder $BASE/src/*.so /out/runtimes/linux-x64/native/
COPY --from=linux-arm64-builder $BASE/src/*.so /out/runtimes/linux-arm64/native/
COPY --from=win-x64-builder $BASE/bin/*.dll /out/runtimes/win-x64/native/
COPY --from=win-arm64-builder $BASE/bin/*.dll /out/runtimes/win-arm64/native/
33 changes: 33 additions & 0 deletions GGMLSharp/build/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

set -e

IMAGE_NAME="ggml-multi-platform-builder"
OUTPUT_DIR="GGMLSharp/runtimes"

if [ ! -d "ggml" ]; then
echo "Error: 'ggml' submodule directory not found."
echo "Please run this script from the root of your project."
exit 1
fi

GGML_COMMIT=$(git rev-parse HEAD:ggml)
echo "Using ggml commit: ${GGML_COMMIT}"

echo "Building the Docker image"
docker build -t ${IMAGE_NAME} --network host -f GGMLSharp/build/Dockerfile --build-arg GGML_COMMIT=${GGML_COMMIT} .

echo "Extracting the compiled runtimes"

CONTAINER_ID=$(docker create ${IMAGE_NAME})

if [ -d "${OUTPUT_DIR}" ]; then
echo "Removing existing output directory: ${OUTPUT_DIR}"
rm -rf "${OUTPUT_DIR}"
fi
mkdir -p "${OUTPUT_DIR}"

docker cp "${CONTAINER_ID}:/out/runtimes/." "${OUTPUT_DIR}/"
docker rm -v "${CONTAINER_ID}"

echo "Done"
Binary file removed GGMLSharp/ggml.dll
Binary file not shown.
Binary file added GGMLSharp/runtimes/linux-arm64/native/libggml.so
Binary file not shown.
Binary file added GGMLSharp/runtimes/linux-x64/native/libggml.so
Binary file not shown.
Binary file added GGMLSharp/runtimes/win-arm64/native/libggml.dll
Binary file not shown.
Binary file added GGMLSharp/runtimes/win-x64/native/libggml.dll
Binary file not shown.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ GGMLSharp contains all ggml shared libs and some demos.
### Yolov3Tiny

[Yolov3Tiny](./Demos/Yolov3Tiny/) is a Demo shows how to implement YOLO object detection with ggml using pretrained model. The weight have been converted to gguf.

## Library compilation

Supported targets:
- Win x64/arm64
- Linux x64/arm64

to compile the shared `ggml` libraries yourself, just run:

```bash
bash GGMLSharp/build/build.sh
```

from the root repo directory.