Skip to content

Commit fa43eef

Browse files
committed
fix: int64 indexing migration batch 5 - BUILD PASSING
Complete migration of int32 to int64 for indices, sizes, strides, offsets across 16 files. Build now compiles successfully. Core changes: - Shape.Broadcasting.cs: All dimension/stride arrays now long[] - NDArray.cs: Added long size constructor overloads - TensorEngine/np.nonzero: Return NDArray<long>[] for index arrays - ILKernelGenerator.Reduction.Axis.Simd: AxisReductionKernel delegate now uses long* for strides/shapes and long for sizes - np.size: Return type changed to long - np.array: Stride variables changed to long for pointer arithmetic - NDArray.Indexing.Masking: Shape arrays and counts now long Random functions: - np.random.choice/shuffle: Added overflow checks for int.MaxValue limit (Random.Next only supports int; full long support deferred) Build infrastructure: - NumSharp.Core.csproj: Exclude *.template.cs and *.regen_disabled files Test status: 193 failures due to memory corruption - needs investigation in stride/offset calculations.
1 parent 469233b commit fa43eef

16 files changed

Lines changed: 261 additions & 244 deletions

docs/INT64_MIGRATION_PROGRESS.md

Lines changed: 139 additions & 156 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial class np
1111
/// <param name="axis">Axis along which the elements are counted. By default, give the total number of elements.</param>
1212
/// <returns>Number of elements along the specified axis.</returns>
1313
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.ma.size.html</remarks>
14-
public static int size(NDArray a, int? axis = null)
14+
public static long size(NDArray a, int? axis = null)
1515
{
1616
if (a == null)
1717
throw new ArgumentNullException(nameof(a));

src/NumSharp.Core/Backends/Default/ArrayManipulation/Default.Transpose.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public override NDArray SwapAxes(NDArray nd, int axis1, int axis2)
9292
dims[axis1] = axis2;
9393
dims[axis2] = axis1;
9494

95-
return Transpose(nd, dims);
95+
// Convert long[] to int[] for Transpose (axes are limited to int range)
96+
var intDims = new int[ndims];
97+
for (int i = 0; i < ndims; i++)
98+
intDims[i] = (int)dims[i];
99+
return Transpose(nd, intDims);
96100
}
97101

