Skip to content

Python API debug symbols#4803

Open
CharlieL7 wants to merge 125 commits intodevelopfrom
python_api_debug_symbols
Open

Python API debug symbols#4803
CharlieL7 wants to merge 125 commits intodevelopfrom
python_api_debug_symbols

Conversation

@CharlieL7
Copy link
Copy Markdown
Collaborator

@CharlieL7 CharlieL7 commented Apr 20, 2026

Motivation

  • Add debug symbols functionality to python API

Technical Details

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

CharlieL7 and others added 30 commits January 13, 2026 16:41
Traverse over ins with empty debug symbols also just in case.
Probably will not occur however
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@CharlieL7 CharlieL7 self-assigned this Apr 20, 2026
Copilot AI review requested due to automatic review settings April 20, 2026 18:56
@CharlieL7 CharlieL7 requested review from a team and causten as code owners April 20, 2026 18:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends MIGraphX’s Python bindings to expose instruction/module debug symbols and adds a Netron export path that emits an ONNX ModelProto binary (including debug symbols when present), updating the driver/docs/tests accordingly.

Changes:

  • Added Python API bindings for instruction_ref.get_debug_symbols() and module add/remove/has_debug_symbols.
  • Reworked Netron export from JSON to ONNX protobuf binary via write_netron_output(...), and added program APIs to write it to a file.
  • Added C++/C/Python tests covering Netron output and debug symbols.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/py/migraphx_py.cpp Exposes debug symbols + program.write_netron_output() in pybind11 module.
src/onnx/netron_output.cpp Implements ONNX ModelProto Netron export and includes debug symbols as node attributes.
src/include/migraphx/netron_output.hpp Updates public Netron export API to a stream-based writer.
src/api/api.cpp / tools/api/api.cpp Adds C API plumbing to write Netron output to a file.
src/api/include/migraphx/migraphx.h / migraphx.hpp Adds migraphx_program_write_netron_output and C++ wrapper method.
src/driver/main.cpp + docs (docs/*) Switches --netron output to protobuf binary and updates help/docs text.
test/* Adds/updates C++ and Python tests for Netron export and debug symbols.
src/program.cpp / src/include/migraphx/program.hpp Exposes program file version via getter and centralizes constant.
src/CMakeLists.txt / codecov.yml Removes old src/netron_output.cpp from build/coverage ignore.
CHANGELOG.md Adds release note about Netron export and debug symbols.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

{
onnx::ModelProto model;
auto prog_value = prog.to_value();
model.set_ir_version(prog.get_program_file_version());
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

model.set_ir_version(prog.get_program_file_version()) uses MIGraphX's MXR serialization version as the ONNX IR version. ONNX consumers expect an ONNX IR version (e.g., onnx::IR_VERSION), and using an unrelated value can make the exported model invalid or rejected. Set ir_version to the ONNX constant and consider also populating opset_import to improve compatibility with ONNX tooling/Netron.

Suggested change
model.set_ir_version(prog.get_program_file_version());
model.set_ir_version(onnx::IR_VERSION);

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +35
#include <ostream>
#include <migraphx/config.hpp>
#include <migraphx/program.hpp>
#include <migraphx/onnx/export.h>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

MIGRAPHX_EXPORT std::string make_netron_output(const program& prog);
MIGRAPHX_ONNX_EXPORT void write_netron_output(const program& prog, std::ostream& os);
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This header replaces the existing make_netron_output(const program&) -> std::string API with write_netron_output(program, ostream), which is a breaking change for C++ consumers. Consider keeping make_netron_output as a compatibility wrapper (e.g., implemented via std::ostringstream + write_netron_output) and/or deprecating it instead of removing it outright.

Copilot uses AI. Check for mistakes.
Comment thread CHANGELOG.md
* Updated `argmin` and `argmax` ops to be implemented as reduction ops, so they now have JIT support and can fuse (#4620).
* Replaced usages of `std::cout` and `std::cerr` with the logger (#4732)
* Converted RNN variable sequence length operations (`rnn_var_sl_shift_sequence`, `rnn_var_sl_shift_output`, `rnn_var_sl_last_output`) from device implementation to JIT compilation (#4755).
* Updated netron output to create an ONNX-like protobuff. Now also includes debug symbols if enabled. (#4701)
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog entry says "protobuff"; the correct term is "protobuf".

Suggested change
* Updated netron output to create an ONNX-like protobuff. Now also includes debug symbols if enabled. (#4701)
* Updated netron output to create an ONNX-like protobuf. Now also includes debug symbols if enabled. (#4701)

Copilot uses AI. Check for mistakes.
Comment thread CHANGELOG.md
Comment thread src/api/api.cpp
Comment on lines +342 to +348
static void write_netron_output_file(const program& p, const char* filename)
{
std::ofstream os(filename, std::ios::binary);
if(not os.is_open())
MIGRAPHX_THROW(migraphx_status_bad_param,
"Failed to open file for writing: " + std::string(filename));
write_netron_output(p, os);
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write_netron_output_file constructs std::ofstream and later builds std::string(filename) without validating filename. If the C API is called with a null pointer, this is undefined behavior and can crash before returning a status. Add an explicit filename == nullptr (and ideally empty-string) check and throw migraphx_status_bad_param before using it.

Copilot uses AI. Check for mistakes.
Comment thread tools/api/api.cpp
Comment on lines +342 to +348
static void write_netron_output_file(const program& p, const char* filename)
{
std::ofstream os(filename, std::ios::binary);
if(not os.is_open())
MIGRAPHX_THROW(migraphx_status_bad_param,
"Failed to open file for writing: " + std::string(filename));
write_netron_output(p, os);
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template version has the same issue as the generated C API: write_netron_output_file uses filename without checking for null, and std::string(filename) will crash on nullptr. Add a filename == nullptr (and optionally empty-string) check before opening/formatting the filename.

Copilot uses AI. Check for mistakes.
Comment thread src/py/migraphx_py.cpp
[](migraphx::module& mm, migraphx::instruction_ref ins) {
mm.remove_debug_symbols(ins);
},
py::arg("ins"))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only get_debug_symbols should be exposed to the API.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a user wants to add a debug symbol to an instruction when manually adding instructions?

Copy link
Copy Markdown
Collaborator

@pfultz2 pfultz2 Apr 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a debug_symbols parameter to the add_instruction, insert_instruction, add_macro, insert_macro, etc methods. So they can write m.add_instruction(op("add"), [x, y], debug_symbols=['addOp']).

This will make it consistent and can handle the macros, which is a little more complicated.

Hold off on the C++ API as we need a better design to make it more extensible, and it doesnt even support macros right now.


void print() const { call(&migraphx_program_print, this->get_handle_ptr()); }

void write_netron_output(const char* filename) const
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we adding this to the C++ API?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from #4701. There wasn't a way to write out as ONNX-like protobuff from the C++ API before, only the driver.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is #4701 in this PR? The python API debug symbols are not related to this.

Comment thread src/include/migraphx/program.hpp
case shape::fp8e5m2_type: return onnx::TensorProto::FLOAT8E5M2;
case shape::fp8e5m2fnuz_type: return onnx::TensorProto::FLOAT8E5M2FNUZ;
case shape::tuple_type: return onnx::TensorProto::UNDEFINED;
case shape::fp4x2_type: return onnx::TensorProto::UINT4;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be onnx::TensorProto::FLOAT4E2M1 or are we limited by ONNX opset version? Will using UINT4 result in display issues for fp4 types?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is also from #4701. The update should be doable. I made the netron output before ONNX had the type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants