|
| 1 | +import numpy as np |
| 2 | +import conf |
| 3 | +from PE import PE |
| 4 | +from Activiation import Relu |
| 5 | +from IOCompression import * |
| 6 | + |
| 7 | + |
| 8 | +class EyerissF: |
| 9 | + GlobalBuffer = conf.SRAMSize |
| 10 | + EyerissWidth = conf.EyerissWidth |
| 11 | + EyerissHeight = conf.EyerissHeight |
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self.__InitPEs__() |
| 15 | + |
| 16 | + def Conv2d(self, Picture, FilterWeight, PictureNum, FilterWeightNum): |
| 17 | + |
| 18 | + PictureColumnLength, FilterWeightColumnLength = self.__DataDeliver__(Picture, FilterWeight, PictureNum, FilterWeightNum) |
| 19 | + self.__run__() |
| 20 | + ConvedArray = self.__PsumTransport__(PictureColumnLength, FilterWeightColumnLength) |
| 21 | + ReluedConvedArray = Relu(ConvedArray) |
| 22 | + self.__SetALLPEsState__(conf.ClockGate) |
| 23 | + |
| 24 | + return ReluedConvedArray |
| 25 | + |
| 26 | + def __InitPEs__(self, PEsWidth=conf.EyerissWidth, PEsHeight=conf.EyerissHeight): |
| 27 | + self.PEArray = list() |
| 28 | + for x in range(0, PEsHeight): |
| 29 | + self.PEArray.append(list()) |
| 30 | + for y in range(0, PEsWidth): |
| 31 | + self.PEArray[x].append(PE()) |
| 32 | + |
| 33 | + def __SetALLPEsState__(self, State): |
| 34 | + |
| 35 | + assert State == conf.ClockGate or State == conf.Running |
| 36 | + |
| 37 | + for ColumnELement in range(0, EyerissF.EyerissHeight): |
| 38 | + for RowElement in range(0, EyerissF.EyerissWidth): |
| 39 | + self.PEArray[ColumnELement][RowElement].SetPEState(State) |
| 40 | + |
| 41 | + def __SetPEsRunningState__(self, PictureColumnLength, FilterWeightColumnLength): |
| 42 | + |
| 43 | + # 卷积核行数必须小于图片行数 |
| 44 | + assert FilterWeightColumnLength <= PictureColumnLength |
| 45 | + |
| 46 | + # 卷积核行数必须小于Eyeriss高度 |
| 47 | + assert FilterWeightColumnLength <= EyerissF.EyerissHeight |
| 48 | + |
| 49 | + # 图片行数必须小于 Eyeriss长宽只和-1 |
| 50 | + assert PictureColumnLength <= EyerissF.EyerissHeight + EyerissF.EyerissWidth - 1 |
| 51 | + |
| 52 | + for ColumnELement in range(0, FilterWeightColumnLength): |
| 53 | + for RowElement in range(0, PictureColumnLength + 1 - FilterWeightColumnLength): |
| 54 | + try: |
| 55 | + self.PEArray[ColumnELement][RowElement].SetPEState(conf.Running) |
| 56 | + except: |
| 57 | + pass |
| 58 | + |
| 59 | + def __SetALLPEImgNumAndFltNum__(self, ImageNum, FilterNum): |
| 60 | + for ColumnELement in range(0, EyerissF.EyerissHeight): |
| 61 | + for RowElement in range(0, EyerissF.EyerissWidth): |
| 62 | + self.PEArray[ColumnELement][RowElement].SetPEImgAndFlt(ImageNum, FilterNum) |
| 63 | + |
| 64 | + def __DataDeliver__(self, Picture, FilterWeight, ImageNum, FilterNum): |
| 65 | + # put the pic and filter row data into PEArray |
| 66 | + |
| 67 | + # Eyeriss越界检查 |
| 68 | + # 卷积核行数不能超过 Eyeriss的高度(12) |
| 69 | + assert len(FilterWeight) <= self.EyerissHeight |
| 70 | + |
| 71 | + # 图片的行数不能超过 卷积核行数 + Eyeriss宽度(14) -1 |
| 72 | + assert len(Picture) <= len(FilterWeight) + self.EyerissWidth - 1 |
| 73 | + |
| 74 | + PictureColumnLength = len(Picture) |
| 75 | + FilterWeightColumnLength = len(FilterWeight) |
| 76 | + |
| 77 | + self.__SetALLPEImgNumAndFltNum__(ImageNum, FilterNum) |
| 78 | + self.__SetPEsRunningState__(PictureColumnLength, FilterWeightColumnLength) |
| 79 | + |
| 80 | + # filterWeight 从左到右 |
| 81 | + for ColumnELement in range(0, len(FilterWeight)): |
| 82 | + for RowElement in range(0, self.EyerissWidth): |
| 83 | + self.PEArray[ColumnELement][RowElement].SetFilterWeight(FilterWeight[ColumnELement]) |
| 84 | + |
| 85 | + # ImageRow 从左下到右上 |
| 86 | + for ColumnELement in range(0, len(Picture)): |
| 87 | + DeliverinitR = 0 |
| 88 | + DeliverinitH = ColumnELement |
| 89 | + for c in range(0, ColumnELement + 1): |
| 90 | + try: |
| 91 | + # 当len(pic)大于12的时候会发生一场,找不到PEArray[13][0],但是与后续不影响 |
| 92 | + self.PEArray[DeliverinitH][DeliverinitR].SetImageRow(Picture[ColumnELement]) |
| 93 | + except: |
| 94 | + pass |
| 95 | + |
| 96 | + DeliverinitR = DeliverinitR + 1 |
| 97 | + DeliverinitH = DeliverinitH - 1 |
| 98 | + |
| 99 | + return PictureColumnLength, FilterWeightColumnLength |
| 100 | + |
| 101 | + def __run__(self): |
| 102 | + # 整个系统开始计算 |
| 103 | + |
| 104 | + for x in range(0, conf.EyerissHeight): |
| 105 | + for y in range(0, conf.EyerissWidth): |
| 106 | + if self.PEArray[x][y].PEState == conf.Running: |
| 107 | + self.PEArray[x][y].CountPsum() |
| 108 | + |
| 109 | + def __PsumTransport__(self, PictureColumnLength, FilterWeightColumnLength): |
| 110 | + |
| 111 | + line = list() |
| 112 | + result = list() |
| 113 | + for RowElement in range(0, PictureColumnLength + 1 - FilterWeightColumnLength): |
| 114 | + |
| 115 | + # 清空list |
| 116 | + line.clear() |
| 117 | + for ColumnElement in range(0, FilterWeightColumnLength).__reversed__(): |
| 118 | + # 从上到下把psum加入list |
| 119 | + line.append(self.PEArray[ColumnElement][RowElement].Psum) |
| 120 | + |
| 121 | + # 将list中的Psum做和,得到一行卷积值,保存到r中 |
| 122 | + result.append(np.sum(line, axis=0, dtype=int)) |
| 123 | + |
| 124 | + # 将r中全部的卷积值组合成一个矩阵,并返回 |
| 125 | + if result == []: |
| 126 | + return |
| 127 | + |
| 128 | + return np.vstack(result) |
| 129 | + |
| 130 | + def __ShowPEState__(self, x, y): |
| 131 | + print("PE is : ", x, ",", y) |
| 132 | + |
| 133 | + if self.PEArray[x][y].PEState == conf.Running: |
| 134 | + print("PEState : Running") |
| 135 | + |
| 136 | + else: |
| 137 | + print("PEState : ClockGate") |
| 138 | + |
| 139 | + print("FilterWeight :", self.PEArray[x][y].FilterWeight) |
| 140 | + print("ImageRow :", self.PEArray[x][y].ImageRow) |
| 141 | + |
| 142 | + def __ShowAllPEState__(self): |
| 143 | + |
| 144 | + xx = list() |
| 145 | + yy = list() |
| 146 | + for x in range(conf.EyerissHeight): |
| 147 | + for y in range(conf.EyerissWidth): |
| 148 | + self.__ShowPEState__(x, y) |
| 149 | + if self.PEArray[x][y].PEState == conf.Running: |
| 150 | + yy.append(1) |
| 151 | + else: |
| 152 | + yy.append(0) |
| 153 | + |
| 154 | + xx.append(yy) |
| 155 | + yy = [] |
| 156 | + print(np.array(xx)) |
| 157 | + |
| 158 | + def __ShowRunningPEState__(self): |
| 159 | + |
| 160 | + c = 0 |
| 161 | + xx = list() |
| 162 | + yy = list() |
| 163 | + for x in range(conf.EyerissHeight): |
| 164 | + for y in range(conf.EyerissWidth): |
| 165 | + |
| 166 | + if self.PEArray[x][y].PEState == conf.Running: |
| 167 | + self.__ShowPEState__(x, y) |
| 168 | + c = c + 1 |
| 169 | + yy.append(1) |
| 170 | + else: |
| 171 | + yy.append(0) |
| 172 | + xx.append(yy) |
| 173 | + yy = [] |
| 174 | + print("一共有", c, "个PE正在运行") |
| 175 | + print(np.array(xx)) |
| 176 | + |
| 177 | + def __ShowStates__(self): |
| 178 | + c = 0 |
| 179 | + xx = list() |
| 180 | + yy = list() |
| 181 | + for x in range(conf.EyerissHeight): |
| 182 | + for y in range(conf.EyerissWidth): |
| 183 | + |
| 184 | + if self.PEArray[x][y].PEState == conf.Running: |
| 185 | + c = c + 1 |
| 186 | + yy.append(1) |
| 187 | + else: |
| 188 | + yy.append(0) |
| 189 | + xx.append(yy) |
| 190 | + yy = [] |
| 191 | + print("一共有", c, "个PE正在运行") |
| 192 | + print(np.array(xx)) |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == '__main__': |
| 196 | + ... |
0 commit comments