From 0f1736ebb4ff9822e682e518075be48e58cbbd7e Mon Sep 17 00:00:00 2001 From: Aryan Sharma Date: Thu, 15 Oct 2026 19:53:19 +0530 Subject: [PATCH 1/5] feat: add `math/base/special/fast/gelu-tanh` Signed-off-by: Aryan Sharma --- .../base/special/fast/gelu-tanh/README.md | 217 ++++++++++++++++++ .../fast/gelu-tanh/benchmark/benchmark.js | 52 +++++ .../gelu-tanh/benchmark/benchmark.native.js | 62 +++++ .../gelu-tanh/benchmark/c/native/Makefile | 146 ++++++++++++ .../gelu-tanh/benchmark/c/native/benchmark.c | 136 +++++++++++ .../base/special/fast/gelu-tanh/binding.gyp | 163 +++++++++++++ .../base/special/fast/gelu-tanh/docs/repl.txt | 30 +++ .../fast/gelu-tanh/docs/types/index.d.ts | 52 +++++ .../special/fast/gelu-tanh/docs/types/test.ts | 44 ++++ .../fast/gelu-tanh/examples/c/Makefile | 146 ++++++++++++ .../fast/gelu-tanh/examples/c/example.c | 31 +++ .../special/fast/gelu-tanh/examples/index.js | 30 +++ .../base/special/fast/gelu-tanh/include.gypi | 53 +++++ .../stdlib/math/base/special/fast/gelu_tanh.h | 38 +++ .../base/special/fast/gelu-tanh/lib/index.js | 52 +++++ .../base/special/fast/gelu-tanh/lib/main.js | 76 ++++++ .../base/special/fast/gelu-tanh/lib/native.js | 62 +++++ .../base/special/fast/gelu-tanh/manifest.json | 81 +++++++ .../base/special/fast/gelu-tanh/package.json | 71 ++++++ .../base/special/fast/gelu-tanh/src/Makefile | 70 ++++++ .../base/special/fast/gelu-tanh/src/addon.c | 22 ++ .../base/special/fast/gelu-tanh/src/main.c | 48 ++++ .../python/gelu_tanh_large_negative.json | 1 + .../python/gelu_tanh_large_positive.json | 1 + .../fixtures/python/gelu_tanh_medium.json | 1 + .../fixtures/python/gelu_tanh_near_zero.json | 1 + .../fixtures/python/gelu_tanh_special.json | 1 + .../gelu-tanh/test/fixtures/python/runner.py | 82 +++++++ .../base/special/fast/gelu-tanh/test/test.js | 171 ++++++++++++++ .../fast/gelu-tanh/test/test.native.js | 180 +++++++++++++++ 30 files changed, 2120 insertions(+) create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/README.md create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/benchmark.native.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/c/native/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/c/native/benchmark.c create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/c/example.c create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/index.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/include.gypi create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/include/stdlib/math/base/special/fast/gelu_tanh.h create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/lib/index.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/lib/main.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/lib/native.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/manifest.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/package.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/Makefile create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/addon.c create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/main.c create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_negative.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_positive.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_medium.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_near_zero.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_special.json create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js create mode 100644 lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/README.md b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/README.md new file mode 100644 index 000000000000..b722d844accf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/README.md @@ -0,0 +1,217 @@ + + +# geluTanh + +> Evaluate the [Gaussian error linear unit][activation-function] (GELU) using a tanh approximation. + +
+ +The [Gaussian error linear unit][activation-function] (GELU) using a tanh approximation is defined as: + + + +```math +\mathrm{gelu}(x) \approx \frac{x}{2} \left[ 1 + \tanh\left( \sqrt{\frac{2}{\pi}} \left( x + 0.044715 \, x^3 \right) \right) \right] +``` + + + +where $\tanh(x)$ is the [hyperbolic tangent][hyperbolic-functions]. + +
+ + + +
+ +## Usage + +```javascript +var geluTanh = require( '@stdlib/math/base/special/fast/gelu-tanh' ); +``` + +#### geluTanh( x ) + +Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. + +```javascript +var v = geluTanh( 0.0 ); +// returns 0.0 + +v = geluTanh( 1.0 ); +// returns ~0.8412 + +v = geluTanh( -Infinity ); +// returns -0.0 + +v = geluTanh( Infinity ); +// returns Infinity + +v = geluTanh( NaN ); +// returns NaN +``` + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var geluTanh = require( '@stdlib/math/base/special/fast/gelu-tanh' ); + +var opts = { + 'dtype': 'float64' +}; +var x = uniform( 100, -5.0, 5.0, opts ); + +logEachMap( 'geluTanh(%0.4f) = %0.4f', x, geluTanh ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/math/base/special/fast/gelu_tanh.h" +``` + +#### stdlib_base_fast_gelu_tanh( x ) + +Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. + +```c +double y = stdlib_base_fast_gelu_tanh( 1.0 ); +// returns ~0.8412 +``` + +The function accepts the following arguments: + +- **x**: `[in] double` input value. + +```c +double stdlib_base_fast_gelu_tanh( const double x ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/math/base/special/fast/gelu_tanh.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_fast_gelu_tanh( x[ i ] ); + printf( "gelu_tanh(%lf) = %lf\n", x[ i ], v ); + } +} +``` + +
+ + + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/benchmark.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/benchmark.js new file mode 100644 index 000000000000..43338d43dbd5 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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 geluTanh = 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 = geluTanh( 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/fast/gelu-tanh/benchmark/benchmark.native.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/benchmark.native.js new file mode 100644 index 000000000000..c86604dbed30 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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 geluTanh = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( geluTanh 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 = geluTanh( 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/fast/gelu-tanh/benchmark/c/native/Makefile b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/c/native/Makefile new file mode 100644 index 000000000000..1e0103324ee3 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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`) +# @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/fast/gelu-tanh/benchmark/c/native/benchmark.c b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/benchmark/c/native/benchmark.c new file mode 100644 index 000000000000..444d16101102 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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/fast/gelu_tanh.h" +#include +#include +#include +#include +#include + +#define NAME "gelu-tanh" +#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_fast_gelu_tanh( 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/fast/gelu-tanh/binding.gyp b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp new file mode 100644 index 000000000000..750772983f5e --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp @@ -0,0 +1,163 @@ +# @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': [ + '<(include_dirs)', + ], + + # Define compilation sources: + 'sources': [ + '<(src_files)', + ], + + # Define library directories: + 'link_dirs': [ + '<(library_dirs)', + ], + + # Define libraries: + 'libraries': [ + '<(libraries)', + ], + + # Define direct dependent settings: + 'direct_dependent_settings': { + # Define directories which contain relevant include headers: + 'include_dirs': [ + '<(include_dirs)', + ], + }, # end direct_dependent_settings + + # Set custom conditions: + 'conditions': [ + [ + 'OS=="win"', + { + # Apply custom flags when compiling on Windows: + 'msvs_settings': { + 'VCCLCompilerTool': { + 'ExceptionHandling': '0', # /EHsc + 'AdditionalOptions': [ + '/std:c11', + ], + }, + }, + }, # end condition (OS=="win") + { + # Apply custom flags when compiling on all other platforms: + 'cflags': [ + '-std=c99', + ], + } + ], # end condition (OS=="win") + [ + 'OS=="mac"', + { + 'xcode_settings': { + 'OTHER_CFLAGS': [ + '-std=c99', + ], + }, + }, + ], # end condition (OS=="mac") + ], # end conditions + }, # end target + + # Target to copy the add-on to the destination directory: + { + # The target name should match the add-on export name: + 'target_name': 'action_after_build', + + # The target type is a utility: + 'type': 'none', + + # Explicitly declare dependencies to ensure the dependencies are built before this target: + 'dependencies': [ + '<(addon_target_name)', + ], + + # Define actions to perform: + 'actions': [ + { + # Action name: + 'action_name': 'move_addon', + + # Action inputs: + 'inputs': [ + '<(PRODUCT_DIR)/<(addon_target_name).node', + ], + + # Action outputs: + 'outputs': [ + '<(addon_output_dir)/<(addon_target_name).node', + ], + + # Action to perform: + 'action': [ + 'node', + '-e', + 'require( "fs" ).renameSync( process.argv[ 1 ], process.argv[ 2 ] );', + '<(PRODUCT_DIR)/<(addon_target_name).node', + '<(addon_output_dir)/<(addon_target_name).node', + ], + }, + ], # end actions + }, # end target + ], # end targets +} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt new file mode 100644 index 000000000000..fe6ab58d58f9 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x ) + Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. + + Parameters + ---------- + x: number + Input value. + + Returns + ------- + y: number + Function value. + + Examples + -------- + > var y = {{alias}}( 0.0 ) + 0.0 + > y = {{alias}}( 1.0 ) + ~0.8412 + > 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/fast/gelu-tanh/docs/types/index.d.ts b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/index.d.ts new file mode 100644 index 000000000000..acb9114ebb35 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/** +* Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. +* +* @param x - input value +* @returns function value +* +* @example +* var v = geluTanh( 0.0 ); +* // returns 0.0 +* +* @example +* var v = geluTanh( 1.0 ); +* // returns ~0.8412 +* +* @example +* var v = geluTanh( -Infinity ); +* // returns -0.0 +* +* @example +* var v = geluTanh( Infinity ); +* // returns Infinity +* +* @example +* var v = geluTanh( NaN ); +* // returns NaN +*/ +declare function geluTanh( x: number ): number; + + +// EXPORTS // + +export = geluTanh; diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/test.ts b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/types/test.ts new file mode 100644 index 000000000000..90f404dcff6c --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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 geluTanh = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + geluTanh( 8 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a value other than a number... +{ + geluTanh( true ); // $ExpectError + geluTanh( false ); // $ExpectError + geluTanh( null ); // $ExpectError + geluTanh( undefined ); // $ExpectError + geluTanh( '5' ); // $ExpectError + geluTanh( [] ); // $ExpectError + geluTanh( {} ); // $ExpectError + geluTanh( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + geluTanh(); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/c/Makefile b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/c/Makefile new file mode 100644 index 000000000000..5e0511731e6e --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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`) +# @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 examples. +# +# @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/fast/gelu-tanh/examples/c/example.c b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/c/example.c new file mode 100644 index 000000000000..d1ac8603f091 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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/fast/gelu_tanh.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_fast_gelu_tanh( x[ i ] ); + printf( "gelu_tanh(%lf) = %lf\n", x[ i ], v ); + } +} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/index.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/examples/index.js new file mode 100644 index 000000000000..9639940c2da0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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 geluTanh = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; +var x = uniform( 100, -5.0, 5.0, opts ); + +logEachMap( 'geluTanh(%0.4f) = %0.4f', x, geluTanh ); diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/include.gypi b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/include.gypi new file mode 100644 index 000000000000..bee8d41a2caf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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", + "stdfastmath", + "mathematics", + "math", + "fastmath", + "fast", + "gelu", + "gelu-tanh", + "activation", + "activation function", + "neural", + "neural network", + "deep learning", + "approximation", + "tanh" + ] +} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/Makefile b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/Makefile new file mode 100644 index 000000000000..2caf905cedbe --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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/fast/gelu-tanh/src/addon.c b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/addon.c new file mode 100644 index 000000000000..b196db066fe2 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/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/fast/gelu_tanh.h" +#include "stdlib/math/base/napi/unary.h" + +STDLIB_MATH_BASE_NAPI_MODULE_D_D( stdlib_base_fast_gelu_tanh ) diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/main.c b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/main.c new file mode 100644 index 000000000000..61834e3e4ede --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/src/main.c @@ -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. +*/ + +#include "stdlib/math/base/special/fast/gelu_tanh.h" +#include "stdlib/constants/float64/ninf.h" +#include "stdlib/constants/float64/nan.h" +#include "stdlib/math/base/assert/is_nan.h" +#include "stdlib/math/base/special/expit.h" + +static const double SQRT_TWO_DIVIDED_BY_PI = 0.7978845608028654; + +/** +* Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. +* +* @param x input value +* @return function value +* +* @example +* double v = stdlib_base_fast_gelu_tanh( 0.0 ); +* // returns 0.0 +*/ +double stdlib_base_fast_gelu_tanh( const double x ) { + double z; + + if ( stdlib_base_is_nan( x ) ) { + return STDLIB_CONSTANT_FLOAT64_NAN; + } + if ( x == STDLIB_CONSTANT_FLOAT64_NINF ) { + return -0.0; + } + z = SQRT_TWO_DIVIDED_BY_PI * ( x + ( 0.044715 * x * x * x ) ); + return x * stdlib_base_expit( 2.0 * z ); +} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_negative.json b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_negative.json new file mode 100644 index 000000000000..db741322f311 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_negative.json @@ -0,0 +1 @@ +{"x": [-20.0, -19.88, -19.75, -19.62, -19.5, -19.38, -19.25, -19.12, -19.0, -18.88, -18.75, -18.62, -18.5, -18.38, -18.25, -18.12, -18.0, -17.88, -17.75, -17.62, -17.5, -17.38, -17.25, -17.12, -17.0, -16.88, -16.75, -16.62, -16.5, -16.38, -16.25, -16.12, -16.0, -15.88, -15.75, -15.62, -15.5, -15.38, -15.25, -15.12, -15.0, -14.88, -14.75, -14.62, -14.5, -14.38, -14.25, -14.12, -14.0, -13.88, -13.75, -13.62, -13.5, -13.38, -13.25, -13.12, -13.0, -12.88, -12.75, -12.62, -12.5, -12.38, -12.25, -12.12, -12.0, -11.88, -11.75, -11.62, -11.5, -11.38, -11.25, -11.12, -11.0, -10.88, -10.75, -10.62, -10.5, -10.38, -10.25, -10.12, -10.0, -9.88, -9.75, -9.62, -9.5, -9.38, -9.25, -9.12, -9.0, -8.88, -8.75, -8.62, -8.5, -8.38, -8.25, -8.12, -8.0, -7.88, -7.75, -7.62, -7.5, -7.38, -7.25, -7.12, -7.0, -6.88, -6.75, -6.62, -6.5, -6.38, -6.25, -6.12, -6.0, -5.88, -5.75, -5.62, -5.5, -5.38, -5.25, -5.12, -5.0], "expected": [-3.3754509563111e-261, -1.1080968258937536e-256, -7.535354160971915e-252, -4.441714944179174e-247, -9.91502102520117e-243, -1.962500953394625e-238, -7.741143834229349e-234, -2.656388589730113e-229, -3.6102852236779525e-225, -4.364178951507308e-221, -1.0125256845132593e-216, -2.0510244028620074e-212, -1.719103171866002e-208, -1.2855292426009311e-204, -1.7788212566230665e-200, -2.1568201103541055e-196, -1.1292762288213121e-192, -5.291429919723695e-189, -4.428009134943552e-185, -3.258705506067626e-181, -1.079593144180243e-177, -3.2106970413319137e-174, -1.6476226562081883e-170, -7.462547384270967e-167, -1.5845430789486083e-163, -3.0295820032394406e-160, -9.667218185629468e-157, -2.7325045626416774e-153, -3.766626506357726e-150, -4.6896812624676877e-147, -9.435380712504147e-144, -1.6876625171333639e-140, -1.5297633712990981e-137, -1.256317136200835e-134, -1.6160285453690824e-131, -1.8547193216414174e-128, -1.1197873667454283e-125, -6.144215318028214e-123, -5.123721610491666e-120, -3.826072699222122e-117, -1.5584769937273875e-114, -5.787060595332804e-112, -3.1723527329825563e-109, -1.5628686222592367e-106, -4.350406541145407e-104, -1.1073488165135653e-101, -4.046191120118877e-99, -1.3335049547173865e-96, -2.5694002773535767e-94, -4.5410124557435936e-92, -1.1214666581566025e-89, -2.507118141248447e-87, -3.386962024242372e-85, -4.209843642011844e-83, -7.125305671876238e-81, -1.095630090309725e-78, -1.0511517767632373e-76, -9.307285833250862e-75, -1.0947005249781974e-72, -1.1739735629449553e-70, -8.101996464473597e-69, -5.1762784681468403e-67, -4.289968154941641e-65, -3.2534834493205544e-63, -1.6359854493534714e-61, -7.639016387407256e-60, -4.5234079281358864e-58, -2.4599090823220053e-56, -9.128725161003705e-55, -3.1554641032775443e-53, -1.353658585450136e-51, -5.35237260830485e-50, -1.4847686071591282e-48, -3.8482880978745625e-47, -1.2127028276717489e-45, -3.5350708616880803e-44, -7.424900556078979e-43, -1.4615484194330904e-41, -3.4305316059528595e-40, -7.475347001547308e-39, -1.2040923482098023e-37, -1.8232708743349744e-36, -3.2320486413989405e-35, -5.338141595037037e-34, -6.678928766783929e-33, -7.879859247092866e-32, -1.0696373264148583e-30, -1.3576965532403077e-29, -1.3364595947348808e-28, -1.2443274663892201e-27, -1.311450788484399e-26, -1.2971148824946444e-25, -1.0174447308598222e-24, -7.571785824275761e-24, -6.282266543229132e-23, -4.909109243204186e-22, -3.107782937501113e-21, -1.8723254182171456e-20, -1.2399200006322284e-19, -7.761239290526721e-19, -4.016268217514138e-18, -1.9839114131345588e-17, -1.0631936227657137e-16, -5.404819899162055e-16, -2.315442262072538e-15, -9.497665888177463e-15, -4.175948320418421e-14, -1.7479210074051656e-13, -6.278258772591381e-13, -2.1657341922675232e-12, -7.920355792295727e-12, -2.7673049157041198e-11, -8.439646700762311e-11, -2.479436938627092e-10, -7.645817838404513e-10, -2.260500671914926e-09, -5.9276402638802525e-09, -1.5018490211183306e-08, -3.9584638214958364e-08, -1.003838192885009e-07, -2.291796196629504e-07]} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_positive.json b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_large_positive.json new file mode 100644 index 000000000000..c855800d23bf --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_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.999999770820381, 5.499999994072359, 5.9999999999156035, 6.499999999999372, 6.999999999999998, 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]} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_medium.json b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_medium.json new file mode 100644 index 000000000000..aeed3282bc05 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_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": [-2.291796196629504e-07, -3.202679014473874e-07, -4.451491861211161e-07, -6.154245754638367e-07, -8.463372777437633e-07, -1.1578005809358318e-06, -1.575679755577806e-06, -2.1333801765896724e-06, -2.8738019680088645e-06, -3.851730043891983e-06, -5.1367374119586445e-06, -6.816688842853257e-06, -9.001941250344762e-06, -1.1830346079546662e-05, -1.5473167301920282e-05, -2.0142035844940132e-05, -2.6097066957287436e-05, -3.365627058757381e-05, -4.3206385748086556e-05, -5.5215267420544536e-05, -7.024594819237264e-05, -8.897248584015884e-05, -0.00011219769187022598, -0.00014087281399462081, -0.00017611921714141742, -0.00021925207244165732, -0.00027180602139205326, -0.00033556273289844427, -0.0004125802141635831, -0.000505223672585536, -0.0006161976553736639, -0.0007485791170745485, -0.0009058509834536628, -0.001091935694238043, -0.0013112281183358038, -0.0015686271447474677, -0.0018695651620842869, -0.002220034551173467, -0.0026266102305497734, -0.003096467215696948, -0.003637392081773019, -0.004257787158341684, -0.004966666235471413, -0.0057736405255954994, -0.006688893606879183, -0.00772314407361796, -0.008887594639467896, -0.010193866482108982, -0.011653917685242701, -0.013279944727529453, -0.015084266089998574, -0.01707918720531532, -0.019276846155601723, -0.021689039741605798, -0.02432702979591904, -0.027201329897288608, -0.0303214729619886, -0.03369576054117915, -0.03733099503888788, -0.041232196481418626, -0.045402305912224966, -0.049841877951888824, -0.054548765544667394, -0.05951780040340448, -0.06474047315407029, -0.07020461765874866, -0.07589410444884381, -0.08178854861443825, -0.08786303785563848, -0.09408788669099269, -0.10042842301976704, -0.10684481333220922, -0.1132919328388055, -0.11971928663132052, -0.1260709876826906, -0.1322857970302854, -0.13829723086213513, -0.14403373843746972, -0.14941895382548911, -0.15437202334938305, -0.1588080093917233, -0.16263836987363972, -0.16577151129027942, -0.16811341170035707, -0.1695683085635519, -0.1700394448343797, -0.16942986529488321, -0.16764325378115408, -0.16458480076918525, -0.16016208977003138, -0.15428599017485609, -0.14687154361411522, -0.13783883057264382, -0.1271138039477433, -0.11462907645724074, -0.100324649298315, -0.08414857021789372, -0.0660575101670739, -0.04601724895456484, -0.024003061761377995, 0.0, 0.025996938238622008, 0.05398275104543518, 0.0839424898329261, 0.11585142978210627, 0.149675350701685, 0.18537092354275927, 0.22288619605225668, 0.2621611694273562, 0.30312845638588476, 0.34571400982514394, 0.3898379102299687, 0.43541519923081473, 0.48235674621884594, 0.5305701347051167, 0.5799605551656203, 0.6304316914364483, 0.6818865882996429, 0.7342284887097207, 0.7873616301263602, 0.8411919906082768, 0.8956279766506171, 0.9505810461745109, 1.0059662615625302, 1.0617027691378649, 1.1177142029697147, 1.1739290123173094, 1.2302807133686797, 1.2867080671611946, 1.3431551866677907, 1.3995715769802328, 1.4559121133090072, 1.5121369621443617, 1.5682114513855618, 1.6241058955511563, 1.6797953823412515, 1.7352595268459297, 1.7904821995965956, 1.8454512344553327, 1.900158122048111, 1.9545976940877752, 2.008767803518581, 2.062669004961112, 2.1163042394588207, 2.1696785270380112, 2.222798670102711, 2.2756729702040803, 2.3283109602583947, 2.3807231538443983, 2.4329208127946846, 2.484915733910001, 2.536720055272471, 2.5883460823147573, 2.639806133517891, 2.691112405360532, 2.7422768559263817, 2.7933111063931206, 2.8442263594744044, 2.895033333764528, 2.945742212841658, 2.9963626079182273, 3.046903532784303, 3.0973733897694506, 3.1477799654488265, 3.198130434837916, 3.2484313728552525, 3.2986887718816638, 3.348908064305762, 3.399094149016546, 3.449251420882926, 3.499383802344626, 3.549494776327414, 3.5995874197858364, 3.6496644372671017, 3.699728193978608, 3.7497807479275584, 3.799823880782858, 3.849859127186005, 3.89988780230813, 3.9499110275141596, 3.9999297540518075, 4.049944784732579, 4.099956793614251, 4.149966343729413, 4.199973902933043, 4.2499798579641554, 4.299984526832698, 4.34998816965392, 4.39999099805875, 4.4499931833111575, 4.499994863262588, 4.549996148269956, 4.599997126198032, 4.649997866619824, 4.699998424320245, 4.749998842199419, 4.799999153662722, 4.849999384575424, 4.8999995548508135, 4.949999679732098, 4.999999770820381]} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_near_zero.json b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_near_zero.json new file mode 100644 index 000000000000..219088bdbd75 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_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.1588080093917233, -0.16041789591854838, -0.1619251051939679, -0.16332375358918966, -0.1646078917428168, -0.16577151129027942, -0.16680855185524218, -0.1677129082924057, -0.16847843817016062, -0.16909896948060912, -0.1695683085635519, -0.16988024823014364, -0.17002857607106167, -0.17000708293320457, -0.1698095715481534, -0.16942986529488321, -0.16886181707851824, -0.16809931830627572, -0.16713630794115428, -0.16596678161338457, -0.16458480076918525, -0.1629845018359545, -0.16116010538267678, -0.15910592525404327, -0.15681637765657033, -0.15428599017485609, -0.15150941069604196, -0.1484814162205465, -0.14519692153720762, -0.14165098774111703, -0.13783883057264382, -0.13375582855643656, -0.12939753091955214, -0.12475966526829127, -0.11983814500382205, -0.11462907645724074, -0.10912876572535442, -0.10333372518916696, -0.09724067969781287, -0.09084657240149918, -0.08414857021789372, -0.0771440689173257, -0.06983069781314308, -0.06220632404459751, -0.054269056440695705, -0.04601724895456484, -0.03744950365902323, -0.028564673295223924, -0.01936186336744185, -0.009840433778302907, 0.0, 0.010159566221697091, 0.020638136632558153, 0.03143532670477607, 0.04255049634097678, 0.05398275104543518, 0.0657309435593043, 0.07779367595540251, 0.09016930218685693, 0.1028559310826743, 0.11585142978210627, 0.1291534275985008, 0.1427593203021871, 0.15666627481083303, 0.17087123427464562, 0.18537092354275927, 0.20016185499617797, 0.21524033473170878, 0.23060246908044785, 0.2462441714435634, 0.2621611694273562, 0.278349012258883, 0.29480307846279236, 0.3115185837794536, 0.328490589303958, 0.34571400982514394, 0.36318362234342966, 0.3808940747459568, 0.3988398946173233, 0.4170154981640455, 0.43541519923081473, 0.4540332183866154, 0.47286369205884576, 0.49190068169372436, 0.5111381829214818, 0.5305701347051167, 0.5501904284518465, 0.5699929170667953, 0.5899714239289383, 0.6101197517698564, 0.6304316914364483, 0.6509010305193909, 0.6715215618298392, 0.6922870917075942, 0.7131914481447579, 0.7342284887097207, 0.7553921082571832, 0.7766762464108103, 0.7980748948060321, 0.8195821040814516, 0.8411919906082768]} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_special.json b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_special.json new file mode 100644 index 000000000000..329e10578773 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/gelu_tanh_special.json @@ -0,0 +1 @@ +{"x": [-50.0, -25.0, -22.0, -2.0, -1.0, 0.0, 1.0, 2.0, 10.0, 50.0], "expected": [-0.0, -0.0, -0.0, -0.045402305912224966, -0.1588080093917233, 0.0, 0.8411919906082768, 1.9545976940877752, 10.0, 50.0]} diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py new file mode 100644 index 000000000000..5197bd29dcb1 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py @@ -0,0 +1,82 @@ +#!/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 os +import json +import numpy as np +from scipy.special import expit + +# Get the file path: +FILE = os.path.realpath(__file__) + +# Extract the directory in which this file resides: +DIR = os.path.dirname(FILE) + +# Constant sqrt(2/pi): +SQRT_TWO_DIVIDED_BY_PI = np.sqrt(2.0 / np.pi) + + +def eval_gelu_tanh(x): + """Evaluate GELU tanh approximation formula.""" + z = SQRT_TWO_DIVIDED_BY_PI * (x + 0.044715 * (x**3)) + return x * expit(2.0 * z) + + +def gen(x, eval_fn, name): + """Generate fixture data and writes them to file. + + # Arguments + + * `x`: domain + * `eval_fn`: function to evaluate + * `name::str`: output filename + """ + x_arr = np.array(x, dtype=np.float64) + y_arr = eval_fn(x_arr) + data = { + "x": x_arr.tolist(), + "expected": y_arr.tolist() + } + filepath = os.path.join(DIR, name) + with open(filepath, "w", encoding="utf-8", newline="\n") as outfile: + json.dump(data, outfile) + outfile.write("\n") + + +def main(): + """Generate fixture data.""" + x = [round(float(v), 4) for v in np.linspace(-1.0, 1.0, 101)] + gen(x, eval_gelu_tanh, "gelu_tanh_near_zero.json") + + x = [round(float(v), 4) for v in np.linspace(-5.0, 5.0, 201)] + gen(x, eval_gelu_tanh, "gelu_tanh_medium.json") + + x = [round(float(v), 2) for v in np.linspace(5.0, 50.0, 91)] + gen(x, eval_gelu_tanh, "gelu_tanh_large_positive.json") + + x = [round(float(v), 2) for v in np.linspace(-20.0, -5.0, 121)] + gen(x, eval_gelu_tanh, "gelu_tanh_large_negative.json") + + x = [-50.0, -25.0, -22.0, -2.0, -1.0, 0.0, 1.0, 2.0, 10.0, 50.0] + gen(x, eval_gelu_tanh, "gelu_tanh_special.json") + + +if __name__ == "__main__": + main() diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js new file mode 100644 index 000000000000..58ea8dd62ff0 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js @@ -0,0 +1,171 @@ +/** +* @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 geluTanh = require( './../lib' ); + + +// FIXTURES // + +var nearZero = require( './fixtures/python/gelu_tanh_near_zero.json' ); +var medium = require( './fixtures/python/gelu_tanh_medium.json' ); +var largePositive = require( './fixtures/python/gelu_tanh_large_positive.json' ); +var largeNegative = require( './fixtures/python/gelu_tanh_large_negative.json' ); +var special = require( './fixtures/python/gelu_tanh_special.json' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof geluTanh, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', function test( t ) { + var v = geluTanh( NaN ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function returns `+Infinity` when provided `+Infinity`', function test( t ) { + var v = geluTanh( PINF ); + t.strictEqual( v, PINF, 'returns +Infinity' ); + t.end(); +}); + +tape( 'the function returns `-0.0` when provided `-Infinity`', function test( t ) { + var v = geluTanh( NINF ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function preserves the sign of zero', function test( t ) { + var v = geluTanh( 0.0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0.0' ); + + v = geluTanh( -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 = geluTanh( 1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e200, 'returns x for very large positive x' ); + + v = geluTanh( -1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + + v = geluTanh( 1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e308, 'returns x for very large positive x' ); + + v = geluTanh( -1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation near zero', function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = nearZero.x; + expected = nearZero.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for moderate inputs', function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = medium.x; + expected = medium.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for large positive inputs', function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = largePositive.x; + expected = largePositive.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = largeNegative.x; + expected = largeNegative.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 750 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for special values', function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = special.x; + expected = special.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js new file mode 100644 index 000000000000..de34b208ae07 --- /dev/null +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js @@ -0,0 +1,180 @@ +/** +* @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 geluTanh = tryRequire( resolve( __dirname, './../lib/native.js' ) ); +var opts = { + 'skip': ( geluTanh instanceof Error ) +}; + + +// FIXTURES // + +var nearZero = require( './fixtures/python/gelu_tanh_near_zero.json' ); +var medium = require( './fixtures/python/gelu_tanh_medium.json' ); +var largePositive = require( './fixtures/python/gelu_tanh_large_positive.json' ); +var largeNegative = require( './fixtures/python/gelu_tanh_large_negative.json' ); +var special = require( './fixtures/python/gelu_tanh_special.json' ); + + +// TESTS // + +tape( 'main export is a function', opts, function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof geluTanh, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) { + var v = geluTanh( NaN ); + t.strictEqual( isnan( v ), true, 'returns NaN' ); + t.end(); +}); + +tape( 'the function returns `+Infinity` when provided `+Infinity`', opts, function test( t ) { + var v = geluTanh( PINF ); + t.strictEqual( v, PINF, 'returns +Infinity' ); + t.end(); +}); + +tape( 'the function returns `-0.0` when provided `-Infinity`', opts, function test( t ) { + var v = geluTanh( 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 = geluTanh( 0.0 ); + t.strictEqual( isPositiveZero( v ), true, 'returns +0.0' ); + + v = geluTanh( -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 = geluTanh( 1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e200, 'returns x for very large positive x' ); + + v = geluTanh( -1e200 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + + v = geluTanh( 1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( v, 1e308, 'returns x for very large positive x' ); + + v = geluTanh( -1e308 ); + t.strictEqual( isnan( v ), false, 'does not return NaN' ); + t.strictEqual( isNegativeZero( v ), true, 'returns -0.0' ); + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation near zero', opts, function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = nearZero.x; + expected = nearZero.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for moderate inputs', opts, function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = medium.x; + expected = medium.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 15 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for large positive inputs', opts, function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = largePositive.x; + expected = largePositive.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 1 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', opts, function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = largeNegative.x; + expected = largeNegative.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 750 ), true, 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function evaluates GELU tanh approximation for special values', opts, function test( t ) { // eslint-disable-line max-len + var expected; + var x; + var y; + var i; + + x = special.x; + expected = special.expected; + for ( i = 0; i < x.length; i++ ) { + y = geluTanh( x[ i ] ); + t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' ); + } + t.end(); +}); From 3cdd684dbd68a36cbc572f8f7337dcf1ce2d7e67 Mon Sep 17 00:00:00 2001 From: Aryan Sharma Date: Thu, 15 Oct 2026 20:12:26 +0530 Subject: [PATCH 2/5] fix: list expansion in binding.gyp Signed-off-by: Aryan Sharma --- .../base/special/fast/gelu-tanh/binding.gyp | 127 +++++++++--------- 1 file changed, 67 insertions(+), 60 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp index 750772983f5e..0d6508a12e99 100644 --- a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/binding.gyp @@ -58,106 +58,113 @@ # Define directories which contain relevant include headers: 'include_dirs': [ - '<(include_dirs)', + # Local include directory: + '<@(include_dirs)', ], - # Define compilation sources: + # List of source files: 'sources': [ - '<(src_files)', + '<@(src_files)', ], - # Define library directories: - 'link_dirs': [ - '<(library_dirs)', + # 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', ], - # Define libraries: - 'libraries': [ - '<(libraries)', + # C specific compiler flags: + 'cflags_c': [ + # Specify the C standard to which a program is expected to conform: + '-std=c99', ], - # Define direct dependent settings: - 'direct_dependent_settings': { - # Define directories which contain relevant include headers: - 'include_dirs': [ - '<(include_dirs)', - ], - }, # end direct_dependent_settings + # C++ specific compiler flags: + 'cflags_cpp': [ + # Specify the C++ standard to which a program is expected to conform: + '-std=c++11', + ], + + # Linker flags: + 'ldflags': [], - # Set custom conditions: + # Apply conditions based on the host OS: 'conditions': [ [ - 'OS=="win"', - { - # Apply custom flags when compiling on Windows: - 'msvs_settings': { - 'VCCLCompilerTool': { - 'ExceptionHandling': '0', # /EHsc - 'AdditionalOptions': [ - '/std:c11', - ], - }, - }, - }, # end condition (OS=="win") + 'OS=="mac"', { - # Apply custom flags when compiling on all other platforms: - 'cflags': [ - '-std=c99', + # Linker flags: + 'ldflags': [ + '-undefined dynamic_lookup', + '-Wl,-no-pie', + '-Wl,-search_paths_first', ], - } - ], # end condition (OS=="win") + }, + ], # end condition (OS=="mac") [ - 'OS=="mac"', + 'OS!="win"', { - 'xcode_settings': { - 'OTHER_CFLAGS': [ - '-std=c99', - ], - }, + # C/C++ flags: + 'cflags': [ + # Generate platform-independent code: + '-fPIC', + ], }, - ], # end condition (OS=="mac") + ], # end condition (OS!="win") ], # end conditions - }, # end target + }, # end target <(addon_target_name) - # Target to copy the add-on to the destination directory: + # Target to copy a generated add-on to a standard location: { - # The target name should match the add-on export name: - 'target_name': 'action_after_build', + 'target_name': 'copy_addon', - # The target type is a utility: + # Declare that the output of this target is not linked: 'type': 'none', - # Explicitly declare dependencies to ensure the dependencies are built before this target: + # Define dependencies: 'dependencies': [ + # Require that the add-on be generated before building this target: '<(addon_target_name)', ], - # Define actions to perform: + # Define a list of actions: 'actions': [ { - # Action name: - 'action_name': 'move_addon', + 'action_name': 'copy_addon', + 'message': 'Copying addon...', - # Action inputs: - 'inputs': [ - '<(PRODUCT_DIR)/<(addon_target_name).node', - ], + # Explicitly list the inputs in the command-line invocation below: + 'inputs': [], - # Action outputs: + # Declare the expected outputs: 'outputs': [ '<(addon_output_dir)/<(addon_target_name).node', ], - # Action to perform: + # Define the command-line invocation: 'action': [ - 'node', - '-e', - 'require( "fs" ).renameSync( process.argv[ 1 ], process.argv[ 2 ] );', + 'cp', '<(PRODUCT_DIR)/<(addon_target_name).node', '<(addon_output_dir)/<(addon_target_name).node', ], }, ], # end actions - }, # end target + }, # end target copy_addon ], # end targets } From 6e30fe4240ca060bb8c48ab213e52ff29751c99c Mon Sep 17 00:00:00 2001 From: Aryan Sharma Date: Thu, 15 Oct 2026 20:19:18 +0530 Subject: [PATCH 3/5] fix: remove unused disable directives Signed-off-by: Aryan Sharma --- .../math/base/special/fast/gelu-tanh/test/test.js | 10 +++++----- .../base/special/fast/gelu-tanh/test/test.native.js | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js index 58ea8dd62ff0..ed380daddc82 100644 --- a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.js @@ -95,7 +95,7 @@ tape( 'the function does not return `NaN` for large inputs', function test( t ) t.end(); }); -tape( 'the function evaluates GELU tanh approximation near zero', function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation near zero', function test( t ) { var expected; var x; var y; @@ -110,7 +110,7 @@ tape( 'the function evaluates GELU tanh approximation near zero', function test( t.end(); }); -tape( 'the function evaluates GELU tanh approximation for moderate inputs', function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for moderate inputs', function test( t ) { var expected; var x; var y; @@ -125,7 +125,7 @@ tape( 'the function evaluates GELU tanh approximation for moderate inputs', func t.end(); }); -tape( 'the function evaluates GELU tanh approximation for large positive inputs', function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for large positive inputs', function test( t ) { var expected; var x; var y; @@ -140,7 +140,7 @@ tape( 'the function evaluates GELU tanh approximation for large positive inputs' t.end(); }); -tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', function test( t ) { var expected; var x; var y; @@ -155,7 +155,7 @@ tape( 'the function evaluates GELU tanh approximation for large negative tail in t.end(); }); -tape( 'the function evaluates GELU tanh approximation for special values', function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for special values', function test( t ) { var expected; var x; var y; diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js index de34b208ae07..5c07846b86e2 100644 --- a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/test.native.js @@ -104,7 +104,7 @@ tape( 'the function does not return `NaN` for large inputs', opts, function test t.end(); }); -tape( 'the function evaluates GELU tanh approximation near zero', opts, function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation near zero', opts, function test( t ) { var expected; var x; var y; @@ -119,7 +119,7 @@ tape( 'the function evaluates GELU tanh approximation near zero', opts, function t.end(); }); -tape( 'the function evaluates GELU tanh approximation for moderate inputs', opts, function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for moderate inputs', opts, function test( t ) { var expected; var x; var y; @@ -134,7 +134,7 @@ tape( 'the function evaluates GELU tanh approximation for moderate inputs', opts t.end(); }); -tape( 'the function evaluates GELU tanh approximation for large positive inputs', opts, function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for large positive inputs', opts, function test( t ) { var expected; var x; var y; @@ -149,7 +149,7 @@ tape( 'the function evaluates GELU tanh approximation for large positive inputs' t.end(); }); -tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', opts, function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for large negative tail inputs', opts, function test( t ) { var expected; var x; var y; @@ -164,7 +164,7 @@ tape( 'the function evaluates GELU tanh approximation for large negative tail in t.end(); }); -tape( 'the function evaluates GELU tanh approximation for special values', opts, function test( t ) { // eslint-disable-line max-len +tape( 'the function evaluates GELU tanh approximation for special values', opts, function test( t ) { var expected; var x; var y; From e5251fc3d472e43cea3013e10f96918070023cf6 Mon Sep 17 00:00:00 2001 From: Aryan Sharma Date: Thu, 15 Oct 2026 20:27:53 +0530 Subject: [PATCH 4/5] fix: wrapping lines in repl.txt Signed-off-by: Aryan Sharma --- .../@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt index fe6ab58d58f9..541e70819d02 100644 --- a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/docs/repl.txt @@ -1,6 +1,7 @@ {{alias}}( x ) - Evaluates the Gaussian Error Linear Unit (GELU) activation function using a tanh approximation. + Evaluates the Gaussian Error Linear Unit (GELU) activation function using a + tanh approximation. Parameters ---------- From c611d8f995c51e4b6cc0ecbbfead8d75c9f7fcc0 Mon Sep 17 00:00:00 2001 From: Aryan Sharma Date: Sat, 17 Oct 2026 00:33:32 +0530 Subject: [PATCH 5/5] fix: simplify fixture generator in runner file Signed-off-by: Aryan Sharma --- .../gelu-tanh/test/fixtures/python/runner.py | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py index 5197bd29dcb1..a45d5ed061c3 100644 --- a/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py +++ b/lib/node_modules/@stdlib/math/base/special/fast/gelu-tanh/test/fixtures/python/runner.py @@ -33,49 +33,42 @@ SQRT_TWO_DIVIDED_BY_PI = np.sqrt(2.0 / np.pi) -def eval_gelu_tanh(x): - """Evaluate GELU tanh approximation formula.""" - z = SQRT_TWO_DIVIDED_BY_PI * (x + 0.044715 * (x**3)) - return x * expit(2.0 * z) - - -def gen(x, eval_fn, name): +def gen(x, name): """Generate fixture data and writes them to file. # Arguments * `x`: domain - * `eval_fn`: function to evaluate * `name::str`: output filename """ x_arr = np.array(x, dtype=np.float64) - y_arr = eval_fn(x_arr) + z = SQRT_TWO_DIVIDED_BY_PI * (x_arr + 0.044715 * (x_arr**3)) + y_arr = x_arr * expit(2.0 * z) data = { "x": x_arr.tolist(), "expected": y_arr.tolist() } filepath = os.path.join(DIR, name) - with open(filepath, "w", encoding="utf-8", newline="\n") as outfile: + with open(filepath, "w", encoding="utf-8") as outfile: json.dump(data, outfile) - outfile.write("\n") def main(): """Generate fixture data.""" x = [round(float(v), 4) for v in np.linspace(-1.0, 1.0, 101)] - gen(x, eval_gelu_tanh, "gelu_tanh_near_zero.json") + gen(x, "gelu_tanh_near_zero.json") x = [round(float(v), 4) for v in np.linspace(-5.0, 5.0, 201)] - gen(x, eval_gelu_tanh, "gelu_tanh_medium.json") + gen(x, "gelu_tanh_medium.json") x = [round(float(v), 2) for v in np.linspace(5.0, 50.0, 91)] - gen(x, eval_gelu_tanh, "gelu_tanh_large_positive.json") + gen(x, "gelu_tanh_large_positive.json") x = [round(float(v), 2) for v in np.linspace(-20.0, -5.0, 121)] - gen(x, eval_gelu_tanh, "gelu_tanh_large_negative.json") + gen(x, "gelu_tanh_large_negative.json") x = [-50.0, -25.0, -22.0, -2.0, -1.0, 0.0, 1.0, 2.0, 10.0, 50.0] - gen(x, eval_gelu_tanh, "gelu_tanh_special.json") + gen(x, "gelu_tanh_special.json") if __name__ == "__main__":