Skip to content

Commit d7c4a8a

Browse files
committed
Refactor test code
1 parent 9cb32cd commit d7c4a8a

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

.github/workflows/validate-linux-binaries.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ jobs:
2929
package-type: libtorch
3030
os: linux
3131
channel: nightly
32-
33-
3432
validate-linux-binaries-conda:
3533
needs: generate-conda-matrix
3634
strategy:
@@ -39,11 +37,8 @@ jobs:
3937
fail-fast: false
4038
runs-on: ${{ matrix.validation_runner }}
4139
steps:
42-
- name: Checkout PyTorch atalman/builder
40+
- name: Checkout PyTorch builder
4341
uses: actions/checkout@v2
44-
with:
45-
ref: main
46-
repository: atalman/builder
4742
- name: Validate binary conda
4843
uses: ./.github/actions/validate-linux-binary
4944
with:

test/smoke_test/smoke_test.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@
33
import torch
44
import torchvision
55
import torchaudio
6+
import pathlib
67

78
gpu_arch_ver = os.getenv('GPU_ARCH_VER')
89
gpu_arch_type = os.getenv('GPU_ARCH_TYPE')
9-
is_cuda_system = gpu_arch_type == "cuda"
10+
is_cuda_system = gpu_arch_type == 'cuda'
1011

1112
def smoke_test_cuda() -> None:
1213
if(not torch.cuda.is_available() and is_cuda_system):
13-
print(f"Expected CUDA {gpu_arch_ver}. However CUDA is not loaded.")
14-
sys.exit(1)
14+
raise RuntimeError(f'Expected CUDA {gpu_arch_ver}. However CUDA is not loaded.')
1515
if(torch.cuda.is_available()):
1616
if(torch.version.cuda != gpu_arch_ver):
17-
print(f"Wrong CUDA version. Loaded: {torch.version.cuda} Expected: {gpu_arch_ver}")
18-
sys.exit(1)
19-
y=torch.randn([3,5]).cuda()
20-
print(f"torch cuda: {torch.version.cuda}")
17+
raise RuntimeError(f'Wrong CUDA version. Loaded: {torch.version.cuda} Expected: {gpu_arch_ver}')
18+
print(f'torch cuda: {torch.version.cuda}')
2119
#todo add cudnn version validation
22-
print(f"torch cudnn: {torch.backends.cudnn.version()}")
20+
print(f'torch cudnn: {torch.backends.cudnn.version()}')
2321

2422
def smoke_test_conv2d() -> None:
2523
import torch.nn as nn
26-
print(f"Calling smoke_test_conv2d")
24+
print('Calling smoke_test_conv2d')
2725
# With square kernels and equal stride
2826
m = nn.Conv2d(16, 33, 3, stride=2)
2927
# non-square kernels and unequal stride and with padding
@@ -33,29 +31,28 @@ def smoke_test_conv2d() -> None:
3331
input = torch.randn(20, 16, 50, 100)
3432
output = m(input)
3533
if(is_cuda_system):
36-
print(f"Testing smoke_test_conv2d with cuda")
34+
print('Testing smoke_test_conv2d with cuda')
3735
conv = nn.Conv2d(3, 3, 3).cuda()
3836
x = torch.randn(1, 3, 24, 24).cuda()
3937
with torch.cuda.amp.autocast():
4038
out = conv(x)
41-
print(out.sum())
4239

4340
def smoke_test_torchvision() -> None:
44-
import torchvision.datasets as dset
45-
import torchvision.transforms
46-
from torchvision.io import read_file, decode_jpeg, decode_png
4741
print('Is torchvision useable?', all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]))
48-
img_jpg = read_file('./test/smoke_test/assets/rgb_pytorch.jpg')
49-
img_jpg_nv = decode_jpeg(img_jpg)
50-
img_png = read_file('./test/smoke_test/assets/rgb_pytorch.png')
51-
img__png_nv = decode_png(img_png)
5242

53-
def smoke_test_vision_resnet50_classify() -> None:
43+
def smoke_test_torchvision_read_decode() -> None:
44+
from torchvision.io import decode_image, read_image,
45+
img_jpg = read_image(str(pathlib.Path(__file__).parent / 'assets' / 'rgb_pytorch.jpg'))
46+
img_jpg_nv = decode_image(img_jpg)
47+
img_png = read_image(str(pathlib.Path(__file__).parent / 'assets' / 'rgb_pytorch.png'))
48+
assert img_png.ndim == 3 and img_png.numel() > 100
49+
img_png_nv = decode_image(img_png)
50+
51+
def smoke_test_torchvision_resnet50_classify() -> None:
5452
from torchvision.io import read_image
5553
from torchvision.models import resnet50, ResNet50_Weights
5654

57-
img = read_image("./test/smoke_test/assets/dog2.jpg")
58-
55+
img = read_image(str(pathlib.Path(__file__).parent / 'assets' / 'dog2.jpg'))
5956
# Step 1: Initialize model with the best available weights
6057
weights = ResNet50_Weights.DEFAULT
6158
model = resnet50(weights=weights)
@@ -71,12 +68,11 @@ def smoke_test_vision_resnet50_classify() -> None:
7168
prediction = model(batch).squeeze(0).softmax(0)
7269
class_id = prediction.argmax().item()
7370
score = prediction[class_id].item()
74-
category_name = weights.meta["categories"][class_id]
75-
expected_category = "German shepherd"
76-
print(f"{category_name}: {100 * score:.1f}%")
71+
category_name = weights.meta['categories'][class_id]
72+
expected_category = 'German shepherd'
73+
print(f'{category_name}: {100 * score:.1f}%')
7774
if(category_name != expected_category):
78-
print(f"Failed ResNet50 classify {category_name} Expected: {expected_category}")
79-
sys.exit(1)
75+
raise RuntimeError(f'Failed ResNet50 classify {category_name} Expected: {expected_category}')
8076

8177

8278
def smoke_test_torchaudio() -> None:
@@ -92,14 +88,15 @@ def smoke_test_torchaudio() -> None:
9288

9389
def main() -> None:
9490
#todo add torch, torchvision and torchaudio tests
95-
print(f"torch: {torch.__version__}")
96-
print(f"torchvision: {torchvision.__version__}")
97-
print(f"torchaudio: {torchaudio.__version__}")
91+
print(f'torch: {torch.__version__}')
92+
print(f'torchvision: {torchvision.__version__}')
93+
print(f'torchaudio: {torchaudio.__version__}')
9894
smoke_test_cuda()
9995
smoke_test_conv2d()
100-
smoke_test_torchvision()
10196
smoke_test_torchaudio()
102-
smoke_test_vision_resnet50_classify()
97+
smoke_test_torchvision()
98+
smoke_test_torchvision_read_decode()
99+
smoke_test_torchvision_resnet50_classify()
103100

104-
if __name__ == "__main__":
101+
if __name__ == '__main__':
105102
main()

0 commit comments

Comments
 (0)