test_pass_fold_scale_axis.py 23.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
import numpy as np
18

19
import tvm
Zhi committed
20 21 22
from tvm import relay
from tvm.relay import transform

23 24 25
def _get_positive_scale(size):
    return np.random.uniform(0.5, 1, size=size).astype('float32')

26

Zhi committed
27 28
def run_opt_pass(expr, opt_pass):
    assert isinstance(opt_pass, transform.Pass)
29
    mod = tvm.IRModule.from_expr(expr)
Zhi committed
30
    mod = opt_pass(mod)
31
    entry = mod["main"]
Zhi committed
32 33 34
    return entry if isinstance(expr, relay.Function) else entry.body


35 36 37
def test_fold_fwd_simple():
    """Simple testcase."""
    def before(x, conv_weight, in_bias, in_scale, channels):
38
        args = [x, conv_weight, in_bias]
39 40 41 42 43 44 45 46
        in_bias = relay.expand_dims(in_bias, axis=1, num_newaxis=2)
        x = relay.multiply(x, in_scale)
        x = relay.nn.relu(x)
        x = relay.add(x, in_bias)
        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
47

48 49 50 51
        return relay.Function(args, y)

    def expected(x, conv_weight, in_bias, in_scale, channels):
        # use a fixed order of args so alpha equal check can pass
52
        args = [x, conv_weight, in_bias]
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        in_bias = relay.expand_dims(in_bias, axis=1, num_newaxis=2)
        squeezed_scale = relay.squeeze(in_scale, axis=[1,2])
        x = relay.nn.relu(x)
        in_bias = relay.divide(in_bias, relay.expand_dims(squeezed_scale, axis=1, num_newaxis=2))
        x = relay.add(x, in_bias)
        conv_weight = relay.multiply(
            conv_weight , relay.expand_dims(squeezed_scale, axis=1, num_newaxis=2))
        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        in_bias = relay.var("in_bias", shape=(in_channels,))
71
        in_scale = relay.const(_get_positive_scale((in_channels, 1, 1)))
72
        y1 = before(x, weight, in_bias, in_scale, channels)
Zhi committed
73
        y1 = run_opt_pass(y1, transform.InferType())
74 75
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
76
        y1_folded = run_opt_pass(y1, transform.ForwardFoldScaleAxis())
77
        y1_expected = expected(x, weight, in_bias, in_scale, channels)
78

Zhi committed
79 80 81
        y1_folded = run_opt_pass(y1_folded, transform.InferType())
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
82 83 84 85 86 87 88

    check((2, 4, 10, 10), 2)


def test_fold_fwd_dual_path():
    """scale axis being consumed by two consumers"""
    def before(x, conv_weight, in_bias, in_scale, channels):
89
        args = [x, conv_weight, in_bias]
90 91 92 93 94 95 96
        x = relay.multiply(in_scale, x)
        x = relay.nn.relu(x)
        x = relay.subtract(x, in_bias)
        y1 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
97
                             kernel_layout="HWIO",
98 99 100 101 102 103
                             groups=channels,
                             padding=(1, 1))
        y2 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
104
                             kernel_layout="HWIO",
105 106 107 108 109 110
                             groups=channels,
                             padding=(1, 1))
        z = relay.add(y1, y2)
        return relay.Function(args, z)

    def expected(x, conv_weight, in_bias, in_scale, channels):
111
        args = [x, conv_weight, in_bias]
112 113 114 115 116 117 118 119
        x = relay.nn.relu(x)
        in_bias = relay.divide(in_bias, in_scale)
        x = relay.subtract(x, in_bias)
        y1 = relay.nn.conv2d(x,
                             relay.multiply(conv_weight, in_scale),
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
120
                             kernel_layout="HWIO",
121 122 123 124 125 126 127
                             groups=channels,
                             padding=(1, 1))
        y2 = relay.nn.conv2d(x,
                             relay.multiply(conv_weight, in_scale),
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
128
                             kernel_layout="HWIO",
129 130 131 132 133
                             groups=channels,
                             padding=(1, 1))
        z = relay.add(y1, y2)
        return relay.Function(args, z)

134 135 136
    def check(dshape, channels):
        x =  relay.var("x", shape=dshape)
        in_channels = dshape[-1]
137 138
        # test depthwise
        assert in_channels == channels
139 140
        wshape = (3, 3, 1, channels) # HWIO
        weight = relay.var("weight", shape=wshape)
141
        in_bias = relay.var("in_bias", shape=(in_channels,))
142
        in_scale = relay.const(_get_positive_scale(in_channels,))
143
        y1 = before(x, weight, in_bias, in_scale, channels)
Zhi committed
144 145
        y1 = run_opt_pass(y1, transform.InferType())
        y1_folded = run_opt_pass(y1, transform.ForwardFoldScaleAxis())
