Skip to content
Closed
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
2 changes: 2 additions & 0 deletions DEVGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Requirements:
Build with

dotnet build /p:SkipNative=true

By default, this will build for your system's architecture (x64 vs arm64/M1). To override this, add `/p:TargetArchitecture=x64` or `/p:TargetArchitecture=arm64` to your build command.

## Packages

Expand Down
13 changes: 11 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<PropertyGroup>
<Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
<Configurations>Debug;Release</Configurations>
<Platform Condition="'$(Platform)'==''">x64</Platform>
<TargetArchitecture Condition="'$(TargetArchitecture)' == ''">x64</TargetArchitecture>
<_DefaultArchitecture>$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLower())</_DefaultArchitecture>
<Platform Condition="'$(Platform)'==''">AnyCPU</Platform>
<TargetArchitecture Condition="'$(TargetArchitecture)' == ''">$(_DefaultArchitecture)</TargetArchitecture>
<NativeTargetArchitecture Condition="'$(NativeTargetArchitecture)' == ''">$(TargetArchitecture)</NativeTargetArchitecture>
<PlatformConfig>$(Platform).$(Configuration)</PlatformConfig>
</PropertyGroup>
Expand Down Expand Up @@ -50,9 +51,12 @@
<TargetOS Condition="'$(TargetOS)' == '' AND $([MSBuild]::IsOSPlatform('osx'))">mac</TargetOS>
<TargetOS Condition="'$(TargetOS)' == '' AND '$(OS)' != 'Windows_NT'">linux</TargetOS>

<TargetPlatform Condition="'$(TargetPlatform)' == ''">$(TargetOS)-$(TargetArchitecture)</TargetPlatform>

<TargetRuntimeID Condition="'$(TargetOS)' == 'windows'">win-x64</TargetRuntimeID>
<TargetRuntimeID Condition="'$(TargetOS)' == 'linux'">linux-x64</TargetRuntimeID>
<TargetRuntimeID Condition="'$(TargetOS)' == 'mac'">osx-x64</TargetRuntimeID>
<TargetRuntimeID Condition="'$(TargetPlatform)' == 'mac-arm64'">osx-arm64</TargetRuntimeID>

<!-- on windows separate debug binaries of LibTorch are available-->
<LibTorchDebug Condition="('$(NativeConfiguration)' == 'Debug') AND '$(TargetOS)' == 'windows'">-debug</LibTorchDebug>
Expand Down Expand Up @@ -125,13 +129,18 @@
</PropertyGroup>

<PropertyGroup>
<LibTorchArchiveSource>pytorch</LibTorchArchiveSource>
<LibTorchArchiveSource Condition="'$(TargetPlatform)' == 'mac-arm64'">conda</LibTorchArchiveSource>
<CondaArchivePlatformName Condition="'$(TargetPlatform)' == 'mac-arm64'">osx-arm64</CondaArchivePlatformName>
<LibTorchCpuArchiveNameSuffix Condition="'$(TargetOS)' != 'mac'">%252Bcpu</LibTorchCpuArchiveNameSuffix>
<LibTorchCpuArchiveNameSuffix Condition="'$(LibTorchArchiveSource)' == 'conda'">-py3.10_0</LibTorchCpuArchiveNameSuffix>
<LibTorchCudaArchiveNameSuffix>%252Bcu$(CudaVersionNoDot)</LibTorchCudaArchiveNameSuffix>
<LibTorchCpuLocalNameSuffix>cpu</LibTorchCpuLocalNameSuffix>
<LibTorchCudaLocalNameSuffix>cu$(CudaVersionNoDot)</LibTorchCudaLocalNameSuffix>
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'windows'">libtorch-win-shared-with-deps$(LibTorchDebug)</LibTorchArchiveCoreName>
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'linux'">libtorch-shared-with-deps</LibTorchArchiveCoreName>
<LibTorchArchiveCoreName Condition="'$(TargetOS)' == 'mac'">libtorch-macos</LibTorchArchiveCoreName>
<LibTorchArchiveCoreName Condition="'$(LibTorchArchiveSource)' == 'conda'">pytorch</LibTorchArchiveCoreName>
<LibTorchCpuArchiveBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCpuArchiveNameSuffix)</LibTorchCpuArchiveBase>
<LibTorchCudaArchiveBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCudaArchiveNameSuffix)</LibTorchCudaArchiveBase>
<LibTorchCpuLocalBase>$(LibTorchArchiveCoreName)-$(LibTorchVersion)$(LibTorchCpuLocalNameSuffix)</LibTorchCpuLocalBase>
Expand Down
17 changes: 13 additions & 4 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<!-- We copy over the LibTorch native binaries in order to run tests. This can probably be automated better. -->
<!-- These lists are duplicated in redist/... with file splitting added for package prep -->
<!-- These lists are duplicated in redist/... with file splitting added for package prep -->

