Skip to content

Commit c845bb5

Browse files
authored
Check TypeScript errors during build (huggingface#1081)
* migrate jsconfig.json to tsconfig.json * add @ts-expect-error * add type assertion * fix types for navigator.gpu * fix types for Processor * add @ts-expect-error * ignore error in batch_decode * Add types for MultiModalityCausalLM
1 parent 8e075f4 commit c845bb5

16 files changed

+84
-5
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"format": "prettier --write .",
2626
"format:check": "prettier --check .",
27-
"typegen": "tsc ./src/transformers.js --allowJs --declaration --emitDeclarationOnly --declarationMap --outDir types",
27+
"typegen": "tsc --build",
2828
"dev": "webpack serve --no-client-overlay",
2929
"build": "webpack && npm run typegen",
3030
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --verbose",

src/base/image_processors_utils.js

+11
Original file line numberDiff line numberDiff line change
@@ -604,14 +604,20 @@ export class ImageProcessor extends Callable {
604604
this.do_thumbnail = config.do_thumbnail;
605605
this.size = config.size ?? config.image_size;
606606
this.do_resize = config.do_resize ?? (this.size !== undefined);
607+
// @ts-expect-error TS2339
607608
this.size_divisibility = config.size_divisibility ?? config.size_divisor;
608609

609610
this.do_center_crop = config.do_center_crop;
611+
// @ts-expect-error TS2339
610612
this.crop_size = config.crop_size;
613+
// @ts-expect-error TS2339
611614
this.do_convert_rgb = config.do_convert_rgb ?? true;
615+
// @ts-expect-error TS2339
612616
this.do_crop_margin = config.do_crop_margin;
613617

618+
// @ts-expect-error TS2339
614619
this.pad_size = config.pad_size;
620+
// @ts-expect-error TS2339
615621
this.do_pad = config.do_pad;
616622

617623
if (this.do_pad && !this.pad_size && this.size && this.size.width !== undefined && this.size.height !== undefined) {
@@ -820,6 +826,7 @@ export class ImageProcessor extends Callable {
820826
// Support both formats for backwards compatibility
821827
else if (Number.isInteger(size)) {
822828
shortest_edge = size;
829+
// @ts-expect-error TS2339
823830
longest_edge = this.config.max_size ?? shortest_edge;
824831

825832
} else if (size !== undefined) {
@@ -888,6 +895,7 @@ export class ImageProcessor extends Callable {
888895
} else if (size.min_pixels !== undefined && size.max_pixels !== undefined) {
889896
// Custom resize logic for Qwen2-VL models
890897
const { min_pixels, max_pixels } = size;
898+
// @ts-expect-error TS2339
891899
const factor = this.config.patch_size * this.config.merge_size;
892900
return smart_resize(srcHeight, srcWidth, factor, min_pixels, max_pixels);
893901
} else {
@@ -903,6 +911,7 @@ export class ImageProcessor extends Callable {
903911
async resize(image) {
904912
const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size);
905913
return await image.resize(newWidth, newHeight, {
914+
// @ts-expect-error TS2322
906915
resample: this.resample,
907916
});
908917
}
@@ -953,6 +962,7 @@ export class ImageProcessor extends Callable {
953962

954963
// Resize the image using thumbnail method.
955964
if (this.do_thumbnail) {
965+
// @ts-expect-error TS2345
956966
image = await this.thumbnail(image, this.size, this.resample);
957967
}
958968

@@ -977,6 +987,7 @@ export class ImageProcessor extends Callable {
977987
// NOTE: All pixel-level manipulation (i.e., modifying `pixelData`)
978988
// occurs with data in the hwc format (height, width, channels),
979989
// to emulate the behavior of the original Python code (w/ numpy).
990+
/** @type {Float32Array} */
980991
let pixelData = Float32Array.from(image.data);
981992
let imgDims = [image.height, image.width, image.channels];
982993

src/base/processing_utils.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { getModelJSON } from '../utils/hub.js';
2828
/**
2929
* @typedef {Object} ProcessorProperties Additional processor-specific properties.
3030
* @typedef {import('../utils/hub.js').PretrainedOptions & ProcessorProperties} PretrainedProcessorOptions
31+
* @typedef {import('../tokenizers.js').PreTrainedTokenizer} PreTrainedTokenizer
3132
*/
3233

3334

@@ -61,7 +62,7 @@ export class Processor extends Callable {
6162
}
6263

6364
/**
64-
* @returns {import('../tokenizers.js').PreTrainedTokenizer|undefined} The tokenizer of the processor, if it exists.
65+
* @returns {PreTrainedTokenizer|undefined} The tokenizer of the processor, if it exists.
6566
*/
6667
get tokenizer() {
6768
return this.components.tokenizer;
@@ -74,6 +75,11 @@ export class Processor extends Callable {
7475
return this.components.feature_extractor;
7576
}
7677

78+
/**
79+
* @param {Parameters<PreTrainedTokenizer['apply_chat_template']>[0]} messages
80+
* @param {Parameters<PreTrainedTokenizer['apply_chat_template']>[1]} options
81+
* @returns {ReturnType<PreTrainedTokenizer['apply_chat_template']>}
82+
*/
7783
apply_chat_template(messages, options = {}) {
7884
if (!this.tokenizer) {
7985
throw new Error('Unable to apply chat template without a tokenizer.');
@@ -84,6 +90,10 @@ export class Processor extends Callable {
8490
});
8591
}
8692

93+
/**
94+
* @param {Parameters<PreTrainedTokenizer['batch_decode']>} args
95+
* @returns {ReturnType<PreTrainedTokenizer['batch_decode']>}
96+
*/
8797
batch_decode(...args) {
8898
if (!this.tokenizer) {
8999
throw new Error('Unable to decode without a tokenizer.');

src/configs.js

+5
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,19 @@ function getNormalizedConfig(config) {
7070
case 'florence2':
7171
case 'llava_onevision':
7272
case 'idefics3':
73+
// @ts-expect-error TS2339
7374
init_normalized_config = getNormalizedConfig(config.text_config);
7475
break;
7576
case 'moondream1':
77+
// @ts-expect-error TS2339
7678
init_normalized_config = getNormalizedConfig(config.phi_config);
7779
break;
7880
case 'musicgen':
81+
// @ts-expect-error TS2339
7982
init_normalized_config = getNormalizedConfig(config.decoder);
8083
break;
8184
case 'multi_modality':
85+
// @ts-expect-error TS2339
8286
init_normalized_config = getNormalizedConfig(config.language_config);
8387
break;
8488

@@ -199,6 +203,7 @@ function getNormalizedConfig(config) {
199203
break;
200204

201205
case 'vision-encoder-decoder':
206+
// @ts-expect-error TS2339
202207
const decoderConfig = getNormalizedConfig(config.decoder);
203208

204209
const add_encoder_pkv = 'num_decoder_layers' in decoderConfig;

src/models.js

+19
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ async function getSession(pretrained_model_name_or_path, fileName, options) {
270270
} else if (session_options.externalData !== undefined) {
271271
externalDataPromises = session_options.externalData.map(async (ext) => {
272272
// if the external data is a string, fetch the file and replace the string with its content
273+
// @ts-expect-error TS2339
273274
if (typeof ext.data === "string") {
275+
// @ts-expect-error TS2339
274276
const ext_buffer = await getModelFile(pretrained_model_name_or_path, ext.data, true, options);
277+
// @ts-expect-error TS2698
275278
return { ...ext, data: ext_buffer };
276279
}
277280
return ext;
@@ -1519,6 +1522,7 @@ export class PreTrainedModel extends Callable {
15191522
if (this.config.model_type === 'musicgen') {
15201523
// Custom logic (TODO: move to Musicgen class)
15211524
decoder_input_ids = Array.from({
1525+
// @ts-expect-error TS2339
15221526
length: batch_size * this.config.decoder.num_codebooks
15231527
}, () => [decoder_start_token_id]);
15241528

@@ -1848,11 +1852,13 @@ export class PreTrainedModel extends Callable {
18481852
async encode_image({ pixel_values }) {
18491853
// image_inputs === { pixel_values }
18501854
const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values })).image_features;
1855+
// @ts-expect-error TS2339
18511856
if (!this.config.num_image_tokens) {
18521857
console.warn(
18531858
'The number of image tokens was not set in the model configuration. ' +
18541859
`Setting it to the number of features detected by the vision encoder (${features.dims[1]}).`
18551860
)
1861+
// @ts-expect-error TS2339
18561862
this.config.num_image_tokens = features.dims[1];
18571863
}
18581864
return features;
@@ -3280,6 +3286,7 @@ export class WhisperForConditionalGeneration extends WhisperPreTrainedModel {
32803286

32813287
if (generation_config.return_token_timestamps) {
32823288
outputs["token_timestamps"] = this._extract_token_timestamps(
3289+
// @ts-expect-error TS2345
32833290
outputs,
32843291
generation_config.alignment_heads,
32853292
generation_config.num_frames,
@@ -3315,6 +3322,7 @@ export class WhisperForConditionalGeneration extends WhisperPreTrainedModel {
33153322
);
33163323
}
33173324

3325+
// @ts-expect-error TS2339
33183326
let median_filter_width = this.config.median_filter_width;
33193327
if (median_filter_width === undefined) {
33203328
console.warn("Model config has no `median_filter_width`, using default value of 7.")
@@ -3325,6 +3333,7 @@ export class WhisperForConditionalGeneration extends WhisperPreTrainedModel {
33253333
const batch = generate_outputs.cross_attentions;
33263334
// Create a list with `decoder_layers` elements, each a tensor of shape
33273335
// (batch size, attention_heads, output length, input length).
3336+
// @ts-expect-error TS2339
33283337
const cross_attentions = Array.from({ length: this.config.decoder_layers },
33293338
// Concatenate the cross attentions for each layer across sequence length dimension.
33303339
(_, i) => cat(batch.map(x => x[i]), 2)
@@ -3468,6 +3477,7 @@ export class LlavaForConditionalGeneration extends LlavaPreTrainedModel {
34683477
attention_mask,
34693478
}) {
34703479

3480+
// @ts-expect-error TS2339
34713481
const image_token_index = this.config.image_token_index;
34723482

34733483
const idsList = input_ids.tolist();
@@ -6201,10 +6211,12 @@ export class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel {
62016211

62026212
const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs);
62036213

6214+
// @ts-expect-error TS2339
62046215
const r = encoder_outputs.dims[1] / this.config.reduction_factor;
62056216
const maxlen = Math.floor(r * maxlenratio);
62066217
const minlen = Math.floor(r * minlenratio);
62076218

6219+
// @ts-expect-error TS2339
62086220
const num_mel_bins = this.config.num_mel_bins;
62096221

62106222
let spectrogramParts = [];
@@ -6569,11 +6581,13 @@ export class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE:
65696581
*/
65706582
_apply_and_filter_by_delay_pattern_mask(outputs) {
65716583
const [bs_x_codebooks, seqLength] = outputs.dims;
6584+
// @ts-expect-error TS2339
65726585
const num_codebooks = this.config.decoder.num_codebooks;
65736586
const upperBound = (seqLength - num_codebooks);
65746587

65756588
let newDataSize = 0;
65766589
for (let i = 0; i < outputs.size; ++i) {
6590+
// @ts-expect-error TS2339
65776591
if (outputs.data[i] === this.config.decoder.pad_token_id) {
65786592
continue;
65796593
}
@@ -6603,7 +6617,9 @@ export class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE:
66036617
let clonedInputIds = structuredClone(input_ids);
66046618
for (let i = 0; i < clonedInputIds.length; ++i) {
66056619
for (let j = 0; j < clonedInputIds[i].length; ++j) {
6620+
// @ts-expect-error TS2339
66066621
if ((i % this.config.decoder.num_codebooks) >= j) {
6622+
// @ts-expect-error TS2339
66076623
clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id);
66086624
}
66096625
}
@@ -6760,6 +6776,9 @@ export class MultiModalityCausalLM extends MultiModalityPreTrainedModel {
67606776
'past_key_values',
67616777
];
67626778

6779+
/**
6780+
* @param {ConstructorParameters<typeof MultiModalityPreTrainedModel>} args
6781+
*/
67636782
constructor(...args) {
67646783
super(...args);
67656784

src/models/convnext/image_processing_convnext.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export class ConvNextImageProcessor extends ImageProcessor {
99
/**
1010
* Percentage of the image to crop. Only has an effect if this.size < 384.
1111
*/
12+
// @ts-expect-error TS2339
1213
this.crop_pct = this.config.crop_pct ?? (224 / 256);
1314
}
1415

src/models/efficientnet/image_processing_efficientnet.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
export class EfficientNetImageProcessor extends ImageProcessor {
66
constructor(config) {
77
super(config);
8+
// @ts-expect-error TS2339
89
this.include_top = this.config.include_top ?? true;
910
if (this.include_top) {
1011
this.image_std = this.image_std.map(x => x * x);

src/models/florence2/processing_florence2.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ export class Florence2Processor extends Processor {
1010
super(config, components);
1111

1212
const {
13+
// @ts-expect-error TS2339
1314
tasks_answer_post_processing_type,
15+
// @ts-expect-error TS2339
1416
task_prompts_without_inputs,
17+
// @ts-expect-error TS2339
1518
task_prompts_with_input,
1619
} = this.image_processor.config;
1720

src/models/janus/image_processing_janus.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class VLMImageProcessor extends ImageProcessor {
1313
},
1414
...config,
1515
});
16+
// @ts-expect-error TS2339
1617
this.constant_values = this.config.background_color.map(x => x * this.rescale_factor)
1718
}
1819

src/models/mgp_str/processing_mgp_str.js

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export class MgpstrProcessor extends Processor {
119119
* - bpe_preds: The list of BPE decoded sentences.
120120
* - wp_preds: The list of wp decoded sentences.
121121
*/
122+
// @ts-expect-error The type of this method is not compatible with the one
123+
// in the base class. It might be a good idea to fix this.
122124
batch_decode([char_logits, bpe_logits, wp_logits]) {
123125
const [char_preds, char_scores] = this._decode_helper(char_logits, 'char');
124126
const [bpe_preds, bpe_scores] = this._decode_helper(bpe_logits, 'bpe');

src/models/paligemma/processing_paligemma.js

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export class PaliGemmaProcessor extends Processor {
4141
}
4242

4343
const bos_token = this.tokenizer.bos_token;
44+
// @ts-expect-error TS2339
4445
const image_seq_length = this.image_processor.config.image_seq_length;
4546
let input_strings;
4647
if (text.some((t) => t.includes(IMAGE_TOKEN))) {

src/models/qwen2_vl/processing_qwen2_vl.js

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class Qwen2VLProcessor extends Processor {
2828
}
2929

3030
if (image_grid_thw) {
31+
// @ts-expect-error TS2551
3132
let merge_length = this.image_processor.config.merge_size ** 2;
3233
let index = 0;
3334

0 commit comments

Comments
 (0)