-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdim_types.py
More file actions
223 lines (168 loc) · 4.78 KB
/
dim_types.py
File metadata and controls
223 lines (168 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# dim_types.py — Algebraic Type System for Dim
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Union, Any, Tuple
@dataclass
class Type:
def unify(self, other):
# type: (Type) -> Optional[Type]
if self == other:
return self
if isinstance(other, UnknownType):
return self
if isinstance(self, UnknownType):
return other
return None
@dataclass
class PrimType(Type):
kind: str
def __repr__(self):
return self.kind
def __hash__(self):
return hash(self.kind)
I32 = PrimType("i32")
I64 = PrimType("i64")
F32 = PrimType("f32")
F64 = PrimType("f64")
BOOL = PrimType("bool")
STR = PrimType("str")
UNIT = PrimType("Unit")
NEVER = PrimType("Never")
@dataclass
class TypeVar(Type):
name: str
def __repr__(self):
return "'" + self.name
def __hash__(self):
return hash(self.name)
@dataclass
class FunctionType(Type):
params: List[Type]
return_type: Type
is_async: bool = False
capabilities: List[str] = field(default_factory=list)
generics: List[str] = field(default_factory=list)
def __repr__(self):
p_str = ", ".join(repr(p) for p in self.params)
async_str = "async " if self.is_async else ""
cap_str = " caps:" + str(self.capabilities) if self.capabilities else ""
gen_str = "<" + ", ".join(self.generics) + ">" if self.generics else ""
return (
async_str
+ "fn"
+ gen_str
+ "("
+ p_str
+ ") -> "
+ repr(self.return_type)
+ cap_str
)
@dataclass
class TensorType(Type):
dtype: Type
shape: List[Optional[int]]
def __repr__(self):
shape_str = ", ".join(str(d) if d is not None else "?" for d in self.shape)
return f"Tensor[{self.dtype}, [{shape_str}]]"
def shape_rank(self) -> int:
return len(self.shape)
def is_fully_static(self) -> bool:
return all(d is not None for d in self.shape)
def unify_shapes(self, other: "TensorType") -> Optional[List[Optional[int]]]:
if len(self.shape) != len(other.shape):
return None
result: List[Optional[int]] = []
for a, b in zip(self.shape, other.shape):
if a is not None and b is not None:
if a != b:
return None
result.append(a)
elif a is not None:
result.append(a)
else:
result.append(b)
return result
@dataclass
class SymbolicDim:
name: str
constraints: List[Tuple[str, int]] = field(default_factory=list)
def __repr__(self):
if self.constraints:
cons = ", ".join(f"{op}{val}" for op, val in self.constraints)
return f"{self.name}::{cons}"
return self.name
def constrain(self, op: str, val: int):
self.constraints.append((op, val))
@dataclass
class PromptType(Type):
name: str
input_type: Type
output_type: Type
deterministic: bool = False
def __repr__(self):
return "Prompt<" + self.name + ">"
@dataclass
class GenericType(Type):
name: str
args: List[Type]
def __repr__(self):
args_str = ", ".join(repr(a) for a in self.args)
return self.name + "[" + args_str + "]"
@dataclass
class RefType(Type):
inner: Type
mutable: bool = False
def __repr__(self):
m = "mut " if self.mutable else ""
return f"&{m}{self.inner}"
def __hash__(self):
return hash(("RefType", self.inner, self.mutable))
@dataclass
class StructType(Type):
name: str
fields: Dict[str, Type] = field(default_factory=dict)
def __repr__(self):
return self.name
@dataclass
class EnumType(Type):
name: str
variants: Dict[str, Optional[List[Type]]] = field(default_factory=dict)
def __repr__(self):
return self.name
@dataclass
class TraitType(Type):
name: str
methods: Dict[str, FunctionType]
def __repr__(self):
return "trait " + self.name
@dataclass
class FutureType(Type):
inner: Type
def __repr__(self):
return "Future[" + repr(self.inner) + "]"
@dataclass
class UnknownType(Type):
def __repr__(self):
return "?"
BUILTIN_TYPES = {
"i32": I32,
"i64": I64,
"f32": F32,
"f64": F64,
"bool": BOOL,
"str": STR,
"Unit": UNIT,
"Never": NEVER,
}
def resolve_builtin(name):
return BUILTIN_TYPES.get(name)
def numeric_promotion(t1, t2):
NUMERIC_TYPES = {I32, I64, F32, F64}
if t1 not in NUMERIC_TYPES or t2 not in NUMERIC_TYPES:
return None
if t1 == F64 or t2 == F64:
return F64
if t1 == F32 or t2 == F32:
return F32
if t1 == I64 or t2 == I64:
return I64
return I32