Skip to content

feat: support privateuse1 backends in benchmark harness - #2708

Open
markc-614 wants to merge 4 commits into
pytorch:mainfrom
markc-614:mashaobin/benchmark/adapt-privateuse1-bench
Open

markc-614 wants to merge 4 commits into
pytorch:mainfrom
markc-614:mashaobin/benchmark/adapt-privateuse1-bench

Conversation

@markc-614

Copy link
Copy Markdown

Generalize device handling so third-party accelerators registered via the privateuse1 mechanism can run the benchmark, instead of hardcoding CUDA

  • Detect and append the registered privateuse1 backend to SUPPORT_DEVICE_LIST
  • Replace torch.cuda.synchronize() with backend-agnostic torch.get_device_module(device).synchronize() in timing/memory paths
  • Add register_deterministic_backend() and thread device through save/load_deterministic_dict() to apply backend-specific determinism flags
  • Gate gpu_peak_mem on max_memory_allocated availability instead of device == "cuda"
  • Generalize OOM detection from torch.cuda.OutOfMemoryError to torch.OutOfMemoryError
  • Use get_device_module for empty_cache / amp / device-name resolution

  Generalize device handling so third-party accelerators registered via the
  privateuse1 mechanism can run the benchmark, instead of hardcoding CUDA

  - Detect and append the registered privateuse1 backend to SUPPORT_DEVICE_LIST
  - Replace torch.cuda.synchronize() with backend-agnostic
    torch.get_device_module(device).synchronize() in timing/memory paths
  - Add register_deterministic_backend() and thread `device` through
    save/load_deterministic_dict() to apply backend-specific determinism flags
  - Gate gpu_peak_mem on max_memory_allocated availability instead of device == "cuda"
  - Generalize OOM detection from torch.cuda.OutOfMemoryError to torch.OutOfMemoryError
  - Use get_device_module for empty_cache / amp / device-name resolution
@meta-cla

meta-cla Bot commented Sep 20, 2026

Copy link
Copy Markdown

Hi @markc-614!

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 cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Sep 20, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the cla signed label Sep 20, 2026
@markc-614

Copy link
Copy Markdown
Author

@xuzhao9 @benjaminglass1 @retonym Could you please review this PR when you get a chance? Thanks!

@benjaminglass1 benjaminglass1 left a comment

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.

I don't have merge authority in this repo, but generally on board with this.

Comment on lines +125 to +133
if device == "cuda":
torch.cuda.reset_peak_memory_stats()
torch.cuda.empty_cache()
else:
device_module = torch.get_device_module(device)
if hasattr(device_module, "reset_peak_memory_stats"):
device_module.reset_peak_memory_stats()
if hasattr(device_module, "empty_cache"):
device_module.empty_cache()

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.

Couldn't these be collapsed? It doesn't looks like the cuda path is doing anything different.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for your comment. fixed.

Comment on lines +137 to +143
device_id = torch.cuda.current_device()
gpu_peak_mem = torch.cuda.max_memory_allocated() / 10**9
else:
device_module = torch.get_device_module(device)
if hasattr(device_module, "max_memory_allocated"):
device_id = device_module.current_device()
gpu_peak_mem = device_module.max_memory_allocated() / 10**9

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.

Similarly here about collapsing these.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for your comment. fixed.

Comment thread torchbenchmark/util/env_check.py Outdated
Comment on lines +346 to +358
if device == "cuda":
torch.backends.cudnn.allow_tf32 = determinism_dict[
"torch.backends.cudnn.allow_tf32"
]
torch.backends.cudnn.benchmark = determinism_dict[
"torch.backends.cudnn.benchmark"
]
torch.backends.cuda.matmul.allow_tf32 = determinism_dict[
"torch.backends.cuda.matmul.allow_tf32"
]
elif device in _DETERMINISTIC_FLAGS:
for flag in _DETERMINISTIC_FLAGS[device]:
_set_flag_value(flag, determinism_dict[flag])

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.

Is it too big of a change to put the CUDA flags in the _DETERMINISM_FLAGS dict as well?

@markc-614 markc-614 Sep 29, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not too big. I'll add them.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The CUDA flags are now registered. One behavioral note:
torch.backends.cudnn.deterministic is now snapshotted on save and restored on load, whereas before it was forced to True and never restored. This makes save/restore symmetric; happy to revert that bit if you'd rather keep the old restore semantics exactly.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants