Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -537,18 +537,32 @@ work. There are two such mechanisms.

**Subgraphs** are the supported, modern split. The compiler partitions
the model into independent compute graphs early, each in its own
folder under `vaiml_folder/<model>/vaiml_par_<N>/`. The debugger debugs
one subgraph at a time. If the model has more than one subgraph the
user must name the one to debug in `vitisai_config.json` under
`passes[].vaiml_config.include_subgraphs`.
folder under `vaiml_folder/<model>/vaiml_par_<N>/`. On XRT, with more
than one NPU partition and no `include_subgraphs`, the debugger reads
the halt plugin's performance counters before releasing the cores.
Counter 0 is the FNV-1a hash of the halted partition's kernel name.
Counter 1 is the halt count: 0 when the plugin halts only the first
run of the context (`halt_on_every_run` off), otherwise 1 for the first
halt and one higher for each later halt. A count of 1 or more is
followed until the hardware context goes away, and a partition may
run again later in the sequence. A count of 0, or a subgraph named in
`vitisai_config.json` under
`passes[].vaiml_config.include_subgraphs`, keeps the manual advance
described below. `--dump_layers` writes every partition; it does not
read the counters. `-s` and `--dump-aie-status` use one subgraph's
metadata when there is only one, and otherwise talk to the hardware
directly with the default overlay (or `-o`).

**Failsafe partitions (FSP)** are an older mechanism, deprecated but
still supported. They split a single subgraph into multiple
program-memory partitions for very large models. Each FSP has its own
work directory and `buffer_info.json`. Their execution order comes from
`partition-info.json`. The debugger runs `debug()` once per FSP,
prompting the user (or coordinating with FlexML) to advance between
partitions.
`partition-info.json`. On the manual path the debugger runs `debug()`
once per FSP. Between parts it arms a breakpoint on the lock-acquire
instruction. With `-l3` or `--automated_debug` it then closes the debug
server so FlexML can load the next part. Otherwise it asks the user to
press Enter in the VAIML process, wait for `waiting for user input`,
and press Enter in the debugger.

