Skip to content

[PyTorch] Avoid select op in PyTorch extensions - #865

Merged
timmoon10 merged 6 commits into
NVIDIA:mainfrom
timmoon10:cast-transpose-avoid-select-op
Jun 10, 2024
Merged

[PyTorch] Avoid select op in PyTorch extensions#865
timmoon10 merged 6 commits into
NVIDIA:mainfrom
timmoon10:cast-transpose-avoid-select-op

Conversation

@timmoon10

@timmoon10timmoon10 commented May 24, 2024

Copy link
Copy Markdown
Member

Description

While profiling CPU overheads in te.Linear with FP8 support enabled, I find that a non-trivial amount of CPU time is spent in cast-transpose functions:

image

Much of this runtime is spent indexing into FP8 scaling factors. It takes a ~5 us on CPU to create each PyTorch tensor, and this can become significant when GEMM kernels take ~100 us on GPU. (Actually, it seems much of this is profiler overhead. There's some overhead, but it's not as bad as 5 us.)

This PR makes some backward-compatible changes to some of the PyTorch extensions to avoid calling the select op, and thus avoiding some tensor creation. It adds optional arguments for tensor offsets so that we can do pointer arithmetic instead of relying on PyTorch. With these changes, I see a 1.05x speedup in the forward pass of very small te.Linears.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Changes

  • Add FP8 scaling factor offset args to cast-transpose PyTorch extensions
  • Add FP8 scaling factor offset args to LayerNorm and RMSNorm PyTorch extensions

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Signed-off-by: Tim Moon <tmoon@nvidia.com>
Signed-off-by: Tim Moon <tmoon@nvidia.com>
Signed-off-by: Tim Moon <tmoon@nvidia.com>
@timmoon10timmoon10 added the enhancement New feature or request label May 24, 2024
@timmoon10

Copy link
Copy Markdown
MemberAuthor

/te-ci pytorch

1 similar comment
@timmoon10

Copy link
Copy Markdown
MemberAuthor

/te-ci pytorch

@timmoon10
timmoon10force-pushed the cast-transpose-avoid-select-op branch from e659b96 to b96c4e4CompareMay 24, 2024 01:45
Signed-off-by: Tim Moon <tmoon@nvidia.com>
@timmoon10
timmoon10force-pushed the cast-transpose-avoid-select-op branch from b96c4e4 to 9470b5cCompareMay 24, 2024 01:47
@timmoon10

Copy link
Copy Markdown
MemberAuthor

/te-ci pytorch

@timmoon10

Copy link
Copy Markdown
MemberAuthor

/te-ci pytorch

@cyanguwacyanguwa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@timmoon10
timmoon10 merged commit 90f3c9a into NVIDIA:mainJun 10, 2024
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@timmoon10@cyanguwa