Skip to content

Add approximate fast path for ARM - #178639

Closed
scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:arm_approx_clock
Closed

scotts wants to merge 1 commit into
pytorch:mainfrom
scotts:arm_approx_clock

Conversation

@scotts

@scotts scotts commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

There is a similar PR out for Kineto, as these files are copy-pasted to there: pytorch/kineto#1333

cc @snadampal @milpuz01 @aditew01 @nikhil-arm @fadara01 @nWEIdia

@pytorch-bot

pytorch-bot Bot commented Mar 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 34174f7 with merge base 39565a7 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot

pytorch-bot Bot commented Mar 27, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

scotts added a commit to scotts/kineto that referenced this pull request Mar 27, 2026
Summary:
Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Differential Revision: D98514697
@scotts scotts added module: arm Related to ARM architectures builds of PyTorch. Includes Apple M1 topic: not user facing topic category labels Mar 27, 2026
@scotts
scotts marked this pull request as ready for review March 28, 2026 00:12
@meta-codesync

meta-codesync Bot commented Mar 30, 2026

Copy link
Copy Markdown

@scotts has imported this pull request. If you are a Meta employee, you can view this in D98725960.

scotts added a commit to scotts/kineto that referenced this pull request Mar 31, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Mar 31, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Mar 31, 2026
Summary:
Pull Request resolved: pytorch#1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:
Pull Request resolved: pytorch#1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:
Pull Request resolved: pytorch#1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
scotts added a commit to scotts/kineto that referenced this pull request Apr 2, 2026
Summary:
Pull Request resolved: pytorch#1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: jiannanWang

Differential Revision: D98514697
@scotts

scotts commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 3, 2026
@pytorchmergebot

Copy link
Copy Markdown
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

meta-codesync Bot pushed a commit to pytorch/kineto that referenced this pull request Apr 3, 2026
Summary:
Pull Request resolved: #1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: ryanzhang22, jiannanWang

Differential Revision: D98514697

fbshipit-source-id: 7dad173cf1a299ebdf28cfabfb2ff3e69e9c6473
weifengpy pushed a commit that referenced this pull request Apr 7, 2026
There is a similar PR out for Kineto, as these files are copy-pasted to there: pytorch/kineto#1333

Pull Request resolved: #178639
Approved by: https://github.com/jiannanWang
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
@saagarjha

Copy link
Copy Markdown
Contributor

Hi! Does this need a kineto bump to include the associated commit as well? I noticed I stopped getting CUDA events with this merged in.

pytorch-bot Bot pushed a commit that referenced this pull request Apr 10, 2026
There is a similar PR out for Kineto, as these files are copy-pasted to there: pytorch/kineto#1333

Pull Request resolved: #178639
Approved by: https://github.com/jiannanWang
pytorchmergebot pushed a commit that referenced this pull request Jul 7, 2026
## Summary

`getApproximateTime()` returns a cheap monotonic counter that the profiler calibrates against wall-clock time, avoiding a `clock_gettime` syscall on every timestamp. It uses `__rdtsc` on x86 and `cntvct_el0` on aarch64, but had **no RISC-V branch** — so on RISC-V every timestamp fell back to the `getTime()` syscall path, the slow path this mechanism exists to avoid.

This adds a RISC-V (rv64) fast path that reads the `time` CSR via `rdtime`, mirroring the aarch64 `cntvct_el0` path from #178639.

## Details

`rdtime` is a constant-frequency counter and is user-readable on Linux — the same assumption the aarch64 path already makes for `cntvct_el0`. The branch is gated on `__riscv_xlen == 64` because on RV32 `rdtime` returns only the low 32 bits, so RV32 correctly keeps the `getTime()` fallback.

**Alternative considered:** `rdcycle`. It counts core cycles, which vary with frequency scaling and are commonly privileged (trap) in user mode, so it is unsuitable for a wall-time approximation; `rdtime` is the right counter.

## Test plan

```
# riscv64 emits the rdtime fast path:
clang --target=riscv64-unknown-elf -march=rv64gc -ffreestanding -O2 -c harness.c
llvm-objdump -d harness.o        # getApproximateTime: c0102573  rdtime a0

# rv32 correctly keeps the syscall fallback (no rdtime, calls getTime):
clang --target=riscv32-unknown-elf -march=rv32gc -ffreestanding -O2 -c harness.c
llvm-objdump -dr harness.o       # no rdtime; R_RISCV_CALL_PLT getTime

# existing aarch64 path still compiles and runs (verified natively):
clang harness.c -o t && ./t      # rc=0
```

Part of the RISC-V enablement effort #180975 (Phase 1.3).

Pull Request resolved: #189000
Approved by: https://github.com/ezyang
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
## Summary

`getApproximateTime()` returns a cheap monotonic counter that the profiler calibrates against wall-clock time, avoiding a `clock_gettime` syscall on every timestamp. It uses `__rdtsc` on x86 and `cntvct_el0` on aarch64, but had **no RISC-V branch** — so on RISC-V every timestamp fell back to the `getTime()` syscall path, the slow path this mechanism exists to avoid.

This adds a RISC-V (rv64) fast path that reads the `time` CSR via `rdtime`, mirroring the aarch64 `cntvct_el0` path from pytorch#178639.

## Details

`rdtime` is a constant-frequency counter and is user-readable on Linux — the same assumption the aarch64 path already makes for `cntvct_el0`. The branch is gated on `__riscv_xlen == 64` because on RV32 `rdtime` returns only the low 32 bits, so RV32 correctly keeps the `getTime()` fallback.

