test_nhwc_bias_add.py 4.21 KB
Newer Older
aiss's avatar
aiss committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'''
Copyright 2022 The Microsoft DeepSpeed Team
'''

import pytest
import torch
from deepspeed.ops.transformer.inference.bias_add import nhwc_bias_add
from deepspeed.accelerator import get_accelerator


def allclose(x, y):
    assert x.dtype == y.dtype
    rtol, atol = {torch.float32: (5e-3, 5e-4), torch.float16: (3e-2, 2e-3), torch.int8: (1, 1)}[x.dtype]
    return torch.allclose(x, y, rtol=rtol, atol=atol)


def ref_bias_add(activations, bias):
    return activations + bias.reshape(1, -1, 1, 1)


channels_list = [
    192,
    384,
    320,
    576,
    640,
    768,
    960,
    1152,
    1280,
    1536,
    1600,
    1920,
    2240,
    2560
]


@pytest.mark.inference_ops
@pytest.mark.parametrize("batch", [1, 2, 10])
@pytest.mark.parametrize("image_size", [16, 32, 64])
@pytest.mark.parametrize("channels", channels_list)
def test_bias_add(batch, image_size, channels):
    activations = torch.randn(
        (batch,
         channels,
         image_size,
         image_size),
        dtype=torch.float16,
        device=get_accelerator().device_name()).to(memory_format=torch.channels_last)
    bias = torch.randn((channels),
                       dtype=torch.float16,
                       device=get_accelerator().device_name())

    ref_vals = ref_bias_add(activations.clone().detach(), bias)
    ds_vals = nhwc_bias_add(activations, bias)

    assert allclose(ds_vals, ref_vals)


def ref_bias_add_add(activations, bias, other):
    return (activations + bias.reshape(1, -1, 1, 1)) + other


@pytest.mark.inference_ops
@pytest.mark.parametrize("batch", [1, 2, 10])
@pytest.mark.parametrize("image_size", [16, 32, 64])
@pytest.mark.parametrize("channels", channels_list)
def test_bias_add_add(batch, image_size, channels):
    activations = torch.randn(
        (batch,
         channels,
         image_size,
         image_size),
        dtype=torch.float16,
        device=get_accelerator().device_name()).to(memory_format=torch.channels_last)
    other = torch.randn(
        (batch,
         channels,
         image_size,
         image_size),
        dtype=torch.float16,
        device=get_accelerator().device_name()).to(memory_format=torch.channels_last)
    bias = torch.randn((channels),
                       dtype=torch.float16,
                       device=get_accelerator().device_name())

    ref_vals = ref_bias_add_add(activations.clone().detach(), bias, other)
    ds_vals = nhwc_bias_add(activations, bias, other=other)

    assert allclose(ds_vals, ref_vals)


def ref_bias_add_bias_add(activations, bias, other, other_bias):
    return (activations + bias.reshape(1,
                                       -1,
                                       1,
                                       1)) + (other + other_bias.reshape(1,
                                                                         -1,
                                                                         1,
                                                                         1))


@pytest.mark.inference_ops
@pytest.mark.parametrize("batch", [1, 2, 10])
@pytest.mark.parametrize("image_size", [16, 32, 64])
@pytest.mark.parametrize("channels", channels_list)
def test_bias_add_bias_add(batch, image_size, channels):
    activations = torch.randn(
        (batch,
         channels,
         image_size,
         image_size),
        dtype=torch.float16,
        device=get_accelerator().device_name()).to(memory_format=torch.channels_last)
    other = torch.randn(
        (batch,
         channels,
         image_size,
         image_size),
        dtype=torch.float16,
        device=get_accelerator().device_name()).to(memory_format=torch.channels_last)
    bias = torch.randn((channels),
                       dtype=torch.float16,
                       device=get_accelerator().device_name())
    other_bias = torch.randn((channels),
                             dtype=torch.float16,
                             device=get_accelerator().device_name())

    ref_vals = ref_bias_add_bias_add(activations.clone().detach(),
                                     bias,
                                     other,
                                     other_bias)
    ds_vals = nhwc_bias_add(activations, bias, other=other, other_bias=other_bias)

    assert allclose(ds_vals, ref_vals)