Skip to content

add inference_mode context manager with decorator support - #3621

Merged
geohot merged 4 commits into
tinygrad:masterfrom
MaxWolf-01:inference-mode-train-mode-decorators
Mar 9, 2024
Merged

geohot merged 4 commits into
tinygrad:masterfrom
MaxWolf-01:inference-mode-train-mode-decorators

Conversation

@MaxWolf-01

Copy link
Copy Markdown
Contributor

This is #3281 but with the right API.

Introduces inference_mode context manager akin to torch.inference_mode:

with Tensor.inference_mode(): ...
# OR
@Tensor.inference_mode()
def f(): ...

Also adds ability to use Tensor.train() as a decorator:

@Tensor.train():
def f(): ...

@geohot

geohot commented Mar 9, 2024

Copy link
Copy Markdown
Collaborator

Why is it val and not mode to match torch?

@github-actions

github-actions Bot commented Mar 9, 2024

Copy link
Copy Markdown
Contributor

Changes

Name                  Lines    Diff    Tokens/Line    Diff
------------------  -------  ------  -------------  ------
tinygrad/tensor.py      729      +5           22.0    -0.1


total lines changes: +5

Comment thread tinygrad/tensor.py
def __init__(self, val=True): self.val = val
def __enter__(self): self.prev, Tensor.training = Tensor.training, self.val
class train(ContextDecorator):
def __init__(self, mode:bool = True): self.mode = mode

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.

Err, why did this one change? Does this match torch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, torch also uses mode for train. See source code

@geohot
geohot merged commit 8ae85b2 into tinygrad:master Mar 9, 2024
@geohot

geohot commented Mar 9, 2024

Copy link
Copy Markdown
Collaborator

Cool, merged

@MaxWolf-01
MaxWolf-01 deleted the inference-mode-train-mode-decorators branch March 9, 2024 17:12
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.

2 participants