Merged
fix(examples): Separate dynamic logic from elementwise_ops and complete dynamic usage examples #1220
shengxinchenAdd on Feb 27
fix(examples): Separate dynamic logic from elementwise_ops and complete dynamic usage examples #1220
Merged
Merged fromshengxinchen/pyptofix/2_examples-260227intocann/pyptomaster
Total: 2 files changed+483-202
| @@ -99,14 +99,7 @@ def test_abs_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 99 | # ============================================================================ | 99 | # ============================================================================ |
| 100 | 100 | ||
| 101 | 101 | ||
| 102 | -def create_add_op_kernel(a_shape: tuple, b_shape: tuple, run_mode: str = "npu", dynamic: bool = False): | 102 | +def create_add_op_kernel(a_shape: tuple, b_shape: tuple, run_mode: str = "npu"): |
| 103 | - if dynamic: | ||
| 104 | - a_shape = pypto.frontend.dynamic("a_shape") | ||
| 105 | - b_shape = pypto.frontend.dynamic("b_shape") | ||
| 106 | - else: | ||
| 107 | - a_shape = a_shape | ||
| 108 | - b_shape = b_shape | ||
| 109 | - | ||
| 110 | if run_mode == "npu": | 103 | if run_mode == "npu": |
| 111 | mode = pypto.RunMode.NPU | 104 | mode = pypto.RunMode.NPU |
| 112 | elif run_mode == "sim": | 105 | elif run_mode == "sim": |
| @@ -120,8 +113,8 @@ def create_add_op_kernel(a_shape: tuple, b_shape: tuple, run_mode: str = "npu", | |||
| 120 | b: pypto.Tensor(b_shape, pypto.DT_FP32), | 113 | b: pypto.Tensor(b_shape, pypto.DT_FP32), |
| 121 | ) -> pypto.Tensor(a_shape, pypto.DT_FP32): | 114 | ) -> pypto.Tensor(a_shape, pypto.DT_FP32): |
| 122 | pypto.set_vec_tile_shapes(2, 8) | 115 | pypto.set_vec_tile_shapes(2, 8) |
| 123 | - out = pypto.add(a, b) | 116 | + output = pypto.add(a, b) |
| 124 | - return out | 117 | + return output |
| 125 | 118 | ||
| 126 | return add_kernel | 119 | return add_kernel |
| 127 | 120 | ||
| @@ -147,16 +140,10 @@ def test_add_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 147 | print("✓ Basic usage of add function completed successfully") | 140 | print("✓ Basic usage of add function completed successfully") |
| 148 | 141 | ||
| 149 | 142 | ||
| 150 | -def create_add_broadcast_op_kernel(a_shape: tuple, b_shape: tuple, | 143 | +def create_add_broadcast_op_kernel(a_shape: tuple, b_shape: tuple, |
| 151 | - run_mode: str = "npu", | 144 | + run_mode: str = "npu"): |
| 152 | - dynamic: bool = False): | 145 | + m, n = a_shape |
| 153 | - if dynamic: | 146 | + b_shape = b_shape |
| 154 | - m = pypto.frontend.dynamic("m") | ||
| 155 | - n = a_shape[1] | ||
| 156 | - b_shape = pypto.frontend.dynamic("b_shape") | ||
| 157 | - else: | ||
| 158 | - m, n = a_shape | ||
| 159 | - b_shape = b_shape | ||
| 160 | 147 | ||
| 161 | if run_mode == "npu": | 148 | if run_mode == "npu": |
| 162 | mode = pypto.RunMode.NPU | 149 | mode = pypto.RunMode.NPU |
| @@ -198,12 +185,7 @@ def test_add_broadcast(device_id: int = None, run_mode: str = "npu"): | |||
| 198 | print("✓ Test Broadcasting Between Tensors completed successfully") | 185 | print("✓ Test Broadcasting Between Tensors completed successfully") |
| 199 | 186 | ||
| 200 | 187 | ||
| 201 | -def create_add_scalar_op_kernel(shape: tuple, scalar: float, run_mode: str = "npu", | 188 | +def create_add_scalar_op_kernel(shape: tuple, scalar: float, run_mode: str = "npu"): |
| 202 | - dynamic: bool = False): | ||
| 203 | - if dynamic: | ||
| 204 | - shape = pypto.frontend.dynamic("shape") | ||
| 205 | - else: | ||
| 206 | - shape = shape | ||
| 207 | 189 | ||
| 208 | if run_mode == "npu": | 190 | if run_mode == "npu": |
| 209 | mode = pypto.RunMode.NPU | 191 | mode = pypto.RunMode.NPU |
| @@ -244,14 +226,8 @@ def test_add_scalar(device_id: int = None, run_mode: str = "npu"): | |||
| 244 | print("✓ Test Adding a scalar to a tensor completed successfully") | 226 | print("✓ Test Adding a scalar to a tensor completed successfully") |
| 245 | 227 | ||
| 246 | 228 | ||
| 247 | -def create_add_with_alpha_op_kernel(a_shape: tuple, b_shape: tuple, | 229 | +def create_add_with_alpha_op_kernel(a_shape: tuple, b_shape: tuple, |
| 248 | - alpha: float, run_mode: str = "npu", dynamic: bool = False): | 230 | + alpha: float, run_mode: str = "npu"): |
| 249 | - if dynamic: | ||
| 250 | - a_shape = pypto.frontend.dynamic("a_shape") | ||
| 251 | - b_shape = pypto.frontend.dynamic("b_shape") | ||
| 252 | - else: | ||
| 253 | - a_shape = a_shape | ||
| 254 | - b_shape = b_shape | ||
| 255 | 231 | ||
| 256 | if run_mode == "npu": | 232 | if run_mode == "npu": |
| 257 | mode = pypto.RunMode.NPU | 233 | mode = pypto.RunMode.NPU |
| @@ -299,20 +275,11 @@ def test_add_with_alpha(device_id: int = None, run_mode: str = "npu"): | |||
| 299 | # ============================================================================ | 275 | # ============================================================================ |
| 300 | 276 | ||
| 301 | 277 | ||
| 302 | -def create_clip_op_kernel(a_shape: tuple, min_shape: tuple, | 278 | +def create_clip_op_kernel(a_shape: tuple, min_shape: tuple, |
| 303 | - max_shape: tuple, run_mode: str = "npu", | 279 | + max_shape: tuple, run_mode: str = "npu"): |
| 304 | - dynamic: bool = False): | 280 | + m, n = a_shape |
| 305 | - if dynamic: | 281 | + min_m, min_n = min_shape |
| 306 | - m = pypto.frontend.dynamic("m") | 282 | + max_m, max_n = max_shape |
| 307 | - n = a_shape[1] | ||
| 308 | - min_m = pypto.frontend.dynamic("min_m") | ||
| 309 | - min_n = min_shape[1] | ||
| 310 | - max_m = pypto.frontend.dynamic("max_m") | ||
| 311 | - max_n = max_shape[1] | ||
| 312 | - else: | ||
| 313 | - m, n = a_shape | ||
| 314 | - min_m, min_n = min_shape | ||
| 315 | - max_m, max_n = max_shape | ||
| 316 | 283 | ||
| 317 | if run_mode == "npu": | 284 | if run_mode == "npu": |
| 318 | mode = pypto.RunMode.NPU | 285 | mode = pypto.RunMode.NPU |
| @@ -355,17 +322,10 @@ def test_clip_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 355 | print("✓ Basic usage of clip function completed successfully") | 322 | print("✓ Basic usage of clip function completed successfully") |
| 356 | 323 | ||
| 357 | 324 | ||
| 358 | -def create_clip_broadcast_op_kernel(a_shape: tuple, min_shape: tuple, | 325 | +def create_clip_broadcast_op_kernel(a_shape: tuple, min_shape: tuple, |
| 359 | - max_shape: tuple, run_mode: str = "npu", | 326 | + max_shape: tuple, run_mode: str = "npu"): |
| 360 | - dynamic: bool = False): | 327 | + m, n = a_shape |
| 361 | - if dynamic: | 328 | + min_shape, max_shape = min_shape, max_shape |
| 362 | - m = pypto.frontend.dynamic("m") | ||
| 363 | - n = a_shape[1] | ||
| 364 | - min_shape = pypto.frontend.dynamic("min_shape") | ||
| 365 | - max_shape = pypto.frontend.dynamic("max_shape") | ||
| 366 | - else: | ||
| 367 | - m, n = a_shape | ||
| 368 | - min_shape, max_shape = min_shape, max_shape | ||
| 369 | 329 | ||
| 370 | if run_mode == "npu": | 330 | if run_mode == "npu": |
| 371 | mode = pypto.RunMode.NPU | 331 | mode = pypto.RunMode.NPU |
| @@ -414,13 +374,7 @@ def test_clip_broadcast(device_id: int = None, run_mode: str = "npu"): | |||
| 414 | # ============================================================================ | 374 | # ============================================================================ |
| 415 | 375 | ||
| 416 | 376 | ||
| 417 | -def create_div_op_kernel(a_shape: tuple, b_shape: tuple, run_mode: str = "npu", dynamic: bool = False): | 377 | +def create_div_op_kernel(a_shape: tuple, b_shape: tuple, run_mode: str = "npu"): |
| 418 | - if dynamic: | ||
| 419 | - a_shape = pypto.frontend.dynamic("a_shape") | ||
| 420 | - b_shape = pypto.frontend.dynamic("b_shape") | ||
| 421 | - else: | ||
| 422 | - a_shape = a_shape | ||
| 423 | - b_shape = b_shape | ||
| 424 | 378 | ||
| 425 | if run_mode == "npu": | 379 | if run_mode == "npu": |
| 426 | mode = pypto.RunMode.NPU | 380 | mode = pypto.RunMode.NPU |
| @@ -462,16 +416,10 @@ def test_div_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 462 | print("✓ Basic usage of div function completed successfully") | 416 | print("✓ Basic usage of div function completed successfully") |
| 463 | 417 | ||
| 464 | 418 | ||
| 465 | -def create_div_broadcast_op_kernel(a_shape: tuple, b_shape: tuple, | 419 | +def create_div_broadcast_op_kernel(a_shape: tuple, b_shape: tuple, |
| 466 | - run_mode: str = "npu", | 420 | + run_mode: str = "npu"): |
| 467 | - dynamic: bool = False): | 421 | + b_shape = b_shape |
| 468 | - if dynamic: | 422 | + m, n = a_shape |
| 469 | - n = a_shape[1] | ||
| 470 | - m = pypto.frontend.dynamic("m") | ||
| 471 | - b_shape = pypto.frontend.dynamic("b_shape") | ||
| 472 | - else: | ||
| 473 | - b_shape = b_shape | ||
| 474 | - m, n = a_shape | ||
| 475 | 423 | ||
| 476 | mode = pypto.RunMode.SIM if run_mode != "npu" else pypto.RunMode.NPU | 424 | mode = pypto.RunMode.SIM if run_mode != "npu" else pypto.RunMode.NPU |
| 477 | 425 | ||
| @@ -508,12 +456,8 @@ def test_div_broadcast(device_id: int = None, run_mode: str = "npu"): | |||
| 508 | print("✓ Test Broadcasting Between Tensors completed successfully") | 456 | print("✓ Test Broadcasting Between Tensors completed successfully") |
| 509 | 457 | ||
| 510 | 458 | ||
| 511 | -def create_div_scalar_op_kernel(a_shape: tuple, scalar: float, | 459 | +def create_div_scalar_op_kernel(a_shape: tuple, scalar: float, |
| 512 | - run_mode: str = "npu", dynamic: bool = False): | 460 | + run_mode: str = "npu"): |
| 513 | - if dynamic: | ||
| 514 | - a_shape = pypto.frontend.dynamic("a_shape") | ||
| 515 | - else: | ||
| 516 | - a_shape = a_shape | ||
| 517 | 461 | ||
| 518 | if run_mode == "npu": | 462 | if run_mode == "npu": |
| 519 | mode = pypto.RunMode.NPU | 463 | mode = pypto.RunMode.NPU |
| @@ -559,11 +503,7 @@ def test_div_scalar(device_id: int = None, run_mode: str = "npu"): | |||
| 559 | # ============================================================================ | 503 | # ============================================================================ |
| 560 | 504 | ||
| 561 | 505 | ||
| 562 | -def create_exp_op_kernel(x_shape: tuple, run_mode: str = "npu", dynamic: bool = False): | 506 | +def create_exp_op_kernel(x_shape: tuple, run_mode: str = "npu"): |
| 563 | - if dynamic: | ||
| 564 | - x_shape = pypto.frontend.dynamic("x_shape") | ||
| 565 | - else: | ||
| 566 | - x_shape = x_shape | ||
| 567 | 507 | ||
| 568 | if run_mode == "npu": | 508 | if run_mode == "npu": |
| 569 | mode = pypto.RunMode.NPU | 509 | mode = pypto.RunMode.NPU |
| @@ -608,11 +548,7 @@ def test_exp_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 608 | # ============================================================================ | 548 | # ============================================================================ |
| 609 | 549 | ||
| 610 | 550 | ||
| 611 | -def create_log_op_kernel(a_shape: tuple, run_mode: str = "npu", dynamic: bool = False): | 551 | +def create_log_op_kernel(a_shape: tuple, run_mode: str = "npu"): |
| 612 | - if dynamic: | ||
| 613 | - a_shape = pypto.frontend.dynamic("a_shape") | ||
| 614 | - else: | ||
| 615 | - a_shape = a_shape | ||
| 616 | 552 | ||
| 617 | if run_mode == "npu": | 553 | if run_mode == "npu": |
| 618 | mode = pypto.RunMode.NPU | 554 | mode = pypto.RunMode.NPU |
| @@ -656,7 +592,7 @@ def test_log_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 656 | # MUL Examples | 592 | # MUL Examples |
| 657 | # ============================================================================ | 593 | # ============================================================================ |
| 658 | 594 | ||
| 659 | -def mul_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 595 | +def mul_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 660 | a_shape, b_shape = a.shape, b.shape | 596 | a_shape, b_shape = a.shape, b.shape |
| 661 | if run_mode == "npu": | 597 | if run_mode == "npu": |
| 662 | mode = pypto.RunMode.NPU | 598 | mode = pypto.RunMode.NPU |
| @@ -701,7 +637,7 @@ def test_mul_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 701 | print("✓ Basic usage of mul function completed successfully") | 637 | print("✓ Basic usage of mul function completed successfully") |
| 702 | 638 | ||
| 703 | 639 | ||
| 704 | -def mul_broadcast_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 640 | +def mul_broadcast_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 705 | a_shape, b_shape = a.shape, b.shape | 641 | a_shape, b_shape = a.shape, b.shape |
| 706 | 642 | ||
| 707 | if run_mode == "npu": | 643 | if run_mode == "npu": |
| @@ -748,7 +684,7 @@ def test_mul_broadcast(device_id: int = None, run_mode: str = "npu"): | |||
| 748 | print("✓ Test Broadcasting Between Tensors completed successfully") | 684 | print("✓ Test Broadcasting Between Tensors completed successfully") |
| 749 | 685 | ||
| 750 | 686 | ||
| 751 | -def mul_scalar_op(a: torch.Tensor, scalar: float, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 687 | +def mul_scalar_op(a: torch.Tensor, scalar: float, run_mode: str = "npu") -> torch.Tensor: |
| 752 | a_shape = a.shape | 688 | a_shape = a.shape |
| 753 | 689 | ||
| 754 | if run_mode == "npu": | 690 | if run_mode == "npu": |
| @@ -798,7 +734,7 @@ def test_mul_scalar(device_id: int = None, run_mode: str = "npu"): | |||
| 798 | # ============================================================================ | 734 | # ============================================================================ |
| 799 | 735 | ||
| 800 | 736 | ||
| 801 | -def neg_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 737 | +def neg_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 802 | a_shape = a.shape | 738 | a_shape = a.shape |
| 803 | if run_mode == "npu": | 739 | if run_mode == "npu": |
| 804 | mode = pypto.RunMode.NPU | 740 | mode = pypto.RunMode.NPU |
| @@ -844,7 +780,7 @@ def test_neg_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 844 | # POW Examples | 780 | # POW Examples |
| 845 | # ============================================================================ | 781 | # ============================================================================ |
| 846 | 782 | ||
| 847 | -def pow_op(a: torch.Tensor, b: float, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 783 | +def pow_op(a: torch.Tensor, b: float, run_mode: str = "npu") -> torch.Tensor: |
| 848 | a_shape = a.shape | 784 | a_shape = a.shape |
| 849 | 785 | ||
| 850 | if run_mode == "npu": | 786 | if run_mode == "npu": |
| @@ -889,7 +825,7 @@ def test_pow_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 889 | # ROUND Examples | 825 | # ROUND Examples |
| 890 | # ============================================================================ | 826 | # ============================================================================ |
| 891 | 827 | ||
| 892 | -def round_op(a: torch.Tensor, b: int, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 828 | +def round_op(a: torch.Tensor, b: int, run_mode: str = "npu") -> torch.Tensor: |
| 893 | a_shape = a.shape | 829 | a_shape = a.shape |
| 894 | 830 | ||
| 895 | if run_mode == "npu": | 831 | if run_mode == "npu": |
| @@ -936,7 +872,7 @@ def test_round_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 936 | # RSQRT Examples | 872 | # RSQRT Examples |
| 937 | # ============================================================================ | 873 | # ============================================================================ |
| 938 | 874 | ||
| 939 | -def rsqrt_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 875 | +def rsqrt_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 940 | a_shape = a.shape | 876 | a_shape = a.shape |
| 941 | 877 | ||
| 942 | if run_mode == "npu": | 878 | if run_mode == "npu": |
| @@ -982,7 +918,7 @@ def test_rsqrt_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 982 | # CEIL Examples | 918 | # CEIL Examples |
| 983 | # ============================================================================ | 919 | # ============================================================================ |
| 984 | 920 | ||
| 985 | -def ceil_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 921 | +def ceil_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 986 | a_shape = a.shape | 922 | a_shape = a.shape |
| 987 | 923 | ||
| 988 | if run_mode == "npu": | 924 | if run_mode == "npu": |
| @@ -1028,7 +964,7 @@ def test_ceil_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 1028 | # FLOOR Examples | 964 | # FLOOR Examples |
| 1029 | # ============================================================================ | 965 | # ============================================================================ |
| 1030 | 966 | ||
| 1031 | -def floor_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 967 | +def floor_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 1032 | a_shape = a.shape | 968 | a_shape = a.shape |
| 1033 | 969 | ||
| 1034 | if run_mode == "npu": | 970 | if run_mode == "npu": |
| @@ -1074,7 +1010,7 @@ def test_floor_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 1074 | # TRUNC Examples | 1010 | # TRUNC Examples |
| 1075 | # ============================================================================ | 1011 | # ============================================================================ |
| 1076 | 1012 | ||
| 1077 | -def trunc_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1013 | +def trunc_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 1078 | a_shape = a.shape | 1014 | a_shape = a.shape |
| 1079 | if run_mode == "npu": | 1015 | if run_mode == "npu": |
| 1080 | mode = pypto.RunMode.NPU | 1016 | mode = pypto.RunMode.NPU |
| @@ -1119,7 +1055,7 @@ def test_trunc_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 1119 | # SQRT Examples | 1055 | # SQRT Examples |
| 1120 | # ============================================================================ | 1056 | # ============================================================================ |
| 1121 | 1057 | ||
| 1122 | -def sqrt_op(a: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1058 | +def sqrt_op(a: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 1123 | a_shape = a.shape | 1059 | a_shape = a.shape |
| 1124 | if run_mode == "npu": | 1060 | if run_mode == "npu": |
| 1125 | mode = pypto.RunMode.NPU | 1061 | mode = pypto.RunMode.NPU |
| @@ -1165,7 +1101,7 @@ def test_sqrt_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 1165 | # ============================================================================ | 1101 | # ============================================================================ |
| 1166 | 1102 | ||
| 1167 | 1103 | ||
| 1168 | -def sub_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1104 | +def sub_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 1169 | a_shape, b_shape = a.shape, b.shape | 1105 | a_shape, b_shape = a.shape, b.shape |
| 1170 | 1106 | ||
| 1171 | if run_mode == "npu": | 1107 | if run_mode == "npu": |
| @@ -1211,7 +1147,7 @@ def test_sub_basic(device_id: int = None, run_mode: str = "npu"): | |||
| 1211 | print("✓ Basic usage of sub function completed successfully") | 1147 | print("✓ Basic usage of sub function completed successfully") |
| 1212 | 1148 | ||
| 1213 | 1149 | ||
| 1214 | -def sub_broadcast_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1150 | +def sub_broadcast_op(a: torch.Tensor, b: torch.Tensor, run_mode: str = "npu") -> torch.Tensor: |
| 1215 | a_shape, b_shape = a.shape, b.shape | 1151 | a_shape, b_shape = a.shape, b.shape |
| 1216 | 1152 | ||
| 1217 | if run_mode == "npu": | 1153 | if run_mode == "npu": |
| @@ -1257,7 +1193,7 @@ def test_sub_broadcast(device_id: int = None, run_mode: str = "npu"): | |||
| 1257 | print("✓ Test Broadcasting Between Tensors completed successfully") | 1193 | print("✓ Test Broadcasting Between Tensors completed successfully") |
| 1258 | 1194 | ||
| 1259 | 1195 | ||
| 1260 | -def sub_scalar_op(a: torch.Tensor, scalar: float, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1196 | +def sub_scalar_op(a: torch.Tensor, scalar: float, run_mode: str = "npu") -> torch.Tensor: |
| 1261 | a_shape = a.shape | 1197 | a_shape = a.shape |
| 1262 | if run_mode == "npu": | 1198 | if run_mode == "npu": |
| 1263 | mode = pypto.RunMode.NPU | 1199 | mode = pypto.RunMode.NPU |
| @@ -1298,7 +1234,7 @@ def test_sub_scalar(device_id: int = None, run_mode: str = "npu"): | |||
| 1298 | 1234 | ||
| 1299 | 1235 | ||
| 1300 | 1236 | ||
| 1301 | -def sub_with_alpha_op(a: torch.Tensor, b: torch.Tensor, alpha: float, run_mode: str = "npu", dynamic: bool = False) -> torch.Tensor: | 1237 | +def sub_with_alpha_op(a: torch.Tensor, b: torch.Tensor, alpha: float, run_mode: str = "npu") -> torch.Tensor: |
| 1302 | a_shape, b_shape = a.shape, b.shape | 1238 | a_shape, b_shape = a.shape, b.shape |
| 1303 | 1239 | ||
| 1304 | if run_mode == "npu": | 1240 | if run_mode == "npu": |
| @@ -9,10 +9,27 @@ | |||
| 9 | # See LICENSE in the root of the software repository for the full text of the License. | 9 | # See LICENSE in the root of the software repository for the full text of the License. |
| 10 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 11 | """ | 11 | """ |
| 12 | -Dynamic Axis Example for PyPTO | 12 | +Dynamic Shape Examples for PyPTO |
| 13 | 13 | ||
| 14 | -This example demonstrates: | 14 | +This file demonstrates the usage of PyPTO's dynamic shape feature, which allows |
| 15 | -- Run attention module with dynamic shapes. | 15 | +kernels to handle inputs with shapes that are not known at compile time. |
| 16 | + | ||
| 17 | +Key Concepts: | ||
| 18 | + 1. Define dynamic dimensions at module level using pypto.frontend.dynamic() | ||
| 19 | + 2. Only make necessary dimensions dynamic; keep others as concrete values | ||
| 20 | + 3. Use pypto.view / pypto.assemble with pypto.loop for explicit tiling and | ||
| 21 | + boundary management on dynamic dimensions | ||
| 22 | + | ||
| 23 | +Examples included: | ||
| 24 | + - dynamic_mul: Basic dynamic batch dimension with view/assemble tiling | ||
| 25 | + - dynamic_partial: Partial dynamic dimensions (only batch is dynamic, others concrete) | ||
| 26 | + - dynamic_attention: Multi-head attention with dynamic batch size | ||
| 27 | + - dynamic_multi_dim: Multiple dynamic dimensions in a single kernel | ||
| 28 | + | ||
| 29 | +Usage: | ||
| 30 | + python dynamic.py # Run all examples | ||
| 31 | + python dynamic.py --list # List all available examples | ||
| 32 | + python dynamic.py dynamic_mul::test_dynamic_mul # Run a specific case | ||
| 16 | """ | 33 | """ |
| 17 | 34 | ||
| 18 | import os | 35 | import os |