```
vaiml_folder/<model>/vaiml_par_0/
Expand Down
152 changes: 139 additions & 13 deletions src/mldebug/batch_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

from mldebug.utils import LOGGER, cleanup_and_exit, timeit, wait_until

# How long to wait for the driver to debug-halt every core at the next partition.
NEXT_PARTITION_HALT_TIMEOUT_S = 300


class BatchRunner:
"""
Expand Down Expand Up @@ -62,6 +65,11 @@ def common_init(self):
single-stamp collapse is handled up front by the Overlay.
"""
for sid in self.design_info.overlay.get_stampids():
# A previous partition's breakpoints survive into this session: the
# cores are halted, not reset, between partitions. Those PCs mean
# something else in this partition's program.
self.aie_utls[sid].clear_pc_breakpoint(0)
self.aie_utls[sid].clear_pc_breakpoint(1)
self.impls[sid].enable_pc_halt()
if self.args.run_flags.skip_iter:
self.aie_utls[sid].init_skip_iterations()
Expand Down Expand Up @@ -496,29 +504,147 @@ def execute_and_dump(self):
if layer.runs_replica(sid):
self.state.pm_reload[sid] = self.check_pm_reload(sid)

for sid in overlay.get_stampids():
self.aie_utls[sid].initialize_stamp()
# skip_iter2's last layer already cleared the PC breakpoints and continued
# once. initialize_stamp writes DEBUG_CONTROL0 again, and that second
# release lets cores run past the next partition's halt. The legacy FSP
# flow arms its own breakpoint afterwards, so it always releases here.
more = self.args.vaiml_folder_path and not getattr(self.args, "last_fsp", True)
legacy = getattr(self.args, "legacy_fsp", False)
if not (more and not legacy and self.args.run_flags.skip_iter2):
for sid in overlay.get_stampids():
self.aie_utls[sid].initialize_stamp()

LOGGER.log("\nFinished Execution")
self._handle_fsp()
self._write_run_summary("SUCCESS")

def _handle_fsp(self):
"""Handle end-of-run logic for VAIML Failsafe Partition mode."""
is_fsp = self.args.vaiml_folder_path and not self.args.last_fsp
def _partition_label(self):
"""Name of the partition that just ran, as the driver prints it."""
return (
getattr(self.args, "partition_name", None)
or getattr(self.args, "subgraph_name", None)
or "unknown"
)

def _ref_core_pc(self):
"""PC of the reference core, the first core tile of stamp 0."""
if not self.aie_utls:
return ""
utl = self.aie_utls[0]
c, r = utl.pm_tracker_tile
pc = utl.read_core_pc_tile(c, r)
return f", PC: 0x{pc:x}"

def _cores_all_halted(self):
"""
True when every core in every stamp is debug-halted.

None means the register read failed. Between partitions that is the
hardware context going away after this partition has already finished.
"""
try:
return all(impl.poll_core_status() for impl in self.impls)
except Exception as err: # pylint: disable=broad-except
self._context_gone_error = err
return None

if is_fsp:
for utl in self.aie_utls:
utl.set_fsp_breakpoint()
def _session_done_context_gone(self):
"""
End the session successfully.

This partition's dumps are already written. The context disappeared
while waiting for the next one, which is how a later subgraph on its
own context looks from here.
"""
err = self._context_gone_error
name = self._partition_label()
LOGGER.log(
f"[INFO] Partition {name} finished. The hardware context is gone, "
f"so this session is complete. ({type(err).__name__}: {err})"
)
self._write_run_summary("SUCCESS")
cleanup_and_exit(self.args, 0)

def wait_for_next_partition(self):
"""
Wait until every core debug-halts at the next partition.

The halt can land before the first poll, so an already-halted sample counts.
The L3 server is closed first so FlexML can load the next partition.
A failed read ends the session successfully: this partition is done and
the context is no longer here to halt.
"""
# Batch mode also closes in _handle_fsp. Closing again is a no-op, and
# interactive continue reaches this wait without that call.
if self.dumper.debug_server:
self.dumper.debug_server.close()
elif is_fsp:
input(
"First, please press Enter ONCE in the VAIML process "
"to load the next Failsafe Partition and wait for "
"`waiting for user input`. Then press Enter here."
if self.args.backend == "test":
return
name = self._partition_label()
LOGGER.log(
f"Partition {name} finished. "
"Waiting for all cores to debug-halt at the next partition."
)
halted = self._cores_all_halted()
if halted is None:
self._session_done_context_gone()
return
if halted:
LOGGER.log(
f"All cores debug-halted after partition {name}{self._ref_core_pc()}."
)
return
deadline = time.monotonic() + NEXT_PARTITION_HALT_TIMEOUT_S
while True:
halted = self._cores_all_halted()
if halted is None:
self._session_done_context_gone()
return
if halted:
LOGGER.log(
f"All cores debug-halted after partition {name}{self._ref_core_pc()}."
)
return
if time.monotonic() >= deadline:
minutes = NEXT_PARTITION_HALT_TIMEOUT_S // 60
LOGGER.log(
f"[ERROR] Timed out after {minutes} minutes waiting for all cores "
f"to reach debug halt after partition {name}."
)
# Dumps for this partition are already on disk. The context is still
# readable, so the next halt failing is a real error. Record it, or
# automated_debug keeps the previous partition's SUCCESS.
self._write_run_summary("FAIL")
cleanup_and_exit(self.args, 1)
time.sleep(0.1)

def _handle_fsp(self):
"""
Close the L3 server, then wait for the next NPU partition's debug halt.

The server is closed first so the runtime can load that partition.
A pinned subgraph (include_subgraphs) keeps the legacy flow instead: arm a
lock-acquire breakpoint and, without an L3 server, ask the user to load
the next partition.
"""
more = self.args.vaiml_folder_path and not getattr(self.args, "last_fsp", True)
if getattr(self.args, "legacy_fsp", False):
if more:
for utl in self.aie_utls:
utl.set_fsp_breakpoint()
if self.dumper.debug_server:
self.dumper.debug_server.close()
elif more:
input(
"First, please press Enter ONCE in the VAIML process "
"to load the next Failsafe Partition and wait for "
"`waiting for user input`. Then press Enter here."
)
return
if self.dumper.debug_server:
self.dumper.debug_server.close()
if more:
self.wait_for_next_partition()

def _write_run_summary(self, status):
"""
Expand Down
12 changes: 11 additions & 1 deletion src/mldebug/client_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,22 @@ def print_kernel_functions(self, layer):
def dump_layers(self, filename=None):
"""
Print a text report of every debuggable layer, or write it to filename.

Later partitions append to an existing file so one path collects them all.
"""
report = format_layer_report(self.design_info)
name = getattr(self.args, "partition_name", None)
if name:
report = f"{name}\n{report}"
if not filename:
print(report)
return
with open(filename, "w", encoding="utf-8") as fd:
# The first partition of this process truncates. Later ones append.
append = getattr(self.args, "layers_report_started", False)
self.args.layers_report_started = True
with open(filename, "a" if append else "w", encoding="utf-8") as fd:
if append:
fd.write("\n")
fd.write(report + "\n")
print(f"[INFO] Layer report written to {filename}")

Expand Down
Loading
Loading