Repository: onnx/onnx
File: onnx/cpp2py_export.cc (C++)

Code before:
    auto tensor = ctx->getInputData(index);
    if (tensor == nullptr) {
      fail_shape_inference("Internal error: `tensor` is nullptr in `__get_input_data`");
    }
    std::string data;
    tensor->SerializeToString(&data);
    return py::bytes(data);
  });
  inference_ctx.def("__get_input_sparse_data", [](InferenceContext* ctx, size_t index) {
    if (ctx == nullptr) {
      fail_shape_inference("Internal error: `ctx` is nullptr in `__get_input_sparse_data`");
    }
    auto stensor = ctx->getInputSparseData(index);
    if (stensor == nullptr) {
      fail_shape_inference("Internal error: `stensor` is nullptr in `__get_input_sparse_data`");
    }
    std::string data;
    stensor->SerializeToString(&data);
    return py::bytes(data);
  });
  inference_ctx.def("__get_symbolic_input", [](InferenceContext* ctx, size_t index) {
    if (ctx == nullptr) {
      fail_shape_inference("Internal error: `ctx` is nullptr in `__get_symbolic_input`");
    }
    auto shape = ctx->getSymbolicInput(index);
    if (shape == nullptr) {
      fail_shape_inference("Internal error: `shape` is nullptr in `__get_symbolic_input`");
    }
    std::string data;
    shape->SerializeToString(&data);
    return py::bytes(data);
  });
  inference_ctx.def("__get_graph_attribute_inferencer", &InferenceContext::getGraphAttributeInferencer);
  inference_ctx.def("get_num_outputs", &InferenceContext::getNumOutputs);
  inference_ctx.def("__get_output_type", [](InferenceContext* ctx, size_t index) {
    if (ctx == nullptr) {
      fail_shape_inference("Internal error: `ctx` is nullptr in `__get_output_type`");
    }
    auto type = ctx->getOutputType(index);
    if (type == nullptr) {
      fail_shape_inference("Internal error: `type` is nullptr in `__get_output_type`");
    }
    std::string data;
    type->SerializeToString(&data);
    return py::bytes(data);
  });
  inference_ctx.def("__set_output_type", [](InferenceContext* ctx, size_t index, py::bytes bytes) {
    if (ctx == nullptr) {
      fail_shape_inference("Internal error: `ctx` is nullptr in `__set_output_type`");
    }
    auto type = ctx->getOutputType(index);

Review comment (suggestion):
```suggestion
  py::class_<InferenceContext> inference_context(shape_inference, "InferenceContext", "Inference context");
```



Code after:
  auto version_converter = onnx_cpp2py_export.def_submodule("version_converter");
  version_converter.doc() = "VersionConverter submodule";
  py::register_exception<ConvertError>(version_converter, "ConvertError");

  version_converter.def("convert_version", [](const py::bytes& bytes, const py::int_& target) {
    ModelProto proto{};
    ParseProtoFromPyBytes(&proto, bytes);
    shape_inference::InferShapes(proto);
    auto result = version_conversion::ConvertVersion(proto, target);
    std::string out;
    result.SerializeToString(&out);
    return py::bytes(out);
  });

  // Submodule `inliner`
  auto inliner = onnx_cpp2py_export.def_submodule("inliner");
  inliner.doc() = "Inliner submodule";

  inliner.def("inline_local_functions", [](const py::bytes& bytes, bool convert_version) {
    ModelProto model{};
    ParseProtoFromPyBytes(&model, bytes);
    inliner::InlineLocalFunctions(model, convert_version);
    std::string out;
    model.SerializeToString(&out);
    return py::bytes(out);
  });

  // inline_selected_functions: Inlines all functions specified in function_ids, unless
  // exclude is true, in which case it inlines all functions except those specified in
  // function_ids.
  inliner.def(
      "inline_selected_functions",
      [](const py::bytes& bytes, std::vector<std::pair<std::string, std::string>> function_ids, bool exclude) {
        ModelProto model{};
        ParseProtoFromPyBytes(&model, bytes);
        auto function_id_set = inliner::FunctionIdSet::Create(std::move(function_ids), exclude);
        inliner::InlineSelectedFunctions(model, *function_id_set);
        std::string out;
        model.SerializeToString(&out);
        return py::bytes(out);
      });

  // Submodule `shape_inference`
  auto shape_inference = onnx_cpp2py_export.def_submodule("shape_inference");
  shape_inference.doc() = "Shape Inference submodule";
  py::register_exception<InferenceError>(shape_inference, "InferenceError");

  py::class_<InferenceContext> inference_context(shape_inference, "InferenceContext", "Inference context");

  inference_context.def("get_attribute", &InferenceContext::getAttribute);
  inference_context.def("get_num_inputs", &InferenceContext::getNumInputs);