Skip to content

[AIMIGRAPHX-911] Add callback function to eval()#4780

Open
eddieliao wants to merge 15 commits intodevelopfrom
eval_callback
Open

[AIMIGRAPHX-911] Add callback function to eval()#4780
eddieliao wants to merge 15 commits intodevelopfrom
eval_callback

Conversation

@eddieliao
Copy link
Copy Markdown
Contributor

@eddieliao eddieliao commented Apr 13, 2026

Motivation

Allow users to inspect data from each instruction.

Technical Details

Adds a callback function to eval() that can be used to get data from specific instructions in the compiled graph. See examples/migraphx/cpp_trace_callback for an implementation of a basic callback function.

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.

@eddieliao eddieliao self-assigned this Apr 13, 2026
@eddieliao eddieliao added enhancement New feature or request Changelog: Added New functionality. labels Apr 13, 2026
@eddieliao eddieliao requested a review from Copilot April 13, 2026 16:41
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

Adds a per-instruction evaluation callback to MIGraphX program execution so callers can inspect operator outputs as the graph runs.

Changes:

  • Introduces migraphx::eval_callback and a new program::eval(..., eval_callback, ...) overload to invoke a callback during evaluation.
  • Exposes the feature through the C API / C++ API wrapper (migraphx_program_run_callback, program::eval(pparams, F callback)).
  • Adds unit tests plus a C++ example (examples/migraphx/cpp_eval_callback) demonstrating filtering options.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/program.cpp Implements program::eval overload and wires callback invocation + host-copy behavior into the eval path.
src/include/migraphx/program.hpp Declares new eval overload taking eval_callback.
src/include/migraphx/eval_callback.hpp Adds the new callback type with name/instruction filtering helpers.
src/api/api.cpp Adds C API entrypoint migraphx_program_run_callback and bridges to eval_callback.
src/api/include/migraphx/migraphx.h Declares the C API callback typedef + function.
src/api/include/migraphx/migraphx.hpp Adds a C++ convenience wrapper overload to register a per-instruction callback.
test/eval_test.cpp Adds tests validating callback enablement, firing, and filtering behavior.
examples/migraphx/cpp_eval_callback/eval_callback_example.cpp Provides a runnable example demonstrating callback usage + filtering.
examples/migraphx/cpp_eval_callback/README.md Documents building/running the example and filter modes.
examples/migraphx/cpp_eval_callback/CMakeLists.txt Build configuration for the new example.

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

Comment thread examples/migraphx/cpp_eval_callback/eval_callback_example.cpp Outdated
Comment thread src/include/migraphx/program.hpp Outdated
Comment thread src/include/migraphx/program.hpp Outdated
Comment thread src/include/migraphx/eval_callback.hpp Outdated
Comment thread src/api/include/migraphx/migraphx.h Outdated
void* data);

MIGRAPHX_C_EXPORT migraphx_status
migraphx_program_run_callback(migraphx_arguments_t* out,
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.

This should be called migraphx_program_run_trace to make it clear its enabling the trace.

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.

Or maybe we should not expose another overload and expose the execution_environment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've left it exposed through the API for now since I believe it's cleaner than just giving users the execution_environment; I've modified the example as well to show usage of this in trace_callback_example.cpp.

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.

It just starts create more complicated combinations that user might want to do. What if they want to pass a stream and do trace callback? I think for these more advance cases it makes sense to have the user do it through an execution_environment which we they can use the full options for execution.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 13, 2026

Codecov Report

❌ Patch coverage is 20.31250% with 51 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/program.cpp 27.27% 32 Missing ⚠️
src/api/api.cpp 5.00% 19 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4780      +/-   ##
===========================================
+ Coverage    92.29%   92.45%   +0.16%     
===========================================
  Files          580      584       +4     
  Lines        28688    29598     +910     
===========================================
+ Hits         26475    27362     +887     
- Misses        2213     2236      +23     
Files with missing lines Coverage Δ
src/api/include/migraphx/migraphx.hpp 98.79% <ø> (ø)
src/include/migraphx/execution_environment.hpp 100.00% <ø> (ø)
src/api/api.cpp 70.47% <5.00%> (-1.74%) ⬇️
src/program.cpp 72.63% <27.27%> (+0.76%) ⬆️

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

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


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

Comment thread examples/migraphx/cpp_trace_callback/README.md
Comment thread src/api/include/migraphx/migraphx.h Outdated
Comment thread src/include/migraphx/execution_environment.hpp Outdated
Comment thread src/api/api.cpp Outdated
Comment thread src/api/include/migraphx/migraphx.hpp
Comment thread examples/migraphx/cpp_trace_callback/trace_callback_example.cpp Outdated
Comment thread src/api/api.cpp Outdated
Comment thread src/program.cpp
{
ret = generic_eval(*this, contexts, params, [&](auto&&, auto f) { return f(); });
auto trace_level = value_of(MIGRAPHX_TRACE_EVAL{});
if(trace_level > 0)
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.

So I dont think we need two paths for the tracing. Instead when MIGRAPHX_TRACE_EVAL is set, then we set the trace callback and run the same trace code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Are you suggesting that MIGRAPHX_TRACE_EVAL should overwrite any callback function the user passes in with the current trace callback? Or do we want to support multiple callbacks at once (i.e. both trace and user callback at the same time).

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.

Just overwrite for now.

Comment thread src/include/migraphx/execution_environment.hpp Outdated
Comment thread src/api/api.cpp Outdated
@eddieliao eddieliao marked this pull request as ready for review April 16, 2026 19:03
@eddieliao eddieliao requested review from a team and causten as code owners April 16, 2026 19:03
@eddieliao eddieliao requested a review from shivadbhavsar April 16, 2026 19:03
Comment thread CHANGELOG.md Outdated
Co-authored-by: anisha-amd <anisha.sankar@amd.com>
@eddieliao eddieliao requested a review from pfultz2 April 17, 2026 16:33
@eddieliao eddieliao changed the title Add callback function to eval() [AIMIGRAPHX-911] Add callback function to eval() Apr 21, 2026
@eddieliao eddieliao requested a review from ahsan-ca April 21, 2026 20:18
Copy link
Copy Markdown
Contributor

@shivadbhavsar shivadbhavsar left a comment

Choose a reason for hiding this comment

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

LGTM. You can unify the trace_eval here if that's decided, or the unification can done after in a separate PR too. Re-request if you decide to do it in this pr

Copy link
Copy Markdown
Collaborator

@pfultz2 pfultz2 left a comment

Choose a reason for hiding this comment

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

Tests needed to be added for the API calls.

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

Labels

Changelog: Added New functionality. enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants