From 58d4e0a6b3dc64f925dc7acdba9a81aa65508d9e Mon Sep 17 00:00:00 2001 From: Samarth Kolarkar Date: Thu, 24 Sep 2026 15:09:07 +0530 Subject: [PATCH] feat: add float16 dtype support to ndarray/base/transpose --- .../base/transpose/benchmark/benchmark.js | 22 +++++ .../benchmark/benchmark.size.float16.js | 96 +++++++++++++++++++ .../ndarray/base/transpose/docs/types/test.ts | 1 + .../ndarray/base/transpose/test/test.js | 77 +++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.size.float16.js diff --git a/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.js index e73d175f0e94..7b65fd94e5ca 100644 --- a/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.js @@ -74,6 +74,28 @@ bench( format( '%s::base:dtype=float32', pkg ), function benchmark( b ) { b.end(); }); +bench( format( '%s::base:dtype=float16', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = zeros( 'float16', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = transpose( x, false ); + if ( y.length !== 4 ) { + b.fail( 'should have expected length' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + bench( format( '%s::base:dtype=complex128', pkg ), function benchmark( b ) { var x; var y; diff --git a/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.size.float16.js b/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.size.float16.js new file mode 100644 index 000000000000..a5e24c9c74fe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/transpose/benchmark/benchmark.size.float16.js @@ -0,0 +1,96 @@ +/** +* @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 pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var transpose = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeros( 'float16', [ 10, len/10 ], 'row-major' ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var arr; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + arr = transpose( x, false ); + if ( arr.length !== len ) { + b.fail( 'unexpected length' ); + } + } + b.toc(); + if ( !isndarrayLike( arr ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s::base:dtype=float16,size=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/ndarray/base/transpose/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/transpose/docs/types/test.ts index 2ae474b1ae6b..a2067023a87c 100644 --- a/lib/node_modules/@stdlib/ndarray/base/transpose/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/transpose/docs/types/test.ts @@ -29,6 +29,7 @@ import transpose = require( './index' ); transpose( zeros( 'float64', sh, ord ), false ); // $ExpectType float64ndarray transpose( zeros( 'float32', sh, ord ), false ); // $ExpectType float32ndarray + transpose( zeros( 'float16', sh, ord ), false ); // $ExpectType float16ndarray transpose( zeros( 'complex128', sh, ord ), false ); // $ExpectType complex128ndarray transpose( zeros( 'complex64', sh, ord ), false ); // $ExpectType complex64ndarray transpose( zeros( 'int32', sh, ord ), false ); // $ExpectType int32ndarray diff --git a/lib/node_modules/@stdlib/ndarray/base/transpose/test/test.js b/lib/node_modules/@stdlib/ndarray/base/transpose/test/test.js index 30bbcae826c9..e9840d954c14 100644 --- a/lib/node_modules/@stdlib/ndarray/base/transpose/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/base/transpose/test/test.js @@ -23,6 +23,7 @@ var tape = require( 'tape' ); var Float64Array = require( '@stdlib/array/float64' ); var Float32Array = require( '@stdlib/array/float32' ); +var Float16Array = require( '@stdlib/array/float16' ); var Int32Array = require( '@stdlib/array/int32' ); var Uint32Array = require( '@stdlib/array/uint32' ); var Int16Array = require( '@stdlib/array/int16' ); @@ -251,6 +252,82 @@ tape( 'the function transposes a matrix (dtype=float32, non-base)', function tes t.end(); }); +tape( 'the function transposes a matrix (dtype=float16, base)', function test( t ) { + var arr; + var buf; + var v1; + var v2; + var x; + var i; + var j; + + buf = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float16', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + arr = transpose( x, false ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + + for ( i = 0; i < 2; i++ ) { + for ( j = 0; j < 3; j++ ) { + v1 = x.get( i, j ); + v2 = arr.get( j, i ); + t.strictEqual( v1, v2, 'returns expected value for ('+j+','+i+')' ); + } + } + + t.end(); +}); + +tape( 'the function transposes a matrix (dtype=float16, non-base)', function test( t ) { + var arr; + var buf; + var v1; + var v2; + var x; + var i; + var j; + + buf = new Float16Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = array( buf, { + 'shape': [ 2, 3 ], + 'dtype': 'float16' + }); + arr = transpose( x, false ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + + for ( i = 0; i < 2; i++ ) { + for ( j = 0; j < 3; j++ ) { + v1 = x.get( i, j ); + v2 = arr.get( j, i ); + t.strictEqual( v1, v2, 'returns expected value for ('+j+','+i+')' ); + } + } + + arr = transpose( x, true ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( arr ), [ 1, 3 ], 'returns expected value' ); + t.strictEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), false, 'returns expected value' ); + + t.end(); +}); + tape( 'the function transposes a matrix (dtype=int32, base)', function test( t ) { var arr; var buf;