Skip to content

[DEV] Support nested wrapper #70

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

Merged
merged 14 commits into from
Jun 4, 2025
Merged

[DEV] Support nested wrapper #70

merged 14 commits into from
Jun 4, 2025

Conversation

mark14wu
Copy link
Collaborator

  • tests/test_trace_add_clients.py
    • new test validating client deduplication and dynamic addition via stacked @trace decorators.
  • triton_viz/clients/sanitizer/sanitizer.py
    • Replaced function-level factory with class Sanitizer(ABC); concrete implementation selected in new.
    • Added NullSanitizer for backend == "off".
    • Registered SanitizerBruteForce, SanitizerSymbolicExecution, and NullSanitizer with Sanitizer.register(...).
    • Removed unused imports and switched to cfg for backend lookup.
  • triton_viz/core/client.py
    • ClientManager.__init__ defaults clients to an empty list.
    • Added add_clients() to append new clients with type-based deduplication.
  • triton_viz/core/config.py
    • Stored _sanitizer_backend lazily; raises at first access if unset instead of on module import.
    • Environment variable fetch simplified; removed immediate ValueError.
  • triton_viz/core/patch.py
    • Replaced direct imports of report_grid_execution_progress and sanitizer_backend with cfg.*.
  • triton_viz/core/trace.py
    • Introduced _normalize_client() and add_clients() for runtime client management and deduplication.
    • trace() decorator:
      • Supports stacking:
        • if target is already a Trace, it appends clients
        • if a JITFunction, it wraps once.
    • Trace.__init__ now initializes an empty ClientManager then adds clients via add_clients().
  • Header/Import cleanup across modified files to consistently reference configuration via cfg and drop unused modules.

@mark14wu mark14wu requested a review from Copilot May 30, 2025 00:59
Copy link

@Copilot 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

This PR enables support for nested trace wrappers while improving dynamic client addition and deduplication for instrumentation. Key changes include refactoring the Trace class to use an internal add_clients method, updating client instantiation to use a factory pattern via the Sanitizer abstract class, and cleaning up configuration and patching code.

  • Updated Trace class to support stacking and client deduplication
  • Refactored sanitizer instantiation to use a class-based factory with NullSanitizer
  • Header and import cleanup to consistently reference configuration via cfg

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
triton_viz/core/trace.py Refactored Trace: added _normalize_client and add_clients methods
triton_viz/core/patch.py Updated progress reporting and sanitizer backend lookups
triton_viz/core/config.py Made sanitizer_backend lazy, deferring its check until first access
triton_viz/core/client.py Modified ClientManager: added add_clients; mutable default parameter
triton_viz/clients/sanitizer/sanitizer.py Switched to class-based sanitizer factory and added NullSanitizer
tests/test_trace_add_clients.py Added tests for client deduplication across stacked trace decorators

"""
@triton_viz.trace(("sanitizer", "profiler"))
@triton_viz.trace("tracer")
@triton_viz.trace(("sanitizer",)) # Duplicate Sanitizer (should be ignored)
Copy link
Member

Choose a reason for hiding this comment

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

It seems too complicated isn't it...Why not we just standardize the api to support only one of the possible use cases? It can be either an array of modes or nested annotations, but not both

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not hard for me to support both...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I want to support nested wrapper because I want to deal with the legacy code that use @triton_viz.sanitizer wrapper. With the new command-line tool triton-sanitizer, there may be nested wrappers.

Copy link
Member

Choose a reason for hiding this comment

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

It's not hard for me to support both...

It will be error-prone and complicate users. Let's just modify it to be either the first or the second convention.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I prefer the second convention because

I want to deal with the legacy code that use @triton_viz.sanitizer wrapper.

Copy link
Member

Choose a reason for hiding this comment

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

It's OK. Please update your code accordingly

@mark14wu mark14wu changed the title Support nested wrapper [DEV] Support nested wrapper May 30, 2025
raise ValueError("At least one client must be specified!")

if not isinstance(client, (str, Client)):
raise TypeError(f"Expected str or Client, got {type(client)}")

def decorator(kernel) -> Trace:
# When sanitizer is off, skip tracing and return the original kernel unchanged
if cfg.sanitizer_backend == "off":
return kernel

# First-time wrapping
if isinstance(kernel, JITFunction):
Copy link
Member

Choose a reason for hiding this comment

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

Have you tested this with @triton.autotune?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. Let me write a unittest with autotune.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe we can consider running all tests using CI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just ran test/test_autotune_add.py and it worked well.

Copy link
Member

Choose a reason for hiding this comment

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

Let's fix the bugs before merging

@mark14wu mark14wu force-pushed the support_nested_wrapper branch from 91e1409 to 06741bf Compare May 30, 2025 23:15
@mark14wu mark14wu merged commit 12dc9fc into keren/v2.0 Jun 4, 2025
0 of 2 checks passed
@mark14wu mark14wu deleted the support_nested_wrapper branch June 4, 2025 17:31
@@ -5,9 +5,9 @@ name: Python application

on:
push:
branches: [ "main" ]
branches: []
Copy link
Member

Choose a reason for hiding this comment

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

Those changes are suspicious

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change has been fixed in #71.
We use

   branches-ignore:
      - '**'

instead of

branches: []

to ignore all ci tests for now.

cd triton_viz
pip install pre-commit
pre-commit run --all-files
# - name: Lint with pre-commit
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Member

Choose a reason for hiding this comment

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

And why merge without making all tests passed?

Copy link
Collaborator Author

@mark14wu mark14wu Jun 4, 2025

Choose a reason for hiding this comment

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

I want to focus on indirect load first and fix CI later.

As for CI problem, I figured out that once you set TRITON_INTERPRET=1, triton will skip driver checking.
See https://github.com/triton-lang/triton/blob/d141ab8b1bfa8e8dc703459412cd827b09f80b21/python/triton/_internal_testing.py#L30
However, triton-viz cannot work with TRITON_INTERPRET=1 and needs to be fixed.

I will do this after I finish indirect load.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, it has to be compatible with TRITON_INTERPRET=1

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.

2 participants