mirror of https://github.com/hpcaitech/ColossalAI
[lazyinit] add correctness verification (#3147)
* [lazyinit] fix shared module * [tests] add lazy init test utils * [tests] add torchvision for lazy init * [lazyinit] fix pre op fn * [lazyinit] handle legacy constructor * [tests] refactor lazy init test models * [tests] refactor lazy init test utils * [lazyinit] fix ops don't support meta * [tests] lazy init test timm models * [lazyinit] fix set data * [lazyinit] handle apex layers * [tests] lazy init test transformers models * [tests] lazy init test torchaudio models * [lazyinit] fix import path * [tests] lazy init test torchrec models * [tests] update torch version in CI * [tests] revert torch version in CI * [tests] skip lazy init testpull/3162/head
parent
3c01280a56
commit
6ae8ed0407
@ -0,0 +1 @@
|
||||
from .torchrec import *
|
@ -0,0 +1,23 @@
|
||||
import pytest
|
||||
|
||||
from tests.kit.model_zoo import model_zoo
|
||||
|
||||
# FIXME(ver217): uncomment this line
|
||||
# from utils import check_lazy_init
|
||||
|
||||
|
||||
# FIXME(ver217): temporarily skip this test since torch 1.11 does not fully support meta tensor
|
||||
@pytest.mark.skip
|
||||
@pytest.mark.parametrize('subset', ['torchvision', 'diffusers', 'timm', 'transformers', 'torchaudio', 'deepfm', 'dlrm'])
|
||||
def test_torchvision_models_lazy_init(subset):
|
||||
sub_model_zoo = model_zoo.get_sub_registry(subset)
|
||||
for name, entry in sub_model_zoo.items():
|
||||
# TODO(ver217): lazy init does not support weight norm, skip these models
|
||||
if name in ('torchaudio_wav2vec2_base', 'torchaudio_hubert_base'):
|
||||
continue
|
||||
# FIXME(ver217): uncomment this line
|
||||
# check_lazy_init(entry, verbose=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_torchvision_models_lazy_init('torchvision')
|
@ -0,0 +1,69 @@
|
||||
import random
|
||||
from typing import Any, Callable, Optional, Tuple
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from colossalai.utils.model.experimental import LazyInitContext, LazyTensor, _MyTensor
|
||||
from tests.kit.model_zoo.registry import ModelAttribute
|
||||
|
||||
# model_fn, data_gen_fn, output_transform_fn, model_attr
|
||||
TestingEntry = Tuple[Callable[[], torch.nn.Module], Callable[[], dict], Callable[[], dict], Optional[ModelAttribute]]
|
||||
|
||||
|
||||
def set_seed(seed: int) -> None:
|
||||
random.seed(seed)
|
||||
np.random.seed(seed)
|
||||
torch.manual_seed(seed)
|
||||
|
||||
|
||||
def assert_model_eqaual(m1: torch.nn.Module, m2: torch.nn.Module) -> None:
|
||||
s1 = m1.state_dict()
|
||||
s2 = m2.state_dict()
|
||||
|
||||
assert len(s1) == len(s2), f'len {len(s1)} vs {len(s2)}'
|
||||
|
||||
for (n1, t1), (n2, t2) in zip(s1.items(), s2.items()):
|
||||
assert n1 == n2
|
||||
assert torch.equal(t1, t2), f'{n1} {t1} vs {t2}'
|
||||
|
||||
|
||||
def assert_forward_equal(m1: torch.nn.Module, m2: torch.nn.Module, data_gen_fn: Callable[[], dict],
|
||||
output_transform_fn: Callable[[Any], dict]) -> None:
|
||||
data = data_gen_fn()
|
||||
|
||||
m1.eval()
|
||||
m2.eval()
|
||||
# run forward
|
||||
with torch.no_grad():
|
||||
outputs1 = m1(**data)
|
||||
outputs2 = m2(**data)
|
||||
|
||||
# compare output
|
||||
transformed_out1 = output_transform_fn(outputs1)
|
||||
transformed_out2 = output_transform_fn(outputs2)
|
||||
|
||||
assert len(transformed_out1) == len(transformed_out2)
|
||||
|
||||
for key, out1 in transformed_out1.items():
|
||||
out2 = transformed_out2[key]
|
||||
assert torch.allclose(out1, out2, atol=1e-5), \
|
||||
f'{m1.__class__.__name__} has inconsistent outputs, {out1} vs {out2}'
|
||||
|
||||
|
||||
def check_lazy_init(entry: TestingEntry, seed: int = 42, verbose: bool = False, check_forward: bool = False) -> None:
|
||||
model_fn, data_gen_fn, output_transform_fn, model_attr = entry
|
||||
_MyTensor._pre_op_fn = lambda *args: set_seed(seed)
|
||||
LazyTensor._pre_op_fn = lambda *args: set_seed(seed)
|
||||
ctx = LazyInitContext(tensor_cls=_MyTensor)
|
||||
with ctx:
|
||||
model = model_fn()
|
||||
ctx = LazyInitContext()
|
||||
with ctx:
|
||||
deferred_model = model_fn()
|
||||
deferred_model = ctx.materialize(deferred_model, verbose=verbose)
|
||||
assert_model_eqaual(model, deferred_model)
|
||||
if check_forward:
|
||||
assert_forward_equal(model, deferred_model, data_gen_fn, output_transform_fn)
|
||||
if verbose:
|
||||
print(f'{model.__class__.__name__} pass')
|
Loading…
Reference in new issue