mirror of https://github.com/hpcaitech/ColossalAI
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.4 KiB
64 lines
2.4 KiB
2 years ago
|
import torch
|
||
|
from colossalai.fx.tracer.meta_patch import patched_function
|
||
|
from functools import partial
|
||
|
|
||
|
|
||
|
def _run(data, patch_fn):
|
||
|
try:
|
||
|
output = patch_fn(data)
|
||
|
return output
|
||
|
except Exception as e:
|
||
|
return e
|
||
|
|
||
|
|
||
|
def _assert_output_shape(data, patch_fn, expect_exception, output_shape):
|
||
|
output = _run(data, patch_fn)
|
||
|
|
||
|
if expect_exception:
|
||
|
assert isinstance(output, AssertionError)
|
||
|
else:
|
||
|
assert not isinstance(output, Exception)
|
||
|
assert output.is_meta
|
||
|
assert output.shape == output_shape
|
||
|
|
||
|
|
||
|
def test_repeat_interleave():
|
||
|
patch_fn = patched_function.torch_repeat_interleave
|
||
|
|
||
|
# examples from https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html
|
||
|
data = torch.tensor([1, 2, 3])
|
||
|
materialized_output = torch.repeat_interleave(data, repeats=2)
|
||
|
repeat_interleave = partial(patch_fn, repeats=2)
|
||
|
meta_data = data.to('meta')
|
||
|
_assert_output_shape(data=meta_data,
|
||
|
patch_fn=repeat_interleave,
|
||
|
expect_exception=False,
|
||
|
output_shape=materialized_output.shape)
|
||
|
|
||
|
data = torch.tensor([[1, 2], [3, 4]])
|
||
|
materialized_output = torch.repeat_interleave(data, repeats=3, dim=1)
|
||
|
repeat_interleave = partial(patch_fn, repeats=3, dim=1)
|
||
|
meta_data = data.to('meta')
|
||
|
_assert_output_shape(data=meta_data,
|
||
|
patch_fn=repeat_interleave,
|
||
|
expect_exception=False,
|
||
|
output_shape=materialized_output.shape)
|
||
|
|
||
|
data = torch.tensor([[1, 2], [3, 4]])
|
||
|
materialized_output = torch.repeat_interleave(data, repeats=torch.tensor([1, 2]), dim=-1)
|
||
|
repeat_interleave = partial(patch_fn, repeats=torch.tensor([1, 2]), dim=-1)
|
||
|
meta_data = data.to('meta')
|
||
|
_assert_output_shape(data=meta_data,
|
||
|
patch_fn=repeat_interleave,
|
||
|
expect_exception=False,
|
||
|
output_shape=materialized_output.shape)
|
||
|
|
||
|
data = torch.tensor([[1, 2], [3, 4]])
|
||
|
materialized_output = torch.repeat_interleave(data, repeats=torch.tensor([1, 2]), dim=0)
|
||
|
repeat_interleave = partial(patch_fn, repeats=[1, 2], dim=0)
|
||
|
meta_data = data.to('meta')
|
||
|
_assert_output_shape(data=meta_data,
|
||
|
patch_fn=repeat_interleave,
|
||
|
expect_exception=True,
|
||
|
output_shape=materialized_output.shape)
|