Skip to content

Commit 09cbb0c

Browse files
kungfoomanxenova
andauthored
Fix more _call LSP errors + extra typings (#304)
* types are only inferred through this assignments in constructor * Typing PreprocessedImage * LSP + other typings for ImageFeatureExtractor / SamImageProcessor / DetrFeatureExtractor * Fix SamProcessor error * Fix PretrainedOptions * Fix double AnyTypedArray * Update `unused` variable name * Mark `_update` image function as private * Update processor JSDoc --------- Co-authored-by: Joshua Lochner <[email protected]>
1 parent df94965 commit 09cbb0c

File tree

8 files changed

+88
-54
lines changed

8 files changed

+88
-54
lines changed

src/models.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ import { executionProviders, ONNX } from './backends/onnx.js';
8383
import { medianFilter } from './transformers.js';
8484
const { InferenceSession, Tensor: ONNXTensor } = ONNX;
8585

86-
/**
87-
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
88-
*/
89-
90-
9186
//////////////////////////////////////////////////
9287
// Model types: used internally
9388
const MODEL_TYPES = {
@@ -113,7 +108,7 @@ const MODEL_CLASS_TO_NAME_MAPPING = new Map();
113108
* Constructs an InferenceSession using a model file located at the specified path.
114109
* @param {string} pretrained_model_name_or_path The path to the directory containing the model file.
115110
* @param {string} fileName The name of the model file.
116-
* @param {PretrainedOptions} options Additional options for loading the model.
111+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the model.
117112
* @returns {Promise<InferenceSession>} A Promise that resolves to an InferenceSession object.
118113
* @private
119114
*/
@@ -664,7 +659,7 @@ export class PreTrainedModel extends Callable {
664659
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
665660
* user or organization name, like `dbmdz/bert-base-german-cased`.
666661
* - A path to a *directory* containing model weights, e.g., `./my_model_directory/`.
667-
* @param {PretrainedOptions} options Additional options for loading the model.
662+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the model.
668663
*
669664
* @returns {Promise<PreTrainedModel>} A new instance of the `PreTrainedModel` class.
670665
*/

src/pipelines.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,10 +2063,6 @@ const TASK_ALIASES = {
20632063
"embeddings": "feature-extraction",
20642064
}
20652065

2066-
/**
2067-
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
2068-
*/
2069-
20702066
/**
20712067
* Utility factory method to build a [`Pipeline`] object.
20722068
*
@@ -2091,7 +2087,7 @@ const TASK_ALIASES = {
20912087
* - `"zero-shot-classification"`: will return a `ZeroShotClassificationPipeline`.
20922088
* - `"zero-shot-image-classification"`: will return a `ZeroShotImageClassificationPipeline`.
20932089
* @param {string} [model=null] The name of the pre-trained model to use. If not specified, the default model for the task will be used.
2094-
* @param {PretrainedOptions} [options] Optional parameters for the pipeline.
2090+
* @param {import('./utils/hub.js').PretrainedOptions} [options] Optional parameters for the pipeline.
20952091
* @returns {Promise<Pipeline>} A Pipeline object for the specified task.
20962092
* @throws {Error} If an unsupported pipeline is requested.
20972093
*/
@@ -2158,7 +2154,7 @@ export async function pipeline(
21582154
* Helper function to get applicable model, tokenizer, or processor classes for a given model.
21592155
* @param {Map<string, any>} mapping The mapping of names to classes, arrays of classes, or null.
21602156
* @param {string} model The name of the model to load.
2161-
* @param {PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method.
2157+
* @param {import('./utils/hub.js').PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method.
21622158
* @private
21632159
*/
21642160
async function loadItems(mapping, model, pretrainedOptions) {

src/processors.js

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ function post_process_object_detection(outputs, threshold = 0.5, target_sizes =
120120
return toReturn;
121121
}
122122

123+
/**
124+
* Named tuple to indicate the order we are using is (height x width), even though
125+
* the Graphics’ industry standard is (width x height).
126+
* @typedef {[height: number, width: number]} HeightWidth
127+
*/
128+
123129
/**
124130
* Base class for feature extractors.
125131
*
@@ -137,6 +143,13 @@ export class FeatureExtractor extends Callable {
137143
}
138144
}
139145

146+
/**
147+
* @typedef {object} ImageFeatureExtractorResult
148+
* @property {Tensor} pixel_values The pixel values of the batched preprocessed images.
149+
* @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]].
150+
* @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]].
151+
*/
152+
140153
/**
141154
* Feature extractor for image models.
142155
*
@@ -216,11 +229,18 @@ export class ImageFeatureExtractor extends FeatureExtractor {
216229
return await image.resize(width, height, { resample });
217230
}
218231

232+
/**
233+
* @typedef {object} PreprocessedImage
234+
* @property {HeightWidth} original_size The original size of the image.
235+
* @property {HeightWidth} reshaped_input_size The reshaped input size of the image.
236+
* @property {Tensor} pixel_values The pixel values of the preprocessed image.
237+
*/
238+
219239
/**
220240
* Preprocesses the given image.
221241
*
222242
* @param {RawImage} image The image to preprocess.
223-
* @returns {Promise<any>} The preprocessed image as a Tensor.
243+
* @returns {Promise<PreprocessedImage>} The preprocessed image.
224244
*/
225245
async preprocess(image) {
226246

@@ -316,6 +336,7 @@ export class ImageFeatureExtractor extends FeatureExtractor {
316336
image = await image.center_crop(crop_width, crop_height);
317337
}
318338

339+
/** @type {HeightWidth} */
319340
let reshaped_input_size = [image.height, image.width];
320341

321342
// TODO is it okay to pad before rescaling/normalizing?
@@ -374,22 +395,23 @@ export class ImageFeatureExtractor extends FeatureExtractor {
374395
* Calls the feature extraction process on an array of image
375396
* URLs, preprocesses each image, and concatenates the resulting
376397
* features into a single Tensor.
377-
* @param {any} images The URL(s) of the image(s) to extract features from.
378-
* @returns {Promise<Object>} An object containing the concatenated pixel values (and other metadata) of the preprocessed images.
398+
* @param {any[]} images The URL(s) of the image(s) to extract features from.
399+
* @param {...any} args Additional arguments.
400+
* @returns {Promise<ImageFeatureExtractorResult>} An object containing the concatenated pixel values (and other metadata) of the preprocessed images.
379401
*/
380-
async _call(images) {
402+
async _call(images, ...args) {
381403
if (!Array.isArray(images)) {
382404
images = [images];
383405
}
384-
385-
let imageData = await Promise.all(images.map(x => this.preprocess(x)));
406+
/** @type {PreprocessedImage[]} */
407+
const imageData = await Promise.all(images.map(x => this.preprocess(x)));
386408

387409
// TODO:
388410

389411
// Concatenate pixel values
390412
// TEMP: Add batch dimension so that concat works
391413
imageData.forEach(x => x.pixel_values.dims = [1, ...x.pixel_values.dims]);
392-
let pixel_values = cat(imageData.map(x => x.pixel_values));
414+
const pixel_values = cat(imageData.map(x => x.pixel_values));
393415

394416
return {
395417
pixel_values: pixel_values,
@@ -411,6 +433,12 @@ export class DeiTFeatureExtractor extends ImageFeatureExtractor { }
411433
export class BeitFeatureExtractor extends ImageFeatureExtractor { }
412434
export class DonutFeatureExtractor extends ImageFeatureExtractor { }
413435

436+
/**
437+
* @typedef {object} DetrFeatureExtractorResultProps
438+
* @property {Tensor} pixel_mask
439+
* @typedef {ImageFeatureExtractorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult
440+
*/
441+
414442
/**
415443
* Detr Feature Extractor.
416444
*
@@ -420,24 +448,23 @@ export class DetrFeatureExtractor extends ImageFeatureExtractor {
420448
/**
421449
* Calls the feature extraction process on an array of image URLs, preprocesses
422450
* each image, and concatenates the resulting features into a single Tensor.
423-
* @param {any} urls The URL(s) of the image(s) to extract features from.
424-
* @returns {Promise<Object>} An object containing the concatenated pixel values of the preprocessed images.
451+
* @param {any[]} urls The URL(s) of the image(s) to extract features from.
452+
* @returns {Promise<DetrFeatureExtractorResult>} An object containing the concatenated pixel values of the preprocessed images.
425453
*/
426454
async _call(urls) {
427-
let result = await super._call(urls);
455+
const result = await super._call(urls);
428456

429457
// TODO support differently-sized images, for now assume all images are the same size.
430458
// TODO support different mask sizes (not just 64x64)
431459
// Currently, just fill pixel mask with 1s
432-
let maskSize = [result.pixel_values.dims[0], 64, 64];
433-
result.pixel_mask = new Tensor(
460+
const maskSize = [result.pixel_values.dims[0], 64, 64];
461+
const pixel_mask = new Tensor(
434462
'int64',
435-
// TODO: fix error below
436463
new BigInt64Array(maskSize.reduce((a, b) => a * b)).fill(1n),
437464
maskSize
438465
);
439466

440-
return result;
467+
return { ...result, pixel_mask };
441468
}
442469

443470
/**
@@ -737,7 +764,22 @@ export class YolosFeatureExtractor extends ImageFeatureExtractor {
737764
}
738765
}
739766

767+
/**
768+
* @typedef {object} SamImageProcessorResult
769+
* @property {Tensor} pixel_values
770+
* @property {HeightWidth[]} original_sizes
771+
* @property {HeightWidth[]} reshaped_input_sizes
772+
* @property {Tensor} input_points
773+
*/
774+
740775
export class SamImageProcessor extends ImageFeatureExtractor {
776+
/**
777+
* @param {any[]} images The URL(s) of the image(s) to extract features from.
778+
* @param {*} input_points A 3D or 4D array, representing the input points provided by the user.
779+
* - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1.
780+
* - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`.
781+
* @returns {Promise<SamImageProcessorResult>}
782+
*/
741783
async _call(images, input_points) {
742784
let {
743785
pixel_values,
@@ -747,6 +789,7 @@ export class SamImageProcessor extends ImageFeatureExtractor {
747789

748790
let shape = calculateDimensions(input_points);
749791

792+
// TODO: add support for 2D input_points
750793
if (shape.length === 3) {
751794
// Correct user's input
752795
shape = [1, ...shape];
@@ -1284,15 +1327,20 @@ export class Processor extends Callable {
12841327
/**
12851328
* Calls the feature_extractor function with the given input.
12861329
* @param {any} input The input to extract features from.
1330+
* @param {...any} args Additional arguments.
12871331
* @returns {Promise<any>} A Promise that resolves with the extracted features.
12881332
*/
1289-
async _call(input) {
1333+
async _call(input, ...args) {
12901334
return await this.feature_extractor(input);
12911335
}
12921336
}
12931337

12941338
export class SamProcessor extends Processor {
1295-
1339+
/**
1340+
* @param {*} images
1341+
* @param {*} input_points
1342+
* @returns {Promise<any>}
1343+
*/
12961344
async _call(images, input_points) {
12971345
return await this.feature_extractor(images, input_points);
12981346
}
@@ -1334,9 +1382,6 @@ export class Wav2Vec2ProcessorWithLM extends Processor {
13341382
}
13351383

13361384
//////////////////////////////////////////////////
1337-
/**
1338-
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
1339-
*/
13401385
/**
13411386
* Helper class which is used to instantiate pretrained processors with the `from_pretrained` function.
13421387
* The chosen processor class is determined by the type specified in the processor config.
@@ -1400,7 +1445,7 @@ export class AutoProcessor {
14001445
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
14011446
* user or organization name, like `dbmdz/bert-base-german-cased`.
14021447
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
1403-
* @param {PretrainedOptions} options Additional options for loading the processor.
1448+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
14041449
*
14051450
* @returns {Promise<Processor>} A new instance of the Processor class.
14061451
*/

src/tokenizers.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ import {
4141
CharTrie,
4242
} from './utils/data-structures.js';
4343

44-
/**
45-
* @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions
46-
*/
47-
4844
/**
4945
* Loads a tokenizer from the specified path.
5046
* @param {string} pretrained_model_name_or_path The path to the tokenizer directory.
51-
* @param {PretrainedOptions} options Additional options for loading the tokenizer.
52-
* @returns {Promise<Array>} A promise that resolves with information about the loaded tokenizer.
47+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the tokenizer.
48+
* @returns {Promise<any[]>} A promise that resolves with information about the loaded tokenizer.
5349
*/
5450
async function loadTokenizer(pretrained_model_name_or_path, options) {
5551

@@ -2228,7 +2224,7 @@ export class PreTrainedTokenizer extends Callable {
22282224
* Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`.
22292225
*
22302226
* @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer.
2231-
* @param {PretrainedOptions} options Additional options for loading the tokenizer.
2227+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the tokenizer.
22322228
*
22332229
* @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`.
22342230
* @returns {Promise<PreTrainedTokenizer>} A new instance of the `PreTrainedTokenizer` class.
@@ -3814,7 +3810,7 @@ export class AutoTokenizer {
38143810
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
38153811
* user or organization name, like `dbmdz/bert-base-german-cased`.
38163812
* - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`.
3817-
* @param {PretrainedOptions} options Additional options for loading the tokenizer.
3813+
* @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the tokenizer.
38183814
*
38193815
* @returns {Promise<PreTrainedTokenizer>} A new instance of the PreTrainedTokenizer class.
38203816
*/

src/transformers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-nocheck
2-
31
/**
42
* @file Entry point for the Transformers.js library. Only the exports from this file
53
* are available to the end user, and are grouped as follows:

src/utils/hub.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ if (!globalThis.ReadableStream) {
1919

2020
/**
2121
* @typedef {Object} PretrainedOptions Options for loading a pretrained model.
22-
* @property {boolean?} [options.quantized=true] Whether to load the 8-bit quantized version of the model (only applicable when loading model files).
23-
* @property {function} [options.progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
24-
* @property {Object} [options.config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
22+
* @property {boolean?} [quantized=true] Whether to load the 8-bit quantized version of the model (only applicable when loading model files).
23+
* @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates.
24+
* @property {Object} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when:
2525
* - The model is a model provided by the library (loaded with the *model id* string of a pretrained model).
2626
* - 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.
27-
* @property {string} [options.cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
28-
* @property {boolean} [options.local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model).
29-
* @property {string} [options.revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id,
27+
* @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used.
28+
* @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model).
29+
* @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id,
3030
* 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.
3131
* NOTE: This setting is ignored for local requests.
32-
* @property {string} [options.model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
32+
* @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models.
3333
*/
3434

3535
class FileResponse {

src/utils/image.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export class RawImage {
8484
* @param {1|2|3|4} channels The number of channels.
8585
*/
8686
constructor(data, width, height, channels) {
87-
this._update(data, width, height, channels);
87+
this.data = data;
88+
this.width = width;
89+
this.height = height;
90+
this.channels = channels;
8891
}
8992

9093
/**
@@ -508,7 +511,8 @@ export class RawImage {
508511
* @param {Uint8ClampedArray} data The new image data.
509512
* @param {number} width The new width of the image.
510513
* @param {number} height The new height of the image.
511-
* @param {1|2|3|4} channels The new number of channels of the image.
514+
* @param {1|2|3|4|null} [channels] The new number of channels of the image.
515+
* @private
512516
*/
513517
_update(data, width, height, channels = null) {
514518
this.data = data;

src/utils/tensor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616

1717

1818
/**
19-
* @typedef {import('./maths.js').AnyTypedArray} AnyTypedArray
19+
* @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray
2020
*/
2121

2222
/** @type {Object} */
@@ -25,7 +25,7 @@ const ONNXTensor = ONNX.Tensor;
2525
export class Tensor extends ONNXTensor {
2626
/**
2727
* Create a new Tensor or copy an existing Tensor.
28-
* @param {[string, Array|AnyTypedArray, number[]]|[ONNXTensor]} args
28+
* @param {[string, DataArray, number[]]|[ONNXTensor]} args
2929
*/
3030
constructor(...args) {
3131
if (args[0] instanceof ONNX.Tensor) {

0 commit comments

Comments
 (0)