STeP Stream Type #4
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supporting Generic Aliases as Type Variables
Issue
Previously in
base.py
, if a type contained a type variableT
in its definition, we would only allow an instance of a concrete type forT
. For example, if we had a typeStream[T]
, we could support types likeint
, but not types likeTuple[int]
.For the
Tuple[int]
case, we would enter thetyping.GenericAlias
case and callr_resolve(globalns, localns, rarg)
. Here, however,Tuple[int]
would be considered a type variable, falling under thetyping.TypeVar()
case. Then, we would raise an ArgonError, failing to resolveTuple[int]
.Solution
We simply change the if statement here to just check if
rarg.__name__
is in theglobalns
and thelocalns
. Then, as previously, we returnlocalns[rarg.__name__]
. This expands the definition for allowed types in the definition which is necessary for the STeP frontend, becauseStream
needs to take in tuples of integers and floats as well as buffers.Implementing Stream Type
Details
The Stream type
Stream[T, RK]
defines a stream of values of typeT
with tensor rankRK
. To denote tensor rank, a rank-RK stream consist of any number of rank-(RK-1) steams delimited byStop
tokens, which are values of typeint
.Here are some examples of streams:
Stream[int,R0]:
1
Stream[float,R1]:
1.0
,2.0
,3.0
,1
Stream[float,R2]:
1.0
,2.0
,1
,3.0
,4.0
,1
,5.0
,6.0
,2
Solution
We first define classes
Val
andStop
.Val
is a value with typeST
corresponding to one element of the stream of values, andStop
corresponds to a Stop token.We define the Stream type with the header
Stream[ST,SRK](Ref[List[Union[Val[ST], Stop]], "Stream[ST,SRK]"])
. We choose to represent streams astyping.List[Union[Val[ST], Stop]]
since streams are essentially lists of values and stops.Using Rank in Type Definition
Issue
We want the stream type to express its rank in its type definition, but we cannot use constants as a type variable. For example,
Stream[int,0]
is not allowed because0
is a constant and not a type variable.Solution
Our solution is to create a type for each rank. However, we cannot just dynamically create types on the fly, since Python does not recognize dynamically created types as the same.
Thus, we utilize the class as a static variable, and store a dictionary that updates whenever a new type is created. The
RankGen
class has a methodget_rank
that creates a new type or looks up the type in the dictionary, and then returns the type. This implementation bypasses the dynamic type issue since it allows types to be shared across instances.