Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ fn main() {
}

pub fn bytes_to_f32_vec(data: Vec<u8>) -> Vec<f32> {
let chunks: Vec<&[u8]> = data.chunks(4).collect();
let v: Vec<f32> = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ fn postprocess(output_tensor: Vec<f32>) -> Vec<f32> {
}

pub fn bytes_to_f32_vec(data: Vec<u8>) -> Vec<f32> {
let chunks: Vec<&[u8]> = data.chunks(4).collect();
let v: Vec<f32> = 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.
Expand Down
22 changes: 12 additions & 10 deletions crates/wasi-nn/src/backend/onnx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,19 +437,21 @@ fn to_input_value(slot: &TensorSlot) -> Result<[SessionInputValue<'_>; 1], Backe
}

pub fn f32_vec_to_bytes(data: Vec<f32>) -> Vec<u8> {
let chunks: Vec<[u8; 4]> = data.into_iter().map(|f| f.to_le_bytes()).collect();
let result: Vec<u8> = 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<u8>) -> Vec<f32> {
let chunks: Vec<&[u8]> = data.chunks(4).collect();
let v: Vec<f32> = 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.
Expand Down