-
Notifications
You must be signed in to change notification settings - Fork 76
Swizzle for ring-based overlap #5889
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -806,14 +806,36 @@ void HostIrEvaluator::handle(ShardByStream* shard) { | |||||||||||||||||||||||||||||
| IterDomain* stream_id = *i; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| auto in_tensor = getKnownConcreteValue(shard->in()).as<at::Tensor>(); | ||||||||||||||||||||||||||||||
| auto stream_index = | ||||||||||||||||||||||||||||||
| expr_evaluator_.evaluate(shard->stream_index()).as<int64_t>(); | ||||||||||||||||||||||||||||||
| auto index = expr_evaluator_.evaluate(shard->stream_index()).as<int64_t>(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (stream_id->definition() != nullptr) { | ||||||||||||||||||||||||||||||
| // If the stream axis is defined by a swizzle, the input to | ||||||||||||||||||||||||||||||
| // the swizzle is the index into the `in_tensor`. | ||||||||||||||||||||||||||||||
| // Currently, we use cyclic shift swizzle to compute the index: | ||||||||||||||||||||||||||||||
| // in_index = (out_index (stream index) + device_id) % num_devices | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| NVF_CHECK(stream_id->definition()->isA<hir::Swizzle>()); | ||||||||||||||||||||||||||||||
| auto* swizzle = stream_id->definition()->as<hir::Swizzle>(); | ||||||||||||||||||||||||||||||
| ParallelType pt = swizzle->pt(); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| auto mesh = out_tv->getDeviceMesh(); | ||||||||||||||||||||||||||||||
| // Find the index of the current device in the slice of mesh corresponding | ||||||||||||||||||||||||||||||
| // to the parallel type | ||||||||||||||||||||||||||||||
| auto team_size = mesh.size(pt); | ||||||||||||||||||||||||||||||
| at::Tensor md_index = | ||||||||||||||||||||||||||||||
| mesh.multiDimensionalIndexOf(communicator_->deviceId()); | ||||||||||||||||||||||||||||||
| auto pt_axis = mesh.parallelTypeToAxis(pt); | ||||||||||||||||||||||||||||||
| int64_t team_index = md_index[pt_axis].item<int64_t>(); | ||||||||||||||||||||||||||||||
| index = (index + team_index) % team_size; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| at::Tensor out_tensor = | ||||||||||||||||||||||||||||||
| in_tensor | ||||||||||||||||||||||||||||||
| .chunk( | ||||||||||||||||||||||||||||||
| stream_id->extent()->evaluate().as<int64_t>(), | ||||||||||||||||||||||||||||||
| index, | ||||||||||||||||||||||||||||||
| getShardedLogicalAxis(out_tv, ParallelType::Stream)) | ||||||||||||||||||||||||||||||
| .at(stream_index); | ||||||||||||||||||||||||||||||
| .at(index); | ||||||||||||||||||||||||||||||
|
Comment on lines
832
to
+838
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. incorrect number of arguments passed to
Suggested change
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| expr_evaluator_.bind(out_tv, out_tensor); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -569,4 +569,40 @@ class ForLoop : public Expr { | |||||
| } | ||||||
| }; | ||||||
|
|
||||||
| class Swizzle : public Expr { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This should be moved to |
||||||
| public: | ||||||
| using Expr::Expr; | ||||||
|
|
||||||
| Swizzle( | ||||||
| IrBuilderPasskey passkey, | ||||||
| IterDomain* in, | ||||||
| IterDomain* out, | ||||||
| ParallelType pt); | ||||||
|
|
||||||
| Swizzle(const Swizzle& other) = delete; | ||||||
| Swizzle& operator=(const Swizzle& other) = delete; | ||||||
| Swizzle(Swizzle&& other) = delete; | ||||||
| Swizzle& operator=(Swizzle&& other) = delete; | ||||||
|
|
||||||
| NVFUSER_DECLARE_CLONE_AND_CREATE | ||||||
|
|
||||||
| std::string toString(int indent_size = 0) const override; | ||||||
| std::string toInlineString(int indent_size = 0) const override; | ||||||
| const char* getOpString() const override { | ||||||
| return "hir::Swizzle"; | ||||||
| } | ||||||
|
|
||||||
| IterDomain* in() const { | ||||||
| return inputs().at(0)->as<IterDomain>(); | ||||||
| } | ||||||
|
|
||||||
| IterDomain* out() const { | ||||||
| return outputs().at(0)->as<IterDomain>(); | ||||||
| } | ||||||
|
|
||||||
| ParallelType pt() const { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return attribute<ParallelType>(0); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| } // namespace nvfuser::hir | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,21 @@ | |
|
|
||
| namespace nvfuser::hir { | ||
|
|
||
| TensorView* swizzle(TensorView* in, int64_t axis, ParallelType pt) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TensorView::swizzle1d |
||
| NVF_ERROR(in != nullptr); | ||
|
|
||
| IterDomain* swizzle_in = in->axis(axis); | ||
| IterDomain* swizzle_out = IterDomainBuilder(swizzle_in).build(); | ||
| IrBuilder::create<Swizzle>(swizzle_in, swizzle_out, pt); | ||
|
|
||
| std::vector<IterDomain*> loop_domain = in->getLoopDomain(); | ||
| loop_domain.erase(loop_domain.begin() + axis); | ||
| loop_domain.insert(loop_domain.begin() + axis, swizzle_out); | ||
| in->setLoopDomain(loop_domain); | ||
|
|
||
| return in; | ||
| } | ||
|
|
||
| TensorView* shardByStream(TensorView* source, Val* stream_index, Expr* e) { | ||
| NVF_ERROR( | ||
| getShardedIterDomain( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: consider putting + and % to the HostIrContainer in the future. See also
Fuser/csrc/swizzle.h
Lines 26 to 52 in 77abd29