hexagon: n_hvx and n_threads cleanup (#31857)

* hex-nhvx: apply nhvx updates to the latest master

* hex-nhvx: set N_HVX and N_THREADS based on the arch to avoid overallocation

* hex-build: add support for enabling HTP_DEBUG via build.py

* hex-trace: move tracing state/helpers into new htp-trace.h header

* hex-dcache: cleanup dcache #defines

* hex-hnvx: minimize the diff by reflowing the macros

* hex-ci: fix python type/linter warnings

* hex-nhvx: consitent logging
This commit is contained in:
Max Krasnyansky authored and GitHub committed 2026-11-07 15:03:31 -08:00
1 parent a2654ec431
commit e9a2a44fe9
87 files changed
+2555 -2297

No files matched your search

+27 -2
View File
@@ -106,6 +106,7 @@ def main():
parser.add_argument("--no-docker", action="store_true", help="Build natively on the host instead of in a docker container")
parser.add_argument("--preset", help="Override the CMake preset to use")
parser.add_argument("--debug", action="store_true", help="Build in debug mode (uses -debug presets instead of -release)")
parser.add_argument("--hex-debug", "--hexagon-debug", action="store_true", help="Enable Hexagon HTP debug output (sets GGML_HEXAGON_HTP_DEBUG=ON)")
parser.add_argument("--hex-only", "--hexagon-only", action="store_true", help="Only build and push ggml-hexagon and libggml-htp libraries")
parser.add_argument("--hex-arch", choices=["v73", "v75", "v79", "v81", "v85", "73", "75", "79", "81", "85"], help="Specific Hexagon DSP architecture to build with --hex-only (default: all)")
@@ -197,13 +198,15 @@ def main():
else:
hex_targets = ["ggml-hexagon", "htp-v73", "htp-v75", "htp-v79", "htp-v81", "htp-v85"]
hex_debug_arg = f"-DGGML_HEXAGON_HTP_DEBUG={'ON' if args.hex_debug else 'OFF'}"
if args.no_docker:
# Native/local host build
logger.info("Running native/local CMake build...")
install_prefix = os.path.join(repo_root, install_dir, "llama.cpp")
# Configure
configure_cmd = ["cmake", f"--preset={preset}", "-B", build_dir]
configure_cmd = ["cmake", f"--preset={preset}", hex_debug_arg, "-B", build_dir]
logger.info(f"+ {' '.join(configure_cmd)}")
res = subprocess.run(configure_cmd, cwd=repo_root)
if res.returncode != 0:
@@ -238,7 +241,7 @@ def main():
target_args = f"--target {' '.join(hex_targets)} " if args.hex_only else ""
build_sh_cmd = (
f"cmake --preset {preset} -B /workspace/{build_dir} && "
f"cmake --preset {preset} {hex_debug_arg} -B /workspace/{build_dir} && "
f"cmake --build /workspace/{build_dir} {target_args}-j {jobs} && "
f"cmake --install /workspace/{build_dir} --prefix {install_prefix_container}"
)
@@ -261,6 +264,17 @@ def main():
logger.error("Docker-based build failed.")
sys.exit(res.returncode)
install_lib_dir = os.path.join(repo_root, install_dir, "llama.cpp", "lib")
if os.path.isdir(install_lib_dir):
if args.hex_debug:
farf_src = os.path.join(repo_root, "scripts", "snapdragon", "adb", "llama-cli.farf")
farf_apps = ["llama-server", "llama-cli", "llama-completion", "test-backend-ops", "test-llama-archs"]
for app in farf_apps:
shutil.copyfile(farf_src, os.path.join(install_lib_dir, f"{app}.farf"))
else:
for f in glob.glob(os.path.join(install_lib_dir, "*.farf")):
os.remove(f)
logger.info("\nBuild and installation completed successfully!")
# Push/deploy if requested
@@ -286,6 +300,9 @@ def main():
else:
hex_libs += glob.glob(os.path.join(lib_dir, "libggml-htp*"))
if args.hex_debug:
hex_libs += glob.glob(os.path.join(lib_dir, "*.farf"))
if not hex_libs:
logger.error(f"Error: no hexagon libraries found in {lib_dir} to push.")
sys.exit(1)
@@ -307,6 +324,10 @@ def main():
if res.returncode != 0:
logger.error("ADB mkdir failed.")
sys.exit(res.returncode)
if not args.hex_debug:
rm_farf_cmd = adb_cmd + ["shell", f"rm -f {shlex.quote(target_lib_dir)}/*.farf"]
logger.info(f"+ {' '.join(rm_farf_cmd)}")
subprocess.run(rm_farf_cmd)
for lib in hex_libs:
push_cmd = adb_cmd + ["push", lib, f"{target_lib_dir}/"]
logger.info(f"+ {' '.join(push_cmd)}")
@@ -383,6 +404,10 @@ def main():
if args.hex_only:
logger.info(f"\nDeploying Hexagon libraries to Linux device {ssh_host} via SCP...")
target_lib_dir = f"{target_dir}/lib"
if not args.hex_debug:
rm_farf_cmd = ["ssh", ssh_host, f"rm -f {target_lib_dir}/*.farf"]
logger.info(f"+ {' '.join(rm_farf_cmd)}")
subprocess.run(rm_farf_cmd)
deploy_cmd = ["scp"] + hex_libs + [f"{ssh_host}:{target_lib_dir}/"]
logger.info(f"+ {' '.join(deploy_cmd)}")
res = subprocess.run(deploy_cmd)
+241 -6
View File
@@ -6,6 +6,7 @@ Usage:
align-macros.py [paths...] # Check and report misaligned macros
align-macros.py --diff [paths...] # Show unified diff of fixes
align-macros.py --fix [paths...] # Fix misaligned macros in-place
align-macros.py --fix --reflow false ... # Disable line reflowing in mostly-aligned macros
align-macros.py --fix --mode majority ... # Align to the dominant column
align-macros.py --fix --pad 2 ... # Align to (max_content_len + pad)
@@ -127,9 +128,227 @@ def is_macro_aligned(macro: MacroDef) -> bool:
return len(set(bs_cols)) == 1 and not has_trailing_ws
def str_to_bool(val: str) -> bool:
if isinstance(val, bool):
return val
v = val.strip().lower()
if v in ("true", "1", "yes", "y", "on"):
return True
if v in ("false", "0", "no", "n", "off"):
return False
raise argparse.ArgumentTypeError(f"Boolean value expected, got '{val}'")
def find_paren_comma_groups(s: str) -> List[Tuple[int, int, List[int]]]:
"""Find parenthesized groups (open_idx, close_idx, comma_indices) ordered from outermost to innermost."""
stack: List[Tuple[int, List[int]]] = []
groups: List[Tuple[int, int, List[int]]] = []
in_str: Optional[str] = None
escape = False
for idx, ch in enumerate(s):
if escape:
escape = False
continue
if ch == "\\":
escape = True
continue
if in_str is not None:
if ch == in_str:
in_str = None
continue
if ch in ('"', "'"):
in_str = ch
continue
if ch == "(":
stack.append((idx, []))
elif ch == ")" and stack:
open_idx, commas = stack.pop()
if commas:
groups.append((open_idx, idx, commas))
elif ch == "," and stack:
stack[-1][1].append(idx)
groups.sort(key=lambda g: g[0])
return groups
def find_top_level_op_splits(s: str) -> Tuple[Optional[int], List[int]]:
"""Find split positions after binary operators (+, -, &&, ||) at paren depth 0."""
depth = 0
in_str: Optional[str] = None
escape = False
assign_idx: Optional[int] = None
splits: List[int] = []
n = len(s)
i = 0
while i < n:
ch = s[i]
if escape:
escape = False
i += 1
continue
if ch == "\\":
escape = True
i += 1
continue
if in_str is not None:
if ch == in_str:
in_str = None
i += 1
continue
if ch in ('"', "'"):
in_str = ch
i += 1
continue
if ch in "([{":
depth += 1
elif ch in ")]}":
depth = max(0, depth - 1)
elif depth == 0:
if s.startswith("&&", i) or s.startswith("||", i):
splits.append(i + 2)
i += 2
continue
if ch == "=" and (i == 0 or s[i - 1] not in "=!<>+-*/%&|^") and (i + 1 >= n or s[i + 1] != "="):
if assign_idx is None:
assign_idx = i
elif ch in "+-" and i > 0 and s[i - 1] == " " and i + 1 < n and s[i + 1] == " ":
splits.append(i + 1)
i += 1
return assign_idx, splits
def reflow_c_line(content: str, max_len: int) -> Optional[List[str]]:
"""Try to cleanly reflow a C statement/signature line so each piece has len <= max_len."""
if len(content) <= max_len:
return [content]
stripped = content.lstrip()
if not stripped or stripped.startswith("#") or "//" in content or "/*" in content:
return None
base_indent = content[:len(content) - len(stripped)]
# 1. Try splitting comma-separated arguments/parameters inside (...)
for open_idx, _, commas in find_paren_comma_groups(content):
pts = [0] + [c + 1 for c in commas] + [len(content)]
segs: List[str] = []
for idx in range(len(pts) - 1):
seg = content[pts[idx]:pts[idx + 1]].rstrip()
if idx > 0:
seg = seg.lstrip()
segs.append(seg)
for cont_indent in [" " * (open_idx + 1), base_indent + " ", base_indent + " "]:
lines = [segs[0]]
for seg in segs[1:]:
cand = f"{lines[-1]} {seg}"
if len(cand) <= max_len:
lines[-1] = cand
else:
lines.append(f"{cont_indent}{seg}")
if len(lines) > 1 and all(len(line) <= max_len for line in lines):
return lines
# 2. Try splitting after top-level binary operators (+, -, &&, ||) or assignment (=)
assign_idx, op_splits = find_top_level_op_splits(content)
if not op_splits and assign_idx is not None:
op_splits = [assign_idx + 1]
if op_splits:
pts = [0] + op_splits + [len(content)]
segs = []
for idx in range(len(pts) - 1):
seg = content[pts[idx]:pts[idx + 1]].rstrip()
if idx > 0:
seg = seg.lstrip()
segs.append(seg)
indents = []
if assign_idx is not None:
rhs_start = assign_idx + 1
while rhs_start < len(content) and content[rhs_start] == " ":
rhs_start += 1
indents.append(" " * rhs_start)
indents.extend([base_indent + " ", base_indent + " "])
for cont_indent in indents:
lines = [segs[0]]
for seg in segs[1:]:
cand = f"{lines[-1]} {seg}"
if len(cand) <= max_len:
lines[-1] = cand
else:
lines.append(f"{cont_indent}{seg}")
if len(lines) > 1 and all(len(line) <= max_len for line in lines):
return lines
return None
def reflow_macro_if_needed(
macro: MacroDef,
pad: int,
dom_ratio_threshold: float = 0.80,
fit_ratio_threshold: float = 0.90,
) -> Tuple[MacroDef, Optional[int]]:
"""
If a macro is already mostly aligned at a dominant column (>= dom_ratio_threshold)
and >= fit_ratio_threshold of continuation lines fit within that column, reflow any
exceeding lines so the macro can keep its dominant column instead of shifting all lines.
"""
cont_lines = [ml for ml in macro.lines if ml.bs_col is not None]
if not cont_lines:
return macro, None
counts = Counter(ml.bs_col for ml in cont_lines if ml.bs_col is not None)
dom_col, dom_count = sorted(counts.items(), key=lambda x: (-x[1], -x[0]))[0]
max_allowed_len = dom_col - pad
if max_allowed_len <= 0:
return macro, None
dom_ratio = dom_count / len(cont_lines)
fit_count = sum(1 for ml in cont_lines if len(ml.content) <= max_allowed_len)
fit_ratio = fit_count / len(cont_lines)
effective_dom_threshold = 0.70 if fit_count == len(cont_lines) else dom_ratio_threshold
if dom_ratio < effective_dom_threshold or fit_ratio < fit_ratio_threshold:
return macro, None
new_macro_lines: List[MacroLine] = []
for ml in macro.lines:
if ml.bs_col is None or len(ml.content) <= max_allowed_len:
new_macro_lines.append(ml)
else:
reflowed = reflow_c_line(ml.content, max_allowed_len)
if not reflowed:
return macro, None
nl = "\r\n" if ml.raw.endswith("\r\n") else "\n"
for piece in reflowed:
new_macro_lines.append(MacroLine(
line_num=ml.line_num,
raw=f"{piece} \\{nl}",
content=piece,
bs_col=dom_col,
trailing_ws=False,
))
return MacroDef(
name=macro.name,
filepath=macro.filepath,
start_line=macro.start_line,
end_line=macro.end_line,
lines=new_macro_lines,
), dom_col
def compute_target_column(macro: MacroDef, mode: str, pad: int, target_col: Optional[int]) -> int:
"""Determine the column where backslashes should be aligned."""
max_content_len = max(len(ml.content) for ml in macro.lines)
cont_lines = [ml for ml in macro.lines if ml.bs_col is not None]
lines_for_len = cont_lines if cont_lines else macro.lines
max_content_len = max(len(ml.content) for ml in lines_for_len)
min_needed = max_content_len + pad
if target_col is not None:
@@ -181,6 +400,7 @@ def process_file(filepath: str, args: argparse.Namespace) -> Tuple[int, int, Opt
misaligned_count = 0
modified = False
new_file_lines = list(file_lines)
line_offset = 0
for macro in macros:
aligned = is_macro_aligned(macro)
@@ -188,8 +408,9 @@ def process_file(filepath: str, args: argparse.Namespace) -> Tuple[int, int, Opt
if not aligned:
misaligned_count += 1
bs_cols = [ml.bs_col for ml in macro.lines if ml.bs_col is not None]
max_content = max(len(ml.content) for ml in macro.lines)
cont_lines = [ml for ml in macro.lines if ml.bs_col is not None]
bs_cols = [ml.bs_col for ml in cont_lines if ml.bs_col is not None]
max_content = max(len(ml.content) for ml in (cont_lines or macro.lines))
col_counts = Counter(bs_cols)
if not args.quiet:
@@ -200,16 +421,28 @@ def process_file(filepath: str, args: argparse.Namespace) -> Tuple[int, int, Opt
if trailing_ws_lines:
logger.warning(f" Warning: Trailing whitespace after backslash on line(s): {trailing_ws_lines}")
target_col = compute_target_column(macro, args.mode, args.pad, args.target_col)
reflow_col: Optional[int] = None
target_macro = macro
if args.reflow and args.target_col is None:
target_macro, reflow_col = reflow_macro_if_needed(macro, args.pad)
if reflow_col is not None:
target_col = reflow_col
if not args.quiet and len(target_macro.lines) != len(macro.lines):
logger.info(f" -> Reflowed {len(target_macro.lines) - len(macro.lines)} line(s) to preserve column {target_col}")
else:
target_col = compute_target_column(target_macro, args.mode, args.pad, args.target_col)
if not args.quiet:
logger.info(f" -> Target alignment column: {target_col}")
realigned = realign_macro_lines(macro, target_col)
realigned = realign_macro_lines(target_macro, target_col)
start_idx = macro.start_line - 1
start_idx = macro.start_line - 1 + line_offset
end_idx = start_idx + len(macro.lines)
if new_file_lines[start_idx:end_idx] != realigned:
new_file_lines[start_idx:end_idx] = realigned
line_offset += len(realigned) - len(macro.lines)
modified = True
diff_text = None
@@ -258,6 +491,8 @@ def main():
parser.add_argument("--check", action="store_true", help="Exit with code 1 if misaligned macros exist")
parser.add_argument("--mode", choices=["min", "max", "majority"], default="min",
help="Alignment mode: 'min' (max_len + pad), 'max' (max existing col), 'majority' (dominant col)")
parser.add_argument("--reflow", type=str_to_bool, nargs="?", const=True, default=True,
help="Reflow long lines in mostly-aligned macros instead of shifting the whole macro (default: true)")
parser.add_argument("--pad", type=int, default=2, help="Spaces between longest line and backslash (default: 2)")
parser.add_argument("--target-col", type=int, default=None, help="Force alignment to an exact column")
parser.add_argument("--all", action="store_true", help="Realign all macros even if already aligned (default: only misaligned)")
+4 -2
View File
@@ -466,10 +466,11 @@ def print_bubbles_timeline(op):
logger.info(f" Avg Thread DMA IDLE : {avg_dma_idle:.1f}%")
logger.info("-" * 100)
hmx_threads = {e['thread'] for e in events if e['event'].startswith('HMX')}
logger.info(" Per-Thread Idle Analysis:")
for t in active_threads:
stats = thread_stats[t]
thread_name = f"Thread {t:<2} (HVX)" if t != 10 else "Thread 10 (HMX)"
thread_name = f"Thread {t:<2} (HMX)" if t in hmx_threads else f"Thread {t:<2} (HVX)"
logger.info(f" {thread_name} -> Compute Idle: {stats['compute_idle_pct']:.1f}% | DMA Idle: {stats['dma_idle_pct']:.1f}%")
all_bubbles = []
@@ -531,8 +532,9 @@ def print_ascii_summary(op_name, dims, types, usec, cycles, events):
norm_evt = normalize_event_name(evt)
thread_totals[t][norm_evt] += dur
hmx_threads = {e['thread'] for e in events if e['event'].startswith('HMX')}
for t in sorted(thread_totals.keys()):
thread_name = f"Thread {t} (HVX)" if t != 10 else "Thread 10 (HMX)"
thread_name = f"Thread {t} (HMX)" if t in hmx_threads else f"Thread {t} (HVX)"
sorted_evts = sorted(thread_totals[t].items(), key=lambda item: item[0])
evt_strs = []
+12 -8
View File
@@ -433,7 +433,7 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
time_mappers = {dev: DeviceTimeMapper(dev, filtered_ops) for dev in unique_devices}
# Process events
completed_events = []
completed_events: List[Dict[str, Any]] = []
if trace_events:
trace_events = sorted(trace_events, key=lambda e: e['unwrapped_cycles'])
@@ -516,6 +516,9 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
dur_ns = tm.dur_cycles_to_ns(e['start_cyc'], e['end_cyc'] - e['start_cyc'])
e['dur_ns'] = max(dur_ns, 1)
# Identify HMX threads dynamically from HMX* events
hmx_threads = {(e['device'], e['thread']) for e in completed_events if e['event'].startswith('HMX')}
# Allocate slots (sub-tracks) to prevent overlaps on same virtual track
active_slots = defaultdict(list)
for e in completed_events:
@@ -528,7 +531,7 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
norm_evt = normalize_event_name(evt, e['info'])
if norm_evt == "DMA":
track_key = (dev, t, "DMA")
elif t == 10:
elif (dev, t) in hmx_threads:
track_key = (dev, t, "HMX")
else:
track_key = (dev, t, "HVX")
@@ -552,23 +555,24 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
evt = e['event']
slot = e['slot']
dev = e['device']
is_hmx = (dev, t) in hmx_threads
norm_evt = normalize_event_name(evt, e['info'])
if norm_evt == "DMA":
track_evt = "DMA"
evt_id = 1
elif t == 10:
elif is_hmx:
track_evt = "HMX"
evt_id = 3
else:
track_evt = "HVX"
evt_id = 2
t_sort = 1 if t == 10 else t + 2
t_sort = 1 if is_hmx else t + 2
dev_idx = device_to_idx[dev]
# Unique UUID for each sub-track
if t == 10:
if is_hmx:
uuid = dev_idx * 10000000 + 20 # HMX thread track UUID
else:
uuid = int(dev_idx * 10000000 + t_sort * 1000000 + evt_id * 1000 + slot)
@@ -601,9 +605,9 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
hmx_packet = make_trace_packet(0, track_descriptor=make_track_descriptor(hmx_track_uuid, parent_uuid=proc_uuid, thread=hmx_thread_desc))
write_trace_packet_to_file(f, hmx_packet)
# Define Thread Tracks (T0, T1, ..., T9) for this device
# Define Thread Tracks for this device
dev_used_tracks = {uuid: val for uuid, val in used_tracks.items() if val[0] == dev}
unique_threads = sorted(list(set(t for (_, t, _, _) in dev_used_tracks.values() if t != 10)))
unique_threads = sorted(list(set(t for (_, t, _, _) in dev_used_tracks.values() if (dev, t) not in hmx_threads)))
for t in unique_threads:
thread_uuid = dev_idx * 10000000 + 10 + t
thread_name = f"T{t}"
@@ -623,7 +627,7 @@ def generate_perfetto_trace(filtered_ops, trace_events, output_path):
for uuid in sorted(used_tracks.keys()):
dev, t, evt, slot = used_tracks[uuid]
dev_idx = device_to_idx[dev]
if t == 10:
if (dev, t) in hmx_threads:
continue
name = f"T{t} {evt}"
rank = 0 if evt == "HVX" else 1