<ItemGroup Condition="'$(TestUsesLibTorch)' == 'true' and '$(SkipNative)' != 'true' ">
<NativeAssemblyReference Include="LibTorchSharp" />
Expand Down Expand Up @@ -62,7 +62,7 @@
<NativeAssemblyReference Include="zlibwapi" Variant="cuda\" />
</ItemGroup>

<!-- Mac libtorch binary list used for examples and testing -->
<!-- Mac x64 libtorch binary list used for examples and testing -->
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'x64'and $([MSBuild]::IsOSPlatform('osx')) and '$(TestUsesLibTorch)' == 'true' and '$(SkipNative)' != 'true' ">
<NativeAssemblyReference Include="backend_with_compiler" />
<NativeAssemblyReference Include="c10" />
Expand All @@ -78,6 +78,15 @@
<NativeAssemblyReference Include="torchbind_test" />
</ItemGroup>

<!-- Mac arm64 libtorch binary list used for examples and testing -->
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'arm64'and $([MSBuild]::IsOSPlatform('osx')) and '$(TestUsesLibTorch)' == 'true' and '$(SkipNative)' != 'true' ">
<NativeAssemblyReference Include="c10" />
<NativeAssemblyReference Include="shm" />
<NativeAssemblyReference Include="torch" />
<NativeAssemblyReference Include="torch_cpu" />
<NativeAssemblyReference Include="torch_global_deps" />
</ItemGroup>

<!-- Linux CPU libtorch binary list used for examples and testing -->
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'x64' and $([MSBuild]::IsOSPlatform('linux')) and '$(TestUsesLibTorch)' == 'true' and '$(SkipNative)' != 'true' ">
<NativeAssemblyReference Include="backend_with_compiler" />
Expand Down Expand Up @@ -124,5 +133,5 @@
</Copy>

</Target>

</Project>
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ To use TorchSharp, you also need one of the LibTorch backend packages: https://w

* `libtorch-cpu-win-x64` (CPU, Windows)

* `libtorch-cpu-osx-x64` (CPU, OSX)
* `libtorch-cpu-osx-x64` (CPU, OSX-x64)

* `libtorch-cpu` (CPU, references all three, larger download but simpler)
* `libtorch-cpu-osx-arm64` (CPU, OSX-arm64)

* `libtorch-cpu` (CPU, references all four, larger download but simpler)

* `libtorch-cuda-11.7-linux-x64` (CPU/CUDA 11.3, Linux)

Expand Down
101 changes: 85 additions & 16 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ jobs:
pool:
vmImage: 'macos-11'

- template: /build/ci/job-template.yml
parameters:
prepScript: echo "no prep needed"
name: MacOS_arm64
buildScript: dotnet build /p:SkipCuda=true /p:TargetArchitecture=arm64 -c
testScript: echo "Azure Pipelines does not support arm64 yet, can't run tests"
pool:
vmImage: 'macos-11'

################################################################################
# {Build} --> combine --> package to build native bits on multiple OS's
################################################################################
Expand Down Expand Up @@ -166,7 +175,7 @@ jobs:
artifact: WindowsAssets

# ################################################################################
- job: MacOS_Native_Build_For_Packages
- job: MacOS_x64_Native_Build_For_Packages
# ################################################################################
condition: and(ne(variables['system.pullrequest.isfork'], true), eq(variables['build.sourcebranchname'], 'main'))
variables:
Expand All @@ -182,7 +191,7 @@ jobs:
displayName: Download libtorch native binaries

- script: dotnet build -c $(BuildConfig) src/TorchSharp/TorchSharp.csproj /p:SkipCuda=true
displayName: Build mac
displayName: Build mac-x64

- script: dotnet build -c $(BuildConfig) src/TorchVision/TorchVision.csproj /p:SkipCuda=true
displayName: Build TorchVision
Expand All @@ -191,7 +200,35 @@ jobs:
displayName: Build TorchAudio

- publish: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)
artifact: MacAssets
artifact: MacAssets_x64

# ################################################################################
- job: MacOS_arm64_Native_Build_For_Packages
# ################################################################################
condition: and(ne(variables['system.pullrequest.isfork'], true), eq(variables['build.sourcebranchname'], 'main'))
variables:
BuildConfig: Release
OfficialBuildId: $(BUILD.BUILDNUMBER)
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_MULTILEVEL_LOOKUP: 0
pool:
vmImage: 'macos-11'
steps:
- script: dotnet build -c $(BuildConfig) src/Redist/libtorch-cpu/libtorch-cpu.proj /p:UpdateSHA=true /p:TargetOS=mac /p:TargetArchitecture=arm64 /t:Build /p:IncludeLibTorchCpuPackages=true
displayName: Download libtorch native binaries

