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

Code before:
                value = *i.data<float>();
                is_constant = true;
                break;
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16:
                value = math::halfToFloat(i.data<MLFloat16>()->val);
                is_constant = true;
                break;
              default:
                is_constant = false;
                break;
            }
            return is_constant;
          }
          const Node* producer = graph.GetProducerNode(input->Name());
          if (producer && producer->OpType() == "DequantizeLinear") {
            const auto& dq_inputs = producer->InputDefs();
            const ONNX_NAMESPACE::TensorProto* dq_input = graph.GetConstantInitializer(dq_inputs[0]->Name(), true);
            const ONNX_NAMESPACE::TensorProto* dq_scale = graph.GetConstantInitializer(dq_inputs[1]->Name(), true);
            const ONNX_NAMESPACE::TensorProto* dq_zero_point = graph.GetConstantInitializer(dq_inputs[2]->Name(), true);
            if (!dq_input || !dq_scale || !dq_zero_point) {
              return false;
            }
            // Check scale and zero_point are scalar
            Initializer scale_initializer(graph, *dq_scale, graph.ModelPath());
            Initializer zero_point_initializer(graph, *dq_zero_point, graph.ModelPath());
            if (scale_initializer.dims().size() != 0 || zero_point_initializer.dims().size() != 0) {
              return false;  // Not scalar
            }
            float scale = 1.0f;
            float zero_point = 0.0f;
            // Get scale
            switch (dq_scale->data_type()) {
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT: {
                scale = *scale_initializer.data<float>();
                break;
              }
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16: {
                scale = math::halfToFloat(scale_initializer.data<MLFloat16>()->val);
                break;
              }
              default:
                return false;
            }
            // Get zero_point
            switch (dq_zero_point->data_type()) {
              case ONNX_NAMESPACE::TensorProto_DataType_UINT8: {
                zero_point = static_cast<float>(*zero_point_initializer.data<uint8_t>());
                break;
              }
              case ONNX_NAMESPACE::TensorProto_DataType_INT8: {
                zero_point = static_cast<float>(*zero_point_initializer.data<int8_t>());

Review comment (performance):
Should this also handle a shape of `(1,)` for the scale? I believe a scale of this shape is also considered per-tensor quantization.

Refer to:
https://github.com/microsoft/onnxruntime/blob/fc3bc097c941c812f481cff42594f2da1ae79097/onnxruntime/core/optimizer/transpose_optimization/onnx_transpose_optimization.cc#L29C16-L32

Code after:
          const ONNX_NAMESPACE::TensorProto* initializer = graph.GetConstantInitializer(input->Name(), true);
          if (initializer) {
            Initializer i(graph, *initializer, graph.ModelPath());
            switch (initializer->data_type()) {
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT:
                value = *i.data<float>();
                is_constant = true;
                break;
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16:
                value = math::halfToFloat(i.data<MLFloat16>()->val);
                is_constant = true;
                break;
              default:
                is_constant = false;
                break;
            }
            return is_constant;
          }
          const Node* producer = graph.GetProducerNode(input->Name());
          if (producer && producer->OpType() == "DequantizeLinear") {
            const auto& dq_inputs = producer->InputDefs();
            const ONNX_NAMESPACE::TensorProto* dq_input = graph.GetConstantInitializer(dq_inputs[0]->Name(), true);
            const ONNX_NAMESPACE::TensorProto* dq_scale = graph.GetConstantInitializer(dq_inputs[1]->Name(), true);
            const ONNX_NAMESPACE::TensorProto* dq_zero_point = graph.GetConstantInitializer(dq_inputs[2]->Name(), true);
            if (!dq_input || !dq_scale || !dq_zero_point) {
              return false;
            }
            // Check scale and zero_point are scalar
            Initializer scale_initializer(graph, *dq_scale, graph.ModelPath());
            Initializer zero_point_initializer(graph, *dq_zero_point, graph.ModelPath());
            if (!IsScalarOr1Element1DTensor(scale_initializer.dims()) || !IsScalarOr1Element1DTensor(zero_point_initializer.dims())) {
              return false;
            }
            float scale = 1.0f;
            float zero_point = 0.0f;
            // Get scale
            switch (dq_scale->data_type()) {
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT: {
                scale = *scale_initializer.data<float>();
                break;
              }
              case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16: {
                scale = math::halfToFloat(scale_initializer.data<MLFloat16>()->val);
                break;
              }
              default:
                return false;
            }
            // Get zero_point
            switch (dq_zero_point->data_type()) {
              case ONNX_NAMESPACE::TensorProto_DataType_UINT8: {