Open
Conversation
4aea621 to
c233f38
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 variableTin 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.GenericAliascase 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 theglobalnsand 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, becauseStreamneeds 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 typeTwith tensor rankRK. To denote tensor rank, a rank-RK stream consist of any number of rank-(RK-1) steams delimited byStoptokens, which are values of typeint.Here are some examples of streams:
Stream[int,R0]:1Stream[float,R1]:1.0,2.0,3.0,1Stream[float,R2]:1.0,2.0,1,3.0,4.0,1,5.0,6.0,2Solution
We first define classes
ValandStop.Valis a value with typeSTcorresponding to one element of the stream of values, andStopcorresponds 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 because0is 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
RankGenclass has a methodget_rankthat 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.