easy-embeddings
Version:
Easy, fast and WASM/WebGPU accelerated vector embedding for the web platform. Locally via ONNX/Transformers.js and via API. Compatible with Browsers, Workers, Web Extensions, Node.js & co.
1,170 lines (1,159 loc) • 196 kB
text/typescript
import { EmbeddingProvider, EmbeddingParams, EmbeddingApiOptions, EmbeddingResponse } from 'cross-llm';
declare const DATA_TYPES: Readonly<{
fp32: "fp32";
fp16: "fp16";
q8: "q8";
int8: "int8";
uint8: "uint8";
q4: "q4";
bnb4: "bnb4";
q4f16: "q4f16";
}>;
type DataType$1 = keyof typeof DATA_TYPES;
declare const DEVICE_TYPES: Readonly<{
cpu: "cpu";
gpu: "gpu";
wasm: "wasm";
webgpu: "webgpu";
}>;
type DeviceType = keyof typeof DEVICE_TYPES;
/**
* Options for loading a pretrained model.
*/
type PretrainedOptions$1 = {
/**
* If specified, this function will be called during model construction, to provide the user with progress updates.
*/
progress_callback?: Function;
/**
* Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
* - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory.
*/
config?: any;
/**
* Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
*/
cache_dir?: string;
/**
* Whether or not to only look at local files (e.g., not try downloading the model).
*/
local_files_only?: boolean;
/**
* The specific model version to use. It can be a branch name, a tag name, or a commit id,
* since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git.
* NOTE: This setting is ignored for local requests.
*/
revision?: string;
};
/**
* Options for loading a pretrained model.
*/
type ModelSpecificPretrainedOptions = {
/**
* In case the relevant files are located inside a subfolder of the model repo on huggingface.co,
* you can specify the folder name here.
*/
subfolder?: string;
/**
* If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
*/
model_file_name?: string;
/**
* The device to run the model on. If not specified, the device will be chosen from the environment settings.
*/
device?: DeviceType | Record<string, DeviceType>;
/**
* The data type to use for the model. If not specified, the data type will be chosen from the environment settings.
*/
dtype?: DataType$1 | Record<string, DataType$1>;
/**
* Whether to load the model using the external data format (used for models >= 2GB in size).
*/
use_external_data_format?: boolean | Record<string, boolean>;
/**
* (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen.
*/
session_options?: any;
};
/**
* Options for loading a pretrained model.
*/
type PretrainedModelOptions = PretrainedOptions$1 & ModelSpecificPretrainedOptions;
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
type BigTypedArray = BigInt64Array | BigUint64Array;
type AnyTypedArray = TypedArray | BigTypedArray;
type ImageFormat = 'RGB' | 'RGBA' | 'BGR' | 'RBG';
type ImageTensorLayout = 'NHWC' | 'NCHW';
/**
* represent the parameter for constructing a tensor from a GPU resource.
*/
interface GpuResourceConstructorParameters<T extends Tensor$1.Type> {
/**
* an optional callback function to download data from GPU to CPU.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
download?(): Promise<Tensor$1.DataTypeMap[T]>;
/**
* an optional callback function that will be called when the tensor is disposed.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
dispose?(): void;
}
interface OptionsFormat {
/**
* Describes the image format represented in RGBA color space.
*/
format?: ImageFormat;
}
interface OptionsTensorFormat {
/**
* Describes the image format of the tensor.
*
* NOTE: this is different from option 'format'. While option 'format' represents the original image, 'tensorFormat'
* represents the target format of the tensor. A transpose will be performed if they are different.
*/
tensorFormat?: ImageFormat;
}
interface OptionsTensorDataType {
/**
* Describes the data type of the tensor.
*/
dataType?: 'float32' | 'uint8';
}
interface OptionsTensorLayout {
/**
* Describes the tensor layout when representing data of one or more image(s).
*/
tensorLayout?: ImageTensorLayout;
}
interface OptionsDimensions {
/**
* Describes the image height in pixel
*/
height?: number;
/**
* Describes the image width in pixel
*/
width?: number;
}
interface OptionResizedDimensions {
/**
* Describes the resized height. If omitted, original height will be used.
*/
resizedHeight?: number;
/**
* Describes resized width - can be accessed via tensor dimensions as well
*/
resizedWidth?: number;
}
interface OptionsNormalizationParameters {
/**
* Describes normalization parameters when preprocessing the image as model input.
*
* Data element are ranged from 0 to 255.
*/
norm?: {
/**
* The 'bias' value for image normalization.
* - If omitted, use default value 0.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
bias?: number | [number, number, number] | [number, number, number, number];
/**
* The 'mean' value for image normalization.
* - If omitted, use default value 255.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
mean?: number | [number, number, number] | [number, number, number, number];
};
}
interface TensorFromImageDataOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
interface TensorFromImageElementOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
interface TensorFromUrlOptions extends OptionsDimensions, OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
interface TensorFromImageBitmapOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
interface TensorFromTextureOptions<T extends Tensor$1.TextureDataTypes> extends Required<OptionsDimensions>, OptionsFormat, GpuResourceConstructorParameters<T> {
}
interface TensorFromGpuBufferOptions<T extends Tensor$1.GpuBufferDataTypes> extends Pick<Tensor$1, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
/**
* type TensorFactory defines the factory functions of 'Tensor' to create tensor instances from existing data or
* resources.
*/
interface TensorFactory {
/**
* create a tensor from an ImageData object
*
* @param imageData - the ImageData object to create tensor from
* @param options - An optional object representing options for creating tensor from ImageData.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageData: ImageData, options?: TensorFromImageDataOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a HTMLImageElement object
*
* @param imageElement - the HTMLImageElement object to create tensor from
* @param options - An optional object representing options for creating tensor from HTMLImageElement.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageElement: HTMLImageElement, options?: TensorFromImageElementOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from URL
*
* @param urlSource - a string as a URL to the image or a data URL containing the image data.
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(urlSource: string, options?: TensorFromUrlOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from an ImageBitmap object
*
* @param bitmap - the ImageBitmap object to create tensor from
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(bitmap: ImageBitmap, options: TensorFromImageBitmapOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a WebGL texture
*
* @param texture - the WebGLTexture object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGL texture.
*
* The options include following properties:
* - `width`: the width of the texture. Required.
* - `height`: the height of the texture. Required.
* - `format`: the format of the texture. If omitted, assume 'RGBA'.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromTexture<T extends Tensor$1.TextureDataTypes = 'float32'>(texture: Tensor$1.TextureType, options: TensorFromTextureOptions<T>): TypedTensor<'float32'>;
/**
* create a tensor from a WebGPU buffer
*
* @param buffer - the GPUBuffer object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGPU buffer.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromGpuBuffer<T extends Tensor$1.GpuBufferDataTypes>(buffer: Tensor$1.GpuBufferType, options: TensorFromGpuBufferOptions<T>): TypedTensor<T>;
/**
* create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.
*
* @param type - the tensor element type.
* @param buffer - a TypedArray corresponding to the type.
* @param dims - specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*
* @returns a tensor object
*/
fromPinnedBuffer<T extends Exclude<Tensor$1.Type, 'string'>>(type: T, buffer: Tensor$1.DataTypeMap[T], dims?: readonly number[]): TypedTensor<T>;
}
interface TensorToDataUrlOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
interface TensorToImageDataOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
interface ConversionUtils {
/**
* creates a DataURL instance from tensor
*
* @param options - An optional object representing options for creating a DataURL instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns a DataURL string representing the image converted from tensor data
*/
toDataURL(options?: TensorToDataUrlOptions): string;
/**
* creates an ImageData instance from tensor
*
* @param options - An optional object representing options for creating an ImageData instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns an ImageData instance representing the image converted from tensor data
*/
toImageData(options?: TensorToImageDataOptions): ImageData;
}
interface Properties {
/**
* Get the number of elements in the tensor.
*/
readonly size: number;
}
interface TypedShapeUtils<T extends Tensor$1.Type> {
/**
* Create a new tensor with the same data buffer and specified dims.
*
* @param dims - New dimensions. Size should match the old one.
*/
reshape(dims: readonly number[]): TypedTensor<T>;
}
/**
* interface `TensorUtils` includes all utility members that does not use the type parameter from their signature.
*/
interface TensorUtils extends Properties, ConversionUtils {
}
/**
* interface `TypedShapeUtils` includes all utility members that uses the type parameter from their signature.
*/
interface TypedTensorUtils<T extends Tensor$1.Type> extends TensorUtils, TypedShapeUtils<T> {
}
/**
* represent a basic tensor with specified dimensions and data type.
*/
interface TypedTensorBase<T extends Tensor$1.Type> {
/**
* Get the dimensions of the tensor.
*/
readonly dims: readonly number[];
/**
* Get the data type of the tensor.
*/
readonly type: T;
/**
* Get the buffer data of the tensor.
*
* If the data is not on CPU (eg. it's in the form of WebGL texture or WebGPU buffer), throw error.
*/
readonly data: Tensor$1.DataTypeMap[T];
/**
* Get the location of the data.
*/
readonly location: Tensor$1.DataLocation;
/**
* Get the WebGL texture that holds the tensor data.
*
* If the data is not on GPU as WebGL texture, throw error.
*/
readonly texture: Tensor$1.TextureType;
/**
* Get the WebGPU buffer that holds the tensor data.
*
* If the data is not on GPU as WebGPU buffer, throw error.
*/
readonly gpuBuffer: Tensor$1.GpuBufferType;
/**
* Get the buffer data of the tensor.
*
* If the data is on CPU, returns the data immediately.
* If the data is on GPU, downloads the data and returns the promise.
*
* @param releaseData - whether release the data on GPU. Ignore if data is already on CPU.
*/
getData(releaseData?: boolean): Promise<Tensor$1.DataTypeMap[T]>;
/**
* Dispose the tensor data.
*
* If the data is on CPU, remove its internal reference to the underlying data.
* If the data is on GPU, release the data on GPU.
*
* After calling this function, the tensor is considered no longer valid. Its location will be set to 'none'.
*/
dispose(): void;
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
interface TypedTensor<T extends Tensor$1.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {
}
/**
* type TensorConstructor defines the constructors of 'Tensor' to create CPU tensor instances.
*/
interface TensorConstructor extends TensorFactory {
/**
* Construct a new string 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: 'string', data: Tensor$1.DataTypeMap['string'] | readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool 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: 'bool', data: Tensor$1.DataTypeMap['bool'] | readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new 64-bit integer typed 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 <T extends 'uint64' | 'int64'>(type: T, data: Tensor$1.DataTypeMap[T] | readonly bigint[] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new numeric 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 <T extends Exclude<Tensor$1.Type, 'string' | 'bool' | 'uint64' | 'int64'>>(type: T, data: Tensor$1.DataTypeMap[T] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new float32 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: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;
/**
* Construct a new int8 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: Int8Array, dims?: readonly number[]): TypedTensor<'int8'>;
/**
* Construct a new uint8 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: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint16 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: Uint16Array, dims?: readonly number[]): TypedTensor<'uint16'>;
/**
* Construct a new int16 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: Int16Array, dims?: readonly number[]): TypedTensor<'int16'>;
/**
* Construct a new int32 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: Int32Array, dims?: readonly number[]): TypedTensor<'int32'>;
/**
* Construct a new int64 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: BigInt64Array, dims?: readonly number[]): TypedTensor<'int64'>;
/**
* Construct a new string 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: readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool 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: readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new float64 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: Float64Array, dims?: readonly number[]): TypedTensor<'float64'>;
/**
* Construct a new uint32 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: Uint32Array, dims?: readonly number[]): TypedTensor<'uint32'>;
/**
* Construct a new uint64 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: BigUint64Array, dims?: readonly number[]): TypedTensor<'uint64'>;
/**
* Construct a new 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: Tensor$1.Type, data: Tensor$1.DataType | readonly number[] | readonly string[] | readonly bigint[] | readonly boolean[], dims?: readonly number[]): Tensor$1;
/**
* Construct a new 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: Tensor$1.DataType, dims?: readonly number[]): Tensor$1;
}
declare namespace Tensor$1 {
interface DataTypeMap {
float32: Float32Array;
uint8: Uint8Array;
int8: Int8Array;
uint16: Uint16Array;
int16: Int16Array;
int32: Int32Array;
int64: BigInt64Array;
string: string[];
bool: Uint8Array;
float16: Uint16Array;
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: bigint;
string: string;
bool: boolean;
float16: number;
float64: number;
uint32: number;
uint64: bigint;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* supported data types for constructing a tensor from a pinned CPU buffer
*/
type CpuPinnedDataTypes = Exclude<Tensor$1.Type, 'string'>;
/**
* type alias for WebGL texture
*/
type TextureType = WebGLTexture;
/**
* supported data types for constructing a tensor from a WebGL texture
*/
type TextureDataTypes = 'float32';
/**
* type alias for WebGPU buffer
*
* The reason why we don't use type "GPUBuffer" defined in webgpu.d.ts from @webgpu/types is because "@webgpu/types"
* requires "@types/dom-webcodecs" as peer dependency when using TypeScript < v5.1 and its version need to be chosen
* carefully according to the TypeScript version being used. This means so far there is not a way to keep every
* TypeScript version happy. It turns out that we will easily broke users on some TypeScript version.
*
* for more info see https://github.com/gpuweb/types/issues/127
*/
type GpuBufferType = {
size: number;
mapState: 'unmapped' | 'pending' | 'mapped';
};
/**
* supported data types for constructing a tensor from a WebGPU buffer
*/
type GpuBufferDataTypes = 'float32' | 'float16' | 'int32' | 'int64' | 'uint32' | 'uint8' | 'bool';
/**
* represent where the tensor data is stored
*/
type DataLocation = 'none' | 'cpu' | 'cpu-pinned' | 'texture' | 'gpu-buffer';
/**
* represent the data type of a tensor
*/
type Type = keyof DataTypeMap;
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
interface Tensor$1 extends TypedTensorBase<Tensor$1.Type>, TypedTensorUtils<Tensor$1.Type> {
}
declare const Tensor$1: TensorConstructor;
/**
* @typedef {keyof typeof DataTypeMap} DataType
* @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray
*/
declare class Tensor {
/**
* Create a new Tensor or copy an existing Tensor.
* @param {[DataType, DataArray, number[]]|[ONNXTensor]} args
*/
constructor(...args: [DataType, DataArray, number[]] | [Tensor$1]);
set dims(arg: number[]);
/** @type {number[]} Dimensions of the tensor. */
get dims(): number[];
/** @type {DataType} Type of the tensor. */
get type(): "string" | "int8" | "uint8" | "float32" | "uint16" | "int16" | "int32" | "int64" | "bool" | "float16" | "float64" | "uint32" | "uint64";
/** @type {DataArray} The data stored in the tensor. */
get data(): DataArray;
/** @type {number} The number of elements in the tensor. */
get size(): number;
/** @type {string} The location of the tensor data. */
get location(): string;
ort_tensor: Tensor$1;
dispose(): void;
/**
* Index into a Tensor object.
* @param {number} index The index to access.
* @returns {Tensor} The data at the specified index.
*/
_getitem(index: number): Tensor;
/**
* @param {number|bigint} item The item to search for in the tensor
* @returns {number} The index of the first occurrence of item in the tensor data.
*/
indexOf(item: number | bigint): number;
/**
* @param {number} index
* @param {number} iterSize
* @param {any} iterDims
* @returns {Tensor}
*/
_subarray(index: number, iterSize: number, iterDims: any): Tensor;
/**
* Returns the value of this tensor as a standard JavaScript Number. This only works
* for tensors with one element. For other cases, see `Tensor.tolist()`.
* @returns {number|bigint} The value of this tensor as a standard JavaScript Number.
* @throws {Error} If the tensor has more than one element.
*/
item(): number | bigint;
/**
* Convert tensor data to a n-dimensional JS list
* @returns {Array}
*/
tolist(): any[];
/**
* Return a new Tensor with the sigmoid function applied to each element.
* @returns {Tensor} The tensor with the sigmoid function applied.
*/
sigmoid(): Tensor;
/**
* Applies the sigmoid function to the tensor in place.
* @returns {Tensor} Returns `this`.
*/
sigmoid_(): Tensor;
/**
* Return a new Tensor with every element multiplied by a constant.
* @param {number} val The value to multiply by.
* @returns {Tensor} The new tensor.
*/
mul(val: number): Tensor;
/**
* Multiply the tensor by a constant in place.
* @param {number} val The value to multiply by.
* @returns {Tensor} Returns `this`.
*/
mul_(val: number): Tensor;
/**
* Return a new Tensor with every element divided by a constant.
* @param {number} val The value to divide by.
* @returns {Tensor} The new tensor.
*/
div(val: number): Tensor;
/**
* Divide the tensor by a constant in place.
* @param {number} val The value to divide by.
* @returns {Tensor} Returns `this`.
*/
div_(val: number): Tensor;
/**
* Return a new Tensor with every element added by a constant.
* @param {number} val The value to add by.
* @returns {Tensor} The new tensor.
*/
add(val: number): Tensor;
/**
* Add the tensor by a constant in place.
* @param {number} val The value to add by.
* @returns {Tensor} Returns `this`.
*/
add_(val: number): Tensor;
clone(): Tensor;
slice(...slices: any[]): Tensor;
/**
* Return a permuted version of this Tensor, according to the provided dimensions.
* @param {...number} dims Dimensions to permute.
* @returns {Tensor} The permuted tensor.
*/
permute(...dims: number[]): Tensor;
transpose(...dims: any[]): Tensor;
/**
* Returns the sum of each row of the input tensor in the given dimension dim.
*
* @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced.
* @param {boolean} keepdim Whether the output tensor has `dim` retained or not.
* @returns The summed tensor
*/
sum(dim?: number, keepdim?: boolean): Tensor;
/**
* Returns the matrix norm or vector norm of a given tensor.
* @param {number|string} [p='fro'] The order of norm
* @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across.
* If dim is None, the norm will be calculated across all dimensions of input.
* @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not.
* @returns {Tensor} The norm of the tensor.
*/
norm(p?: number | string, dim?: number, keepdim?: boolean): Tensor;
/**
* Performs `L_p` normalization of inputs over specified dimension. Operates in place.
* @param {number} [p=2] The exponent value in the norm formulation
* @param {number} [dim=1] The dimension to reduce
* @returns {Tensor} `this` for operation chaining.
*/
normalize_(p?: number, dim?: number): Tensor;
/**
* Performs `L_p` normalization of inputs over specified dimension.
* @param {number} [p=2] The exponent value in the norm formulation
* @param {number} [dim=1] The dimension to reduce
* @returns {Tensor} The normalized tensor.
*/
normalize(p?: number, dim?: number): Tensor;
/**
* Compute and return the stride of this tensor.
* Stride is the jump necessary to go from one element to the next one in the specified dimension dim.
* @returns {number[]} The stride of this tensor.
*/
stride(): number[];
/**
* Returns a tensor with all specified dimensions of input of size 1 removed.
*
* NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.
* If you would like a copy, use `tensor.clone()` before squeezing.
*
* @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions.
* @returns {Tensor} The squeezed tensor
*/
squeeze(dim?: number): Tensor;
/**
* In-place version of @see {@link Tensor.squeeze}
*/
squeeze_(dim?: any): this;
/**
* Returns a new tensor with a dimension of size one inserted at the specified position.
*
* NOTE: The returned tensor shares the same underlying data with this tensor.
*
* @param {number} dim The index at which to insert the singleton dimension
* @returns {Tensor} The unsqueezed tensor
*/
unsqueeze(dim?: number): Tensor;
/**
* In-place version of @see {@link Tensor.unsqueeze}
*/
unsqueeze_(dim?: any): this;
/**
* In-place version of @see {@link Tensor.flatten}
*/
flatten_(start_dim?: number, end_dim?: number): this;
/**
* Flattens input by reshaping it into a one-dimensional tensor.
* If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim`
* and ending with `end_dim` are flattened. The order of elements in input is unchanged.
* @param {number} start_dim the first dim to flatten
* @param {number} end_dim the last dim to flatten
* @returns {Tensor} The flattened tensor.
*/
flatten(start_dim?: number, end_dim?: number): Tensor;
/**
* Returns a new tensor with the same data as the `self` tensor but of a different `shape`.
* @param {...number} dims the desired size
* @returns {Tensor} The tensor with the same data but different shape
*/
view(...dims: number[]): Tensor;
neg_(): this;
neg(): Tensor;
/**
* In-place version of @see {@link Tensor.clamp}
*/
clamp_(min: any, max: any): this;
/**
* Clamps all elements in input into the range [ min, max ]
* @param {number} min lower-bound of the range to be clamped to
* @param {number} max upper-bound of the range to be clamped to
* @returns {Tensor} the output tensor.
*/
clamp(min: number, max: number): Tensor;
/**
* In-place version of @see {@link Tensor.round}
*/
round_(): this;
/**
* Rounds elements of input to the nearest integer.
* @returns {Tensor} the output tensor.
*/
round(): Tensor;
mean(dim?: any, keepdim?: boolean): Tensor;
/**
* Performs Tensor dtype conversion.
* @param {DataType} type The desired data type.
* @returns {Tensor} The converted tensor.
*/
to(type: DataType): Tensor;
/**
* Returns an iterator object for iterating over the tensor data in row-major order.
* If the tensor has more than one dimension, the iterator will yield subarrays.
* @returns {Iterator} An iterator object for iterating over the tensor data in row-major order.
*/
[Symbol.iterator](): Iterator<any, any, undefined>;
}
type DataType = keyof typeof DataTypeMap;
type DataArray = AnyTypedArray | any[];
declare const DataTypeMap: Readonly<{
float32: Float32ArrayConstructor;
float16: Uint16ArrayConstructor;
float64: Float64ArrayConstructor;
string: ArrayConstructor;
int8: Int8ArrayConstructor;
uint8: Uint8ArrayConstructor;
int16: Int16ArrayConstructor;
uint16: Uint16ArrayConstructor;
int32: Int32ArrayConstructor;
uint32: Uint32ArrayConstructor;
int64: BigInt64ArrayConstructor;
uint64: BigUint64ArrayConstructor;
bool: Uint8ArrayConstructor;
}>;
declare const TokenizerModel_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* Abstract base class for tokenizer models.
*
* @extends Callable
*/
declare class TokenizerModel extends TokenizerModel_base {
/**
* Instantiates a new TokenizerModel instance based on the configuration object provided.
* @param {Object} config The configuration object for the TokenizerModel.
* @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor.
* @returns {TokenizerModel} A new instance of a TokenizerModel.
* @throws Will throw an error if the TokenizerModel type in the config is not recognized.
*/
static fromConfig(config: any, ...args: any[]): TokenizerModel;
/**
* Creates a new instance of TokenizerModel.
* @param {Object} config The configuration object for the TokenizerModel.
*/
constructor(config: any);
config: any;
/** @type {string[]} */
vocab: string[];
/**
* A mapping of tokens to ids.
* @type {Map<string, number>}
*/
tokens_to_ids: Map<string, number>;
unk_token_id: any;
unk_token: any;
end_of_word_suffix: any;
/** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */
fuse_unk: boolean;
/**
* Internal function to call the TokenizerModel instance.
* @param {string[]} tokens The tokens to encode.
* @returns {string[]} The encoded token IDs.
*/
_call(tokens: string[]): string[];
/**
* Encodes a list of tokens into a list of token IDs.
* @param {string[]} tokens The tokens to encode.
* @returns {string[]} The encoded tokens.
* @throws Will throw an error if not implemented in a subclass.
*/
encode(tokens: string[]): string[];
/**
* Converts a list of tokens into a list of token IDs.
* @param {string[]} tokens The tokens to convert.
* @returns {number[]} The converted token IDs.
*/
convert_tokens_to_ids(tokens: string[]): number[];
/**
* Converts a list of token IDs into a list of tokens.
* @param {number[]|bigint[]} ids The token IDs to convert.
* @returns {string[]} The converted tokens.
*/
convert_ids_to_tokens(ids: number[] | bigint[]): string[];
}
declare const PreTrainedTokenizer_base: new () => {
(...args: any[]): any;
_call(...args: any[]): any;
};
/**
* @typedef {Object} Message
* @property {string} role The role of the message (e.g., "user" or "assistant" or "system").
* @property {string} content The content of the message.
*/
declare class PreTrainedTokenizer extends PreTrainedTokenizer_base {
/**
* Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`.
*
* @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer.
* @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer.
*
* @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`.
* @returns {Promise<PreTrainedTokenizer>} A new instance of the `PreTrainedTokenizer` class.
*/
static from_pretrained(pretrained_model_name_or_path: string, { progress_callback, config, cache_dir, local_files_only, revision, legacy, }?: PretrainedTokenizerOptions): Promise<PreTrainedTokenizer>;
/**
* Create a new PreTrainedTokenizer instance.
* @param {Object} tokenizerJSON The JSON of the tokenizer.
* @param {Object} tokenizerConfig The config of the tokenizer.
*/
constructor(tokenizerJSON: any, tokenizerConfig: any);
return_token_type_ids: boolean;
_default_chat_template: string;
padding_side: string;
_tokenizer_config: any;
normalizer: Normalizer;
pre_tokenizer: PreTokenizer;
model: TokenizerModel;
post_processor: PostProcessor;
decoder: Decoder;
special_tokens: any[];
all_special_ids: number[];
/** @type {AddedToken[]} */
added_tokens: AddedToken[];
additional_special_tokens: any;
added_tokens_regex: RegExp;
mask_token: string;
mask_token_id: number;
pad_token: string;
pad_token_id: number;
sep_token: string;
sep_token_id: number;
unk_token: string;
unk_token_id: number;
model_max_length: any;
/** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */
remove_space: boolean;
clean_up_tokenization_spaces: any;
do_lowercase_and_remove_accent: any;
legacy: boolean;
chat_template: any;
_compiled_template_cache: Map<any, any>;
/**
* Returns the value of the first matching key in the tokenizer config object.
* @param {...string} keys One or more keys to search for in the tokenizer config object.
* @returns {string|null} The value associated with the first matching key, or null if no match is found.
* @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken".
* @private
*/
private getToken;
/**
* @typedef {number[]|number[][]|Tensor} BatchEncodingItem
*
* @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function.
* @property {BatchEncodingItem} input_ids List of token ids to be fed to a model.
* @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model.
* @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model.
*/
/**
* Encode/tokenize the given text(s).
* @param {string|string[]} text The text to tokenize.
* @param {Object} options An optional object containing the following properties:
* @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text.
* @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences.
* @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
* @param {boolean} [options.truncation=null] Whether to truncate the input sequences.
* @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length.
* @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays.
* @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids.
* @returns {BatchEncoding} Object to be passed to the model.
*/
_call(text: string | string[], { text_pair, add_special_tokens, padding, truncation, max_length, return_tensor, return_token_type_ids, }?: {
text_pair?: string | string[];
padding?: boolean | 'max_length';
add_special_tokens?: boolean;
truncation?: boolean;
max_length?: number;
return_tensor?: boolean;
return_token_type_ids?: boolean;
}): {
/**
* List of token ids to be fed to a model.
*/
input_ids: number[] | number[][] | Tensor;
/**
* List of indices specifying which tokens should be attended to by the model.
*/
attention_mask: number[] | number[][] | Tensor;
/**
* List of token type ids to be fed to a model.
*/
token_type_ids?: number[] | number[][] | Tensor;
};
/**
* Encodes a single text using the preprocessor pipeline of the tokenizer.
*
* @param {string|null} text The text to encode.
* @returns {string[]|null} The encoded tokens.
*/
_encode_text(text: string | null): string[] | null;
/**
* Encodes a single text or a pair of texts using the model's tokenizer.
*
* @param {string} text The text to encode.
* @param {Object} options An optional object containing the following properties:
* @param {string} [options.text_pair=null] The optional second text to encode.
* @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
* @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids.
* @returns {EncodingSingle} An object containing the encoded text.
* @private
*/
private _encode_plus;
/**
* Internal helper function to tokenize a text, and optionally a pair of texts.
* @param {string} text The text to tokenize.
* @param {Object} options An optional object containing the following properties:
* @param {string} [options.pair=null] The optional second text to tokenize.
* @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model.
* @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs.
*/
_tokenize_helper(text: string, { pair, add_special_tokens, }?: {
pair?: string;
add_special_tokens?: boolean;
}): {
tokens: string[];
token_type_ids?: number[];
};
/**
* Converts a string into a sequence of tokens.
* @param {string} text The sequence to be encoded.
* @param {Object} options An optional object containing the following properties:
* @param {string} [options.pair] A second sequence to be encoded with the first.
* @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model.
* @returns {string[]} The list of tokens.
*/
tokenize(text: string, { pair, add_special_tokens, }?: {
pair?: string;
add_special_tokens?: boolean;
}): string[];
/**
* Encodes a single text or a pair of texts using the model's tokenizer.
*
* @param {string} text The text to encode.
* @param {Object} options An optional object containing the following properties:
* @param {string} [options.text_pair=null] The optional second text to encode.
* @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model.
* @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids.
* @returns {number[]} An array of token IDs representing the encoded text(s).
*/
encode(text: string, { text_pair, add_special_tokens, return_token_type_ids, }?: {
text_pair?: string;
add_special_tokens?: boolean;
return_token_type_ids?: boolean;
}): number[];
/**
* Decode a batch of tokenized sequences.
* @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences.
* @param {Object} decode_args (Optional) Object with decoding arguments.
* @returns {string[]} List of decoded sequences.
*/
batch_decode(batch: number[][] | Tensor, decode_args?: any): string[];
/**
* Decodes a sequence of token IDs back to a string.
*
* @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode.
* @param {Object} [decode_args={}]
* @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string.
* @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed.
*
* @returns {string} The decoded string.
* @throws {Error} If `token_ids` is not a non-empty array of integers.
*/
decode(token_ids: number[] | bigint[] | Tensor, decode_args?: {
skip_special_tokens?: boolean;
clean_up_tokenization_spaces?: boolean;
}): string;
/**
* Decode a single list of token ids to a string.
* @param {number[]|bigint[]} token_ids List of token ids to decode
* @param {Object} decode_args Optional arguments for decoding
* @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding
* @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding.
* If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`.
* @returns {string} The decoded string
*/
decode_single(token_ids: number[] | bigint[], { skip_special_tokens, clean_up_tokenization_spaces, }: {
skip_special_tokens?: boolean;
clean_up_tokenization_spaces?: boolean;
}): string;
get default_chat_template(): string;
_warned_about_chat_template: boolean;
/**
* Converts a list of message objects with `"role"` and `"content"` keys to a list of token
* ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to
* determine the format and control tokens to use when converting. When chat_template is None, it will fall back
* to the default_chat_template specified at the class level.
*
* See [here](https://huggingface.co/docs/transformers/chat_templating) for more information.
*
* **Example:** Applying a chat template to a conversation.
*
* ```javascript
* import { AutoTokenizer } from "@xenova/transformers";
*
* const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1");
*
* const chat = [
* { "role": "user", "content": "Hello, how are you?" },
* { "role": "assistant", "content": "I'm doing great. How can I help you today?" },
* { "role": "user", "content": "I'd like to show off how chat templating works!" },
* ]
*
* const text = tokenizer.apply_chat_template(chat, { tokenize: false });
* // "<s>[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today?</s> [INST] I'd like to show off how chat templating works! [/INST]"
*
* const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false });
* // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 1