Skip to content

Optimize Dynamo Training Execute - #4523

Merged
JackCaoG merged 3 commits into
masterfrom
JackCaoG/dynamo_reduce_execute
Jan 30, 2023
Merged

JackCaoG merged 3 commits into
masterfrom
JackCaoG/dynamo_reduce_execute

Conversation

@JackCaoG

@JackCaoG JackCaoG commented Jan 27, 2023 •

Copy link
Copy Markdown
Collaborator

This will help #4475

Currently each dynamo training step(without optimizer) will result in 5 graph execution

They are

  1. sync_multi to sync input to fwd graph
  2. Execute the fwd graph
  3. sync_multi to sync input to bwd graph
  4. Execute bwd graph
  5. mark_step at the end of optimized_mod

My finding is that

  1. The first graph executation is uecessary. It is triggered by GetXlaData in

    if (auto xla_tensor_ptr =
    bridge::TryGetXlaTensor(ivalue.toTensor())) {
    dataptr = xla_tensor_ptr->GetXlaData();

    However if I check the HLO, it is actually a DeviceData IR(not sure why). We don't need to call applyPendingGraph to trigger execeution to retrive the XLAData. Hence my fix in GetXlaData

  2. The graph 3 and graph 5 are both triggered by aot_autograd. Instead of use xla_sync_multi to only sync the partial pending tensors and use mark_step to sync the rest, we can just do one mark_step in the beginning to combine 2 graphs. One thing to note is that we need to make first mark_step to be blocking.

With above 2 fixes, we can cut graph from 5 -> 3.

With the latest change, I am seeing a much more promising result
<style type="text/css"></style>

model Old Speed Up new Speed up
resnet50 0.758 0.937
resnet18 0.66 1.003
BERT_pytorch 1.441 1.869
resnext50_32x4d 0.87 1.139
alexnet 0.632 0.802
mobilenet_v2 0.549 0.672
mnasnet1_0 0.698 0.967
vgg16 0.712 0.742
timm_vision_transformer 1.275 1.69
squeezenet1_1 0.72 0.958
Avg 0.8315 1.0779
@JackCaoG

Copy link
Copy Markdown
Collaborator Author

I run the benchmark again using this pr and sadly find out the improvement is not as big as I would hope
image

The mark_step between forward and backward are very expensive, despite the actual device execution is very tiny. I will look into

  1. If there is a way to make this mark_step faster
  2. If there is a way to make this mark_step async

For the second part I actually had some idea. I might merge this pr as it is for now.

@JackCaoG

Copy link
Copy Markdown
Collaborator Author

<style type="text/css"></style>

model Old Speed Up New Speed Up  
resnet50 0.758 0.731  
resnet18 0.66 0.693  
BERT_pytorch 1.441 1.459  
resnext50_32x4d 0.87 0.845  
alexnet 0.632 0.747  
mobilenet_v2 0.549 0.546  
mnasnet1_0 0.698 0.674  
vgg16 0.712 0.731  
timm_vision_transformer 1.275 1.335  
squeezenet1_1 0.72 0.767  
Avg 0.8315 0.8528 2.56% better
# mark_step needs to be blocking since we want to access args's XLADatas
# and they can't be placeholder.
if any(torch_xla._XLAC._check_tensor_need_materialization(args)):
xm.mark_step(wait=True)

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.

In general, does a synchronous mark_step can be slower than _xla_sync_multi? Since the latter does not wait for the execution.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

_xla_sync_multi is also synchronous, but it might be executing a smaller graph.

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, I didn't realize that the default behavior of 'mark_step (not wait for compute)' and '_xla_sync_multi (wait for compute)' are different. Thanks for clarification!

@JackCaoG

Copy link
Copy Markdown
Collaborator Author

Oh interesting, if I remove the mark_step at the end of the optimized_mod, resnet50 speed up jump from
0.731 -> 0.928

I think what happened is that even mark_step at the end might be a no-op, it will still try to acquire the device lock. However device lock is hold by the async thread of torch_xla._XLAC._run_cached_graph which blocks the mark_step.

I think we should not unconditionally perform the mark_step at the end

@JackCaoG

Copy link
Copy Markdown
Collaborator Author

Oh interesting, if I remove the mark_step at the end of the optimized_mod, resnet50 speed up jump from 0.731 -> 0.928

I think what happened is that even mark_step at the end might be a no-op, it will still try to acquire the device lock. However device lock is hold by the async thread of torch_xla._XLAC._run_cached_graph which blocks the mark_step.

I think we should not unconditionally perform the mark_step at the end

This also explains why my previous pr to make execution async for dynam doesn't help, it is because mark_step will block on that async execution anyway.

@alanwaketan alanwaketan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

@JackCaoG

Copy link
Copy Markdown
Collaborator Author

With the latest change, I am seeing a much more promising result
<style type="text/css"></style>

model Old Speed Up new Speed up
resnet50 0.758 0.937
resnet18 0.66 1.003
BERT_pytorch 1.441 1.869
resnext50_32x4d 0.87 1.139
alexnet 0.632 0.802
mobilenet_v2 0.549 0.672
mnasnet1_0 0.698 0.967
vgg16 0.712 0.742
timm_vision_transformer 1.275 1.69
squeezenet1_1 0.72 0.958
Avg 0.8315 1.0779

I think there still are some unit test failing, need to investigate a bit

@JackCaoG

Copy link
Copy Markdown
Collaborator Author

Ok I think I fixed all of the failing tests, let's wait for CI to confirm

@JackCaoG
JackCaoG force-pushed the JackCaoG/dynamo_reduce_execute branch from c5724db to 6632e48 Compare January 28, 2023 00:29
@JackCaoG
JackCaoG merged commit f5494cd into master Jan 30, 2023
ManfeiBai pushed a commit that referenced this pull request Jan 30, 2023
* Optimize Dynamo Training Execute

* Check before doing mark_step for dynamo

* Fix failing tests
mateuszlewko pushed a commit that referenced this pull request Mar 15, 2023
* Optimize Dynamo Training Execute

* Check before doing mark_step for dynamo

* Fix failing tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3 participants