Skip to content

Conversation

zzb610
Copy link
Contributor

@zzb610 zzb610 commented Nov 20, 2024

Fixes #ISSUE_NUMBER

Description

The event should be synchronized before calling elapsed_time to ensure that the corresponding event has completed. This change is necessary to avoid runtime errors related to incomplete events.

For more details, refer to the relevant code in the PyTorch repository:

Example Code

import torch
def case():
    torch.cuda.set_device(0)

    init_start_event = torch.cuda.Event(enable_timing=True)
    init_end_event = torch.cuda.Event(enable_timing=True)

    init_start_event.record()
    for i in range(100):
        a = torch.randn(size=(1024, 1024), device="cuda")
        b = torch.randn(size=(1024, 1024), device="cuda")
        out = torch.add(a, b)
        out = torch.matmul(a, b)
        out = torch.matmul(a, b)
        out = torch.matmul(a, b)
        out = torch.matmul(a, b)
    init_end_event.record()

    # print("init_end_event.query(): ", init_end_event.query()) # False
    # # RuntimeError: CUDA error: device not ready
    # print(
    #     f"CUDA event elapsed time: {init_start_event.elapsed_time(init_end_event) / 1000}sec"
    # )

    # OK
    init_end_event.synchronize()
    print("init_end_event.query(): ", init_end_event.query())
    print(
        f"CUDA event elapsed time: {init_start_event.elapsed_time(init_end_event) / 1000}sec"
    )

Copy link

pytorch-bot bot commented Nov 20, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/tutorials/3165

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot
Copy link
Contributor

Hi @zzb610!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@zzb610 zzb610 closed this Nov 20, 2024
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.

3 participants