1
1
using System . Reflection ;
2
2
using System . Runtime . InteropServices ;
3
- using System . Runtime . Intrinsics ;
4
- using System . Runtime . Intrinsics . X86 ;
5
3
using System . Text ;
6
4
7
5
namespace Sapling . Engine . Evaluation ;
8
- #if AVX512
9
- using AvxIntrinsics = System . Runtime . Intrinsics . X86 . Avx512BW ;
10
- using VectorType = System . Runtime . Intrinsics . Vector512 ;
11
- using VectorInt = System . Runtime . Intrinsics . Vector512 < int > ;
12
- using VectorShort = System . Runtime . Intrinsics . Vector512 < short > ;
13
- #else
14
- using AvxIntrinsics = Avx2 ;
15
- using VectorType = Vector256 ;
16
- using VectorInt = Vector256 < int > ;
17
- using VectorShort = Vector256 < short > ;
18
- #endif
19
-
20
6
public static class NnueWeights
21
7
{
22
8
public const int InputSize = 768 ;
@@ -152,162 +138,4 @@ public static unsafe void Dispose()
152
138
Marshal . FreeHGlobal ( ( IntPtr ) OutputWeights ) ;
153
139
}
154
140
}
155
- }
156
-
157
- //using System.Reflection;
158
- //using System.Runtime.InteropServices;
159
- //using System.Runtime.Intrinsics;
160
- //using System.Runtime.Intrinsics.X86;
161
- //using System.Text;
162
-
163
- //namespace Sapling.Engine.Evaluation;
164
- //#if AVX512
165
- //using AvxIntrinsics = System.Runtime.Intrinsics.X86.Avx512BW;
166
- //using VectorType = System.Runtime.Intrinsics.Vector512;
167
- //using VectorInt = System.Runtime.Intrinsics.Vector512<int>;
168
- //using VectorShort = System.Runtime.Intrinsics.Vector512<short>;
169
- //#else
170
- //using AvxIntrinsics = Avx2;
171
- //using VectorType = Vector256;
172
- //using VectorInt = Vector256<int>;
173
- //using VectorShort = Vector256<short>;
174
- //#endif
175
-
176
- //public static class NnueWeights
177
- //{
178
- // public const int InputSize = 768;
179
- // public const int Layer1Size = 768;
180
-
181
- // public const short OutputBuckets = 8;
182
-
183
- // public static readonly unsafe VectorShort* FeatureWeights;
184
- // public static readonly unsafe VectorShort* FeatureBiases;
185
- // public static readonly unsafe VectorShort* OutputWeights;
186
- // public static readonly unsafe short* OutputBiases;
187
-
188
- // static unsafe NnueWeights()
189
- // {
190
- // var assembly = Assembly.GetAssembly(typeof(GameState));
191
- // var info = assembly.GetName();
192
- // var name = info.Name;
193
- // using var stream = assembly
194
- // .GetManifestResourceStream($"{name}.Resources.sapling.nnue")!;
195
-
196
- // var featureWeightsCount = InputSize * Layer1Size;
197
- // var featureBiasesCount = Layer1Size;
198
- // var outputWeightsCount = Layer1Size * 2 * OutputBuckets;
199
- // var outputBiasesCount = OutputBuckets;
200
-
201
-
202
- // var featureWeights = stackalloc short[featureWeightsCount];
203
- // var featureBiases = stackalloc short[featureBiasesCount];
204
- // var outputWeights = stackalloc short[outputWeightsCount];
205
- // var outputBiases = stackalloc short[outputBiasesCount];
206
-
207
- // using var reader = new BinaryReader(stream, Encoding.UTF8, false);
208
- // for (var i = 0; i < featureWeightsCount; i++)
209
- // {
210
- // featureWeights[i] = reader.ReadInt16();
211
- // }
212
-
213
- // for (var i = 0; i < featureBiasesCount; i++)
214
- // {
215
- // featureBiases[i] = reader.ReadInt16();
216
- // }
217
-
218
- // for (var i = 0; i < outputWeightsCount; i++)
219
- // {
220
- // outputWeights[i] = reader.ReadInt16();
221
- // }
222
-
223
- // var transposedWeights = stackalloc short[outputWeightsCount];
224
-
225
- // // Transposing logic
226
- // for (var i = 0; i < 2 * Layer1Size; i++)
227
- // {
228
- // for (var j = 0; j < OutputBuckets; j++)
229
- // {
230
- // // Original index calculation
231
- // var originalIndex = i * OutputBuckets + j;
232
-
233
- // // Transposed index calculation
234
- // var transposedIndex = j * 2 * Layer1Size + i;
235
-
236
- // // Assign value to transposed position
237
- // transposedWeights[transposedIndex] = outputWeights[originalIndex];
238
- // }
239
- // }
240
-
241
- // outputWeights = transposedWeights;
242
-
243
- // for (var i = 0; i < outputBiasesCount; i++)
244
- // {
245
- // outputBiases[i] = reader.ReadInt16();
246
- // }
247
-
248
- // // Allocate unmanaged memory
249
- // FeatureWeights = AlignedAllocZeroedShort((nuint)featureWeightsCount);
250
- // FeatureBiases = AlignedAllocZeroedShort((nuint)featureBiasesCount);
251
- // OutputWeights = AlignedAllocZeroedShort((nuint)outputWeightsCount);
252
- // OutputBiases = AlignedAllocZeroed((nuint)outputBiasesCount);
253
-
254
- // // Copy managed array to unmanaged memory
255
- // Buffer.MemoryCopy(featureWeights, FeatureWeights, featureWeightsCount * sizeof(short),
256
- // featureWeightsCount * sizeof(short));
257
-
258
- // Buffer.MemoryCopy(featureBiases, FeatureBiases, featureBiasesCount * sizeof(short),
259
- // featureBiasesCount * sizeof(short));
260
-
261
- // Buffer.MemoryCopy(outputWeights, OutputWeights, outputWeightsCount * sizeof(short),
262
- // outputWeightsCount * sizeof(short));
263
-
264
- // Buffer.MemoryCopy(outputBiases, OutputBiases, outputBiasesCount * sizeof(short),
265
- // outputBiasesCount * sizeof(short));
266
- // }
267
-
268
- // public static unsafe VectorShort* AlignedAllocZeroedShort(nuint items)
269
- // {
270
- // const nuint alignment = 64;
271
- // var bytes = sizeof(short) * items;
272
- // var block = NativeMemory.AlignedAlloc(bytes, alignment);
273
- // if (block == null)
274
- // {
275
- // throw new OutOfMemoryException("Failed to allocate aligned memory.");
276
- // }
277
-
278
- // NativeMemory.Clear(block, bytes);
279
- // return (VectorShort*)block;
280
- // }
281
-
282
- // public static unsafe short* AlignedAllocZeroed(nuint items)
283
- // {
284
- // const nuint alignment = 64;
285
- // var bytes = sizeof(short) * items;
286
- // var block = NativeMemory.AlignedAlloc(bytes, alignment);
287
- // if (block == null)
288
- // {
289
- // throw new OutOfMemoryException("Failed to allocate aligned memory.");
290
- // }
291
-
292
- // NativeMemory.Clear(block, bytes);
293
- // return (short*)block;
294
- // }
295
-
296
- // public static unsafe void Dispose()
297
- // {
298
- // if (FeatureWeights != null)
299
- // {
300
- // Marshal.FreeHGlobal((IntPtr)FeatureWeights);
301
- // }
302
-
303
- // if (FeatureBiases != null)
304
- // {
305
- // Marshal.FreeHGlobal((IntPtr)FeatureBiases);
306
- // }
307
-
308
- // if (OutputWeights != null)
309
- // {
310
- // Marshal.FreeHGlobal((IntPtr)OutputWeights);
311
- // }
312
- // }
313
- //}
141
+ }
0 commit comments