Repository: huggingface/candle
File: candle-onnx/src/eval.rs (Rust)

Code before:
                    bail!("Either scales or sizes must be provided for Resize operation");
                }

                // Get attributes with their default values according to ONNX spec
                let mode = get_attr_opt::<str>(node, "mode")?.unwrap_or("nearest");
                let coordinate_transformation_mode =
                    get_attr_opt::<str>(node, "coordinate_transformation_mode")?
                        .unwrap_or("half_pixel");

                let output_dims = if let Some(sizes_tensor) = sizes {
                    // Use specified sizes
                    sizes_tensor
                        .to_vec1::<i64>()?
                        .iter()
                        .map(|&d| d as usize)
                        .collect::<Vec<_>>()
                } else if let Some(scales_tensor) = scales {
                    // Calculate from scales
                    let scale_values = scales_tensor.to_vec1::<f32>()?;
                    input.dims()
                        .iter()
                        .enumerate()
                        .map(|(i, &d)| (d as f32 * scale_values[i]) as usize)
                        .collect::<Vec<_>>()
                } else {
                    unreachable!("Either scales or sizes should be present");
                };

                let output = match mode {
                    "nearest" => match coordinate_transformation_mode {
                        "asymmetric" => {
                            if input.rank() == 4 {
                                let h = output_dims[2];
                                let w = output_dims[3];
                                input.upsample_nearest2d(h, w)?
                            } else {
                                bail!("Unsupported rank for nearest resize: {}", input.rank())
                            }
                        }
                        _ => bail!(
                            "Unsupported coordinate_transformation_mode for nearest: {}",
                            coordinate_transformation_mode
                        ),
                    },
                    _ => bail!("Unsupported resize mode: {}", mode),
                };

                values.insert(node.output[0].clone(), output);
            }
            op_type => bail!("unsupported op_type {op_type} for op {node:?}"),
        }

Review comment (refactor):
I would use `bail!` rather than unreachable here just in case, we do the check with `is_none()` above but it's possible that the check will be removed through some refactoring. Actually I would even remove the check above so that it's entirely handled here.
Also what about the case where both `scales` and `sizes` are set, should we have an error or is there a reason to choose `sizes` first?

Code after:
                    None
                };

                let output_dims = match (scales, sizes) {
                    (Some(_), Some(_)) => {
                        bail!("Scales and sizes cannot both be set for Resize operation")
                    }
                    (Some(scales_tensor), None) => {
                        let scale_values = scales_tensor.to_vec1::<f32>()?;
                        input
                            .dims()
                            .iter()
                            .enumerate()
                            .map(|(i, &d)| (d as f32 * scale_values[i]) as usize)
                            .collect::<Vec<_>>()
                    }
                    (None, Some(sizes_tensor)) => sizes_tensor
                        .to_vec1::<i64>()?
                        .iter()
                        .map(|&d| d as usize)
                        .collect::<Vec<_>>(),
                    (None, None) => bail!("Either scales or sizes should be present"),
                };

                let coordinate_transformation_mode =
                    get_attr_opt::<str>(node, "coordinate_transformation_mode")?
                        .unwrap_or("half_pixel");
                // Interpolation mode: nearest, linear, or cubic.
                let mode = get_attr_opt::<str>(node, "mode")?.unwrap_or("nearest");
                // How to determine the "nearest" pixel in nearest interpolation mode.
                let nearest_mode =
                    get_attr_opt::<str>(node, "nearest_mode")?.unwrap_or("round_prefer_floor");

                if mode != "nearest" {
                    bail!("Unsupported resize mode: {}", mode);
                }

                if nearest_mode != "floor" {
                    bail!("Unsupported nearest_mode for resize: {}", nearest_mode);
                }

                if coordinate_transformation_mode != "asymmetric" {
                    bail!(
                        "Unsupported coordinate_transformation_mode for resize: {}",
                        coordinate_transformation_mode
                    );
                }

                let h = output_dims[2];
                let w = output_dims[3];
                let output = input.upsample_nearest2d(h, w)?;