-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.py
More file actions
61 lines (44 loc) · 1.32 KB
/
tile.py
File metadata and controls
61 lines (44 loc) · 1.32 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
from typing import List, Tuple
import dataclasses as dc
import numpy as np
import opcodes
@dc.dataclass
class Tile:
"""A portion of a tensor stored in SRAM."""
shape: Tuple[int]
dtype_in: np.dtype
is_transposed: bool
@dc.dataclass
class TileOp:
"""Abstract base class for a tile operator"""
def generate(self) -> List[opcodes.OpCode]:
raise NotImplementedError
class Add(TileOp):
tile_in_a: Tile
tile_in_b: Tile
tile_out: Tile
class MatMul(TileOp):
tile_in_a: Tile
tile_in_b: Tile
tile_out: Tile
def generate(self) -> List[opcodes.OpCode]:
# Generate one dot product instruction for each of the 32x32 entries of
# the result matrix.
return [
opcodes.DotProduct(
opcodes.ScalarAdd(self.tile_in_a.address, i * 32),
opcodes.ScalarAdd(self.tile_in_b.address, j * 32),
opcodes.ScalarAdd(self.tile_out.address, i * 32 + j),
)
for i in range(32)
for j in range(32)
]
class ExpSum(TileOp):
tile_in: Tile
tile_out_exps: Tile
tile_out_accumulator: Tile
class FusedTileOperator(TileOp):
tile_op1: TileOp
tile_op2: TileOp
def generate(self) -> List[opcodes.OpCode]:
return self.tile_op1.generate() + self.tile_op2.generate()