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

[Ansor][AutoTVM v2.0] Phase 1: Add annotation/compute_at/compute_root/compute_inline steps #6073

Merged
merged 15 commits into from
Jul 21, 2020

Conversation

jcf94
Copy link
Contributor

@jcf94 jcf94 commented Jul 16, 2020

For full upstream plan, see Ansor RFC.

In this PR, we bring annotation/compute_at/compute_root/compute_inline steps for Ansor auto_scheduler.

To record the stage/iterator attach relations, we introduced a new structure AttachMap in State.

cc @tqchen @merrymercy @FrozenGene @minminsun @comaniac

@junrushao
Copy link
Member

Just noticed that #6077 is also titled as "part 1", shall we make one of them part 2 instead?

python/tvm/auto_scheduler/loop_state.py Outdated Show resolved Hide resolved
@@ -169,6 +206,18 @@ struct Handler<::tvm::Array<::tvm::auto_scheduler::Step>> {
fused_ids.push_back(i);
}
data->push_back(::tvm::auto_scheduler::FuseStep(stage_id, fused_ids));
} else if (name == "AN") {
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel the current approach of (de)serializing records is not scalable and hard to be maintained. Specifically, we put all deserialization rules to Read and all serialization rules to Write. The key to connect the serialization logic to the corresponding deserialization logic is a two character short name (e.g., AN). It seems to me that it would be better to define a short name and (de)serialization logic in each step. In this case, we can use ps->serialize(writer) for serialization, and build a step factory to deserialize them.

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 point.
@merrymercy Currently to add a new transform steps, we have to modify 7 files ... Maybe it would better to move all specific implementation to transform_step.h/cc, and left the other file to just add some interface.

Copy link
Member

Choose a reason for hiding this comment

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

I agree with your points.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm working on this.

src/auto_scheduler/loop_state.h Outdated Show resolved Hide resolved
src/auto_scheduler/loop_state.cc Outdated Show resolved Hide resolved
src/auto_scheduler/loop_state.cc Outdated Show resolved Hide resolved
src/auto_scheduler/loop_state.cc Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
@comaniac
Copy link
Contributor

comaniac commented Jul 17, 2020

Just noticed that #6077 is also titled as "part 1", shall we make one of them part 2 instead?

Part 1 will have several PRs. "Phase 1" might be a better word to describe this process, but anyways.

@jcf94
Copy link
Contributor Author

jcf94 commented Jul 17, 2020

Just noticed that #6077 is also titled as "part 1", shall we make one of them part 2 instead?

Part 1 will have several PRs. "Phase 1" might be a better word to describe this process, but anyways.

@junrushao1994 My bad, yes I was intended to express a meaning of stage/phase...
@comaniac Let's just use phase, I'll rename these PRs.

@jcf94 jcf94 changed the title [Ansor][AutoTVM v2.0] Part 1: Add annotation/compute_at/compute_root/compute_inline steps [Ansor][AutoTVM v2.0] Phase 1: Add annotation/compute_at/compute_root/compute_inline steps Jul 17, 2020
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
src/auto_scheduler/transform_step.cc Outdated Show resolved Hide resolved
src/auto_scheduler/transform_step.cc Show resolved Hide resolved
src/auto_scheduler/transform_step.cc Outdated Show resolved Hide resolved
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

Thanks @jcf94! Overall it looks good. Just some nitpicks.

src/auto_scheduler/utils.h Outdated Show resolved Hide resolved
src/auto_scheduler/loop_state.cc Outdated Show resolved Hide resolved
src/auto_scheduler/loop_state.h Outdated Show resolved Hide resolved
python/tvm/auto_scheduler/loop_state.py Outdated Show resolved Hide resolved
python/tvm/auto_scheduler/loop_state.py Outdated Show resolved Hide resolved
src/auto_scheduler/compute_dag.cc Outdated Show resolved Hide resolved
@@ -169,6 +206,18 @@ struct Handler<::tvm::Array<::tvm::auto_scheduler::Step>> {
fused_ids.push_back(i);
}
data->push_back(::tvm::auto_scheduler::FuseStep(stage_id, fused_ids));
} else if (name == "AN") {
Copy link
Member

Choose a reason for hiding this comment

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

I agree with your points.

tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

Overall LGTM. What's the plan of fixing the step serialization issue? Will that be done in this PR?

src/auto_scheduler/loop_state.cc Outdated Show resolved Hide resolved
tests/python/unittest/test_auto_scheduler_loop_state.py Outdated Show resolved Hide resolved
@junrushao
Copy link
Member

A minor typo we didn't catch before (github doesn't allow me to comment on that line because it is not related to this change): in file "loop_state.h", document for class StageNode, "Similar to te::Stage in `include/schedule.h`", the path is incorrect.

@jcf94 jcf94 requested review from merrymercy and FrozenGene July 20, 2020 03:49
@jcf94
Copy link
Contributor Author

jcf94 commented Jul 20, 2020

Moved all the implementation to transform_step.h/cc, and updated the order of steps.
In the current implementation, the next pr of steps adding will only need to change transform_step.h/cc two files.
cc @merrymercy @junrushao1994 @comaniac @FrozenGene
... I'm not sure why I failed to re-request review of @junrushao1994 and @comaniac

} else {
LOG(FATAL) << "Invalid step: " << step;
}
StepApplyToState(step, this, dag);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Looks like we can remove StepApplyToState and simply use step->ApplyToState(this)?
  • dag seems unused in both this function and StepApplyToState.

