Skip to content

Add countNonzero to the Swift API - #479

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
Silenterc:add-count-nonzero
Sep 14, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
Silenterc:add-count-nonzero

Conversation

@Silenterc

Copy link
Copy Markdown
Contributor

Proposed changes

  • Noticed that the Swift API was missing count_nonzero, so I added it as a free function and an MLXArray method + docs
  • Added tests covering the 6 overloads

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

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.

@davidkoski davidkoski left a comment

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.

Change looks good, thank you!

@davidkoski
davidkoski merged commit 2bebe4e into ml-explore:main Sep 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants