diff --git a/mlx/backend/common/primitives.cpp b/mlx/backend/common/primitives.cpp index 88d53b447..9893b51cb 100644 --- a/mlx/backend/common/primitives.cpp +++ b/mlx/backend/common/primitives.cpp @@ -606,7 +606,7 @@ void View::eval_cpu(const std::vector& inputs, array& out) { if (ibytes == obytes || obytes < ibytes && in.strides().back() == 1 || in.flags().row_contiguous) { auto strides = in.strides(); - for (int i = 0; i < strides.size() - 1; ++i) { + for (int i = 0; i < static_cast(strides.size()) - 1; ++i) { strides[i] *= ibytes; strides[i] /= obytes; } diff --git a/mlx/backend/metal/primitives.cpp b/mlx/backend/metal/primitives.cpp index d09176f20..f788435a2 100644 --- a/mlx/backend/metal/primitives.cpp +++ b/mlx/backend/metal/primitives.cpp @@ -417,7 +417,7 @@ void View::eval_gpu(const std::vector& inputs, array& out) { if (ibytes == obytes || obytes < ibytes && in.strides().back() == 1 || in.flags().row_contiguous) { auto strides = in.strides(); - for (int i = 0; i < strides.size() - 1; ++i) { + for (int i = 0; i < static_cast(strides.size()) - 1; ++i) { strides[i] *= ibytes; strides[i] /= obytes; } diff --git a/mlx/ops.cpp b/mlx/ops.cpp index cd972502a..4aef90639 100644 --- a/mlx/ops.cpp +++ b/mlx/ops.cpp @@ -4587,7 +4587,7 @@ array view(const array& a, const Dtype& dtype, StreamOrDevice s /* = {} */) { " axis must be a multiple of the requested type size."); } out_shape.back() /= (obytes / ibytes); - } else { + } else if (ibytes > obytes) { // Type size ratios are always integers out_shape.back() *= (ibytes / obytes); } diff --git a/python/tests/test_ops.py b/python/tests/test_ops.py index 99d253dd8..54a3cf8c4 100644 --- a/python/tests/test_ops.py +++ b/python/tests/test_ops.py @@ -2532,6 +2532,10 @@ class TestOps(mlx_tests.MLXTestCase): self.assertTrue(np.array_equal(np.array(out_mlx), out_np)) def test_view(self): + # Check scalar + out = mx.array(1, mx.int8).view(mx.uint8).item() + self.assertEqual(out, 1) + a = mx.random.randint(shape=(4, 2, 4), low=-100, high=100) a_np = np.array(a)