**Alternative considered:** `rdcycle`. It counts core cycles, which vary with frequency scaling and are commonly privileged (trap) in user mode, so it is unsuitable for a wall-time approximation; `rdtime` is the right counter.

## Test plan

```
# riscv64 emits the rdtime fast path:
clang --target=riscv64-unknown-elf -march=rv64gc -ffreestanding -O2 -c harness.c
llvm-objdump -d harness.o        # getApproximateTime: c0102573  rdtime a0

# rv32 correctly keeps the syscall fallback (no rdtime, calls getTime):
clang --target=riscv32-unknown-elf -march=rv32gc -ffreestanding -O2 -c harness.c
llvm-objdump -dr harness.o       # no rdtime; R_RISCV_CALL_PLT getTime

# existing aarch64 path still compiles and runs (verified natively):
clang harness.c -o t && ./t      # rc=0
```

Part of the RISC-V enablement effort pytorch#180975 (Phase 1.3).

Pull Request resolved: pytorch#189000
Approved by: https://github.com/ezyang
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
## Summary

`getApproximateTime()` returns a cheap monotonic counter that the profiler calibrates against wall-clock time, avoiding a `clock_gettime` syscall on every timestamp. It uses `__rdtsc` on x86 and `cntvct_el0` on aarch64, but had **no RISC-V branch** — so on RISC-V every timestamp fell back to the `getTime()` syscall path, the slow path this mechanism exists to avoid.

This adds a RISC-V (rv64) fast path that reads the `time` CSR via `rdtime`, mirroring the aarch64 `cntvct_el0` path from pytorch#178639.

## Details

`rdtime` is a constant-frequency counter and is user-readable on Linux — the same assumption the aarch64 path already makes for `cntvct_el0`. The branch is gated on `__riscv_xlen == 64` because on RV32 `rdtime` returns only the low 32 bits, so RV32 correctly keeps the `getTime()` fallback.

**Alternative considered:** `rdcycle`. It counts core cycles, which vary with frequency scaling and are commonly privileged (trap) in user mode, so it is unsuitable for a wall-time approximation; `rdtime` is the right counter.

## Test plan

```
# riscv64 emits the rdtime fast path:
clang --target=riscv64-unknown-elf -march=rv64gc -ffreestanding -O2 -c harness.c
llvm-objdump -d harness.o        # getApproximateTime: c0102573  rdtime a0

# rv32 correctly keeps the syscall fallback (no rdtime, calls getTime):
clang --target=riscv32-unknown-elf -march=rv32gc -ffreestanding -O2 -c harness.c
llvm-objdump -dr harness.o       # no rdtime; R_RISCV_CALL_PLT getTime

# existing aarch64 path still compiles and runs (verified natively):
clang harness.c -o t && ./t      # rc=0
```

Part of the RISC-V enablement effort pytorch#180975 (Phase 1.3).

Pull Request resolved: pytorch#189000
Approved by: https://github.com/ezyang
ani300 pushed a commit to IBM/kineto-spyre that referenced this pull request Jul 28, 2026
Summary:
Pull Request resolved: pytorch/kineto#1333

Implements fast-path for ARM approximate clock.

There's a similar PR out for PyTorch, as these files are copy-pasted from there: pytorch/pytorch#178639

Reviewed By: ryanzhang22, jiannanWang

Differential Revision: D98514697

fbshipit-source-id: 7dad173cf1a299ebdf28cfabfb2ff3e69e9c6473
Signed-off-by: Antoni Viros i Martin <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
## Summary

`getApproximateTime()` returns a cheap monotonic counter that the profiler calibrates against wall-clock time, avoiding a `clock_gettime` syscall on every timestamp. It uses `__rdtsc` on x86 and `cntvct_el0` on aarch64, but had **no RISC-V branch** — so on RISC-V every timestamp fell back to the `getTime()` syscall path, the slow path this mechanism exists to avoid.

This adds a RISC-V (rv64) fast path that reads the `time` CSR via `rdtime`, mirroring the aarch64 `cntvct_el0` path from pytorch#178639.

## Details

`rdtime` is a constant-frequency counter and is user-readable on Linux — the same assumption the aarch64 path already makes for `cntvct_el0`. The branch is gated on `__riscv_xlen == 64` because on RV32 `rdtime` returns only the low 32 bits, so RV32 correctly keeps the `getTime()` fallback.

**Alternative considered:** `rdcycle`. It counts core cycles, which vary with frequency scaling and are commonly privileged (trap) in user mode, so it is unsuitable for a wall-time approximation; `rdtime` is the right counter.

## Test plan

```
# riscv64 emits the rdtime fast path:
clang --target=riscv64-unknown-elf -march=rv64gc -ffreestanding -O2 -c harness.c
llvm-objdump -d harness.o        # getApproximateTime: c0102573  rdtime a0

# rv32 correctly keeps the syscall fallback (no rdtime, calls getTime):
clang --target=riscv32-unknown-elf -march=rv32gc -ffreestanding -O2 -c harness.c
llvm-objdump -dr harness.o       # no rdtime; R_RISCV_CALL_PLT getTime

# existing aarch64 path still compiles and runs (verified natively):
clang harness.c -o t && ./t      # rc=0
```

Part of the RISC-V enablement effort pytorch#180975 (Phase 1.3).

Pull Request resolved: pytorch#189000
Approved by: https://github.com/ezyang
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 module: arm Related to ARM architectures builds of PyTorch. Includes Apple M1 topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants