forked from dotnet/TorchSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libtorch-cpu.proj
226 lines (207 loc) · 13.8 KB
/
libtorch-cpu.proj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<Project>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Directory.Build.props))\Directory.Build.props" />
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!-- see https://pytorch.org/get-started/locally/ for download links -->
<!-- For example Linux, LibTorch 1.9.0 https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-1.9.0%2Bcpu.zip -->
<!-- note the very odd characters %2Bcpu in the LibTorch download name. The % is encoded via %25 -->
<LocalBaseName>$(LibTorchCpuLocalBase)</LocalBaseName>
<ArchiveBaseName>$(LibTorchCpuArchiveBase)</ArchiveBaseName>
<ArchiveSource>$(LibTorchArchiveSource)</ArchiveSource>
<ArchiveExtension>.zip</ArchiveExtension>
<ArchiveUrl>https://download.pytorch.org/libtorch/cpu/$(ArchiveBaseName)$(ArchiveExtension)</ArchiveUrl>
<ArchiveExtension Condition="'$(ArchiveSource)' == 'conda'">.tar.bz2</ArchiveExtension>
<ArchiveUrl Condition="'$(ArchiveSource)' == 'conda'">
https://conda.anaconda.org/pytorch/$(CondaArchivePlatformName)/$(ArchiveBaseName)$(ArchiveExtension)
</ArchiveUrl>
<DownloadedArchiveFolder>$(MassiveDownloadRoot)$(MSBuildProjectName)\</DownloadedArchiveFolder>
<DownloadedArchiveFileName>$(ArchiveBaseName)$(ArchiveExtension)</DownloadedArchiveFileName>
<DownloadedArchive>$(DownloadedArchiveFolder)$(DownloadedArchiveFileName)</DownloadedArchive>
<ArchiveShaFile>$(IntermediateOutputPath)$(ArchiveBaseName)$(ArchiveExtension).sha</ArchiveShaFile>
<ExtractDirectory>$(IntermediateOutputPath)$(LocalBaseName)</ExtractDirectory>
<ExtractSemaphore>$(ExtractDirectory)\.extracted</ExtractSemaphore>
<LocalShaFile>$(MSBuildProjectDirectory)\$(ArchiveBaseName)$(ArchiveExtension).sha</LocalShaFile>
<LicenseUrl>https://raw.githubusercontent.com/pytorch/pytorch/master/LICENSE</LicenseUrl>
<DownloadedLicenseFolder>$(IntermediateOutputPath)</DownloadedLicenseFolder>
<DownloadedLicenseFileName>LICENSE-LIBTORCH</DownloadedLicenseFileName>
<DownloadedLicenseFile>$(DownloadedLicenseFolder)$(DownloadedLicenseFileName)</DownloadedLicenseFile>
<MainPackageFolder>$(PackagePreparationPath)$(MSBuildProjectName)-$(TargetRuntimeID)</MainPackageFolder>
<CopyToPackageSemaphore>$(MainPackageFolder)\.copied.SkipTests.$(SkipTests).IncludeLibTorchCpuPackages.$(IncludeLibTorchCpuPackages)</CopyToPackageSemaphore>
</PropertyGroup>
<ItemGroup Condition="'$(TargetOS)' == 'windows'">
<File Include="libtorch\lib\asmjit.dll" />
<File Include="libtorch\lib\c10.dll" />
<File Include="libtorch\lib\fbgemm.dll" />
<File Include="libtorch\lib\fbjni.dll" />
<File Include="libtorch\lib\libiomp5md.dll" />
<File Include="libtorch\lib\libiompstubs5md.dll" />
<File Include="libtorch\lib\pytorch_jni.dll" />
<File Include="libtorch\lib\torch.dll" />
<File Include="libtorch\lib\torch_cpu.dll" />
<File Include="libtorch\lib\torch_global_deps.dll" />
<File Include="libtorch\lib\uv.dll" />
</ItemGroup>
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'x64' and '$(TargetOS)' == 'mac'">
<File Include="libtorch\lib\libbackend_with_compiler.dylib" />
<File Include="libtorch\lib\libc10.dylib" />
<File Include="libtorch\lib\libfbjni.dylib" />
<File Include="libtorch\lib\libiomp5.dylib" />
<File Include="libtorch\lib\libjitbackend_test.dylib" />
<File Include="libtorch\lib\libpytorch_jni.dylib" />
<File Include="libtorch\lib\libshm.dylib" />
<File Include="libtorch\lib\libtorch.dylib" />
<File Include="libtorch\lib\libtorch_cpu.dylib" />
<File Include="libtorch\lib\libtorchbind_test.dylib" />
<File Include="libtorch\lib\libtorch_global_deps.dylib" />
<File Include="libtorch\lib\libtorch_python.dylib" />
</ItemGroup>
<ItemGroup Condition="'$(NativeTargetArchitecture)' == 'arm64' and '$(TargetOS)' == 'mac'">
<File Include="libtorch\lib\libc10.dylib" />
<File Include="libtorch\lib\libshm.dylib" />
<File Include="libtorch\lib\libtorch.dylib" />
<File Include="libtorch\lib\libtorch_cpu.dylib" />
<File Include="libtorch\lib\libtorch_global_deps.dylib" />
<File Include="libtorch\lib\libtorch_python.dylib" />
</ItemGroup>
<ItemGroup Condition="'$(TargetOS)' == 'linux'">
<File Include="libtorch\lib\libbackend_with_compiler.so"/>
<File Include="libtorch\lib\libc10.so"/>
<File Include="libtorch\lib\libgomp-98b21ff3.so.1"/>
<File Include="libtorch\lib\libjitbackend_test.so"/>
<File Include="libtorch\lib\libnnapi_backend.so"/>
<File Include="libtorch\lib\libshm.so"/>
<File Include="libtorch\lib\libtorch.so"/>
<File Include="libtorch\lib\libtorchbind_test.so"/>
<File Include="libtorch\lib\libtorch_global_deps.so"/>
<File Include="libtorch\lib\libtorch_python.so"/>
<File Include= "libtorch\lib\libtorch_cpu.so" />
<File Include= "libtorch\lib\libtorch_cpu.so" />
</ItemGroup>
<Target Name="DownloadArchive" Inputs="..\..\..\LICENSE" Outputs="$(DownloadedArchive);$(DownloadedLicenseFile)">
<MakeDir Directories="$(DownloadedArchiveFolder)" />
<MakeDir Directories="$(DownloadedLicenseFolder)" />
<DownloadFile SourceUrl="$(ArchiveUrl)" DestinationFolder="$(DownloadedArchiveFolder)" DestinationFileName="$(DownloadedArchiveFileName)" Condition="!Exists('$(DownloadedArchive)')" />
<DownloadFile SourceUrl="$(LicenseUrl)" DestinationFolder="$(DownloadedLicenseFolder)" DestinationFileName="$(DownloadedLicenseFileName)" Condition="!Exists('$(DownloadedLicense)')" />
</Target>
<!-- Validate and extract Archive for Libtorch -->
<Target Name="ValidateAndExtractArchives" DependsOnTargets="DownloadArchive" Inputs="$(DownloadedArchive)" Outputs="$(ExtractSemaphore)">
<Message Importance="High" Text="GetFileHash $(DownloadedArchive)" />
<GetFileHash Files="$(DownloadedArchive)"> <Output TaskParameter="Hash" PropertyName="DownloadedArchiveHash" /> </GetFileHash>
<WriteLinesToFile File="$(ArchiveShaFile)" Lines="$(DownloadedArchiveHash)" Overwrite="true" />
<!-- If specified we'll update the checked in SHAs with the downloaded ones. -->
<Copy Condition="'$(UpdateSHA)' == 'true'" SourceFiles="$(ArchiveShaFile)" DestinationFiles="$(LocalShaFile)" />
<Error Condition="!Exists('$(LocalShaFile)')" Text="SHA file '$(LocalShaFile)' does not exist. Build with /p:UpdateSHA=true to save it." />
<!-- Read in the SHAs as metadata on the archive items -->
<ItemGroup>
<Archive>
<LocalSha>$([System.IO.File]::ReadAllText('$(LocalShaFile)'))</LocalSha>
<DownloadSha>$([System.IO.File]::ReadAllText('$(ArchiveShaFile)'))</DownloadSha>
</Archive>
</ItemGroup>
<!-- Validate that the downloaded SHAs match the expected checked in SHAs -->
<Error Condition="'%(Archive.LocalSha)' != '%(Archive.DownloadSha)'" Text="Downloaded file '$(DownloadedArchive)' has unexpected SHA.%0A expected: %(Archive.LocalSha)%0A actual: %(Archive.DownloadSha)%0ABuild with /p:UpdateSHA=true if you intentionally changed the URL and wish to update the SHAs, otherwise this could indicate an incomplete download or intercerpted URL and should be examined." />
<!-- The archives are valid, lets extract them, ensuring an empty directory -->
<RemoveDir Directories="$(ExtractDirectory)" />
<MakeDir Directories="$(ExtractDirectory)" />
<Message Importance="High" Text="Unzip $(DownloadedArchive) --> $(ExtractDirectory)" />
<Unzip Condition="'$(ArchiveExtension)' == '.zip'" SourceFiles="$(DownloadedArchive)" DestinationFolder="$(ExtractDirectory)" OverwriteReadOnlyFiles="true" />
<Exec Condition="'$(ArchiveExtension)' == '.tar.gz'"
WorkingDirectory="$(ExtractDirectory)"
Command="tar -xzmf "$([MSBuild]::MakeRelative('$(ExtractDirectory)', '$(DownloadedArchive)'))"" />
<Exec Condition="'$(ArchiveExtension)' == '.tar.bz2'"
WorkingDirectory="$(ExtractDirectory)"
Command="tar -xf "$([MSBuild]::MakeRelative('$(ExtractDirectory)', '$(DownloadedArchive)'))"" />
<WriteLinesToFile File="$(ExtractSemaphore)" Lines="done" Overwrite="true" />
<!-- delete the archive files when running on CI -->
<Message Importance="High" Text="Deleting '$(DownloadedArchive)' to free up space." Condition="'$(TF_BUILD)' != ''" />
<Delete Files="$(DownloadedArchive)" Condition="'$(TF_BUILD)' != ''" />
<!-- If we downloaded from conda instead of pytorch, we need to move it's torch folder to libtorch -->
<Exec Condition="'$(ArchiveSource)' == 'conda'"
WorkingDirectory="$(ExtractDirectory)"
Command="mv lib/python3.10/site-packages/torch libtorch" />
</Target>
<!-- Hack vast files into separate pieces so we can place them in separate nuget packages. -->
<UsingTask
TaskName="FileUnstitch"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<SourceFile ParameterType="System.String" Required="true" />
<DestinationFolder ParameterType="System.String" Required="true" />
<Start ParameterType="System.Int64" Required="true" />
<Size ParameterType="System.Int64" Required="true" />
<Index ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.IO.MemoryMappedFiles"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
{
string name = Path.GetFileName(SourceFile) + ((Start == 0) ? "" : ".fragment" + Index);
if (Size == -1)
Size = Int32.MaxValue;
var fi = new FileInfo(SourceFile);
Console.WriteLine($"Opening source {SourceFile} for fragment {Index}...");
using (var mmf = MemoryMappedFile.CreateFromFile(SourceFile))
{
long totSize = fi.Length;
if (Start != 0)
DestinationFolder = DestinationFolder.Replace("runtimes", "fragments");
Directory.CreateDirectory(DestinationFolder);
string outfile = Path.Combine(DestinationFolder, name);
Size = Math.Max(Math.Min(Size, totSize - Start), 0);
Console.WriteLine($"Reading fragment {Index} ({Start}..{Start + Size - 1}) from {SourceFile}...");
var outBytes = new byte[Size];
using (var accessor = mmf.CreateViewAccessor(Start, Size))
{
accessor.ReadArray<byte>(0, outBytes, 0, (int)Size);
}
Console.WriteLine($"Writing fragment to {outfile}...");
File.WriteAllBytes(outfile, outBytes);
}
}
if (Start == 0)
{
using (var sha256Hash = System.Security.Cryptography.SHA256.Create())
{
using (FileStream stream = File.OpenRead(SourceFile))
{
Console.WriteLine("Computing SHA...");
byte[] bytes = sha256Hash.ComputeHash(stream);
var builder = new System.Text.StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
var sha = builder.ToString();
string shaFileName = Path.Combine(DestinationFolder, Path.GetFileName(SourceFile) + ".sha");
File.WriteAllText(shaFileName, sha);
Console.WriteLine($"Computed SHA {sha}...");
}
}
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="CopyFilesFromArchive" DependsOnTargets="ValidateAndExtractArchives" Inputs="$(ExtractSemaphore)" Outputs="$(CopyToPackageSemaphore)">
<Message Importance="High" Text="Copy @(File) -> $(MainPackageFolder)\runtimes\$(TargetRuntimeID)\native\" />
<!-- Copy to MainPackageFolder to use in libtorch-cpu package build -->
<Copy SourceFiles="$(ExtractDirectory)\%(File.Identity)" DestinationFolder="$(MainPackageFolder)\runtimes\$(TargetRuntimeID)\native\" SkipUnchangedFiles="true" Condition="'$(IncludeLibTorchCpuPackages)' == 'true' AND '%(File.PackageSuffix)' == ''"/>
<Copy SourceFiles="$(ExtractDirectory)\%(File.Identity)" DestinationFolder="$(MainPackageFolder)-%(File.PackageSuffix)\runtimes\$(TargetRuntimeID)\native\" SkipUnchangedFiles="true" Condition="'%(File.FileUnstitchStart)' == '' AND '%(File.PackageSuffix)' != ''"/>
<FileUnstitch SourceFile="$(ExtractDirectory)\%(File.Identity)" DestinationFolder="$(MainPackageFolder)-%(File.PackageSuffix)\runtimes\$(TargetRuntimeID)\native\" Start="%(File.FileUnstitchStart)" Size="%(File.FileUnstitchSize)" Index="%(File.FileUnstitchIndex)" Condition="'%(File.FileUnstitchStart)' != ''" />
<!-- Copy license to MainPackageFolder to use in libtorch-cpu packages build -->
<Copy SourceFiles="$(DownloadedLicenseFile)" DestinationFolder="$(MainPackageFolder)\" SkipUnchangedFiles="true" Condition="'$(IncludeLibTorchCpuPackages)' == 'true'" />
<!-- Copy to NativeOutputPath to use in tests (headers, lib etc.) -->
<MakeDir Directories="$(NativeOutputPath)" Condition="'$(SkipTests)' != 'true'" />
<Copy SourceFiles="$(ExtractDirectory)\%(File.Identity)" DestinationFolder="$(NativeOutputPath)" SkipUnchangedFiles="true" Condition="'$(SkipTests)' != 'true'" />
<WriteLinesToFile File="$(CopyToPackageSemaphore)" Lines="done" Overwrite="true" />
</Target>
<!-- Define Build to do the work of downloading and copying assets -->
<Target Name="Build" DependsOnTargets="CopyFilesFromArchive" />
<Target Name="Clean">
<RemoveDir Directories="$(IntermediateOutputPath)" />
</Target>
</Project>