Skip to content

Add printoptions#3333

Open
ChristophePRAT wants to merge 5 commits intoml-explore:mainfrom
ChristophePRAT:feat/print_options
Open

Add printoptions#3333
ChristophePRAT wants to merge 5 commits intoml-explore:mainfrom
ChristophePRAT:feat/print_options

Conversation

@ChristophePRAT
Copy link
Copy Markdown
Contributor

Proposed changes

Add minimal print options similar to numpy's set_printoptions.

This PR should fix #471.

Quick example

import mlx.core as mx

a = mx.array([0.3668831623])

print(a) # 0.3668831623

with mx.printoptions(precision=2):
    print(a) # 0.37

print(a) # 0.3668831623

mx.set_printoptions(precision=4)
print(a) # 0.3669
print(a) # 0.3669

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)

int precision{-1};
};

MLX_API void set_printoptions(int precision);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you make the API take a struct instead, for future extension?

mx::set_printoptions(new_precision);
return *this;
}
void __exit__(nb::args) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For the C++ code we don't add __.

Suggested change
void __exit__(nb::args) {
void exit(nb::args) {

// Set Python print formatting options
mx::get_global_formatter().capitalize_bool = true;

// Expose printing options to Python: allow setting global precision.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you move the print APIs to a new print.cpp file? The array.cpp is already bloated.

@@ -0,0 +1,11 @@
Print Options
============
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you fix the warning when building docs?

/home/runner/work/mlx/mlx/docs/src/python/printoptions.rst:2: WARNING: Title underline too short.

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.

API for array printing options

2 participants