Skip to content

Commit

Permalink
Minimal fixes for type declarations to resolve errors when importing …
Browse files Browse the repository at this point in the history
…transformers.js via typescript (partially resolves #1093) (#1122)

* Explicitly declare `batch_decode` as a function with polymorphic args/returns

Resolves typescript inference errors in `models/mgp_str/processing_mgp_str.js` where it does extend from `Processor` but the override returns an object instead of a string array.

* Directly refer to `PretrainedProcessorOptions` in typedec for `AutoProcessor.from_pretrained`

Resolves an issue with tsc where using `@type {typeof Processor.from_pretrained}` does not actually import the `PretrainedProcessorOptions` type alias defined in `processing_utils.js`.

* Explicitly define third argument for Phi3VProcessor._call

Resolves a typescript error where typescript tries to destructure an array directly into padding/truncation/num_crops. Also improves the type safety of calling this function.

* Fix decode type

* Re-use `from_pretrained` types where possible

---------

Co-authored-by: Joshua Lochner <[email protected]>
  • Loading branch information
shrirajh and xenova authored Dec 28, 2024
1 parent c845bb5 commit d874c90
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 45 deletions.
18 changes: 9 additions & 9 deletions src/base/feature_extraction_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ export class FeatureExtractor extends Callable {
}

/**
* Instantiate one of the processor classes of the library from a pretrained model.
* Instantiate one of the feature extractor classes of the library from a pretrained model.
*
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
* The feature extractor class to instantiate is selected based on the `feature_extractor_type` property of
* the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
* - A string, the *model id* of a pretrained feature_extractor hosted inside a model repo on huggingface.co.
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
* user or organization name, like `dbmdz/bert-base-german-cased`.
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
* @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
* - A path to a *directory* containing feature_extractor files, e.g., `./my_model_directory/`.
* @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the feature_extractor.
*
* @returns {Promise<FeatureExtractor>} A new instance of the Processor class.
* @returns {Promise<FeatureExtractor>} A new instance of the Feature Extractor class.
*/
static async from_pretrained(pretrained_model_name_or_path, options) {
const preprocessorConfig = await getModelJSON(pretrained_model_name_or_path, FEATURE_EXTRACTOR_NAME, true, options);
return new this(preprocessorConfig);
const config = await getModelJSON(pretrained_model_name_or_path, FEATURE_EXTRACTOR_NAME, true, options);
return new this(config);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/base/processing_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ export class Processor extends Callable {
/**
* Instantiate one of the processor classes of the library from a pretrained model.
*
* The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object
* (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
Expand Down
16 changes: 0 additions & 16 deletions src/models/auto/feature_extraction_auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ import * as AllFeatureExtractors from '../feature_extractors.js';

export class AutoFeatureExtractor {

/**
* Instantiate one of the feature extractor classes of the library from a pretrained model.
*
* The processor class to instantiate is selected based on the `feature_extractor_type` property of
* the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
* user or organization name, like `dbmdz/bert-base-german-cased`.
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
* @param {import('../../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
*
* @returns {Promise<AllFeatureExtractors.ImageProcessor>} A new instance of the Processor class.
*/

/** @type {typeof FeatureExtractor.from_pretrained} */
static async from_pretrained(pretrained_model_name_or_path, options={}) {

Expand Down
16 changes: 0 additions & 16 deletions src/models/auto/processing_auto.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ import * as AllFeatureExtractors from '../feature_extractors.js';
*/
export class AutoProcessor {

/**
* Instantiate one of the processor classes of the library from a pretrained model.
*
* The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy)
* property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible)
*
* @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either:
* - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co.
* Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a
* user or organization name, like `dbmdz/bert-base-german-cased`.
* - A path to a *directory* containing processor files, e.g., `./my_model_directory/`.
* @param {import('../../utils/hub.js').PretrainedOptions} options Additional options for loading the processor.
*
* @returns {Promise<Processor>} A new instance of the Processor class.
*/

/** @type {typeof Processor.from_pretrained} */
static async from_pretrained(pretrained_model_name_or_path, options={}) {

Expand Down
2 changes: 1 addition & 1 deletion src/models/phi3_v/processing_phi3_v.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Phi3VProcessor extends Processor {
*
* @param {string|string[]} text
* @param {RawImage|RawImage[]} images
* @param {...any} args
* @param { { padding?: boolean, truncation?: boolean, num_crops?: number } | undefined } options
* @returns {Promise<any>}
*/
async _call(text, images = null, {
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,7 @@ export class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options
const max_new_tokens = Math.floor(aud.length / sampling_rate) * 6;
const outputs = await this.model.generate({ max_new_tokens, ...kwargs, ...inputs });

const text = this.processor.batch_decode(outputs, { skip_special_tokens: true })[0];
const text = this.processor.batch_decode(/** @type {Tensor} */(outputs), { skip_special_tokens: true })[0];
toReturn.push({ text });
}
return single ? toReturn[0] : toReturn;
Expand Down

0 comments on commit d874c90

Please sign in to comment.