Skip to content

Commit e1abe8e

Browse files
committed
mlx
1 parent d9ebff3 commit e1abe8e

File tree

4 files changed

+69
-22
lines changed

4 files changed

+69
-22
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ jobs:
1414
with:
1515
submodules: recursive
1616

17+
- name: Install CMake
18+
run: brew install cmake
19+
20+
- name: Build native stub library
21+
run: |
22+
cmake -S native -B native/build -DCMAKE_BUILD_TYPE=Release
23+
cmake --build native/build --target mlxsharp --config Release
24+
mkdir -p src/MLXSharp.Native/runtimes/osx-arm64/native
25+
cp native/build/libmlxsharp.dylib src/MLXSharp.Native/runtimes/osx-arm64/native/
26+
1727
- name: Setup .NET
1828
uses: actions/setup-dotnet@v4
1929
with:
2030
dotnet-version: 9.0.x
21-
include-prerelease: true
2231

2332
- name: Show .NET info
2433
run: dotnet --info
@@ -29,28 +38,38 @@ jobs:
2938
- name: Build
3039
run: dotnet build MLXSharp.sln --configuration Release --no-restore
3140

41+
- name: Copy native library to test output
42+
run: |
43+
TEST_OUTPUT="src/MLXSharp.Tests/bin/Release/net9.0"
44+
mkdir -p "$TEST_OUTPUT/runtimes/osx-arm64/native"
45+
cp src/MLXSharp.Native/runtimes/osx-arm64/native/libmlxsharp.dylib "$TEST_OUTPUT/runtimes/osx-arm64/native/"
46+
ls -la "$TEST_OUTPUT/runtimes/osx-arm64/native/"
47+
48+
- name: Run tests
49+
run: dotnet test MLXSharp.sln --configuration Release --no-build --logger "trx;LogFileName=TestResults.trx" --results-directory artifacts/test-results
50+
3251
- name: Prepare artifact folders
3352
run: |
3453
mkdir -p artifacts/test-results
3554
mkdir -p artifacts/packages
3655
mkdir -p artifacts/native
37-
38-
- name: Install dependencies
39-
run: brew install cmake
40-
41-
- name: Build native stub library
42-
run: |
43-
cmake -S native -B native/build -DCMAKE_BUILD_TYPE=Release
44-
cmake --build native/build --target mlxsharp --config Release
45-
cp native/build/libmlxsharp.dylib artifacts/native/
46-
cp native/build/libmlxsharp.dylib src/MLXSharp.Native/runtimes/osx-arm64/native/
47-
48-
- name: Run tests
49-
run: dotnet test MLXSharp.sln --configuration Release --no-build --logger "trx;LogFileName=TestResults.trx" --results-directory artifacts/test-results
56+
cp src/MLXSharp.Native/runtimes/osx-arm64/native/libmlxsharp.dylib artifacts/native/
5057
5158
- name: Pack library
5259
run: dotnet pack src/MLXSharp/MLXSharp.csproj --configuration Release --no-build --output artifacts/packages
5360

61+
- name: Verify package contains native libraries
62+
run: |
63+
echo "Checking package contents..."
64+
if unzip -l artifacts/packages/*.nupkg | grep -q "runtimes/osx-arm64/native/libmlxsharp.dylib"; then
65+
echo "✓ Native library found in package"
66+
else
67+
echo "✗ ERROR: Native library NOT found in package!"
68+
echo "Package contents:"
69+
unzip -l artifacts/packages/*.nupkg
70+
exit 1
71+
fi
72+
5473
- name: Upload native artifact
5574
uses: actions/upload-artifact@v4
5675
with:

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,24 @@ history.AddUserMessage("Summarise MLX in one sentence");
6767
var response = await chat.GetChatMessageContentsAsync(history, new PromptExecutionSettings(), kernel, CancellationToken.None);
6868
```
6969

70-
## Native Runtime Packaging
71-
72-
- `src/MLXSharp.Native` mimics NuGet runtime assets with `runtimes/<rid>/native/libmlxsharp.*` folders.
73-
The Linux x64 directory contains a stub library compiled from `native/src/mlxsharp.cpp` so tests can load it.
74-
- Drop the real macOS build (`libmlxsharp.dylib`) into `runtimes/osx-arm64/native/` before packing a release build.
75-
- At runtime `MlxNativeLibrary` probes the explicit `MlxClientOptions.LibraryPath`, the `MLXSHARP_LIBRARY` environment variable, and packaged runtime folders before falling back to system search paths.
70+
## NuGet Package
71+
72+
**MLXSharp** NuGet package містить:
73+
- Managed DLL з `Microsoft.Extensions.AI` інтеграціями
74+
- Native бібліотеки в `runtimes/{rid}/native/`:
75+
- `runtimes/linux-x64/native/libmlxsharp.so` - stub для CI/testing
76+
- `runtimes/osx-arm64/native/libmlxsharp.dylib` - білдиться в CI на macOS
77+
78+
GitHub Actions автоматично:
79+
1. Компілює native wrapper з MLX submodule
80+
2. Копіює `libmlxsharp.dylib` в `src/MLXSharp.Native/runtimes/osx-arm64/native/`
81+
3. Пакує managed + native разом в один NuGet пакет
82+
83+
Під час виконання `MlxNativeLibrary` автоматично знаходить правильну бібліотеку:
84+
- Перевіряє `MlxClientOptions.LibraryPath`
85+
- Перевіряє `MLXSHARP_LIBRARY` environment variable
86+
- Шукає в `runtimes/{rid}/native/` (NuGet розпаковує автоматично)
87+
- Fallback на system library search paths
7688

7789
To build the native wrapper locally (mirroring the LLamaSharp workflow):
7890

src/MLXSharp.Native/MLXSharp.Native.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<None Include="runtimes/**" Pack="true" PackagePath="runtimes/%(RecursiveDir)%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
16+
<None Include="runtimes/**/*.*"
17+
Pack="true"
18+
PackagePath="runtimes/%(RecursiveDir)%(Filename)%(Extension)"
19+
CopyToOutputDirectory="PreserveNewest"
20+
Link="runtimes/%(RecursiveDir)%(Filename)%(Extension)" />
1721
</ItemGroup>
1822
</Project>

src/MLXSharp/MLXSharp.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616
</ItemGroup>
1717

1818
<ItemGroup>
19-
<ProjectReference Include="..\MLXSharp.Native\MLXSharp.Native.csproj" ReferenceOutputAssembly="false" PrivateAssets="all" />
19+
<ProjectReference Include="..\MLXSharp.Native\MLXSharp.Native.csproj"
20+
ReferenceOutputAssembly="false"
21+
PrivateAssets="all"
22+
IncludeAssets="none" />
23+
</ItemGroup>
24+
25+
<!-- Include native binaries from MLXSharp.Native project into this package -->
26+
<ItemGroup>
27+
<None Include="..\MLXSharp.Native\runtimes\**\*.*"
28+
Pack="true"
29+
PackagePath="runtimes/%(RecursiveDir)%(Filename)%(Extension)"
30+
CopyToOutputDirectory="PreserveNewest"
31+
Link="runtimes\%(RecursiveDir)%(Filename)%(Extension)" />
2032
</ItemGroup>
2133
</Project>

0 commit comments

Comments
 (0)