Skip to content

Commit 49a5261

Browse files
ManishAradwadawni
andauthored
Added formatter structure and a boolean value formatter (#354)
* added formatter structure and a boolean value formatter --------- Co-authored-by: Awni Hannun <[email protected]>
1 parent d1fef34 commit 49a5261

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

mlx/utils.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,46 @@
77

88
namespace mlx::core {
99

10+
void PrintFormatter::print(std::ostream& os, bool val) {
11+
if (capitalize_bool) {
12+
os << (val ? "True" : "False");
13+
} else {
14+
os << val;
15+
}
16+
}
17+
inline void PrintFormatter::print(std::ostream& os, int16_t val) {
18+
os << val;
19+
}
20+
inline void PrintFormatter::print(std::ostream& os, uint16_t val) {
21+
os << val;
22+
}
23+
inline void PrintFormatter::print(std::ostream& os, int32_t val) {
24+
os << val;
25+
}
26+
inline void PrintFormatter::print(std::ostream& os, uint32_t val) {
27+
os << val;
28+
}
29+
inline void PrintFormatter::print(std::ostream& os, int64_t val) {
30+
os << val;
31+
}
32+
inline void PrintFormatter::print(std::ostream& os, uint64_t val) {
33+
os << val;
34+
}
35+
inline void PrintFormatter::print(std::ostream& os, float16_t val) {
36+
os << val;
37+
}
38+
inline void PrintFormatter::print(std::ostream& os, bfloat16_t val) {
39+
os << val;
40+
}
41+
inline void PrintFormatter::print(std::ostream& os, float val) {
42+
os << val;
43+
}
44+
inline void PrintFormatter::print(std::ostream& os, complex64_t val) {
45+
os << val;
46+
}
47+
48+
PrintFormatter global_formatter;
49+
1050
Dtype result_type(const std::vector<array>& arrays) {
1151
std::vector<Dtype> dtypes(1, bool_);
1252
for (auto& arr : arrays) {
@@ -136,7 +176,7 @@ void print_subarray(std::ostream& os, const array& a, size_t index, int dim) {
136176
i = n - num_print - 1;
137177
index += s * (n - 2 * num_print - 1);
138178
} else if (is_last) {
139-
os << a.data<T>()[index];
179+
global_formatter.print(os, a.data<T>()[index]);
140180
} else {
141181
print_subarray<T>(os, a, index, dim + 1);
142182
}
@@ -153,7 +193,7 @@ void print_array(std::ostream& os, const array& a) {
153193
os << "array(";
154194
if (a.ndim() == 0) {
155195
auto data = a.data<T>();
156-
os << data[0];
196+
global_formatter.print(os, data[0]);
157197
} else {
158198
print_subarray<T>(os, a, 0, 0);
159199
}

mlx/utils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@
99

1010
namespace mlx::core {
1111

12+
struct PrintFormatter {
13+
inline void print(std::ostream& os, bool val);
14+
inline void print(std::ostream& os, int16_t val);
15+
inline void print(std::ostream& os, uint16_t val);
16+
inline void print(std::ostream& os, int32_t val);
17+
inline void print(std::ostream& os, uint32_t val);
18+
inline void print(std::ostream& os, int64_t val);
19+
inline void print(std::ostream& os, uint64_t val);
20+
inline void print(std::ostream& os, float16_t val);
21+
inline void print(std::ostream& os, bfloat16_t val);
22+
inline void print(std::ostream& os, float val);
23+
inline void print(std::ostream& os, complex64_t val);
24+
25+
bool capitalize_bool{false};
26+
};
27+
28+
extern PrintFormatter global_formatter;
29+
1230
/** The type from promoting the arrays' types with one another. */
1331
Dtype result_type(const std::vector<array>& arrays);
1432

python/src/array.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@ class ArrayPythonIterator {
520520
};
521521

522522
void init_array(py::module_& m) {
523+
// Set Python print formatting options
524+
mlx::core::global_formatter.capitalize_bool = true;
525+
523526
// Types
524527
py::class_<Dtype>(
525528
m,

python/tests/test_array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def test_init_from_array(self):
304304

305305
def test_array_repr(self):
306306
x = mx.array(True)
307-
self.assertEqual(str(x), "array(true, dtype=bool)")
307+
self.assertEqual(str(x), "array(True, dtype=bool)")
308308
x = mx.array(1)
309309
self.assertEqual(str(x), "array(1, dtype=int32)")
310310
x = mx.array(1.0)

0 commit comments

Comments
 (0)