Skip to content

Commit b26c37a

Browse files
committed
build: add native library splitter and adjust directory structure.
1 parent 8208f76 commit b26c37a

34 files changed

+243
-0
lines changed

TensorFlow.NET.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.UnitTest.RedistH
3737
EndProject
3838
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.CodeGen", "Tensorflow.CodeGen\Tensorflow.CodeGen.csproj", "{BADBB104-2F03-4824-A249-803A871D8122}"
3939
EndProject
40+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensorflow.Redist.NativeLibrarySplitter", "NativeLibrarySplitter\Tensorflow.Redist.NativeLibrarySplitter.csproj", "{B85FA7C7-1E8D-4567-B3F4-605955557DAE}"
41+
EndProject
4042
Global
4143
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4244
Debug|Any CPU = Debug|Any CPU
@@ -302,6 +304,24 @@ Global
302304
{BADBB104-2F03-4824-A249-803A871D8122}.Release|x64.Build.0 = Release|Any CPU
303305
{BADBB104-2F03-4824-A249-803A871D8122}.Release|x86.ActiveCfg = Release|Any CPU
304306
{BADBB104-2F03-4824-A249-803A871D8122}.Release|x86.Build.0 = Release|Any CPU
307+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
308+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
309+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|x64.ActiveCfg = Debug|Any CPU
310+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|x64.Build.0 = Debug|Any CPU
311+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|x86.ActiveCfg = Debug|Any CPU
312+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Debug|x86.Build.0 = Debug|Any CPU
313+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
314+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|Any CPU.Build.0 = Debug|Any CPU
315+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|x64.ActiveCfg = Debug|Any CPU
316+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|x64.Build.0 = Debug|Any CPU
317+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|x86.ActiveCfg = Debug|Any CPU
318+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.GPU|x86.Build.0 = Debug|Any CPU
319+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
320+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|Any CPU.Build.0 = Release|Any CPU
321+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|x64.ActiveCfg = Release|Any CPU
322+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|x64.Build.0 = Release|Any CPU
323+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|x86.ActiveCfg = Release|Any CPU
324+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE}.Release|x86.Build.0 = Release|Any CPU
305325
EndGlobalSection
306326
GlobalSection(SolutionProperties) = preSolution
307327
HideSolutionNode = FALSE
@@ -321,6 +341,7 @@ Global
321341
{7DEA8760-E401-4872-81F3-405F185A13A0} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD}
322342
{62D543A2-8846-45A3-829B-5754B094A8E2} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
323343
{BADBB104-2F03-4824-A249-803A871D8122} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
344+
{B85FA7C7-1E8D-4567-B3F4-605955557DAE} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
324345
EndGlobalSection
325346
GlobalSection(ExtensibilityGlobals) = postSolution
326347
SolutionGuid = {2DEAD3CC-486B-4918-A607-50B0DE7B114A}
File renamed without changes.
File renamed without changes.
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+

