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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Security Latest
Security Latest
Recorded Future
Recorded Future
S
Secure Thoughts
P
Privacy International News Feed
L
Lohrmann on Cybersecurity
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google DeepMind News
Google DeepMind News
L
LINUX DO - 热门话题
T
The Blog of Author Tim Ferriss
T
Threatpost
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
V
Vulnerabilities – Threatpost
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
S
Schneier on Security
S
Security @ Cisco Blogs
S
Securelist
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
H
Heimdal Security Blog
D
DataBreaches.Net
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
C
Check Point Blog
D
Docker

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.