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
8 changes: 6 additions & 2 deletions python/src/convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ mx::array array_from_list_impl(
std::vector<uint32_t> vals;
fill_vector(pl, vals);
return mx::array(vals.begin(), shape, dtype);
} else if (dtype == mx::float64) {
std::vector<double> vals;
fill_vector(pl, vals);
return mx::array(vals.begin(), shape, dtype);
} else if (mx::issubdtype(dtype, mx::inexact)) {
std::vector<float> vals;
fill_vector(pl, vals);
Expand All @@ -653,7 +657,7 @@ mx::array array_from_list_impl(
}
case pyfloat: {
auto out_type = specified_type.value_or(mx::float32);
if (out_type == mx::float64) {
if (out_type == mx::float64 || !mx::issubdtype(out_type, mx::inexact)) {
std::vector<double> vals;
fill_vector(pl, vals);
return mx::array(vals.begin(), shape, out_type);
Expand Down Expand Up @@ -748,7 +752,7 @@ mx::array create_array(
return mx::array(val, t.value_or(default_type));
} else if (nb::isinstance<nb::float_>(v)) {
auto out_type = t.value_or(mx::float32);
if (out_type == mx::float64) {
if (out_type == mx::float64 || !mx::issubdtype(out_type, mx::inexact)) {
return mx::array(nb::cast<double>(v), out_type);
} else {
return mx::array(nb::cast<float>(v), out_type);
Expand Down
12 changes: 12 additions & 0 deletions python/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,18 @@ def test_double_keeps_precision(self):
out = mx.array([x], dtype=mx.float64).item()
self.assertEqual(out, x)

# Integer and bool targets
v = 2**24 + 1
self.assertEqual(mx.array(float(v), dtype=mx.int32).item(), v)
self.assertEqual(mx.array([float(v)], dtype=mx.int64).tolist(), [v])
# Small non-zero values are still True
self.assertTrue(mx.array(1e-50, dtype=mx.bool_).item())
self.assertEqual(mx.array([1e-50], dtype=mx.bool_).tolist(), [True])

# Python ints to float64
out = mx.array([v], dtype=mx.float64).tolist()
self.assertEqual(out, [float(v)])

def test_construction_from_lists_wide_ints(self):
# A python int that does not fit in int32 widens to int64, the same
# rule the scalar path already uses. It used to raise std::bad_cast.
Expand Down
Loading