2+
// =================================================================== //
3+
// This is a tool to split the native .so file of linux gpu library //
4+
// =================================================================== //
5+
6+
using System.Security.Cryptography;
7+
8+
string filename = "libtensorflow.so";
9+
int count = 5;
10+
SplitFile(filename, count);
11+
12+
static void SplitFile(string filename, int count)
13+
{
14+
// 打开读取二进制文件的文件流
15+
using (FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read))
16+
{
17+
long filesize = new FileInfo(filename).Length; // 获取文件大小
18+
long fragmentSize = (long)(filesize / count + 1); // 计算每个分片的大小
19+
20+
byte[] buffer = new byte[fragmentSize]; // 设置缓冲区大小
21+
int bytesRead; // 存储读取长度
22+
int fragmentIndex = 1; // 分片计数器
23+
24+
// 使用循环遍历分片并写入相应的文件
25+
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
26+
{
27+
string outputFileName = $"{filename}.fragment{fragmentIndex++}";
28+
using (FileStream output = new FileStream(outputFileName, FileMode.Create, FileAccess.Write))
29+
{
30+
output.Write(buffer, 0, bytesRead);
31+
}
32+
}
33+
34+
// 计算整个文件的 SHA-256 哈希值并写入 .sha 文件
35+
using (SHA256 sha256Hash = SHA256.Create())
36+
{
37+
input.Seek(0, SeekOrigin.Begin);
38+
byte[] hashValue = sha256Hash.ComputeHash(input);
39+
40+
string shaFileName = $"{filename}.sha";
41+
using (StreamWriter writer = new StreamWriter(shaFileName, false))
42+
{
43+
writer.Write(BitConverter.ToString(hashValue).Replace("-", ""));
44+
}
45+
}
46+
}
47+
}
48+
49+
// Resume the file from fregments. Thanks for the code in TorchSharp!
50+
static void Restitch(string RestitcherPackage)
51+
{
52+
// !!!!!!!------------------------------NOTE------------------------------------!!!!!!
53+
// !!!!!!! This code is manually copied into pkg\common\RestitchPackage.targets !!!!!!
54+
// !!!!!!!------------------------------NOTE------------------------------------!!!!!!
55+
//
56+
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvv START HERE vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
57+
try
58+
{
59+
if (Directory.Exists(RestitcherPackage))
60+
{
61+
using (var writer = File.CreateText("obj/tensorflow_redist_build_log.txt"))
62+
{
63+
foreach (var p in Directory.EnumerateFiles(RestitcherPackage, "*", SearchOption.AllDirectories))
64+
{
65+
66+
var primaryFile = Path.GetFullPath(p);
67+
writer.WriteLine("Found primary file at {0}", primaryFile);
68+
69+
// See if there are fragments in the parallel nuget packages. If the primary is
70+
// some-package-primary\runtimes\....\a.so
71+
// some-package-primary\runtimes\....\a.so.sha
72+
// then the expected fragments are
73+
// some-package-fragment1\fragments\....\a.so
74+
// some-package-fragment2\fragments\....\a.so
75+
// some-package-fragment3\fragments\....\a.so
76+
// some-package-fragment4\fragments\....\a.so
77+
// some-package-fragment5\fragments\....\a.so
78+
// some-package-fragment6\fragments\....\a.so
79+
// some-package-fragment7\fragments\....\a.so
80+
// some-package-fragment8\fragments\....\a.so
81+
// some-package-fragment9\fragments\....\a.so
82+
// some-package-fragment10\fragments\....\a.so
83+
var shaFile = primaryFile + ".sha";
84+
var fragmentFile1 = primaryFile.Replace("-primary", "-fragment1").Replace("runtimes", "fragments") + ".fragment1";
85+
var fragmentFile2 = primaryFile.Replace("-primary", "-fragment2").Replace("runtimes", "fragments") + ".fragment2";
86+
var fragmentFile3 = primaryFile.Replace("-primary", "-fragment3").Replace("runtimes", "fragments") + ".fragment3";
87+
var fragmentFile4 = primaryFile.Replace("-primary", "-fragment4").Replace("runtimes", "fragments") + ".fragment4";
88+
var fragmentFile5 = primaryFile.Replace("-primary", "-fragment5").Replace("runtimes", "fragments") + ".fragment5";
89+
90+
91+
if (File.Exists(fragmentFile1)) writer.WriteLine("Found fragment file at {0}", fragmentFile1);
92+
if (File.Exists(fragmentFile2)) writer.WriteLine("Found fragment file at {0}", fragmentFile2);
93+
if (File.Exists(fragmentFile3)) writer.WriteLine("Found fragment file at {0}", fragmentFile3);
94+
if (File.Exists(fragmentFile4)) writer.WriteLine("Found fragment file at {0}", fragmentFile4);
95+
if (File.Exists(fragmentFile5)) writer.WriteLine("Found fragment file at {0}", fragmentFile5);
96+
97+
if (File.Exists(fragmentFile1))
98+
{
99+
var tmpFile = Path.GetTempFileName();
100+
101+
{
102+
writer.WriteLine("Writing restored primary file at {0}", tmpFile);
103+
using (var os = File.OpenWrite(tmpFile))
104+
{
105+
106+
//writer.WriteLine("Writing bytes from {0} to {1}", primaryFile, tmpFile);
107+
//var primaryBytes = File.ReadAllBytes(primaryFile);
108+
109+
//os.Write(primaryBytes, 0, primaryBytes.Length);
110+
if (File.Exists(fragmentFile1))
111+
{
112+
writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile1, tmpFile);
113+
var fragmentBytes1 = File.ReadAllBytes(fragmentFile1);
114+
os.Write(fragmentBytes1, 0, fragmentBytes1.Length);
115+
}
116+
if (File.Exists(fragmentFile2))
117+
{
118+
writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile2, tmpFile);
119+
var fragmentBytes2 = File.ReadAllBytes(fragmentFile2);
120+
os.Write(fragmentBytes2, 0, fragmentBytes2.Length);
121+
}
122+
if (File.Exists(fragmentFile3))
123+
{
124+
writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile3, tmpFile);
125+
var fragmentBytes3 = File.ReadAllBytes(fragmentFile3);
126+
os.Write(fragmentBytes3, 0, fragmentBytes3.Length);
127+
}
128+
if (File.Exists(fragmentFile4))
129+
{
130+
writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile4, tmpFile);
131+
var fragmentBytes4 = File.ReadAllBytes(fragmentFile4);
132+
os.Write(fragmentBytes4, 0, fragmentBytes4.Length);
133+
}
134+
if (File.Exists(fragmentFile5))
135+
{
136+
writer.WriteLine("Writing fragment bytes from {0} to {1}", fragmentFile5, tmpFile);
137+
var fragmentBytes5 = File.ReadAllBytes(fragmentFile5);
138+
os.Write(fragmentBytes5, 0, fragmentBytes5.Length);
139+
}
140+
}
141+
}
142+
143+
var shaExpected = File.Exists(shaFile) ? File.ReadAllText(shaFile).ToUpper() : "";
144+
writer.WriteLine($"real sha: {shaExpected}");
145+
146+
using (var sha256Hash = System.Security.Cryptography.SHA256.Create())
147+
{
148+
using (var os2 = File.OpenRead(tmpFile))
149+
{
150+
151+
byte[] bytes = sha256Hash.ComputeHash(os2);
152+
var builder = new System.Text.StringBuilder();
153+
for (int i = 0; i < bytes.Length; i++)
154+
{
155+
builder.Append(bytes[i].ToString("x2"));
156+
}
157+
var shaReconstituted = builder.ToString().ToUpper();
158+
if (shaExpected != shaReconstituted)
159+
{
160+
string msg =
161+
$"Error downloading and reviving packages. Reconsituted file contents have incorrect SHA\n\tExpected SHA: ${shaExpected}\n\tActual SHA: ${shaReconstituted}\n\tFile was reconstituted from:"
162+
+ $"\n\t{primaryFile} (length ${new FileInfo(primaryFile).Length})"
163+
+ (File.Exists(fragmentFile1) ? $"\n\t{fragmentFile1} (length ${new FileInfo(fragmentFile1).Length})" : "")
164+
+ (File.Exists(fragmentFile2) ? $"\n\t{fragmentFile2} (length ${new FileInfo(fragmentFile2).Length})" : "")
165+
+ (File.Exists(fragmentFile3) ? $"\n\t{fragmentFile3} (length ${new FileInfo(fragmentFile3).Length})" : "")
166+
+ (File.Exists(fragmentFile4) ? $"\n\t{fragmentFile4} (length ${new FileInfo(fragmentFile4).Length})" : "")
167+
+ (File.Exists(fragmentFile5) ? $"\n\t{fragmentFile5} (length ${new FileInfo(fragmentFile5).Length})" : "");
168+
writer.WriteLine(msg);
169+
throw new Exception(msg);
170+
}
171+
}
172+
173+
}
174+
175+
writer.WriteLine("Deleting {0}", primaryFile);
176+
File.Delete(primaryFile);
177+
if (File.Exists(primaryFile))
178+
throw new Exception("wtf?");
179+
180+
writer.WriteLine("Moving {0} --> {1}", tmpFile, primaryFile);
181+
File.Move(tmpFile, primaryFile);
182+
183+
writer.WriteLine("Deleting {0}", fragmentFile1);
184+
File.Delete(fragmentFile1); // free up space and prevent us doing this again
185+
186+
writer.WriteLine("Deleting {0}", fragmentFile2);
187+
if (File.Exists(fragmentFile2))
188+
File.Delete(fragmentFile2); // free up space and prevent us doing this again
189+
190+
writer.WriteLine("Deleting {0}", fragmentFile3);
191+
if (File.Exists(fragmentFile3))
192+
File.Delete(fragmentFile3); // free up space and prevent us doing this again
193+
194+
writer.WriteLine("Deleting {0}", fragmentFile4);
195+
if (File.Exists(fragmentFile4))
196+
File.Delete(fragmentFile4); // free up space and prevent us doing this again
197+
198+
writer.WriteLine("Deleting {0}", fragmentFile5);
199+
if (File.Exists(fragmentFile5))
200+
File.Delete(fragmentFile5); // free up space and prevent us doing this again
201+
}
202+
}
203+
}
204+
}
205+
}
206+
catch (Exception ex)
207+
{
208+
Console.Error.WriteLine(ex.ToString());
209+
Console.Error.WriteLine(ex.StackTrace);
210+
}
211+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ END HERE^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
212+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
File renamed without changes.

0 commit comments

Comments
 (0)