Skip to content

Fix broadcast InferCorrectLayout#10156

Merged
masahi merged 10 commits into
apache:mainfrom
lazycal:fix-bcast-infer-layout1
Feb 5, 2022
Merged

Fix broadcast InferCorrectLayout#10156
masahi merged 10 commits into
apache:mainfrom
lazycal:fix-bcast-infer-layout1

Conversation

@lazycal

@lazycal lazycal commented Feb 3, 2022

Copy link
Copy Markdown
Contributor

Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from Reviewers by @ them in the pull request thread.

I'm reviewing this function after PR #10118, and found several buggy places. For instance,

  • it does not respect the original input tensor's layout but always reset it to the other input's layout (see test_broadcast_respect_input_layouts);
  • it assumes one can always only transform one layout to adapt to the other. However, this is not always possible, see test_broadcast_non_adaptable.
  • AdjustSubordinateFactors should also be called in the else branch
    AdjustSubordinateFactors(layouts[large_idx], old_small_layout, old_small_shape);
    layouts.Set(small_idx, new_small_layout);
    } else {
    // Support scenarios where original operands were of type [N, H, W, C] and [C]. In this case,
    // while transforming the layout, we expand dims to make C go to NHWC, and then use the
    // modified layout of the first operator to call the layout transform. E.g.
    // a in NCHWC16c
    // b in C
    // b = expand_dims(b) from C -> NHWC
    // b = layout_transform(b) from NHWC -> NCHW16c
    // add(a, b)
    layouts.Set(small_idx, ret);
    }
    . (see test_alter_layout_nonscalar_broadcast)

This PR rewrites BinaryBroadcastLayoutHelper a bit, and I tried to make it as backward compatible as possible.

Misc

BTW I seem to spot another bug when I was working on this PR. So I sometimes I need to create a scalar layout, and I used Layout(""), but it returned an undefined layout. From the doc it should return a scalar layout right? I checked the constructor, and it seems in this if statement:

if (name.empty()) return; // scalar
, it directly returns without setting the data_ field as in normal path:
data_ = std::move(node);
. I didn't fix this issue but work it around somehow, but it does seem like a bug to me :-).

@lazycal

lazycal commented Feb 3, 2022

Copy link
Copy Markdown
Contributor Author

@masahi

@masahi masahi self-assigned this Feb 3, 2022
Comment thread src/relay/transforms/infer_layout_utils.cc
Comment thread src/relay/transforms/infer_layout_utils.cc
Comment thread src/relay/transforms/infer_layout_utils.cc Outdated
Comment thread src/relay/transforms/infer_layout_utils.cc
@lazycal

lazycal commented Feb 4, 2022

Copy link
Copy Markdown
Contributor Author

@masahi The code is changed according to your suggestions.

@masahi masahi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't actually try to understand the code, but why not, ship it

@masahi masahi merged commit 63377f2 into apache:main Feb 5, 2022
ylc pushed a commit to ylc/tvm that referenced this pull request Feb 16, 2022
* Move function body to .cc file.

* fix broadcast infer layout

* add unittest

* backward-compat: optimize for scalar layout

* fix lint

* fix lint and warning

* Add newlines; Use std::vector

* fix lint

* jostle ci
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