Skip to content

Commit b50cdd8

Browse files
committed
nhl
1 parent 2680bbb commit b50cdd8

14 files changed

+90
-27
lines changed

onnx_diagnostic/helpers/config_helper.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import functools
22
import importlib
33
import inspect
4+
import os
45
import re
56
from typing import Any, Callable, Dict, Optional, Tuple, Union
67
import transformers
@@ -110,3 +111,12 @@ def config_class_from_architecture(arch: str, exc: bool = False) -> Optional[typ
110111
)
111112
cls_name = unique.pop()
112113
return getattr(transformers, cls_name)
114+
115+
116+
def default_num_hidden_layers():
117+
"""
118+
Returns the default number of layers.
119+
It is lower when the unit tests are running
120+
when ``UNITTEST_GOING=1``.
121+
"""
122+
return 2 if os.environ("UNITTEST_GOING", "0") == "1" else 4

onnx_diagnostic/tasks/automatic_speech_recognition.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import torch
33
import transformers
44
from ..helpers.cache_helper import make_dynamic_cache, make_encoder_decoder_cache
5-
from ..helpers.config_helper import update_config, check_hasattr
5+
from ..helpers.config_helper import (
6+
update_config,
7+
check_hasattr,
8+
default_num_hidden_layers as nhl,
9+
)
610

711
__TASK__ = "automatic-speech-recognition"
812

@@ -15,7 +19,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
1519
if hasattr(config, "decoder_layers"):
1620
config.decoder_layers = min(config.decoder_layers, 2)
1721
if hasattr(config, "num_hidden_layers"):
18-
config.num_hidden_layers = min(config.num_hidden_layers, 4)
22+
config.num_hidden_layers = min(config.num_hidden_layers, nhl())
1923
update_config(config, kwargs)
2024
return kwargs
2125

onnx_diagnostic/tasks/feature_extraction.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48
from ..helpers.cache_helper import make_dynamic_cache, make_encoder_decoder_cache
59

10+
611
__TASK__ = "feature-extraction"
712

813

914
def reduce_model_config(config: Any) -> Dict[str, Any]:
1015
"""Reduces a model size."""
1116
check_hasattr(config, "num_hidden_layers")
12-
kwargs = dict(num_hidden_layers=min(config.num_hidden_layers, 4))
17+
kwargs = dict(num_hidden_layers=min(config.num_hidden_layers, nhl()))
1318
update_config(config, kwargs)
1419
return kwargs
1520

onnx_diagnostic/tasks/fill_mask.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48

59
__TASK__ = "fill-mask"
610

@@ -9,7 +13,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
913
"""Reduces a model size."""
1014
check_hasattr(config, "num_attention_heads", "num_hidden_layers")
1115
kwargs = dict(
12-
num_hidden_layers=min(config.num_hidden_layers, 4),
16+
num_hidden_layers=min(config.num_hidden_layers, nhl()),
1317
num_attention_heads=min(config.num_attention_heads, 4),
1418
)
1519
update_config(config, kwargs)

onnx_diagnostic/tasks/image_classification.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48

59
__TASK__ = "image-classification"
610

@@ -17,7 +21,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
1721
check_hasattr(config, ("num_hidden_layers", "hidden_sizes"))
1822
kwargs = dict(
1923
num_hidden_layers=(
20-
min(config.num_hidden_layers, 4)
24+
min(config.num_hidden_layers, nhl())
2125
if hasattr(config, "num_hidden_layers")
2226
else len(config.hidden_sizes)
2327
)

onnx_diagnostic/tasks/image_text_to_text.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
33
from ..helpers.cache_helper import make_dynamic_cache, make_hybrid_cache
4-
from ..helpers.config_helper import update_config, check_hasattr, _pick
4+
from ..helpers.config_helper import (
5+
update_config,
6+
check_hasattr,
7+
_pick,
8+
default_num_hidden_layers as nhl,
9+
)
510

611
__TASK__ = "image-text-to-text"
712

@@ -10,7 +15,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
1015
"""Reduces a model size."""
1116
kwargs: Dict[str, Any] = {}
1217
if hasattr(config, "num_hidden_layers"):
13-
config.num_hidden_layers = min(config.num_hidden_layers, 4)
18+
config.num_hidden_layers = min(config.num_hidden_layers, nhl())
1419
if hasattr(config, "mm_tokens_per_image"):
1520
config.mm_tokens_per_image = min(config.mm_tokens_per_image, 2)
1621
if hasattr(config, "vision_config"):

onnx_diagnostic/tasks/mask_generation.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48

59
__TASK__ = "mask-generation"
610

@@ -9,7 +13,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
913
"""Reduces a model size."""
1014
kwargs: Dict[str, Any] = {}
1115
if hasattr(config, "num_hidden_layers"):
12-
config.num_hidden_layers = min(config.num_hidden_layers, 4)
16+
config.num_hidden_layers = min(config.num_hidden_layers, nhl())
1317
if hasattr(config, "vision_config") and hasattr(config.vision_config, "num_hidden_layers"):
1418
config.vision_config.num_hidden_layers = min(config.vision_config.num_hidden_layers, 2)
1519
update_config(config, kwargs)

onnx_diagnostic/tasks/mixture_of_expert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import torch
33

44
# from ..helpers.cache_helper import make_dynamic_cache
5-
from ..helpers.config_helper import update_config # , check_hasattr, _pick
5+
from ..helpers.config_helper import update_config, default_num_hidden_layers as nhl
66

77
__TASK__ = "MoE"
88

@@ -11,7 +11,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
1111
"""Reduces a model size."""
1212
kwargs: Dict[str, Any] = {}
1313
if hasattr(config, "num_hidden_layers"):
14-
config.num_hidden_layers = min(config.num_hidden_layers, 4)
14+
config.num_hidden_layers = min(config.num_hidden_layers, nhl())
1515
if hasattr(config, "vision_config") and hasattr(config.vision_config, "num_hidden_layers"):
1616
config.vision_config.num_hidden_layers = min(config.vision_config.num_hidden_layers, 2)
1717
if hasattr(config, "audio_processor") and hasattr(

onnx_diagnostic/tasks/object_detection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48

59
__TASK__ = "object-detection"
610

@@ -10,7 +14,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
1014
check_hasattr(config, ("num_hidden_layers", "hidden_sizes"))
1115
kwargs = dict(
1216
num_hidden_layers=(
13-
min(config.num_hidden_layers, 4)
17+
min(config.num_hidden_layers, nhl())
1418
if hasattr(config, "num_hidden_layers")
1519
else len(config.hidden_sizes)
1620
)

onnx_diagnostic/tasks/sentence_similarity.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Callable, Dict, Optional, Tuple
22
import torch
3-
from ..helpers.config_helper import update_config, check_hasattr
3+
from ..helpers.config_helper import (
4+
update_config,
5+
check_hasattr,
6+
default_num_hidden_layers as nhl,
7+
)
48

59
__TASK__ = "sentence-similarity"
610

@@ -9,7 +13,7 @@ def reduce_model_config(config: Any) -> Dict[str, Any]:
913
"""Reduces a model size."""
1014
check_hasattr(config, "num_attention_heads", "num_hidden_layers")
1115
kwargs = dict(
12-
num_hidden_layers=min(config.num_hidden_layers, 4),
16+
num_hidden_layers=min(config.num_hidden_layers, nhl()),
1317
num_attention_heads=min(config.num_attention_heads, 4),
1418
)
1519
update_config(config, kwargs)

0 commit comments

Comments
 (0)