Skip to content

Commit 871ff49

Browse files
Ejhg osx arm64 lib fix (#21)
* Increase libtorch version to 2.4.0 and use libtorch for osx-arm64 - libtorch requires libomp.dylib, but it is only included with libtorch 2.4.0 and above - compiling osx-arm64 against libtorch-macos-arm64 instead of conda py3.10 libraries source significantly improves performance (~6X on M2 Macbook Air) * Clean up references to unused conda LibTorchArchiveSource conditions * Update SHA hashes for libtorch 2.4.0 on Windows and Linux * Update distribution filenames * Set BuildLibTorchPackages to true to build artifacts in CI * Pin mac-x64 target against latest libtorch 2.2.2 * Specify TargetOS to fix failing CI build * Revert "Specify TargetOS to fix failing CI build" This reverts commit bf723f5. * Specify TargetOS to fix failing CI build for mac-x64 --------- Co-authored-by: Eugenio De Hoyos <[email protected]>
1 parent a926e17 commit 871ff49

15 files changed

+62
-55
lines changed

DEVGUIDE.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Build with
5252

5353
## Packages
5454

55-
An ephemeral feed of packages from Azure DevOps CI is available for those
55+
An ephemeral feed of packages from Azure DevOps CI is available for those
5656

5757
* View link: https://dotnet.visualstudio.com/TorchSharp/_packaging?_a=feed&feed=SignedPackages
5858
* Nuget feed: https://dotnet.pkgs.visualstudio.com/TorchSharp/_packaging/SignedPackages/nuget/v3/index.json
@@ -77,7 +77,7 @@ To change the TorchSharp package version update this [file](https://github.com/d
7777
The TorchSharp package is pushed to nuget.org via Azure DevOps CI release pipeline. Assuming you're not building or updating the LibTorch packages
7878
(`BuildLibTorchPackages` is `false` in [azure-pipelines.yml](azure-pipelines.yml)) this is pretty simple once you have the permissions:
7979

80-
1. Update the version number in [./build/BranchInfo.props](./build/BranchInfo.props) and in the [Release Notes](./RELEASENOTES.md) file and then submit a PR.
80+
1. Update the version number in [./build/BranchInfo.props](./build/BranchInfo.props) and in the [Release Notes](./RELEASENOTES.md) file and then submit a PR.
8181

8282
Updating the major or minor version number should only be done after a discussion with repo admins. The patch number should be incremented by one each release and set to zero after a change to the major or minor version.
8383
2. Integrate code to main and wait for CI to process
@@ -149,7 +149,7 @@ For this reason, we do the following
149149
This project grabs LibTorch and makes a C API wrapper for it, then calls these from C#. When updating to a newer
150150
version of PyTorch then quite a lot of careful work needs to be done.
151151

152-
0. Make sure you have plenty of disk space, e.g. 15GB.
152+
0. Make sure you have plenty of disk space, e.g. 15GB.
153153

154154
1. Clean and reset to main
155155

@@ -163,7 +163,7 @@ version of PyTorch then quite a lot of careful work needs to be done.
163163
https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.2.0%2Bcpu.zip
164164

165165
Don't download anything yet, or manually. The downloads are acquired automatically in step 2.
166-
166+
167167
To update the version, update this in [Dependencies.props](build/Dependencies.props):
168168

169169
<LibTorchVersion>2.2.0</LibTorchVersion>
@@ -179,8 +179,9 @@ version of PyTorch then quite a lot of careful work needs to be done.
179179
On Windows:
180180

181181
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=linux /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
182-
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
183-
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
182+
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:TargetArchitecture=x64 /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
183+
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:TargetArchitecture=arm64 /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
184+
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
184185
dotnet build src\Redist\libtorch-cpu\libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Debug /t:Build /p:IncludeLibTorchCpuPackages=true
185186

186187
dotnet build src\Redist\libtorch-cuda-12.1\libtorch-cuda-12.1.proj /p:UpdateSHA=true /p:TargetOS=linux /p:Configuration=Release /t:Build /p:IncludeLibTorchCudaPackages=true
@@ -190,8 +191,9 @@ On Windows:
190191
On Linux / Mac:
191192

192193
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=linux /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
193-
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
194-
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
194+
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:TargetArchitecture=x64 /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
195+
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:TargetArchitecture=arm64 /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
196+
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Release /t:Build /p:IncludeLibTorchCpuPackages=true
195197
dotnet build src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=windows /p:Configuration=Debug /t:Build /p:IncludeLibTorchCpuPackages=true
196198

197199
dotnet build src/Redist/libtorch-cuda-12.1/libtorch-cuda-12.1.proj /p:UpdateSHA=true /p:TargetOS=linux /p:Configuration=Release /t:Build /p:IncludeLibTorchCudaPackages=true
@@ -213,7 +215,7 @@ On Linux / Mac:
213215
dir bin\obj\x64.Release\libtorch-cpu\libtorch-cxx11-abi-shared-with-deps-2.2.0cpu\libtorch\lib\*.so*
214216

215217
You may also need to precisely refactor the binaries into multiple parts so each package ends up under ~300MB. Before release 2.2.0 of libtorch, this really only affected the CUDA packagages, but it is now also affecting the CPU packages on Linux and OSX. Windows CPU is still small enough to be contained in just one package. The NuGet gallery does not allow packages larger than 250MB, so if files are 300MB, after compression, they are likely to be smaller than 250MB. However, you have to look out: if the compression is poor, then packages may end up larger. Note that it is 250 million
216-
bytes that is the limit, **not** 250*1024*1024. In other words, it is 250 MB, not 250 MiB. Note that Windows Explorer will show file sizes in KiB, not thousands of bytes. Use 'dir' from a CMD window to get the exact size in bytes for each file. For example -- the file `libtorch_cpu.so` shows up as 511,872 KB in Windows Explorer, but 524,156,144 bytes in CMD. The 2.4% difference can be significant. Getting the partitioning right requires precision.
218+
bytes that is the limit, **not** 250*1024*1024. In other words, it is 250 MB, not 250 MiB. Note that Windows Explorer will show file sizes in KiB, not thousands of bytes. Use 'dir' from a CMD window to get the exact size in bytes for each file. For example -- the file `libtorch_cpu.so` shows up as 511,872 KB in Windows Explorer, but 524,156,144 bytes in CMD. The 2.4% difference can be significant. Getting the partitioning right requires precision.
217219

218220
If the combined size of the files going into a part is smaller than 250MB, then everything is fine, and there is no need to split the part. It can be singular. If that is not the case, then the part should be fragmented into two or more parts that are linked together by their names.
219221

@@ -229,12 +231,12 @@ On Linux / Mac:
229231
They must all be called either 'primary,' which should be the first fragment, or 'fragmentN' where 'N' is the ordinal number of the fragment, starting with '1'. The current logic allows for as many as 10 non-primary fragments. If more are needed, the code in [FileRestitcher.cs](pkg/FileRestitcher/FileRestitcher/FileRestitcher.cs) and [RestitchPackage.targets](pkg/common/RestitchPackage.targets) needs to be updated. Note that the size of each fragment is expressed in bytes, and that fragment start must be
230232
the sum of the size of all previous fragments. A '-1' should be used for the last fragment (and only for the last fragment): it means that the fragment size will be based on how much there is still left of the file.
231233

232-
Each part, whether singular or fragmented, should have its own .nupkgproj file in its own folder under pkg. The folder and file should have the same name as the part. If you need to add new fragments, it is straightforward to just copy an existing fragment folder and rename it as well as the project file to the new fragment.
233-
234+
Each part, whether singular or fragmented, should have its own .nupkgproj file in its own folder under pkg. The folder and file should have the same name as the part. If you need to add new fragments, it is straightforward to just copy an existing fragment folder and rename it as well as the project file to the new fragment.
235+
234236
__Important:__
235237

236-
If you must fragment a previously singular part, it is best to rename the existing folder and file to '-fragment1' and then copy a '-primary' folder and rename with the right part name. This is because the primary .nupkgproj files look different from others.
237-
238+
If you must fragment a previously singular part, it is best to rename the existing folder and file to '-fragment1' and then copy a '-primary' folder and rename with the right part name. This is because the primary .nupkgproj files look different from others.
239+
238240
Specifically, they include different build targets:
239241

240242
```xml
@@ -295,8 +297,8 @@ On Linux / Mac:
295297

296298
<LibTorchPackageVersion>2.0.1.1</LibTorchPackageVersion>
297299

298-
dotnet pack -c Release -v:n /p:SkipNative=true /p:SkipTests=true /p:IncludeTorchSharpPackage=true /p:IncludeLibTorchCpuPackages=true /p:IncludeLibTorchCudaPackages=true
299-
dotnet pack -c Release -v:n /p:SkipNative=true /p:SkipTests=true /p:TargetOS=linux /p:IncludeTorchSharpPackage=true /p:IncludeLibTorchCpuPackages=true /p:IncludeLibTorchCudaPackages=true
300+
dotnet pack -c Release -v:n /p:SkipNative=true /p:SkipTests=true /p:IncludeTorchSharpPackage=true /p:IncludeLibTorchCpuPackages=true /p:IncludeLibTorchCudaPackages=true
301+
dotnet pack -c Release -v:n /p:SkipNative=true /p:SkipTests=true /p:TargetOS=linux /p:IncludeTorchSharpPackage=true /p:IncludeLibTorchCpuPackages=true /p:IncludeLibTorchCudaPackages=true
300302

301303
Once these finish, the output can be found in `bin\packages\Release`. Look at the file sizes -- if anything is larger than 250,000,000 bytes, you need to go back to #3 above and redefine the package contents and fragmentation scheme. It maybe necessary to introduce new fragments.
302304

Directory.Build.props

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<SourceDir>$(RepoRoot)src/</SourceDir>
2121
<PkgDir>$(RepoRoot)pkg/</PkgDir>
2222

23-
<LibTorchPackageVersion>2.2.1.1</LibTorchPackageVersion>
23+
<LibTorchPackageVersion>2.4.0.0</LibTorchPackageVersion>
24+
<LibTorchPackageVersion Condition="'$(TargetOS)' == 'mac' and '$(TargetArchitecture)' == 'x64'">2.2.2.0</LibTorchPackageVersion>
2425

2526
<!-- when building on local machines the massive downloads get placed up one directory -->
2627
<!-- so we can clean without triggering a re-download-->
@@ -86,7 +87,8 @@
8687
<!-- use stable versions for libtorch packages based on LibTorch version number scheme-->
8788
<!-- we manually update these -->
8889
<PropertyGroup Condition="'$(MSBuildProjectName.IndexOf(`libtorch-`))' != '-1'">
89-
<LibTorchPackageVersion>2.2.1.1</LibTorchPackageVersion>
90+
<LibTorchPackageVersion>2.4.0.0</LibTorchPackageVersion>
91+
<LibTorchPackageVersion Condition="'$(TargetOS)' == 'mac' and '$(TargetArchitecture)' == 'x64'">2.2.2.0</LibTorchPackageVersion>
9092
<EnablePackageValidation>false</EnablePackageValidation>
9193
<VersionPrefix>$(LibTorchPackageVersion)</VersionPrefix>
9294
<VersionSuffix></VersionSuffix>
@@ -135,17 +137,14 @@
135137

136138
<PropertyGroup>
137139
<LibTorchArchiveSource>pytorch</LibTorchArchiveSource>
138-
<LibTorchArchiveSource Condition="'$(TargetPlatform)' == 'mac-arm64'">conda</LibTorchArchiveSource>
139-
<CondaArchivePlatformName Condition="'$(TargetPlatform)' == 'mac-arm64'">osx-arm64</CondaArchivePlatformName>
140140
<LibTorchCpuArchiveNameSuffix Condition="'$(TargetOS)' != 'mac'">%252Bcpu</LibTorchCpuArchiveNameSuffix>
141-
<LibTorchCpuArchiveNameSuffix Condition="'$(LibTorchArchiveSource)' == 'conda'">-py3.10_0</LibTorchCpuArchiveNameSuffix>
142141
<LibTorchCudaArchiveNameSuffix>%252Bcu$(CudaVersionNoDot)</LibTorchCudaArchiveNameSuffix>
143142
<LibTorchCpuLocalNameSuffix>cpu</LibTorchCpuLocalNameSuffix>
144143
<LibTorchCudaLocalNameSuffix>cu$(CudaVersionNoDot)</LibTorchCudaLocalNameSuffix>
145144
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'windows'">libtorch-win-shared-with-deps$(LibTorchDebug)</LibTorchArchiveCoreName>
146145
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'linux'">libtorch-cxx11-abi-shared-with-deps</LibTorchArchiveCoreName>
147146
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'mac'">libtorch-macos-x86_64</LibTorchArchiveCoreName>
148-
<LibTorchArchiveCoreName Condition="'$(LibTorchArchiveSource)' == 'conda'">pytorch</LibTorchArchiveCoreName>
147+
<LibTorchArchiveCoreName Condition="'$(TargetPlatform)' == 'mac-arm64'">libtorch-macos-arm64</LibTorchArchiveCoreName>
149148
<LibTorchCpuArchiveBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCpuArchiveNameSuffix)</LibTorchCpuArchiveBase>
150149
<LibTorchCudaArchiveBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCudaArchiveNameSuffix)</LibTorchCudaArchiveBase>
151150
<LibTorchCpuLocalBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCpuLocalNameSuffix)</LibTorchCpuLocalBase>

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ parameters:
99
# push them to the artifacts feed of the Azure CI project
1010
- name: BuildLibTorchPackages
1111
type: boolean
12-
default: false
12+
default: true
1313
- name: PushPackagesToFeed
1414
type: boolean
1515
default: false
@@ -78,7 +78,7 @@ jobs:
7878
parameters:
7979
prepScript: echo "no prep needed"
8080
name: MacOS_x64
81-
buildScript: dotnet build /p:SkipCuda=true /p:TargetArchitecture=x64 -c
81+
buildScript: dotnet build /p:SkipCuda=true /p:TargetOS=mac /p:TargetArchitecture=x64 -c
8282
testScript: dotnet test --blame /p:SkipCuda=true --blame test/TorchSharpTest/TorchSharpTest.csproj -c
8383
pool:
8484
vmImage: 'macos-latest'

build/Dependencies.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
<!-- Other/Non-Core Product Dependencies -->
99
<PropertyGroup>
10-
<LibTorchVersion>2.2.1</LibTorchVersion>
10+
<LibTorchVersion>2.4.0</LibTorchVersion>
11+
<LibTorchVersion Condition="'$(TargetArchitecture)' == 'x64' and '$(TargetOS)' == 'mac'">2.2.2</LibTorchVersion>
1112
<CudaVersionDot>12.1</CudaVersionDot>
1213
<CudaVersionNoDot>121</CudaVersionNoDot>
1314
<MklDnnVersion>2019.0.5.20190502</MklDnnVersion>

src/Redist/libtorch-cpu/libtorch-cpu.proj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<MainPackageFolder>$(PackagePreparationPath)$(MSBuildProjectName)-$(TargetRuntimeID)</MainPackageFolder>
3030
<CopyToPackageSemaphore>$(MainPackageFolder)\.copied.SkipTests.$(SkipTests).IncludeLibTorchCpuPackages.$(IncludeLibTorchCpuPackages)</CopyToPackageSemaphore>
3131
</PropertyGroup>
32-
32+
3333
<ItemGroup Condition="'$(TargetOS)' == 'windows'">
3434
<File Include="libtorch\lib\asmjit.dll" />
3535
<File Include="libtorch\lib\c10.dll" />
@@ -60,6 +60,8 @@
6060
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'arm64' and '$(TargetOS)' == 'mac'">
6161
<File Include="libtorch\lib\libc10.dylib" />
6262
<File Include="libtorch\lib\libshm.dylib" />
63+
<!-- libomp.dylib is required for libtorch, but it is only bundled with libtorch 2.4.0 and above. -->
64+
<File Include="libtorch\lib\libomp.dylib"/>
6365
<File Include="libtorch\lib\libtorch.dylib" />
6466
<File Include="libtorch\lib\libtorch_cpu.dylib" />
6567
<File Include="libtorch\lib\libtorch_global_deps.dylib" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9D16CC0DA41E057F20C0BE5F26D7418F969E857631CFCB86550CCDECFEE8DE60
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5B42BEDB9EE7BD42C7C72A0449A46985483A15F5A7C30FD42408A040F10EF104
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
03A8066EE79BAEF6225BC3665FD3B0B7C4C11AC024086457974751BEBDFE43C1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
D9C6290583680F9B850884AFE92978FA8C567FED079C8AB54F54B16E27CE04B5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
B01B5E57E772075DBC2034A0046D17F9A503EC5D0B4A6EE15A83DC42EE71C329

0 commit comments

Comments
 (0)