diff --git a/profiler/compare_tools/compare_backend/utils/common_func.py b/profiler/compare_tools/compare_backend/utils/common_func.py index 68a1ab584f1514980bc784f4a55152efffe698cf..b0929ee191bf2896b573350a5b01e363af9425c4 100644 --- a/profiler/compare_tools/compare_backend/utils/common_func.py +++ b/profiler/compare_tools/compare_backend/utils/common_func.py @@ -93,3 +93,82 @@ def longest_common_subsequence_matching(base_ops: list, comparison_ops: list, na base_index -= 1 matched_op.reverse() return matched_op + + +def longest_common_subsequence_matching_new(base_ops: list, comparison_ops: list, name_func: any) -> list: + if not comparison_ops: + result_data = [None] * len(base_ops) + for index, value in enumerate(base_ops): + result_data[index] = [value, None] + return result_data + + result_data = [] + base_ops_name_map = {} + base_index = 0 + for op in base_ops: + base_ops_name_map.setdefault(name_func(op), []).append(base_index) + base_index += 1 + lis_list = [] + comparison_index_lis_map = {} + comparison_index = 0 + for op in comparison_ops: + in_base_ops_index = base_ops_name_map.get(name_func(op), []) + in_base_ops_index.reverse() + for base_index in in_base_ops_index: + comparison_index_lis_map[len(lis_list)] = comparison_index + lis_list.append(base_index) + comparison_index += 1 + lis_result = SequenceOfLIS(lis_list) + base_index, comparison_index = 0, 0 + for index in lis_result: + matched_base_index = lis_list[index] + matched_comparison_index = comparison_index_lis_map.get(index) + while base_index < matched_base_index: + result_data.append([base_ops[base_index], None]) + base_index += 1 + while comparison_index < matched_comparison_index: + result_data.append([None, comparison_ops[comparison_index]]) + comparison_index += 1 + result_data.append([base_ops[matched_base_index], comparison_ops[matched_comparison_index]]) + base_index += 1 + comparison_index += 1 + while base_index < len(base_ops): + result_data.append([base_ops[base_index], None]) + base_index += 1 + while comparison_index < len(comparison_ops): + result_data.append([None, comparison_ops[comparison_index]]) + comparison_index += 1 + return result_data + + +def SequenceOfLIS(nums: List[int]) -> int: + """ + 返回最长递增序列,以输入list的index表示 + """ + recall_list = [None] * len(nums) + lis_length = [0] + for index, num in enumerate(nums): + if not index: + continue + if num > nums[lis_length[-1]]: + lis_length.append(index) + recall_list[index] = lis_length[-1] + else: + l, r = 0, len(lis_length) - 1 + loc = r + while l <= r: + mid = (l + r) // 2 + if nums[lis_length[mid]] >= num: + loc = mid + r = mid - 1 + else: + l = mid + 1 + lis_length[loc] = index + if loc > 0: + recall_list[index] = lis_length[loc - 1] + result = [nums[lis_length[-1]]] + pre_index = lis_length[-1] + while pre_index is not None: + result.append(nums[pre_index]) + pre_index = recall_list[pre_index] + return result.reservse() \ No newline at end of file