credit.domain_parallel.halo_exchange#
Halo exchange for domain-parallel operations.
Implements differentiable halo exchange along the sharding dimension using point-to-point communication (isend/irecv). The backward pass performs the reverse exchange so gradients flow correctly across domain boundaries.
For latitude sharding of weather data: - The “previous” neighbor is the rank holding the region just north. - The “next” neighbor is the rank holding the region just south. - Edge ranks (poles) get zero-padded halos on their outer boundary,
since TensorPadding already handled pole reflection before sharding.
Classes#
Differentiable halo exchange. |
|
Halo exchange layer for domain-parallel operations. |
Module Contents#
- class credit.domain_parallel.halo_exchange._HaloExchangeFunction(*args, **kwargs)#
Bases:
torch.autograd.FunctionDifferentiable halo exchange.
Forward: pads the tensor with halo rows received from neighbors. Backward: sends gradient halos back to the ranks that contributed them.
- static forward(ctx, x, halo_width, dim, manager)#
Define the forward of the custom autograd Function.
This function is to be overridden by all subclasses. There are two ways to define forward:
Usage 1 (Combined forward and ctx):
@staticmethod def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any: pass
It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).
See combining-forward-context for more details
Usage 2 (Separate forward and ctx):
@staticmethod def forward(*args: Any, **kwargs: Any) -> Any: pass @staticmethod def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None: pass
The forward no longer accepts a ctx argument.
Instead, you must also override the
torch.autograd.Function.setup_context()staticmethod to handle setting up thectxobject.outputis the output of the forward,inputsare a Tuple of inputs to the forward.See extending-autograd for more details
The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with
ctx.save_for_backward()if they are intended to be used inbackward(equivalently,vjp) orctx.save_for_forward()if they are intended to be used for injvp.
- static backward(ctx, grad_output)#
Define a formula for differentiating the operation with backward mode automatic differentiation.
This function is to be overridden by all subclasses. (Defining this function is equivalent to defining the
vjpfunction.)It must accept a context
ctxas the first argument, followed by as many outputs as theforward()returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs toforward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.The context can be used to retrieve tensors saved during the forward pass. It also has an attribute
ctx.needs_input_gradas a tuple of booleans representing whether each input needs gradient. E.g.,backward()will havectx.needs_input_grad[0] = Trueif the first input toforward()needs gradient computed w.r.t. the output.
- class credit.domain_parallel.halo_exchange.HaloExchange(halo_width, dim=-2)#
Bases:
torch.nn.ModuleHalo exchange layer for domain-parallel operations.
Pads the input tensor with halo rows from neighboring ranks along the sharding dimension. The operation is differentiable.
- Parameters:
halo_width – Number of rows to exchange on each side.
dim – Tensor dimension to exchange along (default: -2 for lat in BCHW).
- halo_width#
- dim = -2#
- forward(x)#
- static trim(x, halo_before, halo_after, dim=-2)#
Trim halo rows from the output after a convolution.
- Parameters:
x – Tensor with extra halo rows.
halo_before – Number of rows to trim from the start.
halo_after – Number of rows to trim from the end.
dim – Dimension to trim along.