Repository: microsoft/onnxruntime
File: onnxruntime/core/optimizer/qdq_transformer/weight_bias_quantization.cc (C++)

Code before:
    if (dq_1 && bias_size == -1) {
      continue;
    }

    bool is_per_tensor_scale = true;
    // If weight is quantized, it's either per-tensor or per-channel on specific axis (0 for Conv and 1 for Gemm).
    if (dq_1 && !optimizer_utils::IsScalar(*dq_1->InputDefs()[1])) {
      is_per_tensor_scale = false;
      auto weight_scale_shape = dq_1->InputDefs()[1]->Shape();
      if (!weight_scale_shape || weight_scale_shape->dim_size() != 1 || !weight_scale_shape->dim(0).has_dim_value() ||
          weight_scale_shape->dim(0).dim_value() != bias_size) {
        continue;
      }

      const auto& dq_attrs = dq_1->GetAttributes();
      auto attr_it = dq_attrs.find("block_size");
      // Default value of block_size=0 has no significance. Don't skip weight_bias_quantization.
      if (attr_it != dq_attrs.end() && attr_it->second.i() != 0) {
        continue;
      }

      int64_t axis = 1;
      if (auto axis_iter = dq_attrs.find("axis"); axis_iter != dq_attrs.end()) {
        axis = axis_iter->second.i();
        const ONNX_NAMESPACE::TensorShapeProto* weight_shape = weight_arg->Shape();
        if (!weight_shape && dq_1 && dq_1->InputDefs()[0]) {
          weight_shape = dq_1->InputDefs()[0]->Shape();
        }
        if (axis < 0 && !weight_shape) {
          continue;
        }
        axis = HandleNegativeAxis(axis, weight_shape->dim_size());
      }

      int64_t expected_axis = 0;
      if (node.OpType() == "Gemm") {
        int64_t transB = 0;
        const auto& gemm_attrs = node.GetAttributes();
        if (auto trans_b_iter = gemm_attrs.find("transB"); trans_b_iter != gemm_attrs.end()) {
          transB = trans_b_iter->second.i();
        }
        expected_axis = transB == 0 ? 1 : 0;
      }

      if (axis != expected_axis) {
        continue;
      }
    }

    NodeArg* weight_scale_arg = nullptr;
    if (!dq_1) {

Review comment (bug):
nit: it is redundant to check for `dq_1 != nullptr` here as that ensured earlier.

https://github.com/CodeLinaro/onnxruntime/blob/01112314cc770c15db297b8a37b31ea9110dcb04/onnxruntime/core/optimizer/qdq_transformer/weight_bias_quantization.cc#L114

Code after:
    if (dq_1 && bias_size == -1) {
      continue;
    }

    bool is_per_tensor_scale = true;
    // If weight is quantized, it's either per-tensor or per-channel on specific axis (0 for Conv and 1 for Gemm).
    if (dq_1 && !optimizer_utils::IsScalar(*dq_1->InputDefs()[1])) {
      is_per_tensor_scale = false;
      auto weight_scale_shape = dq_1->InputDefs()[1]->Shape();
      if (!weight_scale_shape || weight_scale_shape->dim_size() != 1 || !weight_scale_shape->dim(0).has_dim_value() ||
          weight_scale_shape->dim(0).dim_value() != bias_size) {
        continue;
      }

      const auto& dq_attrs = dq_1->GetAttributes();
      auto attr_it = dq_attrs.find("block_size");
      // Default value of block_size=0 has no significance. Don't skip weight_bias_quantization.
      if (attr_it != dq_attrs.end() && attr_it->second.i() != 0) {
        continue;
      }

      int64_t axis = 1;
      if (auto axis_iter = dq_attrs.find("axis"); axis_iter != dq_attrs.end()) {
        axis = axis_iter->second.i();
        const ONNX_NAMESPACE::TensorShapeProto* weight_shape = weight_arg->Shape();
        if (!weight_shape && dq_1->InputDefs()[0]) {
          weight_shape = dq_1->InputDefs()[0]->Shape();
        }
        if (axis < 0 && !weight_shape) {
          continue;
        }
        axis = HandleNegativeAxis(axis, weight_shape->dim_size());
      }

      int64_t expected_axis = 0;
      if (node.OpType() == "Gemm") {
        int64_t transB = 0;
        const auto& gemm_attrs = node.GetAttributes();
        if (auto trans_b_iter = gemm_attrs.find("transB"); trans_b_iter != gemm_attrs.end()) {
          transB = trans_b_iter->second.i();
        }
        expected_axis = transB == 0 ? 1 : 0;
      }

      if (axis != expected_axis) {
        continue;
      }
    }

    NodeArg* weight_scale_arg = nullptr;
    if (!dq_1) {