-
Notifications
You must be signed in to change notification settings - Fork 36
Add hl.rand op with seed arg lowering to tl.rand #652
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import torch | ||
|
||
from .._compiler.ast_extension import expr_from_string | ||
from .._compiler.compile_environment import CompileEnvironment | ||
from ..exc import NotInsideKernel | ||
from . import _decorators | ||
from .ref_tile import RefTile | ||
|
||
if TYPE_CHECKING: | ||
import ast | ||
|
||
from .._compiler.inductor_lowering import CodegenState | ||
|
||
__all__ = ["rand"] | ||
|
||
|
||
@_decorators.api(tiles_as_sizes=True) | ||
def rand( | ||
shape: list[object], | ||
seed: int, | ||
dtype: torch.dtype = torch.float32, | ||
device: torch.device | None = None, | ||
) -> torch.Tensor: | ||
""" | ||
The main propose of ``hl.rand`` is to explicitly pass a seed arg for deterministic | ||
randomness in helion kernels, whereas ``torch.rand_like`` doesn't take seed arg | ||
(though it can seeded globally)`. ``hl.rand`` lower to ``tl.rand(seed, offset)`` with ``offset`` | ||
built from a linear range over the allocation and reshaped to the given shape. | ||
|
||
Note: | ||
Only use within ``hl.tile()`` loops for creating local tensors. | ||
For host allocations, use ``torch.rand()``. | ||
|
||
Args: | ||
shape: A list of sizes | ||
seed: int seed for the random number generator | ||
dtype: currently only float32 supported | ||
|
||
Returns: | ||
torch.Tensor: A device tensor of the given shape and dtype filled with random values | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
@helion.kernel | ||
def process_kernel(x: torch.Tensor) -> torch.Tensor: | ||
output = torch.zeros_like(x) | ||
(m,) = x.shape | ||
for (tile_m,) in hl.tile([m]): | ||
output[tile_m] = hl.rand([tile_m], seed=seed) | ||
return output | ||
|
||
""" | ||
raise NotInsideKernel | ||
|
||
|
||
@_decorators.register_fake(rand) | ||
def _rand_fake( | ||
shape: list[int | torch.SymInt], | ||
seed: int, | ||
dtype: torch.dtype = torch.float32, | ||
device: torch.device | None = None, | ||
) -> torch.Tensor: | ||
if not isinstance(shape, (list, tuple)): | ||
raise TypeError(f"Expected list[SymInt], got {type(shape).__name__}") | ||
env = CompileEnvironment.current() | ||
env.add_kernel_tensor_size(shape) | ||
return torch.empty( | ||
[*shape], | ||
dtype=dtype, | ||
device=env.device if device is None else device, | ||
) | ||
|
||
|
||
@_decorators.codegen(rand) | ||
def _rand_codegen(state: CodegenState) -> ast.AST: | ||
fake_value = state.fake_value | ||
assert isinstance(fake_value, torch.Tensor) | ||
shape_str = state.device_function.tile_strategy.shape_str(fake_value.size()) | ||
|
||
numel = " * ".join(shape_str.strip("[]").split(",")) | ||
seed_ast = state.ast_arg(1) | ||
offs_expr = f"tl.arange(0, {numel}).reshape({shape_str})" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is incorrect.
|
||
expr = f"tl.rand({{seed}}, {offs_expr})" | ||
|
||
return expr_from_string(expr, seed=seed_ast) | ||
|
||
|
||
@_decorators.get_masked_value(rand) | ||
def _( | ||
node: torch.fx.Node, | ||
) -> float: | ||
return 0 | ||
|
||
|
||
@_decorators.ref(rand) | ||
def _( | ||
shape: list[int | RefTile], | ||
seed: int, | ||
dtype: torch.dtype = torch.float32, | ||
device: torch.device | None = None, | ||
) -> torch.Tensor: | ||
processed_shape: list[int] = [] | ||
for s in shape: | ||
if isinstance(s, RefTile): | ||
processed_shape.append(s.end - s.begin) | ||
else: | ||
processed_shape.append(int(s)) | ||
env = CompileEnvironment.current() | ||
gen = torch.Generator(device=env.device if device is None else device) | ||
gen.manual_seed(seed) | ||
return torch.rand( | ||
processed_shape, | ||
dtype=dtype, | ||
generator=gen, | ||
device=env.device if device is None else device, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.