Skip to content

Instantly share code, notes, and snippets.

@smithjessk
Created November 5, 2022 02:00
Show Gist options
  • Save smithjessk/abccdd4d21f9fc65c4a22565b67d095b to your computer and use it in GitHub Desktop.
Save smithjessk/abccdd4d21f9fc65c4a22565b67d095b to your computer and use it in GitHub Desktop.
benchmarked typeguard
# %%
from torchtyping import TensorType as TT, patch_typeguard
import torch as t
from typeguard import typechecked
patch_typeguard()
# %%
def compute_times(size):
x = t.rand(size, size)
print(f'{x.shape=}')
print("computing x @ x inline without a function call")
%timeit x @ x
@typechecked
def at(x: TT[size, size], y: TT[size, size]) -> TT[size, size]:
return x @ y
print("computing x @ x with a function call that uses typeguard")
%timeit at(x, x)
for dim in [1, 10, 100, 1000]:
compute_times(dim)
# %%
for dim in [2000, 4000]:
compute_times(dim)
# %%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment