Repository: microsoft/onnxruntime
File: onnxruntime/core/providers/webnn/builders/impl/reduction_op_builder.cc (C++)

Code before:
        // Use that initializer data.
        const auto& initializers(model_builder.GetInitializerTensors());
        const auto& axes_tensor = *initializers.at(input_defs[1]->Name());
        Initializer axes_initializer(axes_tensor);
        const auto axes_data_span = axes_initializer.DataAsSpan<int64_t>();
        axes_data = HandleNegativeAxes(axes_data_span, input_rank);
      }
    }
  } else {
    if (helper.HasAttr("axes")) {
      axes_data = GetResolvedAxes(helper, input_rank);
    }
  }

  if (axes_data.empty()) {
    // When axes is not provided or is empty, check the 'noop_with_empty_axes' attribute:
    // - If it is false, perform reduction over all dimensions.
    //   (In WebNN, this means the 'axes' option is not set.)
    // - If it is true, no reduction is applied, but other operations are still performed.
    //   (In WebNN, this requires setting 'axes' to an empty array.)
    if (helper.Get("noop_with_empty_axes", 0) == 1) {
      options.set("axes", emscripten::val::array());
    }
  } else {
    options.set("axes", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(axes_data)));
  }

  const std::string_view webnn_op_type = GetWebNNOpType(op_type);
  ORT_RETURN_IF(webnn_op_type.empty(), "Cannot get WebNN op type");

  emscripten::val output = model_builder.GetBuilder().call<emscripten::val>(
      std::string(webnn_op_type).c_str(), input, options);

  model_builder.AddOperand(node.OutputDefs()[0]->Name(), std::move(output));
  return Status::OK();
}

// Operator support related.
bool ReductionOpBuilder::IsOpSupportedImpl(const GraphViewer& graph_viewer,
                                           const Node& node,
                                           const WebnnDeviceType /* device_type */,
                                           const logging::Logger& logger) const {
  const auto& input_defs = node.InputDefs();

  if (TensorExists(input_defs, 1)) {
    std::vector<int64_t> axes_shape;
    if (!GetShape(*input_defs[1], axes_shape, logger)) {
      LOGS(logger, VERBOSE) << "Cannot get shape of input axes";
      return false;
    }


Review comment (refactor):
Would this work?

```c++
  // When axes is not provided or is empty, check the 'noop_with_empty_axes' attribute:
  // - If it is false, perform reduction over all dimensions.
  //   (In WebNN, this means the 'axes' option is not set.)
  // - If it is true, no reduction is applied, but other operations are still performed.
  //   (In WebNN, this requires setting 'axes' to an empty array.)
  if (!axes_data.empty() || helper.Get("noop_with_empty_axes", 0) == 1) {
    options.set("axes", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(axes_data)));
  }
```

Code after:
        // Use that initializer data.
        const auto& initializers(model_builder.GetInitializerTensors());
        const auto& axes_tensor = *initializers.at(input_defs[1]->Name());
        Initializer axes_initializer(axes_tensor);
        const auto axes_data_span = axes_initializer.DataAsSpan<int64_t>();
        axes_data = HandleNegativeAxes(axes_data_span, input_rank);
      }
    }
  } else {
    if (helper.HasAttr("axes")) {
      axes_data = GetResolvedAxes(helper, input_rank);
    }
  }

  // When axes is not provided or is empty, check the 'noop_with_empty_axes' attribute:
  // - If it is false, perform reduction over all dimensions.
  //   (In WebNN, this means the 'axes' option is not set.)
  // - If it is true, no reduction is applied, but other operations are still performed.
  //   (In WebNN, this requires setting 'axes' to an empty array.)
  if (!axes_data.empty() || helper.Get("noop_with_empty_axes", 0) == 1) {
    options.set("axes", emscripten::val::array(GetNarrowedIntFromInt64<uint32_t>(axes_data)));
  }

  const std::string_view webnn_op_type = GetWebNNOpType(op_type);
  ORT_RETURN_IF(webnn_op_type.empty(), "Cannot get WebNN op type");

  emscripten::val output = model_builder.GetBuilder().call<emscripten::val>(
      std::string(webnn_op_type).c_str(), input, options);

  model_builder.AddOperand(node.OutputDefs()[0]->Name(), std::move(output));
  return Status::OK();
}

// Operator support related.
bool ReductionOpBuilder::IsOpSupportedImpl(const GraphViewer& graph_viewer,
                                           const Node& node,
                                           const WebnnDeviceType /* device_type */,
                                           const logging::Logger& logger) const {
  const auto& input_defs = node.InputDefs();

  if (TensorExists(input_defs, 1)) {
    std::vector<int64_t> axes_shape;
    if (!GetShape(*input_defs[1], axes_shape, logger)) {
      LOGS(logger, VERBOSE) << "Cannot get shape of input axes";
      return false;
    }

    if (axes_shape.size() != 1) {
      LOGS(logger, VERBOSE) << "Input axes of " << node.OpType() << " must be 1D";
      return false;
    }