forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
[added] gen and check script slow #2
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
unseenme
wants to merge
3
commits into
tc20042008:monkey
Choose a base branch
from
unseenme:feature_slow
base: monkey
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import time | ||
|
|
||
| def process_py_files_until_file_exists(directory, flag_file): | ||
| """ | ||
| Continuously process Python files in the specified directory until the flag file exists. | ||
|
|
||
| Args: | ||
| - directory (str): The directory to search for Python files. | ||
| - flag_file (str): The name of the flag file to check for existence. | ||
|
|
||
| Returns: | ||
| - int: The number of times the test function was called. | ||
| """ | ||
| selection_dir = os.path.join(directory, "selection") | ||
| if not os.path.exists(selection_dir): | ||
| os.makedirs(selection_dir) | ||
| cnt = 0 | ||
| cnt_slc = 0 | ||
| while not check_file_existence(directory, flag_file): | ||
| py_files = [file for file in os.listdir(directory) if file.endswith(".py")] | ||
|
|
||
| if py_files: | ||
| for py_file in py_files: | ||
| file_path = os.path.join(directory, py_file) | ||
| try: | ||
| subprocess.run(["python", file_path], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | ||
| # print("rm") | ||
| os.remove(file_path) | ||
| except subprocess.CalledProcessError: | ||
| # print("move") | ||
| shutil.move(file_path, selection_dir) | ||
| cnt_slc += 1 | ||
| cnt += 1 | ||
| print(f"\rTested: {cnt}. Selection: {cnt_slc}.", end="") | ||
| # os.remove(file_path) | ||
| else: | ||
| time.sleep(0.05) # Sleep for 50 milliseconds (adjust as needed) | ||
|
|
||
| def check_file_existence(directory, filename): | ||
| """ | ||
| Check if the specified file exists in the given directory. | ||
|
|
||
| Args: | ||
| - directory (str): The directory to search for the file. | ||
| - filename (str): The name of the file to check for. | ||
|
|
||
| Returns: | ||
| - bool: True if the file exists, False otherwise. | ||
| """ | ||
| file_path = os.path.join(directory, filename) | ||
| return os.path.isfile(file_path) | ||
|
|
||
| if __name__ == "__main__": | ||
| call_count = process_py_files_until_file_exists("/dev/shm/test", "stop") | ||
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,274 @@ | ||
| from dataclasses import dataclass | ||
| from collections import namedtuple | ||
| from typing import Generator, Union | ||
| from defensive_list import DList | ||
| from dag_generator import PickWeight | ||
| from script import Script | ||
| import dag_generator | ||
| import dim_eq1_generator as dim_eq1_generator | ||
| import dims_eq1_generator as dims_eq1_generator | ||
| import op_name_generator as op_name_generator | ||
| import tensor_name_generator as tensor_name_generator | ||
| from tensor_name_generator import TensorNameGenRequirement | ||
| import shape_signature_inferer as shape_signature_inferer | ||
| from shape_signature_inferer import StaticDim | ||
| import instruction_util as instruction_util | ||
| import op_call_code_gen | ||
| from paddle_eager_generator import PaddleEagerGenerator | ||
| from numpy_generator import NumpyGenerator | ||
| from unit_test_case_spec import ( | ||
| UnitTestCaseRequirement, | ||
| UnitTestCaseSpec, | ||
| GenerateRandomUnitTestCaseSpec, | ||
| GetAblatedUnitTestCaseSpec | ||
| ) | ||
| import datetime | ||
| import os | ||
| import sys | ||
| import time | ||
|
|
||
| content_head = """ | ||
| import os | ||
| os.environ['FLAGS_cinn_new_group_scheduler'] = '1' | ||
| os.environ['FLAGS_group_schedule_tiling_first'] = '1' | ||
| os.environ['FLAGS_prim_all'] = 'true' | ||
| os.environ['FLAGS_prim_enable_dynamic'] = '1' | ||
| os.environ['FLAGS_print_ir'] = '1' | ||
| os.environ['FLAGS_enable_pir_api'] = '1' | ||
| os.environ['FLAGS_cinn_bucket_compile'] = '1' | ||
|
|
||
| import unittest | ||
| import numpy as np | ||
| import paddle | ||
|
|
||
|
|
||
| class CinnMonkeyNet(paddle.nn.Layer): | ||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| """ | ||
|
|
||
| content_body = """ | ||
| class TestCinnMonkey(unittest.TestCase): | ||
| def setUp(self): | ||
| paddle.seed(2024) | ||
| self.prepare_data() | ||
|
|
||
| def prepare_data(self): | ||
| """ | ||
|
|
||
| content_tail = """ | ||
| def apply_to_static(self, net, use_cinn, input_spec=None): | ||
| build_strategy = paddle.static.BuildStrategy() | ||
| build_strategy.build_cinn_pass = use_cinn | ||
| return paddle.jit.to_static( | ||
| net, | ||
| input_spec=input_spec, | ||
| build_strategy=build_strategy, | ||
| full_graph=True, | ||
| ) | ||
|
|
||
| def train(self, use_cinn): | ||
| net = CinnMonkeyNet() | ||
| net.eval() | ||
| net = self.apply_to_static(net, use_cinn) | ||
| """ | ||
|
|
||
| content_main = """ | ||
| return out | ||
|
|
||
| def test_train(self): | ||
| cinn_out = self.train(use_cinn=True) | ||
| dy_out = self.train(use_cinn=False) | ||
|
|
||
| np.testing.assert_allclose(cinn_out.numpy(), dy_out.numpy(), atol=1e-6) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
| """ | ||
|
|
||
| def GenerateUnitTestCaseSpec( | ||
| unit_test_case_requirement: UnitTestCaseRequirement | ||
| ) -> UnitTestCaseSpec: | ||
| return GenerateRandomUnitTestCaseSpec( | ||
| requirement=unit_test_case_requirement | ||
| ) | ||
|
|
||
| def generate_content_neck(tensors_list): | ||
| num_tensors = len(tensors_list) | ||
| forward_function = " def forward(self" | ||
| for i in range(num_tensors): | ||
| forward_function += f", x{i+1}" | ||
| forward_function += "):\n" | ||
|
|
||
| for i, tensor_name in enumerate(tensors_list): | ||
| forward_function += f" {tensor_name} = x{i+1}\n" | ||
| forward_function += "\n" | ||
|
|
||
| return forward_function | ||
|
|
||
| def generate_content_prepare(shape_list): | ||
| content = '' | ||
| for i, shape in enumerate(shape_list): | ||
| content += f' self.x{i+1} = paddle.uniform([{shape}], dtype="float32", min=-0.5, max=0.5)\n' | ||
| content += f' self.x{i+1}.stop_gradient = True\n' | ||
| return content | ||
|
|
||
| def generate_content_train(num): | ||
| content = ' out = net(' | ||
| for i in range(num): | ||
| content += f'self.x{i+1}, ' | ||
| content = content[:-2] + ')' | ||
| return content | ||
|
|
||
| def get_content_and_write(dir): | ||
| unit_test_case_requirement=UnitTestCaseRequirement( | ||
| dag_gen_requirement=dag_generator.DAGGenRequirement( | ||
| min_num_sources=0, | ||
| max_num_sources=0, | ||
| pick_probability=dag_generator.DAGGenTypePickProbability() | ||
| ), | ||
| dims_eq1_gen_requirement=dims_eq1_generator.DimsEq1GenRequirement( | ||
| dims_eq1_probability=[0.1, 0.15, 0.2] | ||
| ), | ||
| op_name_gen_requirement=op_name_generator.OpNameGenRequirement(), | ||
| tensor_name_gen_requirement=TensorNameGenRequirement(), | ||
| dim_size_requirement=shape_signature_inferer.DimSizeRequirement( | ||
| dim_size=[StaticDim(128), StaticDim(64), StaticDim(32)] | ||
| ) | ||
| ) | ||
| unit_test_case_spec = GenerateUnitTestCaseSpec( | ||
| unit_test_case_requirement=unit_test_case_requirement | ||
| ) | ||
|
|
||
| # numpy_gen = NumpyGenerator() | ||
|
|
||
| paddle_eager_gen = PaddleEagerGenerator() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里基本上跑不出BUG来,因为这是eager。 |
||
| # script = numpy_gen.Generate(unit_test_case_spec.patched_instruction_code_gen_spec) | ||
| # print("import numpy") | ||
| # print(script.file_content) | ||
|
|
||
| script = paddle_eager_gen.Generate(unit_test_case_spec.patched_instruction_code_gen_spec) | ||
| # print("import paddle") | ||
| # print(script.file_content) | ||
| lines = script.file_content.strip().split('\n') | ||
|
|
||
| input_names = [] | ||
| input_shapes = [] | ||
| net_lines = [] | ||
| is_input_line = True | ||
| for line in lines: | ||
| if is_input_line: | ||
| if line.startswith('#'): | ||
| is_input_line = False | ||
| net_lines.append(line) | ||
| if line.startswith('tensor'): | ||
| input_names.append(line.split()[0]) | ||
| start_index = line.find("((") | ||
| end_index = line.find("))") | ||
| if start_index != -1 and end_index != -1: | ||
| input_shapes.append(line[start_index + 2:end_index]) | ||
| else: | ||
| net_lines.append(line) | ||
|
|
||
| out_tensor_name = None | ||
| for line in reversed(net_lines): | ||
| if not line.startswith('assert'): | ||
| out_tensor_name = line.split()[0] | ||
| break | ||
|
|
||
| # dir = "/home/aistudio/data" | ||
| # dir = "/dev/shm/test" | ||
| current_datetime = datetime.datetime.now() | ||
| curr_time = current_datetime.strftime("%Y-%m-%d_%H-%M-%S-%f") | ||
| file_name = dir + "/testcase_" + curr_time + ".py" | ||
| with open(file_name, "w") as f: | ||
| f.write(content_head) | ||
| f.write(generate_content_neck(input_names)) | ||
| for line in net_lines: | ||
| if line.startswith("assert"): | ||
| parts = line.split(" ") | ||
| parts[1] = "tuple(" + parts[1] + ")" | ||
| line = " ".join(parts) | ||
| f.write(" " + line + "\n") | ||
| f.write(" return " + out_tensor_name + "\n\n") | ||
| f.write(content_body) | ||
| f.write(generate_content_prepare(input_shapes)) | ||
| f.write(content_tail) | ||
| f.write(generate_content_train(len(input_names))) | ||
| f.write(content_main) | ||
|
|
||
| def check_file_existence(directory, filename): | ||
| """ | ||
| Check if the specified file exists in the given directory. | ||
|
|
||
| Args: | ||
| - directory (str): The directory to search for the file. | ||
| - filename (str): The name of the file to check for. | ||
|
|
||
| Returns: | ||
| - bool: True if the file exists, False otherwise. | ||
| """ | ||
| file_path = os.path.join(directory, filename) | ||
| return os.path.isfile(file_path) | ||
|
|
||
| def count_python_files(directory): | ||
| """ | ||
| Count the number of Python files in the specified directory. | ||
|
|
||
| Args: | ||
| - directory (str): The directory to search for Python files. | ||
|
|
||
| Returns: | ||
| - int: The number of Python files in the directory. | ||
| """ | ||
| python_files = [file for file in os.listdir(directory) if file.endswith(".py")] | ||
| return len(python_files) | ||
|
|
||
| def generate_test_cases_until_file_exists(directory, flag_file, max_python_files): | ||
| """ | ||
| Continuously generate test cases until the specified file exists. | ||
|
|
||
| Args: | ||
| - directory (str): The directory to search for Python files. | ||
| - flag_file (str): The name of the file to check for existence. | ||
| - max_python_files (int): The maximum number of Python files allowed before pausing. | ||
|
|
||
| Returns: | ||
| - None | ||
| """ | ||
| while not check_file_existence(directory, flag_file): | ||
| python_file_count = count_python_files(directory) | ||
|
|
||
| if python_file_count < max_python_files: | ||
| print("py", python_file_count) | ||
| get_content_and_write(directory) | ||
| # time.sleep(0.05) | ||
| else: | ||
| time.sleep(0.5) | ||
|
|
||
| def process_arguments(): | ||
| if len(sys.argv) < 4: | ||
| arg_dir = "/dev/shm/test" | ||
| arg_flag = "stop" | ||
| arg_max = 10 | ||
| else: | ||
| arg_dir = sys.argv[1] | ||
| arg_flag = sys.argv[2] | ||
| arg_max = int(sys.argv[3]) | ||
|
|
||
| return arg_dir, arg_flag, arg_max | ||
|
|
||
| def main(): | ||
| directory, flag_file, max_python_files = process_arguments() | ||
| print("dir:", directory) | ||
| print("flag:", flag_file) | ||
| print("max:", max_python_files) | ||
|
|
||
| if not os.path.exists(directory): | ||
| os.makedirs(directory) | ||
| generate_test_cases_until_file_exists(directory, flag_file, max_python_files) | ||
|
|
||
| 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,19 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright (c) 2021 CINN Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| nohup python3 gen.py & | ||
|
|
||
| python3 check.py |
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.
这个文件看起来是不太需要的。只要每个单测脚本按照python unittest规范来。它会汇报失败case。