@ -339,15 +339,18 @@ EXCLUDE_FX_FILTERS = []
if ' GITHUB_ACTIONS ' in os . environ :
if ' GITHUB_ACTIONS ' in os . environ :
EXCLUDE_FX_FILTERS + = [
EXCLUDE_FX_FILTERS + = [
' beit_large* ' ,
' beit_large* ' ,
' swin_large* ' ,
' mixer_l* ' ,
' *nfnet_f2* ' ,
' *resnext101_32x32d ' ,
' *resnext101_32x32d ' ,
' resnetv2_152x2* ' ,
' resnetv2_152x2* ' ,
' *nfnet_f2* ' ,
' resmlp_big* ' ,
' resmlp_big* ' ,
' resnetrs270 ' ,
' resnetrs270 ' ,
' swin_large* ' ,
' vgg* ' ,
' vgg* ' ,
' vit_large* ' ,
' vit_large* ' ,
' vit_base_patch8* ' ,
' xcit_large* ' ,
' xcit_large* ' ,
' *evob ' , ' *evos ' , # remove experimental evonorm models, seem to cause issues with dtype manipulation
]
]
@ -368,6 +371,7 @@ def test_model_forward_fx(model_name, batch_size):
input_size = _get_input_size ( model = model , target = TARGET_FWD_FX_SIZE )
input_size = _get_input_size ( model = model , target = TARGET_FWD_FX_SIZE )
if max ( input_size ) > MAX_FWD_FX_SIZE :
if max ( input_size ) > MAX_FWD_FX_SIZE :
pytest . skip ( " Fixed input size model > limit. " )
pytest . skip ( " Fixed input size model > limit. " )
with torch . no_grad ( ) :
inputs = torch . randn ( ( batch_size , * input_size ) )
inputs = torch . randn ( ( batch_size , * input_size ) )
outputs = model ( inputs )
outputs = model ( inputs )
if isinstance ( outputs , tuple ) :
if isinstance ( outputs , tuple ) :
@ -383,11 +387,14 @@ def test_model_forward_fx(model_name, batch_size):
assert not torch . isnan ( outputs ) . any ( ) , ' Output included NaNs '
assert not torch . isnan ( outputs ) . any ( ) , ' Output included NaNs '
@pytest.mark.timeout ( 120 )
if ' GITHUB_ACTIONS ' not in os . environ :
@pytest.mark.parametrize ( ' model_name ' , list_models (
# FIXME this test is causing GitHub actions to run out of RAM and abruptly kill the test process
@pytest.mark.timeout ( 120 )
@pytest.mark.parametrize ( ' model_name ' , list_models (
exclude_filters = EXCLUDE_FILTERS + EXCLUDE_FX_FILTERS , name_matches_cfg = True ) )
exclude_filters = EXCLUDE_FILTERS + EXCLUDE_FX_FILTERS , name_matches_cfg = True ) )
@pytest.mark.parametrize ( ' batch_size ' , [ 2 ] )
@pytest.mark.parametrize ( ' batch_size ' , [ 2 ] )
def test_model_backward_fx ( model_name , batch_size ) :
def test_model_backward_fx ( model_name , batch_size ) :
""" Symbolically trace each model and run single backward pass through the resulting GraphModule """
""" Symbolically trace each model and run single backward pass through the resulting GraphModule """
if not has_fx_feature_extraction :
if not has_fx_feature_extraction :
pytest . skip ( " Can ' t test FX. Torch >= 1.10 and Torchvision >= 0.11 are required. " )
pytest . skip ( " Can ' t test FX. Torch >= 1.10 and Torchvision >= 0.11 are required. " )
@ -397,8 +404,10 @@ def test_model_backward_fx(model_name, batch_size):
pytest . skip ( " Fixed input size model > limit. " )
pytest . skip ( " Fixed input size model > limit. " )
model = create_model ( model_name , pretrained = False , num_classes = 42 )
model = create_model ( model_name , pretrained = False , num_classes = 42 )
num_params = sum ( [ x . numel ( ) for x in model . parameters ( ) ] )
model . train ( )
model . train ( )
num_params = sum ( [ x . numel ( ) for x in model . parameters ( ) ] )
if ' GITHUB_ACTIONS ' in os . environ and num_params > 100e6 :
pytest . skip ( " Skipping FX backward test on model with more than 100M params. " )
model = _create_fx_model ( model , train = True )
model = _create_fx_model ( model , train = True )
outputs = tuple ( model ( torch . randn ( ( batch_size , * input_size ) ) ) . values ( ) )
outputs = tuple ( model ( torch . randn ( ( batch_size , * input_size ) ) ) . values ( ) )
@ -413,20 +422,21 @@ def test_model_backward_fx(model_name, batch_size):
assert num_params == num_grad , ' Some parameters are missing gradients '
assert num_params == num_grad , ' Some parameters are missing gradients '
assert not torch . isnan ( outputs ) . any ( ) , ' Output included NaNs '
assert not torch . isnan ( outputs ) . any ( ) , ' Output included NaNs '
# reason: model is scripted after fx tracing, but beit has torch.jit.is_scripting() control flow
EXCLUDE_FX_JIT_FILTERS = [
# reason: model is scripted after fx tracing, but beit has torch.jit.is_scripting() control flow
EXCLUDE_FX_JIT_FILTERS = [
' deit_*_distilled_patch16_224 ' ,
' deit_*_distilled_patch16_224 ' ,
' levit* ' ,
' levit* ' ,
' pit_*_distilled_224 ' ,
' pit_*_distilled_224 ' ,
] + EXCLUDE_FX_FILTERS
] + EXCLUDE_FX_FILTERS
@pytest.mark.timeout ( 120 )
@pytest.mark.timeout ( 120 )
@pytest.mark.parametrize (
@pytest.mark.parametrize (
' model_name ' , list_models (
' model_name ' , list_models (
exclude_filters = EXCLUDE_FILTERS + EXCLUDE_JIT_FILTERS + EXCLUDE_FX_JIT_FILTERS , name_matches_cfg = True ) )
exclude_filters = EXCLUDE_FILTERS + EXCLUDE_JIT_FILTERS + EXCLUDE_FX_JIT_FILTERS , name_matches_cfg = True ) )
@pytest.mark.parametrize ( ' batch_size ' , [ 1 ] )
@pytest.mark.parametrize ( ' batch_size ' , [ 1 ] )
def test_model_forward_fx_torchscript ( model_name , batch_size ) :
def test_model_forward_fx_torchscript ( model_name , batch_size ) :
""" Symbolically trace each model, script it, and run single forward pass """
""" Symbolically trace each model, script it, and run single forward pass """
if not has_fx_feature_extraction :
if not has_fx_feature_extraction :
pytest . skip ( " Can ' t test FX. Torch >= 1.10 and Torchvision >= 0.11 are required. " )
pytest . skip ( " Can ' t test FX. Torch >= 1.10 and Torchvision >= 0.11 are required. " )
@ -440,6 +450,7 @@ def test_model_forward_fx_torchscript(model_name, batch_size):
model . eval ( )
model . eval ( )
model = torch . jit . script ( _create_fx_model ( model ) )
model = torch . jit . script ( _create_fx_model ( model ) )
with torch . no_grad ( ) :
outputs = tuple ( model ( torch . randn ( ( batch_size , * input_size ) ) ) . values ( ) )
outputs = tuple ( model ( torch . randn ( ( batch_size , * input_size ) ) ) . values ( ) )
if isinstance ( outputs , tuple ) :
if isinstance ( outputs , tuple ) :
outputs = torch . cat ( outputs )
outputs = torch . cat ( outputs )