Repository: microsoft/onnxruntime
File: winml/adapter/winml_adapter_model.cpp (C++)

Code before:
    } else {
      mutable_tensor_type->mutable_shape()->add_dim()->set_dim_value(shape[i]);
    }
  }

  if (shape_len > 0) {
    mutable_tensor_type->mutable_shape()->mutable_dim(0)->set_denotation("DATA_BATCH");
  }
}

ORT_API_STATUS_IMPL(
  winmla::ModelAddInput, _In_ OrtModel* in, _In_ const char* const input_name, _In_ OrtTypeInfo* info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::ValueInfoProto& input = *graph.add_input();
  input.set_name(input_name);

  if (info->type == ONNXType::ONNX_TYPE_TENSOR) {
    auto num_dims = info->tensor_type_info->GetShape()->GetDims().size();
    CreateTypeProto_Tensor(
      input.mutable_type()->mutable_tensor_type(),
      input_name,
      (num_dims == 0) ? nullptr : info->tensor_type_info->GetShape()->GetDims().data(),
      num_dims,
      ONNXTensorElementDataTypeToTensorProto_DataType(info->tensor_type_info->GetElementType())
    );
  }
  return nullptr;
  API_IMPL_END
}

ORT_API_STATUS_IMPL(
  winmla::ModelAddConstantInput,
  _In_ OrtModel* in,
  _In_ const char* const input_name,
  _In_ OrtTypeInfo* info,
  _In_ OrtValue* value
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::TensorProto& input = *graph.add_initializer();
  input.set_name(input_name);

  auto num_dims = info->tensor_type_info->GetShape()->GetDims().size();
  for (size_t i = 0; i < num_dims; i++) {
    input.add_dims(info->tensor_type_info->GetShape()->GetDims()[i]);

Review comment (bug):
`OrtTensorTypeAndShapeInfo::GetShape()` may return `nullptr` [if there is no shape](https://github.com/microsoft/onnxruntime/blob/f501e1d0c7ed4c454bb5b3c3edee791068ec3472/onnxruntime/core/framework/tensor_type_and_shape.h#L43). 

Perhaps this API function should return an error status if `info->tensor_type_info->HasShape()` returns false.

```c++
if (info->type == ONNXType::ONNX_TYPE_TENSOR) {
  if (!info->tensor_type_info->HasShape()) {
    return OrtApis::CreateStatus(ORT_INVALID_ARGUMENT, "Model input tensor must have a shape");
  }
}
```

Code after:
    } else {
      mutable_tensor_type->mutable_shape()->add_dim()->set_dim_value(shape[i]);
    }
  }

  if (shape_len > 0) {
    mutable_tensor_type->mutable_shape()->mutable_dim(0)->set_denotation("DATA_BATCH");
  }
}

ORT_API_STATUS_IMPL(
  winmla::ModelAddInput, _In_ OrtModel* in, _In_ const char* const input_name, _In_ OrtTypeInfo* info
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::ValueInfoProto& input = *graph.add_input();
  input.set_name(input_name);

  if (info->type == ONNXType::ONNX_TYPE_TENSOR) {
    auto num_dims = info->tensor_type_info->HasShape() ? info->tensor_type_info->GetShape()->GetDims().size() : 0;
    CreateTypeProto_Tensor(
      input.mutable_type()->mutable_tensor_type(),
      input_name,
      (num_dims == 0) ? nullptr : info->tensor_type_info->GetShape()->GetDims().data(),
      num_dims,
      ONNXTensorElementDataTypeToTensorProto_DataType(info->tensor_type_info->GetElementType())
    );
  }
  return nullptr;
  API_IMPL_END
}

ORT_API_STATUS_IMPL(
  winmla::ModelAddConstantInput,
  _In_ OrtModel* in,
  _In_ const char* const input_name,
  _In_ OrtTypeInfo* info,
  _In_ OrtValue* value
) {
  API_IMPL_BEGIN
  const OrtModelImpl* model = in->ToInternal();
  auto model_proto = model->UseModelProto();
  ONNX_NAMESPACE::GraphProto& graph = *model_proto->mutable_graph();
  ONNX_NAMESPACE::TensorProto& input = *graph.add_initializer();
  input.set_name(input_name);

  auto num_dims = info->tensor_type_info->HasShape() ? info->tensor_type_info->GetShape()->GetDims().size() : 0;
  for (size_t i = 0; i < num_dims; i++) {
    input.add_dims(info->tensor_type_info->GetShape()->GetDims()[i]);