AAlice_zhouupdate
c2155d54Add on Jul 15History
#!/usr/bin/env python
#Copyright (c) 2025 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.

"""
Vector Error Testing Agent - One-Click Pipeline (Python version)
Usage: python run_pipeline.py <operator_name> [base_dir]
Example: python run_pipeline.py Add
Example: python run_pipeline.py AddLayerNormQuant
"""

import os
import sys
import subprocess

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def run_step(description, cmd):
    print("\n" + "=" * 60)
    print("  " + description)
    print("=" * 60)
    result = subprocess.run(cmd, shell=True, cwd=BASE_DIR)
    if result.returncode != 0:
        print("ERROR: Step failed with return code " + str(result.returncode))
        return False
    return True


def main():
    if len(sys.argv) < 2:
        print("Usage: python run_pipeline.py <operator_name> [base_dir]")
        print("Example: python run_pipeline.py Add")
        print("Example: python run_pipeline.py AddLayerNormQuant")
        sys.exit(1)

    op_name = sys.argv[1]
    base_dir = sys.argv[2] if len(sys.argv) > 2 else BASE_DIR

    op_name_camel = ''.join(w.capitalize() for w in
                            ''.join(['_' + c.lower() if c.isupper() else c
                                     for c in op_name]).split('_'))
    output_dir = os.path.join(base_dir, 'output', op_name_camel)
    constraints_path = os.path.join(output_dir, 'constraints.yaml')
    xlsx_path = os.path.join(output_dir, op_name_camel + '.xlsx')
    results_path = os.path.join(output_dir, 'execution_results.json')
    report_path = os.path.join(output_dir, 'report', 'test_report.md')

    print("Operator: " + op_name)
    print("Output directory: " + output_dir)
    print("Base directory: " + base_dir)

    step1_cmd = 'python skills/error-testing-proto-parser/scripts/parse_proto.py "{0}" "{1}"'.format(
        op_name, base_dir)
    if not run_step("Step 1: Parse Proto and Generate Constraints", step1_cmd):
        sys.exit(1)

    if not os.path.exists(constraints_path):
        print("ERROR: Constraints file not found: " + constraints_path)
        sys.exit(1)

    step2_cmd = 'python skills/error-testing-case-generator/scripts/generate_cases.py "{0}" "{1}"'.format(
        constraints_path, output_dir)
    if not run_step("Step 2: Generate Test Cases", step2_cmd):
        sys.exit(1)

    step3_cmd = 'python skills/error-testing-executor/scripts/execute_error_cases.py "{0}" --base-dir "{1}"'.format(
        xlsx_path, base_dir)
    run_step("Step 3: Execute Error Cases (simulated)", step3_cmd)

    if os.path.exists(results_path):
        step4_cmd = 'python skills/error-testing-reporter/scripts/generate_report.py "{0}" "{1}"'.format(
            results_path, report_path)
        run_step("Step 4: Generate Test Report", step4_cmd)

    print("\n" + "=" * 60)
    print("  Pipeline Complete!")
    print("=" * 60)
    print("  Output directory: " + output_dir)
    print("    Constraints:  " + constraints_path)
    print("    Test cases:   " + xlsx_path)
    print("    Exec results: " + results_path)
    print("    Test report:  " + report_path)
    print("=" * 60)

    if os.path.exists(report_path):
        print()
        with open(report_path, 'r', encoding='utf-8') as f:
            print(f.read())


if __name__ == '__main__':
    main()