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 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
|
# Owner(s): ["module: inductor"]
import sys
import unittest
import torch
import torch._inductor
from torch._inductor.test_case import TestCase
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
IS_FBCODE,
parametrize,
)
from torch.testing._internal.inductor_utils import HAS_CPU, HAS_CUDA
from torch.testing._internal.triton_utils import requires_cuda
aten = torch.ops.aten
try:
try:
from .test_torchinductor import check_model, check_model_cuda
except ImportError:
from test_torchinductor import ( # @manual=fbcode//caffe2/test/inductor:test_inductor-library
check_model,
check_model_cuda,
)
except (unittest.SkipTest, ImportError) as e:
sys.stderr.write(f"{type(e)}: {e}\n")
if __name__ == "__main__":
sys.exit(0)
raise
inplace_bin_ops_under_test = [
torch._foreach_add_,
torch._foreach_mul_,
torch._foreach_sub_,
torch._foreach_div_,
]
bin_ops_under_test = [
torch._foreach_add,
torch._foreach_mul,
torch._foreach_sub,
torch._foreach_div,
torch._foreach_maximum,
torch._foreach_minimum,
torch._foreach_clamp_max,
torch._foreach_clamp_min,
aten._foreach_copy,
]
un_ops_under_test = [
torch._foreach_reciprocal,
torch._foreach_neg,
torch._foreach_sign,
torch._foreach_abs,
torch._foreach_sqrt,
torch._foreach_rsqrt,
]
compose_ops = [torch._foreach_addcdiv, torch._foreach_addcmul]
all_ops = parametrize(
"op", bin_ops_under_test + un_ops_under_test, name_fn=lambda f: f.__name__
)
bin_ops = parametrize("op", bin_ops_under_test, name_fn=lambda f: f.__name__)
inplace_bin_ops = parametrize(
"op", inplace_bin_ops_under_test, name_fn=lambda f: f.__name__
)
scalar_bin_ops = parametrize("op", bin_ops_under_test[:4], name_fn=lambda f: f.__name__)
scalar_tensor_bin_ops = parametrize(
"op", bin_ops_under_test[:2], name_fn=lambda f: f.__name__
)
decomp_ops = parametrize("op", compose_ops, name_fn=lambda f: f.__name__)
def gen_args(op):
if op in un_ops_under_test:
return (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
else:
return (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
@instantiate_parametrized_tests
class ForeachTests(TestCase):
check_model_cuda = check_model_cuda
check_model_cpu = check_model
check_kernel_count = True
def setUp(self):
super().setUp()
torch._inductor.metrics.reset()
def tearDown(self):
super().tearDown()
torch._inductor.metrics.reset()
def _test_single_list(self, op):
if op in un_ops_under_test:
def fn(a0, a1):
return op([a0, a1])
else:
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
self.check_model_cuda(
fn,
gen_args(op),
)
def _test_single_scalar(self, op):
def fn(a0, a1):
return op([a0, a1], 3.3)
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
def _test_single_scalar_tensor(self, op):
def fn(a0, a1):
return op([a0, a1], torch.tensor(3.3, device="cuda:0"))
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
# called in test_cuda_cpp_wrapper.py
@requires_cuda
def test_foreach_cpp_wrapper_cuda(self):
self._test_single_list(op=torch._foreach_add)
@requires_cuda
@all_ops
def test_single_list(self, op):
self._test_single_list(op)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_single_scalar(self, op):
self._test_single_scalar(op)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_tensor_bin_ops
def test_single_scalar_tensor(self, op):
self._test_single_scalar_tensor(op)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@all_ops
def test_scheduler_fusion_list(self, op):
if op in un_ops_under_test:
def fn(a0, a1):
c = op([a0, a1])
return torch._foreach_sqrt(c)
else:
def fn(a0, a1, b0, b1):
c = op([a0, a1], [b0, b1])
return c, torch._foreach_add([a0, a1], c)
self.check_model_cuda(
fn,
gen_args(op),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_scheduler_fusion_scalar(self, op):
def fn(a0, a1):
c = op([a0, a1], 3.4)
return c, torch._foreach_add([a0, a1], c)
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_broadcasting(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
fn_opt = torch._dynamo.optimize()(fn)
inputs = (
torch.rand(10, 1, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(1, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
actual = fn_opt(*inputs)
expected = fn(*inputs)
self.assertEqual(actual, expected)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@all_ops
def test_singleton_lists(self, op):
if op in un_ops_under_test:
def fn(a0):
return op([a0])
args = (torch.rand(10, 10, device="cuda:0"),)
else:
def fn(a0, b0):
return op([a0], [b0])
args = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
)
self.check_model_cuda(
fn,
args,
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@bin_ops
def test_type_promotion(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
fn_opt = torch._dynamo.optimize()(fn)
max32 = torch.iinfo(torch.int32).max
max64 = torch.iinfo(torch.int64).max
inputs = (
torch.randint(max32, (10, 10), device="cuda:0", dtype=torch.int32),
torch.randint(max32, (20, 20), device="cuda:0", dtype=torch.int32),
torch.randint(max32, (10, 10), device="cuda:0", dtype=torch.int32),
torch.randint(max64, (20, 20), device="cuda:0", dtype=torch.int64),
)
actual = fn_opt(*inputs)
expected = fn(*inputs)
self.assertEqual(actual, expected)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_kernel_split_arg_limit_list(self, op):
# NB: foeach_copy won't pass this test because it will dce one set of buffers
def fn(a, b):
return op(a, b)
fn_opt = torch._dynamo.optimize()(fn)
max_args = 370
max_list_len = (max_args // 3) + 1
inputs = (
[torch.rand(10, 10, device="cuda:0") for _ in range(max_list_len)],
[torch.rand(10, 10, device="cuda:0") for _ in range(max_list_len)],
)
actual = fn_opt(*inputs)
expected = fn(*inputs)
self.assertEqual(actual, expected)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@scalar_bin_ops
@unittest.skip(
"Triton recursion depth exceeded: https://github.com/openai/triton/issues/1763"
)
def test_kernel_split_arg_limit_scalar(self, op):
def fn(a):
return op(a, 3.3)
fn_opt = torch._dynamo.optimize()(fn)
max_args = 370
max_list_len = (max_args // 2) + 1
inputs = ([torch.rand(10, 10, device="cuda:0") for _ in range(max_list_len)],)
actual = fn_opt(*inputs)
expected = fn(*inputs)
self.assertEqual(actual, expected)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@bin_ops
def test_fusion_duplicate_buffer_list(self, op):
def fn(a0, a1, b0, b1):
c = op([a0, a1], [b0, b1])
return op([a0, b0], [c[0], c[0]])
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
reference_in_float=False,
check_lowp=False,
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@all_ops
def test_non_foreach_consumer_list(self, op):
if op in un_ops_under_test:
def fn(a0, a1):
c = op([a0, a1])
return torch.mul(c[0], a0)
else:
def fn(a0, a1, b0, b1):
c = op([a0, a1], [b0, b1])
return torch.mul(c[0], a0)
self.check_model_cuda(
fn,
gen_args(op),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_non_foreach_consumer_scalar(self, op):
def fn(a0, a1):
c = op([a0, a1], 4.7)
return torch.mul(c[0], a0)
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@all_ops
def test_non_foreach_producer_list(self, op):
if op in un_ops_under_test:
def fn(a0, a1):
c0 = torch.add(a0, a0)
c1 = torch.add(a1, a1)
return op([c0, c1])
else:
def fn(a0, a1, b0, b1):
c0 = torch.add(a0, b0)
c1 = torch.add(a1, b1)
return op([a0, a1], [c0, c1])
self.check_model_cuda(
fn, gen_args(op), reference_in_float=False, check_lowp=False
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_non_foreach_producer_scalar(self, op):
def fn(a0, a1, b0, b1):
c0 = torch.mul(a0, b0)
c1 = torch.mul(a1, b1)
return op([c0, c1], 5.6)
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@all_ops
def test_non_foreach_consumer_producer_list(self, op):
if op in un_ops_under_test:
def fn(a0, a1):
c0 = torch.add(a0, a0)
c1 = torch.mul(a1, a1)
d = op([c0, c1])
e0 = torch.mul(d[0], a0)
e1 = torch.mul(d[1], a1)
return [e0, e1]
else:
def fn(a0, a1, b0, b1):
c0 = torch.add(a0, b0)
c1 = torch.add(a1, b1)
d = op([a0, a1], [c0, c1])
e0 = torch.mul(d[0], a0)
e1 = torch.mul(d[1], a1)
return [e0, e1]
self.check_model_cuda(
fn,
gen_args(op),
reference_in_float=False,
check_lowp=False,
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@scalar_bin_ops
def test_non_foreach_consumer_producer_scalar(self, op):
def fn(a0, a1, b0, b1):
c0 = torch.add(a0, b0)
c1 = torch.add(a1, b1)
d = op([c0, c1], 5.8)
e0 = torch.mul(d[0], a0)
e1 = torch.mul(d[1], a1)
return [e0, e1]
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
reference_in_float=False,
check_lowp=False,
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@bin_ops
@torch._dynamo.config.patch("automatic_dynamic_shapes", False)
@torch._dynamo.config.patch("assume_static_by_default", False)
def test_dynamic_shapes_fallback(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@torch._dynamo.config.patch("automatic_dynamic_shapes", False)
@torch._dynamo.config.patch("assume_static_by_default", False)
@torch._inductor.config.patch("combo_kernel_foreach_dynamic_shapes", True)
def test_enable_dynamic_shapes_python_wrapper(self, op=torch._foreach_add):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@torch._dynamo.config.patch("automatic_dynamic_shapes", False)
@torch._dynamo.config.patch("assume_static_by_default", False)
@torch._inductor.config.patch("combo_kernel_foreach_dynamic_shapes", True)
@torch._inductor.config.patch("cpp_wrapper", True)
def test_enable_dynamic_shapes_cpp_wrapper_cuda(self, op=torch._foreach_add):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs)
@unittest.skipIf(IS_FBCODE, "cpp compile not supported in fbcode")
@bin_ops
def test_cpu_cpp_fallback(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
inputs = (
torch.rand(10, 10, device="cpu"),
torch.rand(20, 20, device="cpu"),
torch.rand(10, 10, device="cpu"),
torch.rand(20, 20, device="cpu"),
)
self.check_model_cpu(fn, inputs)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@decomp_ops
def test_decomp(self, op):
def fn(a0, a1, b0, b1, c0, c1):
return op([a0, a1], [b0, b1], [c0, c1], value=0.5)
self.check_model_cuda(
fn,
(
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
def test_fuse_concat(self):
def fn(x1, x2, x3, w1, w2, w3):
x = torch.stack([x1, x2, x3])
w = torch.stack([w1, w2, w3])
y = torch.bmm(x, w)
return y
x1 = torch.randn(5, 4).cuda()
x2 = x1 + 1
x3 = x1 + 2
w1 = torch.randn(4, 3).cuda()
w2 = w1 + 1
w3 = w1 + 2
args = (x1, x2, x3, w1, w2, w3)
self.check_model_cuda(fn, args)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
def test_zero_elems(self):
def fn(a0, a1, b0, b1):
return torch._foreach_add([a0, a1], [b0, b1])
self.check_model_cuda(
fn,
(
torch.rand(0, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(0, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@bin_ops
def test_2d_blocking(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
self.check_model_cuda(
fn,
(
torch.rand(10, 40, device="cuda:0"),
torch.rand(10, 30, device="cuda:0"),
torch.rand(40, 10, device="cuda:0").t(),
torch.rand(30, 10, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@bin_ops
def test_2d_blocking_partitioning(self, op):
def fn(a0, a1, b0, b1):
return op([a0, a1], [b0, b1])
self.check_model_cuda(
fn,
(
torch.rand(30, 20, device="cuda:0"),
torch.rand(40, 30, device="cuda:0"),
torch.rand(30, 20, device="cuda:0"),
torch.rand(30, 40, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@bin_ops
def test_2d_blocking_partitioning_elems(self, op):
"""2D blocking should be grouped by number of yelems"""
def fn(a0, a1, a2, b0, b1, b2):
return op([a0, a1, a2], [b0, b1, b2])
self.check_model_cuda(
fn,
(
torch.rand(10, 20, device="cuda:0"),
torch.rand(30, 20, device="cuda:0"),
torch.rand(10, 30, device="cuda:0"),
torch.rand(20, 10, device="cuda:0").t(),
torch.rand(20, 30, device="cuda:0").t(),
torch.rand(30, 10, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@bin_ops
@torch._inductor.config.patch("combo_kernel_allow_mixed_sizes", 2)
def test_2d_blocking_partitioning_mixed_sizes(self, op):
"""2D blocking with mixed sizes should group together"""
def fn(a0, a1, a2, b0, b1, b2):
return op([a0, a1, a2], [b0, b1, b2])
self.check_model_cuda(
fn,
(
torch.rand(10, 20, device="cuda:0"),
torch.rand(30, 20, device="cuda:0"),
torch.rand(10, 30, device="cuda:0"),
torch.rand(20, 10, device="cuda:0").t(),
torch.rand(20, 30, device="cuda:0").t(),
torch.rand(30, 10, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@inplace_bin_ops
def test_reinplacing(self, op):
def fn(a0, a1, b0, b1):
op([a0, a1], [b0, b1])
return [a0, a1]
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs, check_lowp=False)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@inplace_bin_ops
def test_reinplacing_mut_before(self, op):
def fn(a0, a1, b0, b1):
a0.add_(torch.ones(10, 10, device="cuda:0"))
op([a0, a1], [b0, b1])
return [a0, a1]
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs, check_lowp=False)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
@inplace_bin_ops
def test_reinplacing_mut_after(self, op):
def fn(a0, a1, b0, b1):
op([a0, a1], [b0, b1])
a0.add_(torch.ones(10, 10, device="cuda:0"))
return [a0, a1]
inputs = (
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
torch.rand(10, 10, device="cuda:0"),
torch.rand(20, 20, device="cuda:0"),
)
self.check_model_cuda(fn, inputs, check_lowp=False)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
@requires_cuda
def test_multi_device(self):
def test_foreach_add(a0, a1, b0, b1):
return torch._foreach_add([a0, a1], [b0, b1])
inps = [
torch.ones(10, 10, device="cuda"),
torch.ones(20, 20, device="cpu"),
torch.zeros(10, 10, device="cuda"),
torch.zeros(20, 20, device="cpu"),
]
out_eager = test_foreach_add(*inps)
out_compiled = torch.compile(test_foreach_add)(*inps)
self.assertEqual(out_eager, out_compiled)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
def test_aliasing(self):
def test_foreach_add(a0, a1, a2, b0, b1, b2):
return torch._foreach_add_([a0, a1, a2], [b0, b1, b2])
input = torch.ones(10, 10, device="cuda")
input2 = torch.ones(10, 10, device="cuda")
inps = [
input,
input.view(10, 10),
input.view(10, 10),
input2,
input2.view(10, 10),
input2.view(10, 10),
]
out_eager = test_foreach_add(*inps)
out_compiled = torch.compile(test_foreach_add)(*inps)
self.assertEqual(out_eager, out_compiled)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 4)
@requires_cuda
@torch._inductor.config.patch("combo_kernel_allow_mixed_sizes", 1)
def test_2d_block_no_mixed_sizes_no_mask(self):
"""2D blocking with no mixed sizes constant mask"""
def fn(a0, a1, a2, b0, b1, b2):
return torch._foreach_add([a0, a1, a2], [b0, b1, b2])
self.check_model_cuda(
fn,
(
torch.rand(1024, 2048, device="cuda:0"),
torch.rand(2048, 2048, device="cuda:0"),
torch.rand(1024, 2048, device="cuda:0"),
torch.rand(2048, 1024, device="cuda:0").t(),
torch.rand(2048, 2048, device="cuda:0").t(),
torch.rand(2048, 1024, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 2)
@requires_cuda
@torch._inductor.config.patch("combo_kernel_allow_mixed_sizes", 2)
def test_2d_block_mixed_sizes_with_mask(self):
"""2D blocking with mixed sizes should have mask"""
def fn(a0, a1, a2, b0, b1, b2):
return torch._foreach_add([a0, a1, a2], [b0, b1, b2])
self.check_model_cuda(
fn,
(
torch.rand(1024, 2048, device="cuda:0"),
torch.rand(2048, 2048, device="cuda:0"),
torch.rand(1024, 2048, device="cuda:0"),
torch.rand(2048, 1024, device="cuda:0").t(),
torch.rand(2048, 2048, device="cuda:0").t(),
torch.rand(2048, 1024, device="cuda:0").t(),
),
)
self.assertEqual(torch._inductor.metrics.generated_kernel_count, 1)
if __name__ == "__main__":
from torch._inductor.test_case import run_tests
if HAS_CPU or HAS_CUDA:
run_tests(needs="filelock")
|