Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Source/MLX/Documentation.docc/Organization/reduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ See also <doc:logical> and <doc:cumulative>

### MLXArray Aggregating Reduction Functions

- ``MLXArray/countNonzero(keepDims:stream:)``
- ``MLXArray/countNonzero(axis:keepDims:stream:)``
- ``MLXArray/countNonzero(axes:keepDims:stream:)``
- ``MLXArray/logSumExp(keepDims:stream:)``
- ``MLXArray/logSumExp(axis:keepDims:stream:)``
- ``MLXArray/logSumExp(axes:keepDims:stream:)``
Expand Down Expand Up @@ -66,6 +69,9 @@ See also <doc:logical> and <doc:cumulative>

### Aggregating Reduction Free Functions

- ``countNonzero(_:keepDims:stream:)``
- ``countNonzero(_:axis:keepDims:stream:)``
- ``countNonzero(_:axes:keepDims:stream:)``
- ``logSumExp(_:keepDims:stream:)``
- ``logSumExp(_:axis:keepDims:stream:)``
- ``logSumExp(_:axes:keepDims:stream:)``
Expand Down
59 changes: 59 additions & 0 deletions Source/MLX/MLXArray+Ops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,65 @@ extension MLXArray {
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over the given axes.
///
/// - Parameters:
/// - axes: axes to reduce over
/// - keepDims: if `true` keep the reduced axes as singleton dimensions
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(axis:keepDims:stream:)``
/// - ``countNonzero(keepDims:stream:)``
/// - ``countNonzero(_:axes:keepDims:stream:)``
public func countNonzero(
axes: some Collection<Int>, keepDims: Bool = false, stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero_axes(&result, ctx, axes.asInt32, axes.count, keepDims, stream.ctx)
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over the given axis.
///
/// - Parameters:
/// - axis: axis to reduce over
/// - keepDims: if `true` keep reduced axis as singleton dimension
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(axes:keepDims:stream:)``
/// - ``countNonzero(keepDims:stream:)``
/// - ``countNonzero(_:axes:keepDims:stream:)``
public func countNonzero(
axis: Int, keepDims: Bool = false, stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero_axis(&result, ctx, axis.int32, keepDims, stream.ctx)
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over all axes.
///
/// - Parameters:
/// - keepDims: if `true` keep the reduced axes as singleton dimensions
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(axes:keepDims:stream:)``
/// - ``countNonzero(axis:keepDims:stream:)``
/// - ``countNonzero(_:axes:keepDims:stream:)``
public func countNonzero(
keepDims: Bool = false, stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero(&result, ctx, keepDims, stream.ctx)
return MLXArray(result)
}

/// Return the cumulative maximum of the elements along the given axis.
///
/// ```swift
Expand Down
63 changes: 63 additions & 0 deletions Source/MLX/Ops+Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,69 @@ public func conjugate(_ array: MLXArray, stream: StreamOrDevice = .default) -> M
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over the given axes.
///
/// - Parameters:
/// - array: input array
/// - axes: axes to reduce over
/// - keepDims: if `true` keep the reduced axes as singleton dimensions
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(_:axis:keepDims:stream:)``
/// - ``countNonzero(_:keepDims:stream:)``
/// - ``MLXArray/countNonzero(axes:keepDims:stream:)``
public func countNonzero(
_ array: MLXArray, axes: some Collection<Int>, keepDims: Bool = false,
stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero_axes(&result, array.ctx, axes.asInt32, axes.count, keepDims, stream.ctx)
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over the given axis.
///
/// - Parameters:
/// - array: input array
/// - axis: axis to reduce over
/// - keepDims: if `true` keep reduced axis as singleton dimension
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(_:axes:keepDims:stream:)``
/// - ``countNonzero(_:keepDims:stream:)``
/// - ``MLXArray/countNonzero(axes:keepDims:stream:)``
public func countNonzero(
_ array: MLXArray, axis: Int, keepDims: Bool = false, stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero_axis(&result, array.ctx, axis.int32, keepDims, stream.ctx)
return MLXArray(result)
}

/// Count the number of non-zero elements in the array over all axes.
///
/// - Parameters:
/// - array: input array
/// - keepDims: if `true` keep the reduced axes as singleton dimensions
/// - stream: stream or device to evaluate on
///
/// ### See Also
/// - <doc:reduction>
/// - ``countNonzero(_:axes:keepDims:stream:)``
/// - ``countNonzero(_:axis:keepDims:stream:)``
/// - ``MLXArray/countNonzero(axes:keepDims:stream:)``
public func countNonzero(
_ array: MLXArray, keepDims: Bool = false, stream: StreamOrDevice = .default
) -> MLXArray {
var result = mlx_array_new()
mlx_count_nonzero(&result, array.ctx, keepDims, stream.ctx)
return MLXArray(result)
}

/// Return the cumulative maximum of the elements along the given axis.
///
/// ```swift
Expand Down
15 changes: 15 additions & 0 deletions Tests/MLXTests/MLXArray+OpsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,19 @@ class MLXArrayOpsTests: XCTestCase {
XCTAssertEqual(r2, [Complex(-4, 24)])
}

func testCountNonzero() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that the Swift-wrapped ops arent really tested much, please let me know if adding these tests was unnecessary :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are good! See also #477

My logic was that beyond a certain point the swift tests weren't adding much -- the real code was mlx-side. However, when I updated the tests in 477 I found several gaps and bugs in the swift layer.

let a = MLXArray([0, 1, 2, 0, 3, 0], [2, 3])
let resAxes0 = a.countNonzero(axes: [0])
let resAxes1 = a.countNonzero(axes: [1])
let resAxis0 = a.countNonzero(axis: 0)
let resAxis1 = a.countNonzero(axis: 1)
let resAll = a.countNonzero()

assertEqual(resAxes0, [0, 2, 1])
assertEqual(resAxes0, resAxis0)
assertEqual(resAxes1, [2, 1])
assertEqual(resAxes1, resAxis1)
assertEqual(resAll, 3.asMLXArray(dtype: .int32))
}

}
15 changes: 15 additions & 0 deletions Tests/MLXTests/OpsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ class OpsTests: XCTestCase {
XCTAssertEqual(c.dtype, .float32)
}

func testCountNonzero() {
let a = MLXArray([0, 1, 2, 0, 3, 0], [2, 3])
let resAxes0 = countNonzero(a, axes: [0])
let resAxes1 = countNonzero(a, axes: [1])
let resAxis0 = countNonzero(a, axis: 0)
let resAxis1 = countNonzero(a, axis: 1)
let resAll = countNonzero(a)

assertEqual(resAxes0, [0, 2, 1])
assertEqual(resAxes0, resAxis0)
assertEqual(resAxes1, [2, 1])
assertEqual(resAxes1, resAxis1)
assertEqual(resAll, 3.asMLXArray(dtype: .int32))
}

func testFlatten() {
let a = zeros([4, 5, 6, 7])
let b = flatten(a, startAxis: 1, endAxis: 2)
Expand Down
Loading