convolution.py 6.55 KB
Newer Older
1
2
3
4
# Copyright (c) Microsoft Corporation.

# Licensed under the MIT License.
import math
5
6
import operator
from functools import reduce
7
from typing import Tuple
8

9
import torch
10

11
12
13
14
15
16
17
18
19
from ..registry import meta_profiler_module


@meta_profiler_module.register(torch.nn.Conv1d)
def torch_nn_conv1d(self: torch.nn.Conv1d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
    c_in, l_in = input.shape[-2:]
    c_out = self.out_channels
20
21
22
    l_out = math.floor(
        (l_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1
    )
23
24
25
26
    result_shape = input.shape[:-2] + (
        c_out,
        l_out,
    )
27
28
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(operator.mul, result_shape)
29
30
31
32
33
34
35
36
37
38
39
40
41
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
        flops += num_elem
    return flops, macs


@meta_profiler_module.register(torch.nn.Conv2d)
def torch_nn_conv2d(self: torch.nn.Conv2d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html
    c_in, h_in, w_in = input.shape[-3:]
    c_out = self.out_channels
42
43
44
45
46
47
    h_out = math.floor(
        (h_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1
    )
    w_out = math.floor(
        (w_in + 2 * self.padding[1] - self.dilation[1] * (self.kernel_size[1] - 1) - 1) / self.stride[1] + 1
    )
48
49
50
51
52
    result_shape = input.shape[:-3] + (
        c_out,
        h_out,
        w_out,
    )
53
54
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(operator.mul, result_shape)
55
56
57
58
59
60
61
62
63
64
65
66
67
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
        flops += num_elem
    return flops, macs


@meta_profiler_module.register(torch.nn.Conv3d)
def torch_nn_conv3d(self: torch.nn.Conv3d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html
    c_in, d_in, h_in, w_in = input.shape[-4:]
    c_out = self.out_channels
68
69
70
71
72
73
74
75
76
    d_out = math.floor(
        (d_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1
    )
    h_out = math.floor(
        (h_in + 2 * self.padding[1] - self.dilation[1] * (self.kernel_size[1] - 1) - 1) / self.stride[1] + 1
    )
    w_out = math.floor(
        (w_in + 2 * self.padding[2] - self.dilation[2] * (self.kernel_size[2] - 1) - 1) / self.stride[2] + 1
    )
77
78
79
80
81
82
    result_shape = input.shape[:-4] + (
        c_out,
        d_out,
        h_out,
        w_out,
    )
83
84
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(operator.mul, result_shape)
85
86
87
88
89
90
91
92
93
94
95
96
97
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
        flops += num_elem
    return flops, macs


@meta_profiler_module.register(torch.nn.ConvTranspose1d)
def torch_nn_convtranspose1d(self: torch.nn.ConvTranspose1d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose1d.html
    c_in, l_in = input.shape[-2:]
    c_out = self.out_channels
98
99
100
101
102
103
104
    l_out = math.floor(
        (l_in - 1) * self.stride[0]
        - 2 * self.padding[0]
        + self.dilation[0] * (self.kernel_size[0] - 1)
        + self.output_padding[0]
        + 1
    )
105
106
107
108
    result_shape = input.shape[:-2] + (
        c_out,
        l_out,
    )
109
110
111
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(
        operator.mul, input.shape
112
    )  # see https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler/profiler.py#L604
113
114
115
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
116
        flops += reduce(operator.mul, result_shape)
117
118
119
120
121
122
123
124
125
    return flops, macs


@meta_profiler_module.register(torch.nn.ConvTranspose2d)
def torch_nn_convtranspose2d(self: torch.nn.ConvTranspose2d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose2d.html
    c_in, h_in, w_in = input.shape[-3:]
    c_out = self.out_channels
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    h_out = math.floor(
        (h_in - 1) * self.stride[0]
        - 2 * self.padding[0]
        + self.dilation[0] * (self.kernel_size[0] - 1)
        + self.output_padding[0]
        + 1
    )
    w_out = math.floor(
        (w_in - 1) * self.stride[1]
        - 2 * self.padding[1]
        + self.dilation[1] * (self.kernel_size[1] - 1)
        + self.output_padding[1]
        + 1
    )
140
141
142
143
144
    result_shape = input.shape[:-3] + (
        c_out,
        h_out,
        w_out,
    )
145
146
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(operator.mul, input.shape)
147
148
149
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
150
        flops += reduce(operator.mul, result_shape)
151
152
153
154
155
156
157
158
159
    return flops, macs


@meta_profiler_module.register(torch.nn.ConvTranspose3d)
def torch_nn_convtranspose3d(self: torch.nn.ConvTranspose3d, input: torch.Tensor) -> Tuple[int, int]:
    # the output shape is calculated using the formula stated
    # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose3d.html
    c_in, d_in, h_in, w_in = input.shape[-4:]
    c_out = self.out_channels
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    d_out = math.floor(
        (d_in - 1) * self.stride[0]
        - 2 * self.padding[0]
        + self.dilation[0] * (self.kernel_size[0] - 1)
        + self.output_padding[0]
        + 1
    )
    h_out = math.floor(
        (h_in - 1) * self.stride[1]
        - 2 * self.padding[1]
        + self.dilation[1] * (self.kernel_size[1] - 1)
        + self.output_padding[1]
        + 1
    )
    w_out = math.floor(
        (w_in - 1) * self.stride[2]
        - 2 * self.padding[2]
        + self.dilation[2] * (self.kernel_size[2] - 1)
        + self.output_padding[2]
        + 1
    )
181
182
183
184
185
186
    result_shape = input.shape[:-4] + (
        c_out,
        d_out,
        h_out,
        w_out,
    )
187
188
    macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups
    num_elem = reduce(operator.mul, input.shape)
189
190
191
    macs = macs_per_elem * num_elem
    flops = 2 * macs
    if self.bias is not None:
192
        flops += reduce(operator.mul, result_shape)
193
    return flops, macs