Merged
fix(pypto_pro):fix vf check bug #6760
huanyongbenAdd on Sep 29
fix(pypto_pro):fix vf check bug #6760
Merged
Merged fromhuanyongben/pypto_open9.2.0intocann/pypto9.2.0
Total: 7 files changed+118-215
| @@ -459,6 +459,10 @@ class PyptoProError(PyptoError): | |||
| 459 | return | 459 | return |
| 460 | column = self.span.get("column") or self.span.get("begin_column") | 460 | column = self.span.get("column") or self.span.get("begin_column") |
| 461 | parts.append(f" --> {filename}:{line}:{column}") | 461 | parts.append(f" --> {filename}:{line}:{column}") |
| 462 | + if self.source_lines is None and self.span.get("filename"): | ||
| 463 | + # Not every entry point attaches the source (a pipeline probe parse | ||
| 464 | + # calls the parser directly), so read the file the span names. | ||
| 465 | + self.source_lines = source_lines_of(self.span["filename"]) | ||
| 462 | self._append_source_preview(parts, line, column) | 466 | self._append_source_preview(parts, line, column) |
| 463 | 467 | ||
| 464 | def _append_source_preview(self, parts: list, line: int, column: int) -> None: | 468 | def _append_source_preview(self, parts: list, line: int, column: int) -> None: |
| @@ -48,6 +48,8 @@ from .._errors import ( | |||
| 48 | NotSupported, | 48 | NotSupported, |
| 49 | OutOfRange, | 49 | OutOfRange, |
| 50 | RuntimeFailure, | 50 | RuntimeFailure, |
| 51 | + error_class_of_spec_message, | ||
| 52 | + span_of_spec_message, | ||
| 51 | span_source, | 53 | span_source, |
| 52 | ) | 54 | ) |
| 53 | 55 | ||
| @@ -1299,7 +1301,17 @@ def _codegen_target_cce( | |||
| 1299 | Key-independent: produces no launcher and does not write final artifacts. | 1301 | Key-independent: produces no launcher and does not write final artifacts. |
| 1300 | """ | 1302 | """ |
| 1301 | cce_codegen = CCECodegen(target) | 1303 | cce_codegen = CCECodegen(target) |
| 1302 | - cpp_code = cce_codegen.generate_single(prog, arch) | 1304 | + try: |
| 1305 | + cpp_code = cce_codegen.generate_single(prog, arch) | ||
| 1306 | + except RuntimeError as exc: | ||
| 1307 | + # A C++ check states its own first line, code and DSL location. Carry it into | ||
| 1308 | + # the class that code names, the way the parse path does, so the location block | ||
| 1309 | + # renders its source preview rather than reaching the user as a bare | ||
| 1310 | + # RuntimeError. Anything without that first line is not ours to reclassify. | ||
| 1311 | + wrapper = error_class_of_spec_message(str(exc)) | ||
| 1312 | + if wrapper is None: | ||
| 1313 | + raise | ||
| 1314 | + raise wrapper(str(exc), span=span_of_spec_message(str(exc))) from None | ||
| 1303 | if "ffts_cross_core_sync" in cpp_code and arch == "a5": | 1315 | if "ffts_cross_core_sync" in cpp_code and arch == "a5": |
| 1304 | extra_headers = "#include <pto/npu/a5/custom/TSyncCVID.hpp>" | 1316 | extra_headers = "#include <pto/npu/a5/custom/TSyncCVID.hpp>" |
| 1305 | guard = { | 1317 | guard = { |
| @@ -239,7 +239,7 @@ def find_high_bin_and_histograms_low( | |||
| 239 | idx_c = vf.arange(i * 64, dtype=pl.DT_INT32) | 239 | idx_c = vf.arange(i * 64, dtype=pl.DT_INT32) |
| 240 | cout = vf.load_align(hist_high_tile, i * 64) | 240 | cout = vf.load_align(hist_high_tile, i * 64) |
| 241 | preg_ge = vf.ge(cout, btm_k, preg_b32) | 241 | preg_ge = vf.ge(cout, btm_k, preg_b32) |
| 242 | - sqz_idx_high = vf.squeeze(idx_c, preg_ge, gather_mode=pl.SqueezeMode.STORE_REG, dtype=pl.DT_UINT32) | 242 | + sqz_idx_high = vf.squeeze(idx_c, preg_ge, gather_mode=pl.SqueezeMode.STORE_REG) |
| 243 | vf.squeeze_store_unalign(sqz_idx_buf, sqz_idx_high, align_idx_high) | 243 | vf.squeeze_store_unalign(sqz_idx_buf, sqz_idx_high, align_idx_high) |
| 244 | vf.squeeze_store_unalign_post(sqz_idx_buf, align_idx_high) | 244 | vf.squeeze_store_unalign_post(sqz_idx_buf, align_idx_high) |
| 245 | vf.mem_bar() | 245 | vf.mem_bar() |
| @@ -306,7 +306,7 @@ def find_kth_vf(kth_value_tile, hist_low_tile, idx_high_tile, sqz_idx_buf, nk_va | |||
| 306 | idx_c = vf.arange(i * 64, dtype=pl.DT_INT32) | 306 | idx_c = vf.arange(i * 64, dtype=pl.DT_INT32) |
| 307 | cout = vf.load_align(hist_low_tile, i * 64) | 307 | cout = vf.load_align(hist_low_tile, i * 64) |
| 308 | preg_ge = vf.ge(cout, btm_k, preg_b32) | 308 | preg_ge = vf.ge(cout, btm_k, preg_b32) |
| 309 | - sqz_idx_low = vf.squeeze(idx_c, preg_ge, gather_mode=pl.SqueezeMode.STORE_REG, dtype=pl.DT_UINT32) | 309 | + sqz_idx_low = vf.squeeze(idx_c, preg_ge, gather_mode=pl.SqueezeMode.STORE_REG) |
| 310 | vf.squeeze_store_unalign(sqz_idx_buf, sqz_idx_low, align_idx_low) | 310 | vf.squeeze_store_unalign(sqz_idx_buf, sqz_idx_low, align_idx_low) |
| 311 | vf.squeeze_store_unalign_post(sqz_idx_buf, align_idx_low) | 311 | vf.squeeze_store_unalign_post(sqz_idx_buf, align_idx_low) |
| 312 | vf.mem_bar() | 312 | vf.mem_bar() |
| @@ -351,7 +351,7 @@ def collect_indices_gt_eq_vf( | |||
| 351 | idx_c = vf.arange(begin_idx + i * 128, dtype=pl.DT_INT16) | 351 | idx_c = vf.arange(begin_idx + i * 128, dtype=pl.DT_INT16) |
| 352 | vreg_input = vf.load_align(score_u16_tile, i * 128) | 352 | vreg_input = vf.load_align(score_u16_tile, i * 128) |
| 353 | pout_gt = vf.gt(vreg_input, kth_value, preg_b16) | 353 | pout_gt = vf.gt(vreg_input, kth_value, preg_b16) |
| 354 | - sqz_idx_out = vf.squeeze(idx_c, pout_gt, gather_mode=pl.SqueezeMode.STORE_REG, dtype=pl.DT_UINT16) | 354 | + sqz_idx_out = vf.squeeze(idx_c, pout_gt, gather_mode=pl.SqueezeMode.STORE_REG) |
| 355 | vf.squeeze_store_unalign(output_idx_tile, sqz_idx_out, align_idx) | 355 | vf.squeeze_store_unalign(output_idx_tile, sqz_idx_out, align_idx) |
| 356 | 356 | ||
| 357 | # Pass 2: collect EQ indices (appends after GT) | 357 | # Pass 2: collect EQ indices (appends after GT) |
| @@ -359,7 +359,7 @@ def collect_indices_gt_eq_vf( | |||
| 359 | idx_c = vf.arange(begin_idx + i * 128, dtype=pl.DT_INT16) | 359 | idx_c = vf.arange(begin_idx + i * 128, dtype=pl.DT_INT16) |
| 360 | vreg_input = vf.load_align(score_u16_tile, i * 128) | 360 | vreg_input = vf.load_align(score_u16_tile, i * 128) |
| 361 | pout_eq = vf.eq(vreg_input, kth_value, preg_b16) | 361 | pout_eq = vf.eq(vreg_input, kth_value, preg_b16) |
| 362 | - sqz_idx_out = vf.squeeze(idx_c, pout_eq, gather_mode=pl.SqueezeMode.STORE_REG, dtype=pl.DT_UINT16) | 362 | + sqz_idx_out = vf.squeeze(idx_c, pout_eq, gather_mode=pl.SqueezeMode.STORE_REG) |
| 363 | vf.squeeze_store_unalign(output_idx_tile, sqz_idx_out, align_idx) | 363 | vf.squeeze_store_unalign(output_idx_tile, sqz_idx_out, align_idx) |
| 364 | 364 | ||
| 365 | vf.squeeze_store_unalign_post(output_idx_tile, align_idx) | 365 | vf.squeeze_store_unalign_post(output_idx_tile, align_idx) |
| @@ -500,7 +500,7 @@ def make_quant_lightning_indexer_vf_kernel( | |||
| 500 | preg_ge = vf.ge(idx_c, threshold, preg_b32) | 500 | preg_ge = vf.ge(idx_c, threshold, preg_b32) |
| 501 | idx_c = vf.select(neg_one_i32, idx_c, preg_ge) | 501 | idx_c = vf.select(neg_one_i32, idx_c, preg_ge) |
| 502 | # squeeze all 64 elements (truncate INT32→UINT16) | 502 | # squeeze all 64 elements (truncate INT32→UINT16) |
| 503 | - sqz_out = vf.squeeze(idx_c, preg_b32, gather_mode=pl.SqueezeMode.STORE_REG, dtype=pl.DT_UINT32) | 503 | + sqz_out = vf.squeeze(idx_c, preg_b32, gather_mode=pl.SqueezeMode.STORE_REG) |
| 504 | vf.squeeze_store_unalign(output_idx_tile, sqz_out, align_idx) | 504 | vf.squeeze_store_unalign(output_idx_tile, sqz_out, align_idx) |
| 505 | vf.squeeze_store_unalign_post(output_idx_tile, align_idx) | 505 | vf.squeeze_store_unalign_post(output_idx_tile, align_idx) |
| 506 | vf.mem_bar() | 506 | vf.mem_bar() |
| @@ -700,52 +700,6 @@ def kernel_14_mull( | |||
| 700 | pl.store(out_u2, t_u2, [0, 0]) | 700 | pl.store(out_u2, t_u2, [0, 0]) |
| 701 | 701 | ||
| 702 | 702 | ||
| 703 | -@pl.vector_function | ||
| 704 | -def _vf_kernel_15_cmp_unsqueeze_0(in_a, in_b, t_u0): | ||
| 705 | - preg_f32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 706 | - preg_u32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 707 | - reg_a = vf.load_align(in_a, 0) | ||
| 708 | - reg_b = vf.load_align(in_b, 0) | ||
| 709 | - cmp_mask = vf.ge(reg_a, 0.0, preg_f32) | ||
| 710 | - reg_dst_u32 = vf.unsqueeze(cmp_mask, dtype=pl.DT_UINT32) | ||
| 711 | - vf.store_align(t_u0, reg_dst_u32, preg_u32) | ||
| 712 | - reg_i0, reg_i1 = vf.interleave(reg_a, reg_b) | ||
| 713 | - | ||
| 714 | - | ||
| 715 | -@pl.jit() | ||
| 716 | -def kernel_15_cmp_unsqueeze( | ||
| 717 | - a: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 718 | - b: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 719 | - out_f0: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 720 | - out_f1: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_FP32], | ||
| 721 | - out_u0: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 722 | - out_u1: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 723 | - out_u2: pl.Tensor[[pl.DYNAMIC, pl.DYNAMIC], pl.DT_UINT32], | ||
| 724 | -): | ||
| 725 | - tf = pl.TileType(shape=[N, M], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 726 | - tu = pl.TileType(shape=[N, M], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) | ||
| 727 | - in_a = pl.make_tile(tf, addr=VA_IN_A) | ||
| 728 | - in_b = pl.make_tile(tf, addr=VA_IN_B) | ||
| 729 | - t_f0 = pl.make_tile(tf, addr=VA_F0) | ||
| 730 | - t_f1 = pl.make_tile(tf, addr=VA_F1) | ||
| 731 | - t_u0 = pl.make_tile(tu, addr=VA_U0) | ||
| 732 | - t_u1 = pl.make_tile(tu, addr=VA_U1) | ||
| 733 | - t_u2 = pl.make_tile(tu, addr=VA_U2) | ||
| 734 | - with pl.section_vector(): | ||
| 735 | - pl.load(in_a, a, [0, 0]) | ||
| 736 | - pl.load(in_b, b, [0, 0]) | ||
| 737 | - pl.system.sync_src(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0) | ||
| 738 | - pl.system.sync_dst(set_pipe=pl.PipeType.MTE2, wait_pipe=pl.PipeType.V, event_id=0) | ||
| 739 | - _vf_kernel_15_cmp_unsqueeze_0(in_a, in_b, t_u0) | ||
| 740 | - pl.system.sync_src(set_pipe=pl.PipeType.V, wait_pipe=pl.PipeType.MTE3, event_id=1) | ||
| 741 | - pl.system.sync_dst(set_pipe=pl.PipeType.V, wait_pipe=pl.PipeType.MTE3, event_id=1) | ||
| 742 | - pl.store(out_f0, t_f0, [0, 0]) | ||
| 743 | - pl.store(out_f1, t_f1, [0, 0]) | ||
| 744 | - pl.store(out_u0, t_u0, [0, 0]) | ||
| 745 | - pl.store(out_u1, t_u1, [0, 0]) | ||
| 746 | - pl.store(out_u2, t_u2, [0, 0]) | ||
| 747 | - | ||
| 748 | - | ||
| 749 | @pl.vector_function | 703 | @pl.vector_function |
| 750 | def _vf_kernel_16_load_unalign_0(in_a, t_f0): | 704 | def _vf_kernel_16_load_unalign_0(in_a, t_f0): |
| 751 | preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | 705 | preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) |
| @@ -2050,7 +2004,7 @@ def kernel_51_muls_and_xor( | |||
| 2050 | def _vf_kernel_52_shift_rights_0(in_a, t_u0): | 2004 | def _vf_kernel_52_shift_rights_0(in_a, t_u0): |
| 2051 | preg_u32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | 2005 | preg_u32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) |
| 2052 | reg_a_u32 = vf.load_align(in_a, 0, dtype=pl.DT_UINT32) | 2006 | reg_a_u32 = vf.load_align(in_a, 0, dtype=pl.DT_UINT32) |
| 2053 | - reg_dst_u32 = vf.shift_right(reg_a_u32, 2, preg_u32, dtype=pl.DT_UINT32) | 2007 | + reg_dst_u32 = vf.shift_right(reg_a_u32, 2, preg_u32) |
| 2054 | vf.store_align(t_u0, reg_dst_u32, preg_u32) | 2008 | vf.store_align(t_u0, reg_dst_u32, preg_u32) |
| 2055 | 2009 | ||
| 2056 | 2010 | ||
| @@ -3464,7 +3418,6 @@ _KERNELS = [ | |||
| 3464 | kernel_12_prelu_mul, | 3418 | kernel_12_prelu_mul, |
| 3465 | kernel_13_shift_vec, | 3419 | kernel_13_shift_vec, |
| 3466 | kernel_14_mull, | 3420 | kernel_14_mull, |
| 3467 | - kernel_15_cmp_unsqueeze, | ||
| 3468 | kernel_16_load_unalign, | 3421 | kernel_16_load_unalign, |
| 3469 | kernel_19_arange_brc, | 3422 | kernel_19_arange_brc, |
| 3470 | kernel_20_rint_loadsample, | 3423 | kernel_20_rint_loadsample, |
| @@ -3642,9 +3595,6 @@ def test_vf_basic_ops(): | |||
| 3642 | torch.testing.assert_close(u0, (product & 0xFFFFFFFF).to(torch.int32)) | 3595 | torch.testing.assert_close(u0, (product & 0xFFFFFFFF).to(torch.int32)) |
| 3643 | torch.testing.assert_close(u1, ((product >> 32) & 0xFFFFFFFF).to(torch.int32)) | 3596 | torch.testing.assert_close(u1, ((product >> 32) & 0xFFFFFFFF).to(torch.int32)) |
| 3644 | logging.info("Kernel 14 (Mull) PASSED") | 3597 | logging.info("Kernel 14 (Mull) PASSED") |
| 3645 | - _, _, u0, *_ = results[15] | ||
| 3646 | - assert u0.dtype == torch.int32 | ||
| 3647 | - logging.info("Kernel 15 (Compares, Unsqueeze, Interleave) PASSED") | ||
| 3648 | f0, *_ = results[16] | 3598 | f0, *_ = results[16] |
| 3649 | torch.testing.assert_close(f0, a_fp32, rtol=1e-5, atol=1e-5) | 3599 | torch.testing.assert_close(f0, a_fp32, rtol=1e-5, atol=1e-5) |
| 3650 | logging.info("Kernel 16 (LoadUnalign) PASSED") | 3600 | logging.info("Kernel 16 (LoadUnalign) PASSED") |
| @@ -13,6 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | import re | 14 | import re |
| 15 | 15 | ||
| 16 | +from pypto_pro._errors import NotSupported | ||
| 16 | import pypto_pro.language as pl | 17 | import pypto_pro.language as pl |
| 17 | import pytest | 18 | import pytest |
| 18 | 19 | ||
| @@ -519,5 +520,5 @@ def test_simt_auto_mutex_emits_pipe_v_lock_unlock_around_launch(): | |||
| 519 | 520 | ||
| 520 | 521 | ||
| 521 | def test_simt_codegen_rejects_pre_a5_architecture(): | 522 | def test_simt_codegen_rejects_pre_a5_architecture(): |
| 522 | - with pytest.raises(RuntimeError, match="requires arch='a5'"): | 523 | + with pytest.raises(NotSupported, match="requires arch='a5'"): |
| 523 | _compile_to_cce(_simt_tile_codegen_kernel, arch="a3") | 524 | _compile_to_cce(_simt_tile_codegen_kernel, arch="a3") |
| @@ -221,6 +221,46 @@ def test_the_preview_quotes_the_offending_line_with_context(): | |||
| 221 | assert len(rendered.preview) > 1 | 221 | assert len(rendered.preview) > 1 |
| 222 | 222 | ||
| 223 | 223 | ||
| 224 | +def test_a_codegen_check_renders_its_location_block_like_a_parse_one(): | ||
| 225 | + """A C++ check reaching Python from codegen carries the same block a parse check does.""" | ||
| 226 | + from pypto_pro.runtime.jit import _parse_and_codegen_targets | ||
| 227 | + | ||
| 228 | + @pl.vector_function | ||
| 229 | + def unsqueeze_a_float_mask(in_a, t_u0): | ||
| 230 | + preg_f32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | ||
| 231 | + preg_u32 = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_UINT32) | ||
| 232 | + reg_a = vf.load_align(in_a, 0) | ||
| 233 | + cmp_mask = vf.ge(reg_a, 0.0, preg_f32) | ||
| 234 | + reg_dst = vf.unsqueeze(cmp_mask) | ||
| 235 | + vf.store_align(t_u0, reg_dst, preg_u32) | ||
| 236 | + | ||
| 237 | + @pl.jit() | ||
| 238 | + def kernel(a: pl.Tensor[[1, 64], pl.DT_FP32], out: pl.Tensor[[1, 64], pl.DT_UINT32]): | ||
| 239 | + tf = pl.TileType(shape=[1, 64], dtype=pl.DT_FP32, target_memory=pl.MemorySpace.Vec) | ||
| 240 | + tu = pl.TileType(shape=[1, 64], dtype=pl.DT_UINT32, target_memory=pl.MemorySpace.Vec) | ||
| 241 | + in_a = pl.make_tile(tf, addr=0) | ||
| 242 | + t_u0 = pl.make_tile(tu, addr=0x200) | ||
| 243 | + with pl.section_vector(): | ||
| 244 | + pl.load(in_a, a, [0, 0]) | ||
| 245 | + unsqueeze_a_float_mask(in_a, t_u0) | ||
| 246 | + pl.store(out, t_u0, [0, 0]) | ||
| 247 | + | ||
| 248 | + with pytest.raises(PyptoProError) as excinfo: | ||
| 249 | + _parse_and_codegen_targets(kernel.to_kernel_def(), "a5", "") | ||
| 250 | + rendered = Rendered(excinfo.value) | ||
| 251 | + | ||
| 252 | + # The C++ first line is passed through whole: its own file, module and code. | ||
| 253 | + assert rendered.head, rendered.lines[0] | ||
| 254 | + assert rendered.head["module"] == "PRO_CODEGEN" | ||
| 255 | + assert rendered.head["enum"] == "ExternalError::INVALID_TYPE" | ||
| 256 | + # The code names the class, here as on the parse path. | ||
| 257 | + assert isinstance(excinfo.value, InvalidType) | ||
| 258 | + # The location block is what codegen used to lack: a location, and source under it. | ||
| 259 | + assert rendered.loc | ||
| 260 | + assert rendered.loc["file"].endswith("test_error_format.py") | ||
| 261 | + assert "vf.unsqueeze(cmp_mask)" in "".join(rendered.preview) | ||
| 262 | + | ||
| 263 | + | ||
| 224 | # --------------------------------------------------------------------------- | 264 | # --------------------------------------------------------------------------- |
| 225 | # The shapes a check is raised in | 265 | # The shapes a check is raised in |
| 226 | # --------------------------------------------------------------------------- | 266 | # --------------------------------------------------------------------------- |