Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f1f8e84

Browse filesBrowse files
committed
Add float16 workaround test case (#115)
1 parent e1d7094 commit f1f8e84
Copy full SHA for f1f8e84

File tree

Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+32
-1
lines changed

‎src/Numpy/Manual/np.array.cs

Copy file name to clipboardExpand all lines: src/Numpy/Manual/np.array.cs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static NDarray<T> array<T>(T[] @object, Dtype dtype = null, bool? copy =
6262
case short[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
6363
case int[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
6464
case long[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
65+
//case Half[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
6566
case float[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
6667
case double[] a: Marshal.Copy(a, 0, new IntPtr(ptr), a.Length); break;
6768
case bool[] a:

‎test/Numpy.UnitTest/NumpyTest.cs

Copy file name to clipboardExpand all lines: test/Numpy.UnitTest/NumpyTest.cs
+31-1Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.IO;
@@ -1016,6 +1016,36 @@ public async Task IssueByMartinDevans()
10161016
Assert.AreEqual(a, x.split(new[] { 3, 5, 6, 10 }).repr());
10171017
}
10181018

1019+
1020+
[TestMethod]
1021+
public async Task F16Workaround()
1022+
{
1023+
// use byte array to create float16 array
1024+
1025+
// numbers in float16:
1026+
// 0 01111 0000000000 = 2^0 * (1 + 0/1024) = 1
1027+
// 1 01111 0000000000 = -1 * 2 ^ 0 * (1 + 0 / 1024) = -1
1028+
// 0 11110 1111111111 = -1^(0) * 2^(15) * (1 + 1023/1024) ≈ 65504
1029+
// see: https://devblogs.microsoft.com/dotnet/introducing-the-half-type/
1030+
1031+
// these bytes in binary notation correspond to f16 numbers 1, -1 and 65504 (float16 max value)
1032+
// note, the bytes are in reversed order to the bits shown above
1033+
var bytes = new byte[] {
1034+
0b00000000, 0b00111100, // 1
1035+
0b00000000, 0b10111100, // -1
1036+
0b11111111, 0b01111011, // 65504
1037+
};
1038+
var floats = np.zeros(new Shape(3), np.float16);
1039+
Console.WriteLine(floats.repr);
1040+
// note, the using prevents a mem-leak with ctypes
1041+
using (var ctypes = floats.PyObject.ctypes) {
1042+
long ptr = ctypes.data;
1043+
Marshal.Copy(bytes, 0, new IntPtr(ptr), bytes.Length);
1044+
}
1045+
Console.WriteLine(floats.repr);
1046+
Assert.AreEqual("array([ 1.00e+00, -1.00e+00, 6.55e+04], dtype=float16)", floats.repr);
1047+
}
1048+
10191049
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#structural-indexing-tools
10201050
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#assigning-values-to-indexed-arrays
10211051
// TODO: https://docs.scipy.org/doc/numpy/user/basics.indexing.html?highlight=slice#dealing-with-variable-numbers-of-indices-within-programs

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.