307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399 | reception: Previously posted receive buffer and handle.
forward: Select the chronological communication direction.
count: Number of maps in the relevant AllGather prefix or suffix.
"""
if self.is_rd:
state_shape = (1, self.heads, 128, 128)
maps = None if packed is None else packed.reshape(1, self.heads, 128, 256)
result = self.executor.scan_affine(maps, self.plans[not forward], state_shape, reverse=not forward)
return result, None, None
if self.width == self.size:
maps = self.executor.gather_packed(packed.reshape(1, *packed.shape[-3:]))
incoming = maps.new_zeros((maps.shape[1], 128, 128))
if count:
ops.merge_fwd_bwd_kernel(incoming, maps, count, self.rank, forward=forward)
return incoming.unsqueeze(0), None, None
incoming, recv_handle = reception
if self.width > 1:
return self._grouped_exchange(packed, incoming, recv_handle, forward)
transfer = self.info is None or self.info.num_sequences == 1
if recv_handle is not None and transfer:
recv_handle.wait()
recv_handle = None
send_handle, outgoing = None, None
peer = self.peer(forward, receiving=False)
if peer is not None:
outgoing = self.apply(packed, incoming if transfer else None).contiguous()
send_handle = dist.isend(outgoing, dst=self.ranks[peer], group=self.group)
# A reset permits sending the independent tail/head before this wait,
# but local h/dhu still consumes the received first/last fragment state.
if recv_handle is not None:
recv_handle.wait()
return incoming, send_handle, outgoing
def _grouped_exchange(self, packed, incoming, recv_handle, forward):
"""Gather native maps within a group and relay only its exit state."""
if self.info is not None:
if not self.info.transfer_active or not self.info.continues_next:
packed[..., 128:].zero_()
connected = self.info.continues_next if forward else self.info.continues_previous
if not connected:
packed[..., :128].zero_()
maps = packed.new_empty((self.width, *packed.shape[-3:]))
dist.all_gather_into_tensor(maps, packed.reshape(1, *packed.shape[-3:]), group=self.executor.intra)
send_handle, outgoing, result = None, None, None
indices = range(self.width) if forward else range(self.width - 1, -1, -1)
prefix_count = self.local if forward else self.width - self.local - 1
if self.rank == self.owner:
if recv_handle is not None:
recv_handle.wait()
peer = self.peer(forward, receiving=False)
if peer is not None:
outgoing, result = self._scan(maps, incoming, indices, prefix_count)
send_handle = dist.isend(outgoing, dst=self.ranks[peer], group=self.group)
if self.peer(forward, receiving=True) is not None:
dist.broadcast(incoming, src=self.ranks[self.owner], group=self.executor.intra)
if result is None:
local_indices = range(self.local) if forward else range(self.width - 1, self.local, -1)
result, _ = self._scan(maps, incoming, local_indices)
return result, send_handle, outgoing
@staticmethod
def _scan(maps, incoming, indices, prefix_count=None):
"""Keep native M/N orientation while applying a nonzero group entry."""
state = incoming
prefix = incoming if prefix_count == 0 else None
for step, index in enumerate(indices, start=1):
state = _NativeStateExchange.apply(maps[index:index + 1], state)
if step == prefix_count:
prefix = state
return state, prefix
def _exchange_native_gradient(ctx, ops, inputs, reception):
"""Own G/N production, reset injection and the stock dHT dtype boundary."""
if ctx.exchange is None:
return None, None, None
gk, qg, kg, weights, grad, dv_local_head = inputs
maps = None
if ctx.exchange.needs_summary(False):
# Only the stock G/N ABI rounds gk; local dhu/finalize retain FP32.
maps = ops.pre_process_bwd_kernel_merged(
qg, kg, weights, grad, dv_local_head, ctx.scale, 64,
gk=gk.to(qg.dtype), cu_seqlens=ctx.head)
terminal, send_handle, outgoing = ctx.exchange.exchange(ops, maps, reception, False, ctx.suffix)
if ctx.exchange.info is not None:
terminal = ctx.exchange.info.last_state(terminal)
# Communication and summary apply retain FP32; only stock dhu requires BF16.
terminal = None if terminal is None else terminal.to(qg.dtype)
return terminal, send_handle, outgoing
class _AscendCKDAStages(torch.autograd.Function):
"""Share native local stages; state protocols optionally inject H and D."""
|