-
Notifications
You must be signed in to change notification settings - Fork 1
Channels last #12
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
Open
fiedorowicz1
wants to merge
1
commit into
LBANN:main
Choose a base branch
from
fiedorowicz1:channels-last
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Channels last #12
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| import pytest | ||
| import torch | ||
| import torch.distributed as dist | ||
| import torch.nn as nn | ||
| from utils import cleanup_parallel_strategy, fp32_allclose | ||
|
|
||
| from distconv import DCTensor, DistConvDDP, ParallelStrategy | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def parallel_strategy(device: torch.device): | ||
| ps = ParallelStrategy(num_shards=4, device_type=device.type) | ||
| yield ps | ||
| cleanup_parallel_strategy(ps) | ||
|
|
||
|
|
||
| def generate_channels_last_configs(): | ||
| """Generate test configurations for channels_last testing.""" | ||
| configs = [] | ||
| # Test 2D convolutions with channels_last (ndims=2 -> Conv2d) | ||
| for shard_dim in range(2): # H or W dimension | ||
| for kernel_size in [1, 3, 5]: | ||
| configs.append((2, shard_dim, kernel_size, torch.channels_last)) | ||
|
|
||
| # Test 3D convolutions with channels_last_3d (ndims=3 -> Conv3d) | ||
| for shard_dim in range(3): # D, H, or W dimension | ||
| for kernel_size in [1, 3, 5]: | ||
| configs.append((3, shard_dim, kernel_size, torch.channels_last_3d)) | ||
|
|
||
| return "ndims,shard_dim,kernel_size,memory_format", configs | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(*generate_channels_last_configs()) | ||
| def test_channels_last_forward_backward( | ||
| parallel_strategy: ParallelStrategy, | ||
| ndims: int, | ||
| shard_dim: int, | ||
| kernel_size: int, | ||
| memory_format: torch.memory_format, | ||
| device: torch.device, | ||
| ): | ||
| """ | ||
| Test distributed convolution with channels_last memory format. | ||
| Verifies correctness and that memory format is preserved. | ||
|
|
||
| Args: | ||
| parallel_strategy (ParallelStrategy): Parallel strategy for the distributed convolution. | ||
| ndims (int): Number of dimensions for the convolution (2 or 3). | ||
| shard_dim (int): Dimension along which the tensor is sharded. | ||
| kernel_size (int): Size of the convolution kernel. | ||
| memory_format (torch.memory_format): Memory format to use. | ||
| device (torch.device): Torch device to run test with. | ||
| """ | ||
| parallel_strategy.shard_dim = 2 + shard_dim | ||
|
|
||
| # Create input tensor with channels_last format | ||
| shape = [1, 4] + [64] * ndims | ||
| x = torch.randn(*shape, device=device).to(memory_format=memory_format).requires_grad_(True) | ||
|
|
||
| # Verify input is in channels_last format | ||
| assert x.is_contiguous(memory_format=memory_format) | ||
|
|
||
| # Create convolution layer with channels_last format | ||
| conv_class = getattr(nn, f"Conv{ndims}d") | ||
| conv = conv_class(4, 8, kernel_size=kernel_size, padding=kernel_size // 2).to( | ||
| device | ||
| ) | ||
| conv = conv.to(memory_format=memory_format) | ||
|
|
||
| # Reference forward/backward | ||
| conv.zero_grad() | ||
| ref_y = conv(x) | ||
| ref_y.square().mean().backward() | ||
| ref_x_grad = x.grad.clone() | ||
| ref_conv_grad = conv.weight.grad.clone() | ||
|
|
||
| # Distributed forward/backward | ||
| conv.zero_grad() | ||
| x.grad = None | ||
| ddp_conv = DistConvDDP(conv, parallel_strategy=parallel_strategy) | ||
| dcx = DCTensor.distribute(x, parallel_strategy) | ||
|
|
||
| # Verify DCTensor preserves channels_last format | ||
| assert dcx._tensor.is_contiguous(memory_format=memory_format), ( | ||
| "DCTensor did not preserve channels_last format" | ||
| ) | ||
|
|
||
| dcy = ddp_conv(dcx) | ||
|
|
||
| # Verify output preserves channels_last format | ||
| assert dcy._tensor.is_contiguous(memory_format=memory_format), ( | ||
| "Output did not preserve channels_last format" | ||
| ) | ||
|
|
||
| ddpy = dcy.to_ddp() | ||
| ddpy.square().mean().backward() | ||
| x_grad = dcx.grad.to_ddp() | ||
| dc_conv_grad = conv.weight.grad | ||
|
|
||
| # Validate numerical correctness | ||
| if dist.get_rank() == 0: | ||
| assert fp32_allclose(ref_y, ddpy) | ||
| else: | ||
| assert ddpy.numel() == 0 | ||
| assert fp32_allclose(ref_x_grad, x_grad) | ||
| assert fp32_allclose(ref_conv_grad, dc_conv_grad) | ||
|
|
||
|
|
||
| def generate_periodic_channels_last_configs(): | ||
| """Generate test configurations for periodic padding with channels_last.""" | ||
| configs = [] | ||
| # Test 2D convolutions with channels_last | ||
| for shard_dim in range(2): | ||
| for kernel_size in [3, 5]: | ||
| configs.append((2, shard_dim, kernel_size, torch.channels_last)) | ||
|
|
||
| # Test 3D convolutions with channels_last_3d | ||
| for shard_dim in range(3): | ||
| for kernel_size in [3, 5]: | ||
| configs.append((3, shard_dim, kernel_size, torch.channels_last_3d)) | ||
|
|
||
| return "ndims,shard_dim,kernel_size,memory_format", configs | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(*generate_periodic_channels_last_configs()) | ||
| def test_channels_last_periodic_padding( | ||
| parallel_strategy: ParallelStrategy, | ||
| ndims: int, | ||
| shard_dim: int, | ||
| kernel_size: int, | ||
| memory_format: torch.memory_format, | ||
| device: torch.device, | ||
| ): | ||
| """ | ||
| Test periodic padding with channels_last format. | ||
|
|
||
| Args: | ||
| parallel_strategy (ParallelStrategy): Parallel strategy for the distributed convolution. | ||
| ndims (int): Number of dimensions for the convolution (2 or 3). | ||
| shard_dim (int): Dimension along which the tensor is sharded. | ||
| kernel_size (int): Size of the convolution kernel. | ||
| memory_format (torch.memory_format): Memory format to use. | ||
| device (torch.device): Torch device to run test with. | ||
| """ | ||
| parallel_strategy.shard_dim = 2 + shard_dim | ||
|
|
||
| # Create input tensor with channels_last format | ||
| shape = [1, 4] + [64] * ndims | ||
| x = torch.randn(*shape, device=device).to(memory_format=memory_format).requires_grad_(True) | ||
|
|
||
| # Create convolution layer with circular padding | ||
| conv_class = getattr(nn, f"Conv{ndims}d") | ||
| conv = conv_class( | ||
| 4, 8, kernel_size=kernel_size, padding=kernel_size // 2, padding_mode="circular" | ||
| ).to(device) | ||
| conv = conv.to(memory_format=memory_format) | ||
|
|
||
| # Reference forward/backward | ||
| conv.zero_grad() | ||
| ref_y = conv(x) | ||
| ref_y.square().mean().backward() | ||
| ref_x_grad = x.grad.clone() | ||
| ref_conv_grad = conv.weight.grad.clone() | ||
|
|
||
| # Distributed forward/backward | ||
| conv.zero_grad() | ||
| x.grad = None | ||
| ddp_conv = DistConvDDP(conv, parallel_strategy=parallel_strategy) | ||
| dcx = DCTensor.distribute(x, parallel_strategy) | ||
|
|
||
| dcy = ddp_conv(dcx) | ||
|
|
||
| # Verify output preserves channels_last format | ||
| assert dcy._tensor.is_contiguous(memory_format=memory_format), ( | ||
| "Output did not preserve channels_last format with periodic padding" | ||
| ) | ||
|
|
||
| ddpy = dcy.to_ddp() | ||
| ddpy.square().mean().backward() | ||
| x_grad = dcx.grad.to_ddp() | ||
| dc_conv_grad = conv.weight.grad | ||
|
|
||
| # Validate numerical correctness | ||
| if dist.get_rank() == 0: | ||
| assert fp32_allclose(ref_y, ddpy) | ||
| else: | ||
| assert ddpy.numel() == 0 | ||
| assert fp32_allclose(ref_x_grad, x_grad) | ||
| assert fp32_allclose(ref_conv_grad, dc_conv_grad) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could it be noncontiguous in other ways? Via some transposition etc.
To clarify, I’d rename the function to get memory format for halo