Skip to content

Commit 2815422

Browse files
Initial Tensorboard support commit.
1 parent 043c389 commit 2815422

39 files changed

+41621
-2
lines changed

src/Native/LibTorchSharp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ find_package(Torch REQUIRED PATHS ${LIBTORCH_PATH})
88

99
set(SOURCES
1010
cifar10.h
11+
crc32c.h
1112
THSAutograd.h
1213
THSData.h
1314
THSJIT.h
@@ -18,6 +19,7 @@ set(SOURCES
1819
THSVision.h
1920
Utils.h
2021
cifar10.cpp
22+
crc32c.c
2123
THSActivation.cpp
2224
THSAutograd.cpp
2325
THSConvolution.cpp

src/Native/LibTorchSharp/crc32c.c

Lines changed: 1097 additions & 0 deletions
Large diffs are not rendered by default.

src/Native/LibTorchSharp/crc32c.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* Part of CRC-32C library: https://crc32c.machinezoo.com/ */
2+
/*
3+
Copyright (c) 2013 - 2014, 2016 Mark Adler, Robert Vazan, Max Vysokikh
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the author be held liable for any damages
7+
arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any purpose,
10+
including commercial applications, and to alter it and redistribute it
11+
freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must not
14+
claim that you wrote the original software. If you use this software
15+
in a product, an acknowledgment in the product documentation would be
16+
appreciated but is not required.
17+
2. Altered source versions must be plainly marked as such, and must not be
18+
misrepresented as being the original software.
19+
3. This notice may not be removed or altered from any source distribution.
20+
*/
21+
22+
#ifndef CRC32C_H
23+
#define CRC32C_H
24+
25+
#if defined(__GNUC__) || defined(__GNUG__)
26+
#define CRC32C_GCC
27+
#elif defined(_MSC_VER)
28+
#define CRC32C_MSC
29+
#endif
30+
31+
#ifndef CRC32C_STATIC
32+
#ifdef CRC32C_EXPORTS
33+
#ifdef CRC32C_GCC
34+
#define CRC32C_API __attribute__ ((dllexport))
35+
#else
36+
#define CRC32C_API __declspec(dllexport)
37+
#endif
38+
#else
39+
#ifdef CRC32C_GCC
40+
#define CRC32C_API __attribute__ ((dllimport))
41+
#else
42+
#define CRC32C_API __declspec(dllimport)
43+
#endif
44+
#endif
45+
#else
46+
#define CRC32C_API
47+
#endif
48+
49+
#include <stdint.h>
50+
#include <stddef.h>
51+
52+
#ifdef __cplusplus
53+
extern "C" {
54+
#endif
55+
56+
/*
57+
Computes CRC-32C (Castagnoli) checksum. Uses Intel's CRC32 instruction if it is available.
58+
Otherwise it uses a very fast software fallback.
59+
*/
60+
CRC32C_API uint32_t crc32c_append(
61+
uint32_t crc, /* Initial CRC value. Typically it's 0. */
62+
/* You can supply non-trivial initial value here. */
63+
/* Initial value can be used to chain CRC from multiple buffers. */
64+
const uint8_t *input, /* Data to be put through the CRC algorithm. */
65+
size_t length); /* Length of the data in the input buffer. */
66+
67+
68+
/*
69+
Software fallback version of CRC-32C (Castagnoli) checksum.
70+
*/
71+
CRC32C_API uint32_t crc32c_append_sw(uint32_t crc, const uint8_t *input, size_t length);
72+
73+
/*
74+
Hardware version of CRC-32C (Castagnoli) checksum. Will fail, if CPU does not support related instructions. Use a crc32c_append version instead of.
75+
*/
76+
CRC32C_API uint32_t crc32c_append_hw(uint32_t crc, const uint8_t *input, size_t length);
77+
78+
/*
79+
Checks is hardware version of CRC-32C is available.
80+
*/
81+
CRC32C_API int crc32c_hw_available();
82+
83+
#ifndef __cplusplus
84+
/*
85+
Initializes the CRC-32C library. Should be called only by C users to enable hardware version for crc32c_append.
86+
*/
87+
CRC32C_API void crc32c_init();
88+
#endif
89+
90+
91+
#ifdef __cplusplus
92+
}
93+
#endif
94+
95+
#endif

src/TorchSharp/TorchSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24+
<PackageReference Include="Google.Protobuf" Version="3.21.5" />
2425
<PackageReference Include="SharpZipLib" Version="1.3.3" />
2526
<PackageReference Include="System.Memory" Version="4.5.3" />
2627
</ItemGroup>

src/TorchSharp/Utils/CRC32C.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. See LICENSE in the project root for license information.
2+
3+
// Note: the native implementation of CRC32C has the following copyright / license notice:
4+
5+
/* Part of CRC-32C library: https://crc32c.machinezoo.com/ */
6+
/*
7+
Copyright (c) 2013 - 2014, 2016 Mark Adler, Robert Vazan, Max Vysokikh
8+
9+
This software is provided 'as-is', without any express or implied
10+
warranty. In no event will the author be held liable for any damages
11+
arising from the use of this software.
12+
13+
Permission is granted to anyone to use this software for any purpose,
14+
including commercial applications, and to alter it and redistribute it
15+
freely, subject to the following restrictions:
16+
17+
1. The origin of this software must not be misrepresented; you must not
18+
claim that you wrote the original software. If you use this software
19+
in a product, an acknowledgment in the product documentation would be
20+
appreciated but is not required.
21+
2. Altered source versions must be plainly marked as such, and must not be
22+
misrepresented as being the original software.
23+
3. This notice may not be removed or altered from any source distribution.
24+
*/
25+
26+
27+
using System;
28+
using System.Runtime.InteropServices;
29+
30+
namespace TorchSharp.Utils
31+
{
32+
public static class CRC32C
33+
{
34+
// uint32_t crc32c_append(uint32_t crc, buffer input, size_t length)
35+
36+
[DllImport("LibTorchSharp")]
37+
extern static uint crc32c_append(uint crc, IntPtr value, ulong length);
38+
39+
/// <summary>
40+
/// Compule the CRC32C value for a byte array.
41+
/// </summary>
42+
/// <param name="data">A byte array.</param>
43+
public static unsafe uint process(byte[] data)
44+
{
45+
fixed (byte* ptr = data) {
46+
return crc32c_append(0, (IntPtr)ptr, (ulong)data.Length);
47+
}
48+
}
49+
50+
/// <summary>
51+
/// Compule the CRC32C value for a 32-bit integer.
52+
/// </summary>
53+
/// <param name="data">A byte array.</param>
54+
public static unsafe uint process(int data)
55+
{
56+
fixed (int* ptr = stackalloc int[] { data })
57+
return crc32c_append(0, (IntPtr)ptr, (ulong)sizeof(int));
58+
}
59+
60+
/// <summary>
61+
/// Compule the CRC32C value for a 64-bit integer.
62+
/// </summary>
63+
/// <param name="data">A byte array.</param>
64+
public static unsafe uint process(long data)
65+
{
66+
fixed (long* ptr = stackalloc long[] { data })
67+
return crc32c_append(0, (IntPtr)ptr, (ulong)sizeof(long));
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)