Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/fast/gelu-quick/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# geluQuick

> Evaluate the [Gaussian error linear unit][activation-function] (GELU) using a quick 1.702 approximation.

<section class="intro">

The [Gaussian error linear unit][activation-function] (GELU) using a quick 1.702 approximation is defined as:

<!-- <equation class="equation" label="eq:gelu_quick" align="center" raw="\mathrm{gelu}(x) \approx x \cdot \operatorname{expit}( 1.702 \, x )" alt="GELU quick approximation."> -->

```math
\mathrm{gelu}(x) \approx x \cdot \operatorname{expit}( 1.702 \, x )
```

<!-- </equation> -->

where $\operatorname{expit}(x)$ is the [standard logistic function][logistic-function].

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var geluQuick = require( '@stdlib/math/base/special/fast/gelu-quick' );
```

#### geluQuick( x )

Evaluates the Gaussian Error Linear Unit (GELU) activation function using a quick 1.702 approximation.

```javascript
var v = geluQuick( 0.0 );
// returns 0.0

v = geluQuick( 1.0 );
// returns ~0.8458

v = geluQuick( -Infinity );
// returns -0.0

v = geluQuick( Infinity );
// returns Infinity

v = geluQuick( NaN );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );
var geluQuick = require( '@stdlib/math/base/special/fast/gelu-quick' );

var opts = {
'dtype': 'float64'
};
var x = uniform( 100, -5.0, 5.0, opts );

logEachMap( 'geluQuick(%0.4f) = %0.4f', x, geluQuick );
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/math/base/special/fast/gelu_quick.h"
```

#### stdlib_base_fast_gelu_quick( x )

Evaluates the Gaussian Error Linear Unit (GELU) activation function using a quick 1.702 approximation.

```c
double y = stdlib_base_fast_gelu_quick( 1.0 );
// returns ~0.8458
```

The function accepts the following arguments:

- **x**: `[in] double` input value.

```c
double stdlib_base_fast_gelu_quick( const double x );
```

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

### Examples

```c
#include "stdlib/math/base/special/fast/gelu_quick.h"
#include <stdio.h>

int main( void ) {
const double x[] = { -5.0, -3.89, -2.78, -1.67, -0.56, 0.56, 1.67, 2.78, 3.89, 5.0 };

double v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_fast_gelu_quick( x[ i ] );
printf( "gelu_quick(%lf) = %lf\n", x[ i ], v );
}
}
```

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/math/base/special/expit`][@stdlib/math/base/special/expit]</span><span class="delimiter">: </span><span class="description">evaluate the standard logistic function.</span>
- <span class="package-name">[`@stdlib/math/base/special/fast/gelu-tanh`][@stdlib/math/base/special/fast/gelu-tanh]</span><span class="delimiter">: </span><span class="description">evaluate the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation.</span>

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[activation-function]: https://en.wikipedia.org/wiki/Activation_function

[logistic-function]: https://en.wikipedia.org/wiki/Logistic_function

<!-- <related-links> -->

[@stdlib/math/base/special/expit]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/expit

[@stdlib/math/base/special/fast/gelu-tanh]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/fast/gelu-tanh

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var geluQuick = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

x = uniform( 100, -5.0, 5.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = geluQuick( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/array/uniform' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var tryRequire = require( '@stdlib/utils/try-require' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var geluQuick = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( geluQuick instanceof Error )
};


// MAIN //

bench( format( '%s::native', pkg ), opts, function benchmark( b ) {
var x;
var y;
var i;

x = uniform( 100, -5.0, 5.0 );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = geluQuick( x[ i % x.length ] );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading