Skip to content

feat: Initial support for agnostic Event tracking across devices#29

Open
saforem2 wants to merge 2 commits intomainfrom
feat/events
Open

feat: Initial support for agnostic Event tracking across devices#29
saforem2 wants to merge 2 commits intomainfrom
feat/events

Conversation

@saforem2
Copy link
Copy Markdown
Owner

@saforem2 saforem2 commented Feb 26, 2025

Copilot Summary

This pull request introduces a new feature to the ezpz library by adding the get_default_event function and the Event class. These additions enhance the library's capability to handle default events for different device types.

New feature additions:

  • src/ezpz/__init__.py: Imported the get_default_event function from the ezpz.event module and added it to the list of exported functions. [1] [2]

  • src/ezpz/event.py: Added the get_default_event function, which determines the appropriate event type based on the device type and availability. Introduced the Event class, which initializes with default event settings and device-specific configurations.

Summary by Sourcery

New Features:

  • Introduce Event class and get_default_event function to handle default events for different device types (cuda, xpu, mps, cpu).

@saforem2 saforem2 requested a review from Copilot February 26, 2025 18:17
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Feb 26, 2025

Reviewer's Guide by Sourcery

This pull request introduces a new Event class and get_default_event function to handle default events for different device types. The get_default_event function determines the appropriate event type based on the device and its availability. The Event class initializes with default event settings and device-specific configurations.

Sequence diagram for get_default_event function

sequenceDiagram
    participant User
    participant get_default_event
    participant torch.cuda
    participant torch.xpu
    participant torch.mps
    participant torch.cpu

    User->>get_default_event: device_type, enable_timing, blocking, interprocess
    get_default_event->>get_default_event: device_type = device_type or ezpz.get_torch_device_type()
    alt device_type == 'cuda' and torch.cuda.is_available()
        get_default_event->>torch.cuda: Event(enable_timing, blocking, interprocess)
        torch.cuda-->>get_default_event: Event
    else device_type == 'xpu' and torch.xpu.is_available()
        get_default_event->>torch.xpu: Event(enable_timing)
        torch.xpu-->>get_default_event: Event
    else device_type == 'mps'
        get_default_event->>torch.mps: Event(enable_timing)
        torch.mps-->>get_default_event: Event
    else
        get_default_event->>torch.cpu: Event()
        torch.cpu-->>get_default_event: Event
    end
    get_default_event-->>User: Event
Loading

Class diagram for the new Event class

classDiagram
    class Event {
        - enable_timing: bool = False
        - blocking: bool = False
        - interprocess: bool = False
        - device_type: str
        - rank: int
        - local_rank: int
        - device: torch.device
        - event: Event
        + __post_init__()
        + __new__(enable_timing: bool = False)
    }
    note for Event "This class encapsulates device-specific event handling."
Loading

File-Level Changes

Change Details Files
Introduced the get_default_event function to determine the appropriate event type based on device type and availability.
  • Added logic to handle 'cuda' devices using torch.cuda.Event if available.
  • Added logic to handle 'xpu' devices using torch.xpu.Event if available.
  • Added logic to handle 'mps' devices using torch.mps.Event.
  • Added a fallback to torch.cpu.Event for other or unavailable device types.
  • Allows configuration of enable_timing, blocking, and interprocess options for CUDA events.
src/ezpz/event.py
Implemented the Event class for managing device-specific event configurations.
  • Initialized the class with default settings for enable_timing, blocking, and interprocess.
  • Determined the device type, rank, and local rank using ezpz utilities.
  • Created a torch.device instance based on the device type and local rank.
  • Initialized a default event using the get_default_event function.
src/ezpz/event.py
Exposed the get_default_event function in the ezpz library.
  • Imported the get_default_event function from the ezpz.event module.
  • Added get_default_event to the list of exported functions in __init__.py.
src/ezpz/__init__.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @saforem2 - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using a factory pattern instead of a long if/else chain in get_default_event.
  • The Event class's __new__ method seems to have a typo (event.__new__) and doesn't align with the intended behavior; it should likely be removed or corrected.
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/ezpz/event.py
self.device = torch.device(f'{self.device_type}:{self.local_rank}')
self.event = get_default_event()

def __new__(cls, enable_timing: bool = False):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Potential misreference of 'new' in the Event class.

The new method calls 'event.new(cls, enable_timing=enable_timing)', but 'event' is not defined as a class name; it should likely be 'Event.new(cls, enable_timing=enable_timing)' or use 'super().new(cls)' to ensure proper object instantiation.

Comment thread src/ezpz/event.py
self.rank = ezpz.get_rank()
self.local_rank = ezpz.get_local_rank()
self.device = torch.device(f'{self.device_type}:{self.local_rank}')
self.event = get_default_event()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (bug_risk): Inconsistent passing of initialization parameters for the event.

The new method accepts an enable_timing parameter, but post_init calls get_default_event() without passing any parameters. This disconnect means that any intended configuration provided during construction is not propagated to the underlying event, potentially leading to unexpected behavior.

Suggested change
self.event = get_default_event()
self.event = get_default_event(enable_timing=self.enable_timing)

Comment thread src/ezpz/event.py
Comment on lines +39 to +48
@dataclass
class Event:
"""
Event class
"""
enable_timing: bool = False
blocking: bool = False
interprocess: bool = False

def __post_init__(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion: Overriding new in a dataclass may conflict with post-initialization.

Dataclasses typically rely on init and post_init for initialization. Overriding new here—especially to inject extra parameters—is unconventional and can lead to subtle issues in instance creation. Consider consolidating initialization logic within post_init to ensure consistent and clear behavior.

Suggested implementation:

Removing the new method ensures that the dataclass uses the default instance creation process with init and post_init, avoiding potential issues with initialization.

Copy link
Copy Markdown

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.

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread src/ezpz/event.py
self.event = get_default_event()

def __new__(cls, enable_timing: bool = False):
return event.__new__(cls, enable_timing=enable_timing)
Copy link

Copilot AI Feb 26, 2025

Choose a reason for hiding this comment

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

The new method incorrectly references 'event.new' (in the following line) even though 'event' is undefined. Consider using super().new(cls) or referring to Event.new(cls) instead.

Suggested change
return event.__new__(cls, enable_timing=enable_timing)
return super().__new__(cls)

Copilot uses AI. Check for mistakes.
Comment thread src/ezpz/event.py
Comment on lines +53 to +54
self.event = get_default_event()

Copy link

Copilot AI Feb 26, 2025

Choose a reason for hiding this comment

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

[nitpick] The post_init method calls get_default_event() without passing the instance’s event configuration (enable_timing, blocking, interprocess), which may lead to inconsistent behavior. Consider forwarding these parameters to ensure the event is created with the intended settings.

Suggested change
self.event = get_default_event()
self.event = get_default_event(
enable_timing=self.enable_timing,
blocking=self.blocking,
interprocess=self.interprocess
)

Copilot uses AI. Check for mistakes.
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