1. Job Roadmap
  2. Home
  3. Problem Set
  4. codenotelist
  5. Forum
  6. course
  7. Shore Share Sessions
  8. Record
  1. Login
  2. Sign Up
  3. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
    ZhContent TextSol AI分析

解题思路

  • 按题意用“暴力模拟”完整走一遍计算图:

    1. 构造 X 为 n×m 的全 1;构造 W1、W2、W3 为 m×h 的上三角全 1。
    2. 计算 Q=X·W1,K=X·W2,V=X·W3(普通三重循环矩阵乘法)。
    3. 计算 M=(Q·K^T)/sqrt(h)。
    4. 按“简化 softmax”把 M 的每一行做归一化:A[i][j]=M[i][j]/(该行元素和)。
    5. 计算 Y=A·V。

P3712.第2题-大模型Attention模块开发

    1000ms Tried: 3066 Accepted: 1018 Difficulty: 4 所属公司 : 华为
    算法与标签>模拟

题目内容

已知大模型常用的 Attention 模块定义如下:

Y=softmax(QKTh)VY = \text{softmax}\left(\frac{QK^T}{\sqrt{h}}\right)VY=softmax(h​QKT​)V

此处考虑二维情况,其中

Q,K,V=XW1,XW2,XW3∈Rn×h,X∈Rn×m,W1,W2,W3∈Rm×hQ, K, V = XW_1, XW_2, XW_3 \in \mathbb{R}^{n \times h}, \quad X \in \mathbb{R}^{n \times m}, \quad W_1, W_2, W_3 \in \mathbb{R}^{m \times h}Q,K,V=XW1​,XW2​,XW3​∈Rn×h,X∈Rn×m,W1​,W2​,W3​∈Rm×h

注意:

  1. 为简便起见,所有输入初始化为全1 1 1矩阵,所有权重矩阵初始化为上三角全 111 矩阵。

  2. 对任意矩阵 ( M ) 的 softmaxsoftmaxsoftmax 计算简化为:

softmax(M)ij=MijMi,Mi=∑jMij\text{softmax}(M)_{ij} = \frac{M_{ij}}{M_i}, \quad M_i = \sum_j M_{ij}softmax(M)ij​=Mi​Mij​​,Mi​=∑j​Mij​

输入描述

输入为维度参数 n,mn, mn,m和h hh,参数间使用空格隔开,均为小于 100100100 的正整数

输出描述

输出为结果矩阵 Y∈Rn×hY \in \mathbb{R}^{n \times h}Y∈Rn×h的所有元素之和,例如 151515,输出在四舍五入后保留整数

样例1

输入

3 3 3

输出

18 

说明

X=(111111111),W1,W2,W3=(111011001)X = \begin{pmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{pmatrix}, \quad W_1, W_2, W_3 = \begin{pmatrix} 1 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{pmatrix}X=​111​111​111​​,W1​,W2​,W3​=​100​110​111​​

Q,K,V=(123123123),Y=(123123123)Q, K, V = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}, \quad Y = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 2 & 3 \\ 1 & 2 & 3 \end{pmatrix}Q,K,V=​111​222​333​​,Y=​111​222​333​​

输出为:181818

样例2

输入

2 3 1

输出

2

说明

X=(111111),W1,W2,W3=(100)X = \begin{pmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \end{pmatrix}, \quad W_1, W_2, W_3 = \begin{pmatrix} 1 \\ 0 \\ 0 \end{pmatrix}X=(11​11​11​),W1​,W2​,W3​=​100​​

Q,K,V=(11),Y=(11)Q, K, V = \begin{pmatrix} 1 \\ 1 \end{pmatrix}, \quad Y = \begin{pmatrix} 1 \\ 1 \end{pmatrix}Q,K,V=(11​),Y=(11​)

输出为:222

提示

输入参数不包含 000,为正整数

登录后即可使用 AI 分析。

模式
倒计时时长
:

最长 10 小时 59 分;应用后按此时长重新开始。

提示:点击提交记录在左侧题面区域查看详情
题库
AI分析设置
留空使用官方API Key,每天有次数限制(自定义API Key仅限会员和管理员使用,不限次数)
会员和管理员可切换模型;切到 Kimi/智谱/通义/豆包时需填写对应供应商 API Key
升级会员,可将运行与提交冷却时间缩短至 1 秒起

Status

  • Judging Queue
  • Service Status

Development

  • Open Source

Support

  • Help
  • Contact Us

About

  • About
  • Privacy
  • Terms of Service
  • Copyright Complaint
  1. Language
    1. English
    2. 한국어
    3. 简体中文
    4. 正體中文
  2. Legacy mode
  3. Theme
    1. Light
    2. Dark
  1. 京ICP备2025123107号-1
  2. Worker 1, 84ms
  3. Powered by Hydro v5.0.0-beta.18 Community
CLOSE


ScanQRCodePrompt

请使用微信扫描下方二维码完成注册

Forgot password or username?