diff --git a/src/api/api.cpp b/src/api/api.cpp index 30d27a11e22..5c37462641d 100644 --- a/src/api/api.cpp +++ b/src/api/api.cpp @@ -289,6 +289,13 @@ static void quantize_fp8_wrap(program& prog, const target& t, quantize_fp8_optio migraphx::quantize_fp8(prog, t, options.calibration); } +static size_t get_onnx_operators_size() { return migraphx::get_onnx_operators().size(); } + +static char* get_onnx_operator_name_at_index(std::size_t index) +{ + return const_cast(get_onnx_operators().at(index).c_str()); // NOLINT +} + #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral" @@ -2416,6 +2423,19 @@ extern "C" migraphx_status migraphx_quantize_fp8(migraphx_program_t prog, return api_error_result; } +extern "C" migraphx_status migraphx_get_onnx_operator_name_at_index(char** out, size_t index) +{ + auto api_error_result = + migraphx::try_([&] { *out = migraphx::get_onnx_operator_name_at_index((index)); }); + return api_error_result; +} + +extern "C" migraphx_status migraphx_get_onnx_operators_size(size_t* out) +{ + auto api_error_result = migraphx::try_([&] { *out = migraphx::get_onnx_operators_size(); }); + return api_error_result; +} + extern "C" migraphx_status migraphx_context_finish(const_migraphx_context_t context) { auto api_error_result = migraphx::try_([&] { diff --git a/src/api/include/migraphx/migraphx.h b/src/api/include/migraphx/migraphx.h index d1fb5ed316b..c4a992abcf5 100644 --- a/src/api/include/migraphx/migraphx.h +++ b/src/api/include/migraphx/migraphx.h @@ -659,6 +659,11 @@ MIGRAPHX_C_EXPORT migraphx_status migraphx_quantize_fp8(migraphx_program_t prog, migraphx_target_t target, migraphx_quantize_fp8_options_t options); +MIGRAPHX_C_EXPORT migraphx_status migraphx_get_onnx_operator_name_at_index(char** out, + size_t index); + +MIGRAPHX_C_EXPORT migraphx_status migraphx_get_onnx_operators_size(size_t* out); + MIGRAPHX_C_EXPORT migraphx_status migraphx_context_finish(const_migraphx_context_t context); MIGRAPHX_C_EXPORT migraphx_status migraphx_context_get_queue(void** out, diff --git a/src/api/include/migraphx/migraphx.hpp b/src/api/include/migraphx/migraphx.hpp index 4fc2b202a95..d059f7a95c8 100644 --- a/src/api/include/migraphx/migraphx.hpp +++ b/src/api/include/migraphx/migraphx.hpp @@ -1603,6 +1603,23 @@ quantize_fp8(const program& prog, const target& ptarget, const quantize_fp8_opti options.get_handle_ptr()); } +inline std::vector get_onnx_operators() +{ + size_t size = 0; + call(&migraphx_get_onnx_operators_size, &size); + std::vector result(size, ""); + + size_t index = 0; + for(auto& name : result) + { + char* name_op; + call(&migraphx_get_onnx_operator_name_at_index, &name_op, index); + name = name_op; + index++; + } + return result; +} + struct experimental_custom_op_base { experimental_custom_op_base() = default; diff --git a/src/api/migraphx.py b/src/api/migraphx.py index 13810b747d1..3c20e926268 100644 --- a/src/api/migraphx.py +++ b/src/api/migraphx.py @@ -510,6 +510,16 @@ def quantize_fp8_options(h): options='migraphx::quantize_fp8_options'), fname='migraphx::quantize_fp8_wrap') +api.add_function('migraphx_get_onnx_operator_name_at_index', + api.params(index='size_t'), + fname='migraphx::get_onnx_operator_name_at_index', + returns='char *') + +api.add_function('migraphx_get_onnx_operators_size', + fname='migraphx::get_onnx_operators_size', + returns='size_t') + + @auto_handle(ref=True) def context(h): diff --git a/src/include/migraphx/onnx.hpp b/src/include/migraphx/onnx.hpp index b9bf42a4f0e..a85fa131236 100644 --- a/src/include/migraphx/onnx.hpp +++ b/src/include/migraphx/onnx.hpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -76,7 +76,7 @@ MIGRAPHX_ONNX_EXPORT program parse_onnx_buffer(const void* data, std::size_t size, const onnx_options& options); -MIGRAPHX_ONNX_EXPORT std::vector get_onnx_operators(); +MIGRAPHX_ONNX_EXPORT const std::vector& get_onnx_operators(); } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx diff --git a/src/onnx/onnx.cpp b/src/onnx/onnx.cpp index efc1d0a7fa7..0517f38a997 100644 --- a/src/onnx/onnx.cpp +++ b/src/onnx/onnx.cpp @@ -109,7 +109,11 @@ program parse_onnx_buffer(const void* data, std::size_t size, const onnx_options return parse_onnx_from(options, data, size); } -std::vector get_onnx_operators() { return onnx::get_op_parsers(); } +const std::vector& get_onnx_operators() +{ + static std::vector result = onnx::get_op_parsers(); + return result; +} } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx diff --git a/src/py/migraphx_py.cpp b/src/py/migraphx_py.cpp index c3a9c41d5c2..e591474d499 100644 --- a/src/py/migraphx_py.cpp +++ b/src/py/migraphx_py.cpp @@ -621,6 +621,7 @@ MIGRAPHX_PYBIND11_MODULE(migraphx, m) py::arg("map_input_dims") = std::unordered_map>(), py::arg("output_names") = std::vector()); + m.def("get_onnx_operators", [] { return migraphx::get_onnx_operators(); }); m.def( "parse_onnx", [](const std::string& filename, diff --git a/test/api/CMakeLists.txt b/test/api/CMakeLists.txt index fdc9b93ba50..f4bd1d7140f 100644 --- a/test/api/CMakeLists.txt +++ b/test/api/CMakeLists.txt @@ -65,6 +65,7 @@ add_api_test(op test_op_construct.cpp ${TEST_ONNX_DIR}) add_c_api_test(c_op test_c_op_construct.c ${TEST_ONNX_DIR}) add_api_test(custom_op test_custom_op.cpp ${TEST_ONNX_DIR}) add_api_test(tf_parser test_tf_parser.cpp ${TEST_TF_DIR}) +add_api_test(onnx_op_list test_onnx_op_list.cpp ${TEST_ONNX_DIR}) # GPU-based tests if(MIGRAPHX_ENABLE_GPU) add_api_test(gpu test_gpu.cpp ${TEST_ONNX_DIR}) diff --git a/test/api/test_onnx_op_list.cpp b/test/api/test_onnx_op_list.cpp new file mode 100644 index 00000000000..9633fb6fca6 --- /dev/null +++ b/test/api/test_onnx_op_list.cpp @@ -0,0 +1,37 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015-2025 Advanced Micro Devices, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include +#include +#include "test.hpp" + +TEST_CASE(list_onnx_operators) +{ + auto list = migraphx::get_onnx_operators(); + for(const auto& name : list) + std::cout << name << std::endl; + + EXPECT(true); +} + +int main(int argc, const char* argv[]) { test::run(argc, argv); } diff --git a/tools/api/api.cpp b/tools/api/api.cpp index 07165236d28..0526175ddf3 100644 --- a/tools/api/api.cpp +++ b/tools/api/api.cpp @@ -289,6 +289,13 @@ static void quantize_fp8_wrap(program& prog, const target& t, quantize_fp8_optio migraphx::quantize_fp8(prog, t, options.calibration); } +static size_t get_onnx_operators_size() { return migraphx::get_onnx_operators().size(); } + +static char* get_onnx_operator_name_at_index(std::size_t index) +{ + return const_cast(get_onnx_operators().at(index).c_str()); // NOLINT +} + #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-nonliteral"