Layout Gymnastics
Introduction
Colfax recently released an excellent Paper on CuTe
Layouts. In this note I calculated many of the examples of Chapter 2 by hand. I hope this can can be a small complement to the original paper to show the various formulas in action and give a step by step solution to many examples for even better accessibility. Please refer to the original Paper for more the whole theory as the selection I made is obviously not complete.
Learning Notes
- Modes with are redundant because they are the map and therefore don't contain any information
- obey . Note that and therefore and with it follows that from where we can conclude that we can replace the two modes by one mode .
- , last two modes
- , last two modes
- , first two modes
- , squeeze
- last two modes
- If we would have that does not divide we'd have a "jump" within the Layout which could not be filled up by a regular placement in the slots between.
- divides and divides , therefore is complementable.
is not complementable because and does not divide .
- is a Layout that fills out the holes between the modes of the Layout.
Note that is compact which means that is an isomorphism (see 2.1.5.1). That in return leads to the complement being the empty Layout because there are no "holes to fill". This is also clear from 2.1.6.9 because we define the complement to be precisely that Layout that gives us a compact Layout when concatenated with the Layout it complements.
If where is the complement to we say that is an complement of .
- divides , so is complementable.
,
An example:
Note that we simply multiply the strides with the stride of .
Consider the Layout . The strides of means that for all . If we compose such a Layout with Layout than we have because all elements will get mapped to and will get mapped by any Layout function to .
Consider composition with the Layout . Compose with Layout . We have that . Let with than . Let than . Similar and (where in the last case of course we have that because that is the cosize of . Thus we have that is the identity on the image of . That would not necessarily be the case if the strides of would be different.
Consider the composition and than we have
We see that there is no flat layout than can have such a Layout function so we can't compose arbitrary Layouts.
For example:
This leads to .
Congruence means that the profiles of two nested tuples agree. is compatible with but not compatible with .
For example let and than we have . A special case is which is just the concatenation of .
Note that is a refinement of because we have . For we have that the is equal to and each of the modes is a refinement of the corresponding other modes, i.e. is a refinement of because etc.
Note that is a Layout but is not. That is because in the second example has a different profile (namely ) than (namely ).
For example has:
The flattening of a Layout flattens the nested tuple into ordinary tuple, i.e. . For the above example we have .
The Layout function is given by .
Let's consider , than we have which gives us
Not associative, because for example , , . We have because they have different profiles. However the Layout function is the same because when flattened they are equal and the Layout function of nested tuples is defined by the flattened Layout.
The substitution won't affect the Layout function, however it will change the interpretation. To see that consider:
which is a cube. However is a plane!
L'
0 1
(0, 0) 0 4
(1, 0) 1 5
(0, 1) 2 6
(1, 1) 3 7
:
- combine last two modes
- combine second and third mode
- combine first two modes
Note that this is intuitively clear.
,
Note that corresponds to , corresponds to and corresponds to . From here we get the above relative modes.
That makes it easy to determine compactness of hierarchical Layouts, for example is compact because is column major and therefore compact.
, . Let us calculate the N complement for :
- . We see that divides for and divides . Therefore we can calculate the complement.
and
- . We see that divides for and divides
- will remove the redundant first modes
- Therefore .
and
is given by for We have that
- We can identify that such
(0,0) 0
(1,0) 6
(0,1) 1
(1,1) 7
which is . Obviously refines because . Furthermore the Layout is coalesced because is clearly coalesced. Note we could have not expressed this via flat layouts.
This gives a condition on composability.
Intuition: is the complement of . Note that this means that "hits" the holes has in . Therefore in the second mode will give us an index in the corresponding subtile of . The motivated reader may derive the above Layout "by hand" using the calculation techniques we used above.
Conclusion
I hope this can serve as a complementary guide to the original chapter 2 by making many of the examples explicit and giving step by step solution. I am exited about the new way of looking at Layout algebra offered in chapter 3 and 4 and will hopefully publish further posts on this topic in the future. Please consider starring the Github repo that comes with the Colfax paper.