Copy link
Member

Choose a reason for hiding this comment

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

  1. We can remove StepApplyToState
  2. dag will be used in other steps such as cache_write and rfactor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The step->ApplyToState() is actually not a virtual function(the return value of different steps may be different), so I add the StepApplyToState to pack all those step type check to transform_steps.cc.

if (i != after_ids.size() - 1) {
ss << ", ";
}
void StepApplyToSchedule(const Step& step, Array<te::Stage>* stages,
Copy link
Contributor

Choose a reason for hiding this comment

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

For 3 functions in this file (StepApplyToState, StepApplyToSchedule, and StepApplyToPythonAPI), it should be fine and clearer to directly call step->XXX(...);incompute_dag.cc` so that we can avoid unnecessary step node dispatching.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Emm, step->ApplyToState, step->ApplyToSchedule & step->PrintAsPythonAPI all have problems to be written to virtual functions, we can later try to see if there's any better way to deal with this.

node->stage_id = stage_id;
for (const auto& x : after_ids) {
CHECK(x->IsInstance<IntImmNode>());
Step StepReadFromRecord(dmlc::JSONReader* reader) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to have a step registration mechanism to establish a step lookup table from record prefix to the step node class, so that we can totally hide step node dispatching from developers. The potential issues in the current implementation is that 1) people may forget to update this function when adding a new step, and 2) there's no mechanism to check duplicated record prefix of two steps.

In addition, I feel this issue is important but I'm not sure if it's proper to address it in this PR. We may need versioning for records.

Copy link
Member

@merrymercy merrymercy Jul 21, 2020

Choose a reason for hiding this comment

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

  1. Yes, we can add a registration mechanism. But I think it can be done in future PRs.
  2. The current records do have a version field
    https://github.com/apache/incubator-tvm/blob/7ac78d96611ab3a069fae9eb5210e433eb240cc5/src/auto_scheduler/measure_record.cc#L314

} else {
LOG(FATAL) << "Invalid step: " << step;
}
StepApplyToState(step, this, dag);
Copy link
Member

Choose a reason for hiding this comment

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

  1. We can remove StepApplyToState
  2. dag will be used in other steps such as cache_write and rfactor.

src/auto_scheduler/measure_record.cc Outdated Show resolved Hide resolved
Copy link
Member

@merrymercy merrymercy left a comment

Choose a reason for hiding this comment

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

Overall the re-organization looks good to me. We can merge it after fixing the several remaining comments from Cody and me.

Copy link
Member

@FrozenGene FrozenGene left a comment

Choose a reason for hiding this comment

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

Overall LGTM

src/auto_scheduler/compute_dag.cc Show resolved Hide resolved
src/auto_scheduler/compute_dag.cc Show resolved Hide resolved
@jcf94 jcf94 requested a review from merrymercy July 21, 2020 08:09
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the contribution!

Copy link
Contributor

@comaniac comaniac left a comment

Choose a reason for hiding this comment

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

Given that the issue of 3 dispatching functions will be addressed later, this PR LGTM. Thanks for the efforts!.

@merrymercy merrymercy merged commit 71533a5 into apache:master Jul 21, 2020
@jcf94
Copy link
Contributor Author

jcf94 commented Jul 22, 2020

Given that the issue of 3 dispatching functions will be addressed later, this PR LGTM. Thanks for the efforts!.

Thanks, updated our merge plan to track this issue: https://github.com/merrymercy/Ansor/issues/65

@jcf94 jcf94 deleted the compute_at_annotation branch July 29, 2020 07:29
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…/compute_inline steps (apache#6073)

* Add annotation step

* Add compute_at/compute_root/compute_inline

* Doc update

* Update

* Update

* Update measure record UT

* Update

* Update

* Update

* Move state implementation to step

* Move measure_record implementation to step

* Order update & API update

* Update the order of state api

* Update
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Aug 26, 2020
…/compute_inline steps (apache#6073)

* Add annotation step

* Add compute_at/compute_root/compute_inline

* Doc update

* Update

* Update

* Update measure record UT

* Update

* Update

* Update

* Move state implementation to step

* Move measure_record implementation to step

* Order update & API update

* Update the order of state api

* Update
trevor-m pushed a commit to trevor-m/tvm that referenced this pull request Sep 2, 2020
…/compute_inline steps (apache#6073)

* Add annotation step

* Add compute_at/compute_root/compute_inline

* Doc update

* Update

* Update

* Update measure record UT

* Update

* Update

* Update

* Move state implementation to step

* Move measure_record implementation to step

* Order update & API update

* Update the order of state api

* Update
trevor-m pushed a commit to neo-ai/tvm that referenced this pull request Sep 3, 2020
…/compute_inline steps (apache#6073)

* Add annotation step

* Add compute_at/compute_root/compute_inline

* Doc update

* Update

* Update

* Update measure record UT

* Update

* Update

* Update

* Move state implementation to step

* Move measure_record implementation to step

* Order update & API update

* Update the order of state api

* Update
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants