Skip to content

Commit 554f520

Browse files
authored
Merge pull request #628 from mingmingtasd/gelu
Define Gelu operation
2 parents e52f163 + 58d76da commit 554f520

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

index.bs

+71
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,77 @@ partial interface MLGraphBuilder {
28182818
</details>
28192819
</div>
28202820

2821+
### gelu ### {#api-mlgraphbuilder-gelu-method}
2822+
Compute the <a href="https://en.wikipedia.org/wiki/Rectifier_(neural_networks)#Gaussian-error_linear_unit_(GELU)">gaussian error linear unit function</a> (GELU) of the input tensor. The calculation follows the expression `0.5 * x * (1 + erf(x / sqrt(2)))`.
2823+
2824+
<script type=idl>
2825+
partial interface MLGraphBuilder {
2826+
MLOperand gelu(MLOperand input);
2827+
MLActivation gelu();
2828+
};
2829+
</script>
2830+
2831+
<div class="note">
2832+
<details open>
2833+
<summary>
2834+
The behavior of this operation can be generically emulated from the usage of
2835+
other operations as follows. However, user agents typically have a more
2836+
efficient implementation for it. Therefore its usage is encouraged from the
2837+
performance standpoint.
2838+
</summary>
2839+
<pre highlight="js">
2840+
return builder.mul(
2841+
builder.mul(x, builder.constant(0.5)),
2842+
builder.add(
2843+
builder.constant(1),
2844+
builder.erf(
2845+
builder.div(
2846+
x,
2847+
builder.sqrt(builder.constant(2))))));
2848+
</pre>
2849+
</details>
2850+
</div>
2851+
2852+
#### {{MLGraphBuilder/gelu(input)}} #### {#api-mlgraphbuilder-gelu-input}
2853+
<div>
2854+
**Arguments:**
2855+
- *input*: an {{MLOperand}}. The input tensor.
2856+
2857+
**Returns:**
2858+
- an {{MLOperand}}. The output tensor of the same shape as *input*.
2859+
</div>
2860+
2861+
<details open algorithm>
2862+
<summary>
2863+
The <dfn method for=MLGraphBuilder>gelu(|input|)</dfn> method steps are:
2864+
</summary>
2865+
1. If [=MLGraphBuilder/validating operand=] with [=this=] and |input| returns false, then [=exception/throw=] a {{TypeError}}.
2866+
1. *Make graph connections:*
2867+
1. Let |output| be the result of [=copying an MLOperand=] given |input|.
2868+
1. Let |operator| be an [=operator=] for the gelu operation.
2869+
1. Set |output|.{{MLOperand/[[operator]]}} to |operator|.
2870+
1. Set |operator|'s [=operator/input=] to |input|.
2871+
1. Set |operator|'s [=operator/output=] to |output|.
2872+
1. Return |output|.
2873+
</details>
2874+
2875+
#### {{MLGraphBuilder/gelu()}} #### {#api-mlgraphbuilder-gelu}
2876+
<div>
2877+
**Arguments:**
2878+
- None.
2879+
2880+
**Returns:**
2881+
- an {{MLActivation}}. The activation function representing the gelu operation.
2882+
</div>
2883+
2884+
<details open algorithm>
2885+
<summary>
2886+
The <dfn method for=MLGraphBuilder id=gelu-noargs>gelu()</dfn> method steps are:
2887+
</summary>
2888+
1. Let |op| be the result of [=creating an MLActivation=] given [=this=] and "gelu".
2889+
1. Return |op|.
2890+
</details>
2891+
28212892
### gemm ### {#api-mlgraphbuilder-gemm}
28222893
Calculate the [general matrix multiplication of the Basic Linear Algebra Subprograms](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Level_3). The calculation follows the expression `alpha * A * B + beta * C`, where `A` is a 2-D tensor with shape [M, K] or [K, M], `B` is a 2-D tensor with shape [K, N] or [N, K], and `C` is [=unidirectionally broadcastable=] to the shape [M, N]. `A` and `B` may optionally be transposed prior to the calculation.
28232894

0 commit comments

Comments
 (0)