已开启
feat(npu): decouple owner push/ready into separate streams #1
feat(npu): decouple owner push/ready into separate streams #1
已开启
Jia_Austin创建于 10月20日
共 6 个文件变更+120-8
@@ -484,6 +484,7 @@ class Buffer:
484 self.comm_stream_priority = comm_stream_priority484 self.comm_stream_priority = comm_stream_priority
485 self.enable_pdl = enable_pdl485 self.enable_pdl = enable_pdl
486 self._comm_stream: torch_npu.npu.Stream | None = None486 self._comm_stream: torch_npu.npu.Stream | None = None
487+ self._owner_ready_stream: torch_npu.npu.Stream | None = None
487 self._destroyed = False488 self._destroyed = False
488 self._async_lane = None489 self._async_lane = None
489 self._async_sources = {}490 self._async_sources = {}
@@ -592,7 +593,8 @@ class Buffer:
592 def _owner_ready_capability():593 def _owner_ready_capability():
593 abi = getattr(_C, 'LPLUS_B_OWNER_READY_ABI', None)594 abi = getattr(_C, 'LPLUS_B_OWNER_READY_ABI', None)
594 if (type(abi) is not int or abi != 1 or595 if (type(abi) is not int or abi != 1 or
595- not callable(getattr(_C, '_lplusb_owner_push_ready', None)) or596+ not callable(getattr(_C, '_lplusb_owner_push_only', None)) or
597+ not callable(getattr(_C, '_lplusb_owner_wait_ready', None)) or
596 not callable(getattr(_C, '_lplusb_owner_ready_init', None))):598 not callable(getattr(_C, '_lplusb_owner_ready_init', None))):
597 raise RuntimeError('owner-ready prefetch requires a rebuilt native ABI v1')599 raise RuntimeError('owner-ready prefetch requires a rebuilt native ABI v1')
598 return dict(schema=1, mode='required_owners_v1', signal_shape_tail=16,600 return dict(schema=1, mode='required_owners_v1', signal_shape_tail=16,
@@ -728,6 +730,11 @@ class Buffer:
728 bank_capacities={role: spec.count for role, spec in capacity_plan.pools.items()},730 bank_capacities={role: spec.count for role, spec in capacity_plan.pools.items()},
729 expected_steps=steps,731 expected_steps=steps,
730 )732 )
733+ # Decoupled owner-ready lane: Q0 issues pushes back-to-back while
734+ # this stream spins on peer ready signals.
735+ owner_ready_stream = (torch_npu.npu.Stream(
736+ device=int(ctx['device']), priority=self.comm_stream_priority)
737+ if owner_ready else None)
731 except BaseException:738 except BaseException:
732 for tensor in reversed(allocated):739 for tensor in reversed(allocated):
733 tensor._vmm_allocation.destroy()740 tensor._vmm_allocation.destroy()
@@ -740,6 +747,7 @@ class Buffer:
740 self._async_footprint_bytes = sum(747 self._async_footprint_bytes = sum(
741 tensor._vmm_allocation.size for tensor in allocated)748 tensor._vmm_allocation.size for tensor in allocated)
742 self._async_lane = lane749 self._async_lane = lane
750+ self._owner_ready_stream = owner_ready_stream
743 self._async_manifest = manifest751 self._async_manifest = manifest
744 return digest752 return digest
745 753 
@@ -1080,9 +1088,22 @@ class Buffer:
1080 if ready is None:1088 if ready is None:
1081 _C._lplusb_owner_push(pointer_table, slot, experts_to_copy,1089 _C._lplusb_owner_push(pointer_table, slot, experts_to_copy,
1082 int(ctx['rank']), int(slot.shape[0]))1090 int(ctx['rank']), int(slot.shape[0]))
1083- else:1091+ return None
1084- _C._lplusb_owner_push_ready(pointer_table, slot, experts_to_copy, ready,1092+ # Decoupled owner-push: Q0 issues the push without an all-rank
1085- int(ctx['rank']), int(slot.shape[0]), ordinal + 1)1093+ # rendezvous (source readiness, slot reuse and peer visibility are
1094+ # each already ordered by AsyncLane's events and release
1095+ # collective), then a dedicated stream spins on the owner-ready
1096+ # signals so one slow owner never serializes later pushes on Q0.
1097+ _C._lplusb_owner_push_only(pointer_table, slot, experts_to_copy,
1098+ int(ctx['rank']), int(slot.shape[0]))
1099+ push_done = self._async_lane.q0.record_event()
1100+ ready_stream = self._owner_ready_stream
1101+ with torch_npu.npu.stream(ready_stream):
1102+ ready_stream.wait_event(push_done)
1103+ _C._lplusb_owner_wait_ready(ready, experts_to_copy,
1104+ int(ctx['rank']), int(slot.shape[0]),
1105+ ordinal + 1)
1106+ return ready_stream.record_event()
1086 1107 
1087 receipt = self._async_lane.submit(1108 receipt = self._async_lane.submit(
1088 generation=generation,1109 generation=generation,
@@ -1246,6 +1267,7 @@ class Buffer:
1246 ctx.clear()1267 ctx.clear()
1247 self._ctx = None1268 self._ctx = None
1248 self._comm_stream = None1269 self._comm_stream = None
1270+ self._owner_ready_stream = None
1249 for name in ('_async_sources', '_async_owned_sources', '_async_meta_sources',1271 for name in ('_async_sources', '_async_owned_sources', '_async_meta_sources',
1250 '_async_owner_ready_sources', '_async_owner_ready_views',1272 '_async_owner_ready_sources', '_async_owner_ready_views',
1251 '_async_views', '_async_meta_views', '_async_pointer_tables', '_async_plan_generations'):1273 '_async_views', '_async_meta_views', '_async_pointer_tables', '_async_plan_generations'):
@@ -1,7 +1,8 @@
1"""Private Q0 submit/wait/final-use/release protocol for native MoonEP ops.1"""Private Q0 submit/wait/final-use/release protocol for native MoonEP ops.
2 2 
3The caller supplies an operation-specific native launch whose own kernel/barrier3The caller supplies an operation-specific native launch whose own kernel/barrier
4-establishes peer visibility when Q0 records ``peer_visible``. A local event4+establishes peer visibility when Q0 records ``peer_visible``, or the launch
5+returns an event recorded after that proof on another stream. A local event
5alone is never upgraded to peer visibility. Every rank must enqueue submits6alone is never upgraded to peer visibility. Every rank must enqueue submits
6and releases in the same semantic order, including an empty operation.7and releases in the same semantic order, including an empty operation.
7"""8"""
@@ -34,8 +35,12 @@ class AsyncLane:
34 self.q0.wait_event(row.input_ready)35 self.q0.wait_event(row.input_ready)
35 # The native operation must prove required incoming payload36 # The native operation must prove required incoming payload
36 # completion (collective or owner signals) before this event.37 # completion (collective or owner signals) before this event.
37- native_submit()38+ # It may instead return an event recorded on another stream
38- row.peer_visible = self.q0.record_event()39+ # (e.g. a dedicated owner-ready wait stream) which then carries
40+ # the peer-visibility proof so Q0 never spins remotely.
41+ proven = native_submit()
42+ row.peer_visible = (proven if proven is not None
43+ else self.q0.record_event())
39 44 
40 return self.session.submit(generation=generation, kind=kind,45 return self.session.submit(generation=generation, kind=kind,
41 ordinal=ordinal, banks=banks, leases=leases, resources=resources,46 ordinal=ordinal, banks=banks, leases=leases, resources=resources,
@@ -134,6 +134,71 @@ void LPlusBOwnerPushReady(
134 // Do not add a global post-barrier here: it would defeat that admission.134 // Do not add a global post-barrier here: it would defeat that admission.
135}135}
136 136 
137+void LPlusBOwnerPushOnly(
138+ torch::Tensor source_ptrs,
139+ torch::Tensor weight_slots,
140+ torch::Tensor experts_to_copy,
141+ int64_t rank,
142+ int64_t local_experts)
143+{
144+ CheckCommon(source_ptrs, weight_slots, experts_to_copy, rank, local_experts);
145+ TORCH_CHECK(weight_slots.scalar_type() == torch::kBFloat16,
146+ "weight slot arena must be BF16");
147+ aclrtStream stream = c10_npu::getCurrentNPUStream().stream();
148+ // No up-front all-rank rendezvous: source readiness is already carried by
149+ // AsyncLane's producer-stream event ordered ahead of this launch on the
150+ // same stream, slot-reuse safety by its peer-final-use release collective,
151+ // and peer visibility by the kernel's trailing quiet plus the owner-ready
152+ // signal. Dropping the barrier lets Q0 issue every push back-to-back while
153+ // a dedicated stream waits on the ready signals.
154+ const int ret = moonep_lplusb_owner_push(
155+ stream,
156+ reinterpret_cast<const uint64_t*>(source_ptrs.data_ptr<int64_t>()),
157+ weight_slots.data_ptr(), experts_to_copy.data_ptr<int32_t>(),
158+ weight_slots.numel() / weight_slots.size(0),
159+ static_cast<int32_t>(local_experts), static_cast<int32_t>(experts_to_copy.size(0)),
160+ static_cast<int32_t>(experts_to_copy.size(1)), static_cast<int32_t>(rank),
161+ VectorBlocks());
162+ TORCH_CHECK(ret == 0, "L+B owner-push kernel failed with ACL error: ", ret);
163+}
164+ 
165+void LPlusBOwnerWaitReady(
166+ torch::Tensor ready,
167+ torch::Tensor experts_to_copy,
168+ int64_t rank,
169+ int64_t local_experts,
170+ int64_t epoch)
171+{
172+ TORCH_CHECK(ready.device().type() == c10::DeviceType::PrivateUse1 &&
173+ ready.scalar_type() == torch::kInt32 && ready.is_contiguous() &&
174+ ready.dim() == 2 && ready.size(0) > 0 && ready.size(1) == 16 &&
175+ reinterpret_cast<uintptr_t>(ready.data_ptr()) % 64 == 0,
176+ "owner-ready bank must be aligned INT32 [R,16] on NPU");
177+ TORCH_CHECK(experts_to_copy.device() == ready.device() &&
178+ experts_to_copy.scalar_type() == torch::kInt32 &&
179+ experts_to_copy.dim() == 2 && experts_to_copy.is_contiguous() &&
180+ experts_to_copy.size(0) == ready.size(0) && experts_to_copy.size(1) > 0,
181+ "experts_to_copy must be contiguous INT32 [R,B] on the ready NPU");
182+ TORCH_CHECK(rank >= 0 && rank < experts_to_copy.size(0),
183+ "rank is outside the EP group");
184+ TORCH_CHECK(epoch > 0 && epoch <= std::numeric_limits<int32_t>::max(),
185+ "owner-ready epoch must be positive INT32; wrapping is forbidden");
186+ TORCH_CHECK(local_experts > 0 && local_experts <= std::numeric_limits<int32_t>::max() &&
187+ local_experts <= std::numeric_limits<int32_t>::max() / experts_to_copy.size(0) &&
188+ experts_to_copy.size(0) <= std::numeric_limits<int32_t>::max() &&
189+ experts_to_copy.size(1) <= std::numeric_limits<int32_t>::max(),
190+ "L/R/B exceeds the private kernel int32 contract");
191+ // The caller must prove the paired owner-push complete on this stream (or
192+ // an ordered earlier one) before this launch publishes the epoch.
193+ aclrtStream stream = c10_npu::getCurrentNPUStream().stream();
194+ const int ret = moonep_lplusb_owner_ready(
195+ stream, ready.data_ptr<int32_t>(), experts_to_copy.data_ptr<int32_t>(),
196+ static_cast<int32_t>(local_experts), static_cast<int32_t>(experts_to_copy.size(0)),
197+ static_cast<int32_t>(experts_to_copy.size(1)), static_cast<int32_t>(rank),
198+ static_cast<int32_t>(epoch));
199+ TORCH_CHECK(ret == 0, "L+B owner-ready kernel failed with ACL error: ", ret);
200+}
201+ 
137void LPlusBOwnerReduce(202void LPlusBOwnerReduce(
138 torch::Tensor target_ptrs,203 torch::Tensor target_ptrs,
139 torch::Tensor grad_slots,204 torch::Tensor grad_slots,
@@ -25,4 +25,18 @@ void LPlusBOwnerPushReady(
25 torch::Tensor experts_to_copy, torch::Tensor ready,25 torch::Tensor experts_to_copy, torch::Tensor ready,
26 int64_t rank, int64_t local_experts, int64_t epoch);26 int64_t rank, int64_t local_experts, int64_t epoch);
27 27 
28+void LPlusBOwnerPushOnly(
29+ torch::Tensor source_ptrs,
30+ torch::Tensor weight_slots,
31+ torch::Tensor experts_to_copy,
32+ int64_t rank,
33+ int64_t local_experts);
34+ 
35+void LPlusBOwnerWaitReady(
36+ torch::Tensor ready,
37+ torch::Tensor experts_to_copy,
38+ int64_t rank,
39+ int64_t local_experts,
40+ int64_t epoch);
41+ 
28} // namespace ascend_moonep42} // namespace ascend_moonep
@@ -27,6 +27,12 @@ PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
27 m.def("_lplusb_owner_push_ready", &ascend_moonep::LPlusBOwnerPushReady,27 m.def("_lplusb_owner_push_ready", &ascend_moonep::LPlusBOwnerPushReady,
28 arg("source_ptrs"), arg("weight_slots"), arg("experts_to_copy"), arg("ready"),28 arg("source_ptrs"), arg("weight_slots"), arg("experts_to_copy"), arg("ready"),
29 arg("rank"), arg("local_experts"), arg("epoch"));29 arg("rank"), arg("local_experts"), arg("epoch"));
30+ m.def("_lplusb_owner_push_only", &ascend_moonep::LPlusBOwnerPushOnly,
31+ arg("source_ptrs"), arg("weight_slots"), arg("experts_to_copy"),
32+ arg("rank"), arg("local_experts"));
33+ m.def("_lplusb_owner_wait_ready", &ascend_moonep::LPlusBOwnerWaitReady,
34+ arg("ready"), arg("experts_to_copy"), arg("rank"), arg("local_experts"),
35+ arg("epoch"));
30 m.def("_lplusb_owner_push", &ascend_moonep::LPlusBOwnerPush,36 m.def("_lplusb_owner_push", &ascend_moonep::LPlusBOwnerPush,
31 arg("source_ptrs"), arg("weight_slots"), arg("experts_to_copy"),37 arg("source_ptrs"), arg("weight_slots"), arg("experts_to_copy"),
32 arg("rank"), arg("local_experts"));38 arg("rank"), arg("local_experts"));
@@ -146,7 +146,7 @@ def main():
146 [[0, ((destination + 1) % world) * 2 + 1]146 [[0, ((destination + 1) % world) * 2 + 1]
147 for destination in range(world)], dtype=torch.int32, device="npu"))147 for destination in range(world)], dtype=torch.int32, device="npu"))
148 sources = source_sets[0]148 sources = source_sets[0]
149- push_name = ('_lplusb_owner_push_ready' if prefetch_completion == 'required_owners_v1'149+ push_name = ('_lplusb_owner_push_only' if prefetch_completion == 'required_owners_v1'
150 else '_lplusb_owner_push')150 else '_lplusb_owner_push')
151 native_push = getattr(_C, push_name)151 native_push = getattr(_C, push_name)
152 152