-
Notifications
You must be signed in to change notification settings - Fork 26
feat: 添加嵌入算子 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
YdrMaster
wants to merge
2
commits into
InfiniTensor:main
Choose a base branch
from
YdrMaster:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: 添加嵌入算子 #15
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| | ||
| # `EmbeddingBackward` | ||
|
|
||
| `EmbeddingBackward`,即[**嵌入**算子](/infiniop/ops/embedding/README.md)的反向算子。用于训练大模型的词嵌入和加性位置嵌入。 | ||
|
|
||
| `EmbeddingBackward` 算子支持 1 个或 2 个相同的步骤,根据“号码”从将输出的梯度叠加到嵌入表的梯度,其公式表述为: | ||
|
|
||
| $$ \begin{equation} d_{table1} = \alpha_1 \cdot dy[i_1] \end{equation} $$ | ||
|
|
||
| $$ \begin{equation} d_{table2} = \alpha_2 \cdot dy[i_2] \end{equation} $$ | ||
|
|
||
| - 通常 $α$ 为 1; | ||
| - $table2$ 可以不使用,则公式 $(2)$ 不存在; | ||
|
|
||
| ## 接口 | ||
|
|
||
| ### 计算 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopEmbeddingBackward( | ||
| infiniopEmbeddingBackwardDescriptor_t desc, | ||
| void *dtable1, | ||
| void *dtable2, | ||
| const void *dy, | ||
| const void *i1, | ||
| const void *i2, | ||
| void *stream | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数: </div> | ||
|
|
||
| - `desc`: | ||
| 已使用 `infiniopEmbeddingBackwardDescriptor_t()` 初始化的算子描述符; | ||
| - `dtable1`: | ||
| 第 1 个嵌入表的梯度; | ||
| - `dtable2`: | ||
| 第 2 个嵌入表的梯度,不使用则为空; | ||
| - `dy`: | ||
| 输出结果的梯度; | ||
| - `i1`: | ||
| 第 1 个嵌入序号; | ||
| - `i2`: | ||
| 第 2 个嵌入序号,不使用则为空; | ||
| - `stream`: | ||
| 计算流/队列; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值:</div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_PARAM`], [`INFINI_STATUS_BAD_DEVICE`], [`INFINI_STATUS_EXECUTION_FAILED`]. | ||
|
|
||
| ### 创建算子描述 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopCreateEmbeddingBackwardDescriptor( | ||
| infiniopHandle_t handle, | ||
| infiniopEmbeddingBackwardDescriptor_t *desc_ptr, | ||
| infiniopTensorDescriptor_t dtable1_desc, | ||
| infiniopTensorDescriptor_t dtable2_desc, | ||
| infiniopTensorDescriptor_t dy_desc, | ||
| infiniopTensorDescriptor_t i1_desc, | ||
| infiniopTensorDescriptor_t i2_desc, | ||
| float alpha1, | ||
| float alpha2, | ||
| char dtable1_acc, | ||
| char dtable2_acc | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数:</div> | ||
|
|
||
| - `handle`: | ||
| `infiniopHandle_t` 类型的硬件控柄。详情请看:[`InfiniopHandle_t`] | ||
| - `desc_ptr`: | ||
| `infiniopCreateEmbeddingBackwardDescriptor` 指针,指向将被初始化的算子描述符地址; | ||
| - `dtable1_desc` - { dT | (N1, D) | (..., 1) }: | ||
| 算子输入 `table1` 的张量描述; | ||
| - `dtable2_desc` - { dT | (N2, D) | (..., 1) }: | ||
| 算子输入 `table2` 的张量描述; | ||
| - `dy_desc` - { dT | (N, D) | (..., 1) }: | ||
| 算子输出 `y` 的张量描述; | ||
| - `i1_desc` - { dI | (N) | (1) }: | ||
| 算子输入 `i1` 的张量描述; | ||
| - `i2_desc` - { dI | (N) | (1) }: | ||
| 算子输入 `i2` 的张量描述,为空表示不使用 $ table2 $, `alpha2` 必须同时为 0; | ||
| - `alpha1` - float: | ||
| 第 1 项嵌入的缩放因子; | ||
| - `alpha2` - float: | ||
| 第 2 项嵌入的缩放因子,取 0 表示不使用 $ table2 $,`i2_desc` 必须同时为空; | ||
| - `dtable1_acc` - char: | ||
| 第 1 项嵌入是否叠加梯度,0 表示不叠加; | ||
| - `dtable2_acc` - float: | ||
| 第 2 项嵌入是否叠加梯度,0 表示不叠加; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数限制:</div> | ||
|
|
||
| - $dT$: 任意代数类型; | ||
| - $dT_i$: 任意整型; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值:</div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_PARAM`], [`INFINI_STATUS_BAD_TENSOR_SHAPE`], [`INFINI_STATUS_BAD_TENSOR_DTYPE`], [`INFINI_STATUS_BAD_TENSOR_STRIDES`], [`INFINI_STATUS_BAD_DEVICE`]. | ||
|
|
||
| ### 销毁算子描述符 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopDestroyEmbeddingBackwardDescriptor( | ||
| infiniopEmbeddingBackwardDescriptor_t desc | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数: </div> | ||
|
|
||
| - `desc`: | ||
| 输入。待销毁的算子描述符; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值: </div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_DEVICE`]. | ||
|
|
||
| <!-- 链接 --> | ||
| [`InfiniopHandle_t`]: /infiniop/handle/README.md | ||
|
|
||
| [`INFINI_STATUS_SUCCESS`]:/common/status/README.md#INFINI_STATUS_SUCCESS | ||
| [`INFINI_STATUS_BAD_PARAM`]:/common/status/README.md#INFINI_STATUS_BAD_PARAM | ||
| [`INFINI_STATUS_BAD_DEVICE`]:/common/status/README.md#INFINI_STATUS_BAD_DEVICE | ||
| [`INFINI_STATUS_EXECUTION_FAILED`]:/common/status/README.md#INFINI_STATUS_EXECUTION_FAILED | ||
| [`INFINI_STATUS_BAD_TENSOR_SHAPE`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_SHAPE | ||
| [`INFINI_STATUS_BAD_TENSOR_DTYPE`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_DTYPE | ||
| [`INFINI_STATUS_BAD_TENSOR_STRIDES`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_STRIDES |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| | ||
| # `Embedding` | ||
|
|
||
| `Embedding`,即**嵌入**算子。用于对大模型进行词嵌入和加性位置嵌入。 | ||
|
|
||
| `Embedding` 算子支持 1 个或 2 个相同的步骤,根据“号码”从嵌入表中获取嵌入向量并叠加到输出,其公式表述为: | ||
|
|
||
| $$ Y = \alpha_1 \cdot table_1[i_1] + \alpha_2 \cdot table_2[i_2] $$ | ||
|
|
||
| - 通常 $α$ 为 1; | ||
| - $table2$ 可以不使用,则公式变为 $Y = \alpha_1 \cdot table_1[i_1]$; | ||
|
|
||
| ## 接口 | ||
|
|
||
| ### 计算 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopEmbedding( | ||
| infiniopEmbeddingDescriptor_t desc, | ||
| void *y, | ||
| const void *table1, | ||
| const void *table2, | ||
| const void *i1, | ||
| const void *i2, | ||
| void *stream | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数: </div> | ||
|
|
||
| - `desc`: | ||
| 已使用 `infiniopCreateEmbeddingDescriptor()` 初始化的算子描述符; | ||
| - `y`: | ||
| 计算输出结果; | ||
| - `table1`: | ||
| 第 1 个嵌入表; | ||
| - `table2`: | ||
| 第 2 个嵌入表,不使用则为空; | ||
| - `i1`: | ||
| 第 1 个嵌入序号; | ||
| - `i2`: | ||
| 第 2 个嵌入序号,不使用则为空; | ||
| - `stream`: | ||
| 计算流/队列; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值:</div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_PARAM`], [`INFINI_STATUS_BAD_DEVICE`], [`INFINI_STATUS_EXECUTION_FAILED`]. | ||
|
|
||
| ### 创建算子描述 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopCreateEmbeddingDescriptor( | ||
| infiniopHandle_t handle, | ||
| infiniopEmbeddingDescriptor_t *desc_ptr, | ||
| infiniopTensorDescriptor_t y_desc, | ||
| infiniopTensorDescriptor_t table1_desc, | ||
| infiniopTensorDescriptor_t table2_desc, | ||
| infiniopTensorDescriptor_t i1_desc, | ||
| infiniopTensorDescriptor_t i2_desc, | ||
| float alpha1, | ||
| float alpha2 | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数:</div> | ||
|
|
||
| - `handle`: | ||
| `infiniopHandle_t` 类型的硬件控柄。详情请看:[`InfiniopHandle_t`] | ||
| - `desc_ptr`: | ||
| `infiniopCreateEmbeddingDescriptor` 指针,指向将被初始化的算子描述符地址; | ||
| - `y_desc` - { dT | (N, D) | (..., 1) }: | ||
| 算子输出 `y` 的张量描述; | ||
| - `table1_desc` - { dT | (N1, D) | (..., 1) }: | ||
| 算子输入 `table1` 的张量描述; | ||
| - `table2_desc` - { dT | (N2, D) | (..., 1) }: | ||
| 算子输入 `table2` 的张量描述; | ||
| - `i1_desc` - { dI | (N) | (1) }: | ||
| 算子输入 `i1` 的张量描述; | ||
| - `i2_desc` - { dI | (N) | (1) }: | ||
| 算子输入 `i2` 的张量描述,为空表示不使用 $ table2 $, `alpha2` 必须同时为 0; | ||
| - `alpha1` - float: | ||
| 第 1 项嵌入的缩放因子; | ||
| - `alpha2` - float: | ||
| 第 2 项嵌入的缩放因子,取 0 表示不使用 $ table2 $,`i2_desc` 必须同时为空; | ||
|
|
||
| 参数限制: | ||
|
|
||
| - $dT$: 任意代数类型; | ||
| - $dT_i$: 任意整型; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值:</div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_PARAM`], [`INFINI_STATUS_BAD_TENSOR_SHAPE`], [`INFINI_STATUS_BAD_TENSOR_DTYPE`], [`INFINI_STATUS_BAD_TENSOR_STRIDES`], [`INFINI_STATUS_BAD_DEVICE`]. | ||
|
|
||
| ### 销毁算子描述符 | ||
|
|
||
| ```c | ||
| infiniStatus_t infiniopDestroyEmbeddingDescriptor( | ||
| infiniopEmbeddingDescriptor_t desc | ||
| ); | ||
| ``` | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 参数: </div> | ||
|
|
||
| - `desc`: | ||
| 输入。待销毁的算子描述符; | ||
|
|
||
| <div style="background-color: lightblue; padding: 1px;"> 返回值: </div> | ||
|
|
||
| - [`INFINI_STATUS_SUCCESS`], [`INFINI_STATUS_BAD_DEVICE`]. | ||
|
|
||
| <!-- 链接 --> | ||
| [`InfiniopHandle_t`]: /infiniop/handle/README.md | ||
|
|
||
| [`INFINI_STATUS_SUCCESS`]:/common/status/README.md#INFINI_STATUS_SUCCESS | ||
| [`INFINI_STATUS_BAD_PARAM`]:/common/status/README.md#INFINI_STATUS_BAD_PARAM | ||
| [`INFINI_STATUS_BAD_DEVICE`]:/common/status/README.md#INFINI_STATUS_BAD_DEVICE | ||
| [`INFINI_STATUS_EXECUTION_FAILED`]:/common/status/README.md#INFINI_STATUS_EXECUTION_FAILED | ||
| [`INFINI_STATUS_BAD_TENSOR_SHAPE`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_SHAPE | ||
| [`INFINI_STATUS_BAD_TENSOR_DTYPE`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_DTYPE | ||
| [`INFINI_STATUS_BAD_TENSOR_STRIDES`]:/common/status/README.md#INFINI_STATUS_BAD_TENSOR_STRIDES | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.