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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 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 395 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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
# Owner(s): ["module: fx.passes"]
from dataclasses import dataclass
import operator
import logging
import torch
from torch.fx._symbolic_trace import symbolic_trace
from torch.fx.passes.infra.partitioner import CapabilityBasedPartitioner
from torch.fx.passes.operator_support import OperatorSupport
from torch.fx.passes.utils.fuser_utils import fuse_by_partitions
from torch.fx.passes.utils.matcher_utils import SubgraphMatcher
from torch.testing._internal.common_utils import run_tests, parametrize, instantiate_parametrized_tests
from torch.testing._internal.jit_utils import JitTestCase
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
class TestModule(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(4, 4)
self.linear2 = torch.nn.Linear(4, 4)
self.param = torch.nn.Parameter(torch.rand(4, 4))
def forward(self, a, b, c):
add = a + b
linear_1 = self.linear(add)
add_1 = add + c
add_2 = add_1 + self.param
add_3 = add_1 + linear_1
add_4 = add_2 + add_3
linear_2 = self.linear2(add_4)
add_5 = linear_2 + add_4
add_6 = add_5 + a
relu = add_6.relu()
return add_4, add_6, relu
class TestPartitionFunctions:
@staticmethod
def forward1(a, b, c):
add = a + b
add_1 = add + b
add_2 = add_1 + c
relu_1 = add_2.relu()
add_3 = add_1 + add_2
add_4 = add_1 + relu_1 + add_3
relu_2 = add_4.relu()
add_5 = relu_2 + add_4
add_6 = add_5 + add_4
return add_4, add_6
@staticmethod
def forward2(a, b, _):
add = a + b
add_1 = add + b
relu_1 = add_1.relu() # blocked by this
add_3 = add_1 + relu_1
add_4 = add_1 + add_3
return add_4, add_1
@staticmethod
def forward3(a, b, c):
add = a + b
add_1 = a + c
add_2 = b + c
return add, add_1, add_2
@staticmethod
def forward4(a, b, c):
add = a + b
add_1 = a + c
add_2 = b + c
return torch.where(add > 0, add_1, add_2)
@staticmethod
def forward5(a, b, c):
# add should be fused right branch, as left branch is not supported
add = a + 1
# left branch
relu = add.relu()
# right branch
add_1 = add + 2
return relu, add_1
@staticmethod
def forward6(a, b, c):
# add should have its own partition, as neither branchs are supported
add = a + 1
# left branch
relu = add.relu()
# right branch
relu_1 = add.relu()
return relu, relu_1
@staticmethod
def forward7(a, b, c):
# both branches are supported, all adds should be fused together
add = a + 1
# left branch
add_1 = add + 2
# right branch is larger
add_2 = add + 1
add_3 = add_2 + 1
return add_3, add_1
@staticmethod
def forward8(a, b, c):
# both branches are in the same partition, add should join the same partition
add = a + 1
# left branch
add_1 = add + 2
# right branch
add_2 = add + 1
# left and right branch merges
add_3 = add_2 + add_1
return add_3
@staticmethod
def forward9(a, b, c):
add = a + 1
# branch 1
add_1 = add + 1
# branch 2
add_2 = add + 1
# branch_3
add_3 = add + 1
out = torch.stack([add_1, add_2, add_3])
return out
@staticmethod
def forward10(a, b, c):
add = a + 1
# branch 1
add_1 = add + 1
# branch 2
add_2 = add + 1
# branch 3: depends on branch 2
add_3 = add + add_2
out = torch.stack([add_1, add_2, add_3])
return out
@staticmethod
def forward11(a, b, c):
add = a + 1
# branch 1
add_1 = add.relu()
# branch 2 depends on branch 1
add_2 = add + add_1
# branch 3
add_3 = add.relu()
out = torch.stack([add_1, add_2, add_3])
return out
# A mock OperatorSupport class, where only operator.add is supported
class MockOperatorSupport(OperatorSupport):
def is_node_supported(self, submodules, node: torch.fx.Node) -> bool:
return node.op == "call_function" and node.target in {operator.add}
@instantiate_parametrized_tests
class TestFXGraphPasses(JitTestCase):
@parametrize("fn, expected_partition", [
(TestPartitionFunctions.forward1, [["add_7", "add_6"], ["add_5", "add_4", "add_3"], ["add_2", "add_1", "add"]]),
(TestPartitionFunctions.forward2, [["add_3", "add_2"], ["add_1", "add"]]),
# 2 branches cases
(TestPartitionFunctions.forward5, [["add_1", "add"]]),
(TestPartitionFunctions.forward6, [["add"]]),
(TestPartitionFunctions.forward7, [["add_3", "add_2", "add", "add_1"]]),
(TestPartitionFunctions.forward8, [["add_3", "add_2", "add", "add_1"]]),
# 3 branch cases
(TestPartitionFunctions.forward9, [['add_3', 'add_2', 'add_1', 'add']]),
(TestPartitionFunctions.forward10, [['add_3', 'add_2', 'add', 'add_1']]),
(TestPartitionFunctions.forward11, [['add_1'], ['add']]),
])
def test_partitioner(self, fn, expected_partition):
traced = symbolic_trace(fn)
supported_ops = MockOperatorSupport()
partitioner = CapabilityBasedPartitioner(traced, supported_ops, allows_single_node_partition=True)
partitions = partitioner.propose_partitions()
partitions_name = [[node.name for node in partition.nodes] for partition in partitions]
assert len(partitions_name) == len(expected_partition)
for i in range(len(partitions_name)):
assert set(partitions_name[i]) == set(expected_partition[i])
fused_graph = partitioner.fuse_partitions(partitions)
a, b, c = torch.rand(4), torch.rand(4), torch.rand(4)
expected = fn(a, b, c)
result = fused_graph(a, b, c)
torch.testing.assert_close(expected, result)
@parametrize("fn, expected_partition", [
# horizontal fusion without a common downstream node, not supported yet
(TestPartitionFunctions.forward3, [["add_2", "add_1", "add"]]),
# horizontal fusion with a common downstream node, not supported yet
(TestPartitionFunctions.forward4, [["add_2", "add_1", "add"]]),
])
def test_partitioner_xfail(self, fn, expected_partition):
traced = symbolic_trace(fn)
supported_ops = MockOperatorSupport()
partitioner = CapabilityBasedPartitioner(traced, supported_ops, allows_single_node_partition=True)
partitions = partitioner.propose_partitions()
partitions_name = [[node.name for node in partition.nodes] for partition in partitions]
with self.assertRaises(Exception):
assert len(partitions_name) == len(expected_partition)
@parametrize("partition", [
[['add', 'add_1'], ['add_5', 'add_6']],
[['add', 'add_1', 'add_2']], # vertical fusion
[['add_2', 'add_3']], # horizontal fusion
[['add_3', 'add_4']],
[['add_6', 'add_5']], # arbitray node order
[['add_4', 'add_1', 'add_3', 'add_2']], # arbitray node order
[['add_5', 'add_6'], ['add_1', 'add_2', 'add_3', 'add_4']], # arbitray partition order
[['add_5', 'linear2']], # includes call_function + call_module node
[['add_6', 'relu']], # includes call_function + call_module node
[['param', 'add_2']], # includes get_attr + call_module nodes
[['param', 'add_1', 'linear']], # includes get_attr + call_function + call_module nodes
[["add", "linear", "add_1", "param", "add_2", "add_3", "add_4", "linear2", "add_5", "add_6", "relu"]], # full graph
])
def test_fuser_util(self, partition):
m = TestModule()
gm = symbolic_trace(m)
nodes_by_name = {node.name : node for node in gm.graph.nodes}
partitions = []
for node_names in partition:
partitions.append([nodes_by_name[name] for name in node_names])
fused_graph = fuse_by_partitions(gm, partitions)
a, b, c = torch.rand(4), torch.rand(4), torch.rand(4)
expected = m(a, b, c)
result = fused_graph(a, b, c)
torch.testing.assert_close(expected, result)
@parametrize("partition", [
[['add', 'add_1'], ['add_1', 'add_5', 'add_6']], # add_1 exists in multiple partitions
[['add', 'add_1', 'add_3']], # invalid partition: circular dependency
[['add_4', 'add_5']], # invalid partition: circular dependency
[['relu', 'add_5']], # invalid partition: circular dependency
])
def test_fuser_util_xfail(self, partition):
m = TestModule()
gm = symbolic_trace(m)
nodes_by_name = {node.name : node for node in gm.graph.nodes}
partitions = []
for node_names in partition:
partitions.append([nodes_by_name[name] for name in node_names])
with self.assertRaises(Exception):
fuse_by_partitions(gm, partitions)
@dataclass
class TestCase:
match_output: bool
match_placeholder: bool
num_matches: int
remove_overlapping_matches: bool = True
class SingleNodePattern:
@staticmethod
def forward(x):
val = torch.neg(x)
return torch.add(val, val)
@staticmethod
def pattern(a):
return torch.neg(a)
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 0),
TestCase(False, True, 1),
TestCase(True, True, 0)
]
class SimplePattern:
@staticmethod
def forward(x, w1, w2):
m1 = torch.cat([w1, w2]).sum()
m2 = torch.cat([w2, w1]).sum()
m3 = torch.cat([m1, m2]).sum()
return x + torch.max(m1) + torch.max(m2) + m3
@staticmethod
def pattern(a, b):
return torch.cat([a, b]).sum()
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 3),
TestCase(True, False, 0),
TestCase(False, True, 2),
TestCase(True, True, 0)
]
class SimpleFullGraphMatching:
@staticmethod
def forward(x):
a = torch.neg(x)
return torch.add(a, a)
@staticmethod
def pattern(x):
a = torch.neg(x)
return torch.add(a, a)
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 1),
TestCase(False, True, 1),
TestCase(True, True, 1)
]
class DiamondShapePatternTestCase:
@staticmethod
def forward(x):
a = torch.neg(x)
a = a.relu()
left = a.sigmoid()
right = a.relu()
out = left + right
return out
@staticmethod
def pattern(a):
a = a.relu()
left = a.sigmoid()
right = a.relu()
out = left + right
return out
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 1),
TestCase(False, True, 0),
TestCase(True, True, 0)
]
class NonFullyContainedMatches:
@staticmethod
def forward(x, w1, w2, b1, b2):
# fully contained matched subgraph
m1 = torch.cat([w1, w2])
m2 = torch.cat([x, b2])
t0 = torch.addmm(b1, m1, m2.t())
t0_sum = torch.sum(t0) # use of t0 is not leaking
# leaking matched subgraph, m3 is leaked
m3 = torch.cat([w1, w2])
m4 = torch.cat([x, b2])
t1 = torch.addmm(b1, m3, m4.t())
m3_sum = torch.sum(m3)
return t0_sum, m3_sum
@staticmethod
def pattern(x, w1, w2, b1, b2):
m1 = torch.cat([w1, w2])
m2 = torch.cat([x, b2])
return torch.addmm(b1, m1, m2.t())
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 0),
TestCase(False, True, 1), # leaked used of placeholder is not leaking
]
class ChainRepeatedPattern:
@staticmethod
def forward(x):
x = torch.sigmoid(x)
x = torch.sigmoid(x)
x = torch.sigmoid(x)
return torch.sigmoid(x)
@staticmethod
def pattern(x):
return torch.sigmoid(torch.sigmoid(x))
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 3, remove_overlapping_matches=False),
TestCase(False, False, 2, remove_overlapping_matches=True),
TestCase(True, False, 1),
TestCase(False, True, 1),
TestCase(True, True, 0)
]
class QuantizationModel:
@staticmethod
def forward(x):
x += 3
x = x.dequantize()
x = torch.sigmoid(x)
x = x.to(torch.float16)
return x
@staticmethod
def pattern(x):
x = x.dequantize()
x = torch.sigmoid(x)
x = x.to(torch.float16)
return x
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 1),
TestCase(False, True, 0),
TestCase(True, True, 0)
]
class MultipleOutputsWithDependency:
@staticmethod
def forward(x):
y = x.relu()
z = y.sigmoid()
return z, y
@staticmethod
def pattern(a):
b = a.relu()
c = b.sigmoid()
return b, c # outputs have data dependency
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 0),
TestCase(False, True, 1),
TestCase(True, True, 0)
]
class MultipleOutputsWithoutDependency:
@staticmethod
def forward(x):
x = x + 1
# target subgraph to match
x = x.relu()
z = x.sum()
y = x.sigmoid()
out = y.sigmoid() + z.sum()
return out
@staticmethod
def pattern(a):
a = a.relu()
b = a.sigmoid()
c = a.sum()
return b, c
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 0),
TestCase(False, True, 0),
TestCase(True, True, 0)
]
class MultipleOutputsMultipleOverlappingMatches:
@staticmethod
def forward(x):
x = x + 1
# target subgraph to match
x = x.relu()
z = x.sum()
z1 = x.sum()
y = x.sigmoid()
y1 = x.sigmoid()
return z + z1 + y + y1
@staticmethod
def pattern(a):
a = a.relu()
b = a.sigmoid()
c = a.sum()
return a, b, c
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 4, remove_overlapping_matches=False),
TestCase(False, False, 1, remove_overlapping_matches=True),
]
class MultipleOutputsMultipleNonOverlappingMatches:
@staticmethod
def forward(x):
x = x + 1
# target subgraph to match
x = x.relu()
z = x.sum()
y = x.sigmoid()
x = x.relu()
z1 = x.sum()
y1 = x.sigmoid()
return z + z1 + y + y1
@staticmethod
def pattern(a):
a = a.relu()
b = a.sigmoid()
c = a.sum()
return b, c
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
]
class MultipleOutputsIdenticalAnchor:
@staticmethod
def forward(x):
x = x + 1
# target subgraph to match
x = x.relu()
y = x.sigmoid()
y1 = x.sigmoid()
return y, y1
@staticmethod
def pattern(a):
a = a.relu()
b = a.sigmoid()
b1 = a.sigmoid()
return b, b1
test_cases = [
# match_output, match_placeholder, num_matches
# (False, False, 2), # FIXME: currently still matches to 2, should fix to 1
TestCase(True, False, 1),
TestCase(False, True, 0),
]
class MultipleOutputsHorizontalPattern:
@staticmethod
def forward(x):
x = x + 1
# target subgraph to match
y1 = x.relu()
y2 = x.sigmoid()
return y1, y2
@staticmethod
def pattern(a):
b1 = a.relu()
b2 = a.sigmoid()
return b1, b2
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
TestCase(True, False, 1),
TestCase(False, True, 0),
TestCase(True, True, 0)
]
class MultiOutputWithWithInvalidMatches:
@staticmethod
def forward(x):
res0 = torch.nn.functional.linear(x, torch.rand(3, 3))
res1 = torch.sigmoid(res0)
res2 = res0 * res1
res3 = torch.sum(res2, dim=1)
return res3
@staticmethod
def pattern(a, b, c):
lin_res = torch.nn.functional.linear(a, b)
mul_res = lin_res * c
return lin_res, mul_res
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 0),
TestCase(True, False, 0),
TestCase(False, True, 0),
]
class QuantizationFp8Pattern:
@classmethod
def setup(cls):
cls.quantization = torch.library.Library("fp8_quantization", "DEF")
cls.quantization.define("quantize_per_tensor_affine_fp8(Tensor self, int dtype, float scale) -> Tensor")
cls.quantization.define("dequantize_per_tensor_affine_fp8(Tensor self, int dtype, float scale) -> Tensor")
@classmethod
def tearDown(cls):
del cls.quantization
@staticmethod
def forward(self, arg0_1, arg1_1):
qt = torch.ops.fp8_quantization
_scale_0 = self._scale_0
quantize_per_tensor_affine_fp8 = qt.quantize_per_tensor_affine_fp8(arg0_1, 0, _scale_0)
dequantize_per_tensor_affine_fp8 = qt.dequantize_per_tensor_affine_fp8(quantize_per_tensor_affine_fp8, 0, _scale_0)
_scale_1 = self._scale_0
quantize_per_tensor_affine_fp8_1 = qt.quantize_per_tensor_affine_fp8(arg1_1, 0, _scale_1)
dequantize_per_tensor_affine_fp8_1 = qt.dequantize_per_tensor_affine_fp8(quantize_per_tensor_affine_fp8_1, 0, _scale_1)
add = torch.ops.aten.add.Tensor(dequantize_per_tensor_affine_fp8, dequantize_per_tensor_affine_fp8_1)
_scale_2 = self._scale_0
quantize_per_tensor_affine_fp8_2 = qt.quantize_per_tensor_affine_fp8(add, 0, _scale_2)
dequantize_per_tensor_affine_fp8_2 = qt.dequantize_per_tensor_affine_fp8(quantize_per_tensor_affine_fp8_2, 0, _scale_2)
return dequantize_per_tensor_affine_fp8_2
@staticmethod
def pattern(a, a_dtype, a_scale, b, b_dtype, b_scale, out_scale):
qt = torch.ops.fp8_quantization
a = qt.dequantize_per_tensor_affine_fp8(a, a_dtype, a_scale)
b = qt.dequantize_per_tensor_affine_fp8(b, b_dtype, b_scale)
output = torch.ops.aten.add.Tensor(a, b)
qt.dequantize_per_tensor_affine_fp8
output = qt.quantize_per_tensor_affine_fp8(output, a_dtype, out_scale)
return output
test_cases = [
# match_output, match_placeholder, num_matches
TestCase(False, False, 1),
]
@instantiate_parametrized_tests
class TestFXMatcherUtils(JitTestCase):
@parametrize("test_model", [
SingleNodePattern,
SimplePattern,
SimpleFullGraphMatching,
DiamondShapePatternTestCase,
NonFullyContainedMatches,
ChainRepeatedPattern,
QuantizationModel,
MultipleOutputsWithDependency,
MultipleOutputsWithoutDependency,
MultipleOutputsMultipleOverlappingMatches,
MultipleOutputsMultipleNonOverlappingMatches,
MultipleOutputsIdenticalAnchor,
MultipleOutputsHorizontalPattern,
MultiOutputWithWithInvalidMatches,
QuantizationFp8Pattern,
])
def test_subgraph_matcher(self, test_model):
setup = getattr(test_model, "setup", None)
if callable(setup):
setup()
traced = symbolic_trace(test_model.forward)
pattern_traced = symbolic_trace(test_model.pattern)
for test_case in test_model.test_cases:
matcher = SubgraphMatcher(pattern_traced.graph,
match_output=test_case.match_output,
match_placeholder=test_case.match_placeholder,
remove_overlapping_matches=test_case.remove_overlapping_matches)
matches = matcher.match(traced.graph)
assert len(matches) == test_case.num_matches
for match in matches:
for node in pattern_traced.graph.nodes:
if not test_case.match_placeholder and node.op == "placeholder":
continue
if not test_case.match_output and node.op == "output":
continue
assert node in match.nodes_map
tearDown = getattr(test_model, "tearDown", None)
if callable(setup):
tearDown()
if __name__ == "__main__":
run_tests()
|