forked from tinygrad/tinygrad
import and type cleanups [pr] (#8359)
Dict and DefaultDict and some imports
This commit is contained in:
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
import itertools, functools
|
||||
from dataclasses import dataclass
|
||||
from collections import defaultdict
|
||||
from typing import Optional, cast, Final, DefaultDict, Callable, Sequence
|
||||
from typing import Optional, cast, Final, Callable, Sequence
|
||||
from enum import Enum, auto
|
||||
|
||||
from tinygrad.ops import GroupOp, KernelInfo, UOp, Ops, can_pad, print_uops, type_verify, resolve, Variable, sint, \
|
||||
@@ -566,7 +566,7 @@ class Kernel:
|
||||
|
||||
# **** kernel outputs ****
|
||||
|
||||
kernel_cnt: Final[DefaultDict[str, int]] = defaultdict(int)
|
||||
kernel_cnt: Final[defaultdict[str, int]] = defaultdict(int)
|
||||
@functools.cached_property
|
||||
def name(self) -> str:
|
||||
# kernel name (before late upcast)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from __future__ import annotations
|
||||
from typing import Optional, TYPE_CHECKING, Any, DefaultDict, Callable
|
||||
from typing import Optional, TYPE_CHECKING, Any, Callable
|
||||
import functools, itertools, operator
|
||||
from collections import defaultdict
|
||||
from tinygrad.dtype import dtypes, ImageDType, PtrDType
|
||||
@@ -19,7 +19,7 @@ def fold_expanded(ex, buf):
|
||||
is_load, is_image = new_srcs[0].op is Ops.LOAD, isinstance(buf.dtype, ImageDType)
|
||||
|
||||
# first, extract all the relevant offsets
|
||||
offsets_rootsrc: DefaultDict[Any, dict] = defaultdict(dict)
|
||||
offsets_rootsrc: defaultdict[Any, dict] = defaultdict(dict)
|
||||
for i,s in enumerate(new_srcs):
|
||||
idx = s.src[0].src[1]
|
||||
if s.dtype.count != 1 or (is_image and idx.dtype.count == 2): continue
|
||||
|
||||
+3
-3
@@ -2,7 +2,7 @@ from __future__ import annotations
|
||||
import os, functools, platform, time, re, contextlib, operator, hashlib, pickle, sqlite3, tempfile, pathlib, string, ctypes, sys, gzip
|
||||
import urllib.request, subprocess, shutil, math, contextvars, types, copyreg, inspect, importlib
|
||||
from dataclasses import dataclass
|
||||
from typing import Dict, Union, ClassVar, Optional, Iterable, Any, TypeVar, Callable, Sequence, TypeGuard
|
||||
from typing import Union, ClassVar, Optional, Iterable, Any, TypeVar, Callable, Sequence, TypeGuard
|
||||
|
||||
T = TypeVar("T")
|
||||
U = TypeVar("U")
|
||||
@@ -181,7 +181,7 @@ def diskcache_clear():
|
||||
drop_tables = cur.execute("SELECT 'DROP TABLE IF EXISTS ' || quote(name) || ';' FROM sqlite_master WHERE type = 'table';").fetchall()
|
||||
cur.executescript("\n".join([s[0] for s in drop_tables] + ["VACUUM;"]))
|
||||
|
||||
def diskcache_get(table:str, key:Union[Dict, str, int]) -> Any:
|
||||
def diskcache_get(table:str, key:Union[dict, str, int]) -> Any:
|
||||
if CACHELEVEL == 0: return None
|
||||
if isinstance(key, (str,int)): key = {"key": key}
|
||||
conn = db_connection()
|
||||
@@ -194,7 +194,7 @@ def diskcache_get(table:str, key:Union[Dict, str, int]) -> Any:
|
||||
return None
|
||||
|
||||
_db_tables = set()
|
||||
def diskcache_put(table:str, key:Union[Dict, str, int], val:Any, prepickled=False):
|
||||
def diskcache_put(table:str, key:Union[dict, str, int], val:Any, prepickled=False):
|
||||
if CACHELEVEL == 0: return val
|
||||
if isinstance(key, (str,int)): key = {"key": key}
|
||||
conn = db_connection()
|
||||
|
||||
+1
-2
@@ -2,8 +2,7 @@ from __future__ import annotations
|
||||
import functools, itertools, operator
|
||||
from tinygrad.helpers import all_same, all_int, dedup, prod, DEBUG, RING, getenv
|
||||
from tinygrad.dtype import DType
|
||||
from tinygrad.ops import Ops, MathTrait, UOp
|
||||
from tinygrad.shape.shapetracker import sint
|
||||
from tinygrad.ops import Ops, MathTrait, UOp, sint
|
||||
|
||||
def all_reduce(bop: Ops, lbs: list[UOp]) -> list[UOp]:
|
||||
assert all_int(lbs[0].shape), f"does not support symbolic shape {lbs[0].shape}"
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
import math
|
||||
from tinygrad.tensor import Tensor, dtypes
|
||||
from tinygrad.tensor import Tensor
|
||||
from tinygrad.dtype import dtypes
|
||||
from tinygrad.device import is_dtype_supported
|
||||
from tinygrad.helpers import prod, make_tuple, flatten
|
||||
from tinygrad.nn import optim, state, datasets # noqa: F401
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Dict, Optional, Union, DefaultDict, Literal, Callable, cast
|
||||
from typing import Optional, Union, Literal, Callable, cast
|
||||
import os, math
|
||||
from collections import defaultdict, Counter
|
||||
from tinygrad.ops import GroupOp, Ops, UOp, PatternMatcher, UPat
|
||||
@@ -77,7 +77,7 @@ class CStyleLanguage(Renderer):
|
||||
type_map: dict[DType, str] = {}
|
||||
infinity: str = "INFINITY"
|
||||
nan: str = "NAN"
|
||||
code_for_op: Dict = {
|
||||
code_for_op: dict = {
|
||||
Ops.SQRT: lambda x,dtype: f"sqrt({x})", Ops.RECIP: lambda x,dtype: f"(1/{x})", Ops.NEG: lambda x,dtype: f"-{x}",
|
||||
Ops.EXP2: lambda x,dtype: f"exp2({x})", Ops.LOG2: lambda x,dtype: f"log2({x})", Ops.SIN: lambda x,dtype: f"sin({x})",
|
||||
Ops.AND: lambda a,b,dtype: f"({a}&{b})", Ops.XOR: lambda a,b,dtype: f"({a}^{b})", Ops.OR: lambda a,b,dtype: f"({a}|{b})",
|
||||
@@ -117,7 +117,7 @@ class CStyleLanguage(Renderer):
|
||||
bufs: dict[UOp, tuple[str, tuple[DType, bool]]] = {}
|
||||
kernel = []
|
||||
depth = 1
|
||||
c: DefaultDict[str, int] = defaultdict(int)
|
||||
c: defaultdict[str, int] = defaultdict(int)
|
||||
for u in uops:
|
||||
if u.op in (Ops.DEFINE_GLOBAL, Ops.DEFINE_VAR):
|
||||
r[u] = f"data{u.arg}" if u.op is Ops.DEFINE_GLOBAL else u.arg[0]
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
# a python uops emulator
|
||||
# works to test the tensor cores, and all the uops in general
|
||||
# this is the (living) definition of uops
|
||||
import sys
|
||||
from typing import Optional, Any, TYPE_CHECKING
|
||||
import pickle, base64, itertools, time, struct
|
||||
import pickle, base64, itertools, time, struct, sys
|
||||
from tinygrad.dtype import DType, dtypes, ImageDType, PtrDType, truncate
|
||||
from tinygrad.helpers import all_same, getenv, flatten, get_single_element
|
||||
from tinygrad.device import Compiled, Compiler, Allocator
|
||||
|
||||
Reference in New Issue
Block a user