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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
# Owner(s): ["oncall: mobile"]
import torch
import torch._C
import torch.nn.functional as F
from torch.testing._internal.jit_utils import JitTestCase
from torch.testing._internal.common_utils import skipIfNoXNNPACK
class TestOptimizeForMobilePreserveDebugInfo(JitTestCase):
def check_replacement(
self,
model,
replacements,
jit_pass,
):
"""
model: Model which optimization is performed on
replacements: Dict mapping from nodes' kinds in the optimized model
to the kinds of nodes they replaced in the original model
jit_pass: Function to perform optimization
"""
original_kinds = set(replacements.values())
original_source_ranges = {
node.kind(): node.sourceRange()
for node in model.graph.nodes()
if node.kind() in original_kinds
}
jit_pass(model._c)
for node in model.graph.nodes():
if node.kind() in replacements:
self.assertEqual(
node.sourceRange(),
original_source_ranges[replacements[node.kind()]],
)
@skipIfNoXNNPACK
def test_replace_conv1d_with_conv2d(self):
class TestConv1d(torch.nn.Module):
def __init__(self, weight, bias):
super(TestConv1d, self).__init__()
self.weight = weight
self.bias = bias
def forward(self, x):
return F.conv1d(x, self.weight, self.bias)
self.check_replacement(
model=torch.jit.script(
TestConv1d(
weight=torch.rand(3, 3, 3),
bias=torch.rand(3),
),
),
replacements={
"prim::ListUnpack": "aten::conv1d",
"prim::ListConstruct": "aten::conv1d",
"aten::unsqueeze": "aten::conv1d",
"aten::conv2d": "aten::conv1d",
"aten::squeeze": "aten::conv1d",
},
jit_pass=torch._C._jit_pass_transform_conv1d_to_conv2d,
)
@skipIfNoXNNPACK
def test_insert_pre_packed_linear_before_inline_and_conv_2d_op(self):
class TestPrepackedLinearBeforeInlineAndConv2dOp(torch.nn.Module):
def __init__(
self,
linear_weight,
linear_bias,
conv2d_weight,
conv2d_bias,
conv_transpose2d_weight,
conv_transpose2d_bias,
):
super(
TestPrepackedLinearBeforeInlineAndConv2dOp,
self,
).__init__()
self.linear_weight = linear_weight.float()
self.linear_bias = linear_bias.float()
self.conv2d_weight = conv2d_weight.float()
self.conv2d_bias = conv2d_bias.float()
self.conv_transpose2d_weight = conv_transpose2d_weight.float()
self.conv_transpose2d_bias = conv_transpose2d_bias.float()
def forward(self, x):
linear_res = F.linear(
x.float(),
self.linear_weight,
self.linear_bias,
)
conv2d_res = F.conv2d(
input=linear_res.unsqueeze(dim=0).float(),
weight=self.conv2d_weight,
bias=self.conv2d_bias,
)
return F.conv_transpose2d(
input=conv2d_res,
weight=self.conv_transpose2d_weight,
bias=self.conv_transpose2d_bias,
)
minibatch = 1
in_channels = 6
iH = 4
iW = 5
out_channels = 6
kH = 2
kW = 3
self.check_replacement(
model=torch.jit.script(
TestPrepackedLinearBeforeInlineAndConv2dOp(
linear_weight=torch.rand(iW, 3),
linear_bias=torch.rand(iW),
conv2d_weight=torch.rand(out_channels, in_channels, kH, kW),
conv2d_bias=torch.rand(out_channels),
conv_transpose2d_weight=torch.rand(
out_channels,
in_channels,
kH,
kW,
),
conv_transpose2d_bias=torch.rand(out_channels),
),
),
replacements={
"prepacked::linear_clamp_prepack": "aten::linear",
"prepacked::linear_clamp_run": "aten::linear",
"prepacked::conv2d_clamp_prepack": "aten::conv2d",
"prepacked::conv2d_clamp_run": "aten::conv2d",
"prepacked::conv2d_transpose_clamp_prepack":
"aten::conv_transpose2d",
"prepacked::conv2d_transpose_clamp_run":
"aten::conv_transpose2d",
},
jit_pass=torch._C._jit_pass_insert_prepacked_ops,
)
@skipIfNoXNNPACK
def test_insert_pre_packed_linear_op(self):
self.check_replacement(
model=torch.jit.trace(torch.nn.Linear(5, 4), torch.rand(3, 2, 5)),
replacements={
"prepacked::linear_clamp_prepack": "aten::linear",
"prepacked::linear_clamp_run": "aten::linear"
},
jit_pass=torch._C._jit_pass_insert_prepacked_ops,
)
def run_test_fuse_activation_with_pack_ops_linear_conv2d(
self,
linear_activation,
linear_activation_kind,
conv2d_activation,
conv2d_activation_kind,
):
class TestFuseActivationLinearConv2d(torch.nn.Module):
def __init__(
self,
linear_weight,
linear_bias,
conv2d_weight,
conv2d_bias,
):
super(TestFuseActivationLinearConv2d, self).__init__()
self.linear_weight = linear_weight
self.linear_bias = linear_bias
self.conv2d_weight = conv2d_weight
self.conv2d_bias = conv2d_bias
def forward(self, x):
x = F.linear(
input=x,
weight=self.linear_weight,
bias=self.linear_bias,
)
x = linear_activation(x)
x = F.conv2d(
input=x.unsqueeze(dim=0),
weight=self.conv2d_weight,
bias=self.conv2d_bias,
)
return conv2d_activation(x)
linear_in_features = 5
linear_out_features = 4
conv2d_in_channels = 3
conv2d_out_channels = 4
conv2d_kernel = 2
x_shape = (3, 2, 5)
model = torch.jit.trace(
TestFuseActivationLinearConv2d(
linear_weight=torch.nn.Parameter(
data=torch.rand(
linear_out_features,
linear_in_features,
),
requires_grad=False,
),
linear_bias=torch.nn.Parameter(
data=torch.rand(linear_out_features),
requires_grad=False,
),
conv2d_weight=torch.rand(
conv2d_out_channels,
conv2d_in_channels,
conv2d_kernel,
conv2d_kernel,
),
conv2d_bias=torch.rand(conv2d_out_channels),
),
torch.rand(x_shape),
)
torch._C._jit_pass_insert_prepacked_ops(model._c)
self.check_replacement(
model=model,
replacements={
"prepacked::linear_clamp_prepack":
"prepacked::linear_clamp_prepack",
"prepacked::linear_clamp_run": linear_activation_kind,
"prepacked::conv2d_clamp_prepack":
"prepacked::conv2d_clamp_prepack",
"prepacked::conv2d_clamp_run": conv2d_activation_kind,
},
jit_pass=torch._C._jit_pass_fuse_clamp_w_prepacked_linear_conv,
)
@skipIfNoXNNPACK
def test_fuse_activation_with_pack_ops_linear_conv2d_1(self):
self.run_test_fuse_activation_with_pack_ops_linear_conv2d(
linear_activation=F.hardtanh,
linear_activation_kind="aten::hardtanh",
conv2d_activation=F.hardtanh_,
conv2d_activation_kind="aten::hardtanh_"
)
@skipIfNoXNNPACK
def test_fuse_activation_with_pack_ops_linear_conv2d_2(self):
self.run_test_fuse_activation_with_pack_ops_linear_conv2d(
linear_activation=F.hardtanh_,
linear_activation_kind="aten::hardtanh_",
conv2d_activation=F.hardtanh,
conv2d_activation_kind="aten::hardtanh"
)
@skipIfNoXNNPACK
def test_fuse_activation_with_pack_ops_linear_conv2d_3(self):
self.run_test_fuse_activation_with_pack_ops_linear_conv2d(
linear_activation=F.relu,
linear_activation_kind="aten::relu",
conv2d_activation=F.relu_,
conv2d_activation_kind="aten::relu_"
)
@skipIfNoXNNPACK
def test_fuse_activation_with_pack_ops_linear_conv2d_4(self):
self.run_test_fuse_activation_with_pack_ops_linear_conv2d(
linear_activation=F.relu_,
linear_activation_kind="aten::relu_",
conv2d_activation=F.relu,
conv2d_activation_kind="aten::relu"
)
|