Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Buffer flatten #340

Merged

Conversation

junrushao
Copy link
Member

@junrushao junrushao commented Mar 11, 2021

No description provided.

@tqchen
Copy link
Contributor

tqchen commented Mar 14, 2021

Thanks @junrushao1994 One thing that I noticed is the change of unordered map from strong ref object to weak ref(raw ptr). We need to be careful with such a change, especially in cases where an IR object can be mutated.

we need to be careful when using raw ptr in a mutator. Since the children can get de-allocated and re-allocated to another node (causing map info to be inaccurate). Prefer the strong ref version. See a related PR here apache/tvm#6004

Because of that reason, I think we could actually prefer to use strong ref version for unordered map (with ObjectPtrHash and Equal) for most of the maps of interest. The overhead of ref counting is not as large comparing to the gains. For a temporary data structure within a Visitor(that does not change the IR), we should be fine.

@junrushao
Copy link
Member Author

@tqchen It certainly makes sense to avoid deallocation that causes hash map key to be invalid in StmtExprMutators. I wonder in which case it could happen? If we turn off the copy-on-write, I assume it would be valid during visiting. Is that correct?

@tqchen
Copy link
Contributor

tqchen commented Mar 14, 2021

in some cases when we visit, say we want to replace a subtree A=> B, then after the replacement of the subtree A could no longer be referenced anywhere(thus de-allocated)

If the same address of A get re-allocated to another node during the visit, then the information in the mutator becomes stale. I cannot tell exactly which case it happens, but it indeed casued some bugs before, thus the PR from qualcomm

@junrushao
Copy link
Member Author

If copy-on-write is disabled, then I assume the original IR will keep valid during the visit, which means deallocation of A will be deferred until the visit is finished. Is that correct?

@tqchen
Copy link
Contributor

tqchen commented Mar 14, 2021

You are right. In our case there is possibility of COW, so we need to watch out for that

@junrushao
Copy link
Member Author

Got it. I am going to change those data structure outlive the mutator to hold strong references

@junrushao
Copy link
Member Author

A second thought leads me to changing every reference in the mutator to strong ones, in case helping cow

@junrushao
Copy link
Member Author

junrushao commented Mar 16, 2021

@Hzfengsy This PR is basically done, please take a look when you have time :-)

There are two TODOs in the codebase:

  1. Line 445. It checks specifically if the given loop is bound to threadIdx or vthread, as well as if the buffer is allocated in shared memory, which is a bit foggy to me because it couples storage scope and threads. What if we have a special storage scope like "shared.shuffled" used in cutlass?
          if (need_relax || (buffer->scope == "shared" && IsThreadBound(loop))) {
            // TODO
            dom_map[loop_var] = IntSetFromMinExtent(loop->min, loop->extent);
          }
  1. Line 509: To pass this test (https://github.com/Hzfengsy/tvm-tensorir/blob/main/tests/python/tir/test_schedule_tensorize.py#L339), I changed ICHECK_EQ to ICHECK_GE, where the tensorize primitive seems to change the dimension of buffer access. I discussed with @spectrometerHBH a moment ago and he will take care of it.

Additionally, I handled double_buffer in a way different from the original codebase, please take a close look whether it makes sense. CC: @vinx13.

Thank a lot!

}

static bool IsDoubleBufferScope(const Map<String, ObjectRef>& annotations) {
for (const auto& kv : annotations) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

use Map::Get

for (const auto& kv : annotations) {
const String& ann_key = kv.first;
const ObjectRef& ann_value = kv.second;
if (ann_key != attr::double_buffer_scope) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

should be ==?

Copy link
Member Author

Choose a reason for hiding this comment

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

ooops

@tqchen tqchen merged commit b4e545d into tlc-pack:main Mar 18, 2021
Hzfengsy pushed a commit that referenced this pull request Mar 29, 2021
Hzfengsy pushed a commit that referenced this pull request May 24, 2021
Hzfengsy pushed a commit that referenced this pull request Jul 5, 2021
jinhongyii pushed a commit that referenced this pull request Jul 29, 2021
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.

4 participants