- script: dotnet build -c $(BuildConfig) src/TorchSharp/TorchSharp.csproj /p:SkipCuda=true /p:TargetArchitecture=arm64
displayName: Build mac-arm64

- script: dotnet build -c $(BuildConfig) src/TorchVision/TorchVision.csproj /p:SkipCuda=true /p:TargetArchitecture=arm64
displayName: Build TorchVision

- script: dotnet build -c $(BuildConfig) src/TorchAudio/TorchAudio.csproj /p:SkipCuda=true /p:TargetArchitecture=arm64
displayName: Build TorchAudio

- publish: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)
artifact: MacAssets_arm64


################################################################################
Expand All @@ -201,7 +238,8 @@ jobs:
dependsOn:
- Linux_Native_Build_For_Packages
- Windows_Native_Build_For_Packages
- MacOS_Native_Build_For_Packages
- MacOS_x64_Native_Build_For_Packages
- MacOS_arm64_Native_Build_For_Packages
variables:
BuildConfig: Release
OfficialBuildId: $(BUILD.BUILDNUMBER)
Expand Down Expand Up @@ -249,36 +287,67 @@ jobs:
- script: rmdir /s /q $(Pipeline.Workspace)\LinuxAssets
displayName: Free up space (LinuxAssets in workspace)

# Download all bits contributing to the packages from the Mac build
# Download all bits contributing to the packages from the Mac x64 build
- download: current
artifact: MacAssets
artifact: MacAssets_x64

- task: CopyFiles@2
displayName: Copy mac native assets (TorchSharp) to correct folder where the bits contributing to the packages are assembled
displayName: Copy mac-x64 native assets (TorchSharp) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets/TorchSharp
sourceFolder: $(Pipeline.Workspace)/MacAssets_x64/TorchSharp
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchSharp

- task: CopyFiles@2
displayName: Copy mac native assets (TorchAudio) to correct folder where the bits contributing to the packages are assembled
displayName: Copy mac-x64 native assets (TorchAudio) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets/TorchAudio
sourceFolder: $(Pipeline.Workspace)/MacAssets_x64/TorchAudio
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchAudio

- task: CopyFiles@2
displayName: Copy mac native assets (TorchVision) to correct folder where the bits contributing to the packages are assembled
displayName: Copy mac-x64 native assets (TorchVision) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets/TorchVision
sourceFolder: $(Pipeline.Workspace)/MacAssets_x64/TorchVision
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchVision

- task: CopyFiles@2
displayName: Copy mac native assets (libtorch-cpu) to correct folder where the bits contributing to the packages are assembled
displayName: Copy mac-x64 native assets (libtorch-cpu) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets/libtorch-cpu-osx-x64
sourceFolder: $(Pipeline.Workspace)/MacAssets_x64/libtorch-cpu-osx-x64
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/libtorch-cpu-osx-x64

- script: rmdir /s /q $(Pipeline.Workspace)\MacAssets
displayName: Free up space (MacAssets in workspace)
- script: rmdir /s /q $(Pipeline.Workspace)\MacAssets_x64
displayName: Free up space (MacAssets_x64 in workspace)

# Download all bits contributing to the packages from the Mac arm64 build
- download: current
artifact: MacAssets_arm64

- task: CopyFiles@2
displayName: Copy mac-arm64 native assets (TorchSharp) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets_arm64/TorchSharp
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchSharp

- task: CopyFiles@2
displayName: Copy mac-arm64 native assets (TorchAudio) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets_arm64/TorchAudio
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchAudio

- task: CopyFiles@2
displayName: Copy mac-arm64 native assets (TorchVision) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets_arm64/TorchVision
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/TorchVision

- task: CopyFiles@2
displayName: Copy mac-arm64 native assets (libtorch-cpu) to correct folder where the bits contributing to the packages are assembled
inputs:
sourceFolder: $(Pipeline.Workspace)/MacAssets_arm64/libtorch-cpu-osx-arm64
targetFolder: $(Build.SourcesDirectory)/bin/obj/packprep/$(BuildConfig)/libtorch-cpu-osx-arm64

- script: rmdir /s /q $(Pipeline.Workspace)\MacAssets_arm64
displayName: Free up space (MacAssets_arm64 in workspace)