146 147 148
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
        y1_expected = expected(x, weight, in_bias, in_scale, channels)
Zhi committed
149 150
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    check((2, 4, 10, 3), 3)


def test_fold_fwd_fail():
    """testcase where we canont fold"""
    def before(x, conv_weight, in_bias, in_scale, channels):
        x = relay.multiply(x, in_scale)
        xx = relay.nn.leaky_relu(x, alpha=0.1)
        y1 = relay.nn.conv2d(xx, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
                             padding=(1, 1))
        z = relay.add(y1, x)
Zhi committed
166
        return relay.Function(relay.analysis.free_vars(z), z)
167 168 169 170 171 172 173 174

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[-1]
        # test depthwise
        assert in_channels == channels
        weight = relay.var("weight")
        in_bias = relay.var("in_bias", shape=(in_channels,))
175
        in_scale = relay.const(_get_positive_scale(size=(in_channels,)))
176
        y1 = before(x, weight, in_bias, in_scale, channels)
Zhi committed
177 178 179
        y1 = run_opt_pass(y1, transform.InferType())
        y1_folded = run_opt_pass(y1, transform.ForwardFoldScaleAxis())
        assert relay.analysis.alpha_equal(y1, y1_folded)
180 181 182 183

    check((2, 11, 10, 4), 4)


184 185 186 187 188 189 190 191 192 193 194
def test_fold_fwd_relu_fail():
    """testcase where we canont fold because scale can not pass relu"""
    def before(x, conv_weight, in_bias, in_scale, channels):
        x = relay.multiply(x, in_scale)
        xx = relay.nn.relu(x)
        y1 = relay.nn.conv2d(xx, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NHWC",
                             padding=(1, 1))
        z = relay.add(y1, x)
Zhi committed
195
        return relay.Function(relay.analysis.free_vars(z), z)
196 197 198 199 200 201 202 203 204

    def check(shape, channels, in_scale):
        x =  relay.var("x", shape=shape)
        in_channels = shape[-1]
        # test depthwise
        assert in_channels == channels
        weight = relay.var("weight")
        in_bias = relay.var("in_bias", shape=(in_channels,))
        y1 = before(x, weight, in_bias, in_scale, channels)
Zhi committed
205 206 207
        y1 = run_opt_pass(y1, transform.InferType())
        y1_folded = run_opt_pass(y1, transform.ForwardFoldScaleAxis())
        assert relay.analysis.alpha_equal(y1, y1_folded)
208 209 210

    in_scale = relay.var("in_scale", shape=(4,))
    check((2, 11, 10, 4), 4, in_scale)
211
    in_scale = relay.const(-_get_positive_scale((4,)))
