Skip to content

Commit a3b2cc7

Browse files
committed
Added np.std, more performance optimization and bug fixes
- Performance optimization for scalar-related cloning and math-ops - Fixed various issues with all DefaultEngine's reduction methods. - Revamped expand_dims - Added a faster logic for Default.Cast when casting a scalar.
1 parent 83b0358 commit a3b2cc7

42 files changed

Lines changed: 15377 additions & 3096 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/NeuralNetwork.NumSharp/Layers/Activations/ReLU.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override void Forward(NDArray x)
2222

2323
public override void Backward(NDArray grad)
2424
{
25-
InputGrad = grad * (Input > 0);
25+
InputGrad = grad * (NDArray)(Input > 0);
2626
}
2727
}
2828
}

src/NumSharp.Core/APIs/np.amax.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
3+
namespace NumSharp
4+
{
5+
public static partial class np
6+
{
7+
/// <summary>
8+
/// Return the maximum of an array or maximum along an axis.
9+
/// </summary>
10+
/// <typeparam name="T">the type expected as a return, cast is performed if necessary.</typeparam>
11+
/// <returns>Maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.</returns>
12+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.amax.html</remarks>
13+
public static T amax<T>(NDArray a) where T : unmanaged => a.amax<T>();
14+
15+
/// <summary>
16+
/// Return the maximum of an array or maximum along an axis.
17+
/// </summary>
18+
/// <param name="axis">Axis or axes along which to operate.</param>
19+
/// <param name="keepdims">If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.</param>
20+
/// <param name="dtype">the type expected as a return, null will remain the same dtype.</param>
21+
/// <returns>Maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.</returns>
22+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.amax.html</remarks>
23+
public static NDArray amax(NDArray a, int? axis = null, bool keepdims = false, Type dtype = null)
24+
{
25+
if (!axis.HasValue)
26+
return a.amax(dtype);
27+
return a.amax(axis.Value, keepdims, dtype);
28+
}
29+
30+
/// <summary>
31+
/// Return the maximum of an array or maximum along an axis.
32+
/// </summary>
33+
/// <param name="axis">Axis or axes along which to operate.</param>
34+
/// <param name="keepdims">If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.</param>
35+
/// <param name="dtype">the type expected as a return, null will remain the same dtype.</param>
36+
/// <returns>Maximum of a. If axis is None, the result is a scalar value. If axis is given, the result is an array of dimension a.ndim - 1.</returns>
37+
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.amax.html</remarks>
38+
public static NDArray max(NDArray a, int? axis = null, bool keepdims = false, Type dtype = null)
39+
{
40+
if (!axis.HasValue)
41+
return a.amax(dtype);
42+
43+
return a.amax(axis.Value, keepdims, dtype);
44+
}
45+
}
46+
}

src/NumSharp.Core/APIs/np.blas.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static partial class np
1616
/// <param name="y"></param>
1717
/// <returns></returns>
1818
public static NDArray dot(NDArray x, NDArray y)
19-
=> BackendFactory.GetEngine().Dot(x, y);
19+
=> x.TensorEngine.Dot(x, y);
2020

2121
public static NDArray matmul(NDArray x, NDArray y)
22-
=> BackendFactory.GetEngine().MatMul(x, y);
22+
=> x.TensorEngine.MatMul(x, y);
2323
}
2424
}

src/NumSharp.Core/APIs/np.math.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ public static partial class np
99
{
1010
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.add.html</remarks>
1111
public static NDArray add(in NDArray x1, in NDArray x2)
12-
=> BackendFactory.GetEngine().Add(x1, x2);
12+
=> x1.TensorEngine.Add(x1, x2);
1313

1414
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html</remarks>
1515
public static NDArray divide(in NDArray x1, in NDArray x2)
16-
=> BackendFactory.GetEngine().Divide(x1, x2);
16+
=> x1.TensorEngine.Divide(x1, x2);
1717

1818
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.true_divide.html</remarks>
1919
public static NDArray true_divide(in NDArray x1, in NDArray x2)
2020
{
21-
return BackendFactory.GetEngine().Divide(x1, x2);
21+
return x1.TensorEngine.Divide(x1, x2);
2222
}
2323

2424
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.multiply.html</remarks>
2525
public static NDArray multiply(in NDArray x1, in NDArray x2)
26-
=> BackendFactory.GetEngine().Multiply(x1, x2);
26+
=> x1.TensorEngine.Multiply(x1, x2);
2727

2828
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.subtract.html</remarks>
2929
public static NDArray subtract(in NDArray x1, in NDArray x2)
30-
=> BackendFactory.GetEngine().Subtract(x1, x2);
30+
=> x1.TensorEngine.Subtract(x1, x2);
3131

3232
/// <summary>
3333
/// Sum of array elements over a given axis.
@@ -39,7 +39,7 @@ public static NDArray subtract(in NDArray x1, in NDArray x2)
3939
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
4040
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
4141
public static NDArray sum(in NDArray a)
42-
=> BackendFactory.GetEngine().Sum(a, axis: null, typeCode: null, keepdims: false);
42+
=> a.TensorEngine.Sum(a, axis: null, typeCode: null, keepdims: false);
4343

4444
/// <summary>
4545
/// Sum of array elements over a given axis.
@@ -51,7 +51,7 @@ public static NDArray sum(in NDArray a)
5151
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
5252
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
5353
public static NDArray sum(in NDArray a, int axis)
54-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: null, keepdims: false);
54+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: null, keepdims: false);
5555

