@@ -275,23 +275,15 @@ NIF(to_blob) {
275
275
int limit = 0 ;
276
276
bool has_received_limit = (argc == 2 );
277
277
278
+ // Evaluate to ensure data is available
279
+ t->eval ();
280
+
278
281
if (has_received_limit) {
279
282
PARAM (1 , int , param_limit);
280
283
limit = param_limit;
281
284
byte_size = limit * t->itemsize ();
282
285
}
283
286
284
- // Flatten and slice if needed
285
- mlx::core::array flattened = mlx::core::flatten (*t);
286
- mlx::core::array reshaped =
287
- (has_received_limit && byte_size < t->nbytes ())
288
- ? mlx::core::slice (flattened, std::vector<int >{0 },
289
- std::vector<int >{limit})
290
- : flattened;
291
-
292
- // Evaluate to ensure data is available
293
- mlx::core::eval (reshaped);
294
-
295
287
// Create result binary
296
288
void *result_data = (void *)enif_make_new_binary (env, byte_size, &result);
297
289
@@ -300,14 +292,12 @@ NIF(to_blob) {
300
292
// https://github.com/ml-explore/mlx/discussions/1608#discussioncomment-11332071
301
293
//
302
294
// Set up contiguous iterator
303
- std::vector<int > slice_sizes (reshaped.shape ().begin (),
304
- reshaped.shape ().end ());
305
- ContiguousIterator iterator (slice_sizes, reshaped.strides (),
306
- reshaped.ndim ());
295
+ std::vector<int > slice_sizes (t->shape ().begin (), t->shape ().end ());
296
+ ContiguousIterator iterator (slice_sizes, t->strides (), t->ndim ());
307
297
308
298
// Copy data element by element using iterator
309
- size_t element_size = reshaped. itemsize ();
310
- const char *src_data = static_cast <const char *>(reshaped. data <void >());
299
+ size_t element_size = t-> itemsize ();
300
+ const char *src_data = static_cast <const char *>(t-> data <void >());
311
301
char *dst_data = static_cast <char *>(result_data);
312
302
313
303
size_t num_elements = byte_size / element_size;
0 commit comments