-
Notifications
You must be signed in to change notification settings - Fork 91
Add python-nvmath executor for prims.matmul and prims.linear #1917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds an executor leveraging the nvmath-python library for prims.matmul and prims.linear, including an autotuning phase to select an optimal cuBLASLt configuration and a subsequent comparison between nvmath and torch.matmul.
- Introduces a new test suite for verifying the executor behavior.
- Implements the executor logic with caching and autotuning in nvmathex.py.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
thunder/tests/test_nvmath_executor.py | New tests ensuring matmul and linear operators are executed correctly with the nvmath executor. |
thunder/executors/nvmathex.py | Executor implementation with caching, autotuning, and registration for matmul and linear ops. |
for more information, see https://pre-commit.ci
Would you create an issue for the |
CI issues are real dependency issues:
Maybe a |
@@ -0,0 +1,194 @@ | |||
import logging |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment
"""
The nvmath executor. ...
"""
would be nice
@dataclass(frozen=True, slots=True) | ||
class TensorDescriptor: | ||
""" | ||
A dataclass to store the shape, stride, dtype, and device index of a tensor for caching purposes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice comment
|
||
def execute_nvmath_matmul(mm: Matmul, a: torch.Tensor, b: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Executes a matrix multiplication operation using nvmath for the given operands and matmul executor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stellar comment
return mm.execute() # This function has about 75 µs overhead | ||
|
||
|
||
def nvmath_or_pytorch_matmul(mm: Matmul) -> Callable: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi @kiya00
We should look at generalizing selecting executors by benchmarking
return mm(a, b) | ||
|
||
|
||
def matmul_checker(a: TensorProxy, b: TensorProxy) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice checker
return [nvmath_matmul_ex] | ||
|
||
|
||
@ops((op for op in opinfos if op.name in ("matmul", "linear")), supported_executors=(nvMathTestExecutor(),)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
def test(op, device, dtype, executor, comparator): | ||
for sample in op.sample_inputs(device, dtype, requires_grad=False): | ||
# prims do not support broadcasting | ||
if sample.args[0].shape[:-2] != sample.args[1].shape[:-2]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are detailed manipulations of the sample inputs, and they make complete sense
One interesting extension to the OpInfos that might be interesting (and certainly is not for this PR) is to easily bind the sample inputs to the signatures of functions, so then instead of having to write
if len(sample.args) == 3:
or
if op.name == "linear" and len(sample.args) == 2:
a developer might be able to write something like
ba = sample.bind(op)
if ba['bias'] is not None:
...
Which isn't a huge improvement but it's a little more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds really cool @IvanYashchuk , but I'd be weary of having untested executors. |
We could add nvmath to a CI job, or we could look at testing this in NVIDIA's CI |
return [nvmath_matmul_ex] | ||
|
||
|
||
@ops((op for op in opinfos if op.name in ("matmul", "linear")), supported_executors=(nvMathTestExecutor(),)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test should be skipped when CUDA devices are not available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the test will be skipped but because the import is unconditional the CI fails during test discovery
import nvmath | ||
|
||
import torch | ||
from nvmath.linalg.advanced import Matmul, MatmulOptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is nvmath included in thunder dependencies already? If not I think it would be good to add an import check 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't add CUDA-only dependencies to the requirements.txt files. If nvmath is not installed, importing this file will fail with a standard Python import error. What import check would you like to see?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking at something like in TransformerEngine executor, but it is not mandatory ofc
lightning-thunder/thunder/executors/transformer_engineex.py
Lines 34 to 63 in 2d18b7a
TE_AVAILABLE: bool = package_available("transformer_engine") | |
# We rely on internal details of TransformerEngine like `_Linear` autograd.Function. | |
# As these details are not public, they can change | |
# Ex. addition of a positional argument for cpu_offloading (not as the last argument) | |
# between version 1.2 and 1.3. | |
# Hence, we have these guards based on version. | |
te: None | Any = None | |
if TE_AVAILABLE: | |
try: | |
import transformer_engine.pytorch as te | |
from transformer_engine.common import recipe | |
from transformer_engine.common.recipe import MXFP8BlockScaling, DelayedScaling | |
from transformer_engine.pytorch.constants import MXFP8_BLOCK_SCALING_SIZE | |
from transformer_engine.pytorch.module.linear import _Linear | |
from transformer_engine.pytorch.module.base import TransformerEngineBaseModule | |
from transformer_engine.pytorch.fp8 import FP8GlobalStateManager, get_default_fp8_recipe | |
from transformer_engine.pytorch.utils import check_dim_for_fp8_exec | |
from transformer_engine.pytorch.cpu_offload import CPUOffloadEnabled | |
import transformer_engine_torch as tex | |
except Exception as ex: | |
warnings.warn(f"transformer_engine failed to import with exception {ex}") | |
TE_AVAILABLE = False | |
TE_VERSION_2_0_PLUS = LooseVersion(version("transformer_engine")) > LooseVersion("2.0") | |
if not TE_VERSION_2_0_PLUS: | |
msg = f"Installed version of transformer_engine {version('transformer_engine')} is not supported, please upgrade to version 2.0 from https://github.com/NVIDIA/TransformerEngine/tree/release_v2.0. `transformer_engine_ex` will not be used." | |
warnings.warn(msg) | |
TE_AVAILABLE = False |
Adds an executor using https://github.com/NVIDIA/nvmath-python for
prims.matmul
andprims.linear
. This executor is expected to work with any (matmul-compatible) shape, stride, and dtype combination on CUDA devices.The executor has an autotuning phase to select a better cuBLASLt configuration for given shapes and strides, and on top of that, there's an additional autotuning phase for selecting nvmath-cuBLASLt or torch.matmul for execution, comparing the median of kernel times. The result of autotuning is cached for one program run.
Tests take about a minute to run:
Currently, there's a bug in OperatorExecutors that prevents its transformation from decomposing operations to find supported prims in the multilevel representation.
cc @Borda @mruberry