Skip to content

[FEATURE] NKI interpreter - #206

Merged
mark14wu merged 106 commits into
mainfrom
nki/patch-dsl
Dec 23, 2025
Merged

mark14wu merged 106 commits into
mainfrom
nki/patch-dsl

Conversation

@latentCall145

Copy link
Copy Markdown
Collaborator

sorry for big diff :(

Adds the NKI frontend to Triton-Viz:

  • added a NKI interpreter to apply language-level patching (e.g. allow NKI kernels to be executed line-by-line with NumPy; modifies nki.py)
  • AST rewriting to convert NKI loads/stores to masked loads/stores (modifies nki_extract_slice.py)
  • Masked load/store implementation (modifies nki_masked_load.py)
  • added backend (one of "nki" or "triton") as arguments for patching (modifies patch.py, client.py)
  • added tracer callbacks for masked loads/stores (modifies data.py, tracer.py)
  • separate Triton and NKI Trace objects (modifies trace.py)
  • a lot of visualization stuff (credit: @gujialiang123)

Added Tests

  • nki-examples/: demo programs to try out the visualizer (currently, only nki-examples/matmul.py is supported)
  • tests/test_nki.py: basic NDArray slicing
  • tests/test_masked_load.py: make sure masked load/store works correctly

@mark14wu

mark14wu commented Nov 7, 2025

Copy link
Copy Markdown
Collaborator

Hi @mark14wu , can you also review the PR?

Yes. I have dropped some comments, but haven't finished reviewing all files.

@mark14wu

mark14wu commented Nov 7, 2025

Copy link
Copy Markdown
Collaborator

Some tests in CI is not working.

Traceback:
/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_wrapper.py:8: in <module>
    from triton_viz.wrapper import SANITIZER_COMMAND, PROFILER_COMMAND
triton_viz/__init__.py:1: in <module>
    from .core import trace, clear, config
triton_viz/core/__init__.py:1: in <module>
    from .trace import trace, clear
triton_viz/core/trace.py:7: in <module>
    from ..clients import Sanitizer, Profiler, Tracer
triton_viz/clients/__init__.py:1: in <module>
    from .profiler.profiler import Profiler
triton_viz/clients/profiler/profiler.py:1: in <module>
    from ...core.client import Client
triton_viz/core/client.py:8: in <module>
    from .patch import (
triton_viz/core/patch.py:61: in <module>
    from triton_viz.core.nki import nki_builder
triton_viz/core/nki.py:3: in <module>
    import neuronxcc.nki.language as nl
E   ModuleNotFoundError: No module named 'neuronxcc'

Please fix.

@Jokeren

Jokeren commented Dec 23, 2025

Copy link
Copy Markdown
Member

@mark14wu We need to merge this asap to start code clean up and move towards v3.0

@mark14wu mark14wu left a comment

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.

+1

Comment thread triton_viz/core/trace.py

launches: list[Launch] = []

T = TypeVar("T")

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.

Where is T used in this file?

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.

Comment thread triton_viz/core/trace.py
else:
raise ValueError(f"Unknown backend: {backend}")

# Handle NKI functions specifically

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.

if backend == "nki", the function decorator will return at line 177 and line 186 to 193 will never be executed.

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.

Comment thread triton_viz/core/trace.py
else:
raise ValueError(f"Unknown backend: {backend}")

# Handle NKI functions specifically

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.

if backend == "nki", the function decorator will return at line 177 and line 186 to 193 will never be executed.

def _extract_user_frames() -> list[traceback.FrameSummary]:
stack: list[traceback.FrameSummary] = list(traceback.extract_stack())
# drop current frames (this function and callers)
stack = stack[:-2]

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.

Maybe we should lift the _extract_user_frames function as a utility, put it somewhere like utils.py and reuse it in both sanitizer and tracer.

def _extract_user_frames() -> list[traceback.FrameSummary]:
stack: list[traceback.FrameSummary] = list(traceback.extract_stack())
# drop current frames (this function and callers)
stack = stack[:-2]

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.

What I mean is _get_traceback_info() in sanitizer.py

Comment thread triton_viz/core/data.py
call_path: list[traceback.StackSummary] = field(init=False, default_factory=list)
call_path: list[traceback.FrameSummary] = field(init=False, default_factory=list)

def __post_init__(self):

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 we extract call path in __post_init__?

def _extract_user_frames() -> list[traceback.FrameSummary]:
stack: list[traceback.FrameSummary] = list(traceback.extract_stack())
# drop current frames (this function and callers)
stack = stack[:-2]

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.

Please at least move those _extract_user_frames into clients/utils.py.

def _extract_user_frames() -> list[traceback.FrameSummary]:
stack: list[traceback.FrameSummary] = list(traceback.extract_stack())
# drop current frames (this function and callers)
stack = stack[:-2]

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.

@mark14wu

Copy link
Copy Markdown
Collaborator

Before merging, let me fix the conflicts.

@mark14wu
mark14wu merged commit 4b5ff4a into main Dec 23, 2025
1 check passed
@mark14wu
mark14wu deleted the nki/patch-dsl branch December 23, 2025 03:07
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