Skip to content
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

transformer_engine: wrap checker_fn in langctx and cleanup (PR2473) #24

Merged
merged 5 commits into from
Mar 25, 2024

Conversation

kshitij12345
Copy link
Collaborator

This allows checker_fn to call TensorProxy methods.

Following snippet should work now:

import thunder
from thunder.extend import OperatorExecutor, register_executor
import torch

my_exec = OperatorExecutor("my_exec")
register_executor(my_exec)

def _my_checker(x, y):
    # Helper function as input shape can be (*, Hin)
    def _view_input_as_2d(x):
        shape = x.shape
        return x.view((-1, shape[-1]))

    _view_input_as_2d(x)
    return True

my_exec.register_implementation(
    thunder.torch.mul,
    checker=_my_checker,
    execution_transform=lambda x, y: x * y,
)

def foo(x, y):
    return x * y

cfunc = thunder.jit(foo, executors=[my_exec])

# Works when requires_grad=False!
# x = torch.randn(16, 16, requires_grad=False)
x = torch.randn(16, 16, requires_grad=True)
cfunc(x, x)

@kshitij12345 kshitij12345 changed the title jit: checker_fn should run under langctx in split_forward_backward jit: checker_fn should run under langctx in split_forward_backward (PR2473) Mar 21, 2024
@kshitij12345 kshitij12345 marked this pull request as ready for review March 21, 2024 10:28
@t-vi
Copy link
Collaborator

t-vi commented Mar 24, 2024

I know it is late, but how about just decorating the checker function that has it with @langctx(Languages.TORCH) (from thunder.core.langctxs import langctx, Languages) for now (until we have figured out a bit more where we want the langctx)?
This is what we do for other checkers and transforms in executors, too, I would document that to be the way.

@kshitij12345 kshitij12345 changed the title jit: checker_fn should run under langctx in split_forward_backward (PR2473) transformer_engine: wrap checker_fn in langctx and cleanup (PR2473) Mar 25, 2024
Copy link
Contributor

@carmocca carmocca left a comment

Choose a reason for hiding this comment

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

This is useful. I remember having to do

    # cannot use `.view` so reduce the dimension manually
    # n_rows = X.view(-1, X.shape[-1]).shape[0]
    n_rows = reduce(int.__mul__, X.shape[:-1])

for the Unsloth RMSNorm kernel

@carmocca carmocca merged commit b873afa into Lightning-AI:main Mar 25, 2024
36 checks passed
@kshitij12345 kshitij12345 deleted the langctx-checker-fn branch March 25, 2024 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants