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

[Semi-Auto] LayerNorm Parallel Rule #55130

Merged
merged 3 commits into from
Jul 6, 2023

Conversation

zhiqiu
Copy link
Contributor

@zhiqiu zhiqiu commented Jul 4, 2023

PR types

New features

PR changes

OPs

Description

Pcard-70448

add spmd rule for layer_norm

@paddle-bot
Copy link

paddle-bot bot commented Jul 4, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhiqiu zhiqiu requested a review from JZ-LIANG July 5, 2023 06:55
std::pair<std::vector<TensorDistAttr>, std::vector<TensorDistAttr>>
LayerNormSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
const paddle::framework::AttributeMap& attrs) {
// step0: verify input args based on matmul logic
Copy link
Contributor

Choose a reason for hiding this comment

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

layer_norm

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}
std::string out_axes = x_axes;

VLOG(4) << "LayerNormSPMDRule build Einsum notation (x,scale,bias->out): ["
Copy link
Contributor

Choose a reason for hiding this comment

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

should include all layer_norm outputs (x,scale,bias->out, mean, var)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thx

input_dist_attrs.emplace_back(ReplicatedOnMesh(input_specs[2].dist_attr()));

// Step2.4. handle input and out tensor partial
std::vector<int64_t> partial_on_dims;
Copy link
Contributor

Choose a reason for hiding this comment

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

LayerNorm activation output would not be partial

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, thx

<< "]; out dims_mapping: [" << str_join(out_dims_mapping)
<< "], partial_on_dims: [" << str_join(partial_on_dims) << "]";

return {input_dist_attrs, {output_dist_attr_dst}};
Copy link
Contributor

Choose a reason for hiding this comment

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

should infer the distattr of output (variance and mean)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done thx

namespace distributed {
namespace auto_parallel {

TensorDistAttr GetInferedDistAttr(
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Contributor

@JZ-LIANG JZ-LIANG left a comment

Choose a reason for hiding this comment

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

LGTM


SPMDRuleBase* layer_norm_rule = SPMDRuleMap::Instance().Get("layer_norm");

// ijk[1, -1, -1],k[-1],k[-1] --> ijk[1, -1, -1] partial[1]
Copy link
Contributor

Choose a reason for hiding this comment

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

no partial

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 will fix it in the next pr.

VLOG(4) << "test1 done.";

// ijk[1, 0, -1],k[0],k[0] --> ijk[1, 0, -1]
x_dist_tensor_spec.set_dims_mapping({1, 0, -1});
Copy link
Contributor

@JZ-LIANG JZ-LIANG Jul 6, 2023

Choose a reason for hiding this comment

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

GOOD test case !
there are two kind of error in the case:

  1. multiple batch axes (i,j) are sharded in input activation, which is not supported by now.
  2. same mesh dimension ( 0 ) is sharding two different tensor axes (j & k).

it inspires me that we are missing one important precondition checking in InferForward (when support sharding on bias in future):
when [ijkl,y(kl),y(kl)->ijkl,x(ij),x(ij) (x,scale,bias->out,mean,variance, begin_norm_axis=2, x=ij, y=kl)]
thought "y" and "kl" are represented by different char, but they are same tensor axis and should be sharded by same mesh dim.

attrs););
VLOG(4) << "test2 done.";

// ijk[0, -1, -1],z[-1],z[1] --> ijk[0, 1, -1, -1], z=jk
Copy link
Contributor

Choose a reason for hiding this comment

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

ijk[0, -1, -1], k[-1], k[1] --> ijk[0, -1, -1], x[0], x[0], x=ij

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 will fix it in the next pr.

@zhiqiu zhiqiu merged commit 4d1b9f0 into PaddlePaddle:develop Jul 6, 2023
cqulilujia pushed a commit to cqulilujia/Paddle that referenced this pull request Jul 24, 2023
* add layernorm spmd rule

* add ut

* follow 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.

2 participants