mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-07 12:01:39 +00:00
[analyzer] a minimal implementation of static graph analyzer (#2852)
* [hotfix] meta tensor default device. * [siu] add experimental submodules to main branch. * [siu] * [siu] * [analyzer] init. * [analyzer] readme. * [analyzer] readme. * [analyzer] readme. * [analyzer] readme. * [test] add test. * Update symbolic_trace.py * mark skip tests. * try except. * try except. * try except. * s * init * init * fix * skip * skip --------- Co-authored-by: Daniel Shao <superdainiu@MININT-PVARVID.fareast.corp.microsoft.com> Co-authored-by: Daniel Shao <superdainiu@Daniels-Mac.local>
This commit is contained in:
38
tests/test_analyzer/test_subclasses/test_meta_mode.py
Normal file
38
tests/test_analyzer/test_subclasses/test_meta_mode.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
import torchvision.models as tm
|
||||
try:
|
||||
from colossalai._analyzer._subclasses import MetaTensor, MetaTensorMode
|
||||
except:
|
||||
pass
|
||||
from .zoo import tm_models, tmm_models
|
||||
|
||||
|
||||
def compare_all(tensor: torch.Tensor, meta_tensor: torch.Tensor):
|
||||
assert tensor.shape == meta_tensor.shape, f'the shape of tensor ({tensor.shape}) and meta tensor ({meta_tensor.shape}) does not match.'
|
||||
assert tensor.dtype == meta_tensor.dtype, f'the dtype of tensor ({tensor.dtype}) and meta tensor ({meta_tensor.dtype}) does not match.'
|
||||
assert tensor.stride() == meta_tensor.stride(
|
||||
), f'the stride of tensor ({tensor.stride()}) and meta tensor ({meta_tensor.stride()}) does not match.'
|
||||
|
||||
|
||||
def run_and_compare(model):
|
||||
x = torch.rand(2, 3, 224, 224, requires_grad=True)
|
||||
x_out = model(x)
|
||||
with MetaTensorMode():
|
||||
meta_x = torch.rand(2, 3, 224, 224, requires_grad=True)
|
||||
meta_out = model(meta_x)
|
||||
compare_all(x_out, meta_out)
|
||||
x_out.sum().backward()
|
||||
meta_out.sum().backward()
|
||||
compare_all(x.grad, meta_x.grad)
|
||||
|
||||
|
||||
@pytest.mark.skipif(torch.__version__ < '1.12.0', reason='torch version < 12')
|
||||
@pytest.mark.parametrize('m', tm_models + tmm_models)
|
||||
def test_meta_mode_shape(m):
|
||||
run_and_compare(m())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_meta_mode_shape(tm.resnet18)
|
Reference in New Issue
Block a user