// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. // This file is excluded from the build if onnxruntime_PYBIND_EXPORT_OPSCHEMA not defined #include "python/onnxruntime_pybind_state_common.h" #include "core/framework/kernel_registry.h" #include namespace py = pybind11; namespace onnxruntime { namespace python { void addGlobalSchemaFunctions(pybind11::module& m) { m.def( "get_all_operator_schema", []() -> const std::vector { return ONNX_NAMESPACE::OpSchemaRegistry::get_all_schemas_with_history(); }, "Return a vector of OpSchema all registed operators"); m.def( "get_all_opkernel_def", []() -> const std::vector { std::vector result; std::vector> factories = { onnxruntime::CPUProviderFactoryCreator::Create(0), #ifdef USE_CUDA []() { OrtCUDAProviderOptions provider_options{}; return CudaProviderFactoryCreator::Create(&provider_options); }(), #endif #ifdef USE_ROCM onnxruntime::RocmProviderFactoryCreator::Create( [&]() { ROCMExecutionProviderInfo info{}; info.device_id = cuda_device_id; info.gpu_mem_limit = gpu_mem_limit; info.arena_extend_strategy = arena_extend_strategy; info.external_allocator_info = external_allocator_info; return info; }()), #endif #ifdef USE_DNNL onnxruntime::DnnlProviderFactoryCreator::Create(1), #endif #ifdef USE_OPENVINO onnxruntime::OpenVINOProviderFactoryCreator::Create(OrtOpenVINOProviderOptions()), #endif #ifdef USE_TENSORRT onnxruntime::TensorrtProviderFactoryCreator::Create(0), #endif #ifdef USE_MIGRAPHX onnxruntime::MIGraphXProviderFactoryCreator::Create(0), #endif #ifdef USE_VITISAI onnxruntime::VitisAIProviderFactoryCreator::Create("DPUCADX8G", 0, "", ""), #endif #ifdef USE_ACL onnxruntime::ACLProviderFactoryCreator::Create(0), #endif #ifdef USE_ARMNN onnxruntime::ArmNNProviderFactoryCreator::Create(0), #endif #ifdef USE_DML onnxruntime::DMLProviderFactoryCreator::Create(0), #endif #ifdef USE_NNAPI onnxruntime::NnapiProviderFactoryCreator::Create(0), #endif #ifdef USE_RKNPU onnxruntime::RknpuProviderFactoryCreator::Create(), #endif #ifdef USE_COREML onnxruntime::CoreMLProviderFactoryCreator::Create(0), #endif }; for (const auto& f : factories) { for (const auto& m : f->CreateProvider() ->GetKernelRegistry() ->GetKernelCreateMap()) { result.emplace_back(*(m.second.kernel_def)); } } return result; }, "Return a vector of KernelDef for all registered OpKernels"); } void addOpKernelSubmodule(py::module& m) { auto opkernel = m.def_submodule("opkernel"); opkernel.doc() = "OpKernel submodule"; py::class_ kernel_def(opkernel, "KernelDef"); kernel_def.def_property_readonly("op_name", &onnxruntime::KernelDef::OpName) .def_property_readonly("domain", &onnxruntime::KernelDef::Domain) .def_property_readonly("provider", &onnxruntime::KernelDef::Provider) .def_property_readonly("version_range", [](const onnxruntime::KernelDef& kernelDef) -> std::pair { return kernelDef.onnxruntime::KernelDef::SinceVersion(); }) .def_property_readonly("type_constraints", [](const onnxruntime::KernelDef& kernelDef) -> std::unordered_map> { std::unordered_map> result; const auto& tempResult = kernelDef.TypeConstraints(); for (const auto& tc : tempResult) { result[tc.first] = std::vector(); for (const auto& dt : tc.second) { result[tc.first].emplace_back(onnxruntime::DataTypeImpl::ToString(dt)); } } return result; }); } void addOpSchemaSubmodule(py::module& m) { auto schemadef = m.def_submodule("schemadef"); schemadef.doc() = "Schema submodule"; // Keep this binding local to this module py::class_ op_schema(schemadef, "OpSchema", py::module_local()); op_schema.def_property_readonly("file", &ONNX_NAMESPACE::OpSchema::file) .def_property_readonly("line", &ONNX_NAMESPACE::OpSchema::line) .def_property_readonly("support_level", &ONNX_NAMESPACE::OpSchema::support_level) .def_property_readonly( "doc", &ONNX_NAMESPACE::OpSchema::doc, py::return_value_policy::reference) .def_property_readonly("since_version", &ONNX_NAMESPACE::OpSchema::since_version) .def_property_readonly("deprecated", &ONNX_NAMESPACE::OpSchema::deprecated) .def_property_readonly("domain", &ONNX_NAMESPACE::OpSchema::domain) .def_property_readonly("name", &ONNX_NAMESPACE::OpSchema::Name) .def_property_readonly("min_input", &ONNX_NAMESPACE::OpSchema::min_input) .def_property_readonly("max_input", &ONNX_NAMESPACE::OpSchema::max_input) .def_property_readonly("min_output", &ONNX_NAMESPACE::OpSchema::min_output) .def_property_readonly("max_output", &ONNX_NAMESPACE::OpSchema::max_output) .def_property_readonly("attributes", &ONNX_NAMESPACE::OpSchema::attributes) .def_property_readonly("inputs", &ONNX_NAMESPACE::OpSchema::inputs) .def_property_readonly("outputs", &ONNX_NAMESPACE::OpSchema::outputs) .def_property_readonly( "has_type_and_shape_inference_function", &ONNX_NAMESPACE::OpSchema::has_type_and_shape_inference_function) .def_property_readonly( "type_constraints", &ONNX_NAMESPACE::OpSchema::typeConstraintParams) .def_static("is_infinite", [](int v) { return v == std::numeric_limits::max(); }); // Keep this binding local to this module py::class_(op_schema, "Attribute", py::module_local()) .def_readonly("name", &ONNX_NAMESPACE::OpSchema::Attribute::name) .def_readonly("description", &ONNX_NAMESPACE::OpSchema::Attribute::description) .def_readonly("type", &ONNX_NAMESPACE::OpSchema::Attribute::type) .def_property_readonly( "_default_value", [](ONNX_NAMESPACE::OpSchema::Attribute* attr) -> py::bytes { std::string out; attr->default_value.SerializeToString(&out); return out; }) .def_readonly("required", &ONNX_NAMESPACE::OpSchema::Attribute::required); // Keep this binding local to this module py::class_(op_schema, "TypeConstraintParam", py::module_local()) .def_readonly( "type_param_str", &ONNX_NAMESPACE::OpSchema::TypeConstraintParam::type_param_str) .def_readonly("description", &ONNX_NAMESPACE::OpSchema::TypeConstraintParam::description) .def_readonly( "allowed_type_strs", &ONNX_NAMESPACE::OpSchema::TypeConstraintParam::allowed_type_strs); // Keep this binding local to this module py::enum_(op_schema, "FormalParameterOption", py::module_local()) .value("Single", ONNX_NAMESPACE::OpSchema::Single) .value("Optional", ONNX_NAMESPACE::OpSchema::Optional) .value("Variadic", ONNX_NAMESPACE::OpSchema::Variadic); // Keep this binding local to this module py::class_(op_schema, "FormalParameter", py::module_local()) .def_property_readonly("name", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetName) .def_property_readonly("types", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetTypes) .def_property_readonly("typeStr", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetTypeStr) .def_property_readonly( "description", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetDescription) .def_property_readonly("option", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetOption) .def_property_readonly( "isHomogeneous", &ONNX_NAMESPACE::OpSchema::FormalParameter::GetIsHomogeneous); // Keep this binding local to this module py::enum_(op_schema, "AttrType", py::module_local()) .value("FLOAT", ONNX_NAMESPACE::AttributeProto::FLOAT) .value("INT", ONNX_NAMESPACE::AttributeProto::INT) .value("STRING", ONNX_NAMESPACE::AttributeProto::STRING) .value("TENSOR", ONNX_NAMESPACE::AttributeProto::TENSOR) .value("SPARSE_TENSOR", ONNX_NAMESPACE::AttributeProto::SPARSE_TENSOR) .value("GRAPH", ONNX_NAMESPACE::AttributeProto::GRAPH) .value("FLOATS", ONNX_NAMESPACE::AttributeProto::FLOATS) .value("INTS", ONNX_NAMESPACE::AttributeProto::INTS) .value("STRINGS", ONNX_NAMESPACE::AttributeProto::STRINGS) .value("TENSORS", ONNX_NAMESPACE::AttributeProto::TENSORS) .value("SPARSE_TENSORS", ONNX_NAMESPACE::AttributeProto::SPARSE_TENSORS) .value("GRAPHS", ONNX_NAMESPACE::AttributeProto::GRAPHS); // Keep this binding local to this module py::enum_(op_schema, "SupportType", py::module_local()) .value("COMMON", ONNX_NAMESPACE::OpSchema::SupportType::COMMON) .value("EXPERIMENTAL", ONNX_NAMESPACE::OpSchema::SupportType::EXPERIMENTAL); } } // namespace python } // namespace onnxruntime