Skip to content

Conversation

@teslae1
Copy link

@teslae1 teslae1 commented Dec 5, 2025

This is a change that solves a single typing error and moves the
codebase towards the goal of being able to run tinygrad normally with option
TYPED=1 (open issue #7889).

The type error being solved (full trace below):

typeguard.TypeCheckError: argument "x" (tinygrad.uop.ops.UPat) did not match any element in the union:
  Self: is not an instance of the self type (tinygrad.uop.ops.UPatAny)
  float: is neither float or int
  int: is not an instance of int
  bool: is not an instance of bool

Produced by running

python3 -c "import os; os.environ['TYPED'] = '1'; import tinygrad"

on windows (system data below).

Data of system that error was produced on

  • Python version: Python 3.13.1
  • typeguard version: 4.4.4
  • OS: Microsoft Windows [Version 10.0.19045.6216]

Full error trace

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import os; os.environ['TYPED'] = '1'; import tinygrad
                                          ^^^^^^^^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\__init__.py", line 5, in <module>
    from tinygrad.tensor import Tensor                                    # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\tensor.py", line 15, in <module>
    from tinygrad.engine.schedule import ScheduleItem, complete_create_schedule_with_vars
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\engine\schedule.py", line 6, in <module>
    from tinygrad.uop.spec import type_verify, tensor_spec
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\uop\spec.py", line 271, in <module>
    from tinygrad.codegen.opt import Opt, OptOps
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\codegen\__init__.py", line 14, in <module>
    from tinygrad.codegen.late.expander import expander, pm_pre_expander, pm_group_for_reduce
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\codegen\late\expander.py", line 6, in <module>
    from tinygrad.schedule.rangeify import BufferizeOpts
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\schedule\rangeify.py", line 9, in <module>
    from tinygrad.codegen.simplify import pm_flatten_range, pm_reduce_simplify
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_importhook.py", line 98, in exec_module
    super().exec_module(module)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\codegen\simplify.py", line 89, in <module>
    ((UPat.var("x")+UPat.var("y")).or_casted() < UPat.var("c"), lambda x,y,c: (x < (c.cast(y.dtype)-y)) if no_range(y) and no_range(c) else None),
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KarlMogensen\source\repos\contributor\tinygrad\tinygrad\mixin\math.py", line 200, in __lt__
    def __lt__(self, x: Self | ConstType):
      return self.alu(Ops.CMPLT, self.ufix(x))
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_functions.py", line 137, in check_argument_types
    check_type_internal(value, annotation, memo)
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_checkers.py", line 960, in check_type_internal
    checker(value, origin_type, args, memo)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\KarlMogensen\AppData\Local\Programs\Python\Python313\Lib\site-packages\typeguard\_checkers.py", line 427, in check_union
    raise TypeCheckError(f"did not match any element in the union:\n{formatted_errors}")
typeguard.TypeCheckError: argument "x" (tinygrad.uop.ops.UPat) did not match any element in the union:
  Self: is not an instance of the self type (tinygrad.uop.ops.UPatAny)
  float: is neither float or int
  int: is not an instance of int
  bool: is not an instance of bool

@teslae1 teslae1 marked this pull request as draft December 5, 2025 07:08
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

This branch currently is behind tinygrad/master. The line count difference bot is disabled.

@teslae1 teslae1 changed the title [TYPED=1] fix mathmixin typecheck [pr] [TYPED=1] fix mathmixin typecheck Dec 8, 2025
@teslae1
Copy link
Author

teslae1 commented Dec 8, 2025

@chenyuxyz Hey, just wanted to get some input for this change.

My approach is:

  1. Changing the code being typechecked to pass TYPED=1

But i could also approach it by:
2. Changing the typecheck to pass TYPED=1

Am i correct in assuming 1 is the correct approach?

I know i am breaking one of the unit tests with this change which also points me in the direction that this is not the way to go about fixing the this issue - but i wanted to get some input before i worked further in this direction.

The more verbose assumption behind this pull request is that we don't want to do math comparisons such as "tinygrad.uop.ops.UPatAny < tinygrad.uop.ops.UPat" but actually want the left hand side in this case to be "tinygrad.uop.ops.UPat" as the type system enforces in math.py by the following type decoration

  def __lt__(self, x: Self | ConstType):

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.

1 participant