5656
/// <summary>
5757
/// Sum of array elements over a given axis.
@@ -63,7 +63,7 @@ public static NDArray sum(in NDArray a, int axis)
6363
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
6464
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
6565
public static NDArray sum(in NDArray a, bool keepdims)
66-
=> BackendFactory.GetEngine().Sum(a, axis: null, typeCode: null, keepdims: keepdims);
66+
=> a.TensorEngine.Sum(a, axis: null, typeCode: null, keepdims: keepdims);
6767

6868
/// <summary>
6969
/// Sum of array elements over a given axis.
@@ -75,7 +75,7 @@ public static NDArray sum(in NDArray a, bool keepdims)
7575
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
7676
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
7777
public static NDArray sum(in NDArray a, int? axis, bool keepdims)
78-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: null, keepdims: keepdims);
78+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: null, keepdims: keepdims);
7979

8080
/// <summary>
8181
/// Sum of array elements over a given axis.
@@ -87,7 +87,7 @@ public static NDArray sum(in NDArray a, int? axis, bool keepdims)
8787
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
8888
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
8989
public static NDArray sum(in NDArray a, int? axis, bool keepdims, Type dtype)
90-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: dtype?.GetTypeCode(), keepdims: keepdims);
90+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: dtype?.GetTypeCode(), keepdims: keepdims);
9191

9292
/// <summary>
9393
/// Sum of array elements over a given axis.
@@ -99,7 +99,7 @@ public static NDArray sum(in NDArray a, int? axis, bool keepdims, Type dtype)
9999
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
100100
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
101101
public static NDArray sum(in NDArray a, int? axis, bool keepdims, NPTypeCode? typeCode)
102-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: typeCode, keepdims: keepdims);
102+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: typeCode, keepdims: keepdims);
103103

104104
/// <summary>
105105
/// Sum of array elements over a given axis.
@@ -111,7 +111,7 @@ public static NDArray sum(in NDArray a, int? axis, bool keepdims, NPTypeCode? ty
111111
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
112112
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
113113
public static NDArray sum(in NDArray a, int? axis, Type dtype)
114-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: dtype?.GetTypeCode(), keepdims: false);
114+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: dtype?.GetTypeCode(), keepdims: false);
115115

116116
/// <summary>
117117
/// Sum of array elements over a given axis.
@@ -122,7 +122,7 @@ public static NDArray sum(in NDArray a, int? axis, Type dtype)
122122
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
123123
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
124124
public static NDArray sum(in NDArray a, int? axis, NPTypeCode? typeCode)
125-
=> BackendFactory.GetEngine().Sum(a, axis: axis, typeCode: typeCode, keepdims: false);
125+
=> a.TensorEngine.Sum(a, axis: axis, typeCode: typeCode, keepdims: false);
126126

127127
/// <summary>
128128
/// Sum of array elements over a given axis.
@@ -134,7 +134,7 @@ public static NDArray sum(in NDArray a, int? axis, NPTypeCode? typeCode)
134134
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
135135
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
136136
public static NDArray sum(in NDArray a, Type dtype)
137-
=> BackendFactory.GetEngine().Sum(a, axis: null, typeCode: dtype?.GetTypeCode(), keepdims: false);
137+
=> a.TensorEngine.Sum(a, axis: null, typeCode: dtype?.GetTypeCode(), keepdims: false);
138138

139139
/// <summary>
140140
/// Sum of array elements over a given axis.
@@ -146,7 +146,7 @@ public static NDArray sum(in NDArray a, Type dtype)
146146
/// <returns>An array with the same shape as a, with the specified axis removed. If a is a 0-d array, or if axis is None, a scalar is returned. If an output array is specified, a reference to out is returned.</returns>
147147
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html</remarks>
148148
public static NDArray sum(in NDArray a, NPTypeCode? typeCode)
149-
=> BackendFactory.GetEngine().Sum(a, axis: null, typeCode: typeCode, keepdims: false);
149+
=> a.TensorEngine.Sum(a, axis: null, typeCode: typeCode, keepdims: false);
150150

151151
/// <summary>
152152
/// Calculate the absolute value element-wise.
@@ -194,7 +194,7 @@ public static NDArray sqrt(NDArray x)
194194
/// Array elements raised to given powers, element-wise.
195195
/// </summary>
196196
public static NDArray power(NDArray x, ValueType y)
197-
=> BackendFactory.GetEngine().Power(x, y);
197+
=> x.TensorEngine.Power(x, y);
198198

199199
//public static NDArray power<T>(NDArray nd, T exponent)
200200
// => nd.power<T>(exponent);

0 commit comments

Comments
 (0)