- download: current
artifact: WindowsAssets
Expand Down
16 changes: 16 additions & 0 deletions pkg/libtorch-cpu-osx-arm64/libtorch-cpu-osx-arm64.nupkgproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Pack">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
</ItemGroup>

<ItemGroup>
<Content Include="..\common\CpuHeadPackage.props" Pack="true" PackagePath="buildTransitive\netstandard2.0\$(MSBuildProjectName).props" />
<Content Include="..\common\CpuHeadPackage.targets" Pack="true" PackagePath="buildTransitive\netstandard2.0\$(MSBuildProjectName).targets" />
<Content Include="..\empty.txt" Pack="true" PackagePath="lib\netstandard2.0\_._" />
<Content Include="$(RepoRoot)\THIRD-PARTY-NOTICES.txt" Pack="true" PackagePath="LICENSE-LIBTORCH.txt" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions pkg/libtorch-cpu/libtorch-cpu.nupkgproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<ProjectReference Include="../libtorch-cpu-linux-x64/libtorch-cpu-linux-x64.nupkgproj" />
<ProjectReference Include="../libtorch-cpu-osx-x64/libtorch-cpu-osx-x64.nupkgproj" />
<ProjectReference Include="../libtorch-cpu-osx-arm64/libtorch-cpu-osx-arm64.nupkgproj" />
<ProjectReference Include="../libtorch-cpu-win-x64/libtorch-cpu-win-x64.nupkgproj" />
</ItemGroup>

Expand Down
4 changes: 0 additions & 4 deletions src/Examples.Utils/Examples.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<TargetFrameworks Condition="'$(TargetFrameworks)' == ''">net6.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<PackageReference Include="System.Reflection.Metadata" Version="6.0.0" />
<Compile Include="$(MSBuildThisFileDirectory)..\..\src\TorchSharp\netstandard.cs" />
Expand Down
1 change: 0 additions & 1 deletion src/Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<UseStyleCopAnalyzer>false</UseStyleCopAnalyzer>
<StartupObject>TorchSharp.Examples.Program</StartupObject>
<IsPackable>false</IsPackable>
<PlatformTarget>x64</PlatformTarget>
<RootNamespace>TorchSharp.Examples</RootNamespace>
</PropertyGroup>

Expand Down
11 changes: 10 additions & 1 deletion src/Native/LibTorchSharp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project(LibTorchSharp)

if(APPLE)
if(APPLE AND NOT LIBTORCH_ARCH STREQUAL "arm64")
include_directories("/usr/local/include" "/usr/local/opt/llvm/include")
link_directories("/usr/local/lib" "/usr/local/opt/llvm/lib")
endif()
Expand Down Expand Up @@ -56,6 +56,15 @@ if(NOT WIN32)
endif()
endif()

# Enable cross compilation for arm64/x64 on macOS
if(APPLE)
if(LIBTORCH_ARCH STREQUAL "arm64")
set(CMAKE_OSX_ARCHITECTURES "arm64")
else()
set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif()
endif()

# Add libTorch bindings
include_directories(${TORCH_INCLUDE_DIRS})

Expand Down
3 changes: 3 additions & 0 deletions src/Native/LibTorchSharp/crc32c.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@
// Per #2 in the copyright notice above:
//
// The definition of 'NONMINMAX' below was altered from the original.
// cpuid.h and x86intrin.h are only included if we're on x86 or x64, fixing arm builds.

//#define NOMINMAX

#ifdef CRC32C_GCC
#if defined(__x86_64__) || defined(__i386__)
#include <cpuid.h>
#include <x86intrin.h>
#endif
Comment on lines +38 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe affect to tensorboard feature?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea about how crc32c.c works so @NiklasGustafsson Can you look at this?

Copy link
Author

@FancyFurret FancyFurret Feb 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, HW support was removed from crc32c in #2877164, so this shouldn't affect anything. I believe cpuid/x86intrin are only needed for the hardware crc instruction, which is no longer being used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, how about just remove that include lines?

#else
#include <intrin.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/Native/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ while [ "$1" != "" ]; do
done

# Force the build to be release since libtorch is in release.
__cmake_defines="-DCMAKE_BUILD_TYPE=${__configuration} ${__strip_argument} -DLIBTORCH_PATH=${__libtorchpath}"
__cmake_defines="-DCMAKE_BUILD_TYPE=${__configuration} ${__strip_argument} -DLIBTORCH_PATH=${__libtorchpath} -DLIBTORCH_ARCH=${__build_arch}"

__IntermediatesDir="$__baseIntermediateOutputPath/$__build_arch.$__configuration/Native"
__BinDir="$__rootBinPath/$__build_arch.$__configuration/Native"
Expand Down
Loading