KEMBAR78
Allow Custom Time Unit When Printing Profiler Table by GeorgeWigley · Pull Request #157913 · pytorch/pytorch · GitHub
Skip to content

Conversation

@GeorgeWigley
Copy link
Contributor

@GeorgeWigley GeorgeWigley commented Jul 9, 2025

Overview

This PR adds a kwarg to the table() method of the profiler allowing users to specify a time unit to be used for all results in the profiling table. The available options are: s, ms and us. If an invalid unit or no unit is provided, then a time unit is selected based on the size of the value (current default behaviour).

Testing

A unit test has been added to verify this works correctly.

Documentation

I couldn't find any documentation specific to the table() function beyond doc strings which have been updated.

Example Output

import torch
from torch.profiler import profile

with profile() as prof:
    res = torch.mm(torch.rand(1024, 1024), torch.rand(1024, 1024))

print(prof.key_averages().table(time_unit="s"))
print(prof.key_averages().table(time_unit="ms"))
print(prof.key_averages().table(time_unit="us"))
print(prof.key_averages().table())

----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
                  Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
            aten::rand         0.04%        0.000s        10.36%        0.014s        0.007s             2  
           aten::empty         0.04%        0.000s         0.04%        0.000s        0.000s             2  
        aten::uniform_        10.27%        0.014s        10.27%        0.014s        0.007s             2  
              aten::mm        89.64%        0.119s        89.64%        0.119s        0.119s             1  
    aten::resolve_conj         0.00%        0.000s         0.00%        0.000s        0.000s             3  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
Self CPU time total: 0.133s

----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
                  Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
            aten::rand         0.04%       0.055ms        10.36%      13.735ms       6.868ms             2  
           aten::empty         0.04%       0.054ms         0.04%       0.054ms       0.027ms             2  
        aten::uniform_        10.27%      13.626ms        10.27%      13.626ms       6.813ms             2  
              aten::mm        89.64%     118.892ms        89.64%     118.896ms     118.896ms             1  
    aten::resolve_conj         0.00%       0.004ms         0.00%       0.004ms       0.001ms             3  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
Self CPU time total: 132.631ms

----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
                  Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
            aten::rand         0.04%      55.495us        10.36%   13735.202us    6867.601us             2  
           aten::empty         0.04%      54.121us         0.04%      54.121us      27.061us             2  
        aten::uniform_        10.27%   13625.586us        10.27%   13625.586us    6812.793us             2  
              aten::mm        89.64%  118892.284us        89.64%  118895.981us  118895.981us             1  
    aten::resolve_conj         0.00%       3.697us         0.00%       3.697us       1.232us             3  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
Self CPU time total: 132631.183us

----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
                  Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
            aten::rand         0.04%      55.495us        10.36%      13.735ms       6.868ms             2  
           aten::empty         0.04%      54.121us         0.04%      54.121us      27.061us             2  
        aten::uniform_        10.27%      13.626ms        10.27%      13.626ms       6.813ms             2  
              aten::mm        89.64%     118.892ms        89.64%     118.896ms     118.896ms             1  
    aten::resolve_conj         0.00%       3.697us         0.00%       3.697us       1.232us             3  
----------------------  ------------  ------------  ------------  ------------  ------------  ------------  
Self CPU time total: 132.631ms

@GeorgeWigley GeorgeWigley requested a review from sraikund16 as a code owner July 9, 2025 13:42
@pytorch-bot
Copy link

pytorch-bot bot commented Jul 9, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 749b2e2 with merge base 86eaf45 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@GeorgeWigley
Copy link
Contributor Author

@pytorchbot label "release notes: profiler"

@pytorch-bot pytorch-bot bot added the release notes: profiler release notes category label Jul 9, 2025
@sraikund16
Copy link
Contributor

This seems reasonable to me. Just for posterity can you please add an example output to the PR description?

)
self.assertIn("Total MFLOPs", profiler_output)

def test_override_time_units(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

In this unit test you make sure that the time unit you see is as expected but you don't check to make sure that the conversion is correct. Can you please add this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good point, I'll update the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As override_time_unit is a nested function, I cannot write unit tests for it directly. I've opted to instead verify the correctly converted values are present in the table.

@GeorgeWigley GeorgeWigley requested a review from sraikund16 July 10, 2025 12:58
@sraikund16
Copy link
Contributor

Thanks!

@GeorgeWigley
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 10, 2025
@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: profiler release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants