分析Profiler Timeline中的算子序列,通过寻找频繁项集的办法,得到TOPK可融合的算子序列

分析Profiler Timeline中的算子序列,通过寻找频繁项集的办法,得到TOPK可融合的算子序列

本文尝试分析Profiler Timeline中的算子序列,通过寻找频繁项集的办法,得到TOPK可融合的算子序列

1.相关链接

2.代码【仅分析带通信算子的Pattern】

python 复制代码
from collections import defaultdict, deque

def rolling_hash(s, base=257, mod=10**9 + 7):
    h = 0
    for ch in s:
        h = (h * base + ord(ch)) % mod
    return h

def find_top_n_fixed_length_sequences(arr, length, top_n):
    # 创建一个字典来存储子序列及其出现次数和偏移位置
    sequence_data = defaultdict(lambda: {"count": 0, "positions": []})
    base, mod = 257, 10**9 + 7
    
    # 滑动窗口计算固定长度子序列
    for i in range(len(arr) - length + 1):
        window = arr[i:i + length]
        if "all_gather" in window or "reduce_scatter" in window:  #只处理函通信算子的pattern
            flat_window = ''.join(window)
            h = rolling_hash(flat_window, base, mod)
            sequence_data[h]['count'] += 1
            sequence_data[h]['positions'].append(i)
        
    # 按照出现频率排序,并获取前N个子序列
    sorted_sequences = sorted(sequence_data.items(), key=lambda item: item[1]['count'], reverse=True)
    top_sequences = sorted_sequences[:top_n]
    
    return top_sequences, sequence_data
	
# 加载profiler生成的timeline,提取出算子名列表及偏移未知,这里构造了一个简单的数据
operators=["mm","all_gather","binary_add","dropout_backward","fill","eltwise_silu","mm","all_gather","fill"]
offsets=range(0,len(operators))

# 要求最少两个元素的子序列,且取前3个出现频率最高的长度为2的子序列
length = 2
top_n = 1

# 获取前N个频繁的长度为固定长度的子序列
top_sequences, sequence_data = find_top_n_fixed_length_sequences(operators, length, top_n)

# 反向查找实际的序列值
reverse_lookup = {}
for i in range(len(operators) - length + 1):
    window = operators[i:i + length]
    flat_window = ''.join(window)
    h = rolling_hash(flat_window)
    if h not in reverse_lookup:
        reverse_lookup[h] = window

# 输出结果并去重
unique_sequences = set()  # 用来跟踪已经输出的序列
for seq_hash, data in top_sequences:
    seq = reverse_lookup[seq_hash]
    seq_tuple = tuple(seq)
    if seq_tuple not in unique_sequences:
        unique_sequences.add(seq_tuple)
        positions = sequence_data[seq_hash]['positions']
        print(f'序列: {seq}, 出现频率: {data["count"]}')
        for pos in positions:
            beg=pos
            end=pos+length
            ts_beg=offsets[beg]
            ts_end=offsets[end]
            print(ts_beg,ts_end,operators[ts_beg:ts_end])

DEMO 输出

bash 复制代码
序列: ['mm', 'all_gather'], 出现频率: 2
0 2 ['mm', 'all_gather']
6 8 ['mm', 'all_gather']

3.在实际工程中发现 ['all_gather', 'matrix_mm_out']频率最高

4.Ascend MC2

5.torch_npu.npu_all_gather_base_mm

相关推荐
Gary Studio3 分钟前
基于PMSM理论研究加实践
算法
悟空瞎说3 分钟前
前端性能优化进阶指南:从底层原理到工程化闭环
面试·性能优化
AI人工智能+电脑小能手4 分钟前
【大白话说Java面试题】【Java基础篇】第9题:HashMap根据key查询元素的时间复杂度是多少
java·开发语言·数据结构·后端·面试·哈希算法·哈希表
HHHHH1010HHHHH5 分钟前
JavaScript中利用IIFE创建私有命名空间的经典方案
jvm·数据库·python
木子墨5169 分钟前
LeetCode 热题 100 精讲 | 矩阵与图论进阶篇:矩阵置零 · 螺旋矩阵 · 旋转图像 · 搜索二维矩阵 II · 岛屿数量 · 腐烂的橘子
c++·算法·leetcode·矩阵·力扣·图论
干洋芋果果10 分钟前
前端学python
开发语言·前端·python
YJlio11 分钟前
1 1.2 Windows 账户的分类:管理员 / 标准 / 来宾 + 微软账户 vs 本地账户
人工智能·python·microsoft·ai·chatgpt·openai·agent
Ailan_Anjuxi11 分钟前
【附jupyter源码】使用长短期记忆网络(LSTM)实现一个小说写作AI——以训练《西游记》为例
人工智能·算法
stolentime11 分钟前
线段树套?——洛谷P7312 [COCI 2018/2019 #2] Sunčanje题解
c++·算法·图论·洛谷
篮子里的玫瑰18 分钟前
Python与网络爬虫——列表与元组
开发语言·爬虫·python