Skip to content

Commit 72f1a2a

Browse files
committed
Add check for tests that need Nvidia GPU and cuda
To enable tests to run non Nvidia GPU environments, need to check for cuda Signed-off-by: Martin Hickey <[email protected]>
1 parent a818120 commit 72f1a2a

File tree

4 files changed

+58
-54
lines changed

4 files changed

+58
-54
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ venv/
3737
# generated by setuptools_scm
3838
/fms_mo/_version.py
3939

40+
#Generated by tests
41+
qcfg.json
42+

tests/models/conftest.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -505,52 +505,53 @@ def model_config_fp16():
505505
return deepcopy(ToyModel4().half())
506506

507507

508-
class ToyModelQuantized(torch.nn.Module):
509-
"""
510-
Three layer Linear model that has a quantized layer
511-
512-
Extends:
513-
torch.nn.Module
514-
"""
508+
# QLinear class requires Nvidia GPU and cuda
509+
if torch.cuda.is_available():
515510

516-
def __init__(self):
517-
super().__init__()
518-
kwargs = {"qcfg": qconfig_init()} # QLinear requires qconfig to work
519-
self.first_layer = torch.nn.Linear(3, 3, bias=True)
520-
self.second_layer = QLinear(3, 3, bias=True, **kwargs)
521-
self.third_layer = torch.nn.Linear(3, 3, bias=True)
511+
class ToyModelQuantized(torch.nn.Module):
512+
"""
513+
Three layer Linear model that has a quantized layer
522514
523-
def forward(self, input_tensor):
515+
Extends:
516+
torch.nn.Module
524517
"""
525-
Forward func for Toy Model
526518

527-
Args:
528-
input_tensor (torch.FloatTensor): Tensor to operate on
519+
def __init__(self):
520+
super().__init__()
521+
kwargs = {"qcfg": qconfig_init()} # QLinear requires qconfig to work
522+
self.first_layer = torch.nn.Linear(3, 3, bias=True)
523+
self.second_layer = QLinear(3, 3, bias=True, **kwargs)
524+
self.third_layer = torch.nn.Linear(3, 3, bias=True)
529525

530-
Returns:
531-
torch.FloatTensor:
532-
"""
533-
out = self.first_layer(input_tensor)
534-
out = self.second_layer(out)
535-
out = self.third_layer(out)
536-
return out
526+
def forward(self, input_tensor):
527+
"""
528+
Forward func for Toy Model
537529
530+
Args:
531+
input_tensor (torch.FloatTensor): Tensor to operate on
538532
539-
model_quantized_params = [ToyModelQuantized()]
533+
Returns:
534+
torch.FloatTensor:
535+
"""
536+
out = self.first_layer(input_tensor)
537+
out = self.second_layer(out)
538+
out = self.third_layer(out)
539+
return out
540540

541+
model_quantized_params = [ToyModelQuantized()]
541542

542-
@pytest.fixture(scope="function", params=model_quantized_params)
543-
def model_quantized(request):
544-
"""
545-
Toy Model that has quantized layer
543+
@pytest.fixture(scope="function", params=model_quantized_params)
544+
def model_quantized(request):
545+
"""
546+
Toy Model that has quantized layer
546547
547-
Args:
548-
request (torch.nn.Module): Toy Model
548+
Args:
549+
request (torch.nn.Module): Toy Model
549550
550-
Returns:
551-
torch.nn.Module: Toy Model
552-
"""
553-
return deepcopy(request.param)
551+
Returns:
552+
torch.nn.Module: Toy Model
553+
"""
554+
return deepcopy(request.param)
554555

555556

556557
# Get a model to test layer uniqueness

tests/models/test_qmodelprep.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,29 @@
2828
from fms_mo.prep import has_quantized_module
2929
from tests.models.test_model_utils import delete_config, qmodule_error
3030

31-
3231
################
3332
# Qmodel tests #
3433
################
35-
def test_model_quantized(
36-
model_quantized: torch.nn.Module,
37-
sample_input_fp32: torch.FloatTensor,
38-
config_fp32: dict,
39-
):
40-
"""
41-
qmodel_prep should always throw RuntimeError if a model is already quantized
4234

43-
Args:
44-
model_quantized (torch.nn.Module): Quantized Toy Model
45-
sample_input_fp32 (torch.FloatTensor): Sample fp32 input for calibration.
46-
config_fp32 (dict): Config w/ fp32 settings
47-
"""
48-
delete_config()
49-
with pytest.raises(RuntimeError):
50-
qmodel_prep(model_quantized, sample_input_fp32, config_fp32)
35+
# Requires Nvidia GPU to run
36+
if torch.cuda.is_available():
37+
38+
def test_model_quantized(
39+
model_quantized: torch.nn.Module,
40+
sample_input_fp32: torch.FloatTensor,
41+
config_fp32: dict,
42+
):
43+
"""
44+
qmodel_prep should always throw RuntimeError if a model is already quantized
45+
46+
Args:
47+
model_quantized (torch.nn.Module): Quantized Toy Model
48+
sample_input_fp32 (torch.FloatTensor): Sample fp32 input for calibration.
49+
config_fp32 (dict): Config w/ fp32 settings
50+
"""
51+
delete_config()
52+
with pytest.raises(RuntimeError):
53+
qmodel_prep(model_quantized, sample_input_fp32, config_fp32)
5154

5255

5356
def test_double_qmodel_prep_assert(

tox.ini

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ minversion = 4.4
44

55
[testenv]
66
description = run tests (unit)
7-
# Use PyTorch CPU build instead of CUDA build in test envs
8-
setenv =
9-
PIP_EXTRA_INDEX_URL=https://download.pytorch.org/whl/cpu
107
extras =
118
dev
129
package = wheel

0 commit comments

Comments
 (0)