diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/README.md b/lib/node_modules/@stdlib/math/base/special/gelu/README.md new file mode 100644 index 000000000000..02e52f5cdb86 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/README.md @@ -0,0 +1,216 @@ + + +# gelu + +> Evaluate the [Gaussian error linear unit][gelu] (GELU). + +
+ +The [Gaussian error linear unit][gelu] (GELU) activation function is defined as: + + + +```math +\mathrm{gelu}(x) = x \cdot \Phi(x) = \frac{x}{2} \, \mathrm{erfc}\left( -\frac{x}{\sqrt{2}} \right) +``` + + + +where $\Phi(x)$ is the cumulative distribution function of the standard normal distribution and $\mathrm{erfc}(x)$ is the [complementary error function][@stdlib/math/base/special/erfc]. + +
+ + + +
+ +## Usage + +```javascript +var gelu = require( '@stdlib/math/base/special/gelu' ); +``` + +#### gelu( x ) + +Evaluates the Gaussian Error Linear Unit (GELU) activation function. + +```javascript +var v = gelu( 0.0 ); +// returns 0.0 + +v = gelu( 1.0 ); +// returns ~0.8413 + +v = gelu( -Infinity ); +// returns -0.0 + +v = gelu( Infinity ); +// returns Infinity + +v = gelu( NaN ); +// returns NaN +``` + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var gelu = require( '@stdlib/math/base/special/gelu' ); + +var opts = { + 'dtype': 'float64' +}; +var x = uniform( 100, -5.0, 5.0, opts ); + +logEachMap( 'gelu(%0.4f) = %0.4f', x, gelu ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/gelu.h" +``` + +#### stdlib_base_gelu( x ) + +Evaluates the Gaussian Error Linear Unit (GELU) activation function. + +```c +double y = stdlib_base_gelu( 1.0 ); +// returns ~0.8413 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. + +```c +double stdlib_base_gelu( const double x ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/gelu.h" +#include + +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_gelu( x[ i ] ); + printf( "gelu(%lf) = %lf\n", x[ i ], v ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.js new file mode 100644 index 000000000000..19e6b947c103 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.js @@ -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 gelu = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var x; + var y; + var i; + + x = uniform( 100, -10.0, 10.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gelu( 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(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.native.js new file mode 100644 index 000000000000..7d6771a24117 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/benchmark.native.js @@ -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 gelu = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( gelu instanceof Error ) +}; + + +// MAIN // + +bench( format( '%s::native', pkg ), opts, function benchmark( b ) { + var x; + var y; + var i; + + x = uniform( 100, -10.0, 10.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = gelu( 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(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/Makefile b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/Makefile new file mode 100644 index 000000000000..979768abbcec --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/benchmark.c new file mode 100644 index 000000000000..2209f1de230a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/benchmark/c/native/benchmark.c @@ -0,0 +1,136 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gelu.h" +#include +#include +#include +#include +#include + +#define NAME "gelu" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + double x[ 100 ]; + double y; + double t; + int i; + + for ( i = 0; i < 100; i++ ) { + x[ i ] = ( rand_double()*20.0 ) - 10.0; + } + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + y = stdlib_base_gelu( x[ i % 100 ] ); + if ( y != y ) { + printf( "should not return NaN\n" ); + break; + } + } + elapsed = tic() - t; + if ( y != y ) { + printf( "should not return NaN\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::native::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i + 1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/binding.gyp b/lib/node_modules/@stdlib/math/base/special/gelu/binding.gyp new file mode 100644 index 000000000000..0d6508a12e99 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/binding.gyp @@ -0,0 +1,170 @@ +# @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. + +# A `.gyp` file for building a Node.js native add-on. +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # List of files to include in this file: + 'includes': [ + './include.gypi', + ], + + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Target name should match the add-on export name: + 'addon_target_name%': 'addon', + + # Set variables based on the host OS: + 'conditions': [ + [ + 'OS=="win"', + { + # Define the object file suffix: + 'obj': 'obj', + }, + { + # Define the object file suffix: + 'obj': 'o', + } + ], # end condition (OS=="win") + ], # end conditions + }, # end variables + + # Define compile targets: + 'targets': [ + + # Target to generate an add-on: + { + # The target name should match the add-on export name: + 'target_name': '<(addon_target_name)', + + # Define dependencies: + 'dependencies': [], + + # Define directories which contain relevant include headers: + 'include_dirs': [ + # Local include directory: + '<@(include_dirs)', + ], + + # List of source files: + 'sources': [ + '<@(src_files)', + ], + + # Settings which should be applied when a target's object files are used as linker input: + 'link_settings': { + # Define libraries: + 'libraries': [ + '<@(libraries)', + ], + + # Define library directories: + 'library_dirs': [ + '<@(library_dirs)', + ], + }, + + # C/C++ compiler flags: + 'cflags': [ + # Enable commonly used warning options: + '-Wall', + + # Aggressive optimization: + '-O3', + ], + + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', + ], + + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], + + # Apply conditions based on the host OS: + 'conditions': [ + [ + 'OS=="mac"', + { + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', + ], + }, + ], # end condition (OS=="mac") + [ + 'OS!="win"', + { + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], + }, + ], # end condition (OS!="win") + ], # end conditions + }, # end target <(addon_target_name) + + # Target to copy a generated add-on to a standard location: + { + 'target_name': 'copy_addon', + + # Declare that the output of this target is not linked: + 'type': 'none', + + # Define dependencies: + 'dependencies': [ + # Require that the add-on be generated before building this target: + '<(addon_target_name)', + ], + + # Define a list of actions: + 'actions': [ + { + 'action_name': 'copy_addon', + 'message': 'Copying addon...', + + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], + + # Declare the expected outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Define the command-line invocation: + 'action': [ + 'cp', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target copy_addon + ], # end targets +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/gelu/docs/repl.txt new file mode 100644 index 000000000000..17b432810396 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x ) + Evaluates the Gaussian Error Linear Unit (GELU) activation function. + + Parameters + ---------- + x: number + Input value. + + Returns + ------- + y: number + Function value. + + Examples + -------- + > var y = {{alias}}( 0.0 ) + 0.0 + > y = {{alias}}( 1.0 ) + ~0.8413 + > y = {{alias}}( -Infinity ) + -0.0 + > y = {{alias}}( Infinity ) + Infinity + > y = {{alias}}( NaN ) + NaN + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/index.d.ts new file mode 100644 index 000000000000..1bbd143a5eaa --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/index.d.ts @@ -0,0 +1,48 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Evaluates the Gaussian Error Linear Unit (GELU) activation function. +* +* @param x - input value +* @returns function value +* +* @example +* var y = gelu( 0.0 ); +* // returns 0.0 +* +* @example +* var y = gelu( 1.0 ); +* // returns ~0.8413 +* +* @example +* var y = gelu( -Infinity ); +* // returns -0.0 +* +* @example +* var y = gelu( NaN ); +* // returns NaN +*/ +declare function gelu( x: number ): number; + + +// EXPORTS // + +export = gelu; diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/test.ts new file mode 100644 index 000000000000..d6b3ad284b88 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/docs/types/test.ts @@ -0,0 +1,44 @@ +/* +* @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. +*/ + +import gelu = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + gelu( 8 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a value other than a number... +{ + gelu( true ); // $ExpectError + gelu( false ); // $ExpectError + gelu( null ); // $ExpectError + gelu( undefined ); // $ExpectError + gelu( '5' ); // $ExpectError + gelu( [] ); // $ExpectError + gelu( {} ); // $ExpectError + gelu( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + gelu(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/Makefile new file mode 100644 index 000000000000..49b55b63becf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs a compiled example. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/example.c new file mode 100644 index 000000000000..08e9d31eab12 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/examples/c/example.c @@ -0,0 +1,31 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gelu.h" +#include + +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_gelu( x[ i ] ); + printf( "gelu(%lf) = %lf\n", x[ i ], v ); + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/examples/index.js b/lib/node_modules/@stdlib/math/base/special/gelu/examples/index.js new file mode 100644 index 000000000000..9fcc401d6c95 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/examples/index.js @@ -0,0 +1,30 @@ +/** +* @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'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var gelu = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; +var x = uniform( 100, -5.0, 5.0, opts ); + +logEachMap( 'gelu(%0.4f) = %0.4f', x, gelu ); diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/include.gypi b/lib/node_modules/@stdlib/math/base/special/gelu/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/include.gypi @@ -0,0 +1,53 @@ +# @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. + +# A GYP include file for building a Node.js native add-on. +# +# Main documentation: +# +# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md +# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md +{ + # Define variables to be used throughout the configuration for all targets: + 'variables': { + # Source directory: + 'src_dir': './src', + + # Include directories: + 'include_dirs': [ + '=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "gelu", + "activation", + "gaussian", + "neural", + "deep learning", + "gaussian error linear unit" + ], + "__stdlib__": { + "scaffold": { + "$schema": "math/base@v1.0", + "base_alias": "gelu", + "alias": "gelu", + "pkg_desc": "evaluate the Gaussian Error Linear Unit (GELU) for a double-precision floating-point number", + "desc": "evaluates the Gaussian Error Linear Unit (GELU) for a double-precision floating-point number", + "short_desc": "Gaussian error linear unit", + "parameters": [ + { + "name": "x", + "desc": "input value", + "type": { + "javascript": "number", + "jsdoc": "number", + "c": "double", + "dtype": "float64" + }, + "domain": [ + { + "min": "-infinity", + "max": "infinity" + } + ], + "rand": { + "prng": "random/base/uniform", + "parameters": [ + -5, + 5 + ] + }, + "example_values": [ + -4.62, + 3.17, + -2.41, + 1.28, + -0.73, + 2.56, + -3.09, + 0.41, + 4.12, + -1.85, + 2.93, + -4.37, + 1.67, + -0.28, + 3.74, + -2.96, + 0.83, + -1.12, + 4.89, + -3.51 + ] + } + ], + "output_policy": "real_floating_point_and_generic", + "returns": { + "desc": "function value", + "type": { + "javascript": "number", + "jsdoc": "number", + "c": "double", + "dtype": "float64" + } + }, + "keywords": [ + "gelu", + "activation", + "gaussian", + "neural", + "deep learning", + "gaussian error linear unit" + ], + "extra_keywords": [] + } + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/src/Makefile b/lib/node_modules/@stdlib/math/base/special/gelu/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/src/Makefile @@ -0,0 +1,70 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + + +# RULES # + +#/ +# Removes generated files for building an add-on. +# +# @example +# make clean-addon +#/ +clean-addon: + $(QUIET) -rm -f *.o *.node + +.PHONY: clean-addon + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: clean-addon + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/src/addon.c b/lib/node_modules/@stdlib/math/base/special/gelu/src/addon.c new file mode 100644 index 000000000000..89d017cf81d6 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/src/addon.c @@ -0,0 +1,22 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gelu.h" +#include "stdlib/math/base/napi/unary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_gelu ) diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/src/main.c b/lib/node_modules/@stdlib/math/base/special/gelu/src/main.c new file mode 100644 index 000000000000..3758992a4704 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/src/main.c @@ -0,0 +1,44 @@ +/** +* @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. +*/ + +#include "stdlib/math/base/special/gelu.h" +#include "stdlib/math/base/special/erfc.h" +#include "stdlib/constants/float64/sqrt_two.h" +#include "stdlib/constants/float64/ninf.h" +#include "stdlib/constants/float64/nan.h" +#include "stdlib/math/base/assert/is_nan.h" + +/** +* Evaluates the Gaussian Error Linear Unit (GELU) activation function. +* +* @param x input value +* @return function value +* +* @example +* double y = stdlib_base_gelu( 0.0 ); +* // returns 0.0 +*/ +double stdlib_base_gelu( const double x ) { + if ( stdlib_base_is_nan( x ) ) { + return STDLIB_CONSTANT_FLOAT64_NAN; + } + if ( x == STDLIB_CONSTANT_FLOAT64_NINF ) { + return -0.0; + } + return 0.5 * x * stdlib_base_erfc( -x / STDLIB_CONSTANT_FLOAT64_SQRT2 ); +} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_negative.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_negative.json new file mode 100644 index 000000000000..8d671708082c --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_negative.json @@ -0,0 +1 @@ +{"x": [-35.0, -34.75, -34.5, -34.25, -34.0, -33.75, -33.5, -33.25, -33.0, -32.75, -32.5, -32.25, -32.0, -31.75, -31.5, -31.25, -31.0, -30.75, -30.5, -30.25, -30.0, -29.75, -29.5, -29.25, -29.0, -28.75, -28.5, -28.25, -28.0, -27.75, -27.5, -27.25, -27.0, -26.75, -26.5, -26.25, -26.0, -25.75, -25.5, -25.25, -25.0, -24.75, -24.5, -24.25, -24.0, -23.75, -23.5, -23.25, -23.0, -22.75, -22.5, -22.25, -22.0, -21.75, -21.5, -21.25, -21.0, -20.75, -20.5, -20.25, -20.0, -19.75, -19.5, -19.25, -19.0, -18.75, -18.5, -18.25, -18.0, -17.75, -17.5, -17.25, -17.0, -16.75, -16.5, -16.25, -16.0, -15.75, -15.5, -15.25, -15.0, -14.75, -14.5, -14.25, -14.0, -13.75, -13.5, -13.25, -13.0, -12.75, -12.5, -12.25, -12.0, -11.75, -11.5, -11.25, -11.0, -10.75, -10.5, -10.25, -10.0, -9.75, -9.5, -9.25, -9.0, -8.75, -8.5, -8.25, -8.0, -7.75, -7.5, -7.25, -7.0, -6.75, -6.5, -6.25, -6.0, -5.75, -5.5, -5.25, -5.0], "expected": [-3.9371874726539365e-267, -2.4081637249248578e-263, -1.3837014934692866e-259, -7.4688766995176e-256, -3.7872558709531163e-252, -1.804057929660816e-248, -8.072959628653499e-245, -3.393685156365917e-241, -1.3401912549022227e-237, -4.9718551697364803e-234, -1.7327129168958268e-230, -5.672718158997068e-227, -1.7446657931241255e-223, -5.040686946560346e-220, -1.368118269325088e-216, -3.488301295199117e-213, -8.355275251721774e-210, -1.88002642704582e-206, -3.9739606351931184e-203, -7.891136864367767e-200, -1.472014178144629e-196, -2.5795313232301326e-193, -4.2464499290764463e-190, -6.567008692225127e-187, -9.540377273443239e-184, -1.3020266517405716e-180, -1.6692852573348517e-177, -2.0104699772291996e-174, -2.274683451504637e-171, -2.417690348568182e-168, -2.4139969031417394e-165, -2.264274385322235e-162, -1.995159871859118e-159, -1.6515154502715423e-156, -1.2842331049804492e-153, -9.38126382156036e-151, -6.437764620308988e-148, -4.1501623001067495e-145, -2.513340312335806e-142, -1.4298606936518029e-139, -7.641741765957187e-137, -3.836605729148458e-134, -1.809498181065043e-131, -8.017247294357575e-129, -3.3369410845194576e-126, -1.3047518135936841e-123, -4.792513773637573e-121, -1.6536930413484338e-118, -5.360465114307511e-116, -1.6323228903047848e-113, -4.669449297899475e-111, -1.2548194070123027e-108, -3.1677633573192226e-106, -7.51242494477772e-104, -1.6736453715943467e-101, -3.50269708992216e-99, -6.886483839856154e-97, -1.271886203891675e-94, -2.2067580180521575e-92, -3.596804726523045e-90, -5.507248237212664e-88, -7.921511232149765e-86, -1.0703775177537962e-83, -1.3586932163773277e-81, -1.6201725509999565e-79, -1.814915902337725e-77, -1.909884257569255e-75, -1.888047121156572e-73, -1.753370805408698e-71, -1.5296419823821436e-69, -1.2536052840911723e-67, -9.651320481211155e-66, -6.980193543568244e-64, -4.742451421586316e-62, -3.0268639552218015e-60, -1.8148359519024397e-58, -1.0222007040861174e-56, -5.408653555103701e-55, -2.6884142272805166e-53, -1.2553275843060397e-51, -5.506449298969288e-50, -2.269027231097646e-48, -8.783367408402192e-47, -3.194003881588459e-45, -1.0910951546869918e-43, -3.5014049344642884e-42, -1.0555389862638368e-40, -2.9892216500837114e-39, -7.952313719414898e-38, -1.9873760183235298e-36, -4.665705373597261e-35, -1.0289750177951134e-33, -2.1317785344932424e-32, -4.14885731516607e-31, -7.585137163030734e-30, -1.302705358389734e-28, -2.101725531948551e-27, -3.1853119439514743e-26, -4.534956633699722e-25, -6.065106330049765e-24, -7.619853024160595e-23, -8.99282818681597e-22, -9.969789321594586e-21, -1.0382786071978448e-19, -1.015729565358458e-18, -9.334182703540577e-18, -8.057604598872852e-17, -6.533774209580041e-16, -4.9767684594174555e-15, -3.560836262728417e-14, -2.3931687546831897e-13, -1.5107971650372561e-12, -8.958687807200846e-12, -4.989774000162055e-11, -2.610400379508431e-10, -1.2826646407618451e-09, -5.91952587022621e-09, -2.5657491609934278e-08, -1.0444259356238256e-07, -3.992604271156581e-07, -1.4332578593959727e-06]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_positive.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_positive.json new file mode 100644 index 000000000000..b4b5d3c152e1 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_large_positive.json @@ -0,0 +1 @@ +{"x": [5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0, 26.5, 27.0, 27.5, 28.0, 28.5, 29.0, 29.5, 30.0, 30.5, 31.0, 31.5, 32.0, 32.5, 33.0, 33.5, 34.0, 34.5, 35.0, 35.5, 36.0, 36.5, 37.0, 37.5, 38.0, 38.5, 39.0, 39.5, 40.0, 40.5, 41.0, 41.5, 42.0, 42.5, 43.0, 43.5, 44.0, 44.5, 45.0, 45.5, 46.0, 46.5, 47.0, 47.5, 48.0, 48.5, 49.0, 49.5, 50.0], "expected": [4.999998566742141, 5.499999895557407, 5.999999994080474, 6.49999999973896, 6.999999999991041, 7.49999999999976, 7.999999999999995, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0, 20.5, 21.0, 21.5, 22.0, 22.5, 23.0, 23.5, 24.0, 24.5, 25.0, 25.5, 26.0, 26.5, 27.0, 27.5, 28.0, 28.5, 29.0, 29.5, 30.0, 30.5, 31.0, 31.5, 32.0, 32.5, 33.0, 33.5, 34.0, 34.5, 35.0, 35.5, 36.0, 36.5, 37.0, 37.5, 38.0, 38.5, 39.0, 39.5, 40.0, 40.5, 41.0, 41.5, 42.0, 42.5, 43.0, 43.5, 44.0, 44.5, 45.0, 45.5, 46.0, 46.5, 47.0, 47.5, 48.0, 48.5, 49.0, 49.5, 50.0]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_medium.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_medium.json new file mode 100644 index 000000000000..1df3fa58e6ce --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_medium.json @@ -0,0 +1 @@ +{"x": [-5.0, -4.95, -4.9, -4.85, -4.8, -4.75, -4.7, -4.65, -4.6, -4.55, -4.5, -4.45, -4.4, -4.35, -4.3, -4.25, -4.2, -4.15, -4.1, -4.05, -4.0, -3.95, -3.9, -3.85, -3.8, -3.75, -3.7, -3.65, -3.6, -3.55, -3.5, -3.45, -3.4, -3.35, -3.3, -3.25, -3.2, -3.15, -3.1, -3.05, -3.0, -2.95, -2.9, -2.85, -2.8, -2.75, -2.7, -2.65, -2.6, -2.55, -2.5, -2.45, -2.4, -2.35, -2.3, -2.25, -2.2, -2.15, -2.1, -2.05, -2.0, -1.95, -1.9, -1.85, -1.8, -1.75, -1.7, -1.65, -1.6, -1.55, -1.5, -1.45, -1.4, -1.35, -1.3, -1.25, -1.2, -1.15, -1.1, -1.05, -1.0, -0.95, -0.9, -0.85, -0.8, -0.75, -0.7, -0.65, -0.6, -0.55, -0.5, -0.45, -0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2.0, 2.05, 2.1, 2.15, 2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 2.8, 2.85, 2.9, 2.95, 3.0, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 4.0, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.95, 5.0], "expected": [-1.4332578593959727e-06, -1.8367836694185015e-06, -2.34799805529257e-06, -2.9939407542446134e-06, -3.8079751294828665e-06, -4.8311454022013545e-06, -6.1137950334112225e-06, -7.7174894213273e-06, -9.717291631513152e-06, -1.2204445797356795e-05, -1.528952906128528e-05, -1.9106139391374822e-05, -2.381519319389697e-05, -2.9609913207103186e-05, -3.67215935252648e-05, -4.542623454347134e-05, -5.6052145866806655e-05, -6.898861947805686e-05, -8.469577834144184e-05, -0.00010371570671986822, -0.00012668496733247986, -0.00015434860656126086, -0.00018757574166865072, -0.00022737681281285186, -0.00027492256691545656, -0.00033156481950301516, -0.0003988590138663365, -0.0004785885636347692, -0.0005727909245671224, -0.0006837852935064998, -0.0008142017766243376, -0.000967011805015812, -0.001145559503301397, -0.0013535936362444726, -0.001595299669866466, -0.0018753313877699941, -0.0021988414013307156, -0.0025715097854099762, -0.002999569960976905, -0.0034898308346192363, -0.004049694094890288, -0.004687165459726365, -0.00541085857111371, -0.0062299901465027365, -0.007154364925198223, -0.008194348896400033, -0.009360829268209806, -0.01066515963830952, -0.012119088861668756, -0.013734672182870068, -0.015524163314440348, -0.01749988630141499, -0.019674086219030728, -0.02205875800687065, -0.024665453049854374, -0.02750506347385058, -0.030587584529696944, -0.03392185589084461, -0.03751528318191478, -0.041373541581694054, -0.04550026389635843, -0.049896716067195326, -0.05456146365040345, -0.05949003337188535, -0.06467457440326646, -0.07010352451167991, -0.0757612866895232, -0.08162792225551938, -0.0876788667192928, -0.09388467490319144, -0.10021080190328713, -0.10661742643399012, -0.11305932292727952, -0.11948578844049276, -0.12584062996129344, -0.13206221708356908, -0.13808360426604996, -0.14383272598272287, -0.14923266704102095, -0.15420200919469074, -0.15865525393145707, -0.16250331999305775, -0.1656541128120836, -0.16801316165428856, -0.1694843188667174, -0.16997051428265117, -0.1693745565561511, -0.16759997202381208, -0.16455187065004415, -0.1601378277335905, -0.15426876936299347, -0.14685984912956404, -0.13783130335587032, -0.12710927208853331, -0.1146265733433142, -0.10032341857926907, -0.0841480581121794, -0.06605734614446362, -0.0460172162722971, -0.02400305970808138, 0.0, 0.025996940291918624, 0.053982783727702904, 0.08394265385553637, 0.11585194188782061, 0.14967658142073093, 0.18537342665668577, 0.22289072791146664, 0.2621686966441297, 0.303140150870436, 0.3457312306370065, 0.3898621722664095, 0.43544812934995586, 0.4824000279761879, 0.5306254434438489, 0.5800294857173488, 0.6305156811332826, 0.6819868383457115, 0.7343458871879165, 0.7874966800069421, 0.8413447460685428, 0.8957979908053092, 0.9507673329589791, 1.0061672740172771, 1.06191639573395, 1.1179377829164308, 1.1741593700387067, 1.2305142115595074, 1.2869406770727203, 1.3433825735660099, 1.399789198096713, 1.4561153250968086, 1.5123211332807074, 1.5683720777444805, 1.6242387133104768, 1.67989647548832, 1.7353254255967336, 1.7905099666281146, 1.8454385363495964, 1.9001032839328045, 1.9544997361036418, 2.0086264584183056, 2.0624847168180853, 2.116078144109155, 2.1694124154703034, 2.2224949365261493, 2.2753345469501456, 2.3279412419931296, 2.380325913780969, 2.4325001136985853, 2.4844758366855597, 2.5362653278171297, 2.587880911138331, 2.6393348403616903, 2.6906391707317905, 2.7418056511035998, 2.792845635074802, 2.8437700098534973, 2.8945891414288862, 2.9453128345402737, 2.99595030590511, 3.046510169165381, 3.097000430039023, 3.14742849021459, 3.1978011585986694, 3.24812466861223, 3.2984047003301336, 3.3486464063637555, 3.3988544404966987, 3.4490329881949844, 3.4991857982233756, 3.549316214706493, 3.599427209075433, 3.649521411436365, 3.699601140986134, 3.7496684351804968, 3.7997250774330844, 3.849772623187187, 3.8998124242583314, 3.949845651393439, 3.9998733150326675, 4.04989628429328, 4.099915304221658, 4.149931011380523, 4.199943947854133, 4.249954573765456, 4.299963278406475, 4.349970390086793, 4.399976184806806, 4.449980893860609, 4.4999847104709385, 4.549987795554202, 4.599990282708368, 4.649992282510579, 4.699993886204966, 4.749995168854598, 4.799996192024871, 4.849997006059246, 4.899997652001945, 4.949998163216331, 4.999998566742141]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_near_zero.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_near_zero.json new file mode 100644 index 000000000000..23129b2e334d --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_near_zero.json @@ -0,0 +1 @@ +{"x": [-1.0, -0.98, -0.96, -0.94, -0.92, -0.9, -0.88, -0.86, -0.84, -0.82, -0.8, -0.78, -0.76, -0.74, -0.72, -0.7, -0.68, -0.66, -0.64, -0.62, -0.6, -0.58, -0.56, -0.54, -0.52, -0.5, -0.48, -0.46, -0.44, -0.42, -0.4, -0.38, -0.36, -0.34, -0.32, -0.3, -0.28, -0.26, -0.24, -0.22, -0.2, -0.18, -0.16, -0.14, -0.12, -0.1, -0.08, -0.06, -0.04, -0.02, 0.0, 0.02, 0.04, 0.06, 0.08, 0.1, 0.12, 0.14, 0.16, 0.18, 0.2, 0.22, 0.24, 0.26, 0.28, 0.3, 0.32, 0.34, 0.36, 0.38, 0.4, 0.42, 0.44, 0.46, 0.48, 0.5, 0.52, 0.54, 0.56, 0.58, 0.6, 0.62, 0.64, 0.66, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.82, 0.84, 0.86, 0.88, 0.9, 0.92, 0.94, 0.96, 0.98, 1.0], "expected": [-0.15865525393145707, -0.16027219814113852, -0.16178650316816431, -0.16319225351830713, -0.16448346924522197, -0.1656541128120836, -0.16669809620350667, -0.16760928827655522, -0.16838152233877776, -0.16900860394036674, -0.1694843188667174, -0.16980244131687186, -0.1699567422525756, -0.16994099790194508, -0.16974899840106084, -0.1693745565561511, -0.168811516708428, -0.16805376368308186, -0.16709523180343142, -0.16592991395076953, -0.16455187065004415, -0.1629552391611673, -0.16113424255545516, -0.15908319875647695, -0.1567965295244224, -0.15426876936299347, -0.15149457432778685, -0.14846873071515992, -0.1451861636106612, -0.14164194527626478, -0.13783130335587032, -0.13374962887881814, -0.12939248404152318, -0.12475560974775046, -0.11983493288853758, -0.1146265733433142, -0.10912685068437677, -0.10333229056754227, -0.09723963079252901, -0.09084582701739279, -0.0841480581121794, -0.07714373113783786, -0.06983048593737075, -0.062206199327173105, -0.054268988877529935, -0.0460172162722971, -0.03744949023888101, -0.02856466904075359, -0.019361862525886772, -0.00984043372566196, 0.0, 0.010159566274338038, 0.020638137474113232, 0.03143533095924641, 0.04255050976111899, 0.053982783727702904, 0.06573101112247007, 0.07779380067282692, 0.09016951406262927, 0.10285626886216212, 0.11585194188782061, 0.1291541729826072, 0.14276036920747098, 0.15666770943245775, 0.17087314931562325, 0.18537342665668577, 0.20016506711146242, 0.21524439025224956, 0.2306075159584768, 0.24625037112118187, 0.2621686966441297, 0.2783580547237352, 0.2948138363893388, 0.3115312692848401, 0.32850542567221314, 0.3457312306370065, 0.3632034704755776, 0.38091680124352306, 0.3988657574445449, 0.41704476083883263, 0.43544812934995586, 0.4540700860492305, 0.4729047681965686, 0.4919462363169182, 0.5111884832915721, 0.5306254434438489, 0.5502510015989391, 0.5700590020980548, 0.5900432577474244, 0.6101975586831282, 0.6305156811332826, 0.6509913960596332, 0.6716184776612222, 0.6923907117234449, 0.7133019037964933, 0.7343458871879165, 0.7555165307547781, 0.7768077464816928, 0.7982134968318356, 0.8197278018588615, 0.8413447460685428]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_special.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_special.json new file mode 100644 index 000000000000..1567a65a4509 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_special.json @@ -0,0 +1 @@ +{"x": [-40.0, -39.0, -38.6, -2.0, -1.0, 0.0, 1.0, 2.0, 10.0, 50.0], "expected": [-0.0, -0.0, -0.0, -0.04550026389635843, -0.15865525393145707, 0.0, 0.8413447460685428, 1.9544997361036418, 10.0, 50.0]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_subnormal.json b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_subnormal.json new file mode 100644 index 000000000000..bcb14345ed0a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/gelu_subnormal.json @@ -0,0 +1 @@ +{"x": [-37.6, -37.6125, -37.625, -37.6375, -37.65, -37.6625, -37.675, -37.6875, -37.7, -37.7125, -37.725, -37.7375, -37.75, -37.7625, -37.775, -37.7875, -37.8, -37.8125, -37.825, -37.8375, -37.85, -37.8625, -37.875, -37.8875, -37.9, -37.9125, -37.925, -37.9375, -37.95, -37.9625, -37.975, -37.9875, -38.0, -38.0125, -38.025, -38.0375, -38.05, -38.0625, -38.075, -38.0875, -38.1, -38.1125, -38.125, -38.1375, -38.15, -38.1625, -38.175, -38.1875, -38.2, -38.2125, -38.225, -38.2375, -38.25, -38.2625, -38.275, -38.2875, -38.3, -38.3125, -38.325, -38.3375, -38.35, -38.3625, -38.375, -38.3875, -38.4, -38.4125, -38.425, -38.4375, -38.45, -38.4625, -38.475, -38.4875, -38.5, -38.5125, -38.525, -38.5375, -38.55, -38.5625, -38.575, -38.5875, -38.6], "expected": [-4.041290298447324e-308, -2.5256194647181083e-308, -1.5781487109797204e-308, -9.85961768601562e-309, -6.158917217062215e-309, -3.846633411557305e-309, -2.402090544214155e-309, -1.4997887410949e-309, -9.3627396197476e-310, -5.843969540554e-310, -3.6470782625578e-310, -2.2756966147457e-310, -1.4197627754868e-310, -8.8562394127485e-311, -5.5235088325196e-311, -3.4443945906353e-311, -2.147548029322e-311, -1.3387670465565e-311, -8.3444801006e-312, -5.20026761966e-312, -3.240292729287e-312, -2.018714673135e-312, -1.257470410394e-312, -7.83164062385e-313, -4.8768552752e-313, -3.0364010038e-313, -1.89021204457e-313, -1.176505786e-313, -7.3216632413e-314, -4.555725766e-314, -2.834245977e-314, -1.762989172e-314, -1.0964627734e-314, -6.818208565e-315, -4.239150296e-315, -2.63523582e-315, -1.637918554e-315, -1.017881563e-315, -6.3246188e-316, -3.92919465e-316, -2.4406468e-316, -1.51578826e-316, -9.412485e-317, -5.843885e-317, -3.627706e-317, -2.25161e-317, -1.397295e-317, -8.66992e-318, -5.37861e-318, -3.336287e-318, -2.06911e-318, -1.28304e-318, -7.9551e-319, -4.93117e-319, -3.0559e-319, -1.89356e-319, -1.1732e-319, -7.2687e-320, -4.5064e-320, -2.7846e-320, -1.7243e-320, -1.0706e-320, -6.635e-321, -4.076e-321, -2.56e-321, -1.616e-321, -9.5e-322, -5.7e-322, -3.8e-322, -1.9e-322, -9.4e-323, -9.4e-323, -9.4e-323, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0]} diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/runner.py b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/runner.py new file mode 100644 index 000000000000..d0491bc2b90a --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/fixtures/python/runner.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# +# @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. + +"""Generate fixtures.""" + +import math +import os +import json +import numpy as np + +# Get the file path: +FILE = os.path.realpath(__file__) + +# Extract the directory in which this file resides: +DIR = os.path.dirname(FILE) + + +def gen(x, name): + """Generate fixture data and writes them to file. + + # Arguments + + * `x`: domain + * `name::str`: output filename + """ + x_arr = np.array(x, dtype=np.float64) + sqrt2 = math.sqrt(2.0) + y_arr = np.array([0.5 * float(v) * math.erfc(-float(v) / sqrt2) for v in x_arr]) + data = { + "x": x_arr.tolist(), + "expected": y_arr.tolist() + } + filepath = os.path.join(DIR, name) + with open(filepath, "w", encoding="utf-8") as outfile: + json.dump(data, outfile) + + +def main(): + """Generate fixture data.""" + x = [round(float(v), 4) for v in np.linspace(-1.0, 1.0, 101)] + gen(x, "gelu_near_zero.json") + + x = [round(float(v), 4) for v in np.linspace(-5.0, 5.0, 201)] + gen(x, "gelu_medium.json") + + x = [round(float(v), 2) for v in np.linspace(5.0, 50.0, 91)] + gen(x, "gelu_large_positive.json") + + x = [round(float(v), 2) for v in np.linspace(-35.0, -5.0, 121)] + gen(x, "gelu_large_negative.json") + + x = [round(float(v), 4) for v in np.linspace(-37.60, -38.60, 81)] + gen(x, "gelu_subnormal.json") + + x = [-40.0, -39.0, -38.6, -2.0, -1.0, 0.0, 1.0, 2.0, 10.0, 50.0] + gen(x, "gelu_special.json") + + +if __name__ == "__main__": + main() diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/test.js b/lib/node_modules/@stdlib/math/base/special/gelu/test/test.js new file mode 100644 index 000000000000..84fc1e410f87 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/test.js @@ -0,0 +1,187 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var gelu = require( './../lib' ); + + +// FIXTURES // + +var largeNegative = require( './fixtures/python/gelu_large_negative.json' ); +var largePositive = require( './fixtures/python/gelu_large_positive.json' ); +var subnormal = require( './fixtures/python/gelu_subnormal.json' ); +var nearZero = require( './fixtures/python/gelu_near_zero.json' ); +var special = require( './fixtures/python/gelu_special.json' ); +var medium = require( './fixtures/python/gelu_medium.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gelu, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v = gelu( NaN ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function returns `+Infinity` when provided `+Infinity`', function test( t ) { + var v = gelu( PINF ); + t.strictEqual( v, PINF, 'returns +Infinity' ); + t.end(); +}); + +tape( 'the function returns `-0.0` when provided `-Infinity`', function test( t ) { + var v = gelu( NINF ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function preserves the sign of zero', function test( t ) { + var v = gelu( 0.0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0.0' ); + + v = gelu( -0.0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function does not return `NaN` for large inputs', function test( t ) { + var v; + + v = gelu( 1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e200, 'returns x for very large positive x' ); + + v = gelu( -1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + + v = gelu( 1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e308, 'returns x for very large positive x' ); + + v = gelu( -1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function evaluates exact GELU near zero', function test( t ) { + var expected; + var x; + var y; + var i; + + x = nearZero.x; + expected = nearZero.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for moderate inputs', function test( t ) { + var expected; + var x; + var y; + var i; + + x = medium.x; + expected = medium.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 24 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for large positive inputs', function test( t ) { + var expected; + var x; + var y; + var i; + + x = largePositive.x; + expected = largePositive.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for large negative tail inputs', function test( t ) { + var expected; + var x; + var y; + var i; + + x = largeNegative.x; + expected = largeNegative.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 10 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for subnormal inputs', function test( t ) { + var expected; + var x; + var y; + var i; + + x = subnormal.x; + expected = subnormal.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 25 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for special values', function test( t ) { + var expected; + var x; + var y; + var i; + + x = special.x; + expected = special.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/gelu/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/gelu/test/test.native.js new file mode 100644 index 000000000000..7aaa56a2df19 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/gelu/test/test.native.js @@ -0,0 +1,196 @@ +/** +* @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 tape = require( 'tape' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isPositiveZero = require( '@stdlib/assert/is-positive-zero' ); +var isNegativeZero = require( '@stdlib/assert/is-negative-zero' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var PINF = require( '@stdlib/constants/float64/pinf' ); +var NINF = require( '@stdlib/constants/float64/ninf' ); +var tryRequire = require( '@stdlib/utils/try-require' ); + + +// VARIABLES // + +var gelu = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( gelu instanceof Error ) +}; + + +// FIXTURES // + +var largeNegative = require( './fixtures/python/gelu_large_negative.json' ); +var largePositive = require( './fixtures/python/gelu_large_positive.json' ); +var subnormal = require( './fixtures/python/gelu_subnormal.json' ); +var nearZero = require( './fixtures/python/gelu_near_zero.json' ); +var special = require( './fixtures/python/gelu_special.json' ); +var medium = require( './fixtures/python/gelu_medium.json' ); + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof gelu, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v = gelu( NaN ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function returns `+Infinity` when provided `+Infinity`', opts, function test( t ) { + var v = gelu( PINF ); + t.strictEqual( v, PINF, 'returns +Infinity' ); + t.end(); +}); + +tape( 'the function returns `-0.0` when provided `-Infinity`', opts, function test( t ) { + var v = gelu( NINF ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function preserves the sign of zero', opts, function test( t ) { + var v = gelu( 0.0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0.0' ); + + v = gelu( -0.0 ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function does not return `NaN` for large inputs', opts, function test( t ) { + var v; + + v = gelu( 1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e200, 'returns x for very large positive x' ); + + v = gelu( -1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + + v = gelu( 1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e308, 'returns x for very large positive x' ); + + v = gelu( -1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function evaluates exact GELU near zero', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = nearZero.x; + expected = nearZero.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for moderate inputs', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = medium.x; + expected = medium.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 24 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for large positive inputs', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = largePositive.x; + expected = largePositive.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for large negative tail inputs', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = largeNegative.x; + expected = largeNegative.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 10 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for subnormal inputs', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = subnormal.x; + expected = subnormal.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 25 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates exact GELU for special values', opts, function test( t ) { + var expected; + var x; + var y; + var i; + + x = special.x; + expected = special.expected; + for ( i = 0; i < x.length; i++ ) { + y = gelu( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' ); + } + t.end(); +});