212 213 214
    check((2, 11, 10, 4), 4, in_scale)


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
def test_fold_fwd_negative_scale():
    """Testcase of folding negative scale"""
    def before(x, conv_weight, in_scale, channels):
        args = [x, conv_weight]
        x = relay.multiply(x, in_scale)
        y = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        return relay.Function(args, y)

    def expected(x, conv_weight, in_scale, channels):
        # use a fixed order of args so alpha equal check can pass
        args = [x, conv_weight]
        squeezed_scale = relay.squeeze(in_scale, axis=[1,2])
        conv_weight = relay.multiply(
            conv_weight , relay.expand_dims(squeezed_scale, axis=1, num_newaxis=2))
        y = relay.nn.conv2d(x,
                             conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        in_scale = relay.const(-_get_positive_scale((in_channels, 1, 1)))
        weight = relay.var("weight")
        y1 = before(x, weight, in_scale, channels)
Zhi committed
245
        y1 = run_opt_pass(y1, transform.InferType())
246 247
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
248
        y1_folded = run_opt_pass(y1, transform.ForwardFoldScaleAxis())
249
        y1_expected = expected(x, weight, in_scale, channels)
Zhi committed
250 251
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
252 253 254 255

    check((2, 4, 10, 10), 4)


256 257 258
def test_fold_bwd_simple():
    """Simple testcase."""
    def before(x, conv_weight, out_bias, out_scale, channels):
259
        args = [x, conv_weight, out_bias]
260 261 262 263 264 265 266 267 268 269 270 271
        out_bias = relay.expand_dims(out_bias, axis=1, num_newaxis=2)
        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y = relay.add(y, out_bias)
        y = relay.nn.relu(y)
        y = relay.multiply(y, out_scale)
        return relay.Function(args, y)

    def expected(x, conv_weight, out_bias, out_scale, channels):
        # use a fixed order of args so alpha equal check can pass
272
        args = [x, conv_weight, out_bias]
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
        out_bias = relay.expand_dims(out_bias, axis=1, num_newaxis=2)
        squeezed_scale = relay.squeeze(out_scale, axis=[1,2])
        conv_weight = relay.multiply(
            conv_weight , relay.expand_dims(squeezed_scale, axis=1, num_newaxis=3))

        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        out_bias = relay.multiply(out_bias,
                                  relay.expand_dims(squeezed_scale, axis=1, num_newaxis=2))
        y = relay.add(y, out_bias)
        y = relay.nn.relu(y)
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        out_bias = relay.var("out_bias", shape=(channels,))
293
        out_scale = relay.const(_get_positive_scale((channels, 1, 1)))
294 295

        y1 = before(x, weight, out_bias, out_scale, channels)
Zhi committed
296
        y1 = run_opt_pass(y1, transform.InferType())
297 298
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
299
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
300
        y1_expected = expected(x, weight, out_bias, out_scale, channels)
Zhi committed
301 302
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
303 304 305 306 307 308 309

    check((2, 4, 10, 10), 8)


def test_fold_bwd_dual_path():
    """Dual path testcase."""
    def before(x, conv_weight, out_bias, out_scale, channels):
310
        args = [x, conv_weight, out_bias]
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
        y1 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y1 = relay.nn.relu(y1)
        y2 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y2 = relay.nn.relu(y2)
        y = relay.add(y1, y2)
        y = relay.multiply(y, out_scale)
        return relay.Function(args, y)

    def expected(x, conv_weight, out_bias, out_scale, channels):
        # use a fixed order of args so alpha equal check can pass
327
        args = [x, conv_weight, out_bias]
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
        out_bias = relay.expand_dims(out_bias, axis=1, num_newaxis=2)
        squeezed_scale = relay.squeeze(out_scale, axis=[1,2])
        def fold_conv_weight():
            return  relay.multiply(
                conv_weight ,
                relay.expand_dims(squeezed_scale, axis=1, num_newaxis=3))
        y1 = relay.nn.conv2d(x, fold_conv_weight(),
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y1 = relay.nn.relu(y1)
        y2 = relay.nn.conv2d(x, fold_conv_weight(),
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y2 = relay.nn.relu(y2)
        y = relay.add(y1, y2)
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        out_bias = relay.var("out_bias", shape=(channels,))
352
        out_scale = relay.const(_get_positive_scale((channels, 1, 1)))
353 354

        y1 = before(x, weight, out_bias, out_scale, channels)
Zhi committed
355
        y1 = run_opt_pass(y1, transform.InferType())
356 357
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
358
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
359
        y1_expected = expected(x, weight, out_bias, out_scale, channels)
Zhi committed
360 361
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
362 363 364 365

    check((2, 4, 10, 10), 8)


366 367
def test_fold_bwd_dual_consumer():
    def before(x, conv_weight, out_bias, out_scale, channels):
368
        args = [x, conv_weight, out_bias]
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
        y0 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y0 = relay.multiply(y0, out_scale)
        y0 = relay.nn.relu(y0)

        y1 = relay.nn.conv2d(y0, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y1 = relay.multiply(y1, out_scale)
        y1 = relay.nn.relu(y1)

        y2 = relay.nn.conv2d(y0, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y2 = relay.multiply(y2, out_scale)
        y2 = relay.nn.relu(y2)

        y = relay.add(y1, y2)
        return relay.Function(args, y)

    def expected(x, conv_weight, out_bias, out_scale, channels):
        # use a fixed order of args so alpha equal check can pass
395
        args = [x, conv_weight, out_bias]
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        def fold_conv_weight():
            squeezed_scale = relay.squeeze(out_scale, axis=[1,2])
            return  relay.multiply(
                conv_weight ,
                relay.expand_dims(squeezed_scale, axis=1, num_newaxis=3))
        y0 = relay.nn.conv2d(x, fold_conv_weight(),
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y0 = relay.nn.relu(y0)
        y1 = relay.nn.conv2d(y0, fold_conv_weight(),
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y1 = relay.nn.relu(y1)
        y2 = relay.nn.conv2d(y0, fold_conv_weight(),
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y2 = relay.nn.relu(y2)
        y = relay.add(y1, y2)
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        out_bias = relay.var("out_bias", shape=(channels,))
424
        out_scale = relay.const(_get_positive_scale((channels,1, 1)))
425 426

        y1 = before(x, weight, out_bias, out_scale, channels)
Zhi committed
427
        y1 = run_opt_pass(y1, transform.InferType())
428 429
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
430
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
431
        y1_expected = expected(x, weight, out_bias, out_scale, channels)
Zhi committed
432 433
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
434 435 436 437

    check((2, 4, 10, 10), 4)


438 439 440
def test_fold_bwd_fail():
    """Dual path testcase."""
    def fail1(x, conv_weight, out_bias, out_scale, channels):
441
        args = [x, conv_weight, out_bias]
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        out_bias = relay.expand_dims(out_bias, axis=1, num_newaxis=2)
        y1 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y1 = relay.nn.relu(y1)
        y2 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1),
                             out_layout="CNHW")
        # fold will fail because the axis from two path
        # differs from each other.
        y2 = relay.nn.relu(y2)
        y = relay.add(y1, y2)
        y = relay.multiply(y, out_scale)
        return relay.Function(args, y)

    def fail2(x, conv_weight, out_bias, out_scale, channels):
461
        args = [x, conv_weight, out_bias]
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        out_bias = relay.expand_dims(out_bias, axis=1, num_newaxis=2)
        y1 = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             padding=(1, 1))
        y2 = relay.nn.relu(y1)
        # fold will fail because y1 is referred also by y2
        y1 = relay.multiply(y1, out_scale)
        y = relay.add(y1, y2)
        return relay.Function(args, y)

    def check(shape, channels, fbefore):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        out_bias = relay.var("out_bias", shape=(channels,))
478
        out_scale = relay.const(_get_positive_scale((channels, 1, 1)))
479
        y1 = fbefore(x, weight, out_bias, out_scale, channels)
Zhi committed
480 481 482
        y1 = run_opt_pass(y1, transform.InferType())
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
        assert relay.analysis.alpha_equal(y1_folded, y1)
483 484 485 486 487

    check((4, 4, 10, 10), 4, fail1)
    check((4, 4, 10, 10), 4, fail2)


488 489 490 491 492 493 494 495 496 497
def test_fold_bwd_relu_fail():
    """testcase where we canont fold because scale can not pass relu"""
    def before(x, conv_weight, out_scale, channels):
        y = relay.nn.conv2d(x, conv_weight,
                             channels=channels,
                             kernel_size=(3, 3),
                             data_layout="NCHW",
                             padding=(1, 1))
        y = relay.nn.relu(y)
        y = relay.multiply(x, out_scale)
Zhi committed
498
        return relay.Function(relay.analysis.free_vars(y), y)
499 500 501 502 503 504

    def check(shape, channels, out_scale):
        x =  relay.var("x", shape=shape)
        in_channels = shape[1]
        weight = relay.var("weight")
        y1 = before(x, weight, out_scale, channels)
Zhi committed
505 506 507
        y1 = run_opt_pass(y1, transform.InferType())
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
        assert relay.analysis.alpha_equal(y1, y1_folded)
508 509 510 511 512 513 514

    out_scale = relay.var("in_scale", shape=(4, 1, 1))
    check((4, 4, 10, 10), 4, out_scale)
    out_scale = relay.const(np.random.uniform(size=(4, 1, 1), low=-1.0, high=0.0)).astype("float32")
    check((4, 4, 10, 10), 4, out_scale)


515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
def test_fold_bwd_negative_scale():
    """Testcase of folding negative scale"""
    def before(x, conv_weight, out_scale, channels):
        args = [x, conv_weight]
        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        y = relay.multiply(y, out_scale)
        return relay.Function(args, y)

    def expected(x, conv_weight, out_scale, channels):
        # use a fixed order of args so alpha equal check can pass
        args = [x, conv_weight]
        squeezed_scale = relay.squeeze(out_scale, axis=[1,2])
        conv_weight = relay.multiply(
            conv_weight , relay.expand_dims(squeezed_scale, axis=1, num_newaxis=3))
        y = relay.nn.conv2d(x, conv_weight,
                            channels=channels,
                            kernel_size=(3, 3),
                            padding=(1, 1))
        return relay.Function(args, y)

    def check(shape, channels):
        x =  relay.var("x", shape=shape)
        weight = relay.var("weight")
        out_scale = relay.const(-_get_positive_scale((channels, 1, 1)))
        y1 = before(x, weight, out_scale, channels)
Zhi committed
543
        y1 = run_opt_pass(y1, transform.InferType())
544 545
        type_dict = {x.name_hint:x.checked_type for x in y1.params}
        weight = relay.var("weight", type_dict["weight"])
Zhi committed
546
        y1_folded = run_opt_pass(y1, transform.BackwardFoldScaleAxis())
547
        y1_expected = expected(x, weight, out_scale, channels)
Zhi committed
548 549
        y1_expected = run_opt_pass(y1_expected, transform.InferType())
        assert relay.analysis.alpha_equal(y1_folded, y1_expected)
550 551 552 553

    check((2, 4, 10, 10), 8)


554 555 556 557
if __name__ == "__main__":
    test_fold_fwd_simple()
    test_fold_fwd_dual_path()
    test_fold_fwd_fail()
558
    test_fold_fwd_relu_fail()
559
    test_fold_fwd_negative_scale()
560 561
    test_fold_bwd_simple()
    test_fold_bwd_dual_path()
562
    test_fold_bwd_dual_consumer()
563
    test_fold_bwd_fail()
564
    test_fold_bwd_relu_fail()
565
    test_fold_bwd_negative_scale()