From 2c772f99b165f7f6d72ef8d9ca88870f9da58248 Mon Sep 17 00:00:00 2001 From: ANtutov Date: Mon, 2 Feb 2026 18:50:51 +0200 Subject: [PATCH 1/3] Update onnx.rs --- crates/wasi-nn/src/backend/onnx.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/wasi-nn/src/backend/onnx.rs b/crates/wasi-nn/src/backend/onnx.rs index 13ac2dbed8ac..23f4930f939f 100644 --- a/crates/wasi-nn/src/backend/onnx.rs +++ b/crates/wasi-nn/src/backend/onnx.rs @@ -437,19 +437,21 @@ fn to_input_value(slot: &TensorSlot) -> Result<[SessionInputValue<'_>; 1], Backe } pub fn f32_vec_to_bytes(data: Vec) -> Vec { - let chunks: Vec<[u8; 4]> = data.into_iter().map(|f| f.to_le_bytes()).collect(); - let result: Vec = chunks.iter().flatten().copied().collect(); - result + let mut bytes = Vec::with_capacity(data.len() * 4); + for f in data { + bytes.extend_from_slice(&f.to_le_bytes()); + } + bytes } pub fn bytes_to_f32_vec(data: Vec) -> Vec { - let chunks: Vec<&[u8]> = data.chunks(4).collect(); - let v: Vec = chunks - .into_iter() - .map(|c| f32::from_le_bytes(c.try_into().unwrap())) - .collect(); - - v.into_iter().collect() + assert_eq!(data.len() % 4, 0); + data.chunks(4) + .map(|c| { + let arr: [u8; 4] = c.try_into().unwrap(); + f32::from_le_bytes(arr) + }) + .collect() } /// Returns whether the dimension is dynamic. From 56b48151ef511a42775c897afa2d84d345f8830f Mon Sep 17 00:00:00 2001 From: ANtutov Date: Mon, 2 Feb 2026 18:51:12 +0200 Subject: [PATCH 2/3] Update main.rs --- .../classification-component-onnx/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/wasi-nn/examples/classification-component-onnx/src/main.rs b/crates/wasi-nn/examples/classification-component-onnx/src/main.rs index affa61681557..3f19ca7e0596 100644 --- a/crates/wasi-nn/examples/classification-component-onnx/src/main.rs +++ b/crates/wasi-nn/examples/classification-component-onnx/src/main.rs @@ -116,13 +116,13 @@ fn main() { } pub fn bytes_to_f32_vec(data: Vec) -> Vec { - let chunks: Vec<&[u8]> = data.chunks(4).collect(); - let v: Vec = chunks - .into_iter() - .map(|c| f32::from_le_bytes(c.try_into().unwrap())) - .collect(); - - v.into_iter().collect() + assert_eq!(data.len() % 4, 0); + data.chunks(4) + .map(|c| { + let arr: [u8; 4] = c.try_into().unwrap(); + f32::from_le_bytes(arr) + }) + .collect() } // Take the image located at 'path', open it, resize it to height x width, and then converts From b3d437a903cb0961ce4d297cddbf50df5c973eb9 Mon Sep 17 00:00:00 2001 From: ANtutov Date: Mon, 2 Feb 2026 18:51:30 +0200 Subject: [PATCH 3/3] Update main.rs --- .../classification-example-winml/src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/wasi-nn/examples/classification-example-winml/src/main.rs b/crates/wasi-nn/examples/classification-example-winml/src/main.rs index a435fb5f5cc3..59231d13aed9 100644 --- a/crates/wasi-nn/examples/classification-example-winml/src/main.rs +++ b/crates/wasi-nn/examples/classification-example-winml/src/main.rs @@ -121,13 +121,13 @@ fn postprocess(output_tensor: Vec) -> Vec { } pub fn bytes_to_f32_vec(data: Vec) -> Vec { - let chunks: Vec<&[u8]> = data.chunks(4).collect(); - let v: Vec = chunks - .into_iter() - .map(|c| f32::from_ne_bytes(c.try_into().unwrap())) - .collect(); - - v.into_iter().collect() + assert_eq!(data.len() % 4, 0); + data.chunks(4) + .map(|c| { + let arr: [u8; 4] = c.try_into().unwrap(); + f32::from_ne_bytes(arr) + }) + .collect() } // A wrapper for class ID and match probabilities.