Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions js/common/lib/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Tensor as TensorImpl } from './tensor-impl.js';
import { TypedTensorUtils } from './tensor-utils.js';
import { TryGetGlobalType } from './type-helper.js';

// Helper type: resolves to the instance type of `Float16Array` if it exists in the global scope,
// or `never` otherwise. Uses the shared TryGetGlobalType helper.
export type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>;
Copy link

@RReverser RReverser Dec 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it needs to be exported / part of the public API, just a local helper.

Suggested change
export type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>;
type GlobalFloat16Array = TryGetGlobalType<'Float16Array', never>;


/* eslint-disable @typescript-eslint/no-redeclare */

/**
Expand Down Expand Up @@ -240,6 +244,19 @@ export interface TensorConstructor extends TensorFactory {
dims?: readonly number[],
): TypedTensor<T>;

/**
* Construct a new float16 tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (
type: 'float16',
data: Tensor.DataTypeMap['float16'] | GlobalFloat16Array | readonly number[],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, wouldn't it be easier to inline this helper into the DataTypeMap?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated DataTypeMap.float16 to inline TryGetGlobalType<'Float16Array'> and map it to the instance type via prototype, so Float16Array instances are accepted where supported without changing runtime behavior.

dims?: readonly number[],
): TypedTensor<'float16'>;

/**
* Construct a new numeric tensor object from the given type, data and dims.
*
Expand All @@ -264,6 +281,14 @@ export interface TensorConstructor extends TensorFactory {
*/
new (data: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;

/**
* Construct a new float16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: GlobalFloat16Array, dims?: readonly number[]): TypedTensor<'float16'>;

/**
* Construct a new int8 tensor object from the given data and dims.
*
Expand Down