-
Notifications
You must be signed in to change notification settings - Fork 352
Pass pipeline logging instrumentation #4182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 10 commits
a46e663
c8d9ea6
88232a4
d1ee427
a5490db
5d8ca54
23b8d52
e2a8783
b2b4e4e
f495847
e096a63
e7fc24c
8c6b366
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # ============================================================================ # | ||
| # Copyright (c) 2026 NVIDIA Corporation & Affiliates. # | ||
| # All rights reserved. # | ||
| # # | ||
| # This source code and the accompanying materials are made available under # | ||
| # the terms of the Apache License 2.0 which accompanies this distribution. # | ||
| # ============================================================================ # | ||
|
|
||
| import json | ||
|
|
||
| import cudaq | ||
| import pytest | ||
| from cudaq import spin | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_target(): | ||
| cudaq.reset_target() | ||
| yield | ||
| cudaq.reset_target() | ||
|
|
||
|
|
||
| TARGET_CONFIGS = [ | ||
| # `run` is not currently supported on these backend targets. | ||
| pytest.param("infleqtion", {"emulate": True}, False, id="openqasm"), | ||
| pytest.param("quantinuum", {"emulate": True}, False, id="qir"), | ||
| pytest.param("remote-mqpu", {"auto_launch": "1"}, | ||
| True, | ||
| id="remote-simulator"), | ||
| # Keep one local/default case that also exercises `run`. | ||
| pytest.param("qpp-cpu", {}, True, id="default"), | ||
| ] | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def sample_kernel(): | ||
| qubit = cudaq.qubit() | ||
| h(qubit) | ||
| mz(qubit) | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def run_kernel() -> int: | ||
| qubit = cudaq.qubit() | ||
| h(qubit) | ||
| return mz(qubit) | ||
|
|
||
|
|
||
| @cudaq.kernel | ||
| def observe_kernel(): | ||
| qubit = cudaq.qubit() | ||
| h(qubit) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("target,target_kwargs,supports_run", TARGET_CONFIGS) | ||
| def test_pipeline_logging_decorator(tmp_path, monkeypatch, target, | ||
| target_kwargs, supports_run): | ||
| log_path = tmp_path / "pipeline.jsonl" | ||
|
||
| monkeypatch.setenv("CUDAQ_PIPELINE_LOG", str(log_path)) | ||
|
|
||
| if not cudaq.has_target(target): | ||
| pytest.skip(f"target '{target}' not available") | ||
| cudaq.set_target(target, **target_kwargs) | ||
|
|
||
| try: | ||
| cudaq.sample(sample_kernel, shots_count=1) | ||
| cudaq.observe(observe_kernel, spin.z(0)) | ||
| if supports_run: | ||
| cudaq.run(run_kernel, shots_count=1) | ||
| except RuntimeError as err: | ||
| raise | ||
|
|
||
| assert log_path.exists() | ||
|
|
||
| entries = [ | ||
| json.loads(line) | ||
| for line in log_path.read_text().splitlines() | ||
| if line.strip() | ||
| ] | ||
| assert any(entry.get("type") == "configured" for entry in entries) | ||
| assert any(entry.get("type") == "executed" for entry in entries) | ||
|
|
||
|
|
||
| def test_pipeline_logging_builder_default(tmp_path, monkeypatch): | ||
| log_path = tmp_path / "pipeline.jsonl" | ||
|
||
| monkeypatch.setenv("CUDAQ_PIPELINE_LOG", str(log_path)) | ||
|
|
||
| kernel = cudaq.make_kernel() | ||
| qubit = kernel.qalloc() | ||
| kernel.h(qubit) | ||
| kernel.mz(qubit) | ||
|
|
||
| cudaq.sample(kernel, shots_count=1) | ||
|
|
||
| assert log_path.exists() | ||
|
|
||
| entries = [ | ||
| json.loads(line) | ||
| for line in log_path.read_text().splitlines() | ||
| if line.strip() | ||
| ] | ||
| assert any(entry.get("type") == "configured" for entry in entries) | ||
| assert any(entry.get("type") == "executed" for entry in entries) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| #include "common/ArgumentConversion.h" | ||
| #include "common/Environment.h" | ||
| #include "common/JsonConvert.h" | ||
| #include "common/PassPipelineLogging.h" | ||
| #include "common/RemoteKernelExecutor.h" | ||
| #include "common/RestClient.h" | ||
| #include "common/RuntimeMLIR.h" | ||
|
|
@@ -205,6 +206,8 @@ class BaseRemoteRestRuntimeClient : public RemoteRuntimeClient { | |
| moduleOp.getContext()->disableMultithreading(); | ||
| pm.enableIRPrinting(); | ||
| } | ||
| cudaq_internal::maybeLogPassPipeline(pm, | ||
| name + ":" + passName + "-synth"); | ||
|
||
| if (failed(pm.run(moduleOp))) | ||
| throw std::runtime_error("Could not successfully apply " + passName + | ||
| " synth."); | ||
|
|
@@ -247,6 +250,7 @@ class BaseRemoteRestRuntimeClient : public RemoteRuntimeClient { | |
| tm.setEnabled(cudaq::isTimingTagEnabled(cudaq::TIMING_JIT_PASSES)); | ||
| auto timingScope = tm.getRootScope(); // starts the timer | ||
| pm.enableTiming(timingScope); // do this right before pm.run | ||
| cudaq_internal::maybeLogPassPipeline(pm, name + ":client"); | ||
|
||
| if (failed(pm.run(moduleOp))) | ||
| throw std::runtime_error( | ||
| "Remote rest platform: applying IR passes failed."); | ||
|
|
@@ -300,6 +304,7 @@ class BaseRemoteRestRuntimeClient : public RemoteRuntimeClient { | |
| mlir::PassManager pm(ctx); | ||
| // For now, the server side expects full-QIR. | ||
| opt::addAOTPipelineConvertToQIR(pm); | ||
| cudaq_internal::maybeLogPassPipeline(pm, name + ":aot-qir"); | ||
|
||
|
|
||
| if (failed(pm.run(moduleOp))) | ||
| throw std::runtime_error( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we calling
reset_target()before and after the test? Should it be just after the test?