98102
public override NDArray RollAxis(NDArray nd, int axis, int start = 0)
@@ -157,7 +161,7 @@ public override NDArray Transpose(NDArray nd, int[] premute = null)
157161
// Handle empty arrays: just create a new array with permuted dimensions, no data copy needed
158162
if (nd.Shape.size == 0)
159163
{
160-
var emptyDims = new int[n];
164+
var emptyDims = new long[n];
161165
for (i = 0; i < n; i++)
162166
emptyDims[i] = nd.Shape.dimensions[permutation[i]];
163167
return new NDArray(nd.dtype, emptyDims);
@@ -175,8 +179,8 @@ public override NDArray Transpose(NDArray nd, int[] premute = null)
175179
var srcStrides = shape.strides;
176180

177181
// Permute dimensions and strides
178-
var permutedDims = new int[n];
179-
var permutedStrides = new int[n];
182+
var permutedDims = new long[n];
183+
var permutedStrides = new long[n];
180184
for (i = 0; i < n; i++)
181185
{
182186
permutedDims[i] = srcDims[permutation[i]];
@@ -185,7 +189,7 @@ public override NDArray Transpose(NDArray nd, int[] premute = null)
185189

186190
// Create the transposed shape via constructor (immutable)
187191
// IsContiguous is computed from strides and will be false for transposed arrays
188-
int bufSize = shape.bufferSize > 0 ? shape.bufferSize : shape.size;
192+
long bufSize = shape.bufferSize > 0 ? shape.bufferSize : shape.size;
189193
var newShape = new Shape(permutedDims, permutedStrides, shape.offset, bufSize);
190194

191195
// Return an alias (view) with the permuted shape

src/NumSharp.Core/Backends/Default/Indexing/Default.NonZero.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public partial class DefaultEngine
1919
/// - Handles contiguous and strided arrays efficiently
2020
/// </remarks>
2121
/// <param name="nd">Input array</param>
22-
/// <returns>Array of NDArray&lt;int&gt;, one per dimension containing indices of non-zero elements</returns>
23-
public override NDArray<int>[] NonZero(NDArray nd)
22+
/// <returns>Array of NDArray&lt;long&gt;, one per dimension containing indices of non-zero elements</returns>
23+
public override NDArray<long>[] NonZero(NDArray nd)
2424
{
2525
// Type dispatch to generic implementation
2626
switch (nd.typecode)
@@ -44,9 +44,9 @@ public override NDArray<int>[] NonZero(NDArray nd)
4444

4545
/// <summary>
4646
/// Generic implementation of nonzero using ILKernelGenerator.
47-
/// Both contiguous and strided paths now use the unified IL-based approach.
47+
/// Uses coordinate-based iteration via ILKernelGenerator for all cases.
4848
/// </summary>
49-
private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanaged
49+
private static unsafe NDArray<long>[] nonzeros<T>(NDArray<T> x) where T : unmanaged
5050
{
5151
// Ensure at least 1D (NumPy behavior)
5252
x = np.atleast_1d(x).MakeGeneric<T>();
@@ -58,22 +58,14 @@ private static unsafe NDArray<int>[] nonzeros<T>(NDArray<T> x) where T : unmanag
5858
// NumPy: np.nonzero(np.array([])) -> (array([], dtype=int64),)
5959
if (size == 0)
6060
{
61-
var emptyResult = new NDArray<int>[ndim];
61+
var emptyResult = new NDArray<long>[ndim];
6262
for (int i = 0; i < ndim; i++)
63-
emptyResult[i] = new NDArray<int>(0);
63+
emptyResult[i] = new NDArray<long>(0);
6464
return emptyResult;
6565
}
6666

67-
// SIMD fast path for contiguous arrays
68-
if (shape.IsContiguous && ILKernelGenerator.Enabled && ILKernelGenerator.VectorBits > 0)
69-
{
70-
var flatIndices = new ArraySlice<long>(Math.Max(16L, size / 4L));
71-
kp.FindNonZero((T*)x.Address, size, flatIndices);
72-
return kp.ConvertFlatToCoordinates(flatIndices, x.shape);
73-
}
74-
75-
// Strided path for non-contiguous arrays (transposed, sliced, etc.)
76-
// Uses coordinate-based iteration via ILKernelGenerator
67+
// Use strided helper for all cases (handles both contiguous and non-contiguous)
68+
// The ILKernelGenerator.FindNonZeroStridedHelper uses coordinate-based iteration
7769
return ILKernelGenerator.FindNonZeroStridedHelper((T*)x.Address, shape.dimensions, shape.strides, shape.offset);
7870
}
7971

src/NumSharp.Core/Backends/Kernels/ILKernelGenerator.Reduction.Axis.Simd.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public static partial class ILKernelGenerator
2929
private static unsafe AxisReductionKernel CreateAxisReductionKernelTyped<T>(AxisReductionKernelKey key)
3030
where T : unmanaged
3131
{
32-
return (void* input, void* output, int* inputStrides, int* inputShape,
33-
int* outputStrides, int axis, int axisSize, int ndim, int outputSize) =>
32+
return (void* input, void* output, long* inputStrides, long* inputShape,
33+
long* outputStrides, int axis, long axisSize, int ndim, long outputSize) =>
3434
{
3535
AxisReductionSimdHelper<T>(
3636
(T*)input, (T*)output,
@@ -57,12 +57,12 @@ private static unsafe AxisReductionKernel CreateAxisReductionKernelTyped<T>(Axis
5757
/// <param name="op">Reduction operation</param>
5858
internal static unsafe void AxisReductionSimdHelper<T>(
5959
T* input, T* output,
60-
int* inputStrides, int* inputShape, int* outputStrides,
61-
int axis, int axisSize, int ndim, int outputSize,
60+
long* inputStrides, long* inputShape, long* outputStrides,
61+
int axis, long axisSize, int ndim, long outputSize,
6262
ReductionOp op)
6363
where T : unmanaged
6464
{
65-
int axisStride = inputStrides[axis];
65+
long axisStride = inputStrides[axis];
6666

6767
// Check if the reduction axis is contiguous (stride == 1)
6868
bool axisContiguous = axisStride == 1;
@@ -89,19 +89,19 @@ internal static unsafe void AxisReductionSimdHelper<T>(
8989
bool isMean = op == ReductionOp.Mean;
9090

9191
// Sequential loop over output elements
92-
for (int outIdx = 0; outIdx < outputSize; outIdx++)
92+
for (long outIdx = 0; outIdx < outputSize; outIdx++)
9393
{
9494
// Convert linear output index to coordinates and compute input base offset
95-
int remaining = outIdx;
96-
int inputBaseOffset = 0;
97-
int outputOffset = 0;
95+
long remaining = outIdx;
96+
long inputBaseOffset = 0;
97+
long outputOffset = 0;
9898

9999
for (int d = 0; d < outputNdim; d++)
100100
{
101101
// Map output dimension d to input dimension
102102
int inputDim = d >= axis ? d + 1 : d;
103103

104-
int coord = remaining / outputDimStridesArray[d];
104+
long coord = remaining / outputDimStridesArray[d];
105105
remaining = remaining % outputDimStridesArray[d];
106106

107107
inputBaseOffset += coord * inputStrides[inputDim];
@@ -149,7 +149,7 @@ private static T DivideByCountTyped<T>(T value, long count) where T : unmanaged
149149
if (typeof(T) == typeof(int))
150150
{
151151
// Integer division
152-
int result = (int)(object)value / count;
152+
int result = (int)((int)(object)value / count);
153153
return (T)(object)result;
154154
}
155155
if (typeof(T) == typeof(long))

src/NumSharp.Core/Backends/NDArray.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ public NDArray(NPTypeCode dtype, int size) : this(dtype, Shape.Vector(size), tru
263263
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
264264
public NDArray(NPTypeCode dtype, int size, bool fillZeros) : this(dtype, Shape.Vector(size), true) { }
265265

266+
/// <summary>
267+
/// Constructor which initialize elements with length of <paramref name="size"/>
268+
/// </summary>
269+
/// <param name="dtype">Internal data type</param>
270+
/// <param name="size">The size as a single dimension shape (long for large arrays)</param>
271+
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
272+
public NDArray(NPTypeCode dtype, long size) : this(dtype, Shape.Vector(size), true) { }
273+
274+
/// <summary>
275+
/// Constructor which initialize elements with length of <paramref name="size"/>
276+
/// </summary>
277+
/// <param name="dtype">Internal data type</param>
278+
/// <param name="size">The size as a single dimension shape (long for large arrays)</param>
279+
/// <param name="fillZeros">Should set the values of the new allocation to default(dtype)? otherwise - old memory noise</param>
280+
/// <remarks>This constructor calls <see cref="IStorage.Allocate(NumSharp.Shape,System.Type)"/></remarks>
281+
public NDArray(NPTypeCode dtype, long size, bool fillZeros) : this(dtype, Shape.Vector(size), fillZeros) { }
282+
266283
/// <summary>
267284
/// Constructor which initialize elements with 0
268285
/// type and shape are given.

src/NumSharp.Core/Backends/TensorEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public abstract class TensorEngine
212212

213213
#region Indexing
214214

215-
public abstract NDArray<int>[] NonZero(NDArray a);
215+
public abstract NDArray<long>[] NonZero(NDArray a);
216216

217217
public abstract long CountNonZero(NDArray a);
218218

src/NumSharp.Core/Creation/np.are_broadcastable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static bool are_broadcastable(NDArray lhs, NDArray rhs)
6060
/// <remarks>https://docs.scipy.org/doc/numpy/reference/generated/numpy.broadcast_arrays.html</remarks>
6161
public static bool are_broadcastable(long[] shape1, long[] shape2)
6262
{
63-
return DefaultEngine.AreBroadcastable(new[] { new Shape(shape1), new Shape(shape2) });
63+
return Shape.AreBroadcastable(new Shape(shape1), new Shape(shape2));
6464
}
6565
}
6666
}

src/NumSharp.Core/Creation/np.array.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static NDArray array<T>(T[][] data) where T : unmanaged
170170
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2));
171171

172172
var strides = @out.strides;
173-
int stride1 = strides[0];
173+
long stride1 = strides[0];
174174
Debug.Assert(strides[1] == 1);
175175

176176
T* addr = (T*)@out.Address;
@@ -209,8 +209,8 @@ public static NDArray array<T>(T[][][] data) where T : unmanaged
209209
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3));
210210

211211
var strides = @out.strides;
212-
int stride1 = strides[0];
213-
int stride2 = strides[1];
212+
long stride1 = strides[0];
213+
long stride2 = strides[1];
214214
Debug.Assert(strides[2] == 1);
215215

216216
T* addr = (T*)@out.Address;
@@ -255,9 +255,9 @@ public static NDArray array<T>(T[][][][] data) where T : unmanaged
255255
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3, len4));
256256

257257
var strides = @out.strides;
258-
int stride1 = strides[0];
259-
int stride2 = strides[1];
260-
int stride3 = strides[2];
258+
long stride1 = strides[0];
259+
long stride2 = strides[1];
260+
long stride3 = strides[2];
261261
Debug.Assert(strides[3] == 1);
262262

263263
T* addr = (T*)@out.Address;
@@ -308,10 +308,10 @@ public static NDArray array<T>(T[][][][][] data) where T : unmanaged
308308
NDArray @out = new NDArray(InfoOf<T>.NPTypeCode, new Shape(len1, len2, len3, len4, len5));
309309

310310
var strides = @out.strides;
311-
int stride1 = strides[0];
312-
int stride2 = strides[1];
313-
int stride3 = strides[2];
314-
int stride4 = strides[3];
311+
long stride1 = strides[0];
312+
long stride2 = strides[1];
313+
long stride3 = strides[2];
314+
long stride4 = strides[3];
315315
Debug.Assert(strides[4] == 1);
316316

317317
T* addr = (T*)@out.Address;

src/NumSharp.Core/Indexing/np.nonzero.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static partial class np
2020
/// <param name="a">Input array.</param>
2121
/// <returns>Indices of elements that are non-zero.</returns>
2222
/// <remarks>https://numpy.org/doc/stable/reference/generated/numpy.nonzero.html</remarks>
23-
public static NDArray<int>[] nonzero(NDArray a)
23+
public static NDArray<long>[] nonzero(NDArray a)
2424
=> a.TensorEngine.NonZero(a);
2525
}
2626
}

0 commit comments

Comments
 (0)