mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-03 01:55:12 +00:00
[fx] added module patch for pooling layers (#1197)
This commit is contained in:
31
tests/test_fx/test_tracer/test_non_patched_module.py
Normal file
31
tests/test_fx/test_tracer/test_non_patched_module.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import torch
|
||||
import torch.nn
|
||||
|
||||
|
||||
def test_maxpool():
|
||||
layer_to_test = dict(maxpool_1d=dict(layer=torch.nn.MaxPool1d, shape=(4, 3, 4)),
|
||||
maxpool_2d=dict(layer=torch.nn.MaxPool2d, shape=(4, 3, 4, 4)))
|
||||
|
||||
for name, info in layer_to_test.items():
|
||||
data = torch.rand(*info['shape'])
|
||||
meta_data = data.to('meta')
|
||||
layer = info['layer'](kernel_size=3)
|
||||
out = layer(data)
|
||||
meta_out = layer(meta_data)
|
||||
assert meta_out.is_meta
|
||||
assert out.shape == meta_out.shape
|
||||
|
||||
|
||||
def test_avgpool():
|
||||
layer_to_test = dict(maxpool_1d=dict(layer=torch.nn.AvgPool1d, shape=(4, 3, 4)),
|
||||
maxpool_2d=dict(layer=torch.nn.AvgPool2d, shape=(4, 3, 4, 4)),
|
||||
maxpool_3d=dict(layer=torch.nn.AvgPool3d, shape=(4, 3, 4, 4, 4)))
|
||||
|
||||
for name, info in layer_to_test.items():
|
||||
data = torch.rand(*info['shape'])
|
||||
meta_data = data.to('meta')
|
||||
layer = info['layer'](kernel_size=3)
|
||||
out = layer(data)
|
||||
meta_out = layer(meta_data)
|
||||
assert meta_out.is_meta
|
||||
assert out.shape == meta_out.shape
|
@@ -225,3 +225,33 @@ def test_conv3d():
|
||||
patch_fn=patched_module.torch_nn_conv3d,
|
||||
expect_exception=False,
|
||||
output_shape=materialized_output.shape)
|
||||
|
||||
|
||||
def test_maxpool3d():
|
||||
pooler = torch.nn.MaxPool3d(kernel_size=3)
|
||||
|
||||
# test max pool 3d
|
||||
data = torch.rand(2, 3, 4, 4, 4)
|
||||
materialized_output = pooler(data)
|
||||
_assert_output_shape(data=data,
|
||||
module=pooler,
|
||||
patch_fn=patched_module.torch_nn_maxpool3d,
|
||||
expect_exception=False,
|
||||
output_shape=materialized_output.shape)
|
||||
|
||||
# test max pool 3d
|
||||
data = torch.rand(2, 3, 4, 4)
|
||||
materialized_output = pooler(data)
|
||||
_assert_output_shape(data=data,
|
||||
module=pooler,
|
||||
patch_fn=patched_module.torch_nn_maxpool3d,
|
||||
expect_exception=False,
|
||||
output_shape=materialized_output.shape)
|
||||
|
||||
# test max pool 3d
|
||||
data = torch.rand(2, 3, 4)
|
||||
_assert_output_shape(data=data,
|
||||
module=pooler,
|
||||
patch_fn=patched_module.torch_nn_maxpool3d,
|
||||
expect_exception=True,
|
||||
output_shape=None)
|
||||
|
Reference in New Issue
Block a user