|
1 | | -// Copyright 2021-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 1 | +// Copyright 2021-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
2 | 2 | // |
3 | 3 | // Redistribution and use in source and binary forms, with or without |
4 | 4 | // modification, are permitted provided that the following conditions |
@@ -104,6 +104,28 @@ PyDefaultArgumentToMutableType(const py::object& argument) |
104 | 104 | std::string(py::str(argument.get_type()))); |
105 | 105 | } |
106 | 106 |
|
| 107 | +std::string |
| 108 | +PyParametersToJSON(const py::dict& parameters) |
| 109 | +{ |
| 110 | + for (const auto& pair : parameters) { |
| 111 | + if (!py::isinstance<py::str>(pair.first)) { |
| 112 | + throw PythonBackendException( |
| 113 | + "Expect parameters keys to have type str, found type " + |
| 114 | + std::string(py::str(pair.first.get_type()))); |
| 115 | + } |
| 116 | + if (!py::isinstance<py::bool_>(pair.second) && |
| 117 | + !py::isinstance<py::int_>(pair.second) && |
| 118 | + !py::isinstance<py::str>(pair.second)) { |
| 119 | + throw PythonBackendException( |
| 120 | + "Expect parameters values to have type bool/int/str, found type " + |
| 121 | + std::string(py::str(pair.second.get_type()))); |
| 122 | + } |
| 123 | + } |
| 124 | + py::module_ py_json = py::module_::import("json"); |
| 125 | + std::string parameters_str = py::str(py_json.attr("dumps")(parameters)); |
| 126 | + return parameters_str; |
| 127 | +} |
| 128 | + |
107 | 129 | void |
108 | 130 | AsyncEventFutureDoneCallback(const py::object& py_future) |
109 | 131 | { |
@@ -1714,59 +1736,41 @@ PYBIND11_EMBEDDED_MODULE(c_python_backend_utils, module) |
1714 | 1736 | py::class_<InferRequest, std::shared_ptr<InferRequest>>( |
1715 | 1737 | module, "InferenceRequest") |
1716 | 1738 | .def( |
1717 | | - py::init([](const std::string& request_id, |
1718 | | - const py::object& correlation_id, |
1719 | | - const std::vector<std::shared_ptr<PbTensor>>& inputs, |
1720 | | - const std::vector<std::string>& requested_output_names, |
1721 | | - const std::string& model_name, |
1722 | | - const int64_t model_version, const uint32_t flags, |
1723 | | - const uint64_t timeout, |
1724 | | - const PreferredMemory& preferred_memory, |
1725 | | - const InferenceTrace& trace, |
1726 | | - const py::object& parameters_) { |
1727 | | - py::dict parameters = |
1728 | | - PyDefaultArgumentToMutableType<py::dict>(parameters_); |
1729 | | - std::set<std::string> requested_outputs; |
1730 | | - for (auto& requested_output_name : requested_output_names) { |
1731 | | - requested_outputs.emplace(requested_output_name); |
1732 | | - } |
1733 | | - for (const auto& pair : parameters) { |
1734 | | - if (!py::isinstance<py::str>(pair.first)) { |
1735 | | - throw PythonBackendException( |
1736 | | - "Expect parameters keys to have type str, found type " + |
1737 | | - std::string(py::str(pair.first.get_type()))); |
1738 | | - } |
1739 | | - if (!py::isinstance<py::bool_>(pair.second) && |
1740 | | - !py::isinstance<py::int_>(pair.second) && |
1741 | | - !py::isinstance<py::str>(pair.second)) { |
1742 | | - throw PythonBackendException( |
1743 | | - "Expect parameters values to have type bool/int/str, found " |
1744 | | - "type " + |
1745 | | - std::string(py::str(pair.second.get_type()))); |
1746 | | - } |
1747 | | - } |
1748 | | - py::module_ py_json = py::module_::import("json"); |
1749 | | - std::string parameters_str = |
1750 | | - py::str(py_json.attr("dumps")(parameters)); |
1751 | | - |
1752 | | - CorrelationId correlation_id_obj; |
1753 | | - if (py::isinstance<py::int_>(correlation_id)) { |
1754 | | - correlation_id_obj = |
1755 | | - CorrelationId(py::cast<uint64_t>(correlation_id)); |
1756 | | - } else if (py::isinstance<py::str>(correlation_id)) { |
1757 | | - correlation_id_obj = |
1758 | | - CorrelationId(py::cast<std::string>(correlation_id)); |
1759 | | - } else { |
1760 | | - throw PythonBackendException( |
1761 | | - "Correlation ID must be integer or string"); |
1762 | | - } |
1763 | | - |
1764 | | - return std::make_shared<InferRequest>( |
1765 | | - request_id, correlation_id_obj, inputs, requested_outputs, |
1766 | | - model_name, model_version, parameters_str, flags, timeout, |
1767 | | - 0 /*response_factory_address*/, 0 /*request_address*/, |
1768 | | - preferred_memory, trace); |
1769 | | - }), |
| 1739 | + py::init( |
| 1740 | + [](const std::string& request_id, |
| 1741 | + const py::object& correlation_id, |
| 1742 | + const std::vector<std::shared_ptr<PbTensor>>& inputs, |
| 1743 | + const std::vector<std::string>& requested_output_names, |
| 1744 | + const std::string& model_name, const int64_t model_version, |
| 1745 | + const uint32_t flags, const uint64_t timeout, |
| 1746 | + const PreferredMemory& preferred_memory, |
| 1747 | + const InferenceTrace& trace, const py::object& parameters_) { |
| 1748 | + py::dict parameters = |
| 1749 | + PyDefaultArgumentToMutableType<py::dict>(parameters_); |
| 1750 | + std::set<std::string> requested_outputs; |
| 1751 | + for (auto& requested_output_name : requested_output_names) { |
| 1752 | + requested_outputs.emplace(requested_output_name); |
| 1753 | + } |
| 1754 | + std::string parameters_str = PyParametersToJSON(parameters); |
| 1755 | + |
| 1756 | + CorrelationId correlation_id_obj; |
| 1757 | + if (py::isinstance<py::int_>(correlation_id)) { |
| 1758 | + correlation_id_obj = |
| 1759 | + CorrelationId(py::cast<uint64_t>(correlation_id)); |
| 1760 | + } else if (py::isinstance<py::str>(correlation_id)) { |
| 1761 | + correlation_id_obj = |
| 1762 | + CorrelationId(py::cast<std::string>(correlation_id)); |
| 1763 | + } else { |
| 1764 | + throw PythonBackendException( |
| 1765 | + "Correlation ID must be integer or string"); |
| 1766 | + } |
| 1767 | + |
| 1768 | + return std::make_shared<InferRequest>( |
| 1769 | + request_id, correlation_id_obj, inputs, requested_outputs, |
| 1770 | + model_name, model_version, parameters_str, flags, timeout, |
| 1771 | + 0 /*response_factory_address*/, 0 /*request_address*/, |
| 1772 | + preferred_memory, trace); |
| 1773 | + }), |
1770 | 1774 | py::arg("request_id").none(false) = "", |
1771 | 1775 | py::arg("correlation_id").none(false) = 0, |
1772 | 1776 | py::arg("inputs").none(false), |
@@ -1869,16 +1873,25 @@ PYBIND11_EMBEDDED_MODULE(c_python_backend_utils, module) |
1869 | 1873 | py::class_<InferResponse, std::shared_ptr<InferResponse>>( |
1870 | 1874 | module, "InferenceResponse") |
1871 | 1875 | .def( |
1872 | | - py::init< |
1873 | | - const std::vector<std::shared_ptr<PbTensor>>&, |
1874 | | - std::shared_ptr<PbError>>(), |
| 1876 | + py::init( |
| 1877 | + [](const std::vector<std::shared_ptr<PbTensor>>& output_tensors, |
| 1878 | + const std::shared_ptr<PbError>& error, |
| 1879 | + const py::object& parameters_) { |
| 1880 | + py::dict parameters = |
| 1881 | + PyDefaultArgumentToMutableType<py::dict>(parameters_); |
| 1882 | + std::string parameters_str = PyParametersToJSON(parameters); |
| 1883 | + return std::make_shared<InferResponse>( |
| 1884 | + output_tensors, error, parameters_str /* parameters */); |
| 1885 | + }), |
1875 | 1886 | py::arg("output_tensors") = py::list(), |
1876 | | - py::arg("error") = static_cast<std::shared_ptr<PbError>>(nullptr)) |
| 1887 | + py::arg("error") = static_cast<std::shared_ptr<PbError>>(nullptr), |
| 1888 | + py::arg("parameters") = py::none()) |
1877 | 1889 | .def( |
1878 | 1890 | "output_tensors", &InferResponse::OutputTensors, |
1879 | 1891 | py::return_value_policy::reference) |
1880 | 1892 | .def("has_error", &InferResponse::HasError) |
1881 | | - .def("error", &InferResponse::Error); |
| 1893 | + .def("error", &InferResponse::Error) |
| 1894 | + .def("parameters", &InferResponse::Parameters); |
1882 | 1895 |
|
1883 | 1896 | py::class_<ResponseSender, std::shared_ptr<ResponseSender>>( |
1884 | 1897 | module, "InferenceResponseSender") |
|
0 commit comments