惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
量子位
S
SegmentFault 最新的问题
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Threat Research - Cisco Blogs
I
Intezer
Hugging Face - Blog
Hugging Face - Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
G
Google Developers Blog
Cyberwarzone
Cyberwarzone
L
LINUX DO - 热门话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
Jina AI
Jina AI
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
AWS News Blog
AWS News Blog
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
T
Threatpost
大猫的无限游戏
大猫的无限游戏
I
InfoQ
T
Tor Project blog
Project Zero
Project Zero
F
Full Disclosure
L
Lohrmann on Cybersecurity
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
B
Blog
Apple Machine Learning Research
Apple Machine Learning Research
The Hacker News
The Hacker News
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
V2EX
The Cloudflare Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security

Benson's blog

Enjoy life Internship AI on academic research How AI Will Change the Mobile Ecosystem Look ahead Goodbye 2025 Hacker News to Kindle Another project How to imporve english Introduction of Fraud detection PopTranslate Last day in netease Better idea between Copilot-typed and CLI-typed assistant Gemini-cli LLM Post-Training experience Papers I readed recently about LLM application Difference between LLMs and traditional computer technology GRPO Weekly-#26 AI Application Weekly-#25 AI infra and application Weekly-#24 First week as LLM inference engineer Weekly-#23 seeking job Weekly-#22 2025 New Year AutoSwitch Translate Goodbye 2024 Weekly-#20 Breaking of glass Weekly-#18 Cross Entropy Loss of Triton Weekly-#17 Triton Puzzles Weekly-#16 AutoBuilder Weekly-#15 Starting of tanble tennis Weekly-#14 Accident in life Weekly-#13 Trying of xiaohongshu Weekly-#12 summary of LLM acceleration Outline of LLM acceleration Weekly-#11 Copilot-type products Weekly-#10 Preparation for next journey Weekly-#9 Startup of YouTube Notes of flash-attention How to learn knowledge in new fields? Weekly-#8 Start Reading Notes of LoRA Acceleration of LLM - Matrix Multiplication Weekly-#8 Summary for two month Weekly-#7 Staying home Weekly-#6 Cost of PopTranslate Weekly-#5 Updating of PopTranslate Validated example of LLM acceleration Weekly-#4 First insight of LLM accelerate Weekly-#3 PopTranslate Weekly-#2 The fail of first product Weekly-#1 First week of indie develop slack迁移discord 雅思备考 2024Q3 中文博客合集 English Diary in May 五一游记 开始休假 离职前的状态 2024-01-01 duckdb 看懂的第一个PR learning english in October learning english in September learning english in August top hack news 收集 大模型调研 自动驾驶的小玩具 旅游 扬州+苏州 small talk of learning english 新年新气象-碎碎念 刷剧 感染新冠 强化学习简介 神经网络解释性 全局的模型无关解释方法合集 社区发现算法概览 图神经网络入门(GNN) 我的第一款 iOS APP AtCoder Beginner Contest 268 人的信息输入方式对比 重叠社区检测 人穷极一生到底在追求什么 重拾生活规划 社区发现算法 - Louvain 《幸福的方法》 读《人类简史》有感 妙峰山骑行 黑客帝国 特征交互 特征工程 累计局部效应图 模型解释性-PDP 模型解释性 Web3 入门科普 总结 2022.4 孪生网络做 query 相似度任务 学习 2022.4 Imagen DeBERTa 读论文 用CNN做query相似度任务
Cross Entropy Loss of Triton
Benson · 2025-01-05 · via Benson's blog

Cross Entropy

idea of fast cross entropy

Based on previous knowledge on fast cross-entropy, realizing it by triton doesn’t spend too much.

There are only 1e-7 difference between Pytorch and my Triton kernel, But the speed is significantly lower.

Liger-Kernel

Getting advise on gpumode discord group, I can read how it realized in other great Triton kernel repository.

Liger-Kernel is the best option, but the speed is still lower than Pytorch, one result of them is 3053.16ms vs 0.04ms, even it save a lot of memmory, result is 0.51 vs 1.25

Attorch is another Triton kernel repository, withou any adobt, there aren’t much difference among My previous Triton kernel, Liger-Kernel and Attorch.

Pytorch is 1000x faster than Triton kernel which I have ever tried, it’s unreasonable. There should be another reason except core kernel code, but I cannot find it.

With help of friend in gpumode discord server, I found the environment variable is the root reason of self-made triton kernel. After realise the backward, the result as below picture show:

Speed test

code:

@triton.jit
def loss_kernel(input_ptr,
                target_ptr,
                output_ptr,
                M, N,
                BLOCK_SIZE_N: tl.constexpr,
               ):
    pid_m = tl.program_id(0)
    input_ptr += pid_m * N
    target_ptr += pid_m
    output_ptr += pid_m
    offsets_cols = tl.arange(0, BLOCK_SIZE_N)

    target = tl.load(target_ptr)
    max_val = -float("inf")
    sumexp = 0.0
    allcurx = 0.0

    for index in tl.range(0, N, BLOCK_SIZE_N):
        offsets_input = (offsets_cols + index)
        mask_input = (offsets_cols + index) < N
        input_val = tl.load(input_ptr + offsets_input, mask=mask_input, other = -float("inf"))

        if index == 0:
            new_max_val = tl.max(input_val)
        else:
            new_max_val = tl.maximum(max_val, tl.max(input_val))

        sumexp /= tl.exp(new_max_val)
        sumexp *= tl.exp(max_val)
        sumexp += tl.sum(tl.exp(input_val - new_max_val))
        max_val = new_max_val

        if (target >= index) and (target < index + BLOCK_SIZE_N):
            curx = tl.load(input_ptr + target)
            allcurx += curx
    for index in tl.range(0, N, BLOCK_SIZE_N):
        offsets_input = (offsets_cols + index)
        mask_input = (offsets_cols + index) < N
        input_val = tl.load(input_ptr + offsets_input, mask=mask_input, other = -float("inf"))
        target_1d = (offsets_cols + index)
        grad = tl.div_rn(tl.exp(input_val - max_val), sumexp) - tl.where(target_1d == target, 1, 0)
        tl.store(input_ptr + offsets_input, grad, mask = mask_input)
    output = tl.log(sumexp) + max_val - allcurx
    tl.store(output_ptr, output)

def triton_loss(input: torch.Tensor, target: torch.Tensor, bm = 1, bn = 256):
    output = torch.empty(target.shape, dtype = torch.float32, device = input.device)
    grid_loss = (input.shape[0], )
    loss_kernel[grid_loss](input, target, output, M = input.shape[0], N = input.shape[1], BLOCK_SIZE_N = bn)
    return output

This post is licensed under CC BY 4.0 by the author.