2424# See the License for the specific language governing permissions and
2525# limitations under the License.
2626
27+ import math
2728from typing import Dict , List , Tuple
2829
2930from Deeploy .AbstractDataTypes import PointerClass
@@ -135,25 +136,21 @@ def serializeTilingSolution(
135136
136137 # Every output is constructed by a pair of inputs. Reconstruct this pair.
137138 for cube in outputCubes :
139+ MOffset , OOffset = cube .offset [- 2 :]
140+ MSize , OSize = cube .dims [- 2 :]
138141
139- BSize = 1
140- BOffset = 0
141- BatchSize = 1
142- BatchOffset = 0
143-
144- if len (cube .offset ) == 2 :
145- (MOffset , OOffset ) = cube .offset
146- (MSize , OSize ) = cube .dims
147- elif len (cube .offset ) == 3 :
148- (BatchOffset , MOffset , OOffset ) = cube .offset
149- (BatchSize , MSize , OSize ) = cube .dims
142+ if len (cube .offset ) > 2 :
143+ BatchSize = math .prod (cube .dims [:- 2 ])
144+
145+ # Check that we don't tile upper dimensions
146+ if len (cube .offset ) > 3 :
147+ assert all (off == 0 for off in cube .offset [:- 3 ])
150148 else :
151- (BatchOffset , BOffset , MOffset , OOffset ) = cube .offset
152- (BatchSize , BSize , MSize , OSize ) = cube .dims
149+ BatchSize = 1
153150
154151 replacements ["M" ].append (MSize )
155152 replacements ["O" ].append (OSize )
156- replacements ["batch" ].append (BSize )
153+ replacements ["batch" ].append (BatchSize )
157154
158155 if transA == 0 :
159156 AMatrixOffsets = (MOffset , NOffset )
@@ -162,44 +159,26 @@ def serializeTilingSolution(
162159 AMatrixOffsets = (NOffset , MOffset )
163160 AMatrixShape = (NSize , MSize )
164161
162+ if len (buffA .shape ) > 2 :
163+ batchDimCount = len (buffA .shape ) - 2
164+ AMatrixOffsets = tuple (cube .offset [:- 2 ][- batchDimCount :]) + AMatrixOffsets
165+ AMatrixShape = tuple (cube .dims [:- 2 ][- batchDimCount :]) + AMatrixShape
166+
167+ ACube = HyperRectangle (AMatrixOffsets , AMatrixShape )
168+
165169 if transB == 0 :
166170 BMatrixOffsets = (NOffset , OOffset )
167171 BMatrixShape = (NSize , OSize )
168172 else :
169173 BMatrixOffsets = (OOffset , NOffset )
170174 BMatrixShape = (OSize , NSize )
171175
172- if len (buffA .shape ) == 2 :
173- ACube = HyperRectangle (AMatrixOffsets , AMatrixShape )
174- elif len (buffA .shape ) == 3 :
175- ACube = HyperRectangle ((BatchOffset ,) + AMatrixOffsets , (BatchSize ,) + AMatrixShape )
176- else :
177- ACube = HyperRectangle (
178- (
179- BatchOffset ,
180- BOffset ,
181- ) + AMatrixOffsets ,
182- (
183- BatchSize ,
184- BSize ,
185- ) + AMatrixShape ,
186- )
187-
188- if len (buffB .shape ) == 2 :
189- BCube = HyperRectangle (BMatrixOffsets , BMatrixShape )
190- elif len (buffB .shape ) == 3 :
191- BCube = HyperRectangle ((BatchOffset ,) + BMatrixOffsets , (BatchSize ,) + BMatrixShape )
192- else :
193- BCube = HyperRectangle (
194- (
195- BatchOffset ,
196- BOffset ,
197- ) + BMatrixOffsets ,
198- (
199- BatchSize ,
200- BSize ,
201- ) + BMatrixShape ,
202- )
176+ if len (buffB .shape ) > 2 :
177+ batchDimCount = len (buffB .shape ) - 2
178+ BMatrixOffsets = tuple (cube .offset [:- 2 ][- batchDimCount :]) + BMatrixOffsets
179+ BMatrixShape = tuple (cube .dims [:- 2 ][- batchDimCount :]) + BMatrixShape
180+
181+ BCube = HyperRectangle (BMatrixOffsets , BMatrixShape )
203182
204183 RequantCube = HyperRectangle ((OOffset ,), (OSize ,))
205184
@@ -367,25 +346,21 @@ def serializeTilingSolution(
367346
368347 # Every output is constructed by a pair of inputs. Reconstruct this pair.
369348 for cube in outputCubes :
349+ MOffset , OOffset = cube .offset [- 2 :]
350+ MSize , OSize = cube .dims [- 2 :]
351+
352+ if len (cube .offset ) > 2 :
353+ BatchSize = math .prod (cube .dims [:- 2 ])
370354
371- BSize = 1
372- BOffset = 0
373- BatchSize = 1
374- BatchOffset = 0
375-
376- if len (cube .offset ) == 2 :
377- (MOffset , OOffset ) = cube .offset
378- (MSize , OSize ) = cube .dims
379- elif len (cube .offset ) == 3 :
380- (BatchOffset , MOffset , OOffset ) = cube .offset
381- (BatchSize , MSize , OSize ) = cube .dims
355+ # Check that we don't tile upper dimensions
356+ if len (cube .offset ) > 3 :
357+ assert all (off == 0 for off in cube .offset [:- 3 ])
382358 else :
383- (BatchOffset , BOffset , MOffset , OOffset ) = cube .offset
384- (BatchSize , BSize , MSize , OSize ) = cube .dims
359+ BatchSize = 1
385360
386361 replacements ["M" ].append (MSize )
387362 replacements ["O" ].append (OSize )
388- replacements ["batch" ].append (BSize )
363+ replacements ["batch" ].append (BatchSize )
389364
390365 if transA == 0 :
391366 AMatrixOffsets = (MOffset , NOffset )
@@ -394,54 +369,36 @@ def serializeTilingSolution(
394369 AMatrixOffsets = (NOffset , MOffset )
395370 AMatrixShape = (NSize , MSize )
396371
372+ if len (buffA .shape ) > 2 :
373+ batchDimCount = len (buffA .shape ) - 2
374+ AMatrixOffsets = tuple (cube .offset [:- 2 ][- batchDimCount :]) + AMatrixOffsets
375+ AMatrixShape = tuple (cube .dims [:- 2 ][- batchDimCount :]) + AMatrixShape
376+
377+ ACube = HyperRectangle (AMatrixOffsets , AMatrixShape )
378+
397379 if transB == 0 :
398380 BMatrixOffsets = (NOffset , OOffset )
399381 BMatrixShape = (NSize , OSize )
400382 else :
401383 BMatrixOffsets = (OOffset , NOffset )
402384 BMatrixShape = (OSize , NSize )
403385
404- if len (buffA .shape ) == 2 :
405- ACube = HyperRectangle (AMatrixOffsets , AMatrixShape )
406- elif len (buffA .shape ) == 3 :
407- ACube = HyperRectangle ((BatchOffset ,) + AMatrixOffsets , (BatchSize ,) + AMatrixShape )
408- else :
409- ACube = HyperRectangle (
410- (
411- BatchOffset ,
412- BOffset ,
413- ) + AMatrixOffsets ,
414- (
415- BatchSize ,
416- BSize ,
417- ) + AMatrixShape ,
418- )
419-
420- if len (buffB .shape ) == 2 :
421- BCube = HyperRectangle (BMatrixOffsets , BMatrixShape )
422- elif len (buffB .shape ) == 3 :
423- BCube = HyperRectangle ((BatchOffset ,) + BMatrixOffsets , (BatchSize ,) + BMatrixShape )
424- else :
425- BCube = HyperRectangle (
426- (
427- BatchOffset ,
428- BOffset ,
429- ) + BMatrixOffsets ,
430- (
431- BatchSize ,
432- BSize ,
433- ) + BMatrixShape ,
434- )
386+ if len (buffB .shape ) > 2 :
387+ batchDimCount = len (buffB .shape ) - 2
388+ BMatrixOffsets = tuple (cube .offset [:- 2 ][- batchDimCount :]) + BMatrixOffsets
389+ BMatrixShape = tuple (cube .dims [:- 2 ][- batchDimCount :]) + BMatrixShape
390+
391+ BCube = HyperRectangle (BMatrixOffsets , BMatrixShape )
435392
436393 CMatrixOffsets = (MOffset , OOffset )
437394 CMatrixShape = (MSize , OSize )
438395
439- if len (buffC .shape ) == 2 :
440- CCube = HyperRectangle ( CMatrixOffsets , CMatrixShape )
441- elif len ( buffC . shape ) == 3 :
442- CCube = HyperRectangle (( BatchOffset , ) + CMatrixOffsets , ( BatchSize ,) + CMatrixShape )
443- else :
444- CCube = HyperRectangle (( BatchOffset , BOffset ) + CMatrixOffsets , ( BatchSize , BSize ) + CMatrixShape )
396+ if len (buffC .shape ) > 2 :
397+ batchDimCount = len ( buffC . shape ) - 2
398+ CMatrixOffsets = tuple ( cube . offset [: - 2 ][ - batchDimCount :]) + CMatrixOffsets
399+ CMatrixShape = tuple ( cube . dims [: - 2 ][ - batchDimCount :] ) + CMatrixShape
400+
401+ CCube = HyperRectangle (CMatrixOffsets , CMatrixShape )
445402
446403 inputACubes .append (ACube )
447404 inputBCubes .append (BCube )
0 commit comments