-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNativeExecutor.cs
More file actions
108 lines (105 loc) · 5.28 KB
/
Copy pathNativeExecutor.cs
File metadata and controls
108 lines (105 loc) · 5.28 KB
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
using System.Buffers;
using Tensornet.Common;
namespace Tensornet.Native{
internal class NativeExecutor{
internal delegate IntPtr DoubleInputOperation(IntPtr a, IntPtr b, IntPtr oup, IntPtr param, NativeProvider provider);
internal delegate IntPtr SingleInputOperation(IntPtr inp, IntPtr oup, IntPtr param, NativeProvider provider);
internal delegate IntPtr SelfModifyOperation(IntPtr t, IntPtr param, NativeProvider provider);
private NativeExecutor() { }
unsafe internal static IntPtr Execute(DoubleInputOperation func, ITensorMemory a, ITensorMemory b, ITensorMemory oup,
TensorLayout layoutA, TensorLayout layoutB, TensorLayout layoutOup, IntPtr param, NativeProvider provider){
MemoryHandle handleA, handleB, handleOup;
a.Pin(out handleA);
b.Pin(out handleB);
oup.Pin(out handleOup);
IntPtr status;
fixed(int* shapeAPtr = layoutA.Shape, shapeBPtr = layoutB.Shape, shapeOupPtr = layoutOup.Shape,
strideAPtr = layoutA.Stride, strideBPtr = layoutB.Stride, strideOupPtr = layoutOup.Stride){
NativeTensor nativeA = new NativeTensor()
{
dtype = layoutA.DType,
ndim = layoutA.NDim,
offset = layoutA.Offset * TensorTypeInfo.GetTypeSize(layoutA.DType),
shape = new IntPtr(shapeAPtr),
stride = new IntPtr(strideAPtr),
data = new IntPtr(handleA.Pointer)
};
NativeTensor nativeB = new NativeTensor()
{
dtype = layoutB.DType,
ndim = layoutB.NDim,
offset = layoutB.Offset * TensorTypeInfo.GetTypeSize(layoutB.DType),
shape = new IntPtr(shapeBPtr),
stride = new IntPtr(strideBPtr),
data = new IntPtr(handleB.Pointer)
};
NativeTensor nativeOup = new NativeTensor()
{
dtype = layoutOup.DType,
ndim = layoutOup.NDim,
offset = layoutOup.Offset * TensorTypeInfo.GetTypeSize(layoutOup.DType),
shape = new IntPtr(shapeOupPtr),
stride = new IntPtr(strideOupPtr),
data = new IntPtr(handleOup.Pointer)
};
status = func(new IntPtr(&nativeA), new IntPtr(&nativeB), new IntPtr(&nativeOup), param, provider);
}
handleA.Dispose();
handleB.Dispose();
handleOup.Dispose();
return status;
}
unsafe internal static IntPtr Execute(SingleInputOperation func, ITensorMemory inp, ITensorMemory oup, TensorLayout layoutInp,
TensorLayout layoutOup, IntPtr param, NativeProvider provider){
MemoryHandle handleInp, handleOup;
inp.Pin(out handleInp);
oup.Pin(out handleOup);
IntPtr status;
fixed(int* shapeInpPtr = layoutInp.Shape, shapeOupPtr = layoutOup.Shape,
strideInpPtr = layoutInp.Stride, strideOupPtr = layoutOup.Stride){
NativeTensor nativeInp = new NativeTensor()
{
dtype = layoutInp.DType,
ndim = layoutInp.NDim,
offset = layoutInp.Offset * TensorTypeInfo.GetTypeSize(layoutInp.DType),
shape = new IntPtr(shapeInpPtr),
stride = new IntPtr(strideInpPtr),
data = new IntPtr(handleInp.Pointer)
};
NativeTensor nativeOup = new NativeTensor()
{
dtype = layoutOup.DType,
ndim = layoutOup.NDim,
offset = layoutOup.Offset * TensorTypeInfo.GetTypeSize(layoutOup.DType),
shape = new IntPtr(shapeOupPtr),
stride = new IntPtr(strideOupPtr),
data = new IntPtr(handleOup.Pointer)
};
status = func(new IntPtr(&nativeInp), new IntPtr(&nativeOup), param, provider);
}
handleInp.Dispose();
handleOup.Dispose();
return status;
}
unsafe internal static IntPtr Execute(SelfModifyOperation func, ITensorMemory tm, TensorLayout layout, IntPtr param, NativeProvider provider){
MemoryHandle mhandle;
tm.Pin(out mhandle);
IntPtr status;
fixed(int* shapeInpPtr = layout.Shape, shapeOupPtr = layout.Shape,
strideInpPtr = layout.Stride, strideOupPtr = layout.Stride){
NativeTensor nativeTensor = new NativeTensor()
{
dtype = layout.DType,
ndim = layout.NDim,
offset = layout.Offset * TensorTypeInfo.GetTypeSize(layout.DType),
shape = new IntPtr(shapeInpPtr),
stride = new IntPtr(strideInpPtr),
data = new IntPtr(mhandle.Pointer)
};
status = func(new IntPtr(&nativeTensor), param, provider);
}
mhandle.Dispose();
return status;
}
}
}