Skip to content
Merged
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
92 changes: 40 additions & 52 deletions src/python/graph_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,38 +499,32 @@ impl PyMLGraphBuilder {
let pads = pads.unwrap_or_else(|| vec![0, 0, 0, 0]);
let groups = groups.unwrap_or(1);

let input_layout_s = match input_layout.unwrap_or("nchw") {
"nchw" => "nchw",
"nhwc" => "nhwc",
other => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"Invalid input_layout '{}', must be 'nchw' or 'nhwc'",
other
)));
}
};

let filter_layout_s = match filter_layout.unwrap_or("oihw") {
"oihw" => "oihw",
"hwio" => "hwio",
"ohwi" => "ohwi",
"ihwo" => "ihwo",
other => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"Invalid filter_layout '{}', must be 'oihw', 'hwio', 'ohwi', or 'ihwo'",
other
)));
}
};

let conv2d_options = MLConv2dOptions {
label: String::new(),
padding: pads,
strides,
dilations,
groups,
input_layout: input_layout_s.to_string(),
filter_layout: filter_layout_s.to_string(),
input_layout: input_layout
.map(|layout| {
serde_json::from_str(&format!("\"{layout}\"")).map_err(|_err| {
pyo3::exceptions::PyValueError::new_err(format!(
"Invalid input_layout '{layout}', must be 'nchw' or 'nhwc'",
))
})
})
.transpose()?
.unwrap_or_default(),
filter_layout: filter_layout
.map(|layout| {
serde_json::from_str(&format!("\"{layout}\"")).map_err(|_err| {
pyo3::exceptions::PyValueError::new_err(format!(
"Invalid filter_layout '{layout}', must be 'oihw', 'hwio', 'ohwi', or 'ihwo'",
))
})
})
.transpose()?
.unwrap_or_default(),
bias: bias.map(|b| b.id),
};

Expand Down Expand Up @@ -596,30 +590,6 @@ impl PyMLGraphBuilder {
let output_padding = output_padding.unwrap_or_else(|| vec![0, 0]);
let groups = groups.unwrap_or(1);

let input_layout_s = match input_layout.unwrap_or("nchw") {
"nchw" => "nchw",
"nhwc" => "nhwc",
other => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"Invalid input_layout '{}', must be 'nchw' or 'nhwc'",
other
)));
}
};

let filter_layout_s = match filter_layout.unwrap_or("iohw") {
"iohw" => "iohw",
"hwoi" => "hwoi",
"ohwi" => "ohwi",
"oihw" => "oihw",
other => {
return Err(pyo3::exceptions::PyValueError::new_err(format!(
"Invalid filter_layout '{}', must be 'iohw', 'hwoi', 'ohwi', or 'oihw'",
other
)));
}
};

let conv_t_options = MLConvTranspose2dOptions {
label: String::new(),
padding: pads,
Expand All @@ -628,8 +598,26 @@ impl PyMLGraphBuilder {
output_padding,
output_sizes: output_sizes.clone(),
groups,
input_layout: input_layout_s.to_string(),
filter_layout: filter_layout_s.to_string(),
input_layout: input_layout
.map(|layout| {
serde_json::from_str(&format!("\"{layout}\"")).map_err(|_err| {
pyo3::exceptions::PyValueError::new_err(format!(
"Invalid input_layout '{layout}', must be 'nchw' or 'nhwc'",
))
})
})
.transpose()?
.unwrap_or_default(),
filter_layout: filter_layout
.map(|layout| {
serde_json::from_str(&format!("\"{layout}\"")).map_err(|_err| {
pyo3::exceptions::PyValueError::new_err(format!(
"Invalid filter_layout '{layout}', must be 'iohw' | 'hwoi' | 'ohwi'",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous error message was wrong as it allowed more transposed filter layouts than possible (copied from conv2dfilter layouts?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ignores the quotes 🤦

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format!("\"{layout}\"") works in a hacky way but ideally RustNN provides the conversion directly

))
})
})
.transpose()?
.unwrap_or_default(),
bias: bias.map(|b| b.id),
};

Expand Down
Loading