Repository navigation
OperatorX: add Ascend queued GEMM reproducer / OperatorX:新增昇腾排队计时 GEMM 复现工具 #4440
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Ascend GEMM reproduction | ||
|
|
||
| This standalone harness measures single-device native `torch_npu` GEMMs with queued events and numerical checks. It accepts OperatorX testlists and stores raw samples, validation results, runtime/source fingerprints and per-case telemetry in a resumable SQLite journal. It does not register an OperatorX platform, add a CI pool, or upload results. | ||
|
|
||
| The measurement path supports unquantized BF16/FP16/FP32 contracts and E4M3 GEMM with dynamic FP32 activation scales `[1,128]`, static FP32 weight scales `[128,128]`, and BF16 output. Activation quantization is inside the FP8 timer; static weight preparation is outside. Unsupported arguments, K tails, and cases above the explicit memory budget remain visible in the plan rather than being silently changed or padded. Parallel/distributed cases are not implemented. | ||
|
|
||
| ## Environment | ||
|
|
||
| The hardware-tested stack is Ascend950PR_9579 (28 Cube cores, 128 GiB HBM), CANN 9.1.0, PyTorch 2.10.0 and torch_npu 2.10.0.post4 on Linux. Load the installed CANN environment and make the driver's libraries available before running. The host also needs `npu-smi`; FP8 preflight needs a C++17 compiler and `libdl`. No GPU libraries are imported for CPU-only planning. | ||
|
|
||
| Run commands from the repository root, in the Python environment containing `torch_npu`. Choose board/chip/logical-device IDs from the actual SMI/ACL mapping; the example below uses board 0, chip 0 and logical device 0. The controller checks that mapping and requires an idle, healthy board with clean fault/ECC/PCIe diagnostics. Identity probing occurs in a disposable process before Torch initializes ACL. | ||
|
|
||
| ```bash | ||
| ASCEND_SCRIPTS="$PWD/operatorx/scripts/ascend" | ||
| ASCEND_RESULTS="/tmp/ascend-results" | ||
| mkdir -p "$ASCEND_RESULTS" | ||
| ``` | ||
|
|
||
| Use a fresh result directory for a changed plan, source, hardware identity or timing configuration. An existing directory cannot silently combine measurements from different fingerprints. The health parser is strict: an unrecognized driver table fails rather than guessing the device identity. | ||
|
|
||
| ## Unquantized GEMM | ||
|
|
||
| The bundled examples include the large BF16 shape `M=49152, N=7168, K=6144`. A normal testlist such as `operatorx/testlists/gemm_deepseek_v4.json` can replace the example input; multiple files are accepted. Case identity includes every argument, including output precision and scale descriptors. | ||
|
|
||
| ```bash | ||
| python "$ASCEND_SCRIPTS/prepare.py" \ | ||
| --input "$ASCEND_SCRIPTS/examples/bf16.json" --kind unquantized \ | ||
| --memory-budget-bytes 103079215104 --output "$ASCEND_RESULTS/bf16-plan.json" | ||
|
|
||
| python "$ASCEND_SCRIPTS/operatorx_batch_campaign.py" run \ | ||
| --plan "$ASCEND_RESULTS/bf16-plan.json" --run-dir "$ASCEND_RESULTS/bf16" \ | ||
| --phase smoke --device 0 --board-id 0 --chip-id 0 --expected-name 950PR \ | ||
| --warmups 5 --warmup-ms 25 --samples 10 --seed 20261023 \ | ||
| --timeout-seconds 300 --cpu-threads 4 | ||
| ``` | ||
|
|
||
| Then repeat the controller command with `--phase all-ready`, the same plan and run directory. Already successful smoke cases are reused; every other selected case receives its own before/after numerical gates. Execution stops on an unexpected kernel, numerical, health or timeout failure. Inspect failures before resuming in a fresh process. The 300-second per-case watchdog preserves a running marker if execution hangs. | ||
|
|
||
| ## FP8: inspect the installed tile first | ||
|
|
||
| The tested CANN kernel can select a local A tile larger than L0A. For `M=264, N=2304, K=7168`, the installed tile is `272×128`: two FP8 A buffers require 69,632 bytes, exceeding the 65,536-byte capacity. This is a **local tile** limit, not a limit on whole matrix size or a blanket `M <= 256` rule. | ||
|
|
||
| The preload hook reads the actual host tiling before GEMM dispatch. Preflight always rejects the GEMM with error `100000`, including tiles that fit; it produces no timing result. The decoder supports the validated 152-byte ABI and dispatch key 5124 for the named FP8 kernel. Other kernels/ABIs are skipped as unverified. The double-buffer bound is conservative and source-derived; this is not a general CANN tiling decoder. | ||
|
|
||
| ```bash | ||
| c++ -std=c++17 -O2 -shared -fPIC \ | ||
| "$ASCEND_SCRIPTS/operatorx_fp8_l0_guard.cpp" -ldl \ | ||
| -o "$ASCEND_RESULTS/liboperatorx_fp8_l0_guard.so" | ||
|
|
||
| python "$ASCEND_SCRIPTS/prepare.py" \ | ||
| --input "$ASCEND_SCRIPTS/examples/fp8.json" --kind fp8 \ | ||
| --memory-budget-bytes 103079215104 --output "$ASCEND_RESULTS/fp8-plan.json" | ||
|
|
||
| python "$ASCEND_SCRIPTS/operatorx_batch_campaign.py" _preflight-child \ | ||
| "$ASCEND_RESULTS/health.json" \ | ||
| '{"board_id":0,"chip_id":0,"device":0,"expected_name":"950PR","expect_pci":null}' | ||
|
|
||
| TASK_QUEUE_ENABLE=0 LD_PRELOAD="$ASCEND_RESULTS/liboperatorx_fp8_l0_guard.so" \ | ||
| python "$ASCEND_SCRIPTS/operatorx_fp8_l0_preflight.py" \ | ||
| --shape 240 2304 7168 --output "$ASCEND_RESULTS/guard-smoke.jsonl" \ | ||
| --guard-so "$ASCEND_RESULTS/liboperatorx_fp8_l0_guard.so" \ | ||
| --worker-dir "$ASCEND_SCRIPTS" --health-report "$ASCEND_RESULTS/health.json" \ | ||
| --device 0 --memory-budget-bytes 103079215104 | ||
| ``` | ||
|
|
||
| First verify that this known-safe smoke records `status=inspected`, `observed=true`, `decision=within_capacity`, `mode=1`, `forward_allowed=false`, and 61,440 L0A bytes. Do not check a potentially unsafe shape until actual interception is established on the installed stack. | ||
|
|
||
| ```bash | ||
| TASK_QUEUE_ENABLE=0 LD_PRELOAD="$ASCEND_RESULTS/liboperatorx_fp8_l0_guard.so" \ | ||
| python "$ASCEND_SCRIPTS/operatorx_fp8_l0_preflight.py" \ | ||
| --plan "$ASCEND_RESULTS/fp8-plan.json" --output "$ASCEND_RESULTS/guard.jsonl" \ | ||
| --guard-so "$ASCEND_RESULTS/liboperatorx_fp8_l0_guard.so" \ | ||
| --worker-dir "$ASCEND_SCRIPTS" --health-report "$ASCEND_RESULTS/health.json" \ | ||
| --device 0 --memory-budget-bytes 103079215104 | ||
|
|
||
| python "$ASCEND_SCRIPTS/fp8_plan.py" \ | ||
| --plan "$ASCEND_RESULTS/fp8-plan.json" --observations "$ASCEND_RESULTS/guard.jsonl" \ | ||
| --output "$ASCEND_RESULTS/fp8-approved.json" | ||
|
|
||
| python "$ASCEND_SCRIPTS/operatorx_batch_campaign.py" run \ | ||
| --plan "$ASCEND_RESULTS/fp8-approved.json" --run-dir "$ASCEND_RESULTS/fp8" \ | ||
| --worker-script "$ASCEND_SCRIPTS/operatorx_fp8_block_worker.py" \ | ||
| --phase smoke --device 0 --board-id 0 --chip-id 0 --expected-name 950PR \ | ||
| --warmups 5 --warmup-ms 25 --samples 10 --seed 20261023 \ | ||
| --timeout-seconds 300 --cpu-threads 4 | ||
| ``` | ||
|
|
||
| Run `all-ready` afterward as above. `LD_PRELOAD` and `TASK_QUEUE_ENABLE=0` are scoped to the inspection commands and must not be set for timing. The controller requires complete observations for the selected FP8 plan and rechecks the device identity before execution. Over-capacity and unverified cases remain explicit skips, with no fallback implementation or accepted latency. | ||
|
|
||
| ## Timing and validation contract | ||
|
|
||
| - Five warmups, followed by a 25 ms ramp; the first timing call in each process has a 1-second ramp floor. | ||
| - Ten preallocated event pairs, a 256 MiB write before each operation, all ten calls queued, then one synchronization. The reported latency is the sixth sorted sample. | ||
| - Four queued cache writes precede the timed batch as a launch shield. They are **not equivalent to CUDA `_sleep`**. Cache eviction is not proven. NPU graphs and GPU throttle-retry selection are not emulated. | ||
| - BF16 uses the serving-style contiguous `[N,K]` weight layout. FP8 times dynamic quantization and native GEMM together. FP8 uses one Torch CPU thread, while the unquantized worker uses the controller's configured count. | ||
| - Sampled CPU FP64 full-K dot products and a full output-finiteness scan run before and after timing. FP8 additionally checks every activation's quantization error and scale validity. This is not an exhaustive CPU output reference or a claim of CUDA-quantizer byte equivalence. | ||
| - Queued batch wall time includes shield/cache writes and final synchronization. It is recorded separately and is not represented as individual synchronized wall samples. | ||
| - Journals keep all attempts and raw event samples. The last attempt determines resume status; no fastest-of-retries selection is performed. Nothing is submitted automatically. | ||
|
|
||
| The measurement routines were used for the queued 950PR campaign; the large BF16 shape reproduced approximately 377 TFLOPS against a 378-TFLOPS nominal Cube reference. The entry-point packaging and plan approval checks are separate from the external campaign's frozen source hashes. Results from a per-sample synchronized loop are a different protocol and must not be mixed into these curves. | ||
|
|
||
| ## Files and checks | ||
|
|
||
| `results.sqlite3` contains the runtime/source fingerprint and append-only attempts. `results.json` is a latest-attempt summary. Each attempt directory contains the requested case, raw worker output, stdout/stderr, process checks and sampled SMI telemetry. These standalone artifacts are not the normal `operatorx.core.Result` ingestion format. | ||
|
|
||
| ```bash | ||
| python -m pytest operatorx/tests/test_ascend_plan.py \ | ||
| operatorx/tests/test_ascend_queued_timing.py operatorx/tests/test_ascend_l0_guard.py | ||
| ``` | ||
|
|
||
| The guard fixture is captured host tiling data, not a vendor device binary. Tests compile the guard on the CPU and exercise real decoder and launch-hook behavior against a mock runtime. Native preflight is still required to establish Linux symbol interception on each installed stack. | ||
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,5 @@ | ||
| [ | ||
| {"type":"gemm","args":{"m":128,"n":512,"k":512,"a":{"dtype":"bf16"},"b":{"dtype":"bf16"},"out":"bf16"},"sources":[]}, | ||
| {"type":"gemm","args":{"m":128,"n":128,"k":128,"a":{"dtype":"bf16"},"b":{"dtype":"bf16"},"out":"fp32"},"sources":[]}, | ||
| {"type":"gemm","args":{"m":49152,"n":7168,"k":6144,"a":{"dtype":"bf16"},"b":{"dtype":"bf16"},"out":"bf16"},"sources":[]} | ||
| ] |
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,4 @@ | ||
| [ | ||
| {"type":"gemm","args":{"m":240,"n":2304,"k":7168,"a":{"dtype":"e4m3","scale":{"dtype":"fp32","group":[1,128],"static":false}},"b":{"dtype":"e4m3","scale":{"dtype":"fp32","group":[128,128],"static":true}},"out":"bf16"},"sources":[]}, | ||
| {"type":"gemm","args":{"m":264,"n":2304,"k":7168,"a":{"dtype":"e4m3","scale":{"dtype":"fp32","group":[1,128],"static":false}},"b":{"dtype":"e4m3","scale":{"dtype":"fp32","group":[128,128],"static":true}},"out":"bf16"},"sources":[]} | ||
| ] |
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,135 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import hashlib | ||
| import json | ||
| from collections import Counter | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| KERNEL = "QuantBatchMatmulV3_ND_ND_fp8e4m3_fp8e4m3_fp32_bf16_high_performance" | ||
|
|
||
|
|
||
| def validate_observation( | ||
| row: dict[str, Any], case: dict[str, Any], worker_sha: str | ||
| ) -> str: | ||
| if any(row.get(k) != case.get(k) for k in ("case_id", "opType", "args")): | ||
| raise ValueError("Guard case identity mismatch") | ||
| provenance = row.get("provenance", {}) | ||
| error = row.get("launch_exception", "") | ||
| guard = row.get("guard", {}) | ||
| if ( | ||
| row.get("status") != "inspected" | ||
| or "100000" not in error | ||
| or any(code in error for code in ("507015", "507035")) | ||
| or provenance.get("worker_sha256") != worker_sha | ||
| or provenance.get("task_queue_enable") != "0" | ||
| or guard.get("observed") is not True | ||
| or guard.get("mode") != 1 | ||
| or guard.get("forward_allowed") is not False | ||
| ): | ||
| raise ValueError("Guard did not establish an isolated prelaunch rejection") | ||
| if guard.get("decision") == "unverified": | ||
| return "skipped_unverified_tiling" | ||
| if guard.get("dispatch_key") != 5124 or guard.get("kernel_name") not in { | ||
| KERNEL, | ||
| KERNEL + "_5124", | ||
| KERNEL + "_5124_mix_aic", | ||
| KERNEL + "_5124_mix_aiv", | ||
| }: | ||
| raise ValueError("Unknown FP8 kernel ABI/key") | ||
| for dimension in ("m", "n", "k"): | ||
| if any( | ||
| guard.get(prefix + dimension) != case["args"][dimension] | ||
| for prefix in ("", "expected_") | ||
| ): | ||
| raise ValueError( | ||
| "Observed tiling dimensions differ from the requested case" | ||
| ) | ||
| for name, alignment in (("base_m", 16), ("base_n", 16), ("base_k", 32)): | ||
| value = guard.get(name) | ||
| if type(value) is not int or value <= 0 or value % alignment: | ||
| raise ValueError("Invalid local tile") | ||
| required = 2 * guard["base_m"] * guard["base_k"] | ||
| if guard.get("l0a_bytes") != required or guard.get("l0a_capacity_bytes") != 65536: | ||
| raise ValueError("Invalid L0A capacity calculation") | ||
| expected = "within_capacity" if required <= 65536 else "over_capacity" | ||
| if guard.get("decision") != expected: | ||
| raise ValueError("Guard decision contradicts its local tile") | ||
| return "new" if required <= 65536 else "skipped_l0a_capacity" | ||
|
|
||
|
|
||
| def apply_guard(plan: dict[str, Any], rows: list[dict[str, Any]]) -> dict[str, Any]: | ||
| if plan.get("kind") != "fp8": | ||
| raise ValueError("Only FP8 plans require this guard") | ||
| selected = {c["case_id"]: c for c in plan["cases"] if c["phase"] == "new"} | ||
| observations = {} | ||
| identities = set() | ||
| for row in rows: | ||
| cid = row.get("case_id") | ||
| if cid not in selected or cid in observations: | ||
| raise ValueError("Unexpected or duplicate guard case") | ||
| phase = validate_observation(row, selected[cid], plan["worker_sha256"]) | ||
| identity = row.get("hardware_identity") | ||
| if not isinstance(identity, str) or len(identity) != 64: | ||
| raise ValueError("Missing guard hardware identity") | ||
| identities.add(identity) | ||
| observations[cid] = phase | ||
| if set(observations) != set(selected) or len(identities) != 1: | ||
| raise ValueError("Incomplete guard coverage or mixed hardware identities") | ||
| result = json.loads(json.dumps(plan)) | ||
| for case in result["cases"]: | ||
| if case["case_id"] in observations: | ||
| case["phase"] = observations[case["case_id"]] | ||
| valid = {cid for cid, phase in observations.items() if phase == "new"} | ||
| result["smoke_case_ids"] = [cid for cid in plan["smoke_case_ids"] if cid in valid] | ||
| if valid and not result["smoke_case_ids"]: | ||
| result["smoke_case_ids"] = [sorted(valid)[0]] | ||
| result["counts"] = dict(Counter(c["phase"] for c in result["cases"])) | ||
| result["fp8_l0a"] = {"hardware_identity": identities.pop(), "observations": rows} | ||
| return result | ||
|
|
||
|
|
||
| def verify_plan(plan: dict[str, Any]) -> None: | ||
| proof = plan.get("fp8_l0a") | ||
| if not isinstance(proof, dict): | ||
| raise ValueError( | ||
| "FP8 requires an approved prelaunch L0A plan; run the guard first" | ||
| ) | ||
| original = json.loads(json.dumps(plan)) | ||
| for case in original["cases"]: | ||
| if case["phase"] in ("skipped_l0a_capacity", "skipped_unverified_tiling"): | ||
| case["phase"] = "new" | ||
| checked = apply_guard(original, proof["observations"]) | ||
| if ( | ||
| checked["cases"] != plan["cases"] | ||
| or checked["fp8_l0a"]["hardware_identity"] != proof["hardware_identity"] | ||
| ): | ||
| raise ValueError("FP8 plan differs from the validated tiling observations") | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--plan", type=Path, required=True) | ||
| parser.add_argument("--observations", type=Path, required=True) | ||
| parser.add_argument("--output", type=Path, required=True) | ||
| args = parser.parse_args() | ||
| plan = apply_guard( | ||
| json.loads(args.plan.read_text()), | ||
| [ | ||
| json.loads(line) | ||
| for line in args.observations.read_text().splitlines() | ||
| if line.strip() | ||
| ], | ||
| ) | ||
| plan["fp8_l0a"]["source_sha256"] = hashlib.sha256( | ||
| args.observations.read_bytes() | ||
| ).hexdigest() | ||
| with args.output.open("x") as stream: | ||
| json.dump(plan, stream, indent=2, allow_nan=False) | ||
| stream.write("\n") | ||
| print(json.dumps(plan["counts"])) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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.
🟡 WARNING: The documented build command omits
-pthread, but the guard usesstd::mutex/std::lock_guard(operatorx_fp8_l0_guard.cpp:29,110,124,137,155,171) and the test harness that validates this exact source does pass it (operatorx/tests/test_ascend_l0_guard.py:102).Why it matters: on toolchains whose glibc still keeps the pthread symbols in
libpthread(pre-2.34 — e.g. CentOS/EulerOS 7/8 images, which are common for CANN hosts), linking a shared object that usesstd::mutexwithout-pthreadresolves the locking primitives to the libc no-op stubs instead of erroring. The guard then runs with non-functional mutexes whileaclrtLaunchKernelWithHostArgsmutatesg_keys/g_lastfrom runtime threads, so the decision JSON thatoperatorx_fp8_l0_preflight.py:177reads back can be torn or stale — a silently wrong L0A verdict rather than a build failure. Building the shipped artifact differently from the tested one is the part worth closing.Fix: match the harness invocation.