Skip to content

[Unity] Mark result of LazyTransformParams as impure function#15697

Merged
slyubomirsky merged 2 commits intoapache:unityfrom
Lunderberg:unity_impure_lazy_transform_params
Sep 20, 2023
Merged

[Unity] Mark result of LazyTransformParams as impure function#15697
slyubomirsky merged 2 commits intoapache:unityfrom
Lunderberg:unity_impure_lazy_transform_params

Conversation

@Lunderberg
Copy link
Copy Markdown
Contributor

@Lunderberg Lunderberg commented Sep 7, 2023

The function produced by LazyTransformParams has only side effects, through the "get_item" and "set_item" PackedFunc calls, and should be marked as impure.

@R.function
@R.function(pure=False)
def main_transform_params(slice_shape_expr: R.Shape(["slice_index"])):
# we expect ToNonDataflow and RemovePurityTracking to be invoked first
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does LazyTransformParams fall in the phase ordering? Do we expect it to be used early or late in compilation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it isn't present in the lowering flow, and is explicitly called by an end-user. (Based on grep for "LazyTransformParams" across the TVM repo.) While the comment suggests that it was intended to be used in a later stage, so far this seems like just an extra requirement for an end-user to perform, before calling a function intended for an end-user's convenience (e.g. here). By outputting the correct purity annotations, we avoid this requirement.

That said, looking at it again, for correctness, any DataflowBlock in which the mutation occurs should be broken apart into a regular BindingBlock.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, if it's not specifically meant for later stages in compilation, then this change is definitely appropriate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What side effects would get_item have? I can't find where it's defined. Seems a little unusual for a read to be impure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't be impure due to side effects, but impure due to failure to return the same result for each execution. Because the model weights are no longer expressed as input arguments, they rely on external state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's interesting. I would still call it pure since it's not modifying any state (as distinct from, say, a call to a random number generator that modifies the internal state of the generator), but the dependency on external state is a potential bit of trickiness.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. The analogy I'd see would be calls to a get_current_time() function. Calling the function doesn't modify any of the internal state of the system, but still wouldn't be a pure function due to the dependency on external state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a good point. Most definitions of purity consider a function referring to external state not to be pure, so I think you're right. I should emphasize this in the specification, since I had neglected it myself.

@slyubomirsky slyubomirsky merged commit 0fe3ed4 into apache:unity Sep 20, 2023
@Lunderberg Lunderberg deleted the unity_impure_lazy_transform_params branch September 21, 2023 15:40
junrushao pushed a commit that referenced this pull request Sep 21, 2023
Mark the rest of test cases as impure. This is a follow up of #15697
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants