Skip to content

Conversation

@Lunderberg
Copy link
Contributor

Prior to this commit, the LambdaLift pass named each function as "lifted_func_" + i, in incremental order of occurrence. This provided unique names for each function, but could be difficult to read, or to refer to the lifted functions. This commit updates the naming scheme to use the location at which the lifted lambda occurs to generate a unique name for the new GlobalVar.

Prior to this commit, the `LambdaLift` pass named each function
as `"lifted_func_" + i`, in incremental order of occurrence.  This
provided unique names for each function, but could be difficult to
read, or to refer to the lifted functions.  This commit updates the
naming scheme to use the location at which the lifted lambda occurs to
generate a unique name for the new `GlobalVar`.
std::unordered_set<String> unavailable_names = previous_global_vars_;

// A helper function to check de-duplicate names
auto use_if_unique = [&](const auto& generate_proposed_name) {
Copy link
Contributor

@slyubomirsky slyubomirsky Jan 9, 2024

Choose a reason for hiding this comment

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

This might be a bit of a nitpick but I don't think use_if_unique is necessarily a fully descriptive name. What is important to convey is that it takes a scheme for generating names and applies it to all remaining lambdas. It had me scratching my head until I looked through the implementation. Not strictly necessary to change but it might be easier to understand at a glance. Possible alternative: attempt_generate_names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call on the naming. I've updated the name of use_if_unique to attempt_name_generation, the argument from generate_proposed_name to proposed_name_generation_func, and added a comment describing the intended arguments and return value for proposed_name_generation_func.

Copy link
Contributor

@slyubomirsky slyubomirsky left a comment

Choose a reason for hiding this comment

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

Very comprehensive change and it should lead to more human-readable code. Thanks!

@Lunderberg
Copy link
Contributor Author

Very comprehensive change and it should lead to more human-readable code. Thanks!

Thank you! Because I tend to run into errors late in a lowering pipeline, having the names preserved throughout the lowering pipeline, whenever possible, helps out debugging quite a bit.

if (Optional<String> opt_proposed_name = proposed_name_generation_func(func, location)) {
auto proposed_name = opt_proposed_name.value();

if (unavailable_names.count(proposed_name)) {
Copy link
Member

Choose a reason for hiding this comment

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

the body of the if statement is empty, probably we can merge it with else if/ else

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I went back and forth on it. It could be merged with the else if/else, but that would require repeating (and re-evaluating) the condition in the other two branches. It could be pulled out as if(!unavailable_names.count(proposed_name)) to wrap around the other two cases, but this utility is already getting a bit deeply nested for readability.


@R.function(private=True)
def lifted_func_1(
def glob_func_2_inner(
Copy link
Member

Choose a reason for hiding this comment

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

how about adding a test case to cover the dedup of possible duplicate lifted names like:

@ir_module
class Before:
    @R.function
    def foo_foo(x1: R.Tensor):
        @R.function
        def foo(x2: R.Tensor):
            ...

    @R.function
    def foo(x1: R.Tensor):
        @R.function
        def foo_foo(x2: R.Tensor):
            ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, and added.

Copy link
Member

@yongwww yongwww left a comment

Choose a reason for hiding this comment

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

thanks for the effort!

@yongwww yongwww merged commit 298ad2c into apache:unity Jan 9, 2024
@Lunderberg Lunderberg deleted the unity_preserve_name_in_lambda_lift branch January 9, 2024 23:57
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Feb 16, 2024
The `relax.transform.LiftTransformParams` pass splits apart a relax
function, extracting the steps that could be performed at
compile-time.  Prior to this commit, the transformed parameters were
named `param0`, `param1`, and so on.

This commit updates the `LiftTransformParams` pass to preserve any
human-readable parameter names.  The parameter names for the updated
function are taken from the original parameter names, if no
transformation is performed, or from the internal variable binding, if
a transformation is applied.  This implementation uses `LambdaLift`
internally, relying on the changes made in
apache#16306.
Lunderberg added a commit that referenced this pull request Feb 23, 2024
* [Relax][Transform] Preserve param names in LiftTransformParams

The `relax.transform.LiftTransformParams` pass splits apart a relax
function, extracting the steps that could be performed at
compile-time.  Prior to this commit, the transformed parameters were
named `param0`, `param1`, and so on.

This commit updates the `LiftTransformParams` pass to preserve any
human-readable parameter names.  The parameter names for the updated
function are taken from the original parameter names, if no
transformation is performed, or from the internal variable binding, if
a transformation is applied.  This implementation uses `LambdaLift`
internally, relying on the changes made in
#16306.

* Update based on review comments
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.

3 participants