/**
|
* @license
|
* Copyright 2017 Google LLC. All Rights Reserved.
|
* 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.
|
* =============================================================================
|
*/
|
/// <amd-module name="@tensorflow/tfjs-core/dist/tensor" />
|
/// <reference types="@webgpu/types/dist" />
|
import { DataId, TensorInfo } from './tensor_info';
|
import { ArrayMap, BackendValues, DataType, DataTypeMap, DataValues, NumericDataType, Rank, ShapeMap, SingleValueMap } from './types';
|
export interface TensorData<D extends DataType> {
|
dataId?: DataId;
|
values?: DataTypeMap[D];
|
}
|
export interface Backend {
|
}
|
/**
|
* A mutable object, similar to `tf.Tensor`, that allows users to set values
|
* at locations before converting to an immutable `tf.Tensor`.
|
*
|
* See `tf.buffer` for creating a tensor buffer.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
export declare class TensorBuffer<R extends Rank, D extends DataType = 'float32'> {
|
dtype: D;
|
size: number;
|
shape: ShapeMap[R];
|
strides: number[];
|
values: DataTypeMap[D];
|
constructor(shape: ShapeMap[R], dtype: D, values?: DataTypeMap[D]);
|
/**
|
* Sets a value in the buffer at a given location.
|
*
|
* @param value The value to set.
|
* @param locs The location indices.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
*/
|
set(value: SingleValueMap[D], ...locs: number[]): void;
|
/**
|
* Returns the value in the buffer at the provided location.
|
*
|
* @param locs The location indices.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
*/
|
get(...locs: number[]): SingleValueMap[D];
|
locToIndex(locs: number[]): number;
|
indexToLoc(index: number): number[];
|
get rank(): number;
|
/**
|
* Creates an immutable `tf.Tensor` object from the buffer.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Creation'}
|
*/
|
toTensor(): Tensor<R>;
|
}
|
export interface DataToGPUWebGLOption {
|
customTexShape?: [number, number];
|
}
|
export type DataToGPUOptions = DataToGPUWebGLOption;
|
export interface GPUData {
|
tensorRef: Tensor;
|
texture?: WebGLTexture;
|
buffer?: GPUBuffer;
|
texShape?: [number, number];
|
}
|
export interface TensorTracker {
|
makeTensor(values: DataValues, shape: number[], dtype: DataType, backend?: Backend): Tensor;
|
makeVariable(initialValue: Tensor, trainable?: boolean, name?: string, dtype?: DataType): Variable;
|
incRef(a: Tensor, backend: Backend): void;
|
disposeTensor(t: Tensor): void;
|
disposeVariable(v: Variable): void;
|
read(dataId: DataId): Promise<BackendValues>;
|
readSync(dataId: DataId): BackendValues;
|
readToGPU(dataId: DataId, options?: DataToGPUOptions): GPUData;
|
}
|
/**
|
* The Tensor class calls into this handler to delegate chaining operations.
|
*/
|
export interface OpHandler {
|
cast<T extends Tensor>(x: T, dtype: DataType): T;
|
buffer<R extends Rank, D extends DataType>(shape: ShapeMap[R], dtype: D, values?: DataTypeMap[D]): TensorBuffer<R, D>;
|
print<T extends Tensor>(x: T, verbose: boolean): void;
|
clone<T extends Tensor>(x: T): T;
|
}
|
/**
|
* An external consumer can register itself as the tensor tracker. This way
|
* the Tensor class can notify the tracker for every tensor created and
|
* disposed.
|
*/
|
export declare function setTensorTracker(fn: () => TensorTracker): void;
|
/**
|
* An external consumer can register itself as the op handler. This way the
|
* Tensor class can have chaining methods that call into ops via the op
|
* handler.
|
*/
|
export declare function setOpHandler(handler: OpHandler): void;
|
/**
|
* Sets the deprecation warning function to be used by this file. This way the
|
* Tensor class can be a leaf but still use the environment.
|
*/
|
export declare function setDeprecationWarningFn(fn: (msg: string) => void): void;
|
export declare namespace Tensor { }
|
/**
|
* A `tf.Tensor` object represents an immutable, multidimensional array of
|
* numbers that has a shape and a data type.
|
*
|
* For performance reasons, functions that create tensors do not necessarily
|
* perform a copy of the data passed to them (e.g. if the data is passed as a
|
* `Float32Array`), and changes to the data will change the tensor. This is not
|
* a feature and is not supported. To avoid this behavior, use the tensor before
|
* changing the input data or create a copy with `copy = tf.add(yourTensor, 0)`.
|
*
|
* See `tf.tensor` for details on how to create a `tf.Tensor`.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
export declare class Tensor<R extends Rank = Rank> implements TensorInfo {
|
/** Unique id of this tensor. */
|
readonly id: number;
|
/**
|
* Id of the bucket holding the data for this tensor. Multiple arrays can
|
* point to the same bucket (e.g. when calling array.reshape()).
|
*/
|
dataId: DataId;
|
/** The shape of the tensor. */
|
readonly shape: ShapeMap[R];
|
/** Number of elements in the tensor. */
|
readonly size: number;
|
/** The data type for the array. */
|
readonly dtype: DataType;
|
/** The rank type for the array (see `Rank` enum). */
|
readonly rankType: R;
|
/** Whether this tensor has been globally kept. */
|
kept: boolean;
|
/** The id of the scope this tensor is being tracked in. */
|
scopeId: number;
|
/** The keras mask that some keras layers attach to the tensor */
|
kerasMask?: Tensor;
|
/**
|
* Number of elements to skip in each dimension when indexing. See
|
* https://docs.scipy.org/doc/numpy/reference/generated/\
|
* numpy.ndarray.strides.html
|
*/
|
readonly strides: number[];
|
constructor(shape: ShapeMap[R], dtype: DataType, dataId: DataId, id: number);
|
get rank(): number;
|
/**
|
* Returns a promise of `tf.TensorBuffer` that holds the underlying data.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
buffer<D extends DataType = 'float32'>(): Promise<TensorBuffer<R, D>>;
|
/**
|
* Returns a `tf.TensorBuffer` that holds the underlying data.
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
bufferSync<D extends DataType = 'float32'>(): TensorBuffer<R, D>;
|
/**
|
* Returns the tensor data as a nested array. The transfer of data is done
|
* asynchronously.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
array(): Promise<ArrayMap[R]>;
|
/**
|
* Returns the tensor data as a nested array. The transfer of data is done
|
* synchronously.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
arraySync(): ArrayMap[R];
|
/**
|
* Asynchronously downloads the values from the `tf.Tensor`. Returns a
|
* promise of `TypedArray` that resolves when the computation has finished.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
data<D extends DataType = NumericDataType>(): Promise<DataTypeMap[D]>;
|
/**
|
* Copy the tensor's data to a new GPU resource. Comparing to the `dataSync()`
|
* and `data()`, this method prevents data from being downloaded to CPU.
|
*
|
* For WebGL backend, the data will be stored on a densely packed texture.
|
* This means that the texture will use the RGBA channels to store value.
|
*
|
* For WebGPU backend, the data will be stored on a buffer. There is no
|
* parameter, so can not use a user-defined size to create the buffer.
|
*
|
* @param options:
|
* For WebGL,
|
* - customTexShape: Optional. If set, will use the user defined
|
* texture shape to create the texture.
|
*
|
* @returns For WebGL backend, a GPUData contains the new texture and
|
* its information.
|
* {
|
* tensorRef: The tensor that is associated with this texture,
|
* texture: WebGLTexture,
|
* texShape: [number, number] // [height, width]
|
* }
|
*
|
* For WebGPU backend, a GPUData contains the new buffer.
|
* {
|
* tensorRef: The tensor that is associated with this buffer,
|
* buffer: GPUBuffer,
|
* }
|
*
|
* Remember to dispose the GPUData after it is used by
|
* `res.tensorRef.dispose()`.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
dataToGPU(options?: DataToGPUOptions): GPUData;
|
/**
|
* Synchronously downloads the values from the `tf.Tensor`. This blocks the
|
* UI thread until the values are ready, which can cause performance issues.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
dataSync<D extends DataType = NumericDataType>(): DataTypeMap[D];
|
/** Returns the underlying bytes of the tensor's data. */
|
bytes(): Promise<Uint8Array[] | Uint8Array>;
|
/**
|
* Disposes `tf.Tensor` from memory.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
dispose(): void;
|
protected isDisposedInternal: boolean;
|
get isDisposed(): boolean;
|
throwIfDisposed(): void;
|
/**
|
* Prints the `tf.Tensor`. See `tf.print` for details.
|
*
|
* @param verbose Whether to print verbose information about the tensor,
|
* including dtype and size.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
print(verbose?: boolean): void;
|
/**
|
* Returns a copy of the tensor. See `tf.clone` for details.
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
clone<T extends Tensor>(this: T): T;
|
/**
|
* Returns a human-readable description of the tensor. Useful for logging.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
toString(verbose?: boolean): string;
|
variable(trainable?: boolean, name?: string, dtype?: DataType): Variable<R>;
|
}
|
export declare function getGlobalTensorClass(): typeof Tensor;
|
export interface NumericTensor<R extends Rank = Rank> extends Tensor<R> {
|
dtype: NumericDataType;
|
dataSync<D extends DataType = NumericDataType>(): DataTypeMap[D];
|
data<D extends DataType = NumericDataType>(): Promise<DataTypeMap[D]>;
|
dataToGPU(options?: DataToGPUOptions): GPUData;
|
}
|
export interface StringTensor<R extends Rank = Rank> extends Tensor<R> {
|
dtype: 'string';
|
dataSync<D extends DataType = 'string'>(): DataTypeMap[D];
|
data<D extends DataType = 'string'>(): Promise<DataTypeMap[D]>;
|
}
|
/** @doclink Tensor */
|
export type Scalar = Tensor<Rank.R0>;
|
/** @doclink Tensor */
|
export type Tensor1D = Tensor<Rank.R1>;
|
/** @doclink Tensor */
|
export type Tensor2D = Tensor<Rank.R2>;
|
/** @doclink Tensor */
|
export type Tensor3D = Tensor<Rank.R3>;
|
/** @doclink Tensor */
|
export type Tensor4D = Tensor<Rank.R4>;
|
/** @doclink Tensor */
|
export type Tensor5D = Tensor<Rank.R5>;
|
/** @doclink Tensor */
|
export type Tensor6D = Tensor<Rank.R6>;
|
/**
|
* A mutable `tf.Tensor`, useful for persisting state, e.g. for training.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
export declare class Variable<R extends Rank = Rank> extends Tensor<R> {
|
trainable: boolean;
|
name: string;
|
constructor(initialValue: Tensor<R>, trainable: boolean, name: string, tensorId: number);
|
/**
|
* Assign a new `tf.Tensor` to this variable. The new `tf.Tensor` must have
|
* the same shape and dtype as the old `tf.Tensor`.
|
*
|
* @param newValue New tensor to be assigned to this variable.
|
*
|
* @doc {heading: 'Tensors', subheading: 'Classes'}
|
*/
|
assign(newValue: Tensor<R>): void;
|
dispose(): void;
|
}
|