-
Notifications
You must be signed in to change notification settings - Fork 68
Fix VCG group reduction semantics #889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mouliangyu
wants to merge
1
commit into
hw-native-sys:main
Choose a base branch
from
mouliangyu:codex/fix-vcg-group-reductions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| # This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| # CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| # Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| # See LICENSE in the root of the software repository for the full text of the License. | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| def compare_bin(golden_path, output_path, eps): | ||
| if not os.path.exists(golden_path): | ||
| print(f"[ERROR] Golden missing: {golden_path}") | ||
| return False | ||
| if not os.path.exists(output_path): | ||
| print(f"[ERROR] Output missing: {output_path}") | ||
| return False | ||
|
|
||
| golden = np.fromfile(golden_path, dtype=np.float32) | ||
| output = np.fromfile(output_path, dtype=np.float32) | ||
| if golden.shape != output.shape: | ||
| print(f"[ERROR] Shape mismatch: {golden_path} {golden.shape} vs {output_path} {output.shape}") | ||
| return False | ||
| if not np.allclose(golden, output, atol=eps, rtol=eps, equal_nan=True): | ||
| diff = np.abs(golden.astype(np.float64) - output.astype(np.float64)) | ||
| idx = int(np.argmax(diff)) | ||
| print( | ||
| f"[ERROR] Mismatch: {golden_path} vs {output_path}, " | ||
| f"idx={idx}, golden={golden[idx]}, output={output[idx]}, max_diff={diff[idx]}" | ||
| ) | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def main(): | ||
| strict = os.getenv("COMPARE_STRICT", "1") != "0" | ||
| checks = [ | ||
| ("golden_add.bin", "out_add.bin", "vcgadd"), | ||
| ("golden_max.bin", "out_max.bin", "vcgmax"), | ||
| ("golden_min.bin", "out_min.bin", "vcgmin"), | ||
| ] | ||
| failed = [] | ||
| for golden, output, label in checks: | ||
| if not compare_bin(golden, output, 1e-4): | ||
| failed.append(label) | ||
| print(f"[ERROR] compare failed: {label}") | ||
| if failed: | ||
| if strict: | ||
| print(f"[ERROR] {len(failed)} check(s) failed: {', '.join(failed)}") | ||
| sys.exit(2) | ||
| print(f"[WARN] {len(failed)} check(s) failed (non-gating): {', '.join(failed)}") | ||
| return | ||
| print("[INFO] compare passed") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| # This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| # CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| # Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| # See LICENSE in the root of the software repository for the full text of the License. | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| GROUPS = 8 | ||
| ELEMS_PER_GROUP = 8 | ||
| LANES = GROUPS * ELEMS_PER_GROUP | ||
|
|
||
|
|
||
| def generate(output_dir: Path) -> None: | ||
| src = np.array( | ||
| [ | ||
| -7.0, 1.0, 3.5, -2.0, 9.0, -4.5, 6.0, 0.5, | ||
| 8.0, -1.0, -3.0, 4.0, 2.0, -6.0, 5.5, 7.0, | ||
| -0.0, 0.0, -5.0, 5.0, 11.0, -12.0, 13.0, -14.0, | ||
| 1.25, 2.25, 3.25, 4.25, -8.0, -9.0, 10.0, -10.0, | ||
| 15.0, 14.0, 13.0, 12.0, -1.5, -2.5, -3.5, -4.5, | ||
| -20.0, -19.0, -18.0, -17.0, 16.0, 15.5, 14.5, 13.5, | ||
| 0.25, -0.75, 1.5, -2.25, 3.0, -3.75, 4.5, -5.25, | ||
| 31.0, -32.0, 33.0, -34.0, 35.0, -36.0, 37.0, -38.0, | ||
| ], | ||
| dtype=np.float32, | ||
| ) | ||
| groups = src.reshape(GROUPS, ELEMS_PER_GROUP) | ||
|
|
||
| golden_add = np.zeros(LANES, dtype=np.float32) | ||
| golden_max = np.zeros(LANES, dtype=np.float32) | ||
| golden_min = np.zeros(LANES, dtype=np.float32) | ||
| golden_add[:GROUPS] = np.sum(groups, axis=1, dtype=np.float32) | ||
| golden_max[:GROUPS] = np.max(groups, axis=1) | ||
| golden_min[:GROUPS] = np.min(groups, axis=1) | ||
|
|
||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
| src.tofile(output_dir / "src.bin") | ||
| golden_add.tofile(output_dir / "golden_add.bin") | ||
| golden_max.tofile(output_dir / "golden_max.bin") | ||
| golden_min.tofile(output_dir / "golden_min.bin") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--output-dir", type=Path, default=Path(".")) | ||
| args = parser.parse_args() | ||
| generate(args.output_dir) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| // This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| // CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| // Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| // INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| // See LICENSE in the root of the software repository for the full text of the License. | ||
|
|
||
| module attributes {pto.target_arch = "a5", pto.kernel_kind = #pto.kernel_kind<vector>} { | ||
| func.func @vcg_group_kernel(%src: !pto.ptr<f32, gm>, | ||
| %dst_add: !pto.ptr<f32, gm>, | ||
| %dst_max: !pto.ptr<f32, gm>, | ||
| %dst_min: !pto.ptr<f32, gm>) attributes {pto.kernel} { | ||
| %c0 = arith.constant 0 : index | ||
| %c0_i64 = arith.constant 0 : i64 | ||
| %c1_i64 = arith.constant 1 : i64 | ||
| %c256_i64 = arith.constant 256 : i64 | ||
| %c4096_i64 = arith.constant 4096 : i64 | ||
| %c8192_i64 = arith.constant 8192 : i64 | ||
| %c12288_i64 = arith.constant 12288 : i64 | ||
|
|
||
| %ub_src = pto.castptr %c0_i64 : i64 -> !pto.ptr<f32, ub> | ||
| %ub_add = pto.castptr %c4096_i64 : i64 -> !pto.ptr<f32, ub> | ||
| %ub_max = pto.castptr %c8192_i64 : i64 -> !pto.ptr<f32, ub> | ||
| %ub_min = pto.castptr %c12288_i64 : i64 -> !pto.ptr<f32, ub> | ||
|
|
||
| pto.mte_gm_ub %src, %ub_src, %c0_i64, %c256_i64 | ||
| nburst(%c1_i64, %c256_i64, %c256_i64) | ||
| : !pto.ptr<f32, gm>, !pto.ptr<f32, ub>, i64, i64, i64, i64, i64 | ||
|
|
||
| pto.set_flag["PIPE_MTE2", "PIPE_V", "EVENT_ID0"] | ||
| pto.wait_flag["PIPE_MTE2", "PIPE_V", "EVENT_ID0"] | ||
|
|
||
| pto.vecscope { | ||
| %mask = pto.pset_b32 "PAT_ALL" : !pto.mask<b32> | ||
| %vec = pto.vlds %ub_src[%c0] : !pto.ptr<f32, ub> -> !pto.vreg<64xf32> | ||
| %add = pto.vcgadd %vec, %mask : !pto.vreg<64xf32>, !pto.mask<b32> -> !pto.vreg<64xf32> | ||
| %max = pto.vcgmax %vec, %mask : !pto.vreg<64xf32>, !pto.mask<b32> -> !pto.vreg<64xf32> | ||
| %min = pto.vcgmin %vec, %mask : !pto.vreg<64xf32>, !pto.mask<b32> -> !pto.vreg<64xf32> | ||
| pto.vsts %add, %ub_add[%c0], %mask : !pto.vreg<64xf32>, !pto.ptr<f32, ub>, !pto.mask<b32> | ||
| pto.vsts %max, %ub_max[%c0], %mask : !pto.vreg<64xf32>, !pto.ptr<f32, ub>, !pto.mask<b32> | ||
| pto.vsts %min, %ub_min[%c0], %mask : !pto.vreg<64xf32>, !pto.ptr<f32, ub>, !pto.mask<b32> | ||
| } | ||
|
|
||
| pto.set_flag["PIPE_V", "PIPE_MTE3", "EVENT_ID0"] | ||
| pto.wait_flag["PIPE_V", "PIPE_MTE3", "EVENT_ID0"] | ||
|
|
||
| pto.mte_ub_gm %ub_add, %dst_add, %c256_i64 | ||
| nburst(%c1_i64, %c256_i64, %c256_i64) | ||
| : !pto.ptr<f32, ub>, !pto.ptr<f32, gm>, i64, i64, i64, i64 | ||
| pto.mte_ub_gm %ub_max, %dst_max, %c256_i64 | ||
| nburst(%c1_i64, %c256_i64, %c256_i64) | ||
| : !pto.ptr<f32, ub>, !pto.ptr<f32, gm>, i64, i64, i64, i64 | ||
| pto.mte_ub_gm %ub_min, %dst_min, %c256_i64 | ||
| nburst(%c1_i64, %c256_i64, %c256_i64) | ||
| : !pto.ptr<f32, ub>, !pto.ptr<f32, gm>, i64, i64, i64, i64 | ||
| pto.barrier #pto.pipe<PIPE_ALL> | ||
| return | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated documentation states that group reductions produce one result per VLane and write them contiguously to the low elements of the destination vector. However, the documentation should explicitly clarify the behavior when the vector register is not fully populated (e.g., when the element type has fewer than 64 elements, such as
i64which has 32 elements). Please clarify if the remaining elements are always zero-filled up to the full vector register sizeNregardless of the element type.