From 508a7040556dc6b45f09174c662a9632284b2445 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 17 Jun 2025 19:37:18 +0100 Subject: [PATCH] No more Tuple, List, Dict (#38797) * No more Tuple, List, Dict * make fixup * More style fixes * Docstring fixes with regex replacement * Trigger tests * Redo fixes after rebase * Fix copies * [test all] * update * [test all] * update * [test all] * make style after rebase * Patch the hf_argparser test * Patch the hf_argparser test * style fixes * style fixes * style fixes * Fix docstrings in Cohere test * [test all] --------- Co-authored-by: ydshieh --- benchmark/benchmarks_entrypoint.py | 6 +- docs/README.md | 2 +- docs/source/ar/custom_models.md | 2 +- docs/source/en/add_new_model.md | 2 +- docs/source/en/attention_interface.md | 2 +- docs/source/en/custom_models.md | 2 +- docs/source/en/llm_tutorial.md | 2 +- docs/source/en/model_doc/bros.md | 6 +- docs/source/en/model_doc/detr.md | 2 +- docs/source/en/model_doc/video_llava.md | 2 +- docs/source/en/modular_transformers.md | 12 +- docs/source/en/tasks/asr.md | 2 +- docs/source/en/tasks/object_detection.md | 10 +- docs/source/en/tasks/text-to-speech.md | 2 +- docs/source/es/custom_models.md | 2 +- docs/source/es/tasks/asr.md | 2 +- docs/source/it/custom_models.md | 2 +- docs/source/ja/custom_models.md | 2 +- docs/source/ja/hpo_train.md | 2 +- docs/source/ja/model_doc/bros.md | 6 +- docs/source/ja/model_doc/detr.md | 2 +- docs/source/ja/tasks/asr.md | 2 +- docs/source/ja/tasks/object_detection.md | 2 +- docs/source/ja/tasks/text-to-speech.md | 2 +- docs/source/ko/custom_models.md | 2 +- docs/source/ko/tasks/asr.md | 2 +- docs/source/ko/tasks/object_detection.md | 2 +- docs/source/pt/custom_models.md | 2 +- docs/source/zh/custom_models.md | 2 +- docs/source/zh/hpo_train.md | 2 +- docs/source/zh/tasks/asr.md | 2 +- examples/flax/question-answering/utils_qa.py | 4 +- examples/legacy/seq2seq/seq2seq_trainer.py | 4 +- examples/legacy/seq2seq/utils.py | 2 +- .../configuration_my_new_model.py | 4 +- .../image_processing_new_imgproc_model.py | 28 +- .../modeling_add_function.py | 4 +- .../modular-transformers/modeling_dummy.py | 10 +- .../modeling_dummy_bert.py | 22 +- .../modeling_from_uppercase_model.py | 6 +- .../modeling_multimodal1.py | 10 +- .../modeling_multimodal2.py | 8 +- .../modeling_my_new_model2.py | 12 +- .../modeling_new_task_model.py | 14 +- .../modular-transformers/modeling_roberta.py | 26 +- .../modular-transformers/modeling_super.py | 10 +- .../modeling_switch_function.py | 6 +- .../modeling_test_detr.py | 26 +- examples/pytorch/3d_parallel_checks.py | 4 +- .../object-detection/run_object_detection.py | 8 +- .../run_object_detection_no_trainer.py | 8 +- .../pytorch/question-answering/utils_qa.py | 4 +- .../tensorflow/question-answering/utils_qa.py | 4 +- pyproject.toml | 3 +- src/transformers/audio_utils.py | 12 +- src/transformers/cache_utils.py | 148 ++++----- .../commands/add_new_model_like.py | 70 ++-- src/transformers/commands/serving.py | 8 +- src/transformers/configuration_utils.py | 38 +-- src/transformers/data/data_collator.py | 60 ++-- src/transformers/data/datasets/glue.py | 4 +- .../data/datasets/language_modeling.py | 10 +- src/transformers/data/datasets/squad.py | 6 +- src/transformers/data/processors/glue.py | 6 +- src/transformers/data/processors/utils.py | 8 +- src/transformers/debug_utils.py | 2 +- src/transformers/dynamic_module_utils.py | 8 +- .../feature_extraction_sequence_utils.py | 22 +- src/transformers/feature_extraction_utils.py | 14 +- .../generation/beam_constraints.py | 20 +- src/transformers/generation/beam_search.py | 30 +- .../generation/candidate_generator.py | 36 +-- .../generation/configuration_utils.py | 66 ++-- .../generation/continuous_batching.py | 84 ++--- .../generation/flax_logits_process.py | 4 +- src/transformers/generation/flax_utils.py | 30 +- src/transformers/generation/logits_process.py | 48 +-- .../generation/stopping_criteria.py | 18 +- .../generation/tf_logits_process.py | 13 +- src/transformers/generation/tf_utils.py | 132 ++++---- src/transformers/generation/utils.py | 128 ++++---- src/transformers/generation/watermarking.py | 6 +- src/transformers/hf_argparser.py | 4 +- src/transformers/image_processing_base.py | 14 +- src/transformers/image_processing_utils.py | 4 +- .../image_processing_utils_fast.py | 6 +- src/transformers/image_transforms.py | 10 +- src/transformers/image_utils.py | 16 +- src/transformers/integrations/accelerate.py | 2 +- src/transformers/integrations/awq.py | 6 +- src/transformers/integrations/bitnet.py | 4 +- src/transformers/integrations/bitsandbytes.py | 4 +- src/transformers/integrations/eetq.py | 4 +- src/transformers/integrations/fbgemm_fp8.py | 4 +- .../integrations/finegrained_fp8.py | 10 +- .../integrations/flash_attention.py | 4 +- .../integrations/flex_attention.py | 6 +- src/transformers/integrations/hub_kernels.py | 4 +- .../integrations/integration_utils.py | 4 +- src/transformers/integrations/peft.py | 28 +- .../integrations/sdpa_attention.py | 4 +- src/transformers/integrations/sdpa_paged.py | 4 +- .../integrations/tensor_parallel.py | 8 +- src/transformers/integrations/vptq.py | 2 +- src/transformers/keras_callbacks.py | 4 +- src/transformers/loss/loss_d_fine.py | 4 +- .../loss/loss_for_object_detection.py | 14 +- src/transformers/loss/loss_grounding_dino.py | 6 +- src/transformers/loss/loss_rt_detr.py | 4 +- src/transformers/model_debugging_utils.py | 2 +- .../modeling_flash_attention_utils.py | 8 +- src/transformers/modeling_flax_outputs.py | 120 +++---- .../modeling_flax_pytorch_utils.py | 9 +- src/transformers/modeling_flax_utils.py | 28 +- src/transformers/modeling_outputs.py | 252 +++++++-------- src/transformers/modeling_tf_outputs.py | 172 +++++----- src/transformers/modeling_tf_utils.py | 62 ++-- src/transformers/modeling_utils.py | 136 ++++---- .../models/albert/modeling_albert.py | 34 +- .../models/albert/modeling_flax_albert.py | 10 +- .../models/albert/modeling_tf_albert.py | 32 +- .../models/albert/tokenization_albert.py | 18 +- .../models/albert/tokenization_albert_fast.py | 8 +- .../models/align/configuration_align.py | 30 +- .../models/align/modeling_align.py | 34 +- .../models/align/processing_align.py | 8 +- .../models/altclip/configuration_altclip.py | 2 +- .../models/altclip/modeling_altclip.py | 38 +-- .../models/altclip/processing_altclip.py | 6 +- .../models/aria/configuration_aria.py | 8 +- .../models/aria/image_processing_aria.py | 28 +- src/transformers/models/aria/modeling_aria.py | 26 +- src/transformers/models/aria/modular_aria.py | 50 +-- .../models/aria/processing_aria.py | 10 +- ...iguration_audio_spectrogram_transformer.py | 4 +- ...xtraction_audio_spectrogram_transformer.py | 6 +- .../modeling_audio_spectrogram_transformer.py | 18 +- src/transformers/models/auto/auto_factory.py | 8 +- .../models/auto/configuration_auto.py | 2 +- .../models/auto/feature_extraction_auto.py | 10 +- .../models/auto/image_processing_auto.py | 12 +- .../models/auto/processing_auto.py | 4 +- .../models/auto/video_processing_auto.py | 12 +- .../autoformer/configuration_autoformer.py | 8 +- .../models/autoformer/modeling_autoformer.py | 60 ++-- .../models/aya_vision/modeling_aya_vision.py | 26 +- .../models/aya_vision/modular_aya_vision.py | 18 +- .../aya_vision/processing_aya_vision.py | 10 +- .../bamba/convert_mamba_ssm_checkpoint.py | 14 +- .../models/bamba/modeling_bamba.py | 12 +- .../models/bamba/modular_bamba.py | 8 +- .../models/bark/configuration_bark.py | 10 +- .../bark/generation_configuration_bark.py | 10 +- src/transformers/models/bark/modeling_bark.py | 36 +-- .../models/bark/processing_bark.py | 6 +- src/transformers/models/bart/modeling_bart.py | 38 +-- .../models/bart/modeling_flax_bart.py | 18 +- .../models/bart/modeling_tf_bart.py | 40 +-- .../models/bart/tokenization_bart.py | 34 +- .../models/bart/tokenization_bart_fast.py | 14 +- .../models/barthez/tokenization_barthez.py | 38 +-- .../barthez/tokenization_barthez_fast.py | 26 +- .../models/bartpho/tokenization_bartpho.py | 38 +-- .../models/beit/configuration_beit.py | 6 +- .../models/beit/image_processing_beit.py | 66 ++-- .../models/beit/image_processing_beit_fast.py | 10 +- src/transformers/models/beit/modeling_beit.py | 34 +- .../models/beit/modeling_flax_beit.py | 20 +- src/transformers/models/bert/modeling_bert.py | 48 +-- .../models/bert/modeling_flax_bert.py | 10 +- .../models/bert/modeling_tf_bert.py | 58 ++-- .../models/bert/tokenization_bert.py | 12 +- .../models/bert/tokenization_bert_fast.py | 4 +- .../models/bert/tokenization_bert_tf.py | 4 +- .../modeling_bert_generation.py | 26 +- .../tokenization_bert_generation.py | 10 +- .../tokenization_bert_japanese.py | 14 +- .../models/bertweet/tokenization_bertweet.py | 34 +- .../models/big_bird/modeling_big_bird.py | 32 +- .../models/big_bird/modeling_flax_big_bird.py | 12 +- .../models/big_bird/tokenization_big_bird.py | 20 +- .../big_bird/tokenization_big_bird_fast.py | 14 +- .../convert_bigbird_pegasus_tf_to_pytorch.py | 3 +- .../modeling_bigbird_pegasus.py | 30 +- .../models/biogpt/modeling_biogpt.py | 22 +- .../models/biogpt/modular_biogpt.py | 20 +- .../models/biogpt/tokenization_biogpt.py | 12 +- .../models/bit/configuration_bit.py | 8 +- .../models/bit/image_processing_bit.py | 36 +-- src/transformers/models/bit/modeling_bit.py | 4 +- .../models/bitnet/modeling_bitnet.py | 10 +- .../models/bitnet/modular_bitnet.py | 6 +- .../models/blenderbot/modeling_blenderbot.py | 20 +- .../blenderbot/modeling_flax_blenderbot.py | 14 +- .../blenderbot/modeling_tf_blenderbot.py | 28 +- .../blenderbot/tokenization_blenderbot.py | 32 +- .../tokenization_blenderbot_fast.py | 22 +- .../modeling_blenderbot_small.py | 24 +- .../modeling_flax_blenderbot_small.py | 14 +- .../modeling_tf_blenderbot_small.py | 26 +- .../tokenization_blenderbot_small.py | 10 +- .../tokenization_blenderbot_small_fast.py | 12 +- .../models/blip/image_processing_blip.py | 28 +- src/transformers/models/blip/modeling_blip.py | 38 +-- .../models/blip/modeling_blip_text.py | 30 +- .../models/blip/modeling_tf_blip.py | 40 +-- .../models/blip/modeling_tf_blip_text.py | 12 +- .../models/blip/processing_blip.py | 6 +- .../models/blip_2/modeling_blip_2.py | 52 +-- .../models/blip_2/processing_blip_2.py | 6 +- .../models/bloom/configuration_bloom.py | 4 +- .../models/bloom/modeling_bloom.py | 26 +- .../models/bloom/modeling_flax_bloom.py | 8 +- .../models/bloom/tokenization_bloom_fast.py | 4 +- .../image_processing_bridgetower.py | 58 ++-- .../image_processing_bridgetower_fast.py | 14 +- .../bridgetower/modeling_bridgetower.py | 44 +-- .../bridgetower/processing_bridgetower.py | 4 +- src/transformers/models/bros/modeling_bros.py | 32 +- .../models/bros/processing_bros.py | 4 +- .../models/byt5/tokenization_byt5.py | 40 +-- .../models/camembert/modeling_camembert.py | 40 +-- .../models/camembert/modeling_tf_camembert.py | 44 +-- .../camembert/tokenization_camembert.py | 40 +-- .../camembert/tokenization_camembert_fast.py | 26 +- .../models/canine/modeling_canine.py | 46 +-- .../models/canine/tokenization_canine.py | 20 +- .../chameleon/configuration_chameleon.py | 10 +- .../chameleon/image_processing_chameleon.py | 36 +-- .../models/chameleon/modeling_chameleon.py | 12 +- .../models/chameleon/processing_chameleon.py | 10 +- .../image_processing_chinese_clip.py | 36 +-- .../chinese_clip/modeling_chinese_clip.py | 36 +-- .../chinese_clip/processing_chinese_clip.py | 8 +- .../models/clap/feature_extraction_clap.py | 12 +- src/transformers/models/clap/modeling_clap.py | 60 ++-- .../models/clap/processing_clap.py | 4 +- .../models/clip/image_processing_clip.py | 36 +-- src/transformers/models/clip/modeling_clip.py | 16 +- .../models/clip/modeling_flax_clip.py | 18 +- .../models/clip/modeling_tf_clip.py | 34 +- .../models/clip/processing_clip.py | 4 +- .../models/clip/tokenization_clip.py | 34 +- .../models/clip/tokenization_clip_fast.py | 24 +- .../models/clipseg/configuration_clipseg.py | 2 +- .../models/clipseg/modeling_clipseg.py | 32 +- .../models/clipseg/processing_clipseg.py | 6 +- .../models/clvp/feature_extraction_clvp.py | 8 +- src/transformers/models/clvp/modeling_clvp.py | 44 +-- .../models/clvp/tokenization_clvp.py | 14 +- .../code_llama/tokenization_code_llama.py | 30 +- .../tokenization_code_llama_fast.py | 16 +- .../models/codegen/configuration_codegen.py | 4 +- .../models/codegen/modeling_codegen.py | 20 +- .../models/codegen/tokenization_codegen.py | 8 +- .../codegen/tokenization_codegen_fast.py | 8 +- .../models/cohere/configuration_cohere.py | 4 +- .../models/cohere/modeling_cohere.py | 14 +- .../models/cohere/modular_cohere.py | 14 +- .../models/cohere/tokenization_cohere_fast.py | 32 +- .../models/cohere2/configuration_cohere2.py | 4 +- .../models/cohere2/modeling_cohere2.py | 14 +- .../models/cohere2/modular_cohere2.py | 16 +- .../colpali/convert_colpali_weights_to_hf.py | 6 +- .../models/colpali/modeling_colpali.py | 8 +- .../models/colpali/modular_colpali.py | 28 +- .../models/colpali/processing_colpali.py | 34 +- .../models/colqwen2/configuration_colqwen2.py | 4 +- .../convert_colqwen2_weights_to_hf.py | 8 +- .../models/colqwen2/modeling_colqwen2.py | 10 +- .../models/colqwen2/modular_colqwen2.py | 18 +- .../models/colqwen2/processing_colqwen2.py | 32 +- .../image_processing_conditional_detr.py | 200 ++++++------ .../image_processing_conditional_detr_fast.py | 110 +++---- .../modeling_conditional_detr.py | 54 ++-- .../modular_conditional_detr.py | 14 +- .../models/convbert/modeling_convbert.py | 22 +- .../models/convbert/modeling_tf_convbert.py | 14 +- .../models/convbert/tokenization_convbert.py | 12 +- .../convbert/tokenization_convbert_fast.py | 4 +- .../models/convnext/configuration_convnext.py | 8 +- .../convnext/image_processing_convnext.py | 30 +- .../image_processing_convnext_fast.py | 14 +- .../models/convnext/modeling_convnext.py | 10 +- .../models/convnext/modeling_tf_convnext.py | 14 +- .../convnextv2/configuration_convnextv2.py | 8 +- .../models/convnextv2/modeling_convnextv2.py | 10 +- .../convnextv2/modeling_tf_convnextv2.py | 16 +- .../models/cpm/tokenization_cpm.py | 40 +-- .../models/cpm/tokenization_cpm_fast.py | 26 +- .../models/cpmant/modeling_cpmant.py | 24 +- .../models/cpmant/tokenization_cpmant.py | 26 +- .../models/csm/configuration_csm.py | 8 +- src/transformers/models/csm/generation_csm.py | 14 +- src/transformers/models/csm/modeling_csm.py | 32 +- src/transformers/models/csm/modular_csm.py | 24 +- src/transformers/models/csm/processing_csm.py | 16 +- src/transformers/models/ctrl/modeling_ctrl.py | 18 +- .../models/ctrl/modeling_tf_ctrl.py | 20 +- .../models/ctrl/tokenization_ctrl.py | 4 +- .../models/cvt/configuration_cvt.py | 36 +-- src/transformers/models/cvt/modeling_cvt.py | 8 +- .../models/cvt/modeling_tf_cvt.py | 14 +- .../models/d_fine/configuration_d_fine.py | 8 +- .../models/d_fine/modeling_d_fine.py | 56 ++-- .../models/d_fine/modular_d_fine.py | 8 +- .../models/dab_detr/modeling_dab_detr.py | 32 +- .../models/dac/configuration_dac.py | 2 +- .../models/dac/feature_extraction_dac.py | 6 +- .../data2vec/configuration_data2vec_audio.py | 12 +- .../data2vec/configuration_data2vec_vision.py | 4 +- .../data2vec/modeling_data2vec_audio.py | 24 +- .../models/data2vec/modeling_data2vec_text.py | 36 +-- .../data2vec/modeling_data2vec_vision.py | 34 +- .../data2vec/modeling_tf_data2vec_vision.py | 26 +- src/transformers/models/dbrx/modeling_dbrx.py | 34 +- .../models/deberta/configuration_deberta.py | 2 +- .../models/deberta/modeling_deberta.py | 20 +- .../models/deberta/modeling_tf_deberta.py | 26 +- .../models/deberta/tokenization_deberta.py | 12 +- .../deberta/tokenization_deberta_fast.py | 8 +- .../deberta_v2/configuration_deberta_v2.py | 2 +- .../models/deberta_v2/modeling_deberta_v2.py | 20 +- .../deberta_v2/modeling_tf_deberta_v2.py | 28 +- .../deberta_v2/tokenization_deberta_v2.py | 10 +- .../tokenization_deberta_v2_fast.py | 4 +- .../modeling_decision_transformer.py | 18 +- .../deepseek_v3/modeling_deepseek_v3.py | 10 +- .../models/deepseek_v3/modular_deepseek_v3.py | 6 +- .../image_processing_deformable_detr.py | 164 +++++----- .../image_processing_deformable_detr_fast.py | 74 ++--- .../modeling_deformable_detr.py | 46 +-- .../modular_deformable_detr.py | 14 +- .../models/deit/image_processing_deit.py | 38 +-- src/transformers/models/deit/modeling_deit.py | 16 +- .../models/deit/modeling_tf_deit.py | 18 +- .../deprecated/deta/image_processing_deta.py | 130 ++++---- .../models/deprecated/deta/modeling_deta.py | 52 +-- .../configuration_efficientformer.py | 8 +- .../image_processing_efficientformer.py | 34 +- .../modeling_efficientformer.py | 22 +- .../modeling_tf_efficientformer.py | 26 +- .../ernie_m/configuration_ernie_m.py | 4 +- .../deprecated/ernie_m/modeling_ernie_m.py | 34 +- .../ernie_m/tokenization_ernie_m.py | 36 +-- .../modeling_gptsan_japanese.py | 40 +-- .../tokenization_gptsan_japanese.py | 10 +- .../graphormer/collating_graphormer.py | 4 +- .../graphormer/modeling_graphormer.py | 12 +- .../jukebox/configuration_jukebox.py | 4 +- .../deprecated/jukebox/modeling_jukebox.py | 50 +-- .../jukebox/tokenization_jukebox.py | 14 +- .../deprecated/mctct/configuration_mctct.py | 2 +- .../mctct/feature_extraction_mctct.py | 10 +- .../models/deprecated/mctct/modeling_mctct.py | 8 +- .../models/deprecated/mega/modeling_mega.py | 32 +- .../deprecated/nat/configuration_nat.py | 8 +- .../models/deprecated/nat/modeling_nat.py | 36 +-- .../models/deprecated/nezha/modeling_nezha.py | 42 +-- .../open_llama/modeling_open_llama.py | 22 +- .../deprecated/qdqbert/modeling_qdqbert.py | 24 +- .../models/deprecated/realm/modeling_realm.py | 36 +-- .../deprecated/realm/tokenization_realm.py | 12 +- .../realm/tokenization_realm_fast.py | 4 +- .../retribert/tokenization_retribert.py | 12 +- .../retribert/tokenization_retribert_fast.py | 4 +- .../modeling_speech_to_text_2.py | 12 +- .../tokenization_speech_to_text_2.py | 8 +- .../deprecated/tapex/tokenization_tapex.py | 102 +++--- .../modeling_trajectory_transformer.py | 24 +- .../transfo_xl/configuration_transfo_xl.py | 2 +- .../transfo_xl/modeling_tf_transfo_xl.py | 42 +-- .../transfo_xl/modeling_transfo_xl.py | 42 +-- .../transfo_xl/tokenization_transfo_xl.py | 14 +- .../deprecated/tvlt/configuration_tvlt.py | 4 +- .../tvlt/feature_extraction_tvlt.py | 12 +- .../deprecated/tvlt/image_processing_tvlt.py | 56 ++-- .../models/deprecated/tvlt/modeling_tvlt.py | 20 +- .../deprecated/van/configuration_van.py | 10 +- .../deprecated/van/convert_van_to_pytorch.py | 8 +- .../models/deprecated/van/modeling_van.py | 8 +- .../vit_hybrid/configuration_vit_hybrid.py | 4 +- .../vit_hybrid/image_processing_vit_hybrid.py | 36 +-- .../vit_hybrid/modeling_vit_hybrid.py | 16 +- .../xlm_prophetnet/modeling_xlm_prophetnet.py | 92 +++--- .../tokenization_xlm_prophetnet.py | 36 +-- .../configuration_depth_anything.py | 8 +- .../depth_anything/modeling_depth_anything.py | 14 +- .../depth_pro/configuration_depth_pro.py | 16 +- .../depth_pro/image_processing_depth_pro.py | 42 +-- .../image_processing_depth_pro_fast.py | 16 +- .../models/depth_pro/modeling_depth_pro.py | 32 +- .../models/detr/image_processing_detr.py | 214 ++++++------- .../models/detr/image_processing_detr_fast.py | 124 ++++---- src/transformers/models/detr/modeling_detr.py | 48 +-- .../diffllama/configuration_diffllama.py | 4 +- .../models/diffllama/modeling_diffllama.py | 18 +- .../models/diffllama/modular_diffllama.py | 14 +- .../models/dinat/configuration_dinat.py | 10 +- .../models/dinat/modeling_dinat.py | 36 +-- .../models/dinov2/configuration_dinov2.py | 4 +- .../models/dinov2/modeling_dinov2.py | 14 +- .../models/dinov2/modeling_flax_dinov2.py | 4 +- .../configuration_dinov2_with_registers.py | 4 +- .../modeling_dinov2_with_registers.py | 14 +- .../modular_dinov2_with_registers.py | 4 +- .../models/distilbert/modeling_distilbert.py | 34 +- .../distilbert/modeling_flax_distilbert.py | 6 +- .../distilbert/modeling_tf_distilbert.py | 18 +- .../distilbert/tokenization_distilbert.py | 12 +- .../tokenization_distilbert_fast.py | 4 +- .../models/donut/image_processing_donut.py | 42 +-- .../donut/image_processing_donut_fast.py | 6 +- .../models/donut/modeling_donut_swin.py | 50 +-- .../models/donut/processing_donut.py | 4 +- src/transformers/models/dpr/modeling_dpr.py | 26 +- .../models/dpr/modeling_tf_dpr.py | 28 +- .../models/dpr/tokenization_dpr.py | 20 +- .../models/dpr/tokenization_dpr_fast.py | 20 +- .../models/dpt/configuration_dpt.py | 14 +- .../models/dpt/image_processing_dpt.py | 60 ++-- src/transformers/models/dpt/modeling_dpt.py | 36 +-- .../configuration_efficientnet.py | 29 +- .../image_processing_efficientnet.py | 38 +-- .../efficientnet/modeling_efficientnet.py | 8 +- .../models/electra/modeling_electra.py | 42 +-- .../models/electra/modeling_flax_electra.py | 10 +- .../models/electra/modeling_tf_electra.py | 44 +-- .../models/electra/tokenization_electra.py | 12 +- .../electra/tokenization_electra_fast.py | 4 +- .../models/emu3/configuration_emu3.py | 20 +- .../models/emu3/convert_emu3_weights_to_hf.py | 4 +- .../models/emu3/image_processing_emu3.py | 54 ++-- src/transformers/models/emu3/modeling_emu3.py | 22 +- src/transformers/models/emu3/modular_emu3.py | 16 +- .../models/emu3/processing_emu3.py | 10 +- .../models/encodec/configuration_encodec.py | 2 +- .../encodec/feature_extraction_encodec.py | 6 +- .../models/encodec/modeling_encodec.py | 16 +- .../modeling_encoder_decoder.py | 8 +- .../modeling_flax_encoder_decoder.py | 8 +- .../modeling_tf_encoder_decoder.py | 8 +- .../models/ernie/modeling_ernie.py | 44 +-- .../models/esm/configuration_esm.py | 6 +- src/transformers/models/esm/modeling_esm.py | 22 +- .../models/esm/modeling_esmfold.py | 26 +- .../models/esm/modeling_tf_esm.py | 26 +- .../models/esm/openfold_utils/chunk_utils.py | 28 +- .../esm/openfold_utils/data_transforms.py | 5 +- .../models/esm/openfold_utils/feats.py | 12 +- .../models/esm/openfold_utils/loss.py | 6 +- .../models/esm/openfold_utils/protein.py | 22 +- .../esm/openfold_utils/residue_constants.py | 77 +++-- .../models/esm/openfold_utils/rigid_utils.py | 20 +- .../models/esm/openfold_utils/tensor_utils.py | 24 +- .../models/esm/tokenization_esm.py | 14 +- .../models/falcon/configuration_falcon.py | 4 +- .../models/falcon/modeling_falcon.py | 34 +- .../falcon_h1/configuration_falcon_h1.py | 4 +- .../models/falcon_h1/modeling_falcon_h1.py | 32 +- .../models/falcon_h1/modular_falcon_h1.py | 28 +- .../falcon_mamba/modeling_falcon_mamba.py | 14 +- .../configuration_fastspeech2_conformer.py | 14 +- .../modeling_fastspeech2_conformer.py | 16 +- .../tokenization_fastspeech2_conformer.py | 4 +- .../models/flaubert/modeling_flaubert.py | 50 +-- .../models/flaubert/modeling_tf_flaubert.py | 36 +-- .../models/flaubert/tokenization_flaubert.py | 12 +- .../models/flava/configuration_flava.py | 10 +- .../models/flava/image_processing_flava.py | 62 ++-- .../flava/image_processing_flava_fast.py | 10 +- .../models/flava/modeling_flava.py | 26 +- .../models/flava/processing_flava.py | 4 +- src/transformers/models/fnet/modeling_fnet.py | 18 +- .../models/fnet/tokenization_fnet.py | 18 +- .../models/fnet/tokenization_fnet_fast.py | 8 +- .../models/focalnet/configuration_focalnet.py | 6 +- .../models/focalnet/modeling_focalnet.py | 36 +-- .../models/fsmt/configuration_fsmt.py | 2 +- src/transformers/models/fsmt/modeling_fsmt.py | 24 +- .../models/fsmt/tokenization_fsmt.py | 14 +- .../models/funnel/configuration_funnel.py | 4 +- .../models/funnel/modeling_funnel.py | 48 +-- .../models/funnel/modeling_tf_funnel.py | 22 +- .../models/funnel/tokenization_funnel.py | 22 +- .../models/funnel/tokenization_funnel_fast.py | 14 +- .../models/fuyu/configuration_fuyu.py | 2 +- .../models/fuyu/image_processing_fuyu.py | 60 ++-- src/transformers/models/fuyu/modeling_fuyu.py | 12 +- .../models/fuyu/processing_fuyu.py | 44 +-- .../models/gemma/modeling_flax_gemma.py | 8 +- .../models/gemma/modeling_gemma.py | 10 +- .../models/gemma/modular_gemma.py | 10 +- .../models/gemma/tokenization_gemma.py | 32 +- .../models/gemma/tokenization_gemma_fast.py | 4 +- .../models/gemma2/modeling_gemma2.py | 12 +- .../models/gemma2/modular_gemma2.py | 12 +- .../models/gemma3/configuration_gemma3.py | 10 +- .../models/gemma3/image_processing_gemma3.py | 28 +- .../gemma3/image_processing_gemma3_fast.py | 10 +- .../models/gemma3/modeling_gemma3.py | 18 +- .../models/gemma3/modular_gemma3.py | 18 +- .../models/gemma3/processing_gemma3.py | 6 +- .../models/git/convert_git_to_pytorch.py | 4 +- src/transformers/models/git/modeling_git.py | 38 +-- src/transformers/models/git/processing_git.py | 8 +- src/transformers/models/glm/modeling_glm.py | 10 +- src/transformers/models/glm4/modeling_glm4.py | 12 +- src/transformers/models/glm4/modular_glm4.py | 8 +- .../models/glpn/configuration_glpn.py | 14 +- .../models/glpn/image_processing_glpn.py | 16 +- src/transformers/models/glpn/modeling_glpn.py | 10 +- .../models/got_ocr2/configuration_got_ocr2.py | 2 +- .../convert_got_ocr2_weights_to_hf.py | 4 +- .../got_ocr2/image_processing_got_ocr2.py | 44 +-- .../image_processing_got_ocr2_fast.py | 14 +- .../models/got_ocr2/modeling_got_ocr2.py | 32 +- .../models/got_ocr2/modular_got_ocr2.py | 12 +- .../models/got_ocr2/processing_got_ocr2.py | 14 +- .../models/gpt2/configuration_gpt2.py | 4 +- .../models/gpt2/modeling_flax_gpt2.py | 8 +- src/transformers/models/gpt2/modeling_gpt2.py | 48 +-- .../models/gpt2/modeling_tf_gpt2.py | 36 +-- .../models/gpt2/tokenization_gpt2.py | 14 +- .../models/gpt2/tokenization_gpt2_fast.py | 4 +- .../models/gpt2/tokenization_gpt2_tf.py | 10 +- .../gpt_bigcode/modeling_gpt_bigcode.py | 40 +-- .../models/gpt_neo/modeling_flax_gpt_neo.py | 8 +- .../models/gpt_neo/modeling_gpt_neo.py | 24 +- .../models/gpt_neox/configuration_gpt_neox.py | 4 +- .../models/gpt_neox/modeling_gpt_neox.py | 14 +- .../models/gpt_neox/modular_gpt_neox.py | 14 +- .../gpt_neox/tokenization_gpt_neox_fast.py | 14 +- .../configuration_gpt_neox_japanese.py | 4 +- .../modeling_gpt_neox_japanese.py | 14 +- .../tokenization_gpt_neox_japanese.py | 4 +- .../models/gpt_sw3/tokenization_gpt_sw3.py | 24 +- .../models/gptj/configuration_gptj.py | 4 +- .../models/gptj/modeling_flax_gptj.py | 8 +- src/transformers/models/gptj/modeling_gptj.py | 32 +- .../models/gptj/modeling_tf_gptj.py | 26 +- .../models/granite/modeling_granite.py | 14 +- .../models/granite/modular_granite.py | 10 +- .../granite_speech/modeling_granite_speech.py | 12 +- .../processing_granite_speech.py | 8 +- .../models/granitemoe/modeling_granitemoe.py | 22 +- .../modeling_granitemoehybrid.py | 22 +- .../modular_granitemoehybrid.py | 12 +- .../modeling_granitemoeshared.py | 22 +- .../modular_granitemoeshared.py | 8 +- .../image_processing_grounding_dino.py | 164 +++++----- .../image_processing_grounding_dino_fast.py | 72 ++--- .../grounding_dino/modeling_grounding_dino.py | 68 ++-- .../grounding_dino/modular_grounding_dino.py | 12 +- .../processing_grounding_dino.py | 28 +- .../models/groupvit/configuration_groupvit.py | 6 +- .../models/groupvit/modeling_groupvit.py | 24 +- .../models/groupvit/modeling_tf_groupvit.py | 38 +-- .../models/helium/modeling_helium.py | 10 +- .../models/herbert/tokenization_herbert.py | 12 +- .../herbert/tokenization_herbert_fast.py | 12 +- .../models/hgnet_v2/configuration_hgnet_v2.py | 26 +- .../models/hgnet_v2/modular_hgnet_v2.py | 26 +- .../models/hiera/configuration_hiera.py | 4 +- .../models/hiera/convert_hiera_to_hf.py | 3 +- .../models/hiera/modeling_hiera.py | 58 ++-- .../models/hubert/configuration_hubert.py | 6 +- .../models/hubert/modeling_hubert.py | 16 +- .../models/hubert/modeling_tf_hubert.py | 22 +- .../models/hubert/modular_hubert.py | 4 +- .../models/ibert/modeling_ibert.py | 14 +- .../idefics/image_processing_idefics.py | 20 +- .../models/idefics/modeling_idefics.py | 42 +-- .../models/idefics/modeling_tf_idefics.py | 42 +-- src/transformers/models/idefics/perceiver.py | 4 +- .../models/idefics/perceiver_tf.py | 4 +- .../models/idefics/processing_idefics.py | 22 +- src/transformers/models/idefics/vision.py | 14 +- src/transformers/models/idefics/vision_tf.py | 14 +- .../idefics2/image_processing_idefics2.py | 46 +-- .../models/idefics2/modeling_idefics2.py | 44 +-- .../models/idefics2/processing_idefics2.py | 12 +- .../idefics3/image_processing_idefics3.py | 64 ++-- .../models/idefics3/modeling_idefics3.py | 34 +- .../models/idefics3/processing_idefics3.py | 16 +- .../models/ijepa/modeling_ijepa.py | 14 +- .../imagegpt/image_processing_imagegpt.py | 22 +- .../models/imagegpt/modeling_imagegpt.py | 20 +- .../models/informer/configuration_informer.py | 8 +- .../models/informer/modeling_informer.py | 38 +-- .../models/informer/modular_informer.py | 8 +- .../instructblip/modeling_instructblip.py | 36 +-- .../instructblip/processing_instructblip.py | 6 +- .../image_processing_instructblipvideo.py | 34 +- .../modeling_instructblipvideo.py | 38 +-- .../modular_instructblipvideo.py | 8 +- .../processing_instructblipvideo.py | 4 +- .../video_processing_instructblipvideo.py | 10 +- .../models/internvl/modeling_internvl.py | 28 +- .../models/internvl/modular_internvl.py | 14 +- .../models/internvl/processing_internvl.py | 12 +- .../internvl/video_processing_internvl.py | 10 +- .../models/jamba/modeling_jamba.py | 22 +- .../models/janus/configuration_janus.py | 6 +- .../models/janus/image_processing_janus.py | 34 +- .../models/janus/modeling_janus.py | 24 +- .../models/janus/modular_janus.py | 28 +- .../models/janus/processing_janus.py | 8 +- .../models/jetmoe/modeling_jetmoe.py | 24 +- .../models/kosmos2/modeling_kosmos2.py | 60 ++-- .../models/kosmos2/processing_kosmos2.py | 42 +-- .../models/layoutlm/configuration_layoutlm.py | 4 +- .../models/layoutlm/modeling_layoutlm.py | 28 +- .../models/layoutlm/modeling_tf_layoutlm.py | 32 +- .../models/layoutlm/tokenization_layoutlm.py | 12 +- .../layoutlm/tokenization_layoutlm_fast.py | 4 +- .../layoutlmv2/configuration_layoutlmv2.py | 2 +- .../layoutlmv2/image_processing_layoutlmv2.py | 14 +- .../models/layoutlmv2/modeling_layoutlmv2.py | 10 +- .../layoutlmv2/processing_layoutlmv2.py | 10 +- .../layoutlmv2/tokenization_layoutlmv2.py | 78 ++--- .../tokenization_layoutlmv2_fast.py | 44 +-- .../layoutlmv3/image_processing_layoutlmv3.py | 14 +- .../models/layoutlmv3/modeling_layoutlmv3.py | 10 +- .../layoutlmv3/modeling_tf_layoutlmv3.py | 54 ++-- .../layoutlmv3/processing_layoutlmv3.py | 10 +- .../layoutlmv3/tokenization_layoutlmv3.py | 108 +++---- .../tokenization_layoutlmv3_fast.py | 66 ++-- .../models/layoutxlm/processing_layoutxlm.py | 10 +- .../layoutxlm/tokenization_layoutxlm.py | 124 ++++---- .../layoutxlm/tokenization_layoutxlm_fast.py | 84 ++--- .../models/led/configuration_led.py | 4 +- src/transformers/models/led/modeling_led.py | 100 +++--- .../models/led/modeling_tf_led.py | 62 ++-- .../models/led/tokenization_led.py | 36 +-- .../models/led/tokenization_led_fast.py | 16 +- .../models/levit/configuration_levit.py | 12 +- .../models/levit/image_processing_levit.py | 28 +- .../models/levit/modeling_levit.py | 10 +- src/transformers/models/lilt/modeling_lilt.py | 16 +- .../models/llama/configuration_llama.py | 4 +- .../llama/convert_llama_weights_to_hf.py | 3 +- .../models/llama/modeling_flax_llama.py | 8 +- .../models/llama/modeling_llama.py | 10 +- .../models/llama/tokenization_llama.py | 30 +- .../models/llama/tokenization_llama_fast.py | 4 +- .../models/llama4/configuration_llama4.py | 6 +- .../llama4/convert_llama4_weights_to_hf.py | 6 +- .../llama4/image_processing_llama4_fast.py | 40 +-- .../models/llama4/modeling_llama4.py | 44 +-- .../models/llama4/processing_llama4.py | 8 +- .../models/llava/configuration_llava.py | 2 +- .../models/llava/image_processing_llava.py | 40 +-- .../llava/image_processing_llava_fast.py | 12 +- .../models/llava/modeling_llava.py | 26 +- .../models/llava/processing_llava.py | 10 +- .../llava_next/configuration_llava_next.py | 2 +- .../llava_next/image_processing_llava_next.py | 70 ++-- .../image_processing_llava_next_fast.py | 26 +- .../models/llava_next/modeling_llava_next.py | 34 +- .../llava_next/processing_llava_next.py | 16 +- .../configuration_llava_next_video.py | 2 +- .../image_processing_llava_next_video.py | 52 +-- .../modeling_llava_next_video.py | 44 +-- .../modular_llava_next_video.py | 32 +- .../processing_llava_next_video.py | 10 +- .../configuration_llava_onevision.py | 2 +- .../image_processing_llava_onevision.py | 64 ++-- .../image_processing_llava_onevision_fast.py | 32 +- .../modeling_llava_onevision.py | 42 +-- .../modular_llava_onevision.py | 48 +-- .../processing_llava_onevision.py | 22 +- .../longformer/configuration_longformer.py | 10 +- .../models/longformer/modeling_longformer.py | 56 ++-- .../longformer/modeling_tf_longformer.py | 56 ++-- .../longformer/tokenization_longformer.py | 34 +- .../tokenization_longformer_fast.py | 14 +- .../models/longt5/modeling_flax_longt5.py | 10 +- .../models/longt5/modeling_longt5.py | 20 +- src/transformers/models/luke/modeling_luke.py | 72 ++--- .../models/luke/tokenization_luke.py | 128 ++++---- .../models/lxmert/modeling_lxmert.py | 42 +-- .../models/lxmert/modeling_tf_lxmert.py | 32 +- .../models/lxmert/tokenization_lxmert.py | 12 +- .../models/lxmert/tokenization_lxmert_fast.py | 4 +- .../models/m2m_100/modeling_m2m_100.py | 18 +- .../models/m2m_100/tokenization_m2m_100.py | 46 +-- ...convert_mamba_ssm_checkpoint_to_pytorch.py | 3 +- .../models/mamba/modeling_mamba.py | 14 +- ...onvert_mamba2_ssm_checkpoint_to_pytorch.py | 8 +- .../models/mamba2/modeling_mamba2.py | 10 +- .../convert_marian_tatoeba_to_pytorch.py | 3 +- .../marian/convert_marian_to_pytorch.py | 12 +- .../models/marian/modeling_flax_marian.py | 14 +- .../models/marian/modeling_marian.py | 26 +- .../models/marian/modeling_tf_marian.py | 28 +- .../models/marian/tokenization_marian.py | 34 +- .../markuplm/feature_extraction_markuplm.py | 4 +- .../models/markuplm/modeling_markuplm.py | 26 +- .../models/markuplm/tokenization_markuplm.py | 152 ++++----- .../markuplm/tokenization_markuplm_fast.py | 92 +++--- .../mask2former/configuration_mask2former.py | 10 +- ..._original_pytorch_checkpoint_to_pytorch.py | 20 +- .../image_processing_mask2former.py | 120 +++---- .../mask2former/modeling_mask2former.py | 120 +++---- .../maskformer/configuration_maskformer.py | 8 +- .../configuration_maskformer_swin.py | 8 +- ..._original_pytorch_checkpoint_to_pytorch.py | 20 +- .../maskformer/image_processing_maskformer.py | 124 ++++---- .../models/maskformer/modeling_maskformer.py | 114 +++---- .../maskformer/modeling_maskformer_swin.py | 28 +- .../models/mbart/modeling_flax_mbart.py | 14 +- .../models/mbart/modeling_mbart.py | 34 +- .../models/mbart/modeling_tf_mbart.py | 34 +- .../models/mbart/tokenization_mbart.py | 46 +-- .../models/mbart/tokenization_mbart_fast.py | 32 +- .../models/mbart50/tokenization_mbart50.py | 42 +-- .../mbart50/tokenization_mbart50_fast.py | 22 +- .../megatron_bert/modeling_megatron_bert.py | 44 +-- .../models/mgp_str/configuration_mgp_str.py | 2 +- .../models/mgp_str/modeling_mgp_str.py | 14 +- .../models/mgp_str/processing_mgp_str.py | 18 +- .../models/mgp_str/tokenization_mgp_str.py | 4 +- src/transformers/models/mimi/modeling_mimi.py | 46 +-- .../models/minimax/modeling_minimax.py | 28 +- .../models/minimax/modular_minimax.py | 18 +- .../models/mistral/modeling_flax_mistral.py | 10 +- .../models/mistral/modeling_mistral.py | 12 +- .../models/mistral/modeling_tf_mistral.py | 26 +- .../models/mistral/modular_mistral.py | 8 +- .../models/mistral3/configuration_mistral3.py | 2 +- .../models/mistral3/modeling_mistral3.py | 24 +- .../models/mistral3/modular_mistral3.py | 18 +- .../models/mixtral/modeling_mixtral.py | 20 +- .../models/mixtral/modular_mixtral.py | 14 +- src/transformers/models/mlcd/modeling_mlcd.py | 24 +- src/transformers/models/mlcd/modular_mlcd.py | 22 +- .../models/mllama/configuration_mllama.py | 20 +- .../mllama/convert_mllama_weights_to_hf.py | 6 +- .../models/mllama/image_processing_mllama.py | 82 ++--- .../models/mllama/modeling_mllama.py | 48 +-- .../models/mllama/processing_mllama.py | 24 +- .../models/mluke/tokenization_mluke.py | 134 ++++---- .../models/mobilebert/modeling_mobilebert.py | 34 +- .../mobilebert/modeling_tf_mobilebert.py | 22 +- .../mobilebert/tokenization_mobilebert.py | 12 +- .../tokenization_mobilebert_fast.py | 4 +- .../image_processing_mobilenet_v1.py | 38 +-- .../image_processing_mobilenet_v2.py | 44 +-- .../image_processing_mobilenet_v2_fast.py | 8 +- .../mobilevit/configuration_mobilevit.py | 6 +- .../mobilevit/image_processing_mobilevit.py | 40 +-- .../models/mobilevit/modeling_mobilevit.py | 8 +- .../models/mobilevit/modeling_tf_mobilevit.py | 12 +- .../mobilevitv2/configuration_mobilevitv2.py | 6 +- .../mobilevitv2/modeling_mobilevitv2.py | 6 +- .../models/modernbert/modeling_modernbert.py | 30 +- .../models/modernbert/modular_modernbert.py | 30 +- .../moonshine/configuration_moonshine.py | 4 +- .../models/moonshine/modeling_moonshine.py | 42 +-- .../models/moonshine/modular_moonshine.py | 40 +-- .../models/moshi/modeling_moshi.py | 66 ++-- .../models/mpnet/modeling_mpnet.py | 14 +- .../models/mpnet/modeling_tf_mpnet.py | 14 +- .../models/mpnet/tokenization_mpnet.py | 34 +- .../models/mpnet/tokenization_mpnet_fast.py | 14 +- src/transformers/models/mpt/modeling_mpt.py | 32 +- src/transformers/models/mra/modeling_mra.py | 14 +- src/transformers/models/mt5/modeling_mt5.py | 28 +- .../musicgen/convert_musicgen_transformers.py | 3 +- .../models/musicgen/modeling_musicgen.py | 44 +-- .../models/musicgen/processing_musicgen.py | 4 +- .../convert_musicgen_melody_transformers.py | 3 +- .../feature_extraction_musicgen_melody.py | 12 +- .../modeling_musicgen_melody.py | 44 +-- .../processing_musicgen_melody.py | 8 +- src/transformers/models/mvp/modeling_mvp.py | 44 +-- .../models/mvp/tokenization_mvp.py | 34 +- .../models/mvp/tokenization_mvp_fast.py | 14 +- .../models/myt5/tokenization_myt5.py | 60 ++-- .../models/nemotron/modeling_nemotron.py | 26 +- .../models/nllb/tokenization_nllb.py | 48 +-- .../models/nllb/tokenization_nllb_fast.py | 32 +- .../models/nllb_moe/modeling_nllb_moe.py | 26 +- .../models/nougat/image_processing_nougat.py | 42 +-- .../models/nougat/processing_nougat.py | 14 +- .../models/nougat/tokenization_nougat_fast.py | 16 +- .../nystromformer/modeling_nystromformer.py | 14 +- src/transformers/models/olmo/modeling_olmo.py | 10 +- src/transformers/models/olmo/modular_olmo.py | 6 +- .../olmo2/convert_olmo2_weights_to_hf.py | 4 +- .../models/olmo2/modeling_olmo2.py | 10 +- .../models/olmo2/modular_olmo2.py | 10 +- .../models/olmoe/modeling_olmoe.py | 30 +- .../omdet_turbo/configuration_omdet_turbo.py | 4 +- .../omdet_turbo/modeling_omdet_turbo.py | 58 ++-- .../omdet_turbo/processing_omdet_turbo.py | 32 +- .../oneformer/configuration_oneformer.py | 6 +- .../oneformer/convert_to_hf_oneformer.py | 22 +- .../oneformer/image_processing_oneformer.py | 118 +++---- .../models/oneformer/modeling_oneformer.py | 122 +++---- .../models/oneformer/processing_oneformer.py | 12 +- .../models/openai/modeling_openai.py | 16 +- .../models/openai/modeling_tf_openai.py | 16 +- .../models/openai/tokenization_openai.py | 4 +- .../models/openai/tokenization_openai_fast.py | 4 +- .../models/opt/modeling_flax_opt.py | 10 +- src/transformers/models/opt/modeling_opt.py | 30 +- .../models/opt/modeling_tf_opt.py | 30 +- .../models/owlv2/configuration_owlv2.py | 4 +- .../models/owlv2/image_processing_owlv2.py | 42 +-- .../models/owlv2/modeling_owlv2.py | 40 +-- .../models/owlv2/processing_owlv2.py | 32 +- .../models/owlvit/configuration_owlvit.py | 4 +- .../models/owlvit/image_processing_owlvit.py | 46 +-- .../owlvit/image_processing_owlvit_fast.py | 14 +- .../models/owlvit/modeling_owlvit.py | 40 +-- .../models/owlvit/processing_owlvit.py | 32 +- .../models/paligemma/modeling_paligemma.py | 16 +- .../models/paligemma/processing_paligemma.py | 20 +- .../configuration_patchtsmixer.py | 6 +- .../patchtsmixer/modeling_patchtsmixer.py | 26 +- .../models/patchtst/configuration_patchtst.py | 8 +- .../models/patchtst/modeling_patchtst.py | 40 +-- .../pegasus/convert_pegasus_tf_to_pytorch.py | 3 +- .../models/pegasus/modeling_flax_pegasus.py | 14 +- .../models/pegasus/modeling_pegasus.py | 24 +- .../models/pegasus/modeling_tf_pegasus.py | 30 +- .../models/pegasus/tokenization_pegasus.py | 16 +- .../pegasus/tokenization_pegasus_fast.py | 10 +- .../models/pegasus_x/modeling_pegasus_x.py | 22 +- .../perceiver/configuration_perceiver.py | 4 +- .../perceiver/image_processing_perceiver.py | 44 +-- .../image_processing_perceiver_fast.py | 4 +- .../models/perceiver/modeling_perceiver.py | 80 ++--- .../perceiver/tokenization_perceiver.py | 30 +- .../persimmon/configuration_persimmon.py | 4 +- .../models/persimmon/modeling_persimmon.py | 20 +- .../models/phi/configuration_phi.py | 4 +- src/transformers/models/phi/modeling_phi.py | 12 +- src/transformers/models/phi/modular_phi.py | 12 +- src/transformers/models/phi3/modeling_phi3.py | 10 +- src/transformers/models/phi3/modular_phi3.py | 10 +- .../feature_extraction_phi4_multimodal.py | 2 +- .../image_processing_phi4_multimodal_fast.py | 8 +- .../modeling_phi4_multimodal.py | 18 +- .../modular_phi4_multimodal.py | 8 +- .../processing_phi4_multimodal.py | 10 +- .../models/phimoe/modeling_phimoe.py | 28 +- .../models/phobert/tokenization_phobert.py | 34 +- .../pix2struct/image_processing_pix2struct.py | 10 +- .../models/pix2struct/modeling_pix2struct.py | 18 +- .../pix2struct/processing_pix2struct.py | 6 +- .../pixtral/image_processing_pixtral.py | 66 ++-- .../pixtral/image_processing_pixtral_fast.py | 26 +- .../models/pixtral/modeling_pixtral.py | 16 +- .../models/pixtral/processing_pixtral.py | 10 +- .../models/plbart/modeling_plbart.py | 34 +- .../models/plbart/modular_plbart.py | 14 +- .../models/plbart/tokenization_plbart.py | 46 +-- .../poolformer/image_processing_poolformer.py | 38 +-- .../image_processing_poolformer_fast.py | 2 +- .../models/poolformer/modeling_poolformer.py | 6 +- .../pop2piano/feature_extraction_pop2piano.py | 6 +- .../models/pop2piano/modeling_pop2piano.py | 8 +- .../models/pop2piano/processing_pop2piano.py | 8 +- .../pop2piano/tokenization_pop2piano.py | 12 +- .../configuration_prompt_depth_anything.py | 8 +- .../image_processing_prompt_depth_anything.py | 40 +-- .../modeling_prompt_depth_anything.py | 16 +- .../modular_prompt_depth_anything.py | 12 +- .../models/prophetnet/modeling_prophetnet.py | 92 +++--- .../prophetnet/tokenization_prophetnet.py | 16 +- .../models/pvt/configuration_pvt.py | 30 +- .../models/pvt/image_processing_pvt.py | 28 +- src/transformers/models/pvt/modeling_pvt.py | 12 +- .../models/pvt_v2/configuration_pvt_v2.py | 38 +-- .../models/pvt_v2/modeling_pvt_v2.py | 8 +- .../models/qwen2/configuration_qwen2.py | 4 +- .../models/qwen2/modeling_qwen2.py | 10 +- .../models/qwen2/modular_qwen2.py | 6 +- .../models/qwen2/tokenization_qwen2.py | 4 +- .../models/qwen2/tokenization_qwen2_fast.py | 4 +- .../configuration_qwen2_5_omni.py | 22 +- .../qwen2_5_omni/modeling_qwen2_5_omni.py | 66 ++-- .../qwen2_5_omni/modular_qwen2_5_omni.py | 68 ++-- .../qwen2_5_omni/processing_qwen2_5_omni.py | 16 +- .../qwen2_5_vl/configuration_qwen2_5_vl.py | 4 +- .../models/qwen2_5_vl/modeling_qwen2_5_vl.py | 56 ++-- .../models/qwen2_5_vl/modular_qwen2_5_vl.py | 32 +- .../qwen2_5_vl/processing_qwen2_5_vl.py | 18 +- .../qwen2_audio/modeling_qwen2_audio.py | 14 +- .../qwen2_audio/processing_qwen2_audio.py | 10 +- .../qwen2_moe/configuration_qwen2_moe.py | 6 +- .../models/qwen2_moe/modeling_qwen2_moe.py | 28 +- .../models/qwen2_vl/configuration_qwen2_vl.py | 4 +- .../qwen2_vl/image_processing_qwen2_vl.py | 40 +-- .../image_processing_qwen2_vl_fast.py | 22 +- .../models/qwen2_vl/modeling_qwen2_vl.py | 54 ++-- .../models/qwen2_vl/processing_qwen2_vl.py | 16 +- .../qwen2_vl/video_processing_qwen2_vl.py | 10 +- .../models/qwen3/configuration_qwen3.py | 4 +- .../models/qwen3/modeling_qwen3.py | 10 +- .../models/qwen3/modular_qwen3.py | 6 +- .../qwen3_moe/configuration_qwen3_moe.py | 6 +- .../models/qwen3_moe/modeling_qwen3_moe.py | 20 +- .../models/qwen3_moe/modular_qwen3_moe.py | 12 +- src/transformers/models/rag/modeling_rag.py | 60 ++-- .../models/rag/modeling_tf_rag.py | 50 +-- src/transformers/models/rag/retrieval_rag.py | 26 +- .../models/rag/tokenization_rag.py | 6 +- .../configuration_recurrent_gemma.py | 2 +- .../modeling_recurrent_gemma.py | 16 +- .../models/reformer/configuration_reformer.py | 8 +- .../models/reformer/modeling_reformer.py | 36 +-- .../models/reformer/tokenization_reformer.py | 12 +- .../reformer/tokenization_reformer_fast.py | 6 +- .../models/regnet/configuration_regnet.py | 4 +- .../convert_regnet_seer_10b_to_pytorch.py | 12 +- .../regnet/convert_regnet_to_pytorch.py | 16 +- .../models/regnet/modeling_flax_regnet.py | 4 +- .../models/regnet/modeling_tf_regnet.py | 6 +- .../models/rembert/modeling_rembert.py | 36 +-- .../models/rembert/modeling_tf_rembert.py | 48 +-- .../models/rembert/tokenization_rembert.py | 12 +- .../rembert/tokenization_rembert_fast.py | 12 +- .../models/resnet/configuration_resnet.py | 8 +- .../resnet/convert_resnet_to_pytorch.py | 8 +- .../models/resnet/modeling_flax_resnet.py | 4 +- .../models/resnet/modeling_tf_resnet.py | 8 +- .../models/roberta/modeling_flax_roberta.py | 6 +- .../models/roberta/modeling_roberta.py | 40 +-- .../models/roberta/modeling_tf_roberta.py | 44 +-- .../models/roberta/tokenization_roberta.py | 34 +- .../roberta/tokenization_roberta_fast.py | 14 +- .../modeling_flax_roberta_prelayernorm.py | 6 +- .../modeling_roberta_prelayernorm.py | 36 +-- .../modeling_tf_roberta_prelayernorm.py | 44 +-- .../models/roc_bert/modeling_roc_bert.py | 38 +-- .../models/roc_bert/tokenization_roc_bert.py | 50 +-- .../models/roformer/modeling_flax_roformer.py | 6 +- .../models/roformer/modeling_roformer.py | 20 +- .../models/roformer/modeling_tf_roformer.py | 30 +- .../models/roformer/tokenization_roformer.py | 12 +- .../roformer/tokenization_roformer_fast.py | 4 +- .../models/roformer/tokenization_utils.py | 4 +- .../models/rt_detr/configuration_rt_detr.py | 8 +- .../rt_detr/configuration_rt_detr_resnet.py | 8 +- .../rt_detr/image_processing_rt_detr.py | 124 ++++---- .../rt_detr/image_processing_rt_detr_fast.py | 64 ++-- .../models/rt_detr/modeling_rt_detr.py | 56 ++-- .../models/rt_detr/modular_rt_detr.py | 28 +- .../rt_detr_v2/configuration_rt_detr_v2.py | 8 +- .../models/rt_detr_v2/modeling_rt_detr_v2.py | 56 ++-- .../models/rt_detr_v2/modular_rt_detr_v2.py | 12 +- src/transformers/models/rwkv/modeling_rwkv.py | 22 +- .../models/sam/configuration_sam.py | 2 +- .../models/sam/image_processing_sam.py | 110 +++---- src/transformers/models/sam/modeling_sam.py | 32 +- .../models/sam/modeling_tf_sam.py | 36 +-- src/transformers/models/sam/processing_sam.py | 10 +- .../models/sam_hq/configuration_sam_hq.py | 2 +- .../models/sam_hq/modeling_sam_hq.py | 42 +-- .../models/sam_hq/modular_sam_hq.py | 14 +- .../models/sam_hq/processing_samhq.py | 10 +- .../configuration_seamless_m4t.py | 8 +- .../feature_extraction_seamless_m4t.py | 16 +- .../seamless_m4t/modeling_seamless_m4t.py | 84 ++--- .../seamless_m4t/processing_seamless_m4t.py | 4 +- .../seamless_m4t/tokenization_seamless_m4t.py | 64 ++-- .../tokenization_seamless_m4t_fast.py | 48 +-- .../configuration_seamless_m4t_v2.py | 8 +- .../modeling_seamless_m4t_v2.py | 94 +++--- .../segformer/configuration_segformer.py | 14 +- .../segformer/image_processing_segformer.py | 52 +-- .../models/segformer/modeling_segformer.py | 14 +- .../models/segformer/modeling_tf_segformer.py | 22 +- .../models/seggpt/configuration_seggpt.py | 4 +- .../models/seggpt/image_processing_seggpt.py | 60 ++-- .../models/seggpt/modeling_seggpt.py | 34 +- .../models/sew/configuration_sew.py | 6 +- src/transformers/models/sew/modeling_sew.py | 16 +- src/transformers/models/sew/modular_sew.py | 4 +- .../models/sew_d/configuration_sew_d.py | 8 +- .../models/sew_d/modeling_sew_d.py | 12 +- .../shieldgemma2/modeling_shieldgemma2.py | 4 +- .../models/siglip/convert_siglip_to_hf.py | 3 +- .../models/siglip/image_processing_siglip.py | 26 +- .../models/siglip/modeling_siglip.py | 16 +- .../models/siglip/processing_siglip.py | 8 +- .../models/siglip/tokenization_siglip.py | 42 +-- .../siglip2/image_processing_siglip2.py | 24 +- .../siglip2/image_processing_siglip2_fast.py | 10 +- .../models/siglip2/modeling_siglip2.py | 18 +- .../models/siglip2/modular_siglip2.py | 2 +- .../models/siglip2/processing_siglip2.py | 10 +- .../smolvlm/image_processing_smolvlm.py | 62 ++-- .../models/smolvlm/modeling_smolvlm.py | 34 +- .../models/smolvlm/modular_smolvlm.py | 6 +- .../models/smolvlm/processing_smolvlm.py | 20 +- .../smolvlm/video_processing_smolvlm.py | 14 +- .../modeling_flax_speech_encoder_decoder.py | 12 +- .../modeling_speech_encoder_decoder.py | 14 +- .../configuration_speech_to_text.py | 2 +- .../feature_extraction_speech_to_text.py | 10 +- .../speech_to_text/modeling_speech_to_text.py | 26 +- .../modeling_tf_speech_to_text.py | 26 +- .../tokenization_speech_to_text.py | 34 +- .../models/speecht5/configuration_speecht5.py | 14 +- .../speecht5/feature_extraction_speecht5.py | 18 +- .../models/speecht5/modeling_speecht5.py | 84 ++--- .../models/speecht5/tokenization_speecht5.py | 14 +- .../models/splinter/modeling_splinter.py | 32 +- .../models/splinter/tokenization_splinter.py | 34 +- .../splinter/tokenization_splinter_fast.py | 24 +- .../squeezebert/modeling_squeezebert.py | 14 +- .../squeezebert/tokenization_squeezebert.py | 12 +- .../tokenization_squeezebert_fast.py | 4 +- .../models/stablelm/configuration_stablelm.py | 4 +- .../models/stablelm/modeling_stablelm.py | 26 +- .../starcoder2/configuration_starcoder2.py | 4 +- .../models/starcoder2/modeling_starcoder2.py | 14 +- .../models/starcoder2/modular_starcoder2.py | 10 +- .../superglue/configuration_superglue.py | 10 +- .../superglue/convert_superglue_to_hf.py | 3 +- .../superglue/image_processing_superglue.py | 26 +- .../models/superglue/modeling_superglue.py | 36 +-- .../superpoint/configuration_superpoint.py | 3 +- .../superpoint/image_processing_superpoint.py | 26 +- .../models/superpoint/modeling_superpoint.py | 16 +- .../swiftformer/configuration_swiftformer.py | 6 +- .../swiftformer/modeling_swiftformer.py | 4 +- .../swiftformer/modeling_tf_swiftformer.py | 6 +- .../models/swin/configuration_swin.py | 4 +- src/transformers/models/swin/modeling_swin.py | 58 ++-- .../models/swin/modeling_tf_swin.py | 72 ++--- .../swin2sr/image_processing_swin2sr_fast.py | 4 +- .../models/swin2sr/modeling_swin2sr.py | 38 +-- .../models/swinv2/configuration_swinv2.py | 4 +- .../models/swinv2/modeling_swinv2.py | 60 ++-- .../modeling_switch_transformers.py | 22 +- .../models/t5/modeling_flax_t5.py | 6 +- src/transformers/models/t5/modeling_t5.py | 28 +- src/transformers/models/t5/modeling_tf_t5.py | 14 +- src/transformers/models/t5/tokenization_t5.py | 42 +-- .../models/t5/tokenization_t5_fast.py | 28 +- .../modeling_table_transformer.py | 26 +- .../models/tapas/configuration_tapas.py | 4 +- .../models/tapas/modeling_tapas.py | 22 +- .../models/tapas/modeling_tf_tapas.py | 36 +-- .../models/tapas/tokenization_tapas.py | 184 +++++------ .../models/textnet/configuration_textnet.py | 12 +- .../textnet/image_processing_textnet.py | 36 +-- .../models/textnet/modeling_textnet.py | 6 +- .../configuration_time_series_transformer.py | 8 +- .../modeling_time_series_transformer.py | 34 +- .../models/timesfm/configuration_timesfm.py | 6 +- .../timesformer/modeling_timesformer.py | 16 +- .../configuration_timm_backbone.py | 2 +- .../timm_backbone/modeling_timm_backbone.py | 4 +- .../configuration_timm_wrapper.py | 6 +- .../image_processing_timm_wrapper.py | 10 +- .../timm_wrapper/modeling_timm_wrapper.py | 16 +- .../models/trocr/modeling_trocr.py | 12 +- .../models/trocr/processing_trocr.py | 4 +- .../models/tvp/configuration_tvp.py | 2 +- .../models/tvp/image_processing_tvp.py | 68 ++-- src/transformers/models/tvp/modeling_tvp.py | 12 +- src/transformers/models/tvp/processing_tvp.py | 6 +- .../models/udop/configuration_udop.py | 2 +- src/transformers/models/udop/modeling_udop.py | 42 +-- .../models/udop/processing_udop.py | 8 +- .../models/udop/tokenization_udop.py | 168 +++++----- .../models/udop/tokenization_udop_fast.py | 124 ++++---- src/transformers/models/umt5/modeling_umt5.py | 28 +- .../unispeech/configuration_unispeech.py | 6 +- .../models/unispeech/modeling_unispeech.py | 22 +- .../models/unispeech/modular_unispeech.py | 10 +- .../configuration_unispeech_sat.py | 12 +- .../unispeech_sat/modeling_unispeech_sat.py | 30 +- .../unispeech_sat/modular_unispeech_sat.py | 10 +- .../models/univnet/configuration_univnet.py | 6 +- .../univnet/feature_extraction_univnet.py | 14 +- .../models/univnet/modeling_univnet.py | 6 +- .../models/upernet/configuration_upernet.py | 2 +- .../models/upernet/modeling_upernet.py | 16 +- .../video_llava/configuration_video_llava.py | 2 +- .../image_processing_video_llava.py | 46 +-- .../video_llava/modeling_video_llava.py | 38 +-- .../video_llava/processing_video_llava.py | 10 +- .../videomae/image_processing_videomae.py | 48 +-- .../models/videomae/modeling_videomae.py | 30 +- .../models/vilt/image_processing_vilt.py | 48 +-- .../models/vilt/image_processing_vilt_fast.py | 8 +- src/transformers/models/vilt/modeling_vilt.py | 22 +- .../models/vilt/processing_vilt.py | 4 +- .../models/vipllava/configuration_vipllava.py | 2 +- .../models/vipllava/modeling_vipllava.py | 30 +- .../models/vipllava/modular_vipllava.py | 24 +- .../modeling_flax_vision_encoder_decoder.py | 8 +- .../modeling_tf_vision_encoder_decoder.py | 10 +- .../modeling_vision_encoder_decoder.py | 8 +- .../modeling_flax_vision_text_dual_encoder.py | 6 +- .../modeling_tf_vision_text_dual_encoder.py | 6 +- .../modeling_vision_text_dual_encoder.py | 4 +- .../processing_vision_text_dual_encoder.py | 8 +- .../visual_bert/modeling_visual_bert.py | 18 +- .../models/vit/image_processing_vit.py | 28 +- .../models/vit/modeling_flax_vit.py | 4 +- .../models/vit/modeling_tf_vit.py | 18 +- src/transformers/models/vit/modeling_vit.py | 14 +- .../models/vit_mae/modeling_tf_vit_mae.py | 34 +- .../models/vit_mae/modeling_vit_mae.py | 30 +- .../models/vit_msn/modeling_vit_msn.py | 12 +- .../models/vitdet/configuration_vitdet.py | 8 +- .../models/vitdet/modeling_vitdet.py | 18 +- .../models/vitmatte/configuration_vitmatte.py | 11 +- .../vitmatte/image_processing_vitmatte.py | 18 +- .../models/vitmatte/modeling_vitmatte.py | 6 +- .../vitpose/image_processing_vitpose.py | 62 ++-- .../models/vitpose/modeling_vitpose.py | 6 +- .../configuration_vitpose_backbone.py | 6 +- .../modeling_vitpose_backbone.py | 10 +- .../models/vits/configuration_vits.py | 8 +- src/transformers/models/vits/modeling_vits.py | 20 +- .../models/vits/tokenization_vits.py | 14 +- .../models/vivit/configuration_vivit.py | 2 +- .../models/vivit/image_processing_vivit.py | 48 +-- .../models/vivit/modeling_vivit.py | 20 +- .../models/vjepa2/modeling_vjepa2.py | 40 +-- .../models/wav2vec2/configuration_wav2vec2.py | 12 +- .../wav2vec2/feature_extraction_wav2vec2.py | 10 +- .../models/wav2vec2/modeling_flax_wav2vec2.py | 22 +- .../models/wav2vec2/modeling_tf_wav2vec2.py | 28 +- .../models/wav2vec2/modeling_wav2vec2.py | 36 +-- .../models/wav2vec2/processing_wav2vec2.py | 4 +- .../models/wav2vec2/tokenization_wav2vec2.py | 58 ++-- .../configuration_wav2vec2_bert.py | 6 +- .../wav2vec2_bert/modeling_wav2vec2_bert.py | 26 +- .../wav2vec2_bert/modular_wav2vec2_bert.py | 24 +- .../wav2vec2_bert/processing_wav2vec2_bert.py | 8 +- .../configuration_wav2vec2_conformer.py | 12 +- .../modeling_wav2vec2_conformer.py | 28 +- .../modular_wav2vec2_conformer.py | 8 +- .../tokenization_wav2vec2_phoneme.py | 40 +-- .../processing_wav2vec2_with_lm.py | 18 +- .../models/wavlm/configuration_wavlm.py | 12 +- .../models/wavlm/modeling_wavlm.py | 22 +- .../models/wavlm/modular_wavlm.py | 4 +- .../models/whisper/configuration_whisper.py | 4 +- .../models/whisper/convert_openai_to_hf.py | 6 +- .../models/whisper/english_normalizer.py | 4 +- .../whisper/feature_extraction_whisper.py | 12 +- .../models/whisper/generation_whisper.py | 22 +- .../models/whisper/modeling_flax_whisper.py | 26 +- .../models/whisper/modeling_tf_whisper.py | 40 +-- .../models/whisper/modeling_whisper.py | 44 +-- .../models/whisper/tokenization_whisper.py | 34 +- .../whisper/tokenization_whisper_fast.py | 26 +- .../models/x_clip/modeling_x_clip.py | 38 +-- .../models/x_clip/processing_x_clip.py | 6 +- .../models/xglm/modeling_flax_xglm.py | 10 +- .../models/xglm/modeling_tf_xglm.py | 24 +- src/transformers/models/xglm/modeling_xglm.py | 16 +- .../models/xglm/tokenization_xglm.py | 38 +-- .../models/xglm/tokenization_xglm_fast.py | 26 +- .../models/xlm/modeling_tf_xlm.py | 34 +- src/transformers/models/xlm/modeling_xlm.py | 50 +-- .../models/xlm/tokenization_xlm.py | 12 +- .../xlm_roberta/modeling_flax_xlm_roberta.py | 6 +- .../xlm_roberta/modeling_tf_xlm_roberta.py | 44 +-- .../xlm_roberta/modeling_xlm_roberta.py | 40 +-- .../xlm_roberta/tokenization_xlm_roberta.py | 38 +-- .../tokenization_xlm_roberta_fast.py | 26 +- .../xlm_roberta_xl/modeling_xlm_roberta_xl.py | 28 +- .../models/xlnet/modeling_tf_xlnet.py | 64 ++-- .../models/xlnet/modeling_xlnet.py | 88 +++--- .../models/xlnet/tokenization_xlnet.py | 42 +-- .../models/xlnet/tokenization_xlnet_fast.py | 26 +- src/transformers/models/xmod/modeling_xmod.py | 36 +-- .../models/yolos/configuration_yolos.py | 2 +- .../models/yolos/image_processing_yolos.py | 152 ++++----- .../yolos/image_processing_yolos_fast.py | 80 ++--- .../models/yolos/modeling_yolos.py | 28 +- .../models/yolos/modular_yolos.py | 20 +- src/transformers/models/yoso/modeling_yoso.py | 14 +- .../models/zamba/modeling_zamba.py | 28 +- .../models/zamba2/modeling_zamba2.py | 32 +- .../models/zamba2/modular_zamba2.py | 16 +- .../models/zoedepth/configuration_zoedepth.py | 10 +- .../zoedepth/image_processing_zoedepth.py | 46 +-- .../image_processing_zoedepth_fast.py | 25 +- .../models/zoedepth/modeling_zoedepth.py | 24 +- src/transformers/onnx/config.py | 14 +- src/transformers/onnx/convert.py | 22 +- src/transformers/onnx/features.py | 10 +- src/transformers/optimization.py | 2 +- src/transformers/optimization_tf.py | 8 +- src/transformers/pipelines/__init__.py | 74 ++--- .../pipelines/audio_classification.py | 4 +- src/transformers/pipelines/audio_utils.py | 6 +- .../pipelines/automatic_speech_recognition.py | 8 +- src/transformers/pipelines/base.py | 50 +-- .../pipelines/depth_estimation.py | 12 +- .../pipelines/document_question_answering.py | 20 +- .../pipelines/feature_extraction.py | 8 +- src/transformers/pipelines/fill_mask.py | 18 +- .../pipelines/image_classification.py | 12 +- .../pipelines/image_feature_extraction.py | 8 +- .../pipelines/image_segmentation.py | 12 +- .../pipelines/image_text_to_text.py | 32 +- src/transformers/pipelines/image_to_image.py | 12 +- src/transformers/pipelines/image_to_text.py | 10 +- src/transformers/pipelines/mask_generation.py | 12 +- .../pipelines/object_detection.py | 18 +- .../pipelines/question_answering.py | 22 +- .../pipelines/table_question_answering.py | 10 +- .../pipelines/text2text_generation.py | 10 +- .../pipelines/text_classification.py | 10 +- src/transformers/pipelines/text_generation.py | 16 +- src/transformers/pipelines/text_to_audio.py | 12 +- .../pipelines/token_classification.py | 34 +- .../pipelines/video_classification.py | 10 +- .../pipelines/visual_question_answering.py | 10 +- .../zero_shot_audio_classification.py | 8 +- .../pipelines/zero_shot_classification.py | 12 +- .../zero_shot_image_classification.py | 20 +- .../pipelines/zero_shot_object_detection.py | 24 +- src/transformers/processing_utils.py | 44 +-- src/transformers/pytorch_utils.py | 6 +- src/transformers/quantizers/auto.py | 6 +- src/transformers/quantizers/base.py | 36 +-- src/transformers/quantizers/quantizer_awq.py | 4 +- .../quantizers/quantizer_bitnet.py | 6 +- .../quantizers/quantizer_bnb_4bit.py | 12 +- .../quantizers/quantizer_bnb_8bit.py | 12 +- .../quantizer_compressed_tensors.py | 7 +- src/transformers/quantizers/quantizer_eetq.py | 10 +- .../quantizers/quantizer_fbgemm_fp8.py | 12 +- .../quantizers/quantizer_finegrained_fp8.py | 12 +- .../quantizers/quantizer_higgs.py | 10 +- src/transformers/quantizers/quantizer_hqq.py | 16 +- .../quantizers/quantizer_quanto.py | 10 +- .../quantizers/quantizer_quark.py | 4 +- src/transformers/quantizers/quantizer_spqr.py | 4 +- .../quantizers/quantizer_torchao.py | 12 +- src/transformers/quantizers/quantizer_vptq.py | 4 +- .../quantizers/quantizers_utils.py | 4 +- src/transformers/tf_utils.py | 2 +- src/transformers/tokenization_utils.py | 24 +- src/transformers/tokenization_utils_base.py | 298 +++++++++--------- src/transformers/tokenization_utils_fast.py | 22 +- src/transformers/trainer.py | 64 ++-- src/transformers/trainer_callback.py | 8 +- src/transformers/trainer_pt_utils.py | 14 +- src/transformers/trainer_seq2seq.py | 10 +- src/transformers/trainer_utils.py | 6 +- src/transformers/training_args.py | 12 +- src/transformers/utils/args_doc.py | 10 +- src/transformers/utils/backbone_utils.py | 12 +- src/transformers/utils/chat_template_utils.py | 4 +- src/transformers/utils/fx.py | 6 +- src/transformers/utils/generic.py | 4 +- src/transformers/utils/hub.py | 12 +- src/transformers/utils/import_utils.py | 14 +- src/transformers/utils/metrics.py | 6 +- src/transformers/utils/notebook.py | 4 +- src/transformers/utils/peft_utils.py | 2 +- src/transformers/utils/quantization_config.py | 146 ++++----- src/transformers/video_processing_utils.py | 40 +-- src/transformers/video_utils.py | 42 +-- .../models/markuplm/test_modeling_markuplm.py | 2 +- tests/models/timesfm/test_modeling_timesfm.py | 3 +- .../test_feature_extraction_univnet.py | 2 +- tests/test_pipeline_mixin.py | 8 +- ...test_sequence_feature_extraction_common.py | 6 +- tests/utils/test_hf_argparser.py | 23 +- utils/check_copies.py | 14 +- utils/check_doc_toc.py | 3 +- utils/check_docstrings.py | 4 +- utils/check_dummies.py | 6 +- utils/check_inits.py | 8 +- utils/check_repo.py | 19 +- utils/custom_init_isort.py | 6 +- utils/deprecate_models.py | 4 +- utils/modular_model_converter.py | 16 +- utils/notification_service.py | 28 +- utils/notification_service_doc_tests.py | 13 +- utils/pr_slow_ci_models.py | 5 +- utils/tests_fetcher.py | 38 +-- utils/update_metadata.py | 5 +- 1291 files changed, 14906 insertions(+), 14941 deletions(-) diff --git a/benchmark/benchmarks_entrypoint.py b/benchmark/benchmarks_entrypoint.py index 71b83254b40..add1d704172 100644 --- a/benchmark/benchmarks_entrypoint.py +++ b/benchmark/benchmarks_entrypoint.py @@ -28,7 +28,7 @@ class MetricsRecorder: self.commit_id = commit_id self.commit_msg = commit_msg - def initialise_benchmark(self, metadata: Dict[str, str]) -> int: + def initialise_benchmark(self, metadata: dict[str, str]) -> int: """ Creates a new benchmark, returns the benchmark id """ @@ -55,7 +55,7 @@ class MetricsRecorder: f"inserted device measurements for benchmark #{benchmark_id} [CPU util: {cpu_util}, mem MBs: {mem_megabytes}, GPU util: {gpu_util}, GPU mem MBs: {gpu_mem_megabytes}]" ) - def collect_model_measurements(self, benchmark_id: int, measurements: Dict[str, float]): + def collect_model_measurements(self, benchmark_id: int, measurements: dict[str, float]): with self.conn.cursor() as cur: cur.execute( """ @@ -85,7 +85,7 @@ handler.setFormatter(formatter) logger.addHandler(handler) -def parse_arguments() -> Tuple[str, str, str, str]: +def parse_arguments() -> tuple[str, str, str, str]: """ Parse command line arguments for the benchmarking CLI. """ diff --git a/docs/README.md b/docs/README.md index bb54d700413..c295392210e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -278,7 +278,7 @@ Here's an example of a single value return: ```python Returns: - `List[int]`: A list of integers in the range [0, 1] --- 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1] --- 1 for a special token, 0 for a sequence token. ``` Here's an example of a tuple return, comprising several objects: diff --git a/docs/source/ar/custom_models.md b/docs/source/ar/custom_models.md index daaba5e54ee..26956af811f 100644 --- a/docs/source/ar/custom_models.md +++ b/docs/source/ar/custom_models.md @@ -30,7 +30,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/en/add_new_model.md b/docs/source/en/add_new_model.md index 45f1706aff9..a9d4109bd50 100644 --- a/docs/source/en/add_new_model.md +++ b/docs/source/en/add_new_model.md @@ -571,7 +571,7 @@ The processor should call the appropriate modality-specific processors within it def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[YourModelProcessorKwargs], diff --git a/docs/source/en/attention_interface.md b/docs/source/en/attention_interface.md index 76f264d83ee..d78e21413e0 100644 --- a/docs/source/en/attention_interface.md +++ b/docs/source/en/attention_interface.md @@ -92,7 +92,7 @@ def custom_attention( a_new_kwargs = None, # You can now add as many kwargs as you need another_new_kwargs = None, # You can now add as many kwargs as you need **kwargs, # You need to accept **kwargs as models will pass other args -) -> Tuple[torch.Tensor, Optional[torch.Tensor]] +) -> tuple[torch.Tensor, Optional[torch.Tensor]] ... # do your magic! return attn_output, attn_weights # attn_weights are optional here diff --git a/docs/source/en/custom_models.md b/docs/source/en/custom_models.md index 592aa9aa105..a6f9d1238e0 100644 --- a/docs/source/en/custom_models.md +++ b/docs/source/en/custom_models.md @@ -47,7 +47,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/en/llm_tutorial.md b/docs/source/en/llm_tutorial.md index 1283e8b6a4c..68de9169986 100644 --- a/docs/source/en/llm_tutorial.md +++ b/docs/source/en/llm_tutorial.md @@ -152,7 +152,7 @@ print(tokenizer.batch_decode(outputs, skip_special_tokens=True)) | `temperature` | `float` | How unpredictable the next selected token will be. High values (`>0.8`) are good for creative tasks, low values (e.g. `<0.4`) for tasks that require "thinking". Requires `do_sample=True`. | | `num_beams` | `int` | When set to `>1`, activates the beam search algorithm. Beam search is good on input-grounded tasks. Check [this guide](./generation_strategies.md) for more information. | | `repetition_penalty` | `float` | Set it to `>1.0` if you're seeing the model repeat itself often. Larger values apply a larger penalty. | -| `eos_token_id` | `List[int]` | The token(s) that will cause generation to stop. The default value is usually good, but you can specify a different token. | +| `eos_token_id` | `list[int]` | The token(s) that will cause generation to stop. The default value is usually good, but you can specify a different token. | ## Pitfalls diff --git a/docs/source/en/model_doc/bros.md b/docs/source/en/model_doc/bros.md index 67b4bffd258..9ef37e8ea79 100644 --- a/docs/source/en/model_doc/bros.md +++ b/docs/source/en/model_doc/bros.md @@ -62,11 +62,11 @@ def make_box_first_token_mask(bboxes, words, tokenizer, max_seq_length=512): box_first_token_mask = np.zeros(max_seq_length, dtype=np.bool_) - # encode(tokenize) each word from words (List[str]) - input_ids_list: List[List[int]] = [tokenizer.encode(e, add_special_tokens=False) for e in words] + # encode(tokenize) each word from words (list[str]) + input_ids_list: list[list[int]] = [tokenizer.encode(e, add_special_tokens=False) for e in words] # get the length of each box - tokens_length_list: List[int] = [len(l) for l in input_ids_list] + tokens_length_list: list[int] = [len(l) for l in input_ids_list] box_end_token_indices = np.array(list(itertools.accumulate(tokens_length_list))) box_start_token_indices = box_end_token_indices - np.array(tokens_length_list) diff --git a/docs/source/en/model_doc/detr.md b/docs/source/en/model_doc/detr.md index 54094f94dfa..aa942b23194 100644 --- a/docs/source/en/model_doc/detr.md +++ b/docs/source/en/model_doc/detr.md @@ -149,7 +149,7 @@ As a summary, consider the following table: | **Description** | Predicting bounding boxes and class labels around objects in an image | Predicting masks around objects (i.e. instances) in an image | Predicting masks around both objects (i.e. instances) as well as "stuff" (i.e. background things like trees and roads) in an image | | **Model** | [`~transformers.DetrForObjectDetection`] | [`~transformers.DetrForSegmentation`] | [`~transformers.DetrForSegmentation`] | | **Example dataset** | COCO detection | COCO detection, COCO panoptic | COCO panoptic | | -| **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `List[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `List[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} and masks_path (path to directory containing PNG files of the masks) | +| **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `list[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `list[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} and masks_path (path to directory containing PNG files of the masks) | | **Postprocessing** (i.e. converting the output of the model to Pascal VOC format) | [`~transformers.DetrImageProcessor.post_process`] | [`~transformers.DetrImageProcessor.post_process_segmentation`] | [`~transformers.DetrImageProcessor.post_process_segmentation`], [`~transformers.DetrImageProcessor.post_process_panoptic`] | | **evaluators** | `CocoEvaluator` with `iou_types="bbox"` | `CocoEvaluator` with `iou_types="bbox"` or `"segm"` | `CocoEvaluator` with `iou_tupes="bbox"` or `"segm"`, `PanopticEvaluator` | diff --git a/docs/source/en/model_doc/video_llava.md b/docs/source/en/model_doc/video_llava.md index a9282bdad0a..b2051a91f2d 100644 --- a/docs/source/en/model_doc/video_llava.md +++ b/docs/source/en/model_doc/video_llava.md @@ -83,7 +83,7 @@ def read_video_pyav(container, indices): Decode the video with PyAV decoder. Args: container (`av.container.input.InputContainer`): PyAV container. - indices (`List[int]`): List of frame indices to decode. + indices (`list[int]`): List of frame indices to decode. Returns: result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ''' diff --git a/docs/source/en/modular_transformers.md b/docs/source/en/modular_transformers.md index 84d365f9aad..1cd8f0cbb1b 100644 --- a/docs/source/en/modular_transformers.md +++ b/docs/source/en/modular_transformers.md @@ -216,12 +216,12 @@ class Olmo2Attention(OlmoAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -294,9 +294,9 @@ class Olmo2DecoderLayer(OlmoDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states # Self Attention @@ -494,7 +494,7 @@ class LlamaForCausalLM(nn.Module): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -520,7 +520,7 @@ class NewModelForCausalLM(LlamaForCausalLM): | class LlamaForCausalLM(nn.M | input_ids: torch.LongTensor = None, | attention_mask: Optional[torch.Tensor] = None, | position_ids: Optional[torch.LongTensor] = None, - | past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = |None, + | past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = |None, | inputs_embeds: Optional[torch.FloatTensor] = None, | labels: Optional[torch.LongTensor] = None, | use_cache: Optional[bool] = None, diff --git a/docs/source/en/tasks/asr.md b/docs/source/en/tasks/asr.md index e8884d327b5..760f701cf5c 100644 --- a/docs/source/en/tasks/asr.md +++ b/docs/source/en/tasks/asr.md @@ -170,7 +170,7 @@ Unlike other data collators, this specific data collator needs to apply a differ ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... # split inputs and labels since they have to be of different lengths and need ... # different padding methods ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] diff --git a/docs/source/en/tasks/object_detection.md b/docs/source/en/tasks/object_detection.md index c307dd3334f..61db61b7db2 100644 --- a/docs/source/en/tasks/object_detection.md +++ b/docs/source/en/tasks/object_detection.md @@ -243,7 +243,7 @@ and it uses the exact same dataset as an example. Apply some geometric and color ... ) ``` -The `image_processor` expects the annotations to be in the following format: `{'image_id': int, 'annotations': List[Dict]}`, +The `image_processor` expects the annotations to be in the following format: `{'image_id': int, 'annotations': list[Dict]}`, where each dictionary is a COCO object annotation. Let's add a function to reformat annotations for a single example: ```py @@ -252,9 +252,9 @@ The `image_processor` expects the annotations to be in the following format: `{' ... Args: ... image_id (str): image id. e.g. "0001" -... categories (List[int]): list of categories/class labels corresponding to provided bounding boxes -... areas (List[float]): list of corresponding areas to provided bounding boxes -... bboxes (List[Tuple[float]]): list of bounding boxes provided in COCO format +... categories (list[int]): list of categories/class labels corresponding to provided bounding boxes +... areas (list[float]): list of corresponding areas to provided bounding boxes +... bboxes (list[tuple[float]]): list of bounding boxes provided in COCO format ... ([center_x, center_y, width, height] in absolute coordinates) ... Returns: @@ -397,7 +397,7 @@ Intermediate format of boxes used for training is `YOLO` (normalized) but we wil ... Args: ... boxes (torch.Tensor): Bounding boxes in YOLO format -... image_size (Tuple[int, int]): Image size in format (height, width) +... image_size (tuple[int, int]): Image size in format (height, width) ... Returns: ... torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max) diff --git a/docs/source/en/tasks/text-to-speech.md b/docs/source/en/tasks/text-to-speech.md index e25da4e19ef..6715e7b7c6b 100644 --- a/docs/source/en/tasks/text-to-speech.md +++ b/docs/source/en/tasks/text-to-speech.md @@ -408,7 +408,7 @@ instructs the model to ignore that part of the spectrogram when calculating the ... class TTSDataCollatorWithPadding: ... processor: Any -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... input_ids = [{"input_ids": feature["input_ids"]} for feature in features] ... label_features = [{"input_values": feature["labels"]} for feature in features] ... speaker_features = [feature["speaker_embeddings"] for feature in features] diff --git a/docs/source/es/custom_models.md b/docs/source/es/custom_models.md index 022b50d9ba5..7e00505b8df 100644 --- a/docs/source/es/custom_models.md +++ b/docs/source/es/custom_models.md @@ -48,7 +48,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/es/tasks/asr.md b/docs/source/es/tasks/asr.md index 41e9f82e35f..d5bb614e70d 100644 --- a/docs/source/es/tasks/asr.md +++ b/docs/source/es/tasks/asr.md @@ -166,7 +166,7 @@ A diferencia de otros collators de datos, este tiene que aplicarle un método de ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... # particiona las entradas y las etiquetas ya que tienen que tener longitudes distintas y ... # requieren métodos de padding diferentes ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] diff --git a/docs/source/it/custom_models.md b/docs/source/it/custom_models.md index 94626937eb8..a564ea606c7 100644 --- a/docs/source/it/custom_models.md +++ b/docs/source/it/custom_models.md @@ -47,7 +47,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/ja/custom_models.md b/docs/source/ja/custom_models.md index 588e804494e..cd0e70b1f48 100644 --- a/docs/source/ja/custom_models.md +++ b/docs/source/ja/custom_models.md @@ -39,7 +39,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/ja/hpo_train.md b/docs/source/ja/hpo_train.md index 90591daf8b2..ff82d7138b0 100644 --- a/docs/source/ja/hpo_train.md +++ b/docs/source/ja/hpo_train.md @@ -56,7 +56,7 @@ Optunaに関しては、[object_parameter](https://optuna.readthedocs.io/en/stab ... } ``` -Optunaは、多目的のハイパーパラメータ最適化(HPO)を提供しています。 `hyperparameter_search` で `direction` を渡し、複数の目的関数値を返すための独自の `compute_objective` を定義することができます。 Pareto Front(`List[BestRun]`)は `hyperparameter_search` で返され、[test_trainer](https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py) のテストケース `TrainerHyperParameterMultiObjectOptunaIntegrationTest` を参照する必要があります。これは以下のようになります。 +Optunaは、多目的のハイパーパラメータ最適化(HPO)を提供しています。 `hyperparameter_search` で `direction` を渡し、複数の目的関数値を返すための独自の `compute_objective` を定義することができます。 Pareto Front(`list[BestRun]`)は `hyperparameter_search` で返され、[test_trainer](https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py) のテストケース `TrainerHyperParameterMultiObjectOptunaIntegrationTest` を参照する必要があります。これは以下のようになります。 ```py diff --git a/docs/source/ja/model_doc/bros.md b/docs/source/ja/model_doc/bros.md index def35395852..58b59e25246 100644 --- a/docs/source/ja/model_doc/bros.md +++ b/docs/source/ja/model_doc/bros.md @@ -57,11 +57,11 @@ def make_box_first_token_mask(bboxes, words, tokenizer, max_seq_length=512): box_first_token_mask = np.zeros(max_seq_length, dtype=np.bool_) - # encode(tokenize) each word from words (List[str]) - input_ids_list: List[List[int]] = [tokenizer.encode(e, add_special_tokens=False) for e in words] + # encode(tokenize) each word from words (list[str]) + input_ids_list: list[list[int]] = [tokenizer.encode(e, add_special_tokens=False) for e in words] # get the length of each box - tokens_length_list: List[int] = [len(l) for l in input_ids_list] + tokens_length_list: list[int] = [len(l) for l in input_ids_list] box_end_token_indices = np.array(list(itertools.accumulate(tokens_length_list))) box_start_token_indices = box_end_token_indices - np.array(tokens_length_list) diff --git a/docs/source/ja/model_doc/detr.md b/docs/source/ja/model_doc/detr.md index d709d76f043..bef1a8fce77 100644 --- a/docs/source/ja/model_doc/detr.md +++ b/docs/source/ja/model_doc/detr.md @@ -149,7 +149,7 @@ DETR モデルをインスタンス化するには 3 つの方法があります | **Description** |画像内のオブジェクトの周囲の境界ボックスとクラス ラベルを予測する | 画像内のオブジェクト (つまりインスタンス) の周囲のマスクを予測する | 画像内のオブジェクト (インスタンス) と「もの」 (木や道路などの背景) の両方の周囲のマスクを予測します | | **Model** | [`~transformers.DetrForObjectDetection`] | [`~transformers.DetrForSegmentation`] | [`~transformers.DetrForSegmentation`] | | **Example dataset** | COCO detection | COCO detection, COCO panoptic | COCO panoptic | | -| **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `List[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `List[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `List[Dict]`} and masks_path (path to directory containing PNG files of the masks) | +| **Format of annotations to provide to** [`~transformers.DetrImageProcessor`] | {'image_id': `int`, 'annotations': `list[Dict]`} each Dict being a COCO object annotation | {'image_id': `int`, 'annotations': `list[Dict]`} (in case of COCO detection) or {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} (in case of COCO panoptic) | {'file_name': `str`, 'image_id': `int`, 'segments_info': `list[Dict]`} and masks_path (path to directory containing PNG files of the masks) | | **Postprocessing** (i.e. converting the output of the model to Pascal VOC format) | [`~transformers.DetrImageProcessor.post_process`] | [`~transformers.DetrImageProcessor.post_process_segmentation`] | [`~transformers.DetrImageProcessor.post_process_segmentation`], [`~transformers.DetrImageProcessor.post_process_panoptic`] | | **evaluators** | `CocoEvaluator` with `iou_types="bbox"` | `CocoEvaluator` with `iou_types="bbox"` or `"segm"` | `CocoEvaluator` with `iou_tupes="bbox"` or `"segm"`, `PanopticEvaluator` | diff --git a/docs/source/ja/tasks/asr.md b/docs/source/ja/tasks/asr.md index ebefeba831a..5e460a102f4 100644 --- a/docs/source/ja/tasks/asr.md +++ b/docs/source/ja/tasks/asr.md @@ -170,7 +170,7 @@ MInDS-14 データセットのサンプリング レートは 8000kHz です ( ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... # split inputs and labels since they have to be of different lengths and need ... # different padding methods ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] diff --git a/docs/source/ja/tasks/object_detection.md b/docs/source/ja/tasks/object_detection.md index 31e8effa54b..1b6c03ee103 100644 --- a/docs/source/ja/tasks/object_detection.md +++ b/docs/source/ja/tasks/object_detection.md @@ -208,7 +208,7 @@ DETR モデルをトレーニングできる「ラベル」。画像プロセッ ... ) ``` -`image_processor` は、注釈が次の形式であることを期待します: `{'image_id': int, 'annotations': List[Dict]}`, +`image_processor` は、注釈が次の形式であることを期待します: `{'image_id': int, 'annotations': list[Dict]}`, ここで、各辞書は COCO オブジェクトの注釈です。 1 つの例として、注釈を再フォーマットする関数を追加してみましょう。 ```py diff --git a/docs/source/ja/tasks/text-to-speech.md b/docs/source/ja/tasks/text-to-speech.md index 669d15730e2..1c22dfd71a7 100644 --- a/docs/source/ja/tasks/text-to-speech.md +++ b/docs/source/ja/tasks/text-to-speech.md @@ -408,7 +408,7 @@ Y 軸が反転され、スペクトログラムが上下逆に表示されます ... class TTSDataCollatorWithPadding: ... processor: Any -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... input_ids = [{"input_ids": feature["input_ids"]} for feature in features] ... label_features = [{"input_values": feature["labels"]} for feature in features] ... speaker_features = [feature["speaker_embeddings"] for feature in features] diff --git a/docs/source/ko/custom_models.md b/docs/source/ko/custom_models.md index cb67a535b47..1a230a04b28 100644 --- a/docs/source/ko/custom_models.md +++ b/docs/source/ko/custom_models.md @@ -46,7 +46,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/ko/tasks/asr.md b/docs/source/ko/tasks/asr.md index d1e4a5e1d91..6c8ad6fc320 100644 --- a/docs/source/ko/tasks/asr.md +++ b/docs/source/ko/tasks/asr.md @@ -172,7 +172,7 @@ MInDS-14 데이터 세트의 샘플링 레이트는 8000kHz이므로([데이터 ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... # 입력과 레이블을 분할합니다 ... # 길이가 다르고, 각각 다른 패딩 방법을 사용해야 하기 때문입니다 ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] diff --git a/docs/source/ko/tasks/object_detection.md b/docs/source/ko/tasks/object_detection.md index e027ad65a9a..75319d93c24 100644 --- a/docs/source/ko/tasks/object_detection.md +++ b/docs/source/ko/tasks/object_detection.md @@ -201,7 +201,7 @@ DatasetDict({ ... ) ``` -이미지 프로세서는 어노테이션이 다음과 같은 형식일 것으로 예상합니다: `{'image_id': int, 'annotations': List[Dict]}`, 여기서 각 딕셔너리는 COCO 객체 어노테이션입니다. 단일 예제에 대해 어노테이션의 형식을 다시 지정하는 함수를 추가해 보겠습니다: +이미지 프로세서는 어노테이션이 다음과 같은 형식일 것으로 예상합니다: `{'image_id': int, 'annotations': list[Dict]}`, 여기서 각 딕셔너리는 COCO 객체 어노테이션입니다. 단일 예제에 대해 어노테이션의 형식을 다시 지정하는 함수를 추가해 보겠습니다: ```py >>> def formatted_anns(image_id, category, area, bbox): diff --git a/docs/source/pt/custom_models.md b/docs/source/pt/custom_models.md index 27633f9d1bb..75376ff6e50 100644 --- a/docs/source/pt/custom_models.md +++ b/docs/source/pt/custom_models.md @@ -47,7 +47,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/zh/custom_models.md b/docs/source/zh/custom_models.md index 209e593506e..a96f0f545df 100644 --- a/docs/source/zh/custom_models.md +++ b/docs/source/zh/custom_models.md @@ -39,7 +39,7 @@ class ResnetConfig(PretrainedConfig): def __init__( self, block_type="bottleneck", - layers: List[int] = [3, 4, 6, 3], + layers: list[int] = [3, 4, 6, 3], num_classes: int = 1000, input_channels: int = 3, cardinality: int = 1, diff --git a/docs/source/zh/hpo_train.md b/docs/source/zh/hpo_train.md index 907be0a21fa..9bc04109ac0 100644 --- a/docs/source/zh/hpo_train.md +++ b/docs/source/zh/hpo_train.md @@ -56,7 +56,7 @@ pip install optuna/sigopt/wandb/ray[tune] ... } ``` -Optuna提供了多目标HPO。您可以在`hyperparameter_search`中传递`direction`参数,并定义自己的`compute_objective`以返回多个目标值。在`hyperparameter_search`中将返回Pareto Front(`List[BestRun]`),您应该参考[test_trainer](https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py)中的测试用例`TrainerHyperParameterMultiObjectOptunaIntegrationTest`。它类似于以下内容: +Optuna提供了多目标HPO。您可以在`hyperparameter_search`中传递`direction`参数,并定义自己的`compute_objective`以返回多个目标值。在`hyperparameter_search`中将返回Pareto Front(`list[BestRun]`),您应该参考[test_trainer](https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py)中的测试用例`TrainerHyperParameterMultiObjectOptunaIntegrationTest`。它类似于以下内容: ```py >>> best_trials = trainer.hyperparameter_search( diff --git a/docs/source/zh/tasks/asr.md b/docs/source/zh/tasks/asr.md index a4fdbd308e4..3b66888bc10 100644 --- a/docs/source/zh/tasks/asr.md +++ b/docs/source/zh/tasks/asr.md @@ -181,7 +181,7 @@ Wav2Vec2 分词器仅训练了大写字符,因此您需要确保文本与分 ... processor: AutoProcessor ... padding: Union[bool, str] = "longest" -... def __call__(self, features: List[Dict[str, Union[List[int], torch.Tensor]]]) -> Dict[str, torch.Tensor]: +... def __call__(self, features: list[dict[str, Union[list[int], torch.Tensor]]]) -> dict[str, torch.Tensor]: ... # split inputs and labels since they have to be of different lengths and need ... # different padding methods ... input_features = [{"input_values": feature["input_values"][0]} for feature in features] diff --git a/examples/flax/question-answering/utils_qa.py b/examples/flax/question-answering/utils_qa.py index f0cc5c26a69..b30322b0071 100644 --- a/examples/flax/question-answering/utils_qa.py +++ b/examples/flax/question-answering/utils_qa.py @@ -47,7 +47,7 @@ def postprocess_qa_predictions( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): @@ -270,7 +270,7 @@ def postprocess_qa_predictions_with_beam_search( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): diff --git a/examples/legacy/seq2seq/seq2seq_trainer.py b/examples/legacy/seq2seq/seq2seq_trainer.py index afdde6614e2..fb430c00c26 100644 --- a/examples/legacy/seq2seq/seq2seq_trainer.py +++ b/examples/legacy/seq2seq/seq2seq_trainer.py @@ -184,7 +184,7 @@ class Seq2SeqTrainer(Trainer): Args: model (:obj:`nn.Module`): The model to evaluate. - inputs (:obj:`Dict[str, Union[torch.Tensor, Any]]`): + inputs (:obj:`dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. The dictionary will be unpacked before being fed to the model. Most models expect the targets under the @@ -193,7 +193,7 @@ class Seq2SeqTrainer(Trainer): Whether or not to return the loss only. Return: - Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: + tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and labels (each being optional). """ inputs = self._prepare_inputs(inputs) diff --git a/examples/legacy/seq2seq/utils.py b/examples/legacy/seq2seq/utils.py index 001300f1869..221b1405aa2 100644 --- a/examples/legacy/seq2seq/utils.py +++ b/examples/legacy/seq2seq/utils.py @@ -530,7 +530,7 @@ def calculate_rouge( on multi sentence summaries (CNN/DM dataset). Returns: - Dict[score: value] if aggregate else defaultdict(list) keyed by rouge_keys + dict[score: value] if aggregate else defaultdict(list) keyed by rouge_keys """ scorer = rouge_scorer.RougeScorer(rouge_keys, use_stemmer=use_stemmer) diff --git a/examples/modular-transformers/configuration_my_new_model.py b/examples/modular-transformers/configuration_my_new_model.py index bdf142d8d3d..4e9b055dcf9 100644 --- a/examples/modular-transformers/configuration_my_new_model.py +++ b/examples/modular-transformers/configuration_my_new_model.py @@ -91,11 +91,11 @@ class MyNewModelConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/examples/modular-transformers/image_processing_new_imgproc_model.py b/examples/modular-transformers/image_processing_new_imgproc_model.py index c8eae17dadc..4614c8cdaa5 100644 --- a/examples/modular-transformers/image_processing_new_imgproc_model.py +++ b/examples/modular-transformers/image_processing_new_imgproc_model.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_new_imgproc_model.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np import torch @@ -57,11 +57,11 @@ class ImgprocModelImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -74,13 +74,13 @@ class ImgprocModelImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -101,7 +101,7 @@ class ImgprocModelImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -113,7 +113,7 @@ class ImgprocModelImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -151,13 +151,13 @@ class ImgprocModelImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -172,7 +172,7 @@ class ImgprocModelImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -185,9 +185,9 @@ class ImgprocModelImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. diff --git a/examples/modular-transformers/modeling_add_function.py b/examples/modular-transformers/modeling_add_function.py index ee52f883e45..fdc768237be 100644 --- a/examples/modular-transformers/modeling_add_function.py +++ b/examples/modular-transformers/modeling_add_function.py @@ -5,7 +5,7 @@ # modular_add_function.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # Note that zamba does not have the `apply_rotary_pos_emb` function! -from typing import Optional, Tuple +from typing import Optional import torch from torch import nn @@ -62,5 +62,5 @@ class TestAttention(nn.Module): def __init__(self): pass - def forward(self) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + def forward(self) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: _ = apply_rotary_pos_emb(1, 1, 1, 1) diff --git a/examples/modular-transformers/modeling_dummy.py b/examples/modular-transformers/modeling_dummy.py index 6ea859ee2f8..0fe4ae497b4 100644 --- a/examples/modular-transformers/modeling_dummy.py +++ b/examples/modular-transformers/modeling_dummy.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_dummy.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -210,12 +210,12 @@ class DummyAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -278,9 +278,9 @@ class DummyDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/examples/modular-transformers/modeling_dummy_bert.py b/examples/modular-transformers/modeling_dummy_bert.py index 5b2c9f9383d..8b2e8aed90b 100644 --- a/examples/modular-transformers/modeling_dummy_bert.py +++ b/examples/modular-transformers/modeling_dummy_bert.py @@ -6,7 +6,7 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import math import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from packaging import version @@ -136,9 +136,9 @@ class DummyBertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -245,9 +245,9 @@ class DummyBertSdpaSelfAttention(DummyBertSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -386,9 +386,9 @@ class DummyBertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -454,9 +454,9 @@ class DummyBertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -532,12 +532,12 @@ class DummyBertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None diff --git a/examples/modular-transformers/modeling_from_uppercase_model.py b/examples/modular-transformers/modeling_from_uppercase_model.py index c7c57ae08ec..98daf0e8079 100644 --- a/examples/modular-transformers/modeling_from_uppercase_model.py +++ b/examples/modular-transformers/modeling_from_uppercase_model.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_from_uppercase_model.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -71,7 +71,7 @@ class FromUppercaseModelAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -153,7 +153,7 @@ class FromUppercaseModelEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` diff --git a/examples/modular-transformers/modeling_multimodal1.py b/examples/modular-transformers/modeling_multimodal1.py index 3d4b01ebd84..ec1a3346c9b 100644 --- a/examples/modular-transformers/modeling_multimodal1.py +++ b/examples/modular-transformers/modeling_multimodal1.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_multimodal1.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -210,12 +210,12 @@ class Multimodal1TextAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -278,9 +278,9 @@ class Multimodal1TextDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/examples/modular-transformers/modeling_multimodal2.py b/examples/modular-transformers/modeling_multimodal2.py index 4ef8b1e6842..69e7e454754 100644 --- a/examples/modular-transformers/modeling_multimodal2.py +++ b/examples/modular-transformers/modeling_multimodal2.py @@ -5,7 +5,7 @@ # modular_multimodal2.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -81,7 +81,7 @@ class Multimodal2VisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -177,7 +177,7 @@ class Multimodal2Attention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -244,7 +244,7 @@ class Multimodal2VisionEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` diff --git a/examples/modular-transformers/modeling_my_new_model2.py b/examples/modular-transformers/modeling_my_new_model2.py index 7a5176138f4..d8e10885ef8 100644 --- a/examples/modular-transformers/modeling_my_new_model2.py +++ b/examples/modular-transformers/modeling_my_new_model2.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_my_new_model2.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -208,12 +208,12 @@ class MyNewModel2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -276,9 +276,9 @@ class MyNewModel2DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -469,7 +469,7 @@ class MyNewModel2Model(MyNewModel2PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, diff --git a/examples/modular-transformers/modeling_new_task_model.py b/examples/modular-transformers/modeling_new_task_model.py index e618c9a1c62..77e4efa172e 100644 --- a/examples/modular-transformers/modeling_new_task_model.py +++ b/examples/modular-transformers/modeling_new_task_model.py @@ -5,7 +5,7 @@ # modular_new_task_model.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 from dataclasses import dataclass -from typing import ClassVar, List, Optional, Tuple, Union +from typing import ClassVar, Optional, Union import torch from torch import nn @@ -88,9 +88,9 @@ class NewTaskModelCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -249,7 +249,7 @@ class NewTaskModelModel(NewTaskModelPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -259,7 +259,7 @@ class NewTaskModelModel(NewTaskModelPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, NewTaskModelModelOutputWithPast]: + ) -> Union[tuple, NewTaskModelModelOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -442,7 +442,7 @@ class NewTaskModelForNewTask(NewTaskModelPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, num_logits_to_keep: int = 0, - ) -> Union[Tuple, NewTaskModelCausalLMOutputWithPast]: + ) -> Union[tuple, NewTaskModelCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/examples/modular-transformers/modeling_roberta.py b/examples/modular-transformers/modeling_roberta.py index 7f2b1e7d71a..e1bd313a424 100644 --- a/examples/modular-transformers/modeling_roberta.py +++ b/examples/modular-transformers/modeling_roberta.py @@ -6,7 +6,7 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import math import os -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -139,9 +139,9 @@ class RobertaSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -248,9 +248,9 @@ class RobertaSdpaSelfAttention(RobertaSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -389,9 +389,9 @@ class RobertaAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -457,9 +457,9 @@ class RobertaLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -535,12 +535,12 @@ class RobertaEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -903,12 +903,12 @@ class RobertaModel(RobertaPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if diff --git a/examples/modular-transformers/modeling_super.py b/examples/modular-transformers/modeling_super.py index 4d3308d115d..fdcfa41d3f6 100644 --- a/examples/modular-transformers/modeling_super.py +++ b/examples/modular-transformers/modeling_super.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_super.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -211,12 +211,12 @@ class SuperAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -279,9 +279,9 @@ class SuperDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/examples/modular-transformers/modeling_switch_function.py b/examples/modular-transformers/modeling_switch_function.py index f07691a223d..d0ec849b949 100644 --- a/examples/modular-transformers/modeling_switch_function.py +++ b/examples/modular-transformers/modeling_switch_function.py @@ -5,7 +5,7 @@ # modular_switch_function.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # Note that llama and cohere have different definitions for rotate_half -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch from torch import nn @@ -123,12 +123,12 @@ class SwitchFunctionAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) diff --git a/examples/modular-transformers/modeling_test_detr.py b/examples/modular-transformers/modeling_test_detr.py index 7a455333304..de1084de727 100644 --- a/examples/modular-transformers/modeling_test_detr.py +++ b/examples/modular-transformers/modeling_test_detr.py @@ -7,7 +7,7 @@ import math import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -43,7 +43,7 @@ class MultiScaleDeformableAttention(nn.Module): self, value: Tensor, value_spatial_shapes: Tensor, - value_spatial_shapes_list: List[Tuple], + value_spatial_shapes_list: list[tuple], level_start_index: Tensor, sampling_locations: Tensor, attention_weights: Tensor, @@ -124,9 +124,9 @@ class TestDetrDecoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -177,12 +177,12 @@ class TestDetrModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None @@ -557,7 +557,7 @@ class TestDetrMultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -1431,7 +1431,7 @@ class TestDetrModel(TestDetrPreTrainedModel): Args: enc_output (Tensor[batch_size, sequence_length, hidden_size]): Output of the encoder. padding_mask (Tensor[batch_size, sequence_length]): Padding mask for `enc_output`. - spatial_shapes (List[Tuple[int, int]]): Spatial shapes of the feature maps. + spatial_shapes (list[tuple[int, int]]): Spatial shapes of the feature maps. Returns: `tuple(torch.FloatTensor)`: A tuple of feature map and bbox prediction. @@ -1499,7 +1499,7 @@ class TestDetrModel(TestDetrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], TestDetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], TestDetrModelOutput]: r""" Returns: diff --git a/examples/pytorch/3d_parallel_checks.py b/examples/pytorch/3d_parallel_checks.py index cf617dd483a..792a7c8c165 100644 --- a/examples/pytorch/3d_parallel_checks.py +++ b/examples/pytorch/3d_parallel_checks.py @@ -33,7 +33,7 @@ import logging import os from collections.abc import Iterable from contextlib import nullcontext -from typing import Dict, Optional +from typing import Optional import torch import torch.distributed as dist @@ -589,7 +589,7 @@ class ContextParallelCollator: def __init__(self, cp_mesh: Optional[DeviceMesh] = None): self.cp_mesh = cp_mesh - def __call__(self, batch: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: + def __call__(self, batch: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: batch = default_collate(batch) if self.cp_mesh is not None and self.cp_mesh.size() > 1: # Get sequence length from the input batch diff --git a/examples/pytorch/object-detection/run_object_detection.py b/examples/pytorch/object-detection/run_object_detection.py index d5e128de88f..3f0e8fea4bd 100644 --- a/examples/pytorch/object-detection/run_object_detection.py +++ b/examples/pytorch/object-detection/run_object_detection.py @@ -66,9 +66,9 @@ def format_image_annotations_as_coco( Args: image_id (str): image id. e.g. "0001" - categories (List[int]): list of categories/class labels corresponding to provided bounding boxes - areas (List[float]): list of corresponding areas to provided bounding boxes - bboxes (List[Tuple[float]]): list of bounding boxes provided in COCO format + categories (list[int]): list of categories/class labels corresponding to provided bounding boxes + areas (list[float]): list of corresponding areas to provided bounding boxes + bboxes (list[tuple[float]]): list of bounding boxes provided in COCO format ([center_x, center_y, width, height] in absolute coordinates) Returns: @@ -101,7 +101,7 @@ def convert_bbox_yolo_to_pascal(boxes: torch.Tensor, image_size: tuple[int, int] Args: boxes (torch.Tensor): Bounding boxes in YOLO format - image_size (Tuple[int, int]): Image size in format (height, width) + image_size (tuple[int, int]): Image size in format (height, width) Returns: torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max) diff --git a/examples/pytorch/object-detection/run_object_detection_no_trainer.py b/examples/pytorch/object-detection/run_object_detection_no_trainer.py index e95eda17653..fe60ebaa847 100644 --- a/examples/pytorch/object-detection/run_object_detection_no_trainer.py +++ b/examples/pytorch/object-detection/run_object_detection_no_trainer.py @@ -67,9 +67,9 @@ def format_image_annotations_as_coco( Args: image_id (str): image id. e.g. "0001" - categories (List[int]): list of categories/class labels corresponding to provided bounding boxes - areas (List[float]): list of corresponding areas to provided bounding boxes - bboxes (List[Tuple[float]]): list of bounding boxes provided in COCO format + categories (list[int]): list of categories/class labels corresponding to provided bounding boxes + areas (list[float]): list of corresponding areas to provided bounding boxes + bboxes (list[tuple[float]]): list of bounding boxes provided in COCO format ([center_x, center_y, width, height] in absolute coordinates) Returns: @@ -103,7 +103,7 @@ def convert_bbox_yolo_to_pascal(boxes: torch.Tensor, image_size: tuple[int, int] Args: boxes (torch.Tensor): Bounding boxes in YOLO format - image_size (Tuple[int, int]): Image size in format (height, width) + image_size (tuple[int, int]): Image size in format (height, width) Returns: torch.Tensor: Bounding boxes in Pascal VOC format (x_min, y_min, x_max, y_max) diff --git a/examples/pytorch/question-answering/utils_qa.py b/examples/pytorch/question-answering/utils_qa.py index f0cc5c26a69..b30322b0071 100644 --- a/examples/pytorch/question-answering/utils_qa.py +++ b/examples/pytorch/question-answering/utils_qa.py @@ -47,7 +47,7 @@ def postprocess_qa_predictions( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): @@ -270,7 +270,7 @@ def postprocess_qa_predictions_with_beam_search( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): diff --git a/examples/tensorflow/question-answering/utils_qa.py b/examples/tensorflow/question-answering/utils_qa.py index f0cc5c26a69..b30322b0071 100644 --- a/examples/tensorflow/question-answering/utils_qa.py +++ b/examples/tensorflow/question-answering/utils_qa.py @@ -47,7 +47,7 @@ def postprocess_qa_predictions( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): @@ -270,7 +270,7 @@ def postprocess_qa_predictions_with_beam_search( Args: examples: The non-preprocessed dataset (see the main script for more information). features: The processed dataset (see the main script for more information). - predictions (:obj:`Tuple[np.ndarray, np.ndarray]`): + predictions (:obj:`tuple[np.ndarray, np.ndarray]`): The predictions of the model: two arrays containing the start logits and the end logits respectively. Its first dimension must match the number of elements of :obj:`features`. version_2_with_negative (:obj:`bool`, `optional`, defaults to :obj:`False`): diff --git a/pyproject.toml b/pyproject.toml index f49cb1cc738..af22cfe9c62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,8 @@ line-length = 119 ignore = ["C901", "E501", "E741", "F402", "F823" ] # RUF013: Checks for the use of implicit Optional # in type annotations when the default parameter value is None. -select = ["C", "E", "F", "I", "W", "RUF013"] +select = ["C", "E", "F", "I", "W", "RUF013", "UP006"] +extend-safe-fixes = ["UP006"] # Ignore import violations in all `__init__.py` files. [tool.ruff.lint.per-file-ignores] diff --git a/src/transformers/audio_utils.py b/src/transformers/audio_utils.py index e980d4cbef5..40a1a3963d6 100644 --- a/src/transformers/audio_utils.py +++ b/src/transformers/audio_utils.py @@ -19,7 +19,7 @@ and remove unnecessary dependencies. import os import warnings from io import BytesIO -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import requests @@ -70,7 +70,7 @@ def load_audio(audio: Union[str, np.ndarray], sampling_rate=16000, timeout=None) AudioInput = Union[ - np.ndarray, "torch.Tensor", List[np.ndarray], Tuple[np.ndarray], List["torch.Tensor"], Tuple["torch.Tensor"] # noqa: F821 + np.ndarray, "torch.Tensor", list[np.ndarray], tuple[np.ndarray], list["torch.Tensor"], tuple["torch.Tensor"] # noqa: F821 ] @@ -88,7 +88,7 @@ def make_list_of_audio( """ Ensure that the output is a list of audio. Args: - audio (`Union[List[AudioInput], AudioInput]`): + audio (`Union[list[AudioInput], AudioInput]`): The input audio. Returns: list: A list of audio. @@ -246,7 +246,7 @@ def chroma_filter_bank( Tuning deviation from A440 in fractions of a chroma bin. power (`float`, *optional*, defaults to 2.0): If 12.0, normalizes each column with their L2 norm. If 1.0, normalizes each column with their L1 norm. - weighting_parameters (`Tuple[float, float]`, *optional*, defaults to `(5., 2.)`): + weighting_parameters (`tuple[float, float]`, *optional*, defaults to `(5., 2.)`): If specified, apply a Gaussian weighting parameterized by the first element of the tuple being the center and the second element being the Gaussian half-width. start_at_c_chroma (`float`, *optional*, defaults to `True`): @@ -733,7 +733,7 @@ def spectrogram_batch( Note: This function is designed for efficient batch processing of multiple waveforms but retains compatibility with individual waveform processing methods like `librosa.stft`. Args: - waveform_list (`List[np.ndarray]` with arrays of shape `(length,)`): + waveform_list (`list[np.ndarray]` with arrays of shape `(length,)`): The list of input waveforms, each a single-channel (mono) signal. window (`np.ndarray` of shape `(frame_length,)`): The windowing function to apply, including zero-padding if necessary. @@ -775,7 +775,7 @@ def spectrogram_batch( Data type of the output spectrogram. Returns: - List[`np.ndarray`]: A list of spectrogram arrays, one for each input waveform. + list[`np.ndarray`]: A list of spectrogram arrays, one for each input waveform. """ window_length = len(window) diff --git a/src/transformers/cache_utils.py b/src/transformers/cache_utils.py index 453d4a44bbd..10a86893888 100644 --- a/src/transformers/cache_utils.py +++ b/src/transformers/cache_utils.py @@ -4,7 +4,7 @@ import json import os from collections.abc import Iterable from dataclasses import dataclass -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch from packaging import version @@ -28,7 +28,7 @@ def _static_cache_update( key_states: torch.Tensor, value_states: torch.Tensor, cache_position: Optional[torch.LongTensor], -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the static cache tensors in place. @@ -41,7 +41,7 @@ def _static_cache_update( If None, the entire cache is overwritten (prefill). Returns: - Tuple[`torch.Tensor`, `torch.Tensor`]: The updated key and value cache tensors (modified in-place). + tuple[`torch.Tensor`, `torch.Tensor`]: The updated key and value cache tensors (modified in-place). """ if cache_position is None: # Prefill phase where seq_len potentially equals max_cache_len. Directly copy. @@ -67,7 +67,7 @@ def _sliding_cache_update( value_states: torch.Tensor, cache_position: torch.LongTensor, max_cache_len: int, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the sliding window cache tensors, returning the potentially modified tensors. @@ -80,7 +80,7 @@ def _sliding_cache_update( max_cache_len (`int`): The maximum length of the sliding window cache. Returns: - Tuple[`torch.Tensor`, `torch.Tensor`]: The key and value tensors representing the cache state after the update. + tuple[`torch.Tensor`, `torch.Tensor`]: The key and value tensors representing the cache state after the update. For prefill > window, these are the full input states. Otherwise, they are the updated cache tensors. """ @@ -134,8 +134,8 @@ class Cache: key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. @@ -146,7 +146,7 @@ class Cache: The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. These are specific to each subclass and allow new types of cache to be created. @@ -222,7 +222,7 @@ class CacheConfig: """ Constructs a CacheConfig instance from a dictionary of parameters. Args: - config_dict (Dict[str, Any]): Dictionary containing configuration parameters. + config_dict (dict[str, Any]): Dictionary containing configuration parameters. **kwargs: Additional keyword arguments to override dictionary values. Returns: @@ -257,10 +257,10 @@ class CacheConfig: writer.write(json_string) # Copied from transformers.utils.quantization_config.QuantizationConfigMixin.to_dict - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ return copy.deepcopy(self.__dict__) @@ -289,11 +289,11 @@ class CacheConfig: returning all the unused kwargs. Args: - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Dictionary of attributes to tentatively update this class. Returns: - `Dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. + `dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. """ to_remove = [] for key, value in kwargs.items(): @@ -473,8 +473,8 @@ class DynamicCache(Cache): def __init__(self, _distributed_cache_data: Optional[Iterable] = None) -> None: super().__init__() self._seen_tokens = 0 # Used in `generate` to keep tally of how many tokens the cache has seen - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] # `_distributed_cache_data` was originally added for compatibility with `torch.distributed` (DDP). See #36121 # and #36373 for more information. In a nutshell, it is `map(gather_map, zip(*caches))`, i.e. each item in the @@ -487,7 +487,7 @@ class DynamicCache(Cache): self.key_cache.append(key_states) self.value_cache.append(value_states) - def __getitem__(self, layer_idx: int) -> Tuple[torch.Tensor, torch.Tensor]: + def __getitem__(self, layer_idx: int) -> tuple[torch.Tensor, torch.Tensor]: """ Support for backwards-compatible `past_key_value` indexing, e.g. `past_key_value[0][0].shape[2]` to get the sequence length. @@ -517,8 +517,8 @@ class DynamicCache(Cache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. @@ -529,7 +529,7 @@ class DynamicCache(Cache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. No additional arguments are used in `DynamicCache`. Return: @@ -574,7 +574,7 @@ class DynamicCache(Cache): """Returns the maximum sequence length of the cache object. DynamicCache does not have a maximum length.""" return None - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor, torch.Tensor]]: """Converts the `DynamicCache` instance into the its equivalent in the legacy cache format. Used for backward compatibility.""" legacy_cache = () @@ -584,7 +584,7 @@ class DynamicCache(Cache): @classmethod def from_legacy_cache( - cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor, torch.FloatTensor]]] = None + cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor, torch.FloatTensor]]] = None ) -> "DynamicCache": """Converts a cache in the legacy cache format into an equivalent `DynamicCache`. Used for backward compatibility.""" @@ -611,7 +611,7 @@ class DynamicCache(Cache): self.key_cache[idx] = self.key_cache[idx][..., :max_length, :] self.value_cache[idx] = self.value_cache[idx][..., :max_length, :] - def batch_split(self, full_batch_size: int, split_size: int) -> List["DynamicCache"]: + def batch_split(self, full_batch_size: int, split_size: int) -> list["DynamicCache"]: """Split the current instance into a list of `DynamicCache` by the batch size. This will be used by `_split_model_inputs()` in `generation.utils`""" out = [] @@ -624,7 +624,7 @@ class DynamicCache(Cache): return out @classmethod - def from_batch_splits(cls, splits: List["DynamicCache"]) -> "DynamicCache": + def from_batch_splits(cls, splits: list["DynamicCache"]) -> "DynamicCache": """This is the opposite of the above `batch_split()` method. This will be used by `stack_model_outputs` in `generation.utils`""" cache = cls() @@ -762,7 +762,7 @@ class OffloadedCache(DynamicCache): self.key_cache[prev_layer_idx] = self.key_cache[prev_layer_idx].to("cpu", non_blocking=True) self.value_cache[prev_layer_idx] = self.value_cache[prev_layer_idx].to("cpu", non_blocking=True) - def __getitem__(self, layer_idx: int) -> Tuple[torch.Tensor, torch.Tensor]: + def __getitem__(self, layer_idx: int) -> tuple[torch.Tensor, torch.Tensor]: "Gets the cache for this layer to the device. Prefetches the next and evicts the previous layer." if layer_idx < len(self): # Evict the previous layer if necessary @@ -799,8 +799,8 @@ class OffloadedCache(DynamicCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. Parameters: @@ -810,7 +810,7 @@ class OffloadedCache(DynamicCache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. No additional arguments are used in `OffloadedCache`. Return: A tuple containing the updated key and value states. @@ -857,8 +857,8 @@ class QuantizedCache(DynamicCache): def __init__(self, cache_config: QuantizedCacheConfig) -> None: super().__init__() - self._quantized_key_cache: List[torch.Tensor] = [] - self._quantized_value_cache: List[torch.Tensor] = [] + self._quantized_key_cache: list[torch.Tensor] = [] + self._quantized_value_cache: list[torch.Tensor] = [] self.nbits = cache_config.nbits self.residual_length = cache_config.residual_length @@ -875,8 +875,8 @@ class QuantizedCache(DynamicCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: # Update the number of seen tokens if layer_idx == 0: self._seen_tokens += key_states.shape[-2] @@ -1094,7 +1094,7 @@ class StaticCache(Cache): should pass the `layer_device_map` argument instead. dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The default `dtype` to use when initializing the layer. - layer_device_map (`Optional[Dict[int, Union[str, torch.device, int]]]]`, *optional*): + layer_device_map (`Optional[dict[int, Union[str, torch.device, int]]]]`, *optional*): Mapping between the layers and its device. This is required when you are manually initializing the cache and the model is split between different gpus. You can know which layers mapped to which device by checking the associated device_map: `model.hf_device_map`. @@ -1129,7 +1129,7 @@ class StaticCache(Cache): max_cache_len: Optional[int] = None, device: Union[torch.device, str, None] = None, dtype: torch.dtype = torch.float32, - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ) -> None: super().__init__() self.max_batch_size = max_batch_size @@ -1145,8 +1145,8 @@ class StaticCache(Cache): else config.num_key_value_heads ) - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] # Note: There will be significant perf decrease if switching to use 5D tensors instead. cache_shape = (self.max_batch_size, self.num_key_value_heads, self.max_cache_len, self.head_dim) device = torch.device(device) if device is not None else None @@ -1169,8 +1169,8 @@ class StaticCache(Cache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. It is VERY important to index using a tensor, otherwise you introduce a copy to the device. @@ -1182,7 +1182,7 @@ class StaticCache(Cache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. The `StaticCache` needs the `cache_position` input to know how where to write in the cache. @@ -1260,7 +1260,7 @@ class SlidingWindowCache(StaticCache): should pass the `layer_device_map` argument instead. dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The default `dtype` to use when initializing the layer. - layer_device_map (`Optional[Dict[int, Union[str, torch.device, int]]]]`, *optional*): + layer_device_map (`Optional[dict[int, Union[str, torch.device, int]]]]`, *optional*): Mapping between the layers and its device. This is required when you are manually initializing the cache and the model is split between different gpus. You can know which layers mapped to which device by checking the associated device_map: `model.hf_device_map`. @@ -1294,7 +1294,7 @@ class SlidingWindowCache(StaticCache): max_cache_len: Optional[int] = None, device: Union[torch.device, str, None] = None, dtype: torch.dtype = torch.float32, - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ) -> None: if not hasattr(config, "sliding_window") or config.sliding_window is None: raise ValueError( @@ -1318,8 +1318,8 @@ class SlidingWindowCache(StaticCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: if cache_kwargs is None: cache_kwargs = {} cache_position = cache_kwargs.get("cache_position") @@ -1400,7 +1400,7 @@ class EncoderDecoderCache(Cache): for layer_idx in range(len(cross_attention_cache.key_cache)): self.is_updated[layer_idx] = bool(cross_attention_cache.get_seq_length(layer_idx) > 0) - def __getitem__(self, layer_idx: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: + def __getitem__(self, layer_idx: int) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: """ Support for backwards-compatible `past_key_value` indexing, e.g. `past_key_value[0][0].shape[2]` to get the sequence length. @@ -1422,7 +1422,7 @@ class EncoderDecoderCache(Cache): """ return len(self.self_attention_cache) - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor]]: """Converts the `EncoderDecoderCache` instance into its equivalent in the legacy cache format.""" legacy_cache = () if len(self.cross_attention_cache) > 0: @@ -1436,7 +1436,7 @@ class EncoderDecoderCache(Cache): @classmethod def from_legacy_cache( - cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None ) -> "EncoderDecoderCache": """Converts a cache in the legacy cache format into an equivalent `EncoderDecoderCache`.""" cache = cls( @@ -1495,7 +1495,7 @@ class EncoderDecoderCache(Cache): self.check_dynamic_cache(self.crop.__name__) self.self_attention_cache.crop(maximum_length) - def batch_split(self, full_batch_size: int, split_size: int) -> "List[EncoderDecoderCache]": + def batch_split(self, full_batch_size: int, split_size: int) -> "list[EncoderDecoderCache]": """Split the current instance into a list of `DynamicCache` by the batch size. This will be used by `_split_model_inputs()` in `generation.utils`""" self.check_dynamic_cache(self.batch_split.__name__) @@ -1508,7 +1508,7 @@ class EncoderDecoderCache(Cache): return out @classmethod - def from_batch_splits(cls, splits: List["EncoderDecoderCache"]) -> "EncoderDecoderCache": + def from_batch_splits(cls, splits: list["EncoderDecoderCache"]) -> "EncoderDecoderCache": """This is the opposite of the above `batch_split()` method. This will be used by `stack_model_outputs` in `generation.utils`""" self_attention_cache = DynamicCache() @@ -1569,7 +1569,7 @@ class HybridCache(Cache): should pass the `layer_device_map` argument instead. dtype (torch.dtype, *optional*, defaults to `torch.float32`): The default `dtype` to use when initializing the layer. - layer_device_map (`Optional[Dict[int, Union[str, torch.device, int]]]]`, *optional*): + layer_device_map (`Optional[dict[int, Union[str, torch.device, int]]]]`, *optional*): Mapping between the layers and its device. This is required when you are manually initializing the cache and the model is split between different gpus. You can know which layers mapped to which device by checking the associated device_map: `model.hf_device_map`. @@ -1603,7 +1603,7 @@ class HybridCache(Cache): max_cache_len: Optional[int] = None, device: Union[torch.device, str, None] = None, dtype: torch.dtype = torch.float32, - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ) -> None: super().__init__() if not hasattr(config, "sliding_window") or config.sliding_window is None: @@ -1634,8 +1634,8 @@ class HybridCache(Cache): else: self.is_sliding = [False] * config.num_hidden_layers - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] global_cache_shape = (self.max_batch_size, self.num_key_value_heads, self.max_cache_len, self.head_dim) sliding_cache_shape = (self.max_batch_size, self.num_key_value_heads, self.sliding_window_len, self.head_dim) self.sliding_window = min(config.sliding_window, max_cache_len) @@ -1660,8 +1660,8 @@ class HybridCache(Cache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: if cache_kwargs is None: cache_kwargs = {} cache_position = cache_kwargs.get("cache_position") @@ -1757,7 +1757,7 @@ class HybridChunkedCache(Cache): should pass the `layer_device_map` argument instead. dtype (torch.dtype, *optional*, defaults to `torch.bfloat16`): The default `dtype` to use when initializing the layer. - layer_device_map (`Optional[Dict[int, Union[str, torch.device, int]]]]`, *optional*): + layer_device_map (`Optional[dict[int, Union[str, torch.device, int]]]]`, *optional*): Mapping between the layers and its device. This is required when you are manually initializing the cache and the model is split between different gpus. You can know which layers mapped to which device by checking the associated device_map: `model.hf_device_map`. @@ -1791,7 +1791,7 @@ class HybridChunkedCache(Cache): max_cache_len: Optional[int] = None, device: Union[torch.device, str, None] = None, dtype: torch.dtype = torch.bfloat16, - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ) -> None: super().__init__() if not hasattr(config, "sliding_window") or config.sliding_window is None: @@ -1811,8 +1811,8 @@ class HybridChunkedCache(Cache): else: self.is_sliding = [False] * config.num_hidden_layers - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] self.cumulative_length = [0 for _ in range(config.num_hidden_layers)] def initialise_cache_layer(self, layer_idx, key_states): @@ -1880,8 +1880,8 @@ class HybridChunkedCache(Cache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: if cache_kwargs is None: cache_kwargs = {} cache_position = cache_kwargs.get("cache_position") @@ -1968,7 +1968,7 @@ class OffloadedHybridCache(HybridChunkedCache): device: Union[torch.device, str, None] = None, dtype: torch.dtype = torch.bfloat16, offload_device: Union[str, torch.device] = torch.device("cpu"), - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ): super().__init__(config, max_batch_size, max_cache_len, device, dtype, layer_device_map) @@ -2121,8 +2121,8 @@ class MambaCache: self.ssm_state_size = config.state_size self.conv_kernel_size = config.conv_kernel - self.conv_states: List[torch.Tensor] = [] - self.ssm_states: List[torch.Tensor] = [] + self.conv_states: list[torch.Tensor] = [] + self.ssm_states: list[torch.Tensor] = [] device = torch.device(device) if device is not None else None for _ in range(config.num_hidden_layers): conv_state: torch.Tensor = torch.zeros( @@ -2193,7 +2193,7 @@ class OffloadedStaticCache(StaticCache): The default `dtype` to use when initializing the cache. offload_device (`Union[str, torch.device]`, *optional*, defaults to `cpu`): The device to offload to. Defaults to CPU. - layer_device_map (`Dict[int, Union[str, torch.device, int]]`, *optional*): + layer_device_map (`dict[int, Union[str, torch.device, int]]`, *optional*): Mapping between the layers and its device. This is required when you are manually initializing the cache and the model is split between different gpus. You can know which layers mapped to which device by checking the associated device_map: `model.hf_device_map`. @@ -2227,7 +2227,7 @@ class OffloadedStaticCache(StaticCache): device: Union[str, torch.device], dtype: Optional[torch.dtype] = None, offload_device: Union[str, torch.device] = torch.device("cpu"), - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, ) -> None: super(Cache, self).__init__() @@ -2255,8 +2255,8 @@ class OffloadedStaticCache(StaticCache): cache_shape = (max_batch_size, num_key_value_heads, self.max_cache_len, head_dim) # Create offloaded CPU tensors. - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] for i in range(config.num_hidden_layers): # First layer is always on-device. @@ -2268,8 +2268,8 @@ class OffloadedStaticCache(StaticCache): self.value_cache.append(value_cache) # Create device tensors. - self._device_key_cache: List[torch.Tensor] = [] - self._device_value_cache: List[torch.Tensor] = [] + self._device_key_cache: list[torch.Tensor] = [] + self._device_value_cache: list[torch.Tensor] = [] for i in range(2): key_cache, value_cache = self._create_key_value_cache_tensors(cache_shape, self.device) @@ -2289,8 +2289,8 @@ class OffloadedStaticCache(StaticCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. It is VERY important to index using a tensor, otherwise you introduce a copy to the device. @@ -2302,7 +2302,7 @@ class OffloadedStaticCache(StaticCache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, *optional*): + cache_kwargs (`dict[str, Any]`, *optional*): Additional arguments for the cache subclass. The `OffloadedStaticCache` needs the `cache_position` input to know how where to write in the cache. @@ -2401,13 +2401,13 @@ class OffloadedStaticCache(StaticCache): return self._seen_tokens def _create_key_value_cache_tensors( - self, shape: Tuple[int, ...], device: torch.device - ) -> Tuple[torch.Tensor, torch.Tensor]: + self, shape: tuple[int, ...], device: torch.device + ) -> tuple[torch.Tensor, torch.Tensor]: """Creates K/V cache tensors on a device. Pins memory for CPU tensors. Marks them as static addresses for non-CPU tensors. Args: - shape (`Tuple[int, ...]`): Shape. + shape (`tuple[int, ...]`): Shape. device (`torch.device`): Device. Returns: diff --git a/src/transformers/commands/add_new_model_like.py b/src/transformers/commands/add_new_model_like.py index e6ce398290a..9e10f9e37c7 100644 --- a/src/transformers/commands/add_new_model_like.py +++ b/src/transformers/commands/add_new_model_like.py @@ -23,7 +23,7 @@ from datetime import date from itertools import chain from pathlib import Path from re import Pattern -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import yaml @@ -148,7 +148,7 @@ def find_indent(line: str) -> int: return len(search.groups()[0]) -def parse_module_content(content: str) -> List[str]: +def parse_module_content(content: str) -> list[str]: """ Parse the content of a module in the list of objects it defines. @@ -156,7 +156,7 @@ def parse_module_content(content: str) -> List[str]: content (`str`): The content to parse Returns: - `List[str]`: The list of objects defined in the module. + `list[str]`: The list of objects defined in the module. """ objects = [] current_object = [] @@ -336,7 +336,7 @@ def add_content_to_file( def replace_model_patterns( text: str, old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns -) -> Tuple[str, str]: +) -> tuple[str, str]: """ Replace all patterns present in a given text. @@ -414,10 +414,10 @@ def simplify_replacements(replacements): "BertConfig->BertNewConfig" is implied by "Bert->BertNew" so not needed. Args: - replacements (`List[Tuple[str, str]]`): List of patterns (old, new) + replacements (`list[tuple[str, str]]`): List of patterns (old, new) Returns: - `List[Tuple[str, str]]`: The list of patterns simplified. + `list[tuple[str, str]]`: The list of patterns simplified. """ if len(replacements) <= 1: # Nothing to simplify @@ -519,7 +519,7 @@ def duplicate_module( new_model_patterns: ModelPatterns, dest_file: Optional[str] = None, add_copied_from: bool = True, - attrs_to_remove: Optional[List[str]] = None, + attrs_to_remove: Optional[list[str]] = None, ): """ Create a new module from an existing one and adapting all function and classes names from old patterns to new ones. @@ -585,17 +585,17 @@ def duplicate_module( def filter_framework_files( - files: List[Union[str, os.PathLike]], frameworks: Optional[List[str]] = None -) -> List[Union[str, os.PathLike]]: + files: list[Union[str, os.PathLike]], frameworks: Optional[list[str]] = None +) -> list[Union[str, os.PathLike]]: """ Filter a list of files to only keep the ones corresponding to a list of frameworks. Args: - files (`List[Union[str, os.PathLike]]`): The list of files to filter. - frameworks (`List[str]`, *optional*): The list of allowed frameworks. + files (`list[Union[str, os.PathLike]]`): The list of files to filter. + frameworks (`list[str]`, *optional*): The list of allowed frameworks. Returns: - `List[Union[str, os.PathLike]]`: The list of filtered files. + `list[Union[str, os.PathLike]]`: The list of filtered files. """ if frameworks is None: frameworks = get_default_frameworks() @@ -617,17 +617,17 @@ def filter_framework_files( return [framework_to_file[f] for f in frameworks if f in framework_to_file] + others -def get_model_files(model_type: str, frameworks: Optional[List[str]] = None) -> Dict[str, Union[Path, List[Path]]]: +def get_model_files(model_type: str, frameworks: Optional[list[str]] = None) -> dict[str, Union[Path, list[Path]]]: """ Retrieves all the files associated to a model. Args: model_type (`str`): A valid model type (like "bert" or "gpt2") - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If passed, will only keep the model files corresponding to the passed frameworks. Returns: - `Dict[str, Union[Path, List[Path]]]`: A dictionary with the following keys: + `dict[str, Union[Path, list[Path]]]`: A dictionary with the following keys: - **doc_file** -- The documentation file for the model. - **model_files** -- All the files in the model module. - **test_files** -- The test files for the model. @@ -663,14 +663,14 @@ _re_checkpoint_for_doc = re.compile(r"^_CHECKPOINT_FOR_DOC\s+=\s+(\S*)\s*$", fla def find_base_model_checkpoint( - model_type: str, model_files: Optional[Dict[str, Union[Path, List[Path]]]] = None + model_type: str, model_files: Optional[dict[str, Union[Path, list[Path]]]] = None ) -> str: """ Finds the model checkpoint used in the docstrings for a given model. Args: model_type (`str`): A valid model type (like "bert" or "gpt2") - model_files (`Dict[str, Union[Path, List[Path]]`, *optional*): + model_files (`dict[str, Union[Path, list[Path]]`, *optional*): The files associated to `model_type`. Can be passed to speed up the function, otherwise will be computed. Returns: @@ -713,18 +713,18 @@ def get_default_frameworks(): _re_model_mapping = re.compile("MODEL_([A-Z_]*)MAPPING_NAMES") -def retrieve_model_classes(model_type: str, frameworks: Optional[List[str]] = None) -> Dict[str, List[str]]: +def retrieve_model_classes(model_type: str, frameworks: Optional[list[str]] = None) -> dict[str, list[str]]: """ Retrieve the model classes associated to a given model. Args: model_type (`str`): A valid model type (like "bert" or "gpt2") - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): The frameworks to look for. Will default to `["pt", "tf", "flax"]`, passing a smaller list will restrict the classes returned. Returns: - `Dict[str, List[str]]`: A dictionary with one key per framework and the list of model classes associated to + `dict[str, list[str]]`: A dictionary with one key per framework and the list of model classes associated to that framework as values. """ if frameworks is None: @@ -754,20 +754,20 @@ def retrieve_model_classes(model_type: str, frameworks: Optional[List[str]] = No return model_classes -def retrieve_info_for_model(model_type, frameworks: Optional[List[str]] = None): +def retrieve_info_for_model(model_type, frameworks: Optional[list[str]] = None): """ Retrieves all the information from a given model_type. Args: model_type (`str`): A valid model type (like "bert" or "gpt2") - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If passed, will only keep the info corresponding to the passed frameworks. Returns: `Dict`: A dictionary with the following keys: - - **frameworks** (`List[str]`): The list of frameworks that back this model type. - - **model_classes** (`Dict[str, List[str]]`): The model classes implemented for that model type. - - **model_files** (`Dict[str, Union[Path, List[Path]]]`): The files associated with that model type. + - **frameworks** (`list[str]`): The list of frameworks that back this model type. + - **model_classes** (`dict[str, list[str]]`): The model classes implemented for that model type. + - **model_files** (`dict[str, Union[Path, list[Path]]]`): The files associated with that model type. - **model_patterns** (`ModelPatterns`): The various patterns for the model. """ if model_type not in auto_module.MODEL_NAMES_MAPPING: @@ -833,7 +833,7 @@ def retrieve_info_for_model(model_type, frameworks: Optional[List[str]] = None): def clean_frameworks_in_init( - init_file: Union[str, os.PathLike], frameworks: Optional[List[str]] = None, keep_processing: bool = True + init_file: Union[str, os.PathLike], frameworks: Optional[list[str]] = None, keep_processing: bool = True ): """ Removes all the import lines that don't belong to a given list of frameworks or concern tokenizers/feature @@ -841,7 +841,7 @@ def clean_frameworks_in_init( Args: init_file (`str` or `os.PathLike`): The path to the init to treat. - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If passed, this will remove all imports that are subject to a framework not in frameworks keep_processing (`bool`, *optional*, defaults to `True`): Whether or not to keep the preprocessing (tokenizer, feature extractor, image processor, processor) imports @@ -914,7 +914,7 @@ def clean_frameworks_in_init( def add_model_to_main_init( old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns, - frameworks: Optional[List[str]] = None, + frameworks: Optional[list[str]] = None, with_processing: bool = True, ): """ @@ -923,7 +923,7 @@ def add_model_to_main_init( Args: old_model_patterns (`ModelPatterns`): The patterns for the old model. new_model_patterns (`ModelPatterns`): The patterns for the new model. - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If specified, only the models implemented in those frameworks will be added. with_processing (`bool`, *optional*, defaults to `True`): Whether the tokenizer/feature extractor/processor of the model should also be added to the init or not. @@ -1068,7 +1068,7 @@ AUTO_CLASSES_PATTERNS = { def add_model_to_auto_classes( - old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns, model_classes: Dict[str, List[str]] + old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns, model_classes: dict[str, list[str]] ): """ Add a model to the relevant mappings in the auto module. @@ -1076,7 +1076,7 @@ def add_model_to_auto_classes( Args: old_model_patterns (`ModelPatterns`): The patterns for the old model. new_model_patterns (`ModelPatterns`): The patterns for the new model. - model_classes (`Dict[str, List[str]]`): A dictionary framework to list of model classes implemented. + model_classes (`dict[str, list[str]]`): A dictionary framework to list of model classes implemented. """ for filename in AUTO_CLASSES_PATTERNS: # Extend patterns with all model classes if necessary @@ -1169,7 +1169,7 @@ def duplicate_doc_file( old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns, dest_file: Optional[Union[str, os.PathLike]] = None, - frameworks: Optional[List[str]] = None, + frameworks: Optional[list[str]] = None, ): """ Duplicate a documentation file and adapts it for a new model. @@ -1180,7 +1180,7 @@ def duplicate_doc_file( new_model_patterns (`ModelPatterns`): The patterns for the new model. dest_file (`str` or `os.PathLike`, *optional*): Path to the new doc file. Will default to the a file named `{new_model_patterns.model_type}.md` in the same folder as `module_file`. - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If passed, will only keep the model classes corresponding to this list of frameworks in the new doc file. """ with open(doc_file, "r", encoding="utf-8") as f: @@ -1320,7 +1320,7 @@ def create_new_model_like( model_type: str, new_model_patterns: ModelPatterns, add_copied_from: bool = True, - frameworks: Optional[List[str]] = None, + frameworks: Optional[list[str]] = None, old_checkpoint: Optional[str] = None, create_fast_image_processor: bool = False, ): @@ -1332,7 +1332,7 @@ def create_new_model_like( new_model_patterns (`ModelPatterns`): The patterns for the new model. add_copied_from (`bool`, *optional*, defaults to `True`): Whether or not to add "Copied from" statements to all classes in the new model modeling files. - frameworks (`List[str]`, *optional*): + frameworks (`list[str]`, *optional*): If passed, will limit the duplicate to the frameworks specified. old_checkpoint (`str`, *optional*): The name of the base checkpoint for the old model. Should be passed along when it can't be automatically diff --git a/src/transformers/commands/serving.py b/src/transformers/commands/serving.py index 212a4e8710b..2329f3b7501 100644 --- a/src/transformers/commands/serving.py +++ b/src/transformers/commands/serving.py @@ -13,7 +13,7 @@ # limitations under the License. from argparse import ArgumentParser, Namespace -from typing import Any, List, Optional +from typing import Any, Optional from ..pipelines import Pipeline, get_supported_tasks, pipeline from ..utils import logging @@ -69,8 +69,8 @@ class ServeTokenizeResult(BaseModel): Tokenize result model """ - tokens: List[str] - tokens_ids: Optional[List[int]] + tokens: list[str] + tokens_ids: Optional[list[int]] class ServeDeTokenizeResult(BaseModel): @@ -196,7 +196,7 @@ class ServeCommand(BaseTransformersCLICommand): def detokenize( self, - tokens_ids: List[int] = Body(None, embed=True), + tokens_ids: list[int] = Body(None, embed=True), skip_special_tokens: bool = Body(False, embed=True), cleanup_tokenization_spaces: bool = Body(True, embed=True), ): diff --git a/src/transformers/configuration_utils.py b/src/transformers/configuration_utils.py index 74bf8cca488..46f1b14414f 100755 --- a/src/transformers/configuration_utils.py +++ b/src/transformers/configuration_utils.py @@ -63,13 +63,13 @@ class PretrainedConfig(PushToHubMixin): Some configurations requires inputs to be defined at init and have no default values, usually these are composite configs, (but not necessarily) such as [`~transformers.EncoderDecoderConfig`] or [`~RagConfig`]. They have to be initialized from two or more configs of type [`~transformers.PretrainedConfig`]. - - **keys_to_ignore_at_inference** (`List[str]`) -- A list of keys to ignore by default when looking at dictionary + - **keys_to_ignore_at_inference** (`list[str]`) -- A list of keys to ignore by default when looking at dictionary outputs of the model during inference. - - **attribute_map** (`Dict[str, str]`) -- A dict that maps model specific attribute names to the standardized + - **attribute_map** (`dict[str, str]`) -- A dict that maps model specific attribute names to the standardized naming of attributes. - - **base_model_tp_plan** (`Dict[str, Any]`) -- A dict that maps sub-modules FQNs of a base model to a tensor + - **base_model_tp_plan** (`dict[str, Any]`) -- A dict that maps sub-modules FQNs of a base model to a tensor parallel plan applied to the sub-module when `model.tensor_parallel` is called. - - **base_model_pp_plan** (`Dict[str, Tuple[List[str]]]`) -- A dict that maps child-modules of a base model to a + - **base_model_pp_plan** (`dict[str, tuple[list[str]]]`) -- A dict that maps child-modules of a base model to a pipeline parallel plan that enables users to place the child-module on the appropriate device. Common attributes (present in all subclasses): @@ -115,7 +115,7 @@ class PretrainedConfig(PushToHubMixin): tie_encoder_decoder (`bool`, *optional*, defaults to `False`): Whether all encoder weights should be tied to their equivalent decoder weights. This requires the encoder and decoder model to have the exact same parameter names. - prune_heads (`Dict[int, List[int]]`, *optional*, defaults to `{}`): + prune_heads (`dict[int, list[int]]`, *optional*, defaults to `{}`): Pruned heads of the model. The keys are the selected layer indices and the associated values, the list of heads to prune in said layer. @@ -128,17 +128,17 @@ class PretrainedConfig(PushToHubMixin): > Parameters for fine-tuning tasks - architectures (`List[str]`, *optional*): + architectures (`list[str]`, *optional*): Model architectures that can be used with the model pretrained weights. finetuning_task (`str`, *optional*): Name of the task used to fine-tune the model. This can be used when converting from an original (TensorFlow or PyTorch) checkpoint. - id2label (`Dict[int, str]`, *optional*): + id2label (`dict[int, str]`, *optional*): A map from index (for instance prediction index, or target index) to label. - label2id (`Dict[str, int]`, *optional*): A map from label to index for the model. + label2id (`dict[str, int]`, *optional*): A map from label to index for the model. num_labels (`int`, *optional*): Number of labels to use in the last layer added to the model, typically for a classification task. - task_specific_params (`Dict[str, Any]`, *optional*): + task_specific_params (`dict[str, Any]`, *optional*): Additional keyword arguments to store for the current task. problem_type (`str`, *optional*): Problem type for `XxxForSequenceClassification` models. Can be one of `"regression"`, @@ -394,7 +394,7 @@ class PretrainedConfig(PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ self._set_token_in_kwargs(kwargs) @@ -505,7 +505,7 @@ class PretrainedConfig(PushToHubMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -531,7 +531,7 @@ class PretrainedConfig(PushToHubMixin): subfolder (`str`, *optional*, defaults to `""`): In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are configuration attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is controlled by the `return_unused_kwargs` keyword parameter. @@ -599,7 +599,7 @@ class PretrainedConfig(PushToHubMixin): The identifier of the pre-trained checkpoint from which we want the dictionary of parameters. Returns: - `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the configuration object. + `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the configuration object. """ cls._set_token_in_kwargs(kwargs) @@ -723,10 +723,10 @@ class PretrainedConfig(PushToHubMixin): Instantiates a [`PretrainedConfig`] from a Python dictionary of parameters. Args: - config_dict (`Dict[str, Any]`): + config_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [`~PretrainedConfig.get_config_dict`] method. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the configuration object. Returns: @@ -816,7 +816,7 @@ class PretrainedConfig(PushToHubMixin): Python dictionary. Returns: - Dict[str, Any]: Dictionary of all the attributes that make up this configuration instance. + dict[str, Any]: Dictionary of all the attributes that make up this configuration instance. """ config_dict = self.to_dict() @@ -874,7 +874,7 @@ class PretrainedConfig(PushToHubMixin): Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) if hasattr(self.__class__, "model_type"): @@ -940,7 +940,7 @@ class PretrainedConfig(PushToHubMixin): Updates attributes of this class with attributes from `config_dict`. Args: - config_dict (`Dict[str, Any]`): Dictionary of attributes that should be updated for this class. + config_dict (`dict[str, Any]`): Dictionary of attributes that should be updated for this class. """ for key, value in config_dict.items(): setattr(self, key, value) @@ -1163,7 +1163,7 @@ def get_configuration_file(configuration_files: list[str]) -> str: Get the configuration file to use for this version of transformers. Args: - configuration_files (`List[str]`): The list of available configuration files. + configuration_files (`list[str]`): The list of available configuration files. Returns: `str`: The configuration file to use. diff --git a/src/transformers/data/data_collator.py b/src/transformers/data/data_collator.py index 079fb13c2a8..fcefc9270ef 100644 --- a/src/transformers/data/data_collator.py +++ b/src/transformers/data/data_collator.py @@ -18,7 +18,7 @@ import warnings from collections.abc import Mapping from dataclasses import dataclass from random import randint -from typing import Any, Callable, Dict, List, NewType, Optional, Tuple, Union +from typing import Any, Callable, NewType, Optional, Union import numpy as np @@ -33,7 +33,7 @@ InputDataClass = NewType("InputDataClass", Any) A DataCollator is a function that takes a list of samples from a Dataset and collate them into a batch, as a dictionary of PyTorch/TensorFlow tensors or NumPy arrays. """ -DataCollator = NewType("DataCollator", Callable[[List[InputDataClass]], Dict[str, Any]]) +DataCollator = NewType("DataCollator", Callable[[list[InputDataClass]], dict[str, Any]]) class DataCollatorMixin: @@ -72,7 +72,7 @@ def pad_without_fast_tokenizer_warning(tokenizer, *pad_args, **pad_kwargs): return padded -def default_data_collator(features: List[InputDataClass], return_tensors="pt") -> Dict[str, Any]: +def default_data_collator(features: list[InputDataClass], return_tensors="pt") -> dict[str, Any]: """ Very simple data collator that simply collates batches of dict-like objects and performs special handling for potential keys named: @@ -119,13 +119,13 @@ class DefaultDataCollator(DataCollatorMixin): return_tensors: str = "pt" - def __call__(self, features: List[Dict[str, Any]], return_tensors=None) -> Dict[str, Any]: + def __call__(self, features: list[dict[str, Any]], return_tensors=None) -> dict[str, Any]: if return_tensors is None: return_tensors = self.return_tensors return default_data_collator(features, return_tensors) -def torch_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any]: +def torch_default_data_collator(features: list[InputDataClass]) -> dict[str, Any]: import torch if not isinstance(features[0], Mapping): @@ -161,7 +161,7 @@ def torch_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any return batch -def tf_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any]: +def tf_default_data_collator(features: list[InputDataClass]) -> dict[str, Any]: import tensorflow as tf if not isinstance(features[0], Mapping): @@ -202,7 +202,7 @@ def tf_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any]: return batch -def numpy_default_data_collator(features: List[InputDataClass]) -> Dict[str, Any]: +def numpy_default_data_collator(features: list[InputDataClass]) -> dict[str, Any]: if not isinstance(features[0], Mapping): features = [vars(f) for f in features] first = features[0] @@ -268,7 +268,7 @@ class DataCollatorWithPadding: pad_to_multiple_of: Optional[int] = None return_tensors: str = "pt" - def __call__(self, features: List[Dict[str, Any]]) -> Dict[str, Any]: + def __call__(self, features: list[dict[str, Any]]) -> dict[str, Any]: batch = pad_without_fast_tokenizer_warning( self.tokenizer, features, @@ -569,7 +569,7 @@ class DataCollatorForMultipleChoice(DataCollatorMixin): pad_to_multiple_of: Optional[int] = None return_tensors: str = "pt" - def torch_call(self, examples: List[Dict[str, Any]]): # Refactored implementation from the docs. + def torch_call(self, examples: list[dict[str, Any]]): # Refactored implementation from the docs. import torch # Take labels out of the examples beforehand, because they aren't nested. @@ -911,7 +911,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): def tf_mask_tokens( self, inputs: Any, vocab_size, mask_token_id, special_tokens_mask: Optional[Any] = None - ) -> Tuple[Any, Any]: + ) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. """ @@ -956,7 +956,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): # The rest of the time ((1-random_replace_prob-mask_replace_prob)% of the time) we keep the masked input tokens unchanged return inputs, labels - def tf_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def tf_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: import tensorflow as tf if self.seed and self.generator is None: @@ -1002,7 +1002,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): batch["labels"] = labels return batch - def torch_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def torch_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: # Handle dict or lists with proper padding and conversion to tensor. if self.seed and self.generator is None: @@ -1032,7 +1032,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): batch["labels"] = labels return batch - def torch_mask_tokens(self, inputs: Any, special_tokens_mask: Optional[Any] = None) -> Tuple[Any, Any]: + def torch_mask_tokens(self, inputs: Any, special_tokens_mask: Optional[Any] = None) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. """ @@ -1081,7 +1081,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): # The rest of the time ((1-random_replace_prob-mask_replace_prob)% of the time) we keep the masked input tokens unchanged return inputs, labels - def numpy_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def numpy_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: # Handle dict or lists with proper padding and conversion to tensor. if self.seed and self.generator is None: @@ -1111,7 +1111,7 @@ class DataCollatorForLanguageModeling(DataCollatorMixin): batch["labels"] = labels return batch - def numpy_mask_tokens(self, inputs: Any, special_tokens_mask: Optional[Any] = None) -> Tuple[Any, Any]: + def numpy_mask_tokens(self, inputs: Any, special_tokens_mask: Optional[Any] = None) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. """ @@ -1193,7 +1193,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): """ - def torch_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def torch_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: if self.seed and self.generator is None: # If we have a seed, we need to create a generator object. Subsequent calls to this function will use the same generator. # If no seed supplied, we will use the global RNG @@ -1226,7 +1226,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): inputs, labels = self.torch_mask_tokens(batch_input, batch_mask) return {"input_ids": inputs, "labels": labels} - def tf_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def tf_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: import tensorflow as tf if self.seed and self.generator is None: @@ -1261,7 +1261,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): inputs, labels = self.tf_mask_tokens(tf.cast(batch_input, tf.int64), batch_mask) return {"input_ids": inputs, "labels": labels} - def numpy_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def numpy_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: if self.seed and self.generator is None: # If we have a seed, we need to create a generator object. Subsequent calls to this function will use the same generator. # If no seed supplied, we will use the global RNG @@ -1318,7 +1318,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): self.generator.shuffle(cand_indexes) return cand_indexes - def _whole_word_mask(self, input_tokens: List[str], max_predictions=512): + def _whole_word_mask(self, input_tokens: list[str], max_predictions=512): """ Get 0/1 labels for masked tokens with whole word mask proxy """ @@ -1358,7 +1358,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): mask_labels = [1 if i in covered_indexes else 0 for i in range(len(input_tokens))] return mask_labels - def torch_mask_tokens(self, inputs: Any, mask_labels: Any) -> Tuple[Any, Any]: + def torch_mask_tokens(self, inputs: Any, mask_labels: Any) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set 'mask_labels' means we use whole word mask (wwm), we directly mask idxs according to it's ref. @@ -1414,7 +1414,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): # The rest of the time ((1-random_replacement_prob-mask_replace_prob)% of the time) we keep the masked input tokens unchanged return inputs, labels - def tf_mask_tokens(self, inputs: Any, mask_labels: Any) -> Tuple[Any, Any]: + def tf_mask_tokens(self, inputs: Any, mask_labels: Any) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set 'mask_labels' means we use whole word mask (wwm), we directly mask idxs according to it's ref. @@ -1474,7 +1474,7 @@ class DataCollatorForWholeWordMask(DataCollatorForLanguageModeling): # The rest of the time ((1-mask_replace_prob-random_replace_prob)% of the time) we keep the masked input tokens unchanged return inputs, labels - def numpy_mask_tokens(self, inputs: Any, mask_labels: Any) -> Tuple[Any, Any]: + def numpy_mask_tokens(self, inputs: Any, mask_labels: Any) -> tuple[Any, Any]: """ Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set 'mask_labels' means we use whole word mask (wwm), we directly mask idxs according to it's ref. @@ -1564,7 +1564,7 @@ class DataCollatorForSOP(DataCollatorForLanguageModeling): FutureWarning, ) - def __call__(self, examples: List[Dict[str, Any]]) -> Dict[str, Any]: + def __call__(self, examples: list[dict[str, Any]]) -> dict[str, Any]: import torch from torch.nn.utils.rnn import pad_sequence @@ -1587,7 +1587,7 @@ class DataCollatorForSOP(DataCollatorForLanguageModeling): "sentence_order_label": sentence_order_label, } - def mask_tokens(self, inputs: Any) -> Tuple[Any, Any, Any]: + def mask_tokens(self, inputs: Any) -> tuple[Any, Any, Any]: """ Prepare masked tokens inputs/labels/attention_mask for masked language modeling: 80% MASK, 10% random, 10% original. N-gram not applied yet. @@ -1645,28 +1645,28 @@ class DataCollatorForPermutationLanguageModeling(DataCollatorMixin): max_span_length: int = 5 # maximum length of a span of masked tokens return_tensors: str = "pt" - def torch_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def torch_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: if isinstance(examples[0], Mapping): examples = [e["input_ids"] for e in examples] batch = _torch_collate_batch(examples, self.tokenizer) inputs, perm_mask, target_mapping, labels = self.torch_mask_tokens(batch) return {"input_ids": inputs, "perm_mask": perm_mask, "target_mapping": target_mapping, "labels": labels} - def tf_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def tf_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: if isinstance(examples[0], Mapping): examples = [e["input_ids"] for e in examples] batch = _tf_collate_batch(examples, self.tokenizer) inputs, perm_mask, target_mapping, labels = self.tf_mask_tokens(batch) return {"input_ids": inputs, "perm_mask": perm_mask, "target_mapping": target_mapping, "labels": labels} - def numpy_call(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]: + def numpy_call(self, examples: list[Union[list[int], Any, dict[str, Any]]]) -> dict[str, Any]: if isinstance(examples[0], Mapping): examples = [e["input_ids"] for e in examples] batch = _numpy_collate_batch(examples, self.tokenizer) inputs, perm_mask, target_mapping, labels = self.numpy_mask_tokens(batch) return {"input_ids": inputs, "perm_mask": perm_mask, "target_mapping": target_mapping, "labels": labels} - def torch_mask_tokens(self, inputs: Any) -> Tuple[Any, Any, Any, Any]: + def torch_mask_tokens(self, inputs: Any) -> tuple[Any, Any, Any, Any]: """ The masked tokens to be predicted for a particular sequence are determined by the following algorithm: @@ -1765,7 +1765,7 @@ class DataCollatorForPermutationLanguageModeling(DataCollatorMixin): return inputs.long(), perm_mask, target_mapping, labels.long() - def tf_mask_tokens(self, inputs: Any) -> Tuple[Any, Any, Any, Any]: + def tf_mask_tokens(self, inputs: Any) -> tuple[Any, Any, Any, Any]: """ The masked tokens to be predicted for a particular sequence are determined by the following algorithm: @@ -1872,7 +1872,7 @@ class DataCollatorForPermutationLanguageModeling(DataCollatorMixin): return tf.cast(inputs, tf.int64), tf.cast(perm_mask, tf.float32), target_mapping, tf.cast(labels, tf.int64) - def numpy_mask_tokens(self, inputs: Any) -> Tuple[Any, Any, Any, Any]: + def numpy_mask_tokens(self, inputs: Any) -> tuple[Any, Any, Any, Any]: """ The masked tokens to be predicted for a particular sequence are determined by the following algorithm: diff --git a/src/transformers/data/datasets/glue.py b/src/transformers/data/datasets/glue.py index 5541e5927a3..d8db0dfebac 100644 --- a/src/transformers/data/datasets/glue.py +++ b/src/transformers/data/datasets/glue.py @@ -17,7 +17,7 @@ import time import warnings from dataclasses import dataclass, field from enum import Enum -from typing import List, Optional, Union +from typing import Optional, Union import torch from filelock import FileLock @@ -75,7 +75,7 @@ class GlueDataset(Dataset): args: GlueDataTrainingArguments output_mode: str - features: List[InputFeatures] + features: list[InputFeatures] def __init__( self, diff --git a/src/transformers/data/datasets/language_modeling.py b/src/transformers/data/datasets/language_modeling.py index 6c23bf23cf1..07250ef3cb5 100644 --- a/src/transformers/data/datasets/language_modeling.py +++ b/src/transformers/data/datasets/language_modeling.py @@ -18,7 +18,7 @@ import pickle import random import time import warnings -from typing import Dict, List, Optional +from typing import Optional import torch from filelock import FileLock @@ -139,7 +139,7 @@ class LineByLineTextDataset(Dataset): def __len__(self): return len(self.examples) - def __getitem__(self, i) -> Dict[str, torch.tensor]: + def __getitem__(self, i) -> dict[str, torch.tensor]: return self.examples[i] @@ -187,7 +187,7 @@ class LineByLineWithRefDataset(Dataset): def __len__(self): return len(self.examples) - def __getitem__(self, i) -> Dict[str, torch.tensor]: + def __getitem__(self, i) -> dict[str, torch.tensor]: return self.examples[i] @@ -339,7 +339,7 @@ class LineByLineWithSOPTextDataset(Dataset): def __len__(self): return len(self.examples) - def __getitem__(self, i) -> Dict[str, torch.tensor]: + def __getitem__(self, i) -> dict[str, torch.tensor]: return self.examples[i] @@ -433,7 +433,7 @@ class TextDatasetForNextSentencePrediction(Dataset): f"Saving features into cached file {cached_features_file} [took {time.time() - start:.3f} s]" ) - def create_examples_from_document(self, document: List[List[int]], doc_index: int, block_size: int): + def create_examples_from_document(self, document: list[list[int]], doc_index: int, block_size: int): """Creates examples for a single document.""" max_num_tokens = block_size - self.tokenizer.num_special_tokens_to_add(pair=True) diff --git a/src/transformers/data/datasets/squad.py b/src/transformers/data/datasets/squad.py index a84f83d8f84..fdee571e249 100644 --- a/src/transformers/data/datasets/squad.py +++ b/src/transformers/data/datasets/squad.py @@ -16,7 +16,7 @@ import os import time from dataclasses import dataclass, field from enum import Enum -from typing import Dict, List, Optional, Union +from typing import Optional, Union import torch from filelock import FileLock @@ -112,7 +112,7 @@ class SquadDataset(Dataset): """ args: SquadDataTrainingArguments - features: List[SquadFeatures] + features: list[SquadFeatures] mode: Split is_language_sensitive: bool @@ -195,7 +195,7 @@ class SquadDataset(Dataset): def __len__(self): return len(self.features) - def __getitem__(self, i) -> Dict[str, torch.Tensor]: + def __getitem__(self, i) -> dict[str, torch.Tensor]: # Convert to Tensors and build dataset feature = self.features[i] diff --git a/src/transformers/data/processors/glue.py b/src/transformers/data/processors/glue.py index cbb4a70ab02..e005c9bcda1 100644 --- a/src/transformers/data/processors/glue.py +++ b/src/transformers/data/processors/glue.py @@ -19,7 +19,7 @@ import os import warnings from dataclasses import asdict from enum import Enum -from typing import List, Optional, Union +from typing import Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_tf_available, logging @@ -39,7 +39,7 @@ DEPRECATION_WARNING = ( def glue_convert_examples_to_features( - examples: Union[List[InputExample], "tf.data.Dataset"], + examples: Union[list[InputExample], "tf.data.Dataset"], tokenizer: PreTrainedTokenizer, max_length: Optional[int] = None, task=None, @@ -107,7 +107,7 @@ if is_tf_available(): def _glue_convert_examples_to_features( - examples: List[InputExample], + examples: list[InputExample], tokenizer: PreTrainedTokenizer, max_length: Optional[int] = None, task=None, diff --git a/src/transformers/data/processors/utils.py b/src/transformers/data/processors/utils.py index 936f5a51e9f..462156ebac3 100644 --- a/src/transformers/data/processors/utils.py +++ b/src/transformers/data/processors/utils.py @@ -18,7 +18,7 @@ import csv import dataclasses import json from dataclasses import dataclass -from typing import List, Optional, Union +from typing import Optional, Union from ...utils import is_tf_available, is_torch_available, logging @@ -67,9 +67,9 @@ class InputFeatures: float for regression problems. """ - input_ids: List[int] - attention_mask: Optional[List[int]] = None - token_type_ids: Optional[List[int]] = None + input_ids: list[int] + attention_mask: Optional[list[int]] = None + token_type_ids: Optional[list[int]] = None label: Optional[Union[int, float]] = None def to_json_string(self): diff --git a/src/transformers/debug_utils.py b/src/transformers/debug_utils.py index a395ee793d6..100ee85121c 100644 --- a/src/transformers/debug_utils.py +++ b/src/transformers/debug_utils.py @@ -136,7 +136,7 @@ class DebugUnderflowOverflow: The model to debug. max_frames_to_save (`int`, *optional*, defaults to 21): How many frames back to record - trace_batch_nums(`List[int]`, *optional*, defaults to `[]`): + trace_batch_nums(`list[int]`, *optional*, defaults to `[]`): Which batch numbers to trace (turns detection off) abort_after_batch_num (`int``, *optional*): Whether to abort after a certain batch number has finished diff --git a/src/transformers/dynamic_module_utils.py b/src/transformers/dynamic_module_utils.py index 660d0ac6d8d..6a88859e0aa 100644 --- a/src/transformers/dynamic_module_utils.py +++ b/src/transformers/dynamic_module_utils.py @@ -317,7 +317,7 @@ def get_cached_module_file( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -507,7 +507,7 @@ def get_class_from_dynamic_module( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -593,7 +593,7 @@ def custom_object_save(obj: Any, folder: Union[str, os.PathLike], config: Option A config in which to register the auto_map corresponding to this custom object. Returns: - `List[str]`: The list of files saved. + `list[str]`: The list of files saved. """ if obj.__module__ == "__main__": logger.warning( @@ -762,7 +762,7 @@ def check_python_requirements(path_or_repo_id, requirements_file="requirements.t This can be either: - a string, the *model id* of a model repo on huggingface.co. - a path to a *directory* potentially containing the file. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional arguments to pass to `cached_file`. """ failed = [] # error messages regarding requirements diff --git a/src/transformers/feature_extraction_sequence_utils.py b/src/transformers/feature_extraction_sequence_utils.py index c9a26bac9b3..186f3e31115 100644 --- a/src/transformers/feature_extraction_sequence_utils.py +++ b/src/transformers/feature_extraction_sequence_utils.py @@ -81,13 +81,13 @@ class SequenceFeatureExtractor(FeatureExtractionMixin): Args: - processed_features ([`BatchFeature`], list of [`BatchFeature`], `Dict[str, List[float]]`, `Dict[str, List[List[float]]` or `List[Dict[str, List[float]]]`): - Processed inputs. Can represent one input ([`BatchFeature`] or `Dict[str, List[float]]`) or a batch of - input values / vectors (list of [`BatchFeature`], *Dict[str, List[List[float]]]* or *List[Dict[str, - List[float]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader + processed_features ([`BatchFeature`], list of [`BatchFeature`], `dict[str, list[float]]`, `dict[str, list[list[float]]` or `list[dict[str, list[float]]]`): + Processed inputs. Can represent one input ([`BatchFeature`] or `dict[str, list[float]]`) or a batch of + input values / vectors (list of [`BatchFeature`], *dict[str, list[list[float]]]* or *list[dict[str, + list[float]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader collate function. - Instead of `List[float]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), + Instead of `list[float]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see the note above for the return type. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`): Select a strategy to pad the returned sequences (according to the model's padding side and padding @@ -235,9 +235,9 @@ class SequenceFeatureExtractor(FeatureExtractionMixin): Pad inputs (on left/right and up to predefined length or max length in the batch) Args: - processed_features (`Union[Dict[str, np.ndarray], BatchFeature]`): - Dictionary of input values (`np.ndarray[float]`) / input vectors (`List[np.ndarray[float]]`) or batch - of inputs values (`List[np.ndarray[int]]`) / input vectors (`List[np.ndarray[int]]`) + processed_features (`Union[dict[str, np.ndarray], BatchFeature]`): + Dictionary of input values (`np.ndarray[float]`) / input vectors (`list[np.ndarray[float]]`) or batch + of inputs values (`list[np.ndarray[int]]`) / input vectors (`list[np.ndarray[int]]`) max_length (`int`, *optional*): Maximum length of the returned list and optionally padding length (see below) padding_strategy (`PaddingStrategy`, *optional*, default to `PaddingStrategy.DO_NOT_PAD`): @@ -306,9 +306,9 @@ class SequenceFeatureExtractor(FeatureExtractionMixin): Truncate inputs to predefined length or max length in the batch Args: - processed_features(`Union[Dict[str, np.ndarray], BatchFeature]`): - Dictionary of input values (`np.ndarray[float]`) / input vectors (`List[np.ndarray[float]]`) or batch - of inputs values (`List[np.ndarray[int]]`) / input vectors (`List[np.ndarray[int]]`) + processed_features(`Union[dict[str, np.ndarray], BatchFeature]`): + Dictionary of input values (`np.ndarray[float]`) / input vectors (`list[np.ndarray[float]]`) or batch + of inputs values (`list[np.ndarray[int]]`) / input vectors (`list[np.ndarray[int]]`) max_length (`int`, *optional*): maximum length of the returned list and optionally padding length (see below) pad_to_multiple_of (`int`, *optional*) : diff --git a/src/transformers/feature_extraction_utils.py b/src/transformers/feature_extraction_utils.py index bba7d2e351d..0d08f225e5e 100644 --- a/src/transformers/feature_extraction_utils.py +++ b/src/transformers/feature_extraction_utils.py @@ -303,7 +303,7 @@ class FeatureExtractionMixin(PushToHubMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -326,7 +326,7 @@ class FeatureExtractionMixin(PushToHubMixin): functions returns a `Tuple(feature_extractor, unused_kwargs)` where *unused_kwargs* is a dictionary consisting of the key/value pairs whose keys are not feature extractor attributes: i.e., the part of `kwargs` which has not been used to update `feature_extractor` and is otherwise ignored. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are feature extractor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* feature extractor attributes is controlled by the `return_unused_kwargs` keyword parameter. @@ -392,7 +392,7 @@ class FeatureExtractionMixin(PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -454,7 +454,7 @@ class FeatureExtractionMixin(PushToHubMixin): The identifier of the pre-trained checkpoint from which we want the dictionary of parameters. Returns: - `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the feature extractor object. + `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the feature extractor object. """ cache_dir = kwargs.pop("cache_dir", None) force_download = kwargs.pop("force_download", False) @@ -555,11 +555,11 @@ class FeatureExtractionMixin(PushToHubMixin): parameters. Args: - feature_extractor_dict (`Dict[str, Any]`): + feature_extractor_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the feature extractor object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [`~feature_extraction_utils.FeatureExtractionMixin.to_dict`] method. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the feature extractor object. Returns: @@ -588,7 +588,7 @@ class FeatureExtractionMixin(PushToHubMixin): def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) output["feature_extractor_type"] = self.__class__.__name__ diff --git a/src/transformers/generation/beam_constraints.py b/src/transformers/generation/beam_constraints.py index daf64209b79..498ebffd207 100644 --- a/src/transformers/generation/beam_constraints.py +++ b/src/transformers/generation/beam_constraints.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from typing import List, Optional +from typing import Optional class Constraint(ABC): @@ -51,7 +51,7 @@ class Constraint(ABC): When called, returns the token(s) that would take this constraint one step closer to being fulfilled. Return: - token_ids (Union[int, List[int], None]): + token_ids (Union[int, list[int], None]): - A single token ID (int) that advances the constraint, or - A list of token IDs that could advance the constraint - None if the constraint is completed or cannot be advanced @@ -134,11 +134,11 @@ class PhrasalConstraint(Constraint): [`Constraint`] enforcing that an ordered sequence of tokens is included in the output. Args: - token_ids (`List[int]`): + token_ids (`list[int]`): The id of the token that must be generated by the output. """ - def __init__(self, token_ids: List[int]): + def __init__(self, token_ids: list[int]): super(Constraint, self).__init__() if not isinstance(token_ids, list) or len(token_ids) == 0: @@ -205,7 +205,7 @@ class PhrasalConstraint(Constraint): class DisjunctiveTrie: - def __init__(self, nested_token_ids: List[List[int]], no_subsets=True): + def __init__(self, nested_token_ids: list[list[int]], no_subsets=True): r""" A helper class that builds a trie with the words represented in `nested_token_ids`. """ @@ -266,12 +266,12 @@ class DisjunctiveConstraint(Constraint): A special [`Constraint`] that is fulfilled by fulfilling just one of several constraints. Args: - nested_token_ids (`List[List[int]]`): + nested_token_ids (`list[list[int]]`): A list of words, where each word is a list of ids. This constraint is fulfilled by generating just one from the list of words. """ - def __init__(self, nested_token_ids: List[List[int]]): + def __init__(self, nested_token_ids: list[list[int]]): super(Constraint, self).__init__() if not isinstance(nested_token_ids, list) or len(nested_token_ids) == 0: @@ -356,11 +356,11 @@ class ConstraintListState: A class for beam scorers to track its progress through a list of constraints. Args: - constraints (`List[Constraint]`): + constraints (`list[Constraint]`): A list of [`Constraint`] objects that must be fulfilled by the beam scorer. """ - def __init__(self, constraints: List[Constraint]): + def __init__(self, constraints: list[Constraint]): self.constraints = constraints # max # of steps required to fulfill a given constraint @@ -418,7 +418,7 @@ class ConstraintListState: else: return token_list - def reset(self, token_ids: Optional[List[int]]): + def reset(self, token_ids: Optional[list[int]]): """ token_ids: the tokens generated thus far to reset the state of the progress through constraints. """ diff --git a/src/transformers/generation/beam_search.py b/src/transformers/generation/beam_search.py index f783784a4ae..9db8694c234 100644 --- a/src/transformers/generation/beam_search.py +++ b/src/transformers/generation/beam_search.py @@ -15,7 +15,7 @@ from abc import ABC, abstractmethod from collections import UserDict -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -41,7 +41,7 @@ PROCESS_INPUTS_DOCSTRING = r""" Beam indices indicating to which beam hypothesis the `next_tokens` correspond. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. beam_indices (`torch.LongTensor`, *optional*): Beam indices indicating to which beam hypothesis each token correspond. @@ -77,7 +77,7 @@ FINALIZE_INPUTS_DOCSTRING = r""" The beam indices indicating to which beam the `final_beam_tokens` shall be added. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. Return: @@ -103,7 +103,7 @@ class BeamScorer(ABC): next_tokens: torch.LongTensor, next_indices: torch.LongTensor, **kwargs, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: raise NotImplementedError("This is an abstract method.") @abstractmethod @@ -219,11 +219,11 @@ class BeamSearchScorer(BeamScorer): next_tokens: torch.LongTensor, next_indices: torch.LongTensor, pad_token_id: Optional[Union[int, torch.Tensor]] = None, - eos_token_id: Optional[Union[int, List[int], torch.Tensor]] = None, + eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None, beam_indices: Optional[torch.LongTensor] = None, group_index: Optional[int] = 0, decoder_prompt_len: Optional[int] = 0, - ) -> Dict[str, torch.Tensor]: + ) -> dict[str, torch.Tensor]: # add up to the length which the next_scores is calculated on (including decoder prompt) cur_len = input_ids.shape[-1] + 1 batch_size = len(self._beam_hyps) // self.num_beam_groups @@ -325,10 +325,10 @@ class BeamSearchScorer(BeamScorer): final_beam_indices: torch.LongTensor, max_length: int, pad_token_id: Optional[Union[int, torch.Tensor]] = None, - eos_token_id: Optional[Union[int, List[int], torch.Tensor]] = None, + eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None, beam_indices: Optional[torch.LongTensor] = None, decoder_prompt_len: Optional[int] = 0, - ) -> Tuple[torch.LongTensor]: + ) -> tuple[torch.LongTensor]: batch_size = len(self._beam_hyps) // self.num_beam_groups if eos_token_id is not None and not isinstance(eos_token_id, torch.Tensor): @@ -426,7 +426,7 @@ class ConstrainedBeamSearchScorer(BeamScorer): Batch Size of `input_ids` for which standard beam search decoding is run in parallel. num_beams (`int`): Number of beams for beam search. - constraints (`List[Constraint]`): + constraints (`list[Constraint]`): A list of positive constraints represented as `Constraint` objects that must be fulfilled in the generation output. For more information, the documentation of [`Constraint`] should be read. device (`torch.device`): @@ -457,7 +457,7 @@ class ConstrainedBeamSearchScorer(BeamScorer): self, batch_size: int, num_beams: int, - constraints: List[Constraint], + constraints: list[Constraint], device: torch.device, length_penalty: Optional[float] = 1.0, do_early_stopping: Optional[Union[bool, str]] = False, @@ -518,10 +518,10 @@ class ConstrainedBeamSearchScorer(BeamScorer): next_indices: torch.LongTensor, scores_for_all_vocab: torch.FloatTensor, pad_token_id: Optional[Union[int, torch.Tensor]] = None, - eos_token_id: Optional[Union[int, List[int], torch.Tensor]] = None, + eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None, beam_indices: Optional[torch.LongTensor] = None, decoder_prompt_len: Optional[int] = 0, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size * num_beams, sequence_length)`): @@ -541,7 +541,7 @@ class ConstrainedBeamSearchScorer(BeamScorer): The scores of all tokens in the vocabulary for each of the beam hypotheses. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. beam_indices (`torch.LongTensor`, *optional*): Beam indices indicating to which beam hypothesis each token correspond. @@ -818,10 +818,10 @@ class ConstrainedBeamSearchScorer(BeamScorer): final_beam_indices: torch.LongTensor, max_length: int, pad_token_id: Optional[Union[int, torch.Tensor]] = None, - eos_token_id: Optional[Union[int, List[int], torch.Tensor]] = None, + eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None, beam_indices: Optional[torch.LongTensor] = None, decoder_prompt_len: Optional[int] = 0, - ) -> Tuple[torch.LongTensor]: + ) -> tuple[torch.LongTensor]: batch_size = len(self._beam_hyps) if eos_token_id is not None and not isinstance(eos_token_id, torch.Tensor): diff --git a/src/transformers/generation/candidate_generator.py b/src/transformers/generation/candidate_generator.py index b82cdd12f8d..94246b30f5a 100644 --- a/src/transformers/generation/candidate_generator.py +++ b/src/transformers/generation/candidate_generator.py @@ -15,7 +15,7 @@ import copy import weakref -from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple +from typing import TYPE_CHECKING, Any, Optional import numpy as np import torch @@ -44,7 +44,7 @@ from ..utils.deprecation import deprecate_kwarg class CandidateGenerator: """Abstract base class for all candidate generators that can be applied during assisted generation.""" - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """ Fetches the candidates to be tried for the current input. @@ -108,7 +108,7 @@ class AssistedCandidateGenerator(CandidateGenerator): input_ids: torch.LongTensor, assistant_model: "PreTrainedModel", generation_config: "GenerationConfig", - model_kwargs: Dict, + model_kwargs: dict, inputs_tensor: Optional[torch.Tensor] = None, logits_processor: "LogitsProcessorList" = None, ): @@ -198,7 +198,7 @@ class AssistedCandidateGenerator(CandidateGenerator): self.probs = [] self.matches = [] - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """ Fetches the candidates to be tried for the current input. @@ -281,7 +281,7 @@ class AssistedCandidateGenerator(CandidateGenerator): self.assistant_model.generation_config.assistant_confidence_threshold = best_threshold - def _calculate_new_tokens(self, input_ids: torch.LongTensor) -> Tuple[int, int]: + def _calculate_new_tokens(self, input_ids: torch.LongTensor) -> tuple[int, int]: """Calculate the minimum and maximum number of new tokens to generate.""" new_cur_len = input_ids.shape[-1] max_new_tokens = min(int(self.num_assistant_tokens), self.generation_config.max_length - new_cur_len - 1) @@ -305,7 +305,7 @@ class AssistedCandidateGenerator(CandidateGenerator): return has_past_key_values - def _prepare_generation_args(self, input_ids: torch.LongTensor, min_new_tokens: int, max_new_tokens: int) -> Dict: + def _prepare_generation_args(self, input_ids: torch.LongTensor, min_new_tokens: int, max_new_tokens: int) -> dict: """Prepare arguments for the generation call.""" return { self.input_ids_key: input_ids, @@ -315,7 +315,7 @@ class AssistedCandidateGenerator(CandidateGenerator): "logits_processor": self.logits_processor, } - def _generate_candidates(self, generation_args: Dict) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def _generate_candidates(self, generation_args: dict) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """Generate candidate sequences using the assistant model.""" assistant_output = self.assistant_model.generate(**generation_args, **self.assistant_kwargs) self.assistant_kwargs["past_key_values"] = assistant_output.past_key_values @@ -374,7 +374,7 @@ class AssistedCandidateGeneratorDifferentTokenizers(AssistedCandidateGenerator): target_tokenizer: "PreTrainedTokenizerBase", assistant_tokenizer: "PreTrainedTokenizerBase", generation_config: "GenerationConfig", - model_kwargs: Dict, + model_kwargs: dict, inputs_tensor: Optional[torch.Tensor] = None, logits_processor: "LogitsProcessorList" = None, ): @@ -495,7 +495,7 @@ class AssistedCandidateGeneratorDifferentTokenizers(AssistedCandidateGenerator): dest_ids = destination_tokenizer(text, add_special_tokens=True, return_tensors="pt")["input_ids"] return dest_ids.to(input_ids.device) - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """ Fetches the candidates to be tried for the current input. @@ -537,7 +537,7 @@ class AssistedCandidateGeneratorDifferentTokenizers(AssistedCandidateGenerator): return new_target_ids, None - def _prepare_assistant_input_ids(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, int]: + def _prepare_assistant_input_ids(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, int]: """Converts target input IDs to assistant input IDs, handling discrepancies.""" convert_kwargs = { "source_tokenizer": self.target_tokenizer, @@ -782,7 +782,7 @@ class AssistantToTargetTranslator: max_assistant_index = max(assistant_vocab.values()) assistant_to_target_input_ids = torch.full((max_assistant_index + 1,), self.SUPPRESS_TOKEN_ID, dtype=int) - target_to_assistant_input_ids: Dict[int, int] = {} + target_to_assistant_input_ids: dict[int, int] = {} for tok, assistant_id in assistant_vocab.items(): target_id = target_vocab.get(tok) if target_id is not None: @@ -909,7 +909,7 @@ class UniversalSpeculativeDecodingGenerator(AssistedCandidateGeneratorDifferentT target_tokenizer: "PreTrainedTokenizerBase", assistant_tokenizer: "PreTrainedTokenizerBase", generation_config: "GenerationConfig", - model_kwargs: Dict, + model_kwargs: dict, atm_translator: AssistantToTargetTranslator, inputs_tensor: Optional[torch.Tensor] = None, logits_processor: "LogitsProcessorList" = None, @@ -930,7 +930,7 @@ class UniversalSpeculativeDecodingGenerator(AssistedCandidateGeneratorDifferentT self._target_seq_len_with_candidates: int = 0 self._prev_assistant_ids: Optional[torch.LongTensor] = None - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """ Simplified version of get_candidates that uses the translator cache for token conversion. """ @@ -1043,7 +1043,7 @@ class PromptLookupCandidateGenerator(CandidateGenerator): if self.max_matching_ngram_size <= 0 or self.num_output_tokens <= 0: raise ValueError("Invalid max_matching_ngram_size or num_output_tokens") - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: """ Fetches the candidates to be tried for the current input. @@ -1153,7 +1153,7 @@ class EarlyExitCandidateGenerator(AssistedCandidateGenerator): input_ids: torch.LongTensor, assistant_model: "PreTrainedModel", generation_config: "GenerationConfig", - model_kwargs: Dict, + model_kwargs: dict, inputs_tensor: Optional[torch.Tensor] = None, logits_processor: "LogitsProcessorList" = None, ): @@ -1170,7 +1170,7 @@ class EarlyExitCandidateGenerator(AssistedCandidateGenerator): self.assistant_early_exit = self.generation_config.assistant_early_exit self.generation_config.assistant_early_exit = None - def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]: + def get_candidates(self, input_ids: torch.LongTensor) -> tuple[torch.LongTensor, Optional[torch.FloatTensor]]: # Temporarily sets the number of hidden layers to the early exit value base_model = getattr(self.assistant_model, self.assistant_model.base_model_prefix) original_num_hidden_layers = base_model.config.num_hidden_layers @@ -1221,7 +1221,7 @@ def _crop_past_key_values(model, past_key_values, max_length): return past_key_values -def _prepare_attention_mask(model_kwargs: Dict[str, Any], new_length: int, is_encoder_decoder: bool) -> Dict[str, Any]: +def _prepare_attention_mask(model_kwargs: dict[str, Any], new_length: int, is_encoder_decoder: bool) -> dict[str, Any]: """Expands or crops the model's mask for decoding purposes, to the defined length""" mask_key = "decoder_attention_mask" if is_encoder_decoder else "attention_mask" @@ -1257,7 +1257,7 @@ def _prepare_attention_mask(model_kwargs: Dict[str, Any], new_length: int, is_en return model_kwargs -def _prepare_token_type_ids(model_kwargs: Dict[str, Any], new_length: int) -> Dict[str, Any]: +def _prepare_token_type_ids(model_kwargs: dict[str, Any], new_length: int) -> dict[str, Any]: """Expands or crops the model's token_type_ids for decoding purposes, to the defined length""" if "token_type_ids" not in model_kwargs or model_kwargs["token_type_ids"] is None: return model_kwargs diff --git a/src/transformers/generation/configuration_utils.py b/src/transformers/generation/configuration_utils.py index e7e77a8cd6a..76587659371 100644 --- a/src/transformers/generation/configuration_utils.py +++ b/src/transformers/generation/configuration_utils.py @@ -20,7 +20,7 @@ import os import warnings from abc import ABC, abstractmethod from dataclasses import dataclass, is_dataclass -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union from .. import __version__ from ..configuration_utils import PretrainedConfig @@ -149,7 +149,7 @@ class GenerationConfig(PushToHubMixin): max_time (`float`, *optional*): The maximum amount of time you allow the computation to run for in seconds. generation will still finish the current pass after allocated time has been passed. - stop_strings (`str or List[str]`, *optional*): + stop_strings (`str or list[str]`, *optional*): A string or a list of strings that should terminate generation if the model outputs them. > Parameters that control the generation strategy used @@ -163,7 +163,7 @@ class GenerationConfig(PushToHubMixin): [this paper](https://huggingface.co/papers/1610.02424) for more details. penalty_alpha (`float`, *optional*): The values balance the model confidence and the degeneration penalty in contrastive search decoding. - dola_layers (`str` or `List[int]`, *optional*): + dola_layers (`str` or `list[int]`, *optional*): The layers to use for DoLa decoding. If `None`, DoLa decoding is not used. If a string, it must be one of "low" or "high", which means using the lower part or higher part of the model layers, respectively. "low" means the first half of the layers up to the first 20 layers, and "high" means the last half of the @@ -245,26 +245,26 @@ class GenerationConfig(PushToHubMixin): `length_penalty` < 0.0 encourages shorter sequences. no_repeat_ngram_size (`int`, *optional*, defaults to 0): If set to int > 0, all ngrams of that size can only occur once. - bad_words_ids (`List[List[int]]`, *optional*): + bad_words_ids (`list[list[int]]`, *optional*): List of list of token ids that are not allowed to be generated. Check [`~generation.NoBadWordsLogitsProcessor`] for further documentation and examples. - force_words_ids (`List[List[int]]` or `List[List[List[int]]]`, *optional*): - List of token ids that must be generated. If given a `List[List[int]]`, this is treated as a simple list of - words that must be included, the opposite to `bad_words_ids`. If given `List[List[List[int]]]`, this + force_words_ids (`list[list[int]]` or `list[list[list[int]]]`, *optional*): + List of token ids that must be generated. If given a `list[list[int]]`, this is treated as a simple list of + words that must be included, the opposite to `bad_words_ids`. If given `list[list[list[int]]]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. renormalize_logits (`bool`, *optional*, defaults to `False`): Whether to renormalize the logits after applying all the logits processors (including the custom ones). It's highly recommended to set this flag to `True` as the search algorithms suppose the score logits are normalized but some logit processors break the normalization. - constraints (`List[Constraint]`, *optional*): + constraints (`list[Constraint]`, *optional*): Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. forced_bos_token_id (`int`, *optional*, defaults to `model.config.forced_bos_token_id`): The id of the token to force as the first generated token after the `decoder_start_token_id`. Useful for multilingual models like [mBART](../model_doc/mbart) where the first generated token needs to be the target language token. - forced_eos_token_id (`int` or List[int]`, *optional*, defaults to `model.config.forced_eos_token_id`): + forced_eos_token_id (`int` or list[int]`, *optional*, defaults to `model.config.forced_eos_token_id`): The id of the token to force as the last generated token when `max_length` is reached. Optionally, use a list to set multiple *end-of-sequence* tokens. remove_invalid_values (`bool`, *optional*, defaults to `model.config.remove_invalid_values`): @@ -274,13 +274,13 @@ class GenerationConfig(PushToHubMixin): This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay - suppress_tokens (`List[int]`, *optional*): + suppress_tokens (`list[int]`, *optional*): A list of tokens that will be suppressed at generation. The `SupressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. - begin_suppress_tokens (`List[int]`, *optional*): + begin_suppress_tokens (`list[int]`, *optional*): A list of tokens that will be suppressed at the beginning of the generation. The `SupressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. - sequence_bias (`Dict[Tuple[int], float]`, *optional*)): + sequence_bias (`dict[tuple[int], float]`, *optional*)): Dictionary that maps a sequence of tokens to its bias term. Positive biases increase the odds of the sequence being selected, while negative biases do the opposite. Check [`~generation.SequenceBiasLogitsProcessor`] for further documentation and examples. @@ -325,7 +325,7 @@ class GenerationConfig(PushToHubMixin): The id of the *padding* token. bos_token_id (`int`, *optional*): The id of the *beginning-of-sequence* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. > Generation parameters exclusive to encoder-decoder models @@ -333,7 +333,7 @@ class GenerationConfig(PushToHubMixin): encoder_no_repeat_ngram_size (`int`, *optional*, defaults to 0): If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. - decoder_start_token_id (`int` or `List[int]`, *optional*): + decoder_start_token_id (`int` or `list[int]`, *optional*): If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token or a list of length `batch_size`. Indicating a list enables different start ids for each element in the batch (e.g. multilingual models with different target languages in one batch) @@ -846,7 +846,7 @@ class GenerationConfig(PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ @@ -933,7 +933,7 @@ class GenerationConfig(PushToHubMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -959,7 +959,7 @@ class GenerationConfig(PushToHubMixin): subfolder (`str`, *optional*, defaults to `""`): In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are configuration attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* configuration attributes is controlled by the `return_unused_kwargs` keyword parameter. @@ -1090,14 +1090,14 @@ class GenerationConfig(PushToHubMixin): return json.loads(text) @classmethod - def from_dict(cls, config_dict: Dict[str, Any], **kwargs) -> "GenerationConfig": + def from_dict(cls, config_dict: dict[str, Any], **kwargs) -> "GenerationConfig": """ Instantiates a [`GenerationConfig`] from a Python dictionary of parameters. Args: - config_dict (`Dict[str, Any]`): + config_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the configuration object. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the configuration object. Returns: @@ -1123,7 +1123,7 @@ class GenerationConfig(PushToHubMixin): else: return config - def dict_torch_dtype_to_str(self, d: Dict[str, Any]) -> None: + def dict_torch_dtype_to_str(self, d: dict[str, Any]) -> None: """ Checks whether the passed dictionary and its nested dicts have a *torch_dtype* key and if it's not None, converts torch.dtype to a string of just the type. For example, `torch.float32` get converted into *"float32"* @@ -1135,13 +1135,13 @@ class GenerationConfig(PushToHubMixin): if isinstance(value, dict): self.dict_torch_dtype_to_str(value) - def to_diff_dict(self) -> Dict[str, Any]: + def to_diff_dict(self) -> dict[str, Any]: """ Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, """ config_dict = self.to_dict() @@ -1158,12 +1158,12 @@ class GenerationConfig(PushToHubMixin): self.dict_torch_dtype_to_str(serializable_config_dict) return serializable_config_dict - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) @@ -1289,11 +1289,11 @@ class GenerationConfig(PushToHubMixin): returning all the unused kwargs. Args: - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Dictionary of attributes to tentatively update this class. Returns: - `Dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. + `dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. """ to_remove = [] for key, value in kwargs.items(): @@ -1319,7 +1319,7 @@ class BaseWatermarkingConfig(ABC): Constructs a BaseWatermarkingConfig instance from a dictionary of parameters. Args: - config_dict (Dict[str, Any]): Dictionary containing configuration parameters. + config_dict (dict[str, Any]): Dictionary containing configuration parameters. **kwargs: Additional keyword arguments to override dictionary values. Returns: @@ -1348,12 +1348,12 @@ class BaseWatermarkingConfig(ABC): writer.write(json_string) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - Dict[str, Any]: Dictionary of all the attributes that make up this configuration instance. + dict[str, Any]: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) return output @@ -1479,7 +1479,7 @@ class SynthIDTextWatermarkingConfig(BaseWatermarkingConfig): Args: ngram_len (`int`): Ngram length. - keys (`List[int]`): + keys (`list[int]`): A sequence of watermarking keys, one for each depth. context_history_size (`int`, *optional*, defaults to 1024): Size of the tensor to keep track of seen contexts. @@ -1518,7 +1518,7 @@ class SynthIDTextWatermarkingConfig(BaseWatermarkingConfig): def __init__( self, ngram_len: int, - keys: List[int], + keys: list[int], context_history_size: int = 1024, sampling_table_seed: int = 0, sampling_table_size: int = 2**16, @@ -1605,6 +1605,6 @@ class CompileConfig: # Used to flag our `generate` call to compile on e.g. CPU. Often not optimal, but useful for testing purposes. _compile_all_devices = None - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """Serializes this instance to a Python dictionary.""" return copy.deepcopy({key: value for key, value in self.__dict__.items() if key != "_compile_all_devices"}) diff --git a/src/transformers/generation/continuous_batching.py b/src/transformers/generation/continuous_batching.py index 697fc7e424a..3149c63a492 100644 --- a/src/transformers/generation/continuous_batching.py +++ b/src/transformers/generation/continuous_batching.py @@ -23,7 +23,7 @@ from collections import deque from dataclasses import dataclass, field from enum import Enum from functools import partial -from typing import Deque, Dict, List, Optional, Set, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -59,16 +59,16 @@ class GenerationOutput: Attributes: request_id (str): The ID of the generation request. - prompt_ids (List[int]): The IDs of the prompt tokens. - generated_tokens (List[int]): The generated tokens. - logprobs (List[float]): The log probabilities of the generated tokens. + prompt_ids (list[int]): The IDs of the prompt tokens. + generated_tokens (list[int]): The generated tokens. + logprobs (list[float]): The log probabilities of the generated tokens. error (Optional[str]): Any error message associated with the request. When None, the request was successful. """ request_id: str - prompt_ids: List[int] = field(default_factory=list) - generated_tokens: List[int] = field(default_factory=list) - logprobs: List[float] = field(default_factory=list) + prompt_ids: list[int] = field(default_factory=list) + generated_tokens: list[int] = field(default_factory=list) + logprobs: list[float] = field(default_factory=list) error: Optional[str] = None status: RequestStatus = RequestStatus.PENDING created_time: float = field(default_factory=time.time) @@ -85,11 +85,11 @@ class RequestState: # Required fields request_id: str - prompt_ids: Optional[List[int]] = None # the one being processed - full_prompt_ids: Optional[List[int]] = None # the full prompt - remaining_prompt_ids: List[int] = field(default_factory=list) # For split requests - static_outputs: List[int] = field(default_factory=list) - allocated_blocks: List[int] = field(default_factory=list) + prompt_ids: Optional[list[int]] = None # the one being processed + full_prompt_ids: Optional[list[int]] = None # the full prompt + remaining_prompt_ids: list[int] = field(default_factory=list) # For split requests + static_outputs: list[int] = field(default_factory=list) + allocated_blocks: list[int] = field(default_factory=list) position_offset: int = 0 # Current position in the sequence for position_ids status: RequestStatus = RequestStatus.PENDING max_new_tokens: int = 20 @@ -150,8 +150,8 @@ class PagedAttentionCache(Cache): generation_config: GenerationConfig, device: torch.device, dtype: torch.dtype = torch.float16, - layer_device_map: Optional[Dict[int, Union[str, torch.device, int]]] = None, - initial_prompt_shapes: Optional[List[List[int]]] = None, + layer_device_map: Optional[dict[int, Union[str, torch.device, int]]] = None, + initial_prompt_shapes: Optional[list[list[int]]] = None, ) -> None: """Initialize a paged attention cache for efficient memory usage. @@ -191,8 +191,8 @@ class PagedAttentionCache(Cache): self.dtype = dtype self.device = device - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] for idx in range(config.num_hidden_layers): layer_device = layer_device_map[idx] if layer_device_map is not None else device new_layer_key_cache = torch.zeros(self.cache_shape, dtype=self.dtype, device=layer_device) @@ -206,10 +206,10 @@ class PagedAttentionCache(Cache): # Block management data structures self._free_blocks = deque(range(num_blocks)) - self._block_tables: Dict[str, List[int]] = {} + self._block_tables: dict[str, list[int]] = {} @traced - def allocate_blocks(self, n_blocks: int, request_id: str) -> List[int]: + def allocate_blocks(self, n_blocks: int, request_id: str) -> list[int]: """Allocates n_blocks for a given request_id.""" if len(self._free_blocks) < n_blocks: return False @@ -236,12 +236,12 @@ class PagedAttentionCache(Cache): """Returns the number of free blocks available.""" return len(self._free_blocks) - def get_block_table(self, request_id: str) -> List[int]: + def get_block_table(self, request_id: str) -> list[int]: """Returns the block table for a request.""" return self._block_tables.get(request_id, []) @traced - def _get_physical_indices(self, state: RequestState, logical_indices: List[int]) -> List[int]: + def _get_physical_indices(self, state: RequestState, logical_indices: list[int]) -> list[int]: """ Maps logical sequence indices to physical cache indices using the block table, using PyTorch. @@ -289,7 +289,7 @@ class PagedAttentionCache(Cache): read_index, write_index, **kwargs, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: # Reshape cache for easier indexing total_slots = self.num_blocks * self.block_size k_cache_flat = self.key_cache[layer_idx].view(self.num_key_value_heads, total_slots, self.head_dim) @@ -306,9 +306,9 @@ class Scheduler(ABC): """ def __init__(self, cache: PagedAttentionCache, retain_cache_on_finish: bool = False): - self.active_requests: Dict[str, RequestState] = {} - self.waiting_requests: Dict[str, RequestState] = {} - self.waiting_requests_order: Deque[str] = deque() + self.active_requests: dict[str, RequestState] = {} + self.waiting_requests: dict[str, RequestState] = {} + self.waiting_requests_order: deque[str] = deque() self.cache = cache self.retain_cache_on_finish = retain_cache_on_finish @@ -318,7 +318,7 @@ class Scheduler(ABC): pass @abstractmethod - def schedule_batch(self, token_budget: int) -> List[RequestState]: + def schedule_batch(self, token_budget: int) -> list[RequestState]: pass @traced @@ -332,7 +332,7 @@ class Scheduler(ABC): pass @traced - def get_active_request_static_outputs(self, request_id: str) -> List[int]: + def get_active_request_static_outputs(self, request_id: str) -> list[int]: if request_id in self.active_requests: return self.active_requests[request_id].static_outputs return [] @@ -356,7 +356,7 @@ class FIFOScheduler(Scheduler): @traced(span_name="prepare_request") def _prepare_request_for_processing( - self, state: RequestState, token_budget: int, request_ids_to_remove_from_waiting: Set[str] + self, state: RequestState, token_budget: int, request_ids_to_remove_from_waiting: set[str] ): """Prepare a request for processing in the current batch.""" request_tokens = ( @@ -395,9 +395,9 @@ class FIFOScheduler(Scheduler): self.waiting_requests_order.append(state.request_id) @traced - def schedule_batch(self, token_budget: int) -> List[RequestState]: - priority_states: List[RequestState] = [] - second_priority_states: List[RequestState] = [] + def schedule_batch(self, token_budget: int) -> list[RequestState]: + priority_states: list[RequestState] = [] + second_priority_states: list[RequestState] = [] scheduled_requests = [] for state in self.active_requests.values(): @@ -475,7 +475,7 @@ class PrefillFirstScheduler(Scheduler): @traced(span_name="prepare_request") def _prepare_request_for_processing( - self, state: RequestState, token_budget: int, request_ids_to_remove_from_waiting: Set[str] + self, state: RequestState, token_budget: int, request_ids_to_remove_from_waiting: set[str] ): """Prepare a request for processing in the current batch.""" request_tokens = ( @@ -514,9 +514,9 @@ class PrefillFirstScheduler(Scheduler): self.waiting_requests_order.append(state.request_id) @traced - def schedule_batch(self, token_budget: int) -> List[RequestState]: - priority_states: List[RequestState] = [] - second_priority_states: List[RequestState] = [] + def schedule_batch(self, token_budget: int) -> list[RequestState]: + priority_states: list[RequestState] = [] + second_priority_states: list[RequestState] = [] scheduled_requests = [] for state in self.active_requests.values(): @@ -581,7 +581,7 @@ def compute_optimal_blocks( device: torch.device, config: PretrainedConfig, generation_config: GenerationConfig, - inputs: List[List[int]], + inputs: list[list[int]], dtype: torch.dtype = torch.bfloat16, safety_margin: float = 0.9, median_prefill_length: Optional[int] = None, @@ -678,7 +678,7 @@ class PagedAttentionArgs: write_index: torch.Tensor read_index: torch.Tensor logits_indices: torch.Tensor - block_tables: Dict[str, List[int]] + block_tables: dict[str, list[int]] cache: PagedAttentionCache use_cache: bool = False @@ -754,7 +754,7 @@ class ContinuousBatchProcessor: self.streaming = streaming self.manual_eviction = manual_eviction - self.requests_in_batch: List[RequestState] = [] + self.requests_in_batch: list[RequestState] = [] # Get batch size parameters from generation config self._configure_batch_parameters() @@ -1152,7 +1152,7 @@ class ContinuousBatchingManager: self._generation_thread = None def add_request( - self, input_ids: List[int], request_id: Optional[str] = None, max_new_tokens: Optional[int] = None + self, input_ids: list[int], request_id: Optional[str] = None, max_new_tokens: Optional[int] = None ) -> str: """Add a new generation request to the queue. @@ -1184,7 +1184,7 @@ class ContinuousBatchingManager: logger.debug(f"Added request {request_id} to queue.") return request_id - def add_requests(self, inputs: List[List[int]], **kwargs): + def add_requests(self, inputs: list[list[int]], **kwargs): for i, input_ids in enumerate(inputs): # Assign a predictable request ID for ordering results later req_id = f"batch_req_{i}" @@ -1428,11 +1428,11 @@ class ContinuousMixin: @torch.inference_mode() def generate_batch( self, - inputs: List[List[int]], + inputs: list[list[int]], generation_config: Optional[GenerationConfig] = None, progress_bar: bool = True, **kwargs, - ) -> List[List[int]]: + ) -> list[list[int]]: """Generate sequences for a batch of prompts using continuous batching. Args: @@ -1441,7 +1441,7 @@ class ContinuousMixin: **kwargs: Additional generation parameters Returns: - `List[List[int]]`: A list containing the generated sequences (including prompt tokens + `list[list[int]]`: A list containing the generated sequences (including prompt tokens if not handled otherwise) for each input prompt, in the same order. Returns an empty list `[]` for requests that failed. """ diff --git a/src/transformers/generation/flax_logits_process.py b/src/transformers/generation/flax_logits_process.py index fdb0e7a26f6..08fa411dc6f 100644 --- a/src/transformers/generation/flax_logits_process.py +++ b/src/transformers/generation/flax_logits_process.py @@ -39,7 +39,7 @@ LOGITS_PROCESSOR_INPUTS_DOCSTRING = r""" scores (`jnp.ndarray` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: @@ -276,7 +276,7 @@ class FlaxSuppressTokensAtBeginLogitsProcessor(FlaxLogitsProcessor): beginning of the generation. Args: - begin_suppress_tokens (`List[int]`): + begin_suppress_tokens (`list[int]`): Tokens to not sample. begin_index (`int`): Index where the tokens are suppressed. diff --git a/src/transformers/generation/flax_utils.py b/src/transformers/generation/flax_utils.py index 3c8c4795a84..780700233bf 100644 --- a/src/transformers/generation/flax_utils.py +++ b/src/transformers/generation/flax_utils.py @@ -19,7 +19,7 @@ import copy import inspect import warnings from functools import partial -from typing import Any, Dict, Optional, Union +from typing import Any, Optional, Union import flax import jax @@ -103,7 +103,7 @@ class GreedyState: sequences: jnp.ndarray running_token: jnp.ndarray is_sent_finished: jnp.ndarray - model_kwargs: Dict[str, jnp.ndarray] + model_kwargs: dict[str, jnp.ndarray] @flax.struct.dataclass @@ -113,7 +113,7 @@ class SampleState: running_token: jnp.ndarray is_sent_finished: jnp.ndarray prng_key: jnp.ndarray - model_kwargs: Dict[str, jnp.ndarray] + model_kwargs: dict[str, jnp.ndarray] @flax.struct.dataclass @@ -124,7 +124,7 @@ class BeamSearchState: sequences: jnp.ndarray scores: jnp.ndarray is_sent_finished: jnp.ndarray - model_kwargs: Dict[str, jnp.ndarray] + model_kwargs: dict[str, jnp.ndarray] class FlaxGenerationMixin: @@ -173,7 +173,7 @@ class FlaxGenerationMixin: batch_size: int, decoder_start_token_id: Optional[int] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, jnp.ndarray]] = None, + model_kwargs: Optional[dict[str, jnp.ndarray]] = None, ) -> jnp.ndarray: if model_kwargs is not None and "decoder_input_ids" in model_kwargs: # Only use this arg if not None, otherwise just remove from model_kwargs @@ -249,7 +249,7 @@ class FlaxGenerationMixin: exception_message += f" Please use one of the following classes instead: {generate_compatible_classes}" raise TypeError(exception_message) - def _validate_model_kwargs(self, model_kwargs: Dict[str, Any]): + def _validate_model_kwargs(self, model_kwargs: dict[str, Any]): """Validates model kwargs for generation. Generate argument typos will also be caught here.""" unused_model_args = [] model_args = set(inspect.signature(self.prepare_inputs_for_generation).parameters) @@ -273,7 +273,7 @@ class FlaxGenerationMixin: generation_config: Optional[GenerationConfig] = None, prng_key: Optional[jnp.ndarray] = None, trace: bool = True, - params: Optional[Dict[str, jnp.ndarray]] = None, + params: Optional[dict[str, jnp.ndarray]] = None, logits_processor: Optional[FlaxLogitsProcessorList] = None, **kwargs, ): @@ -293,13 +293,13 @@ class FlaxGenerationMixin: trace (`bool`, *optional*, defaults to `True`): Whether to trace generation. Setting `trace=False` should only be used for debugging and will lead to a considerably slower runtime. - params (`Dict[str, jnp.ndarray]`, *optional*): + params (`dict[str, jnp.ndarray]`, *optional*): Optionally the model parameters can be passed. Can be useful for parallelized generation. logits_processor (`FlaxLogitsProcessorList `, *optional*): Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. @@ -580,8 +580,8 @@ class FlaxGenerationMixin: eos_token_id: Optional[int] = None, logits_processor: Optional[FlaxLogitsProcessorList] = None, trace: bool = True, - params: Optional[Dict[str, jnp.ndarray]] = None, - model_kwargs: Optional[Dict[str, jnp.ndarray]] = None, + params: Optional[dict[str, jnp.ndarray]] = None, + model_kwargs: Optional[dict[str, jnp.ndarray]] = None, ): # init values max_length = max_length if max_length is not None else self.generation_config.max_length @@ -668,8 +668,8 @@ class FlaxGenerationMixin: logits_processor: Optional[FlaxLogitsProcessorList] = None, logits_warper: Optional[FlaxLogitsProcessorList] = None, trace: bool = True, - params: Optional[Dict[str, jnp.ndarray]] = None, - model_kwargs: Optional[Dict[str, jnp.ndarray]] = None, + params: Optional[dict[str, jnp.ndarray]] = None, + model_kwargs: Optional[dict[str, jnp.ndarray]] = None, ): # init values max_length = max_length if max_length is not None else self.generation_config.max_length @@ -765,9 +765,9 @@ class FlaxGenerationMixin: early_stopping: Optional[Union[bool, str]] = None, logits_processor: Optional[FlaxLogitsProcessorList] = None, trace: bool = True, - params: Optional[Dict[str, jnp.ndarray]] = None, + params: Optional[dict[str, jnp.ndarray]] = None, num_return_sequences: Optional[int] = None, - model_kwargs: Optional[Dict[str, jnp.ndarray]] = None, + model_kwargs: Optional[dict[str, jnp.ndarray]] = None, ): """ This beam search function is heavily inspired by Flax's official example: diff --git a/src/transformers/generation/logits_process.py b/src/transformers/generation/logits_process.py index b0a1cfcb290..8c72279b6ef 100644 --- a/src/transformers/generation/logits_process.py +++ b/src/transformers/generation/logits_process.py @@ -16,7 +16,7 @@ import inspect import math from collections.abc import Iterable -from typing import TYPE_CHECKING, Callable, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Callable, Optional, Union import numpy as np import torch @@ -72,7 +72,7 @@ class LogitsProcessorList(list): scores (`torch.FloatTensor` of shape `(batch_size, config.vocab_size)`): Prediction scores of a language modeling head. These can be logits for each vocabulary when not using beam search or log softmax for each vocabulary token when using beam search - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional kwargs that are specific to a logits processor. Return: @@ -103,7 +103,7 @@ class MinLengthLogitsProcessor(LogitsProcessor): Args: min_length (`int`): The minimum length below which the score of `eos_token_id` is set to `-float("Inf")`. - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. device (`str`, *optional*, defaults to `"cpu"`): The device to allocate the tensors. @@ -134,7 +134,7 @@ class MinLengthLogitsProcessor(LogitsProcessor): ``` """ - def __init__(self, min_length: int, eos_token_id: Union[int, List[int], torch.Tensor], device: str = "cpu"): + def __init__(self, min_length: int, eos_token_id: Union[int, list[int], torch.Tensor], device: str = "cpu"): if not isinstance(min_length, int) or min_length < 0: raise ValueError(f"`min_length` has to be a non-negative integer, but is {min_length}") @@ -167,7 +167,7 @@ class MinNewTokensLengthLogitsProcessor(LogitsProcessor): input length. min_new_tokens (`int`): The minimum *new* tokens length below which the score of `eos_token_id` is set to `-float("Inf")`. - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. device (`str`, *optional*, defaults to `"cpu"`): The device to allocate the tensors. @@ -197,7 +197,7 @@ class MinNewTokensLengthLogitsProcessor(LogitsProcessor): self, prompt_length_to_skip: int, min_new_tokens: int, - eos_token_id: Union[int, List[int], torch.Tensor], + eos_token_id: Union[int, list[int], torch.Tensor], device: str = "cpu", ): for arg_name, arg_value in [ @@ -917,7 +917,7 @@ def _get_generated_ngrams(banned_ngrams, prev_input_ids, ngram_size, cur_len): def _calc_banned_ngram_tokens( ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int, cur_len: int -) -> List[Iterable[int]]: +) -> list[Iterable[int]]: """Copied from fairseq for no_repeat_ngram in beam_search""" if cur_len + 1 < ngram_size: # return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet @@ -1074,7 +1074,7 @@ class SequenceBiasLogitsProcessor(LogitsProcessor): Args: - sequence_bias (`List[List[Union[List[int], float]]]`): + sequence_bias (`list[list[Union[list[int], float]]]`): List of lists that maps a sequence of tokens to its bias term (e.g. `[[[10, 45], -2.0], [[64], -7.5]]`). Positive biases increase the odds of the sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias @@ -1123,7 +1123,7 @@ class SequenceBiasLogitsProcessor(LogitsProcessor): ``` """ - def __init__(self, sequence_bias: List[List[Union[List[int], float]]]): + def __init__(self, sequence_bias: list[list[Union[list[int], float]]]): self.sequence_bias = sequence_bias self._validate_arguments() self._convert_list_arguments_into_dict() @@ -1250,9 +1250,9 @@ class NoBadWordsLogitsProcessor(SequenceBiasLogitsProcessor): Args: - bad_words_ids (`List[List[int]]`): + bad_words_ids (`list[list[int]]`): List of list of token ids that are not allowed to be generated. - eos_token_id (`Union[int, List[int], torch.Tensor]`, *optional*): + eos_token_id (`Union[int, list[int], torch.Tensor]`, *optional*): The id(s) of the *end-of-sequence* token. Examples: @@ -1291,7 +1291,7 @@ class NoBadWordsLogitsProcessor(SequenceBiasLogitsProcessor): """ def __init__( - self, bad_words_ids: List[List[int]], eos_token_id: Optional[Union[int, List[int], torch.Tensor]] = None + self, bad_words_ids: list[list[int]], eos_token_id: Optional[Union[int, list[int], torch.Tensor]] = None ): self.bad_word_ids = bad_words_ids self._validate_arguments() @@ -1332,7 +1332,7 @@ class PrefixConstrainedLogitsProcessor(LogitsProcessor): generation. See [Autoregressive Entity Retrieval](https://huggingface.co/papers/2010.00904) for more information. Args: - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`): This function constraints the beam search to allowed tokens only at each step. This function takes 2 arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the next generation step conditioned on the previously generated tokens `inputs_ids` and the batch ID @@ -1373,7 +1373,7 @@ class PrefixConstrainedLogitsProcessor(LogitsProcessor): ``` """ - def __init__(self, prefix_allowed_tokens_fn: Callable[[int, torch.Tensor], List[int]], num_beams: int): + def __init__(self, prefix_allowed_tokens_fn: Callable[[int, torch.Tensor], list[int]], num_beams: int): self._prefix_allowed_tokens_fn = prefix_allowed_tokens_fn self._num_beams = num_beams @@ -1586,7 +1586,7 @@ class ForcedEOSTokenLogitsProcessor(LogitsProcessor): Args: max_length (`int`): The maximum length of the sequence to be generated. - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. device (`str`, *optional*, defaults to `"cpu"`): The device to allocate the tensors. @@ -1613,7 +1613,7 @@ class ForcedEOSTokenLogitsProcessor(LogitsProcessor): ``` """ - def __init__(self, max_length: int, eos_token_id: Union[int, List[int], torch.Tensor], device: str = "cpu"): + def __init__(self, max_length: int, eos_token_id: Union[int, list[int], torch.Tensor], device: str = "cpu"): self.max_length = max_length if not isinstance(eos_token_id, torch.Tensor): @@ -1666,7 +1666,7 @@ class ExponentialDecayLengthPenalty(LogitsProcessor): exponential_decay_length_penalty (`tuple(int, float)`): This tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. input_ids_seq_length (`int`): The length of the input sequence. @@ -1726,8 +1726,8 @@ class ExponentialDecayLengthPenalty(LogitsProcessor): def __init__( self, - exponential_decay_length_penalty: Tuple[int, float], - eos_token_id: Union[int, List[int], torch.Tensor], + exponential_decay_length_penalty: tuple[int, float], + eos_token_id: Union[int, list[int], torch.Tensor], input_ids_seq_length: int, ): self.regulation_start = exponential_decay_length_penalty[0] + input_ids_seq_length @@ -2326,13 +2326,13 @@ class BarkEosPrioritizerLogitsProcessor(LogitsProcessor): Args: - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. min_eos_p (`float`, *optional*): Minimum end of speech threshold. """ - def __init__(self, eos_token_id: Union[int, List[int], torch.Tensor], min_eos_p: float, device: str = "cpu"): + def __init__(self, eos_token_id: Union[int, list[int], torch.Tensor], min_eos_p: float, device: str = "cpu"): if not isinstance(eos_token_id, torch.Tensor): if isinstance(eos_token_id, int): eos_token_id = [eos_token_id] @@ -2569,7 +2569,7 @@ class SynthIDTextWatermarkLogitsProcessor(LogitsProcessor): Args: ngram_len (`int`): Ngram length. - keys (`List[int]`): + keys (`list[int]`): A sequence of watermarking keys, one for each depth. sampling_table_size (`int`): Size of the sampling table. @@ -2610,7 +2610,7 @@ class SynthIDTextWatermarkLogitsProcessor(LogitsProcessor): def __init__( self, ngram_len: int, - keys: List[int], + keys: list[int], sampling_table_size: int, sampling_table_seed: int, context_history_size: int, @@ -2808,7 +2808,7 @@ class SynthIDTextWatermarkLogitsProcessor(LogitsProcessor): def _compute_keys( self, n_minus_1_grams: torch.LongTensor, indices: torch.LongTensor - ) -> Tuple[torch.LongTensor, torch.LongTensor]: + ) -> tuple[torch.LongTensor, torch.LongTensor]: """Computes random keys for each ngram and depth. Args: diff --git a/src/transformers/generation/stopping_criteria.py b/src/transformers/generation/stopping_criteria.py index 648b897a1c1..2b9e57aacd8 100644 --- a/src/transformers/generation/stopping_criteria.py +++ b/src/transformers/generation/stopping_criteria.py @@ -3,7 +3,7 @@ import warnings from abc import ABC from collections import OrderedDict from copy import deepcopy -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -33,7 +33,7 @@ STOPPING_CRITERIA_INPUTS_DOCSTRING = r""" Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax or scores for each vocabulary token after SoftMax. If this stopping criteria depends on the `scores` input, make sure you pass `return_dict_in_generate=True, output_scores=True` to `generate`. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional stopping criteria specific kwargs. Return: @@ -209,7 +209,7 @@ class StopStringCriteria(StoppingCriteria): Args: tokenizer (`PreTrainedTokenizer`): The model's associated tokenizer (necessary to extract vocab and tokenize the termination sequences) - stop_strings (`Union[str, List[str]]`): + stop_strings (`Union[str, list[str]]`): A list of strings that should end generation. If a string is passed, it will be treated like a list with a single element. @@ -239,10 +239,10 @@ class StopStringCriteria(StoppingCriteria): ``` """ - def __init__(self, tokenizer: PreTrainedTokenizerBase, stop_strings: Union[str, List[str]]): + def __init__(self, tokenizer: PreTrainedTokenizerBase, stop_strings: Union[str, list[str]]): if isinstance(stop_strings, str): stop_strings = [stop_strings] - self.stop_strings: Tuple[str, ...] = tuple(stop_strings) + self.stop_strings: tuple[str, ...] = tuple(stop_strings) vocab = tokenizer.get_vocab() token_list, token_indices = tuple(vocab.keys()), tuple(vocab.values()) self.embedding_vec, self.max_valid_positions, self.max_valid_end_lens = self.clean_and_embed_tokens_with_cache( @@ -298,7 +298,7 @@ class StopStringCriteria(StoppingCriteria): @staticmethod def _stop_string_get_matching_positions( token_list, token_indices, stop_strings - ) -> Tuple[Dict[str, Dict[str, List[int]]], Dict[str, Dict[str, List[int]]]]: + ) -> tuple[dict[str, dict[str, list[int]]], dict[str, dict[str, list[int]]]]: """This function preprocesses stop strings and the tokenizer vocabulary to determine where tokens can validly appear in the stop strings. For each token, it computes a list of positions in the stop string where the token appears, as well as a list of the possible "end overlaps" for that token - that is, the number of characters @@ -337,7 +337,7 @@ class StopStringCriteria(StoppingCriteria): return token_valid_positions, token_end_overlaps @staticmethod - def _stop_string_create_embedding_vec(token_list, token_indices, stop_strings) -> Dict[str, torch.tensor]: + def _stop_string_create_embedding_vec(token_list, token_indices, stop_strings) -> dict[str, torch.tensor]: """This function precomputes everything needed for the run-time checks in StopStringCriteria, and packs them into an embedding tensor that can be accessed with pure tensor operations. For the specifics of the values that are precomputed and what they are used for, please refer to the StopStringCriteria docstring!""" @@ -455,11 +455,11 @@ class EosTokenCriteria(StoppingCriteria): By default, it uses the `model.generation_config.eos_token_id`. Args: - eos_token_id (`Union[int, List[int], torch.Tensor]`): + eos_token_id (`Union[int, list[int], torch.Tensor]`): The id(s) of the *end-of-sequence* token. """ - def __init__(self, eos_token_id: Union[int, List[int], torch.Tensor]): + def __init__(self, eos_token_id: Union[int, list[int], torch.Tensor]): if not isinstance(eos_token_id, torch.Tensor): if isinstance(eos_token_id, int): eos_token_id = [eos_token_id] diff --git a/src/transformers/generation/tf_logits_process.py b/src/transformers/generation/tf_logits_process.py index 49c4a55f8e4..c88ea5afcc9 100644 --- a/src/transformers/generation/tf_logits_process.py +++ b/src/transformers/generation/tf_logits_process.py @@ -14,7 +14,6 @@ # limitations under the License. import inspect -from typing import List, Tuple import numpy as np import tensorflow as tf @@ -42,7 +41,7 @@ TF_LOGITS_PROCESSOR_INPUTS_DOCSTRING = r""" cur_len (`int`): The current length of valid input sequence tokens. In the TF implementation, the input_ids' sequence length is the maximum length generate can produce, and we need to know which of its tokens are valid. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional logits processor specific kwargs. Return: @@ -290,7 +289,7 @@ class TFNoBadWordsLogitsProcessor(TFLogitsProcessor): [`TFLogitsProcessor`] that enforces that specified sequences will never be sampled. Args: - bad_words_ids (`List[List[int]]`): + bad_words_ids (`list[list[int]]`): List of list of token ids that are not allowed to be generated. In order to get the tokens of the words that should not appear in the generated text, make sure to set `add_prefix_space=True` when initializing the tokenizer, and use `tokenizer(bad_words, add_special_tokens=False).input_ids`. The `add_prefix_space` @@ -300,8 +299,8 @@ class TFNoBadWordsLogitsProcessor(TFLogitsProcessor): The id of the *end-of-sequence* token. """ - def __init__(self, bad_words_ids: List[List[int]], eos_token_id: int): - if not isinstance(bad_words_ids, List) or len(bad_words_ids) == 0: + def __init__(self, bad_words_ids: list[list[int]], eos_token_id: int): + if not isinstance(bad_words_ids, list) or len(bad_words_ids) == 0: raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.") if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids): raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.") @@ -370,7 +369,7 @@ class TFNoBadWordsLogitsProcessor(TFLogitsProcessor): # To remain simple and XLA-compatible, we work on a per-row fashion. # TODO (Joao): this function might trigger XLA retracing as `cur_len` increases. Fix it if it becomes # a frequent choke point. (make `cur_len` a tensor?) - def _get_row_updated_score(row_inputs: Tuple[tf.Tensor]) -> tf.Tensor: + def _get_row_updated_score(row_inputs: tuple[tf.Tensor]) -> tf.Tensor: row_input_ids, row_score = row_inputs banned_tokens = self._calc_row_banned_bad_tokens(row_input_ids[:cur_len]) banned_tokens_mask = tf.scatter_nd( @@ -565,7 +564,7 @@ class TFForceTokensLogitsProcessor(TFLogitsProcessor): indices that will be forced before sampling. The processor will set their log probs to `0` and all other tokens to `-inf` so that they are sampled at their corresponding index.""" - def __init__(self, force_token_map: List[List[int]]): + def __init__(self, force_token_map: list[list[int]]): force_token_map = dict(force_token_map) # Converts the dictionary of format {index: token} containing the tokens to be forced to an array, where the # index of the array corresponds to the index of the token to be forced, for XLA compatibility. diff --git a/src/transformers/generation/tf_utils.py b/src/transformers/generation/tf_utils.py index ae77f32e269..be51c9cd9f4 100644 --- a/src/transformers/generation/tf_utils.py +++ b/src/transformers/generation/tf_utils.py @@ -18,7 +18,7 @@ import copy import inspect import warnings from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -77,9 +77,9 @@ class TFGreedySearchDecoderOnlyOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -116,12 +116,12 @@ class TFGreedySearchEncoderDecoderOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - encoder_attentions: Optional[Tuple[tf.Tensor]] = None - encoder_hidden_states: Optional[Tuple[tf.Tensor]] = None - decoder_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - cross_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + encoder_attentions: Optional[tuple[tf.Tensor]] = None + encoder_hidden_states: Optional[tuple[tf.Tensor]] = None + decoder_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + cross_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -147,9 +147,9 @@ class TFSampleDecoderOnlyOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -186,12 +186,12 @@ class TFSampleEncoderDecoderOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - encoder_attentions: Optional[Tuple[tf.Tensor]] = None - encoder_hidden_states: Optional[Tuple[tf.Tensor]] = None - decoder_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - cross_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + encoder_attentions: Optional[tuple[tf.Tensor]] = None + encoder_hidden_states: Optional[tuple[tf.Tensor]] = None + decoder_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + cross_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -223,10 +223,10 @@ class TFBeamSearchDecoderOnlyOutput(ModelOutput): sequences: Optional[tf.Tensor] = None sequences_scores: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None + scores: Optional[tuple[tf.Tensor]] = None beam_indices: Optional[tf.Tensor] = None - attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + attentions: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -270,13 +270,13 @@ class TFBeamSearchEncoderDecoderOutput(ModelOutput): sequences: Optional[tf.Tensor] = None sequences_scores: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None + scores: Optional[tuple[tf.Tensor]] = None beam_indices: Optional[tf.Tensor] = None - encoder_attentions: Optional[Tuple[tf.Tensor]] = None - encoder_hidden_states: Optional[Tuple[tf.Tensor]] = None - decoder_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - cross_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + encoder_attentions: Optional[tuple[tf.Tensor]] = None + encoder_hidden_states: Optional[tuple[tf.Tensor]] = None + decoder_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + cross_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -308,10 +308,10 @@ class TFBeamSampleDecoderOnlyOutput(ModelOutput): sequences: Optional[tf.Tensor] = None sequences_scores: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None + scores: Optional[tuple[tf.Tensor]] = None beam_indices: Optional[tf.Tensor] = None - attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + attentions: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -354,13 +354,13 @@ class TFBeamSampleEncoderDecoderOutput(ModelOutput): sequences: Optional[tf.Tensor] = None sequences_scores: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None + scores: Optional[tuple[tf.Tensor]] = None beam_indices: Optional[tf.Tensor] = None - encoder_attentions: Optional[Tuple[tf.Tensor]] = None - encoder_hidden_states: Optional[Tuple[tf.Tensor]] = None - decoder_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - cross_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + encoder_attentions: Optional[tuple[tf.Tensor]] = None + encoder_hidden_states: Optional[tuple[tf.Tensor]] = None + decoder_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + cross_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -385,9 +385,9 @@ class TFContrastiveSearchDecoderOnlyOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None @dataclass @@ -423,12 +423,12 @@ class TFContrastiveSearchEncoderDecoderOutput(ModelOutput): """ sequences: Optional[tf.Tensor] = None - scores: Optional[Tuple[tf.Tensor]] = None - encoder_attentions: Optional[Tuple[tf.Tensor]] = None - encoder_hidden_states: Optional[Tuple[tf.Tensor]] = None - decoder_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - cross_attentions: Optional[Tuple[Tuple[tf.Tensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[tf.Tensor]]] = None + scores: Optional[tuple[tf.Tensor]] = None + encoder_attentions: Optional[tuple[tf.Tensor]] = None + encoder_hidden_states: Optional[tuple[tf.Tensor]] = None + decoder_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + cross_attentions: Optional[tuple[tuple[tf.Tensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[tf.Tensor]]] = None TFGreedySearchOutput = Union[TFGreedySearchEncoderDecoderOutput, TFGreedySearchDecoderOnlyOutput] @@ -477,7 +477,7 @@ class TFGenerationMixin: def compute_transition_scores( self, sequences: tf.Tensor, - scores: Tuple[tf.Tensor], + scores: tuple[tf.Tensor], beam_indices: Optional[tf.Tensor] = None, normalize_logits: bool = False, ) -> tf.Tensor: @@ -619,7 +619,7 @@ class TFGenerationMixin: exception_message += f" Please use one of the following classes instead: {generate_compatible_classes}" raise TypeError(exception_message) - def _validate_model_kwargs(self, model_kwargs: Dict[str, Any]): + def _validate_model_kwargs(self, model_kwargs: dict[str, Any]): """Validates model kwargs for generation. Generate argument typos will also be caught here.""" # Excludes arguments that are handled before calling any model function if self.config.is_encoder_decoder: @@ -681,10 +681,10 @@ class TFGenerationMixin: Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - seed (`List[int]`, *optional*): + seed (`list[int]`, *optional*): Random seed to control sampling, containing two integers, used when `do_sample` is `True`. See the `seed` argument from stateless functions in `tf.random`. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. @@ -1044,7 +1044,7 @@ class TFGenerationMixin: def _prepare_encoder_decoder_kwargs_for_generation( self, inputs_tensor: tf.Tensor, model_kwargs, model_input_name: Optional[str] = None - ) -> Dict[str, Any]: + ) -> dict[str, Any]: # 1. get encoder and store encoder outputs encoder = self.get_encoder() @@ -1076,10 +1076,10 @@ class TFGenerationMixin: self, batch_size: int, model_input_name: str, - model_kwargs: Dict[str, tf.Tensor], + model_kwargs: dict[str, tf.Tensor], decoder_start_token_id: Optional[int] = None, bos_token_id: Optional[int] = None, - ) -> Tuple[tf.Tensor, Dict[str, tf.Tensor]]: + ) -> tuple[tf.Tensor, dict[str, tf.Tensor]]: """Prepares `decoder_input_ids` for generation with encoder-decoder models""" # 1. Check whether the user has defined `decoder_input_ids` manually. To facilitate in terms of input naming, # we also allow the user to pass it under `input_ids`, if the encoder does not use it as the main input. @@ -1138,7 +1138,7 @@ class TFGenerationMixin: input_ids: Optional[tf.Tensor] = None, expand_in_new_axis: bool = False, **model_kwargs, - ) -> Tuple[tf.Tensor, Dict[str, Any]]: + ) -> tuple[tf.Tensor, dict[str, Any]]: """ Expands tensors from [batch_size, ...] to [batch_size * expand_size, ...] or [batch_size, expand_size, ...], depending on `expand_in_new_axis`. Beam-based approaches expect this function to be used with @@ -1174,8 +1174,8 @@ class TFGenerationMixin: self, inputs: Optional[tf.Tensor] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, tf.Tensor]] = None, - ) -> Tuple[tf.Tensor, Optional[str], Dict[str, tf.Tensor]]: + model_kwargs: Optional[dict[str, tf.Tensor]] = None, + ) -> tuple[tf.Tensor, Optional[str], dict[str, tf.Tensor]]: """ This function extracts the model-specific `inputs` for generation. """ @@ -1240,7 +1240,7 @@ class TFGenerationMixin: self, inputs: Optional[tf.Tensor] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, tf.Tensor]] = None, + model_kwargs: Optional[dict[str, tf.Tensor]] = None, ) -> tf.Tensor: """Initializes input ids for generation, if necessary.""" if inputs is not None: @@ -1276,8 +1276,8 @@ class TFGenerationMixin: return past_key_values def _update_model_kwargs_for_generation( - self, outputs: ModelOutput, model_kwargs: Dict[str, Any], is_encoder_decoder: bool = False - ) -> Dict[str, Any]: + self, outputs: ModelOutput, model_kwargs: dict[str, Any], is_encoder_decoder: bool = False + ) -> dict[str, Any]: # update past_key_values model_kwargs["past_key_values"] = self._extract_past_from_model_output(outputs) @@ -1294,7 +1294,7 @@ class TFGenerationMixin: def _update_model_kwargs_for_xla_generation( self, model_outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], cur_len: int, max_length: int, batch_size: int, @@ -1550,7 +1550,7 @@ class TFGenerationMixin: The maximum length of the sequence to be generated. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. output_attentions (`bool`, *optional*, defaults to `False`): Whether or not to return the attentions tensors of all attention layers. See `attentions` under @@ -1794,7 +1794,7 @@ class TFGenerationMixin: max_length: Optional[int] = None, pad_token_id: Optional[int] = None, eos_token_id: Optional[int] = None, - seed: Optional[Tuple[int, int]] = None, + seed: Optional[tuple[int, int]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, output_scores: Optional[bool] = None, @@ -1818,9 +1818,9 @@ class TFGenerationMixin: The maximum length of the sequence to be generated. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. - seed (`List[int]`, *optional*): + seed (`list[int]`, *optional*): Random seed to control sampling, containing two integers, used when `do_sample` is `True`. See the `seed` argument from stateless functions in `tf.random`. output_attentions (`bool`, *optional*, defaults to `False`): @@ -2128,7 +2128,7 @@ class TFGenerationMixin: The maximum length of the sequence to be generated. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. length_penalty (`float`, *optional*, defaults to 1.0): Exponential penalty to the length that is used with beam-based generation. It is applied as an exponent @@ -2719,7 +2719,7 @@ class TFGenerationMixin: The maximum length of the sequence to be generated. pad_token_id (`int`, *optional*): The id of the *padding* token. - eos_token_id (`Union[int, List[int]]`, *optional*): + eos_token_id (`Union[int, list[int]]`, *optional*): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. output_attentions (`bool`, *optional*, defaults to `False`): Whether or not to return the attentions tensors of all attention layers. See `attentions` under diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index 7be31637bfd..484b4954cfa 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -18,7 +18,7 @@ import inspect import os import warnings from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import numpy as np import torch @@ -169,11 +169,11 @@ class GenerateDecoderOnlyOutput(ModelOutput): """ sequences: torch.LongTensor - scores: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - past_key_values: Optional[Tuple[Tuple[Tuple[torch.FloatTensor]]]] = None + scores: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[tuple[torch.FloatTensor]]] = None + past_key_values: Optional[tuple[tuple[tuple[torch.FloatTensor]]]] = None @dataclass @@ -214,14 +214,14 @@ class GenerateEncoderDecoderOutput(ModelOutput): """ sequences: torch.LongTensor - scores: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - cross_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - past_key_values: Optional[Tuple[Tuple[Tuple[torch.FloatTensor]]]] = None + scores: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + cross_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[torch.FloatTensor]]] = None + past_key_values: Optional[tuple[tuple[tuple[torch.FloatTensor]]]] = None @dataclass @@ -260,12 +260,12 @@ class GenerateBeamDecoderOnlyOutput(ModelOutput): sequences: torch.LongTensor sequences_scores: Optional[torch.FloatTensor] = None - scores: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + scores: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None beam_indices: Optional[torch.LongTensor] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - past_key_values: Optional[Tuple[Tuple[Tuple[torch.FloatTensor]]]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[tuple[torch.FloatTensor]]] = None + past_key_values: Optional[tuple[tuple[tuple[torch.FloatTensor]]]] = None @dataclass @@ -314,15 +314,15 @@ class GenerateBeamEncoderDecoderOutput(ModelOutput): sequences: torch.LongTensor sequences_scores: Optional[torch.FloatTensor] = None - scores: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + scores: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None beam_indices: Optional[torch.LongTensor] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - cross_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - decoder_hidden_states: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - past_key_values: Optional[Tuple[Tuple[Tuple[torch.FloatTensor]]]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + cross_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[tuple[torch.FloatTensor]]] = None + past_key_values: Optional[tuple[tuple[tuple[torch.FloatTensor]]]] = None # TODO (joao): remove the equivalent classes and typing shortcuts below in v5 @@ -457,7 +457,7 @@ class GenerationMixin(ContinuousMixin): input_ids: torch.LongTensor, inputs_embeds: Optional[torch.FloatTensor], cache_position: Optional[torch.LongTensor], - ) -> Tuple[torch.FloatTensor, torch.LongTensor]: + ) -> tuple[torch.FloatTensor, torch.LongTensor]: """ Generic cache-dependent input preparation The code is put in a separate function to allow granular unit testing @@ -491,7 +491,7 @@ class GenerationMixin(ContinuousMixin): input_ids: torch.LongTensor, inputs_embeds: Optional[torch.FloatTensor], cache_position: Optional[torch.LongTensor], - ) -> Tuple[torch.FloatTensor, torch.LongTensor]: + ) -> tuple[torch.FloatTensor, torch.LongTensor]: """ This method implements method ``_cache_dependant_input_preparation`` with :func:`torch.cond` to make it exportable with :func:`torch.export.export`. @@ -697,8 +697,8 @@ class GenerationMixin(ContinuousMixin): self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[torch.Tensor] = None, - model_kwargs: Optional[Dict[str, torch.Tensor]] = None, - ) -> Tuple[torch.Tensor, Optional[str], Dict[str, torch.Tensor]]: + model_kwargs: Optional[dict[str, torch.Tensor]] = None, + ) -> tuple[torch.Tensor, Optional[str], dict[str, torch.Tensor]]: """ This function extracts the model-specific `inputs` for generation. """ @@ -761,7 +761,7 @@ class GenerationMixin(ContinuousMixin): self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[torch.Tensor] = None, - model_kwargs: Optional[Dict[str, torch.Tensor]] = None, + model_kwargs: Optional[dict[str, torch.Tensor]] = None, ) -> torch.LongTensor: """Initializes input ids for generation, if necessary.""" if inputs is not None: @@ -793,7 +793,7 @@ class GenerationMixin(ContinuousMixin): self, inputs_tensor: torch.Tensor, generation_config: GenerationConfig, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], ) -> torch.LongTensor: pad_token_id = generation_config._pad_token_tensor eos_token_id = generation_config._eos_token_tensor @@ -831,7 +831,7 @@ class GenerationMixin(ContinuousMixin): model_kwargs, model_input_name: Optional[str], generation_config: GenerationConfig, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: # 1. get encoder encoder = self.get_encoder() # Compatibility with Accelerate big model inference: we need the encoder to outputs stuff on the same device @@ -870,10 +870,10 @@ class GenerationMixin(ContinuousMixin): self, batch_size: int, model_input_name: str, - model_kwargs: Dict[str, torch.Tensor], + model_kwargs: dict[str, torch.Tensor], decoder_start_token_id: torch.Tensor, device: Optional[torch.device] = None, - ) -> Tuple[torch.LongTensor, Dict[str, torch.Tensor]]: + ) -> tuple[torch.LongTensor, dict[str, torch.Tensor]]: """Prepares `decoder_input_ids` for generation with encoder-decoder models""" # 1. Check whether the user has defined `decoder_input_ids` manually. To facilitate in terms of input naming, # we also allow the user to pass it under `input_ids`, if the encoder does not use it as the main input. @@ -931,7 +931,7 @@ class GenerationMixin(ContinuousMixin): is_encoder_decoder: bool = False, input_ids: Optional[torch.LongTensor] = None, **model_kwargs, - ) -> Tuple[torch.LongTensor, Dict[str, Any]]: + ) -> tuple[torch.LongTensor, dict[str, Any]]: """Expands tensors from [batch_size, ...] to [batch_size * expand_size, ...]""" # Do not call torch.repeat_interleave if expand_size is 1 because it clones # the input tensor and thus requires more memory although no change is applied @@ -963,10 +963,10 @@ class GenerationMixin(ContinuousMixin): def _update_model_kwargs_for_generation( self, outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], is_encoder_decoder: bool = False, num_new_tokens: int = 1, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: # update past_key_values keeping its naming used in model code for possible_cache_name in ALL_CACHE_NAMES: if possible_cache_name in outputs: @@ -1024,7 +1024,7 @@ class GenerationMixin(ContinuousMixin): logits_processor: LogitsProcessorList, target_tokenizer: "PreTrainedTokenizerBase", assistant_tokenizer: "PreTrainedTokenizerBase", - model_kwargs: Dict, + model_kwargs: dict, ) -> CandidateGenerator: """ Returns the candidate generator to be used in `assisted_generation` @@ -1100,10 +1100,10 @@ class GenerationMixin(ContinuousMixin): generation_config: GenerationConfig, input_ids_seq_length: Optional[int] = None, encoder_input_ids: torch.LongTensor = None, - prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None, + prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], list[int]]] = None, logits_processor: Optional[LogitsProcessorList] = None, device: Optional[str] = None, - model_kwargs: Optional[Dict[str, Any]] = None, + model_kwargs: Optional[dict[str, Any]] = None, negative_prompt_ids: Optional[torch.Tensor] = None, negative_prompt_attention_mask: Optional[torch.Tensor] = None, ) -> LogitsProcessorList: @@ -1403,7 +1403,7 @@ class GenerationMixin(ContinuousMixin): def compute_transition_scores( self, sequences: torch.Tensor, - scores: Tuple[torch.Tensor], + scores: tuple[torch.Tensor], beam_indices: Optional[torch.Tensor] = None, normalize_logits: bool = False, ) -> torch.Tensor: @@ -1552,7 +1552,7 @@ class GenerationMixin(ContinuousMixin): f"The main and assistant moedels have different tokenizers. Please provide `tokenizer` and `assistant_tokenizer` to `generate()` {doc_reference}." ) - def _validate_model_kwargs(self, model_kwargs: Dict[str, Any]): + def _validate_model_kwargs(self, model_kwargs: dict[str, Any]): """Validates model kwargs for generation. Generate argument typos will also be caught here.""" # If a `Cache` instance is passed, checks whether the model is compatible with it if isinstance(model_kwargs.get("past_key_values", None), Cache) and not self._supports_cache_class: @@ -1709,8 +1709,8 @@ class GenerationMixin(ContinuousMixin): return generation_config def _prepare_generation_config( - self, generation_config: Optional[GenerationConfig], use_model_defaults: Optional[bool] = None, **kwargs: Dict - ) -> Tuple[GenerationConfig, Dict]: + self, generation_config: Optional[GenerationConfig], use_model_defaults: Optional[bool] = None, **kwargs: dict + ) -> tuple[GenerationConfig, dict]: """ Prepares the base generation config, then applies any generation configuration options from kwargs. This function handles retrocompatibility with respect to configuration files. @@ -1821,7 +1821,7 @@ class GenerationMixin(ContinuousMixin): model_kwargs["cache_position"] = cache_position return model_kwargs - def _get_layer_device_map_for_cache_init(self) -> Optional[Dict[int, Union[str, int]]]: + def _get_layer_device_map_for_cache_init(self) -> Optional[dict[int, Union[str, int]]]: """ Returns the device map for each decoder layer, to allocate the cache on the right device. Inspired from `dispatch_model` in accelerate. @@ -1982,7 +1982,7 @@ class GenerationMixin(ContinuousMixin): def _prepare_cache_for_generation( self, generation_config: GenerationConfig, - model_kwargs: Dict, + model_kwargs: dict, assistant_model: "PreTrainedModel", batch_size: int, max_cache_length: int, @@ -2191,7 +2191,7 @@ class GenerationMixin(ContinuousMixin): generation_config._pad_token_tensor = pad_token_tensor generation_config._decoder_start_token_tensor = decoder_start_token_tensor - def _valid_auto_compile_criteria(self, model_kwargs: Dict, generation_config: GenerationConfig) -> bool: + def _valid_auto_compile_criteria(self, model_kwargs: dict, generation_config: GenerationConfig) -> bool: """ Determines whether to trigger auto-compilation of the model's forward pass at generation time. """ @@ -2239,7 +2239,7 @@ class GenerationMixin(ContinuousMixin): generation_config: Optional[GenerationConfig] = None, logits_processor: Optional[LogitsProcessorList] = None, stopping_criteria: Optional[StoppingCriteriaList] = None, - prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None, + prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], list[int]]] = None, synced_gpus: Optional[bool] = None, assistant_model: Optional["PreTrainedModel"] = None, streamer: Optional["BaseStreamer"] = None, @@ -2287,7 +2287,7 @@ class GenerationMixin(ContinuousMixin): generation config an error is thrown. If your stopping criteria depends on the `scores` input, make sure you pass `return_dict_in_generate=True, output_scores=True` to `generate`. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -2321,7 +2321,7 @@ class GenerationMixin(ContinuousMixin): function defined in that reposity's `custom_generate/generate.py` file will be executed instead of the standard `generate` method. Note that the logic is for generation is entirely defined in that repository, and the return type may be different from the standard `generate` method. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generation_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. @@ -2695,7 +2695,7 @@ class GenerationMixin(ContinuousMixin): def typeerror(): raise ValueError( - "`force_words_ids` has to either be a `List[List[List[int]]]` or `List[List[int]]` " + "`force_words_ids` has to either be a `list[list[list[int]]]` or `list[list[int]]` " f"of positive integers, but is {generation_config.force_words_ids}." ) @@ -2871,7 +2871,7 @@ class GenerationMixin(ContinuousMixin): def _dola_decoding( self, input_ids: torch.LongTensor, - dola_layers: Union[str, List[int]], + dola_layers: Union[str, list[int]], logits_processor: LogitsProcessorList, stopping_criteria: StoppingCriteriaList, generation_config: GenerationConfig, @@ -2888,7 +2888,7 @@ class GenerationMixin(ContinuousMixin): Parameters: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): The sequence used as a prompt for the generation. - dola_layers (`Union[str, List[int]]`): + dola_layers (`Union[str, list[int]]`): The candidate layers used in contrasting layers of DoLa. It can be either 1) 'low' or 'high', which means the lower part or higher part of the model layers, respectively, or 2) a list of layer indices to be used for candidate layers. The 0-th layer is the word embedding layer of the model. @@ -3806,7 +3806,7 @@ class GenerationMixin(ContinuousMixin): num_beams: int, vocab_size: int, batch_size: int, - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Get top-K continuations given the accumulated log probs on the next token. @@ -3855,7 +3855,7 @@ class GenerationMixin(ContinuousMixin): topk_running_beam_indices: torch.Tensor, next_token_hits_stopping_criteria: torch.Tensor, num_beams: int, - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Given the top-K continuations, their scores, and whether they hit a stopping criteria, select the best non-finished beams to continue beam search in the next iteration. @@ -3886,7 +3886,7 @@ class GenerationMixin(ContinuousMixin): decoder_prompt_len: int, length_penalty: float, early_stopping: Union[bool, str], - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: """ Updates the finished beams if (and only if) there are new completed sequences that have a higher score than the current finished sequences. @@ -5236,8 +5236,8 @@ def _split(data, full_batch_size: int, split_size: int): def _split_model_inputs( - model_input: Union[ModelOutput, Dict], split_size: int, full_batch_size: int, config: PretrainedConfig -) -> List[Union[ModelOutput, Dict]]: + model_input: Union[ModelOutput, dict], split_size: int, full_batch_size: int, config: PretrainedConfig +) -> list[Union[ModelOutput, dict]]: """ Split a ModelOutput object (or its subclasses) or Dict into a list of same-class objects based on a specified split size. The input object is dict when it was prepared for forward pass and ModelOutput when it was returned from @@ -5292,14 +5292,14 @@ def _split_model_inputs( ] # Convert each dictionary in the list to an object of the inferred class - split_model_inputs: List[Union[ModelOutput, Dict]] = [ + split_model_inputs: list[Union[ModelOutput, dict]] = [ model_output_cls(**data_split, **bool_data) for data_split in data_split_list ] return split_model_inputs -def stack_model_outputs(model_outputs: List[ModelOutput], config: PretrainedConfig) -> ModelOutput: +def stack_model_outputs(model_outputs: list[ModelOutput], config: PretrainedConfig) -> ModelOutput: """ Stack a list of ModelOutput objects (or its subclasses) along the batch_size dimension. The function infers the specific ModelOutput subclass from the list provided. @@ -5379,8 +5379,8 @@ def _relative_top_filter( def _dola_select_contrast( - candidate_premature_layers: List[int], - candidate_premature_logits: Dict[int, torch.FloatTensor], + candidate_premature_layers: list[int], + candidate_premature_logits: dict[int, torch.FloatTensor], final_logits: torch.FloatTensor, ) -> torch.FloatTensor: if len(candidate_premature_layers) == 1: diff --git a/src/transformers/generation/watermarking.py b/src/transformers/generation/watermarking.py index 759e46631d1..16993a949a0 100644 --- a/src/transformers/generation/watermarking.py +++ b/src/transformers/generation/watermarking.py @@ -16,7 +16,7 @@ import collections from dataclasses import dataclass from functools import lru_cache -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import torch @@ -126,7 +126,7 @@ class WatermarkDetector: self, model_config: PretrainedConfig, device: str, - watermarking_config: Union[WatermarkingConfig, Dict], + watermarking_config: Union[WatermarkingConfig, dict], ignore_repeated_ngrams: bool = False, max_cache_size: int = 128, ): @@ -300,7 +300,7 @@ class BayesianDetectorWatermarkedLikelihood(nn.Module): self.beta = torch.nn.Parameter(-2.5 + 0.001 * torch.randn(1, 1, watermarking_depth)) self.delta = torch.nn.Parameter(0.001 * torch.randn(1, 1, self.watermarking_depth, watermarking_depth)) - def _compute_latents(self, g_values: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def _compute_latents(self, g_values: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """Computes the unique token probability distribution given g-values. Args: diff --git a/src/transformers/hf_argparser.py b/src/transformers/hf_argparser.py index 0c5610dbfdf..e6d92d2baa8 100644 --- a/src/transformers/hf_argparser.py +++ b/src/transformers/hf_argparser.py @@ -81,7 +81,7 @@ def HfArg( ``` Args: - aliases (Union[str, List[str]], optional): + aliases (Union[str, list[str]], optional): Single string or list of strings of aliases to pass on to argparse, e.g. `aliases=["--example", "-e"]`. Defaults to None. help (str, optional): Help string to pass on to argparse that can be displayed with --help. Defaults to None. @@ -119,7 +119,7 @@ class HfArgumentParser(ArgumentParser): Args: dataclass_types (`DataClassType` or `Iterable[DataClassType]`, *optional*): Dataclass type, or list of dataclass types for which we will "fill" instances with the parsed args. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Passed to `argparse.ArgumentParser()` in the regular way. """ diff --git a/src/transformers/image_processing_base.py b/src/transformers/image_processing_base.py index 42a6b785841..4f4597dcff8 100644 --- a/src/transformers/image_processing_base.py +++ b/src/transformers/image_processing_base.py @@ -127,7 +127,7 @@ class ImageProcessingMixin(PushToHubMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -153,7 +153,7 @@ class ImageProcessingMixin(PushToHubMixin): subfolder (`str`, *optional*, defaults to `""`): In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are image processor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* image processor attributes is controlled by the `return_unused_kwargs` keyword parameter. @@ -219,7 +219,7 @@ class ImageProcessingMixin(PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -286,7 +286,7 @@ class ImageProcessingMixin(PushToHubMixin): The name of the file in the model directory to use for the image processor config. Returns: - `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the image processor object. + `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the image processor object. """ cache_dir = kwargs.pop("cache_dir", None) force_download = kwargs.pop("force_download", False) @@ -387,11 +387,11 @@ class ImageProcessingMixin(PushToHubMixin): Instantiates a type of [`~image_processing_utils.ImageProcessingMixin`] from a Python dictionary of parameters. Args: - image_processor_dict (`Dict[str, Any]`): + image_processor_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the image processor object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [`~image_processing_utils.ImageProcessingMixin.to_dict`] method. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the image processor object. Returns: @@ -431,7 +431,7 @@ class ImageProcessingMixin(PushToHubMixin): Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this image processor instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this image processor instance. """ output = copy.deepcopy(self.__dict__) output["image_processor_type"] = self.__class__.__name__ diff --git a/src/transformers/image_processing_utils.py b/src/transformers/image_processing_utils.py index b3acbb3feb7..d3086de1cce 100644 --- a/src/transformers/image_processing_utils.py +++ b/src/transformers/image_processing_utils.py @@ -130,7 +130,7 @@ class BaseImageProcessor(ImageProcessingMixin): Args: image (`np.ndarray`): Image to center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. If unset, the channel dimension format of the input @@ -227,7 +227,7 @@ def get_size_dict( is set, it is added to the dict as `{"longest_edge": max_size}`. Args: - size (`Union[int, Iterable[int], Dict[str, int]]`, *optional*): + size (`Union[int, Iterable[int], dict[str, int]]`, *optional*): The `size` parameter to be cast into a size dictionary. max_size (`Optional[int]`, *optional*): The `max_size` parameter to be cast into a size dictionary. diff --git a/src/transformers/image_processing_utils_fast.py b/src/transformers/image_processing_utils_fast.py index b1e26141220..4bfd208c9bb 100644 --- a/src/transformers/image_processing_utils_fast.py +++ b/src/transformers/image_processing_utils_fast.py @@ -382,7 +382,7 @@ class BaseImageProcessorFast(BaseImageProcessor): Args: image (`"torch.Tensor"`): Image to center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. Returns: @@ -666,12 +666,12 @@ class SemanticSegmentationMixin: Args: outputs ([`MobileNetV2ForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/image_transforms.py b/src/transformers/image_transforms.py index 7f9be1c671c..b936403be7b 100644 --- a/src/transformers/image_transforms.py +++ b/src/transformers/image_transforms.py @@ -217,7 +217,7 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, in Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -266,7 +266,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or List[int] or `Tuple[int]`): + size (`int` or `tuple[int, int]` or list[int] or `tuple[int]`): The size to use for resizing the image. If `size` is a sequence like (h, w), output size will be matched to this. @@ -334,7 +334,7 @@ def resize( Args: image (`np.ndarray`): The image to resize. - size (`Tuple[int, int]`): + size (`tuple[int, int]`): The size to use for resizing the image. resample (`int`, *optional*, defaults to `PILImageResampling.BILINEAR`): The filter to user for resampling. @@ -464,7 +464,7 @@ def center_crop( Args: image (`np.ndarray`): The image to crop. - size (`Tuple[int, int]`): + size (`tuple[int, int]`): The target size for the cropped image. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. Can be one of: @@ -704,7 +704,7 @@ def pad( Args: image (`np.ndarray`): The image to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. diff --git a/src/transformers/image_utils.py b/src/transformers/image_utils.py index c8c6ab79f2b..f3db6fac44b 100644 --- a/src/transformers/image_utils.py +++ b/src/transformers/image_utils.py @@ -218,7 +218,7 @@ def make_flat_list_of_images( Ensure that the output is a flat list of images. If the input is a single image, it is converted to a list of length 1. If the input is a nested list of images, it is converted to a flat list of images. Args: - images (`Union[List[ImageInput], ImageInput]`): + images (`Union[list[ImageInput], ImageInput]`): The input image. Returns: list: A list of images or a 4d array of images. @@ -252,7 +252,7 @@ def make_nested_list_of_images( """ Ensure that the output is a nested list of images. Args: - images (`Union[List[ImageInput], ImageInput]`): + images (`Union[list[ImageInput], ImageInput]`): The input image. Returns: list: A list of list of images or a list of 4d array of images. @@ -300,7 +300,7 @@ def infer_channel_dimension_format( Args: image (`np.ndarray`): The image to infer the channel dimension of. - num_channels (`int` or `Tuple[int, ...]`, *optional*, defaults to `(1, 3)`): + num_channels (`int` or `tuple[int, ...]`, *optional*, defaults to `(1, 3)`): The number of channels of the image. Returns: @@ -393,7 +393,7 @@ def get_image_size_for_max_height_width( - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400) Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The image to resize. max_height (`int`): The maximum allowed height. @@ -678,9 +678,9 @@ class ImageFeatureExtractionMixin: Args: image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`): The image to normalize. - mean (`List[float]` or `np.ndarray` or `torch.Tensor`): + mean (`list[float]` or `np.ndarray` or `torch.Tensor`): The mean (per channel) to use for normalization. - std (`List[float]` or `np.ndarray` or `torch.Tensor`): + std (`list[float]` or `np.ndarray` or `torch.Tensor`): The standard deviation (per channel) to use for normalization. rescale (`bool`, *optional*, defaults to `False`): Whether or not to rescale the image to be between 0 and 1. If a PIL image is provided, scaling will @@ -729,7 +729,7 @@ class ImageFeatureExtractionMixin: Args: image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor`): The image to resize. - size (`int` or `Tuple[int, int]`): + size (`int` or `tuple[int, int]`): The size to use for resizing the image. If `size` is a sequence like (h, w), output size will be matched to this. @@ -797,7 +797,7 @@ class ImageFeatureExtractionMixin: Args: image (`PIL.Image.Image` or `np.ndarray` or `torch.Tensor` of shape (n_channels, height, width) or (height, width, n_channels)): The image to resize. - size (`int` or `Tuple[int, int]`): + size (`int` or `tuple[int, int]`): The size to which crop the image. Returns: diff --git a/src/transformers/integrations/accelerate.py b/src/transformers/integrations/accelerate.py index 5dbff5e9b04..a404af54b24 100644 --- a/src/transformers/integrations/accelerate.py +++ b/src/transformers/integrations/accelerate.py @@ -156,7 +156,7 @@ def find_tied_parameters(model: "nn.Module", **kwargs): model (`torch.nn.Module`): The model to inspect. Returns: - List[List[str]]: A list of lists of parameter names being all tied together. + list[list[str]]: A list of lists of parameter names being all tied together. Example: diff --git a/src/transformers/integrations/awq.py b/src/transformers/integrations/awq.py index 23a418ead55..c09da6c92e6 100644 --- a/src/transformers/integrations/awq.py +++ b/src/transformers/integrations/awq.py @@ -306,7 +306,7 @@ def _fuse_awq_layernorm(fuse_module_names, module, target_cls): Fuse the LayerNorm layers into a target class using autoawq Args: - fuse_module_names (`List[str]`): + fuse_module_names (`list[str]`): The list of module names to fuse module (`nn.Module`): The pytorch parent module that has layernorm modules to fuse @@ -333,7 +333,7 @@ def _fuse_awq_mlp(model, current_module_name, fuse_module_names, module, target_ The input pretrained model current_module_name (`str`): The current submodule name - fuse_module_names (`List[str]`): + fuse_module_names (`list[str]`): The list of module names to fuse. For the MLP layers it has to be an array of length 3 that consists of the 3 MLP layers in the order (gate (dense layer post-attention) / up / down layers) module (`nn.Module`): @@ -374,7 +374,7 @@ def _fuse_awq_attention_layers(model, module, modules_to_fuse, current_module_na The input pretrained model module (`nn.Module`): The pytorch parent module that has layernorm modules to fuse - modules_to_fuse (`List[str]`): + modules_to_fuse (`list[str]`): The module fusing mapping. The dictionary has to contain a field `attention` with attention module names in the correct order: q, k, v, o layer current_module_name (`str`): diff --git a/src/transformers/integrations/bitnet.py b/src/transformers/integrations/bitnet.py index 2af9447f8dc..5deb3011211 100644 --- a/src/transformers/integrations/bitnet.py +++ b/src/transformers/integrations/bitnet.py @@ -398,10 +398,10 @@ def replace_with_bitnet_linear( Parameters: model (`torch.nn.Module`): Input model or `torch.nn.Module` as the function is run recursively. - modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`): + modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `BitLinear`. In practice we keep the `lm_head` in full precision for numerical stability reasons. - current_key_name (`List[`str`]`, *optional*): + current_key_name (`list[`str`]`, *optional*): An array to track the current key of the recursion. This is used to check whether the current key (part of it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or `disk`). diff --git a/src/transformers/integrations/bitsandbytes.py b/src/transformers/integrations/bitsandbytes.py index 5b2c24119ea..0508477cd99 100644 --- a/src/transformers/integrations/bitsandbytes.py +++ b/src/transformers/integrations/bitsandbytes.py @@ -243,10 +243,10 @@ def replace_with_bnb_linear(model, modules_to_not_convert=None, current_key_name Parameters: model (`torch.nn.Module`): Input model or `torch.nn.Module` as the function is run recursively. - modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`): + modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `Linear8bitLt`. In practice we keep the `lm_head` in full precision for numerical stability reasons. - current_key_name (`List[`str`]`, *optional*): + current_key_name (`list[`str`]`, *optional*): An array to track the current key of the recursion. This is used to check whether the current key (part of it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or `disk`). diff --git a/src/transformers/integrations/eetq.py b/src/transformers/integrations/eetq.py index a3d124aa4b7..45b7548e7e2 100644 --- a/src/transformers/integrations/eetq.py +++ b/src/transformers/integrations/eetq.py @@ -93,10 +93,10 @@ def replace_with_eetq_linear( Parameters: model (`torch.nn.Module`): Input model or `torch.nn.Module` as the function is run recursively. - modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`): + modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `EetqLinear`. In practice we keep the `lm_head` in full precision for numerical stability reasons. - current_key_name (`List[`str`]`, *optional*): + current_key_name (`list[`str`]`, *optional*): An array to track the current key of the recursion. This is used to check whether the current key (part of it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or `disk`). diff --git a/src/transformers/integrations/fbgemm_fp8.py b/src/transformers/integrations/fbgemm_fp8.py index ba4faa96c22..c65db3375ca 100644 --- a/src/transformers/integrations/fbgemm_fp8.py +++ b/src/transformers/integrations/fbgemm_fp8.py @@ -251,10 +251,10 @@ def replace_with_fbgemm_fp8_linear( Parameters: model (`torch.nn.Module`): Input model or `torch.nn.Module` as the function is run recursively. - modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`): + modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `FP8Linear`. In practice we keep the `lm_head` in full precision for numerical stability reasons. - current_key_name (`List[`str`]`, *optional*): + current_key_name (`list[`str`]`, *optional*): An array to track the current key of the recursion. This is used to check whether the current key (part of it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or `disk`). diff --git a/src/transformers/integrations/finegrained_fp8.py b/src/transformers/integrations/finegrained_fp8.py index c259a7dbfaf..8156f1045ba 100644 --- a/src/transformers/integrations/finegrained_fp8.py +++ b/src/transformers/integrations/finegrained_fp8.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Tuple +from typing import Optional from ..utils import is_accelerate_available, is_torch_accelerator_available, is_torch_available, logging @@ -45,7 +45,7 @@ def act_quant_kernel(x_ptr, y_ptr, s_ptr, BLOCK_SIZE: tl.constexpr): tl.store(s_ptr + pid, s) -def act_quant(x: torch.Tensor, block_size: int = 128) -> Tuple[torch.Tensor, torch.Tensor]: +def act_quant(x: torch.Tensor, block_size: int = 128) -> tuple[torch.Tensor, torch.Tensor]: assert x.is_contiguous() assert x.shape[-1] % block_size == 0 y = torch.empty_like(x, dtype=torch.float8_e4m3fn) @@ -149,7 +149,7 @@ def w8a8_block_fp8_matmul_triton( B: torch.Tensor, As: torch.Tensor, Bs: torch.Tensor, - block_size: List[int], + block_size: list[int], output_dtype: torch.dtype = torch.float32, ) -> torch.Tensor: """This function performs matrix multiplication with block-wise @@ -231,7 +231,7 @@ def w8a8_block_fp8_matmul_compile( weight_q: torch.Tensor, # [out_features, hidden_dim] input_scale: torch.Tensor, # [batch * seq_len, num_input_groups] weight_scale: torch.Tensor, # [num_weight_blocks_m, num_weight_blocks_n] - block_size: Optional[Tuple[int, int]] = None, # (M=128, N=128) for weights for example + block_size: Optional[tuple[int, int]] = None, # (M=128, N=128) for weights for example output_dtype: torch.dtype = torch.float32, ) -> torch.Tensor: """ @@ -300,7 +300,7 @@ class FP8Linear(nn.Linear): out_features: int, bias: bool = False, dtype=None, - block_size: Optional[Tuple[int, int]] = None, + block_size: Optional[tuple[int, int]] = None, device=None, activation_scheme="dynamic", ): diff --git a/src/transformers/integrations/flash_attention.py b/src/transformers/integrations/flash_attention.py index 4f76e65a847..5a20ba2c8b2 100644 --- a/src/transformers/integrations/flash_attention.py +++ b/src/transformers/integrations/flash_attention.py @@ -1,4 +1,4 @@ -from typing import Optional, Tuple +from typing import Optional import torch @@ -22,7 +22,7 @@ def flash_attention_forward( sliding_window: Optional[int] = None, softcap: Optional[float] = None, **kwargs, -) -> Tuple[torch.Tensor, None]: +) -> tuple[torch.Tensor, None]: if kwargs.get("output_attentions", False) or kwargs.get("head_mask", None) is not None: logger.warning_once( "`flash_attention_2` does not support `output_attentions=True` or `head_mask`." diff --git a/src/transformers/integrations/flex_attention.py b/src/transformers/integrations/flex_attention.py index 1e1228873f1..fa817f6cb9d 100644 --- a/src/transformers/integrations/flex_attention.py +++ b/src/transformers/integrations/flex_attention.py @@ -26,7 +26,7 @@ Citation: # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from packaging import version @@ -106,7 +106,7 @@ def make_flex_block_causal_mask( attention_chunk_size: Optional[int] = None, query_length=None, key_length=None, - offsets: Optional[Tuple[Offset, Offset]] = None, + offsets: Optional[tuple[Offset, Offset]] = None, is_causal: Optional[bool] = True, ) -> "BlockMask": """ @@ -234,7 +234,7 @@ def flex_attention_forward( softcap: Optional[float] = None, head_mask: Optional[torch.Tensor] = None, **kwargs, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: if head_mask is not None: logger.warning_once( "`flex_attention` does not support `head_mask`. Please set your attention to `eager` if you want this feature." diff --git a/src/transformers/integrations/hub_kernels.py b/src/transformers/integrations/hub_kernels.py index 63e0c381e79..d424aa7c6cc 100644 --- a/src/transformers/integrations/hub_kernels.py +++ b/src/transformers/integrations/hub_kernels.py @@ -11,7 +11,7 @@ # 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. -from typing import Dict, Union +from typing import Union from ..utils import is_torchdynamo_compiling @@ -29,7 +29,7 @@ try: _hub_kernels_available = True - _KERNEL_MAPPING: Dict[str, Dict[Union[Device, str], LayerRepository]] = { + _KERNEL_MAPPING: dict[str, dict[Union[Device, str], LayerRepository]] = { "MultiScaleDeformableAttention": { "cuda": LayerRepository( repo_id="kernels-community/deformable-detr", diff --git a/src/transformers/integrations/integration_utils.py b/src/transformers/integrations/integration_utils.py index c414bf03d54..1f20ed2e7e1 100755 --- a/src/transformers/integrations/integration_utils.py +++ b/src/transformers/integrations/integration_utils.py @@ -29,7 +29,7 @@ import tempfile from dataclasses import asdict, fields from enum import Enum from pathlib import Path -from typing import TYPE_CHECKING, Any, Dict, Literal, Optional, Union +from typing import TYPE_CHECKING, Any, Literal, Optional, Union import numpy as np import packaging.version @@ -1692,7 +1692,7 @@ class NeptuneCallback(TrainerCallback): raise Exception("The trainer doesn't have a NeptuneCallback configured.") - def on_log(self, args, state, control, logs: Optional[Dict[str, float]] = None, **kwargs): + def on_log(self, args, state, control, logs: Optional[dict[str, float]] = None, **kwargs): if not state.is_world_process_zero: return diff --git a/src/transformers/integrations/peft.py b/src/transformers/integrations/peft.py index fb927da23d4..3fc1fcff28f 100644 --- a/src/transformers/integrations/peft.py +++ b/src/transformers/integrations/peft.py @@ -16,7 +16,7 @@ import importlib import inspect import re import warnings -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union from packaging import version @@ -100,11 +100,11 @@ class PeftAdapterMixin: max_memory: Optional[str] = None, offload_folder: Optional[str] = None, offload_index: Optional[int] = None, - peft_config: Optional[Dict[str, Any]] = None, - adapter_state_dict: Optional[Dict[str, "torch.Tensor"]] = None, + peft_config: Optional[dict[str, Any]] = None, + adapter_state_dict: Optional[dict[str, "torch.Tensor"]] = None, low_cpu_mem_usage: bool = False, is_trainable: bool = False, - adapter_kwargs: Optional[Dict[str, Any]] = None, + adapter_kwargs: Optional[dict[str, Any]] = None, ) -> None: """ Load adapter weights from file or remote Hub folder. If you are not familiar with adapters and PEFT methods, we @@ -133,7 +133,7 @@ class PeftAdapterMixin: Whether to use authentication token to load the remote folder. Useful to load private repositories that are on HuggingFace Hub. You might need to call `huggingface-cli login` and paste your tokens to cache it. - device_map (`str` or `Dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): + device_map (`str` or `dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the same device. If we only pass the device (*e.g.*, `"cpu"`, `"cuda:1"`, `"mps"`, or a GPU ordinal rank @@ -150,10 +150,10 @@ class PeftAdapterMixin: If the `device_map` contains any value `"disk"`, the folder where we will offload weights. offload_index (`int`, `optional`): `offload_index` argument to be passed to `accelerate.dispatch_model` method. - peft_config (`Dict[str, Any]`, *optional*): + peft_config (`dict[str, Any]`, *optional*): The configuration of the adapter to add, supported adapters are non-prefix tuning and adaption prompts methods. This argument is used in case users directly pass PEFT state dicts - adapter_state_dict (`Dict[str, torch.Tensor]`, *optional*): + adapter_state_dict (`dict[str, torch.Tensor]`, *optional*): The state dict of the adapter to load. This argument is used in case users directly pass PEFT state dicts low_cpu_mem_usage (`bool`, *optional*, defaults to `False`): @@ -162,7 +162,7 @@ class PeftAdapterMixin: is_trainable (`bool`, *optional*, defaults to `False`): Whether the adapter should be trainable or not. If `False`, the adapter will be frozen and can only be used for inference. - adapter_kwargs (`Dict[str, Any]`, *optional*): + adapter_kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the `from_pretrained` method of the adapter config and `find_adapter_config_file` method. """ @@ -348,7 +348,7 @@ class PeftAdapterMixin: self.set_adapter(adapter_name) - def set_adapter(self, adapter_name: Union[List[str], str]) -> None: + def set_adapter(self, adapter_name: Union[list[str], str]) -> None: """ If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT official documentation: https://huggingface.co/docs/peft @@ -356,7 +356,7 @@ class PeftAdapterMixin: Sets a specific adapter by forcing the model to use a that adapter and disable the other adapters. Args: - adapter_name (`Union[List[str], str]`): + adapter_name (`Union[list[str], str]`): The name of the adapter to set. Can be also a list of strings to set multiple adapters. """ check_peft_version(min_version=MIN_PEFT_VERSION) @@ -438,7 +438,7 @@ class PeftAdapterMixin: else: module.disable_adapters = False - def active_adapters(self) -> List[str]: + def active_adapters(self) -> list[str]: """ If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT official documentation: https://huggingface.co/docs/peft @@ -518,7 +518,7 @@ class PeftAdapterMixin: accelerate (i.e. with `device_map=xxx`) Args: - device_map (`str` or `Dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): + device_map (`str` or `dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the same device. If we only pass the device (*e.g.*, `"cpu"`, `"cuda:1"`, `"mps"`, or a GPU ordinal rank @@ -562,12 +562,12 @@ class PeftAdapterMixin: **dispatch_model_kwargs, ) - def delete_adapter(self, adapter_names: Union[List[str], str]) -> None: + def delete_adapter(self, adapter_names: Union[list[str], str]) -> None: """ Delete an adapter's LoRA layers from the underlying model. Args: - adapter_names (`Union[List[str], str]`): + adapter_names (`Union[list[str], str]`): The name(s) of the adapter(s) to delete. Example: diff --git a/src/transformers/integrations/sdpa_attention.py b/src/transformers/integrations/sdpa_attention.py index 247cd282167..3667832061d 100644 --- a/src/transformers/integrations/sdpa_attention.py +++ b/src/transformers/integrations/sdpa_attention.py @@ -1,4 +1,4 @@ -from typing import Optional, Tuple +from typing import Optional import torch @@ -30,7 +30,7 @@ def sdpa_attention_forward( scaling: Optional[float] = None, is_causal: Optional[bool] = None, **kwargs, -) -> Tuple[torch.Tensor, None]: +) -> tuple[torch.Tensor, None]: if kwargs.get("output_attentions", False) or kwargs.get("head_mask", None) is not None: logger.warning_once( "`sdpa` attention does not support `output_attentions=True` or `head_mask`." diff --git a/src/transformers/integrations/sdpa_paged.py b/src/transformers/integrations/sdpa_paged.py index 640db16d0de..558f4a6f715 100644 --- a/src/transformers/integrations/sdpa_paged.py +++ b/src/transformers/integrations/sdpa_paged.py @@ -1,4 +1,4 @@ -from typing import Optional, Tuple +from typing import Optional import torch @@ -25,7 +25,7 @@ def sdpa_attention_paged_forward( scaling: Optional[float] = None, is_causal: Optional[bool] = None, **kwargs, -) -> Tuple[torch.Tensor, None]: +) -> tuple[torch.Tensor, None]: cache = kwargs.pop("cache", None) if cache is not None: key, value = cache.update(key, value, module.layer_idx, **kwargs) diff --git a/src/transformers/integrations/tensor_parallel.py b/src/transformers/integrations/tensor_parallel.py index 83ce593437e..a5e4595ed93 100644 --- a/src/transformers/integrations/tensor_parallel.py +++ b/src/transformers/integrations/tensor_parallel.py @@ -17,7 +17,7 @@ import operator import os import re from functools import partial, reduce -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.distributed as dist @@ -93,7 +93,7 @@ def initialize_tensor_parallelism(tp_plan, tp_size=None): return tp_device, device_map, device_mesh -def _blocks_to_block_sizes(total_size: int, blocks: Union[int, List[int]]) -> List[int]: +def _blocks_to_block_sizes(total_size: int, blocks: Union[int, list[int]]) -> list[int]: """ Convert block count or proportions to block sizes. @@ -101,7 +101,7 @@ def _blocks_to_block_sizes(total_size: int, blocks: Union[int, List[int]]) -> Li - The number of blocks (int), in which case the block size is total_size//blocks; or - - A list of block sizes (List[int]). + - A list of block sizes (list[int]). In the second case, if sum(blocks) < total_size, the ratios between the block sizes will be preserved. For instance, if blocks is @@ -608,7 +608,7 @@ class RowwiseParallel(TensorParallelLayer): if self.use_dtensor: if isinstance(module, nn.Linear): # rowwise linear runtime sharding requires input tensor shard on last dim - self.desired_input_layouts: Tuple[Placement, ...] = (Shard(-1),) + self.desired_input_layouts: tuple[Placement, ...] = (Shard(-1),) elif isinstance(module, nn.Embedding): # rowwise embedding runtime sharding requires input tensor replicated self.desired_input_layouts = (Replicate(),) diff --git a/src/transformers/integrations/vptq.py b/src/transformers/integrations/vptq.py index f76bd703772..643fa91e652 100644 --- a/src/transformers/integrations/vptq.py +++ b/src/transformers/integrations/vptq.py @@ -35,7 +35,7 @@ def replace_with_vptq_linear( The model to convert, can be any `torch.nn.Module` instance. quantization_config (`VptqConfig`): The quantization config object that contains the quantization parameters. - modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`): + modules_to_not_convert (`list[`str`]`, *optional*, defaults to `["lm_head"]`): Names of the modules to not convert in `VQuantLinear`. In practice we keep the `lm_head` in full precision for numerical stability reasons. current_key_name (`list`, *optional*): diff --git a/src/transformers/keras_callbacks.py b/src/transformers/keras_callbacks.py index 57e72aea7e6..f4059069121 100644 --- a/src/transformers/keras_callbacks.py +++ b/src/transformers/keras_callbacks.py @@ -54,9 +54,9 @@ class KerasMetricCallback(keras.callbacks.Callback): metric names to numerical values. eval_dataset (`tf.data.Dataset` or `dict` or `tuple` or `np.ndarray` or `tf.Tensor`): Validation data to be used to generate predictions for the `metric_fn`. - output_cols (`List[str], *optional*): + output_cols (`list[str], *optional*): A list of columns to be retained from the model output as the predictions. Defaults to all. - label_cols ('`List[str]`, *optional*'): + label_cols ('`list[str]`, *optional*'): A list of columns to be retained from the input dataset as the labels. Will be autodetected if this is not supplied. batch_size (`int`, *optional*): diff --git a/src/transformers/loss/loss_d_fine.py b/src/transformers/loss/loss_d_fine.py index 010c044924c..3fdfd9d65e1 100644 --- a/src/transformers/loss/loss_d_fine.py +++ b/src/transformers/loss/loss_d_fine.py @@ -97,7 +97,7 @@ def translate_gt(gt: torch.Tensor, max_num_bins: int, reg_scale: int, up: torch. up (Tensor): Controls the upper bounds of the Weighting Function. Returns: - Tuple[Tensor, Tensor, Tensor]: + tuple[Tensor, Tensor, Tensor]: - indices (Tensor): Index of the left bin closest to each GT value, shape (N, ). - weight_right (Tensor): Weight assigned to the right bin, shape (N, ). - weight_left (Tensor): Weight assigned to the left bin, shape (N, ). @@ -184,7 +184,7 @@ class DFineLoss(RTDetrLoss): weight_dict (`Dict`): Dictionary relating each loss with its weights. These losses are configured in DFineConf as `weight_loss_vfl`, `weight_loss_bbox`, `weight_loss_giou`, `weight_loss_fgl`, `weight_loss_ddf` - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. See `get_loss` for a list of all available losses. alpha (`float`): Parameter alpha used to compute the focal loss. diff --git a/src/transformers/loss/loss_for_object_detection.py b/src/transformers/loss/loss_for_object_detection.py index 877141f0228..20b16f58bdd 100644 --- a/src/transformers/loss/loss_for_object_detection.py +++ b/src/transformers/loss/loss_for_object_detection.py @@ -11,7 +11,7 @@ # 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. -from typing import List, Optional +from typing import Optional import torch import torch.nn as nn @@ -104,7 +104,7 @@ class ImageLoss(nn.Module): Number of object categories, omitting the special no-object category. eos_coef (`float`): Relative classification weight applied to the no-object category. - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. See `get_loss` for a list of all available losses. """ @@ -243,7 +243,7 @@ class ImageLoss(nn.Module): Args: outputs (`dict`, *optional*): Dictionary of tensors, see the output specification of the model for the format. - targets (`List[dict]`, *optional*): + targets (`list[dict]`, *optional*): List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the losses applied, see each loss' doc. """ @@ -318,7 +318,7 @@ class HungarianMatcher(nn.Module): A dictionary that contains at least these entries: * "logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits * "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates. - targets (`List[dict]`): + targets (`list[dict]`): A list of targets (len(targets) = batch_size), where each target is a dict containing: * "class_labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth @@ -326,7 +326,7 @@ class HungarianMatcher(nn.Module): * "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates. Returns: - `List[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: + `list[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes) @@ -431,7 +431,7 @@ def generalized_box_iou(boxes1, boxes2): # below: taken from https://github.com/facebookresearch/detr/blob/master/util/misc.py#L306 def _max_by_axis(the_list): - # type: (List[List[int]]) -> List[int] + # type: (list[list[int]]) -> list[int] maxes = the_list[0] for sublist in the_list[1:]: for index, item in enumerate(sublist): @@ -460,7 +460,7 @@ class NestedTensor: return str(self.tensors) -def nested_tensor_from_tensor_list(tensor_list: List[Tensor]): +def nested_tensor_from_tensor_list(tensor_list: list[Tensor]): if tensor_list[0].ndim == 3: max_size = _max_by_axis([list(img.shape) for img in tensor_list]) batch_shape = [len(tensor_list)] + max_size diff --git a/src/transformers/loss/loss_grounding_dino.py b/src/transformers/loss/loss_grounding_dino.py index 0294f93232e..17ea065e84a 100644 --- a/src/transformers/loss/loss_grounding_dino.py +++ b/src/transformers/loss/loss_grounding_dino.py @@ -74,7 +74,7 @@ class GroundingDinoHungarianMatcher(HungarianMatcher): * "logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits * "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates. * "label_maps": Tuple of tensors of dim [num_classes, hidden_dim]. - targets (`List[dict]`): + targets (`list[dict]`): A list of targets (len(targets) = batch_size), where each target is a dict containing: * "class_labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth @@ -82,7 +82,7 @@ class GroundingDinoHungarianMatcher(HungarianMatcher): * "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates. Returns: - `List[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: + `list[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes) @@ -136,7 +136,7 @@ class GroundingDinoImageLoss(ImageLoss): Module able to compute a matching between targets and proposals. focal_alpha (`float`): Alpha parameter in focal loss. - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. See `get_loss` for a list of all available losses. """ diff --git a/src/transformers/loss/loss_rt_detr.py b/src/transformers/loss/loss_rt_detr.py index 228a2aa3c76..eb6e2e65a01 100644 --- a/src/transformers/loss/loss_rt_detr.py +++ b/src/transformers/loss/loss_rt_detr.py @@ -136,7 +136,7 @@ class RTDetrLoss(nn.Module): weight_dict (`Dict`): Dictionary relating each loss with its weights. These losses are configured in RTDetrConf as `weight_loss_vfl`, `weight_loss_bbox`, `weight_loss_giou` - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. See `get_loss` for a list of all available losses. alpha (`float`): Parameter alpha used to compute the focal loss. @@ -374,7 +374,7 @@ class RTDetrLoss(nn.Module): Args: outputs (`dict`, *optional*): Dictionary of tensors, see the output specification of the model for the format. - targets (`List[dict]`, *optional*): + targets (`list[dict]`, *optional*): List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the losses applied, see each loss' doc. """ diff --git a/src/transformers/model_debugging_utils.py b/src/transformers/model_debugging_utils.py index 2df9b2ac65f..9f3003150a1 100644 --- a/src/transformers/model_debugging_utils.py +++ b/src/transformers/model_debugging_utils.py @@ -167,7 +167,7 @@ def _repr_to_list(value: torch.Tensor): value (`torch.Tensor`): The tensor to represent. Returns: - `List[str]`: List of string lines representing the tensor. + `list[str]`: List of string lines representing the tensor. """ torch.set_printoptions(sci_mode=True, linewidth=120) with StringIO() as buf, redirect_stdout(buf): diff --git a/src/transformers/modeling_flash_attention_utils.py b/src/transformers/modeling_flash_attention_utils.py index 6ab4cb748aa..03e2922b558 100644 --- a/src/transformers/modeling_flash_attention_utils.py +++ b/src/transformers/modeling_flash_attention_utils.py @@ -144,9 +144,9 @@ def _upad_input( Value state with padding. Shape: (total_source_length, num_key_value_heads, head_dim). indices_q (`torch.Tensor`): The indices of non-masked tokens from the flattened input target sequence. - (cu_seqlens_q, cu_seqlens_k) (`Tuple[int]`): + (cu_seqlens_q, cu_seqlens_k) (`tuple[int]`): The cumulative sequence lengths for the target (query) and source (key, value), used to index into ragged (unpadded) tensors. `cu_seqlens` shape is (batch_size + 1,). - (max_seqlen_in_batch_q, max_seqlen_in_batch_k) (`Tuple[int]`): + (max_seqlen_in_batch_q, max_seqlen_in_batch_k) (`tuple[int]`): Maximum sequence length in batch (`max_seqlen_in_batch_q` for the target sequence i.e. query, `max_seqlen_in_batch_k` for the source sequence i.e. key/value). """ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask) @@ -216,9 +216,9 @@ def prepare_fa2_from_position_ids(query, key, value, position_ids): Value state with padding. Shape: (total_source_length, num_key_value_heads, head_dim). indices_q (`torch.Tensor`): The indices of non-masked tokens from the flattened input target sequence. - (cu_seqlens_q, cu_seqlens_k) (`Tuple[int]`): + (cu_seqlens_q, cu_seqlens_k) (`tuple[int]`): The cumulative sequence lengths for the target (query) and source (key, value), used to index into ragged (unpadded) tensors. `cu_seqlens` shape is (batch_size + 1,). - (max_seqlen_in_batch_q, max_seqlen_in_batch_k) (`Tuple[int]`): + (max_seqlen_in_batch_q, max_seqlen_in_batch_k) (`tuple[int]`): Maximum sequence length in batch (`max_seqlen_in_batch_q` for the target sequence i.e. query, `max_seqlen_in_batch_k` for the source sequence i.e. key/value). """ query = query.view(-1, query.size(-2), query.size(-1)) diff --git a/src/transformers/modeling_flax_outputs.py b/src/transformers/modeling_flax_outputs.py index 325f968571e..5a25a6059a2 100644 --- a/src/transformers/modeling_flax_outputs.py +++ b/src/transformers/modeling_flax_outputs.py @@ -11,7 +11,7 @@ # 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. -from typing import Dict, Optional, Tuple +from typing import Optional import flax import jax.numpy as jnp @@ -41,8 +41,8 @@ class FlaxBaseModelOutput(ModelOutput): """ last_hidden_state: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -60,7 +60,7 @@ class FlaxBaseModelOutputWithNoAttention(ModelOutput): """ last_hidden_state: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -81,7 +81,7 @@ class FlaxBaseModelOutputWithPoolingAndNoAttention(ModelOutput): last_hidden_state: Optional[jnp.ndarray] = None pooler_output: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -100,7 +100,7 @@ class FlaxImageClassifierOutputWithNoAttention(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -111,7 +111,7 @@ class FlaxBaseModelOutputWithPast(ModelOutput): Args: last_hidden_state (`jnp.ndarray` of shape `(batch_size, sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the model. - past_key_values (`Dict[str, jnp.ndarray]`): + past_key_values (`dict[str, jnp.ndarray]`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. hidden_states (`tuple(jnp.ndarray)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): @@ -128,9 +128,9 @@ class FlaxBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[jnp.ndarray] = None - past_key_values: Optional[Dict[str, jnp.ndarray]] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[dict[str, jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -160,8 +160,8 @@ class FlaxBaseModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[jnp.ndarray] = None pooler_output: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -207,10 +207,10 @@ class FlaxBaseModelOutputWithPoolingAndCrossAttentions(ModelOutput): last_hidden_state: Optional[jnp.ndarray] = None pooler_output: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -253,10 +253,10 @@ class FlaxBaseModelOutputWithPastAndCrossAttentions(ModelOutput): """ last_hidden_state: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -311,13 +311,13 @@ class FlaxSeq2SeqModelOutput(ModelOutput): """ last_hidden_state: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - decoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - decoder_attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + decoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + decoder_attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None encoder_last_hidden_state: Optional[jnp.ndarray] = None - encoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - encoder_attentions: Optional[Tuple[jnp.ndarray]] = None + encoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + encoder_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -355,10 +355,10 @@ class FlaxCausalLMOutputWithCrossAttentions(ModelOutput): """ logits: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -383,8 +383,8 @@ class FlaxMaskedLMOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None FlaxCausalLMOutput = FlaxMaskedLMOutput @@ -438,13 +438,13 @@ class FlaxSeq2SeqLMOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - decoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - decoder_attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + decoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + decoder_attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None encoder_last_hidden_state: Optional[jnp.ndarray] = None - encoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - encoder_attentions: Optional[Tuple[jnp.ndarray]] = None + encoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + encoder_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -470,8 +470,8 @@ class FlaxNextSentencePredictorOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -496,8 +496,8 @@ class FlaxSequenceClassifierOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -548,13 +548,13 @@ class FlaxSeq2SeqSequenceClassifierOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - decoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - decoder_attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + decoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + decoder_attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None encoder_last_hidden_state: Optional[jnp.ndarray] = None - encoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - encoder_attentions: Optional[Tuple[jnp.ndarray]] = None + encoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + encoder_attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -581,8 +581,8 @@ class FlaxMultipleChoiceModelOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -607,8 +607,8 @@ class FlaxTokenClassifierOutput(ModelOutput): """ logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -636,8 +636,8 @@ class FlaxQuestionAnsweringModelOutput(ModelOutput): start_logits: Optional[jnp.ndarray] = None end_logits: Optional[jnp.ndarray] = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -691,10 +691,10 @@ class FlaxSeq2SeqQuestionAnsweringModelOutput(ModelOutput): start_logits: Optional[jnp.ndarray] = None end_logits: Optional[jnp.ndarray] = None - past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None - decoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - decoder_attentions: Optional[Tuple[jnp.ndarray]] = None - cross_attentions: Optional[Tuple[jnp.ndarray]] = None + past_key_values: Optional[tuple[tuple[jnp.ndarray]]] = None + decoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + decoder_attentions: Optional[tuple[jnp.ndarray]] = None + cross_attentions: Optional[tuple[jnp.ndarray]] = None encoder_last_hidden_state: Optional[jnp.ndarray] = None - encoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None - encoder_attentions: Optional[Tuple[jnp.ndarray]] = None + encoder_hidden_states: Optional[tuple[jnp.ndarray]] = None + encoder_attentions: Optional[tuple[jnp.ndarray]] = None diff --git a/src/transformers/modeling_flax_pytorch_utils.py b/src/transformers/modeling_flax_pytorch_utils.py index b35e4924607..7d9f1897999 100644 --- a/src/transformers/modeling_flax_pytorch_utils.py +++ b/src/transformers/modeling_flax_pytorch_utils.py @@ -16,7 +16,6 @@ import os from pickle import UnpicklingError -from typing import Dict, Tuple import jax import jax.numpy as jnp @@ -83,14 +82,14 @@ def load_pytorch_checkpoint_in_flax_state_dict( def rename_key_and_reshape_tensor( - pt_tuple_key: Tuple[str], + pt_tuple_key: tuple[str], pt_tensor: np.ndarray, - random_flax_state_dict: Dict[str, jnp.ndarray], + random_flax_state_dict: dict[str, jnp.ndarray], model_prefix: str, -) -> (Tuple[str], np.ndarray): +) -> (tuple[str], np.ndarray): """Rename PT weight names to corresponding Flax weight names and reshape tensor if necessary""" - def is_key_or_prefix_key_in_dict(key: Tuple[str]) -> bool: + def is_key_or_prefix_key_in_dict(key: tuple[str]) -> bool: """Checks if `key` of `(prefix,) + key` is in random_flax_state_dict""" return len(set(random_flax_state_dict) & {key, (model_prefix,) + key}) > 0 diff --git a/src/transformers/modeling_flax_utils.py b/src/transformers/modeling_flax_utils.py index 4b260b14f6e..dfc0631abe0 100644 --- a/src/transformers/modeling_flax_utils.py +++ b/src/transformers/modeling_flax_utils.py @@ -20,7 +20,7 @@ import os import warnings from functools import partial from pickle import UnpicklingError -from typing import Any, Dict, Optional, Set, Tuple, Union +from typing import Any, Optional, Union import flax.linen as nn import jax @@ -174,7 +174,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): self, config: PretrainedConfig, module: nn.Module, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -225,7 +225,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): if _do_init: self.params = random_params - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> Dict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> dict: raise NotImplementedError(f"init method has to be implemented for {self}") def enable_gradient_checkpointing(self): @@ -254,7 +254,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): return self._module @property - def params(self) -> Union[Dict, FrozenDict]: + def params(self) -> Union[dict, FrozenDict]: if not self._is_initialized: raise ValueError( "`params` cannot be accessed from model when the model is created with `_do_init=False`. " @@ -264,15 +264,15 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): return self._params @property - def required_params(self) -> Set: + def required_params(self) -> set: return self._required_params @property - def params_shape_tree(self) -> Dict: + def params_shape_tree(self) -> dict: return self._params_shape_tree @params.setter - def params(self, params: Union[Dict, FrozenDict]): + def params(self, params: Union[dict, FrozenDict]): # don't set params if the model is not initialized if not self._is_initialized: raise ValueError( @@ -290,7 +290,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): ) self._params = params - def _cast_floating_to(self, params: Union[Dict, FrozenDict], dtype: jnp.dtype, mask: Any = None) -> Any: + def _cast_floating_to(self, params: Union[dict, FrozenDict], dtype: jnp.dtype, mask: Any = None) -> Any: """ Helper method to cast floating-point values of given parameter `PyTree` to given `dtype`. """ @@ -313,7 +313,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): return unflatten_dict(flat_params) - def to_bf16(self, params: Union[Dict, FrozenDict], mask: Any = None): + def to_bf16(self, params: Union[dict, FrozenDict], mask: Any = None): r""" Cast the floating-point `params` to `jax.numpy.bfloat16`. This returns a new `params` tree and does not cast the `params` in place. @@ -352,7 +352,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): ```""" return self._cast_floating_to(params, jnp.bfloat16, mask) - def to_fp32(self, params: Union[Dict, FrozenDict], mask: Any = None): + def to_fp32(self, params: Union[dict, FrozenDict], mask: Any = None): r""" Cast the floating-point `params` to `jax.numpy.float32`. This method can be used to explicitly convert the model parameters to fp32 precision. This returns a new `params` tree and does not cast the `params` in place. @@ -379,7 +379,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): ```""" return self._cast_floating_to(params, jnp.float32, mask) - def to_fp16(self, params: Union[Dict, FrozenDict], mask: Any = None): + def to_fp16(self, params: Union[dict, FrozenDict], mask: Any = None): r""" Cast the floating-point `params` to `jax.numpy.float16`. This returns a new `params` tree and does not cast the `params` in place. @@ -453,7 +453,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): loaded in the model. Args: - shard_files (`List[str]`: + shard_files (`list[str]`: The list of shard files to load. Returns: @@ -581,7 +581,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. local_files_only(`bool`, *optional*, defaults to `False`): @@ -1113,7 +1113,7 @@ class FlaxPreTrainedModel(PushToHubMixin, FlaxGenerationMixin): token (`str` or `bool`, *optional*): The token to use as HTTP bearer authorization for remote files. If `True`, or not specified, will use the token generated when running `huggingface-cli login` (stored in `~/.huggingface`). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. safe_serialization (`bool`, *optional*, defaults to `False`): Whether to save the model using `safetensors` or through msgpack. diff --git a/src/transformers/modeling_outputs.py b/src/transformers/modeling_outputs.py index 972db718f61..597e20b28ca 100755 --- a/src/transformers/modeling_outputs.py +++ b/src/transformers/modeling_outputs.py @@ -14,7 +14,7 @@ import warnings from dataclasses import dataclass -from typing import Optional, Tuple +from typing import Optional import torch @@ -44,8 +44,8 @@ class BaseModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -64,7 +64,7 @@ class BaseModelOutputWithNoAttention(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -95,8 +95,8 @@ class BaseModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -118,7 +118,7 @@ class BaseModelOutputWithPoolingAndNoAttention(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -153,8 +153,8 @@ class BaseModelOutputWithPast(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -185,9 +185,9 @@ class BaseModelOutputWithCrossAttentions(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -230,10 +230,10 @@ class BaseModelOutputWithPoolingAndCrossAttentions(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None past_key_values: Optional[Cache] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -274,9 +274,9 @@ class BaseModelOutputWithPastAndCrossAttentions(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -320,11 +320,11 @@ class MoECausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None z_loss: Optional[torch.FloatTensor] = None aux_loss: Optional[torch.FloatTensor] = None - router_logits: Optional[Tuple[torch.FloatTensor]] = None + router_logits: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -354,9 +354,9 @@ class MoEModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - router_probs: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + router_probs: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -393,9 +393,9 @@ class MoeModelOutputWithPast(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - router_logits: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + router_logits: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -441,9 +441,9 @@ class MoeCausalLMOutputWithPast(ModelOutput): aux_loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - router_logits: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + router_logits: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -490,10 +490,10 @@ class MoEModelOutputWithPastAndCrossAttentions(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - router_probs: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + router_probs: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -547,12 +547,12 @@ class Seq2SeqModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -615,14 +615,14 @@ class Seq2SeqMoEModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_router_logits: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_router_logits: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_router_logits: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_router_logits: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -650,8 +650,8 @@ class CausalLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -685,8 +685,8 @@ class CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -726,9 +726,9 @@ class CausalLMOutputWithCrossAttentions(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -762,8 +762,8 @@ class SequenceClassifierOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -791,8 +791,8 @@ class MaskedLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -845,12 +845,12 @@ class Seq2SeqLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -916,14 +916,14 @@ class Seq2SeqMoEOutput(ModelOutput): encoder_aux_loss: Optional[torch.FloatTensor] = None decoder_aux_loss: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_router_logits: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_router_logits: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_router_logits: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_router_logits: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -952,8 +952,8 @@ class NextSentencePredictorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -981,8 +981,8 @@ class SequenceClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1035,12 +1035,12 @@ class Seq2SeqSequenceClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1070,8 +1070,8 @@ class MultipleChoiceModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1099,8 +1099,8 @@ class TokenClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1131,8 +1131,8 @@ class QuestionAnsweringModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1188,12 +1188,12 @@ class Seq2SeqQuestionAnsweringModelOutput(ModelOutput): start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1230,8 +1230,8 @@ class SemanticSegmenterOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1258,8 +1258,8 @@ class ImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1280,7 +1280,7 @@ class ImageClassifierOutputWithNoAttention(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1309,8 +1309,8 @@ class DepthEstimatorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None predicted_depth: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1337,8 +1337,8 @@ class ImageSuperResolutionOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None reconstruction: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1366,8 +1366,8 @@ class Wav2Vec2BaseModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None extract_features: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1398,8 +1398,8 @@ class XVectorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None embeddings: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1424,9 +1424,9 @@ class BackboneOutput(ModelOutput): heads. """ - feature_maps: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + feature_maps: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1461,9 +1461,9 @@ class BaseModelOutputWithPoolingAndProjection(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - projection_state: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + projection_state: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -1516,12 +1516,12 @@ class Seq2SeqSpectrogramOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None spectrogram: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1583,12 +1583,12 @@ class Seq2SeqTSModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None static_features: Optional[torch.FloatTensor] = None @@ -1651,14 +1651,14 @@ class Seq2SeqTSPredictionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - params: Optional[Tuple[torch.FloatTensor]] = None + params: Optional[tuple[torch.FloatTensor]] = None past_key_values: Optional[EncoderDecoderCache] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None static_features: Optional[torch.FloatTensor] = None @@ -1702,8 +1702,8 @@ class MaskedImageModelingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None reconstruction: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @property def logits(self): diff --git a/src/transformers/modeling_tf_outputs.py b/src/transformers/modeling_tf_outputs.py index cbc8b3682af..822c7b486a2 100644 --- a/src/transformers/modeling_tf_outputs.py +++ b/src/transformers/modeling_tf_outputs.py @@ -16,7 +16,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple +from typing import Optional import tensorflow as tf @@ -45,8 +45,8 @@ class TFBaseModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -65,7 +65,7 @@ class TFBaseModelOutputWithNoAttention(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Optional[Tuple[tf.Tensor, ...]] = None + hidden_states: Optional[tuple[tf.Tensor, ...]] = None @dataclass @@ -98,8 +98,8 @@ class TFBaseModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None pooler_output: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -121,7 +121,7 @@ class TFBaseModelOutputWithPoolingAndNoAttention(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None pooler_output: Optional[tf.Tensor] = None - hidden_states: Optional[Tuple[tf.Tensor, ...]] = None + hidden_states: Optional[tuple[tf.Tensor, ...]] = None @dataclass @@ -139,7 +139,7 @@ class TFBaseModelOutputWithPoolingAndCrossAttentions(ModelOutput): This output is usually *not* a good summary of the semantic content of the input, you're often better with averaging or pooling the sequence of hidden-states for the whole input sequence. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -166,10 +166,10 @@ class TFBaseModelOutputWithPoolingAndCrossAttentions(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None pooler_output: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -183,7 +183,7 @@ class TFBaseModelOutputWithPast(ModelOutput): If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1, hidden_size)` is output. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -203,9 +203,9 @@ class TFBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -236,9 +236,9 @@ class TFBaseModelOutputWithCrossAttentions(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -252,7 +252,7 @@ class TFBaseModelOutputWithPastAndCrossAttentions(ModelOutput): If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1, hidden_size)` is output. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -278,10 +278,10 @@ class TFBaseModelOutputWithPastAndCrossAttentions(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -296,7 +296,7 @@ class TFSeq2SeqModelOutput(ModelOutput): If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1, hidden_size)` is output. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -335,13 +335,13 @@ class TFSeq2SeqModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor] | None = None - decoder_attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor] | None = None + decoder_attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor] | None = None - encoder_attentions: Tuple[tf.Tensor] | None = None + encoder_hidden_states: tuple[tf.Tensor] | None = None + encoder_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -369,8 +369,8 @@ class TFCausalLMOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -383,7 +383,7 @@ class TFCausalLMOutputWithPast(ModelOutput): Language modeling loss (for next-token prediction). logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -404,9 +404,9 @@ class TFCausalLMOutputWithPast(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -436,7 +436,7 @@ class TFCausalLMOutputWithCrossAttentions(ModelOutput): Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -446,10 +446,10 @@ class TFCausalLMOutputWithCrossAttentions(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -477,8 +477,8 @@ class TFMaskedLMOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -491,7 +491,7 @@ class TFSeq2SeqLMOutput(ModelOutput): Language modeling loss. logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -531,13 +531,13 @@ class TFSeq2SeqLMOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor] | None = None - decoder_attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor] | None = None + decoder_attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor] | None = None - encoder_attentions: Tuple[tf.Tensor] | None = None + encoder_hidden_states: tuple[tf.Tensor] | None = None + encoder_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -566,8 +566,8 @@ class TFNextSentencePredictorOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -595,8 +595,8 @@ class TFSequenceClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -609,7 +609,7 @@ class TFSeq2SeqSequenceClassifierOutput(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -646,13 +646,13 @@ class TFSeq2SeqSequenceClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor] | None = None - decoder_attentions: Tuple[tf.Tensor] | None = None - cross_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor] | None = None + decoder_attentions: tuple[tf.Tensor] | None = None + cross_attentions: tuple[tf.Tensor] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor] | None = None - encoder_attentions: Tuple[tf.Tensor] | None = None + encoder_hidden_states: tuple[tf.Tensor] | None = None + encoder_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -688,8 +688,8 @@ class TFSemanticSegmenterOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -720,7 +720,7 @@ class TFSemanticSegmenterOutputWithNoAttention(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None @dataclass @@ -746,8 +746,8 @@ class TFImageClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -777,8 +777,8 @@ class TFMultipleChoiceModelOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -806,8 +806,8 @@ class TFTokenClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -838,8 +838,8 @@ class TFQuestionAnsweringModelOutput(ModelOutput): loss: tf.Tensor | None = None start_logits: Optional[tf.Tensor] = None end_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -854,7 +854,7 @@ class TFSeq2SeqQuestionAnsweringModelOutput(ModelOutput): Span-start scores (before SoftMax). end_logits (`tf.Tensor` of shape `(batch_size, sequence_length)`): Span-end scores (before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -889,12 +889,12 @@ class TFSeq2SeqQuestionAnsweringModelOutput(ModelOutput): loss: tf.Tensor | None = None start_logits: Optional[tf.Tensor] = None end_logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor] | None = None - decoder_attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor] | None = None + decoder_attentions: tuple[tf.Tensor] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor] | None = None - encoder_attentions: Tuple[tf.Tensor] | None = None + encoder_hidden_states: tuple[tf.Tensor] | None = None + encoder_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -907,7 +907,7 @@ class TFSequenceClassifierOutputWithPast(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -928,9 +928,9 @@ class TFSequenceClassifierOutputWithPast(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -951,7 +951,7 @@ class TFImageClassifierOutputWithNoAttention(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Optional[Tuple[tf.Tensor, ...]] = None + hidden_states: Optional[tuple[tf.Tensor, ...]] = None @dataclass @@ -978,8 +978,8 @@ class TFMaskedImageModelingOutput(ModelOutput): loss: tf.Tensor | None = None reconstruction: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @property def logits(self): diff --git a/src/transformers/modeling_tf_utils.py b/src/transformers/modeling_tf_utils.py index 0ff83744ae6..4c758daed87 100644 --- a/src/transformers/modeling_tf_utils.py +++ b/src/transformers/modeling_tf_utils.py @@ -27,7 +27,7 @@ import re import warnings from collections.abc import Mapping from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import h5py import numpy as np @@ -106,10 +106,10 @@ except (ModuleNotFoundError, ImportError): tf_logger = tf.get_logger() TFModelInputType = Union[ - List[tf.Tensor], - List[np.ndarray], - Dict[str, tf.Tensor], - Dict[str, np.ndarray], + list[tf.Tensor], + list[np.ndarray], + dict[str, tf.Tensor], + dict[str, np.ndarray], tf.Tensor, np.ndarray, ] @@ -645,7 +645,7 @@ def tf_shard_checkpoint(weights, max_shard_size="10GB", weights_name: str = TF2_ Args: - weights (`Dict[str, tf.RessourceVariable]`): The list of tf.RessourceVariable of a model to save. + weights (`dict[str, tf.RessourceVariable]`): The list of tf.RessourceVariable of a model to save. max_shard_size (`int` or `str`, *optional*, defaults to `"10GB"`): The maximum size of each sub-checkpoint. If expressed as a string, needs to be digits followed by a unit (like `"5MB"`). @@ -1142,12 +1142,12 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT _requires_load_weight_prefix = False @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: + def dummy_inputs(self) -> dict[str, tf.Tensor]: """ Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. + `dict[str, tf.Tensor]`: The dummy inputs. """ dummies = {} for key, spec in self.input_signature.items(): @@ -1288,7 +1288,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT Args: Method used for serving the model. Does not have a specific signature, but will be specialized as concrete functions when saving with `save_pretrained`. - inputs (`Dict[str, tf.Tensor]`): + inputs (`dict[str, tf.Tensor]`): The input of the saved model as a dictionary of tensors. """ output = self.call(inputs) @@ -1296,7 +1296,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT return self.serving_output(output) @property - def input_signature(self) -> Dict[str, tf.TensorSpec]: + def input_signature(self) -> dict[str, tf.TensorSpec]: """ This property should return a dict mapping input names to tf.TensorSpec objects, representing the expected shape and dtype for model inputs. It is used for both serving and for generating dummy inputs. @@ -1414,7 +1414,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT shuffle: bool = True, tokenizer: Optional["PreTrainedTokenizerBase"] = None, collate_fn: Optional[Callable] = None, - collate_fn_args: Optional[Dict[str, Any]] = None, + collate_fn_args: Optional[dict[str, Any]] = None, drop_remainder: Optional[bool] = None, prefetch: bool = True, ): @@ -1440,7 +1440,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT A function that collates samples from the dataset into a single batch. Defaults to `DefaultDataCollator` if no `tokenizer` is supplied or `DataCollatorWithPadding` if a `tokenizer` is passed. - collate_fn_args (`Dict[str, Any]`, *optional*): + collate_fn_args (`dict[str, Any]`, *optional*): A dict of arguments to pass to the `collate_fn` alongside the list of samples. drop_remainder (`bool`, *optional*): Whether to drop the final batch, if the batch_size does not evenly divide the dataset length. Defaults @@ -1816,9 +1816,9 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT tags: Optional[str] = None, finetuned_from: Optional[str] = None, tasks: Optional[str] = None, - dataset_tags: Optional[Union[str, List[str]]] = None, - dataset: Optional[Union[str, List[str]]] = None, - dataset_args: Optional[Union[str, List[str]]] = None, + dataset_tags: Optional[Union[str, list[str]]] = None, + dataset: Optional[Union[str, list[str]]] = None, + dataset_args: Optional[Union[str, list[str]]] = None, ): """ Creates a draft of a model card using the information available to the `Trainer`. @@ -1833,18 +1833,18 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT license (`str`, *optional*): The license of the model. Will default to the license of the pretrained model used, if the original model given to the `Trainer` comes from a repo on the Hub. - tags (`str` or `List[str]`, *optional*): + tags (`str` or `list[str]`, *optional*): Some tags to be included in the metadata of the model card. finetuned_from (`str`, *optional*): The name of the model used to fine-tune this one (if applicable). Will default to the name of the repo of the original model given to the `Trainer` (if it comes from the Hub). - tasks (`str` or `List[str]`, *optional*): + tasks (`str` or `list[str]`, *optional*): One or several task identifiers, to be included in the metadata of the model card. - dataset_tags (`str` or `List[str]`, *optional*): + dataset_tags (`str` or `list[str]`, *optional*): One or several dataset tags, to be included in the metadata of the model card. - dataset (`str` or `List[str]`, *optional*): + dataset (`str` or `list[str]`, *optional*): One or several dataset identifiers, to be included in the metadata of the model card. - dataset_args (`str` or `List[str]`, *optional*): + dataset_args (`str` or `list[str]`, *optional*): One or several dataset arguments, to be included in the metadata of the model card. """ # Avoids a circular import by doing this when necessary. @@ -1947,7 +1947,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT warnings.warn("The method get_prefix_bias_name is deprecated. Please use `get_bias` instead.", FutureWarning) return None - def get_bias(self) -> Union[None, Dict[str, tf.Variable]]: + def get_bias(self) -> Union[None, dict[str, tf.Variable]]: """ Dict of bias attached to an LM head. The key represents the name of the bias attribute. @@ -1969,7 +1969,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT Set all the bias in the LM head. Args: - value (`Dict[tf.Variable]`): + value (`dict[tf.Variable]`): All the new bias attached to an LM head. """ if self.get_lm_head() is not None: @@ -2174,14 +2174,14 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT return new_lm_head_bias def _v2_get_resized_lm_head_bias( - self, old_lm_head_bias: Dict[str, tf.Variable], new_num_tokens: int - ) -> Dict[str, tf.Tensor]: + self, old_lm_head_bias: dict[str, tf.Variable], new_num_tokens: int + ) -> dict[str, tf.Tensor]: """ Build a resized bias from the old ones. Increasing the size will add newly initialized vectors at the end. Reducing the size will remove vectors from the end Args: - old_lm_head_bias (`Dict[str, tf.Variable]`): + old_lm_head_bias (`dict[str, tf.Variable]`): Old lm head bias to be resized. new_num_tokens (`int`): New number of tokens in the linear matrix. Increasing the size will add newly initialized vectors at @@ -2332,7 +2332,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT Prunes heads of the base model. Arguments: - heads_to_prune (`Dict[int, List[int]]`): + heads_to_prune (`dict[int, list[int]]`): Dictionary with keys being selected layer indices (`int`) and associated values being the list of heads to prune in said layer (list of `int`). For instance {1: [0, 2], 2: [2, 3]} will prune heads 0 and 2 on layer 1 and heads 2 and 3 on layer 2. @@ -2389,7 +2389,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT token (`str` or `bool`, *optional*): The token to use as HTTP bearer authorization for remote files. If `True`, or not specified, will use the token generated when running `huggingface-cli login` (stored in `~/.huggingface`). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -2594,7 +2594,7 @@ class TFPreTrainedModel(keras.Model, TFModelUtilsMixin, TFGenerationMixin, PushT Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. proxies: - (`Dict[str, str], `optional`): A dictionary of proxy servers to use by protocol or endpoint, e.g., + (`dict[str, str], `optional`): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. output_loading_info(`bool`, *optional*, defaults to `False`): Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages. @@ -3263,7 +3263,7 @@ class TFConv1D(keras.layers.Layer): The number of input features. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation to use to initialize the weights. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the `__init__` of `keras.layers.Layer`. """ @@ -3308,7 +3308,7 @@ class TFSharedEmbeddings(keras.layers.Layer): initializer_range (`float`, *optional*): The standard deviation to use when initializing the weights. If no value is provided, it will default to \\(1/\sqrt{hidden\_size}\\). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the `__init__` of `keras.layers.Layer`. """ @@ -3423,7 +3423,7 @@ class TFSequenceSummary(keras.layers.Layer): - **summary_last_dropout** (`float`)-- Optional dropout probability after the projection and activation. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation to use to initialize the weights. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the `__init__` of `keras.layers.Layer`. """ diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index a0f5180ac8b..ae6f194a90b 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -33,7 +33,7 @@ from dataclasses import dataclass from enum import Enum from functools import partial, wraps from threading import Thread -from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, TypeVar, Union +from typing import Any, Callable, Optional, TypeVar, Union from zipfile import is_zipfile import torch @@ -339,7 +339,7 @@ def get_parameter_device(parameter: Union[nn.Module, "ModuleUtilsMixin"]): except StopIteration: # For nn.DataParallel compatibility in PyTorch 1.5 - def find_tensor_attributes(module: nn.Module) -> List[Tuple[str, Tensor]]: + def find_tensor_attributes(module: nn.Module) -> list[tuple[str, Tensor]]: tuples = [(k, v) for k, v in module.__dict__.items() if torch.is_tensor(v)] return tuples @@ -374,7 +374,7 @@ def get_parameter_dtype(parameter: Union[nn.Module, "ModuleUtilsMixin"]): return last_dtype # For nn.DataParallel compatibility in PyTorch > 1.5 - def find_tensor_attributes(module: nn.Module) -> List[Tuple[str, Tensor]]: + def find_tensor_attributes(module: nn.Module) -> list[tuple[str, Tensor]]: tuples = [(k, v) for k, v in module.__dict__.items() if torch.is_tensor(v)] return tuples @@ -644,7 +644,7 @@ def _get_tied_weight_keys(module: nn.Module, prefix=""): return tied_weight_keys -def _find_disjoint(tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]) -> Tuple[List[Set[str]], List[str]]: +def _find_disjoint(tensors: list[set[str]], state_dict: dict[str, torch.Tensor]) -> tuple[list[set[str]], list[str]]: filtered_tensors = [] for shared in tensors: if len(shared) < 2: @@ -675,7 +675,7 @@ def _find_disjoint(tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]) return shared_tensors, disjoint_tensors -def _find_identical(tensors: List[Set[str]], state_dict: Dict[str, torch.Tensor]) -> Tuple[List[Set[str]], Set[str]]: +def _find_identical(tensors: list[set[str]], state_dict: dict[str, torch.Tensor]) -> tuple[list[set[str]], set[str]]: shared_tensors = [] identical = [] for shared in tensors: @@ -738,21 +738,21 @@ def _load_parameter_into_model(model: "PreTrainedModel", param_name: str, tensor @torch.no_grad() def _load_state_dict_into_meta_model( model: "PreTrainedModel", - state_dict: Dict, + state_dict: dict, shard_file: str, - expected_keys: List[str], - reverse_renaming_mapping: Dict[str, str], - device_map: Optional[Dict] = None, + expected_keys: list[str], + reverse_renaming_mapping: dict[str, str], + device_map: Optional[dict] = None, disk_offload_folder: Optional[str] = None, - disk_offload_index: Optional[Dict] = None, + disk_offload_index: Optional[dict] = None, cpu_offload_folder: Optional[str] = None, - cpu_offload_index: Optional[Dict] = None, + cpu_offload_index: Optional[dict] = None, hf_quantizer: Optional[HfQuantizer] = None, is_safetensors: bool = False, keep_in_fp32_regex: Optional[re.Pattern] = None, - unexpected_keys: Optional[List[str]] = None, # passing `unexpected` for cleanup from quantization items + unexpected_keys: Optional[list[str]] = None, # passing `unexpected` for cleanup from quantization items device_mesh: Optional["torch.distributed.device_mesh.DeviceMesh"] = None, -) -> Tuple[Optional[Dict], Optional[Dict]]: +) -> tuple[Optional[dict], Optional[dict]]: """Load parameters from `meta_state_dict` into the model. The parameters of the `meta_state_dict` are on the meta device in order to easily infer the shapes and dtypes that they will have. Then proper parameters are then loaded from `shard_file`, which is the actual state dict file on disk. @@ -998,7 +998,7 @@ def _get_resolved_checkpoint_files( use_safetensors: bool, cache_dir: str, force_download: bool, - proxies: Optional[Dict[str, str]], + proxies: Optional[dict[str, str]], local_files_only: bool, token: Optional[Union[str, bool]], user_agent: dict, @@ -1006,7 +1006,7 @@ def _get_resolved_checkpoint_files( commit_hash: Optional[str], is_remote_code: bool, # Because we can't determine this inside this function, we need it to be passed in transformers_explicit_filename: Optional[str] = None, -) -> Tuple[Optional[List[str]], Optional[Dict]]: +) -> tuple[Optional[list[str]], Optional[dict]]: """Get all the checkpoint filenames based on `pretrained_model_name_or_path`, and optional metadata if the checkpoints are sharded. This function will download the data if necessary. @@ -1315,13 +1315,13 @@ def _get_resolved_checkpoint_files( def _get_torch_dtype( cls, - torch_dtype: Optional[Union[str, torch.dtype, Dict]], - checkpoint_files: Optional[List[str]], + torch_dtype: Optional[Union[str, torch.dtype, dict]], + checkpoint_files: Optional[list[str]], config: PretrainedConfig, - sharded_metadata: Optional[Dict], - state_dict: Optional[Dict], + sharded_metadata: Optional[dict], + state_dict: Optional[dict], weights_only: bool, -) -> Tuple[PretrainedConfig, Optional[torch.dtype], Optional[torch.dtype]]: +) -> tuple[PretrainedConfig, Optional[torch.dtype], Optional[torch.dtype]]: """Find the correct `torch_dtype` to use based on provided arguments. Also update the `config` based on the inferred dtype. We do the following: 1. If torch_dtype is not None, we use that dtype @@ -1395,12 +1395,12 @@ def _get_torch_dtype( def _get_device_map( model: "PreTrainedModel", - device_map: Optional[Union[str, Dict]], - max_memory: Optional[Dict], + device_map: Optional[Union[str, dict]], + max_memory: Optional[dict], hf_quantizer: Optional[HfQuantizer], torch_dtype: Optional[torch.dtype], keep_in_fp32_regex: Optional[re.Pattern], -) -> Dict: +) -> dict: """Compute the final `device_map` to use if we passed a value in ['auto', 'balanced', 'balanced_low_0', 'sequential']. Otherwise, we check for any device inconsistencies in the device_map. """ @@ -1472,12 +1472,12 @@ def _get_device_map( def _find_missing_and_unexpected_keys( cls, model: "PreTrainedModel", - original_checkpoint_keys: List[str], - checkpoint_keys: List[str], + original_checkpoint_keys: list[str], + checkpoint_keys: list[str], loading_base_model_from_task_state_dict: bool, hf_quantizer: Optional[HfQuantizer], - device_map: Dict, -) -> Tuple[List[str], List[str]]: + device_map: dict, +) -> tuple[list[str], list[str]]: """Find missing keys (keys that are part of the model parameters but were NOT found in the loaded state dict keys) and unexpected keys (keys found in the loaded state dict keys, but that are NOT part of the model parameters) """ @@ -1531,13 +1531,13 @@ def _find_missing_and_unexpected_keys( def _find_mismatched_keys( model: "PreTrainedModel", - state_dict: Optional[Dict], - checkpoint_files: Optional[List[str]], + state_dict: Optional[dict], + checkpoint_files: Optional[list[str]], ignore_mismatched_sizes: bool, - keys_to_rename_mapping: Dict[str, str], + keys_to_rename_mapping: dict[str, str], is_quantized: bool, weights_only: bool, -) -> Tuple[List[str], List[Tuple[int, int]]]: +) -> tuple[list[str], list[tuple[int, int]]]: """ Find potential shape mismatch between the different state dicts and the model parameters, but only if `ignore_mismatched_sizes` is True. Otherwise, return immediately and any shape mismatch that may exist will be raised later on. This avoids checking @@ -1710,7 +1710,7 @@ class ModuleUtilsMixin: return extended_attention_mask def get_extended_attention_mask( - self, attention_mask: Tensor, input_shape: Tuple[int], device: torch.device = None, dtype: torch.float = None + self, attention_mask: Tensor, input_shape: tuple[int], device: torch.device = None, dtype: torch.float = None ) -> Tensor: """ Makes broadcastable attention and causal masks so that future and masked tokens are ignored. @@ -1718,7 +1718,7 @@ class ModuleUtilsMixin: Arguments: attention_mask (`torch.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. Returns: @@ -1853,7 +1853,7 @@ class ModuleUtilsMixin: return sum(total_numel) - def estimate_tokens(self, input_dict: Dict[str, Union[torch.Tensor, Any]]) -> int: + def estimate_tokens(self, input_dict: dict[str, Union[torch.Tensor, Any]]) -> int: """ Helper function to estimate the total number of tokens from the model inputs. @@ -1875,7 +1875,7 @@ class ModuleUtilsMixin: return 0 def floating_point_ops( - self, input_dict: Dict[str, Union[torch.Tensor, Any]], exclude_embeddings: bool = True + self, input_dict: dict[str, Union[torch.Tensor, Any]], exclude_embeddings: bool = True ) -> int: """ Get number of (optionally, non-embeddings) floating-point operations for the forward and backward passes of a @@ -2003,9 +2003,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi _supports_attention_backend = False @property - def dummy_inputs(self) -> Dict[str, torch.Tensor]: + def dummy_inputs(self) -> dict[str, torch.Tensor]: """ - `Dict[str, torch.Tensor]`: Dummy inputs to do a forward pass in the network. + `dict[str, torch.Tensor]`: Dummy inputs to do a forward pass in the network. """ return {"input_ids": torch.tensor(DUMMY_INPUTS)} @@ -2108,13 +2108,13 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi # Remove the attribute now that is has been consumed, so it's no saved in the config. delattr(self.config, "gradient_checkpointing") - def add_model_tags(self, tags: Union[List[str], str]) -> None: + def add_model_tags(self, tags: Union[list[str], str]) -> None: r""" Add custom tags into the model that gets pushed to the Hugging Face Hub. Will not overwrite existing tags in the model. Args: - tags (`Union[List[str], str]`): + tags (`Union[list[str], str]`): The desired tags to inject in the model Examples: @@ -2203,7 +2203,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, check_device_map: bool = True, ): """ @@ -2400,7 +2400,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, check_device_map: bool = True, hard_check_only: bool = False, ) -> PretrainedConfig: @@ -2693,8 +2693,8 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi def _tie_encoder_decoder_weights( encoder: nn.Module, decoder: nn.Module, base_model_prefix: str, base_encoder_name: str ): - uninitialized_encoder_weights: List[str] = [] - tied_weights: List[str] = [] + uninitialized_encoder_weights: list[str] = [] + tied_weights: list[str] = [] if decoder.__class__ != encoder.__class__: logger.info( f"{decoder.__class__} and {encoder.__class__} are not equal. In this case make sure that all encoder" @@ -2706,7 +2706,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi encoder_pointer: nn.Module, module_name: str, base_encoder_name: str, - uninitialized_encoder_weights: List[str], + uninitialized_encoder_weights: list[str], depth=0, total_decoder_name="", total_encoder_name="", @@ -2809,7 +2809,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi The device map value. Options are ["auto", "balanced", "balanced_low_0", "sequential"] Returns: - `List[str]`: List of modules that should not be split + `list[str]`: List of modules that should not be split """ _no_split_modules = set() modules_to_check = [self] @@ -3289,7 +3289,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi f"overwrite this method in the class {self.__class__} in `modeling_{self.__class__.__module__}.py`" ) - def get_position_embeddings(self) -> Union[nn.Embedding, Tuple[nn.Embedding]]: + def get_position_embeddings(self) -> Union[nn.Embedding, tuple[nn.Embedding]]: raise NotImplementedError( f"`get_position_embeddings` is not implemented for {self.__class__}`. To implement it, you should " f"overwrite this method in the class {self.__class__} in `modeling_{self.__class__.__module__}.py`" @@ -3312,12 +3312,12 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi # since from_pretrained(...) calls tie weights anyways self.tie_weights() - def prune_heads(self, heads_to_prune: Dict[int, List[int]]): + def prune_heads(self, heads_to_prune: dict[int, list[int]]): """ Prunes heads of the base model. Arguments: - heads_to_prune (`Dict[int, List[int]]`): + heads_to_prune (`dict[int, list[int]]`): Dictionary with keys being selected layer indices (`int`) and associated values being the list of heads to prune in said layer (list of `int`). For instance {1: [0, 2], 2: [2, 3]} will prune heads 0 and 2 on layer 1 and heads 2 and 3 on layer 2. @@ -3486,7 +3486,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi For backward compatibility with PEFT library, in case adapter weights are attached to the model, all keys of the state dict of adapters needs to be prepended with `base_model.model`. Advanced users can disable this behaviours by setting `save_peft_format` to `False`. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -3997,7 +3997,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi @classmethod @restore_default_torch_dtype def from_pretrained( - cls: Type[SpecificPreTrainedModelType], + cls: type[SpecificPreTrainedModelType], pretrained_model_name_or_path: Optional[Union[str, os.PathLike]], *model_args, config: Optional[Union[PretrainedConfig, str, os.PathLike]] = None, @@ -4057,7 +4057,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi save directory. - 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. - state_dict (`Dict[str, torch.Tensor]`, *optional*): + state_dict (`dict[str, torch.Tensor]`, *optional*): A state dictionary to use instead of a state dictionary loaded from saved weights file. This option can be used if you want to create a model from a pretrained configuration but load your own @@ -4082,7 +4082,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. output_loading_info(`bool`, *optional*, defaults to `False`): @@ -4131,7 +4131,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi - device_map (`str` or `Dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): + device_map (`str` or `dict[str, Union[int, str, torch.device]]` or `int` or `torch.device`, *optional*): A map that specifies where each submodule should go. It doesn't need to be refined to each parameter/buffer name, once a given module name is inside, every submodule of it will be sent to the same device. If we only pass the device (*e.g.*, `"cpu"`, `"cuda:1"`, `"mps"`, or a GPU ordinal rank @@ -4179,7 +4179,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi Indicates whether unpickler should be restricted to loading only tensors, primitive types, dictionaries and any types added via torch.serialization.add_safe_globals(). When set to False, we can load wrapper tensor subclass weights. - key_mapping (`Dict[str, str], *optional*): + key_mapping (`dict[str, str], *optional*): A potential mapping of the weight names if using a model on the Hub which is compatible to a Transformers architecture, but was not converted accordingly. kwargs (remaining dictionary of keyword arguments, *optional*): @@ -4799,7 +4799,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi return model @staticmethod - def _fix_state_dict_key_on_load(key: str) -> Tuple[str, bool]: + def _fix_state_dict_key_on_load(key: str) -> tuple[str, bool]: """Replace legacy parameter names with their modern equivalents. E.g. beta -> bias, gamma -> weight.""" # Rename LayerNorm beta & gamma params for some early models ported from Tensorflow (e.g. Bert) # This rename is logged. @@ -4826,8 +4826,8 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi def _get_key_renaming_mapping( self, - checkpoint_keys: List[str], - key_mapping: Optional[Dict[str, str]] = None, + checkpoint_keys: list[str], + key_mapping: Optional[dict[str, str]] = None, loading_base_model_from_task_state_dict: bool = False, loading_task_model_from_base_state_dict: bool = False, ): @@ -4885,7 +4885,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi return key_renaming_mapping @staticmethod - def _fix_state_dict_key_on_save(key) -> Tuple[str, bool]: + def _fix_state_dict_key_on_save(key) -> tuple[str, bool]: """ Similar to `_fix_state_dict_key_on_load` allows to define hook for state dict key renaming on model save. Do nothing by default, but can be overridden in particular models. @@ -4903,19 +4903,19 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi def _load_pretrained_model( cls, model: "PreTrainedModel", - state_dict: Optional[Dict], - checkpoint_files: Optional[List[str]], + state_dict: Optional[dict], + checkpoint_files: Optional[list[str]], pretrained_model_name_or_path: Optional[str], ignore_mismatched_sizes: bool = False, - sharded_metadata: Optional[Dict] = None, - device_map: Optional[Dict] = None, + sharded_metadata: Optional[dict] = None, + device_map: Optional[dict] = None, disk_offload_folder: Optional[str] = None, offload_state_dict: Optional[bool] = None, dtype: Optional[torch.dtype] = None, hf_quantizer: Optional[HfQuantizer] = None, keep_in_fp32_regex: Optional[re.Pattern] = None, device_mesh: Optional["torch.distributed.device_mesh.DeviceMesh"] = None, - key_mapping: Optional[Dict[str, str]] = None, + key_mapping: Optional[dict[str, str]] = None, weights_only: bool = True, ): # Useful flags @@ -5485,8 +5485,8 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi def _move_missing_keys_from_meta_to_cpu( self, - missing_keys: List[str], - unexpected_keys: List[str], + missing_keys: list[str], + unexpected_keys: list[str], dtype: Optional[torch.dtype], hf_quantizer: Optional[HfQuantizer], ) -> "PreTrainedModel": @@ -5520,7 +5520,7 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, PushToHubMixin, PeftAdapterMi def _initialize_missing_keys( self, - loaded_keys: List[str], + loaded_keys: list[str], ignore_mismatched_sizes: bool, is_quantized: bool, ) -> "PreTrainedModel": @@ -5846,7 +5846,7 @@ class SQuADHead(nn.Module): is_impossible: Optional[torch.LongTensor] = None, p_mask: Optional[torch.FloatTensor] = None, return_dict: bool = False, - ) -> Union[SquadHeadOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SquadHeadOutput, tuple[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor` of shape `(batch_size, seq_len, hidden_size)`): @@ -6090,7 +6090,7 @@ def is_accelerator_device(device: Union[str, int, torch.device]) -> bool: return torch.device(device).type not in ["meta", "cpu"] -def caching_allocator_warmup(model: PreTrainedModel, expanded_device_map: Dict, hf_quantizer: Optional[HfQuantizer]): +def caching_allocator_warmup(model: PreTrainedModel, expanded_device_map: dict, hf_quantizer: Optional[HfQuantizer]): """This function warm-ups the caching allocator based on the size of the model tensors that will reside on each device. It allows to have one large call to Malloc, instead of recursively calling it later when loading the model, which is actually the loading speed bottleneck. diff --git a/src/transformers/models/albert/modeling_albert.py b/src/transformers/models/albert/modeling_albert.py index 11fd1f939cc..1162c16f61c 100755 --- a/src/transformers/models/albert/modeling_albert.py +++ b/src/transformers/models/albert/modeling_albert.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -277,7 +277,7 @@ class AlbertAttention(nn.Module): x = x.view(new_x_shape) return x.permute(0, 2, 1, 3) - def prune_heads(self, heads: List[int]) -> None: + def prune_heads(self, heads: list[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -301,7 +301,7 @@ class AlbertAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: mixed_query_layer = self.query(hidden_states) mixed_key_layer = self.key(hidden_states) mixed_value_layer = self.value(hidden_states) @@ -366,7 +366,7 @@ class AlbertSdpaAttention(AlbertAttention): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: if self.position_embedding_type != "absolute" or output_attentions: logger.warning( "AlbertSdpaAttention is used but `torch.nn.functional.scaled_dot_product_attention` does not support " @@ -435,7 +435,7 @@ class AlbertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, output_hidden_states: bool = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: attention_output = self.attention(hidden_states, attention_mask, head_mask, output_attentions) ffn_output = apply_chunking_to_forward( @@ -468,7 +468,7 @@ class AlbertLayerGroup(nn.Module): head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, output_hidden_states: bool = False, - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]: layer_hidden_states = () layer_attentions = () @@ -506,7 +506,7 @@ class AlbertTransformer(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, - ) -> Union[BaseModelOutput, Tuple]: + ) -> Union[BaseModelOutput, tuple]: hidden_states = self.embedding_hidden_mapping_in(hidden_states) all_hidden_states = (hidden_states,) if output_hidden_states else None @@ -599,8 +599,8 @@ class AlbertForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None sop_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -637,7 +637,7 @@ class AlbertModel(AlbertPreTrainedModel): def set_input_embeddings(self, value: nn.Embedding) -> None: self.embeddings.word_embeddings = value - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} ALBERT has a different architecture in that its layers are shared across groups, which then has inner groups. If an ALBERT @@ -666,7 +666,7 @@ class AlbertModel(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutputWithPooling, Tuple]: + ) -> Union[BaseModelOutputWithPooling, tuple]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -784,7 +784,7 @@ class AlbertForPreTraining(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[AlbertForPreTrainingOutput, Tuple]: + ) -> Union[AlbertForPreTrainingOutput, tuple]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -929,7 +929,7 @@ class AlbertForMaskedLM(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple]: + ) -> Union[MaskedLMOutput, tuple]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1031,7 +1031,7 @@ class AlbertForSequenceClassification(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple]: + ) -> Union[SequenceClassifierOutput, tuple]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1123,7 +1123,7 @@ class AlbertForTokenClassification(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple]: + ) -> Union[TokenClassifierOutput, tuple]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1190,7 +1190,7 @@ class AlbertForQuestionAnswering(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[AlbertForPreTrainingOutput, Tuple]: + ) -> Union[AlbertForPreTrainingOutput, tuple]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.albert( @@ -1267,7 +1267,7 @@ class AlbertForMultipleChoice(AlbertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[AlbertForPreTrainingOutput, Tuple]: + ) -> Union[AlbertForPreTrainingOutput, tuple]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/albert/modeling_flax_albert.py b/src/transformers/models/albert/modeling_flax_albert.py index f7e5f222070..f2f19cb2771 100644 --- a/src/transformers/models/albert/modeling_flax_albert.py +++ b/src/transformers/models/albert/modeling_flax_albert.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax import flax.linen as nn @@ -77,8 +77,8 @@ class FlaxAlbertForPreTrainingOutput(ModelOutput): prediction_logits: jnp.ndarray = None sop_logits: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None ALBERT_START_DOCSTRING = r""" @@ -518,7 +518,7 @@ class FlaxAlbertPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: AlbertConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -527,7 +527,7 @@ class FlaxAlbertPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.zeros_like(input_ids) diff --git a/src/transformers/models/albert/modeling_tf_albert.py b/src/transformers/models/albert/modeling_tf_albert.py index 6800cfa8d16..bc2a7e0222b 100644 --- a/src/transformers/models/albert/modeling_tf_albert.py +++ b/src/transformers/models/albert/modeling_tf_albert.py @@ -19,7 +19,7 @@ from __future__ import annotations import math from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -253,7 +253,7 @@ class TFAlbertAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(input_tensor)[0] mixed_query_layer = self.query(inputs=input_tensor) mixed_key_layer = self.key(inputs=input_tensor) @@ -350,7 +350,7 @@ class TFAlbertLayer(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( input_tensor=hidden_states, attention_mask=attention_mask, @@ -403,7 +403,7 @@ class TFAlbertLayerGroup(keras.layers.Layer): output_attentions: bool, output_hidden_states: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: layer_hidden_states = () if output_hidden_states else None layer_attentions = () if output_attentions else None @@ -466,7 +466,7 @@ class TFAlbertTransformer(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: hidden_states = self.embedding_hidden_mapping_in(inputs=hidden_states) all_attentions = () if output_attentions else None all_hidden_states = (hidden_states,) if output_hidden_states else None @@ -563,7 +563,7 @@ class TFAlbertMLMHead(keras.layers.Layer): self.decoder.weight = value self.decoder.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias, "decoder_bias": self.decoder_bias} def set_bias(self, value: tf.Variable): @@ -633,7 +633,7 @@ class TFAlbertMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -752,8 +752,8 @@ class TFAlbertForPreTrainingOutput(ModelOutput): loss: Optional[tf.Tensor] = None prediction_logits: Optional[tf.Tensor] = None sop_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None ALBERT_START_DOCSTRING = r""" @@ -883,7 +883,7 @@ class TFAlbertModel(TFAlbertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: outputs = self.albert( input_ids=input_ids, attention_mask=attention_mask, @@ -948,7 +948,7 @@ class TFAlbertForPreTraining(TFAlbertPreTrainedModel, TFAlbertPreTrainingLoss): labels: np.ndarray | tf.Tensor | None = None, sentence_order_label: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFAlbertForPreTrainingOutput, Tuple[tf.Tensor]]: + ) -> Union[TFAlbertForPreTrainingOutput, tuple[tf.Tensor]]: r""" Return: @@ -1075,7 +1075,7 @@ class TFAlbertForMaskedLM(TFAlbertPreTrainedModel, TFMaskedLanguageModelingLoss) return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1198,7 +1198,7 @@ class TFAlbertForSequenceClassification(TFAlbertPreTrainedModel, TFSequenceClass return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1295,7 +1295,7 @@ class TFAlbertForTokenClassification(TFAlbertPreTrainedModel, TFTokenClassificat return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1388,7 +1388,7 @@ class TFAlbertForQuestionAnswering(TFAlbertPreTrainedModel, TFQuestionAnsweringL start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -1490,7 +1490,7 @@ class TFAlbertForMultipleChoice(TFAlbertPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` diff --git a/src/transformers/models/albert/tokenization_albert.py b/src/transformers/models/albert/tokenization_albert.py index 8b34a266f61..69de95a2dfe 100644 --- a/src/transformers/models/albert/tokenization_albert.py +++ b/src/transformers/models/albert/tokenization_albert.py @@ -17,7 +17,7 @@ import os import unicodedata from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -122,7 +122,7 @@ class AlbertTokenizer(PreTrainedTokenizer): pad_token="", cls_token="[CLS]", mask_token="[MASK]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it and @@ -162,7 +162,7 @@ class AlbertTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.sp_model) - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab @@ -197,7 +197,7 @@ class AlbertTokenizer(PreTrainedTokenizer): return outputs - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string.""" text = self.preprocess_text(text) pieces = self.sp_model.encode(text, out_type=str) @@ -247,8 +247,8 @@ class AlbertTokenizer(PreTrainedTokenizer): return out_string.strip() def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An ALBERT sequence has the following format: @@ -272,8 +272,8 @@ class AlbertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -299,7 +299,7 @@ class AlbertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/albert/tokenization_albert_fast.py b/src/transformers/models/albert/tokenization_albert_fast.py index 606c614be67..ed9add51d20 100644 --- a/src/transformers/models/albert/tokenization_albert_fast.py +++ b/src/transformers/models/albert/tokenization_albert_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -131,8 +131,8 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An ALBERT sequence has the following format: @@ -155,7 +155,7 @@ class AlbertTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep return cls + token_ids_0 + sep + token_ids_1 + sep - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/align/configuration_align.py b/src/transformers/models/align/configuration_align.py index 31121b5a712..5f6daa5d43c 100644 --- a/src/transformers/models/align/configuration_align.py +++ b/src/transformers/models/align/configuration_align.py @@ -14,7 +14,7 @@ # limitations under the License. """ALIGN model configuration""" -from typing import TYPE_CHECKING, List +from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -156,19 +156,19 @@ class AlignVisionConfig(PretrainedConfig): Scaling coefficient for network depth at each stage. depth_divisor `int`, *optional*, defaults to 8): A unit of network width. - kernel_sizes (`List[int]`, *optional*, defaults to `[3, 3, 5, 3, 5, 5, 3]`): + kernel_sizes (`list[int]`, *optional*, defaults to `[3, 3, 5, 3, 5, 5, 3]`): List of kernel sizes to be used in each block. - in_channels (`List[int]`, *optional*, defaults to `[32, 16, 24, 40, 80, 112, 192]`): + in_channels (`list[int]`, *optional*, defaults to `[32, 16, 24, 40, 80, 112, 192]`): List of input channel sizes to be used in each block for convolutional layers. - out_channels (`List[int]`, *optional*, defaults to `[16, 24, 40, 80, 112, 192, 320]`): + out_channels (`list[int]`, *optional*, defaults to `[16, 24, 40, 80, 112, 192, 320]`): List of output channel sizes to be used in each block for convolutional layers. - depthwise_padding (`List[int]`, *optional*, defaults to `[]`): + depthwise_padding (`list[int]`, *optional*, defaults to `[]`): List of block indices with square padding. - strides (`List[int]`, *optional*, defaults to `[1, 2, 2, 2, 1, 2, 1]`): + strides (`list[int]`, *optional*, defaults to `[1, 2, 2, 2, 1, 2, 1]`): List of stride sizes to be used in each block for convolutional layers. - num_block_repeats (`List[int]`, *optional*, defaults to `[1, 2, 2, 3, 3, 4, 1]`): + num_block_repeats (`list[int]`, *optional*, defaults to `[1, 2, 2, 3, 3, 4, 1]`): List of the number of times each block is to repeated. - expand_ratios (`List[int]`, *optional*, defaults to `[1, 6, 6, 6, 6, 6, 6]`): + expand_ratios (`list[int]`, *optional*, defaults to `[1, 6, 6, 6, 6, 6, 6]`): List of scaling coefficient of each block. squeeze_expansion_ratio (`float`, *optional*, defaults to 0.25): Squeeze expansion ratio. @@ -214,13 +214,13 @@ class AlignVisionConfig(PretrainedConfig): width_coefficient: float = 2.0, depth_coefficient: float = 3.1, depth_divisor: int = 8, - kernel_sizes: List[int] = [3, 3, 5, 3, 5, 5, 3], - in_channels: List[int] = [32, 16, 24, 40, 80, 112, 192], - out_channels: List[int] = [16, 24, 40, 80, 112, 192, 320], - depthwise_padding: List[int] = [], - strides: List[int] = [1, 2, 2, 2, 1, 2, 1], - num_block_repeats: List[int] = [1, 2, 2, 3, 3, 4, 1], - expand_ratios: List[int] = [1, 6, 6, 6, 6, 6, 6], + kernel_sizes: list[int] = [3, 3, 5, 3, 5, 5, 3], + in_channels: list[int] = [32, 16, 24, 40, 80, 112, 192], + out_channels: list[int] = [16, 24, 40, 80, 112, 192, 320], + depthwise_padding: list[int] = [], + strides: list[int] = [1, 2, 2, 2, 1, 2, 1], + num_block_repeats: list[int] = [1, 2, 2, 3, 3, 4, 1], + expand_ratios: list[int] = [1, 6, 6, 6, 6, 6, 6], squeeze_expansion_ratio: float = 0.25, hidden_act: str = "swish", hidden_dim: int = 2560, diff --git a/src/transformers/models/align/modeling_align.py b/src/transformers/models/align/modeling_align.py index bdebd31a266..952fe0bdc9e 100644 --- a/src/transformers/models/align/modeling_align.py +++ b/src/transformers/models/align/modeling_align.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -57,7 +57,7 @@ class AlignVisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -85,8 +85,8 @@ class AlignTextModelOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -119,7 +119,7 @@ class AlignOutput(ModelOutput): text_model_output: BaseModelOutputWithPoolingAndCrossAttentions = None vision_model_output: BaseModelOutputWithPoolingAndNoAttention = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -155,7 +155,7 @@ def round_filters(config: AlignVisionConfig, num_channels: int): # Copied from transformers.models.efficientnet.modeling_efficientnet.correct_pad -def correct_pad(kernel_size: Union[int, Tuple], adjust: bool = True): +def correct_pad(kernel_size: Union[int, tuple], adjust: bool = True): r""" Utility function to get the tuple padding value for the depthwise convolution. @@ -628,9 +628,9 @@ class AlignTextSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -778,9 +778,9 @@ class AlignTextAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -849,9 +849,9 @@ class AlignTextLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -928,12 +928,12 @@ class AlignTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -1091,7 +1091,7 @@ class AlignTextModel(AlignPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: r""" Examples: @@ -1213,7 +1213,7 @@ class AlignVisionModel(AlignPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: r""" Examples: @@ -1410,7 +1410,7 @@ class AlignModel(AlignPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, AlignOutput]: + ) -> Union[tuple, AlignOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. diff --git a/src/transformers/models/align/processing_align.py b/src/transformers/models/align/processing_align.py index ff0db00d766..33aaca27326 100644 --- a/src/transformers/models/align/processing_align.py +++ b/src/transformers/models/align/processing_align.py @@ -16,7 +16,7 @@ Image/Text processor class for ALIGN """ -from typing import List, Union +from typing import Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack, _validate_images_text_input_order @@ -73,7 +73,7 @@ class AlignProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[AlignProcessorKwargs], @@ -86,10 +86,10 @@ class AlignProcessor(ProcessorMixin): to the docstring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`): + text (`str`, `list[str]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/altclip/configuration_altclip.py b/src/transformers/models/altclip/configuration_altclip.py index 4f1392e6c8b..0a07373edda 100755 --- a/src/transformers/models/altclip/configuration_altclip.py +++ b/src/transformers/models/altclip/configuration_altclip.py @@ -65,7 +65,7 @@ class AltCLIPTextConfig(PretrainedConfig): The epsilon used by the layer normalization layers. pad_token_id (`int`, *optional*, defaults to 1): The id of the *padding* token. bos_token_id (`int`, *optional*, defaults to 0): The id of the *beginning-of-sequence* token. - eos_token_id (`Union[int, List[int]]`, *optional*, defaults to 2): + eos_token_id (`Union[int, list[int]]`, *optional*, defaults to 2): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. position_embedding_type (`str`, *optional*, defaults to `"absolute"`): Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For diff --git a/src/transformers/models/altclip/modeling_altclip.py b/src/transformers/models/altclip/modeling_altclip.py index 4c86159b2c9..3e917940809 100755 --- a/src/transformers/models/altclip/modeling_altclip.py +++ b/src/transformers/models/altclip/modeling_altclip.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn as nn @@ -82,7 +82,7 @@ class AltCLIPOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -219,9 +219,9 @@ class AltRobertaSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -369,9 +369,9 @@ class AltRobertaAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -440,9 +440,9 @@ class AltRobertaLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -519,12 +519,12 @@ class AltRobertaEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -666,7 +666,7 @@ class AltCLIPAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -747,7 +747,7 @@ class AltCLIPEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -805,7 +805,7 @@ class AltCLIPEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -1035,7 +1035,7 @@ class AltCLIPVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1091,7 +1091,7 @@ class AltCLIPVisionModel(AltCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -1183,12 +1183,12 @@ class AltRobertaModel(AltCLIPPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1317,7 +1317,7 @@ class AltCLIPTextModel(AltCLIPPreTrainedModel): output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndProjection]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndProjection]: r""" Examples: @@ -1515,7 +1515,7 @@ class AltCLIPModel(AltCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, AltCLIPOutput]: + ) -> Union[tuple, AltCLIPOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. diff --git a/src/transformers/models/altclip/processing_altclip.py b/src/transformers/models/altclip/processing_altclip.py index 3ce4f2481d4..cc7e3c79435 100644 --- a/src/transformers/models/altclip/processing_altclip.py +++ b/src/transformers/models/altclip/processing_altclip.py @@ -16,7 +16,7 @@ Image/Text processor class for AltCLIP """ -from typing import List, Union +from typing import Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack @@ -59,7 +59,7 @@ class AltCLIPProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[AltClipProcessorKwargs], @@ -76,7 +76,7 @@ class AltCLIPProcessor(ProcessorMixin): images (`ImageInput`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/aria/configuration_aria.py b/src/transformers/models/aria/configuration_aria.py index 1d0e1d3c5fe..67f023e1dbf 100644 --- a/src/transformers/models/aria/configuration_aria.py +++ b/src/transformers/models/aria/configuration_aria.py @@ -18,7 +18,7 @@ # 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. -from typing import Dict, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...modeling_rope_utils import rope_config_validation @@ -104,11 +104,11 @@ class AriaTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -268,7 +268,7 @@ class AriaConfig(PretrainedConfig): vision_config=None, vision_feature_layer: int = -1, text_config: AriaTextConfig = None, - projector_patch_to_query_dict: Optional[Dict] = None, + projector_patch_to_query_dict: Optional[dict] = None, image_token_index: int = 9, initializer_range: float = 0.02, **kwargs, diff --git a/src/transformers/models/aria/image_processing_aria.py b/src/transformers/models/aria/image_processing_aria.py index 512ebc1b19e..d2b6c21a7f1 100644 --- a/src/transformers/models/aria/image_processing_aria.py +++ b/src/transformers/models/aria/image_processing_aria.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections.abc import Iterable -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -43,7 +43,7 @@ from ...utils import TensorType, logging logger = logging.get_logger(__name__) -def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> List[np.array]: +def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> list[np.array]: """ Divides an image into patches of a specified size. @@ -107,11 +107,11 @@ class AriaImageProcessor(BaseImageProcessor): def __init__( self, - image_mean: Optional[List[float]] = None, - image_std: Optional[List[float]] = None, + image_mean: Optional[list[float]] = None, + image_std: Optional[list[float]] = None, max_image_size: int = 980, min_image_size: int = 336, - split_resolutions: Optional[List[Tuple[int, int]]] = None, + split_resolutions: Optional[list[tuple[int, int]]] = None, split_image: Optional[bool] = False, do_convert_rgb: Optional[bool] = True, do_rescale: bool = True, @@ -143,9 +143,9 @@ class AriaImageProcessor(BaseImageProcessor): def preprocess( self, - images: Union[ImageInput, List[ImageInput]], - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + images: Union[ImageInput, list[ImageInput]], + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, max_image_size: Optional[int] = None, min_image_size: Optional[int] = None, split_image: Optional[bool] = None, @@ -388,7 +388,7 @@ class AriaImageProcessor(BaseImageProcessor): def pad( self, image: np.ndarray, - padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], + padding: Union[int, tuple[int, int], Iterable[tuple[int, int]]], mode: PaddingMode = PaddingMode.CONSTANT, constant_values: Union[float, Iterable[float]] = 0.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -402,7 +402,7 @@ class AriaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. @@ -454,19 +454,19 @@ class AriaImageProcessor(BaseImageProcessor): def get_image_patches( self, image: np.array, - grid_pinpoints: List[Tuple[int, int]], + grid_pinpoints: list[tuple[int, int]], patch_size: int, resample: PILImageResampling, data_format: ChannelDimension, input_data_format: ChannelDimension, - ) -> List[np.array]: + ) -> list[np.array]: """ Process an image with variable resolutions by dividing it into patches. Args: image (`np.array`): The input image to be processed. - grid_pinpoints (List[Tuple[int, int]]): + grid_pinpoints (list[tuple[int, int]]): A list of possible resolutions as tuples. patch_size (`int`): Size of the patches to divide the image into. @@ -478,7 +478,7 @@ class AriaImageProcessor(BaseImageProcessor): The channel dimension format of the input image. Returns: - `List[np.array]`: A list of NumPy arrays containing the processed image patches. + `list[np.array]`: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") diff --git a/src/transformers/models/aria/modeling_aria.py b/src/transformers/models/aria/modeling_aria.py index 55b0ed79d70..f501a17eb46 100644 --- a/src/transformers/models/aria/modeling_aria.py +++ b/src/transformers/models/aria/modeling_aria.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union from ...activations import ACT2FN from ...cache_utils import Cache, DynamicCache @@ -527,12 +527,12 @@ class AriaTextAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -599,9 +599,9 @@ class AriaTextDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -996,9 +996,9 @@ class AriaCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -1070,7 +1070,7 @@ class AriaModel(AriaPreTrainedModel): The tensors corresponding to the input images. pixel_mask (`torch.FloatTensor]`, *optional*): The tensors corresponding to the input image mask. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -1102,7 +1102,7 @@ class AriaModel(AriaPreTrainedModel): pixel_mask: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1110,7 +1110,7 @@ class AriaModel(AriaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, AriaModelOutputWithPast]: + ) -> Union[tuple, AriaModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1259,7 +1259,7 @@ class AriaForConditionalGeneration(AriaPreTrainedModel, GenerationMixin): pixel_mask: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1269,7 +1269,7 @@ class AriaForConditionalGeneration(AriaPreTrainedModel, GenerationMixin): logits_to_keep: Union[int, torch.Tensor] = 0, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, AriaCausalLMOutputWithPast]: + ) -> Union[tuple, AriaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/aria/modular_aria.py b/src/transformers/models/aria/modular_aria.py index f9de23cfa1b..b5c18a40b71 100644 --- a/src/transformers/models/aria/modular_aria.py +++ b/src/transformers/models/aria/modular_aria.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -175,11 +175,11 @@ class AriaTextConfig(LlamaConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -270,7 +270,7 @@ class AriaConfig(PretrainedConfig): vision_config=None, vision_feature_layer: int = -1, text_config: AriaTextConfig = None, - projector_patch_to_query_dict: Optional[Dict] = None, + projector_patch_to_query_dict: Optional[dict] = None, image_token_index: int = 9, initializer_range: float = 0.02, **kwargs, @@ -491,11 +491,11 @@ class AriaImageProcessor(BaseImageProcessor): def __init__( self, - image_mean: Optional[List[float]] = None, - image_std: Optional[List[float]] = None, + image_mean: Optional[list[float]] = None, + image_std: Optional[list[float]] = None, max_image_size: int = 980, min_image_size: int = 336, - split_resolutions: Optional[List[Tuple[int, int]]] = None, + split_resolutions: Optional[list[tuple[int, int]]] = None, split_image: Optional[bool] = False, do_convert_rgb: Optional[bool] = True, do_rescale: bool = True, @@ -527,9 +527,9 @@ class AriaImageProcessor(BaseImageProcessor): def preprocess( self, - images: Union[ImageInput, List[ImageInput]], - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + images: Union[ImageInput, list[ImageInput]], + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, max_image_size: Optional[int] = None, min_image_size: Optional[int] = None, split_image: Optional[bool] = None, @@ -772,7 +772,7 @@ class AriaImageProcessor(BaseImageProcessor): def pad( self, image: np.ndarray, - padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], + padding: Union[int, tuple[int, int], Iterable[tuple[int, int]]], mode: PaddingMode = PaddingMode.CONSTANT, constant_values: Union[float, Iterable[float]] = 0.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -786,7 +786,7 @@ class AriaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. @@ -838,19 +838,19 @@ class AriaImageProcessor(BaseImageProcessor): def get_image_patches( self, image: np.array, - grid_pinpoints: List[Tuple[int, int]], + grid_pinpoints: list[tuple[int, int]], patch_size: int, resample: PILImageResampling, data_format: ChannelDimension, input_data_format: ChannelDimension, - ) -> List[np.array]: + ) -> list[np.array]: """ Process an image with variable resolutions by dividing it into patches. Args: image (`np.array`): The input image to be processed. - grid_pinpoints (List[Tuple[int, int]]): + grid_pinpoints (list[tuple[int, int]]): A list of possible resolutions as tuples. patch_size (`int`): Size of the patches to divide the image into. @@ -862,7 +862,7 @@ class AriaImageProcessor(BaseImageProcessor): The channel dimension format of the input image. Returns: - `List[np.array]`: A list of NumPy arrays containing the processed image patches. + `list[np.array]`: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") @@ -945,7 +945,7 @@ class AriaProcessor(ProcessorMixin): image_processor=None, tokenizer: Union[AutoTokenizer, str] = None, chat_template: Optional[str] = None, - size_conversion: Optional[Dict[Union[float, int], int]] = None, + size_conversion: Optional[dict[Union[float, int], int]] = None, ): if size_conversion is None: size_conversion = {490: 128, 980: 256} @@ -960,7 +960,7 @@ class AriaProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], images: Optional[ImageInput] = None, audio=None, videos=None, @@ -970,7 +970,7 @@ class AriaProcessor(ProcessorMixin): Main method to prepare for the model one or several sequences(s) and image(s). Args: - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -1030,7 +1030,7 @@ class AriaProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided @@ -1392,7 +1392,7 @@ class AriaModel(LlavaModel): The tensors corresponding to the input images. pixel_mask (`torch.FloatTensor]`, *optional*): The tensors corresponding to the input image mask. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -1422,7 +1422,7 @@ class AriaModel(LlavaModel): pixel_mask: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1430,7 +1430,7 @@ class AriaModel(LlavaModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, AriaModelOutputWithPast]: + ) -> Union[tuple, AriaModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1518,7 +1518,7 @@ class AriaForConditionalGeneration(LlavaForConditionalGeneration): pixel_mask: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1528,7 +1528,7 @@ class AriaForConditionalGeneration(LlavaForConditionalGeneration): logits_to_keep: Union[int, torch.Tensor] = 0, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, AriaCausalLMOutputWithPast]: + ) -> Union[tuple, AriaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/aria/processing_aria.py b/src/transformers/models/aria/processing_aria.py index 7ecf3af670c..07a684cd76c 100644 --- a/src/transformers/models/aria/processing_aria.py +++ b/src/transformers/models/aria/processing_aria.py @@ -18,7 +18,7 @@ # 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. -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -68,7 +68,7 @@ class AriaProcessor(ProcessorMixin): image_processor=None, tokenizer: Union[AutoTokenizer, str] = None, chat_template: Optional[str] = None, - size_conversion: Optional[Dict[Union[float, int], int]] = None, + size_conversion: Optional[dict[Union[float, int], int]] = None, ): if size_conversion is None: size_conversion = {490: 128, 980: 256} @@ -83,7 +83,7 @@ class AriaProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], images: Optional[ImageInput] = None, audio=None, videos=None, @@ -93,7 +93,7 @@ class AriaProcessor(ProcessorMixin): Main method to prepare for the model one or several sequences(s) and image(s). Args: - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -153,7 +153,7 @@ class AriaProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided diff --git a/src/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py b/src/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py index 77bec930236..ecd8f4858fe 100644 --- a/src/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py +++ b/src/transformers/models/audio_spectrogram_transformer/configuration_audio_spectrogram_transformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Audio Spectogram Transformer (AST) model configuration""" -from typing import Any, Dict +from typing import Any from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -124,7 +124,7 @@ class ASTConfig(PretrainedConfig): # Overwritten from the parent class: AST is not compatible with `generate`, but has a config parameter sharing the # same name (`max_length`). Sharing the same name triggers checks regarding the config -> generation_config # generative parameters deprecation cycle, overwriting this function prevents this from happening. - def _get_non_default_generation_parameters(self) -> Dict[str, Any]: + def _get_non_default_generation_parameters(self) -> dict[str, Any]: return {} diff --git a/src/transformers/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.py b/src/transformers/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.py index 888d38e1870..f56c0c3213b 100644 --- a/src/transformers/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.py +++ b/src/transformers/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.py @@ -16,7 +16,7 @@ Feature extractor class for Audio Spectrogram Transformer. """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -160,7 +160,7 @@ class ASTFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], sampling_rate: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, @@ -169,7 +169,7 @@ class ASTFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. diff --git a/src/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py b/src/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py index ef7bf6ff665..d3ccf24153b 100644 --- a/src/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py +++ b/src/transformers/models/audio_spectrogram_transformer/modeling_audio_spectrogram_transformer.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Audio Spectrogram Transformer (AST) model.""" -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -155,7 +155,7 @@ class ASTSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -216,7 +216,7 @@ class ASTAttention(nn.Module): self.output = ASTSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -239,7 +239,7 @@ class ASTAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -300,7 +300,7 @@ class ASTLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in AST, layernorm is applied before self-attention head_mask, @@ -423,7 +423,7 @@ class ASTModel(ASTPreTrainedModel): def get_input_embeddings(self) -> ASTPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -439,11 +439,11 @@ class ASTModel(ASTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, max_length, num_mel_bins)`): Float values mel features extracted from the raw audio waveform. Raw audio waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~ASTFeatureExtractor.__call__`] @@ -533,7 +533,7 @@ class ASTForAudioClassification(ASTPreTrainedModel): r""" input_values (`torch.FloatTensor` of shape `(batch_size, max_length, num_mel_bins)`): Float values mel features extracted from the raw audio waveform. Raw audio waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~ASTFeatureExtractor.__call__`] diff --git a/src/transformers/models/auto/auto_factory.py b/src/transformers/models/auto/auto_factory.py index 00c29860961..eeb959de186 100644 --- a/src/transformers/models/auto/auto_factory.py +++ b/src/transformers/models/auto/auto_factory.py @@ -118,7 +118,7 @@ FROM_PRETRAINED_TORCH_DOCSTRING = """ save directory. - 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. - state_dict (*Dict[str, torch.Tensor]*, *optional*): + state_dict (*dict[str, torch.Tensor]*, *optional*): A state dictionary to use instead of a state dictionary loaded from saved weights file. This option can be used if you want to create a model from a pretrained configuration but load your own @@ -136,7 +136,7 @@ FROM_PRETRAINED_TORCH_DOCSTRING = """ resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. output_loading_info(`bool`, *optional*, defaults to `False`): @@ -235,7 +235,7 @@ FROM_PRETRAINED_TF_DOCSTRING = """ resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. output_loading_info(`bool`, *optional*, defaults to `False`): @@ -334,7 +334,7 @@ FROM_PRETRAINED_FLAX_DOCSTRING = """ resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. output_loading_info(`bool`, *optional*, defaults to `False`): diff --git a/src/transformers/models/auto/configuration_auto.py b/src/transformers/models/auto/configuration_auto.py index 5b46868f64b..9e9d464953d 100644 --- a/src/transformers/models/auto/configuration_auto.py +++ b/src/transformers/models/auto/configuration_auto.py @@ -1096,7 +1096,7 @@ class AutoConfig: resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. revision (`str`, *optional*, defaults to `"main"`): diff --git a/src/transformers/models/auto/feature_extraction_auto.py b/src/transformers/models/auto/feature_extraction_auto.py index dc94a15c7ef..e7db1944d31 100644 --- a/src/transformers/models/auto/feature_extraction_auto.py +++ b/src/transformers/models/auto/feature_extraction_auto.py @@ -19,7 +19,7 @@ import json import os import warnings from collections import OrderedDict -from typing import Dict, Optional, Union +from typing import Optional, Union # Build the list of all feature extractors from ...configuration_utils import PretrainedConfig @@ -148,7 +148,7 @@ def get_feature_extractor_config( cache_dir: Optional[Union[str, os.PathLike]] = None, force_download: bool = False, resume_download: Optional[bool] = None, - proxies: Optional[Dict[str, str]] = None, + proxies: Optional[dict[str, str]] = None, token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, local_files_only: bool = False, @@ -175,7 +175,7 @@ def get_feature_extractor_config( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -292,7 +292,7 @@ class AutoFeatureExtractor: resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -311,7 +311,7 @@ class AutoFeatureExtractor: Whether or not to allow for custom models defined on the Hub in their own modeling files. This option should only be set to `True` for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are feature extractor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* feature extractor attributes is controlled by the `return_unused_kwargs` keyword parameter. diff --git a/src/transformers/models/auto/image_processing_auto.py b/src/transformers/models/auto/image_processing_auto.py index 5b718024659..a3feebced91 100644 --- a/src/transformers/models/auto/image_processing_auto.py +++ b/src/transformers/models/auto/image_processing_auto.py @@ -19,7 +19,7 @@ import json import os import warnings from collections import OrderedDict -from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union # Build the list of all image processors from ...configuration_utils import PretrainedConfig @@ -52,7 +52,7 @@ logger = logging.get_logger(__name__) if TYPE_CHECKING: # This significantly improves completion suggestion performance when # the transformers package is used with Microsoft's Pylance language server. - IMAGE_PROCESSOR_MAPPING_NAMES: OrderedDict[str, Tuple[Optional[str], Optional[str]]] = OrderedDict() + IMAGE_PROCESSOR_MAPPING_NAMES: OrderedDict[str, tuple[Optional[str], Optional[str]]] = OrderedDict() else: IMAGE_PROCESSOR_MAPPING_NAMES = OrderedDict( [ @@ -224,7 +224,7 @@ def get_image_processor_config( cache_dir: Optional[Union[str, os.PathLike]] = None, force_download: bool = False, resume_download: Optional[bool] = None, - proxies: Optional[Dict[str, str]] = None, + proxies: Optional[dict[str, str]] = None, token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, local_files_only: bool = False, @@ -251,7 +251,7 @@ def get_image_processor_config( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -376,7 +376,7 @@ class AutoImageProcessor: resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -401,7 +401,7 @@ class AutoImageProcessor: execute code present on the Hub on your local machine. image_processor_filename (`str`, *optional*, defaults to `"config.json"`): The name of the file in the model directory to use for the image processor config. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are image processor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* image processor attributes is controlled by the `return_unused_kwargs` keyword parameter. diff --git a/src/transformers/models/auto/processing_auto.py b/src/transformers/models/auto/processing_auto.py index 7eb850cfe43..c81d2ab9055 100644 --- a/src/transformers/models/auto/processing_auto.py +++ b/src/transformers/models/auto/processing_auto.py @@ -209,7 +209,7 @@ class AutoProcessor: resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -228,7 +228,7 @@ class AutoProcessor: Whether or not to allow for custom models defined on the Hub in their own modeling files. This option should only be set to `True` for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are feature extractor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* feature extractor attributes is controlled by the `return_unused_kwargs` keyword parameter. diff --git a/src/transformers/models/auto/video_processing_auto.py b/src/transformers/models/auto/video_processing_auto.py index e9e490bb28c..0e63093f715 100644 --- a/src/transformers/models/auto/video_processing_auto.py +++ b/src/transformers/models/auto/video_processing_auto.py @@ -19,7 +19,7 @@ import json import os import warnings from collections import OrderedDict -from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union # Build the list of all video processors from ...configuration_utils import PretrainedConfig @@ -42,7 +42,7 @@ logger = logging.get_logger(__name__) if TYPE_CHECKING: # This significantly improves completion suggestion performance when # the transformers package is used with Microsoft's Pylance language server. - VIDEO_PROCESSOR_MAPPING_NAMES: OrderedDict[str, Tuple[Optional[str], Optional[str]]] = OrderedDict() + VIDEO_PROCESSOR_MAPPING_NAMES: OrderedDict[str, tuple[Optional[str], Optional[str]]] = OrderedDict() else: VIDEO_PROCESSOR_MAPPING_NAMES = OrderedDict( [ @@ -101,7 +101,7 @@ def get_video_processor_config( cache_dir: Optional[Union[str, os.PathLike]] = None, force_download: bool = False, resume_download: Optional[bool] = None, - proxies: Optional[Dict[str, str]] = None, + proxies: Optional[dict[str, str]] = None, token: Optional[Union[bool, str]] = None, revision: Optional[str] = None, local_files_only: bool = False, @@ -128,7 +128,7 @@ def get_video_processor_config( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -243,7 +243,7 @@ class AutoVideoProcessor: resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -262,7 +262,7 @@ class AutoVideoProcessor: Whether or not to allow for custom models defined on the Hub in their own modeling files. This option should only be set to `True` for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are video processor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* video processor attributes is controlled by the `return_unused_kwargs` keyword parameter. diff --git a/src/transformers/models/autoformer/configuration_autoformer.py b/src/transformers/models/autoformer/configuration_autoformer.py index aba83f19a5d..24f0f37a8c8 100644 --- a/src/transformers/models/autoformer/configuration_autoformer.py +++ b/src/transformers/models/autoformer/configuration_autoformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Autoformer model configuration""" -from typing import List, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -142,14 +142,14 @@ class AutoformerConfig(PretrainedConfig): distribution_output: str = "student_t", loss: str = "nll", input_size: int = 1, - lags_sequence: List[int] = [1, 2, 3, 4, 5, 6, 7], + lags_sequence: list[int] = [1, 2, 3, 4, 5, 6, 7], scaling: bool = True, num_time_features: int = 0, num_dynamic_real_features: int = 0, num_static_categorical_features: int = 0, num_static_real_features: int = 0, - cardinality: Optional[List[int]] = None, - embedding_dimension: Optional[List[int]] = None, + cardinality: Optional[list[int]] = None, + embedding_dimension: Optional[list[int]] = None, d_model: int = 64, encoder_attention_heads: int = 2, decoder_attention_heads: int = 2, diff --git a/src/transformers/models/autoformer/modeling_autoformer.py b/src/transformers/models/autoformer/modeling_autoformer.py index dd3fbf57eaf..6db63b4945f 100644 --- a/src/transformers/models/autoformer/modeling_autoformer.py +++ b/src/transformers/models/autoformer/modeling_autoformer.py @@ -18,7 +18,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -87,10 +87,10 @@ class AutoFormerDecoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None trend: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -155,13 +155,13 @@ class AutoformerModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None trend: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None static_features: Optional[torch.FloatTensor] = None @@ -179,7 +179,7 @@ class AutoformerFeatureEmbedder(nn.Module): List of embedding dimensions of the categorical features. """ - def __init__(self, cardinalities: List[int], embedding_dims: List[int]) -> None: + def __init__(self, cardinalities: list[int], embedding_dims: list[int]) -> None: super().__init__() self.num_features = len(cardinalities) @@ -217,7 +217,7 @@ class AutoformerStdScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -254,7 +254,7 @@ class AutoformerMeanScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -306,7 +306,7 @@ class AutoformerNOPScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: Optional[torch.Tensor] = None - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -491,11 +491,11 @@ class AutoformerAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -696,7 +696,7 @@ class AutoformerEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -796,10 +796,10 @@ class AutoformerDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -972,7 +972,7 @@ class AutoformerEncoder(AutoformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1110,13 +1110,13 @@ class AutoformerDecoder(AutoformerPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, AutoFormerDecoderOutput]: + ) -> Union[tuple, AutoFormerDecoderOutput]: r""" Args: trend (`torch.FloatTensor` of shape `(batch_size, prediction_length, feature_size)`, *optional*): @@ -1374,7 +1374,7 @@ class AutoformerModel(AutoformerPreTrainedModel): past_observed_mask: Optional[torch.Tensor] = None, future_values: Optional[torch.Tensor] = None, future_time_features: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: """ Creates the inputs for the network given the past and future values, time features, and static features. @@ -1485,13 +1485,13 @@ class AutoformerModel(AutoformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[AutoformerModelOutput, Tuple]: + ) -> Union[AutoformerModelOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Past values of the time series, that serve as context in order to predict the future. These values may @@ -1751,13 +1751,13 @@ class AutoformerForPrediction(AutoformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Seq2SeqTSPredictionOutput, Tuple]: + ) -> Union[Seq2SeqTSPredictionOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Past values of the time series, that serve as context in order to predict the future. These values may diff --git a/src/transformers/models/aya_vision/modeling_aya_vision.py b/src/transformers/models/aya_vision/modeling_aya_vision.py index f3b9d77b2ce..6430ef425f9 100644 --- a/src/transformers/models/aya_vision/modeling_aya_vision.py +++ b/src/transformers/models/aya_vision/modeling_aya_vision.py @@ -20,7 +20,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -150,9 +150,9 @@ class AyaVisionCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -214,7 +214,7 @@ class AyaVisionModel(AyaVisionPreTrainedModel): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -224,7 +224,7 @@ class AyaVisionModel(AyaVisionPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`): The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -274,9 +274,9 @@ class AyaVisionModel(AyaVisionPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -284,7 +284,7 @@ class AyaVisionModel(AyaVisionPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, AyaVisionModelOutputWithPast]: + ) -> Union[tuple, AyaVisionModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -397,7 +397,7 @@ class AyaVisionForConditionalGeneration(AyaVisionPreTrainedModel, GenerationMixi def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -429,9 +429,9 @@ class AyaVisionForConditionalGeneration(AyaVisionPreTrainedModel, GenerationMixi pixel_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -442,7 +442,7 @@ class AyaVisionForConditionalGeneration(AyaVisionPreTrainedModel, GenerationMixi logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, AyaVisionCausalLMOutputWithPast]: + ) -> Union[tuple, AyaVisionCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/aya_vision/modular_aya_vision.py b/src/transformers/models/aya_vision/modular_aya_vision.py index 533a7f44447..ad5c1e58d43 100644 --- a/src/transformers/models/aya_vision/modular_aya_vision.py +++ b/src/transformers/models/aya_vision/modular_aya_vision.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch AyaVision model.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -121,7 +121,7 @@ class AyaVisionModel(LlavaModel): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -131,7 +131,7 @@ class AyaVisionModel(LlavaModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`): The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -181,9 +181,9 @@ class AyaVisionModel(LlavaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -191,7 +191,7 @@ class AyaVisionModel(LlavaModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, AyaVisionModelOutputWithPast]: + ) -> Union[tuple, AyaVisionModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -267,9 +267,9 @@ class AyaVisionForConditionalGeneration(LlavaForConditionalGeneration): pixel_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -280,7 +280,7 @@ class AyaVisionForConditionalGeneration(LlavaForConditionalGeneration): logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, AyaVisionCausalLMOutputWithPast]: + ) -> Union[tuple, AyaVisionCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/aya_vision/processing_aya_vision.py b/src/transformers/models/aya_vision/processing_aya_vision.py index be3f04a1819..8eacb0b5bba 100644 --- a/src/transformers/models/aya_vision/processing_aya_vision.py +++ b/src/transformers/models/aya_vision/processing_aya_vision.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -139,7 +139,7 @@ class AyaVisionProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[AyaVisionProcessorKwargs], @@ -151,10 +151,10 @@ class AyaVisionProcessor(ProcessorMixin): GotOcr2ImageProcessor's [`~GotOcr2ImageProcessor.__call__`] if `images` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -225,7 +225,7 @@ class AyaVisionProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/bamba/convert_mamba_ssm_checkpoint.py b/src/transformers/models/bamba/convert_mamba_ssm_checkpoint.py index d2487b4e5be..eaf387a8927 100644 --- a/src/transformers/models/bamba/convert_mamba_ssm_checkpoint.py +++ b/src/transformers/models/bamba/convert_mamba_ssm_checkpoint.py @@ -19,7 +19,7 @@ import json import os import re from os import path -from typing import Dict, Optional, Union +from typing import Optional, Union import torch from huggingface_hub import split_torch_state_dict_into_shards @@ -31,7 +31,7 @@ from transformers.utils import SAFE_WEIGHTS_INDEX_NAME, SAFE_WEIGHTS_NAME from .configuration_bamba import BambaConfig -def convert_state_dict_from_mamba_ssm(original_sd: Dict) -> Dict[str, torch.Tensor]: +def convert_state_dict_from_mamba_ssm(original_sd: dict) -> dict[str, torch.Tensor]: state_dict = {} for orig_k, param in original_sd.items(): @@ -85,7 +85,7 @@ def convert_state_dict_from_mamba_ssm(original_sd: Dict) -> Dict[str, torch.Tens # Adapted from transformers.models.mamba.convert_mamba_ssm_checkpoint_to_pytorch.py def convert_ssm_config_to_hf_config( - config_ssm: Dict, + config_ssm: dict, **kwargs, ) -> BambaConfig: """Convert a config from mamba_ssm to a BambaConfig from here.""" @@ -129,9 +129,9 @@ def convert_ssm_config_to_hf_config( def save_single_safetensor( - state_dict: Dict, + state_dict: dict, save_directory: str, - metadata: Dict, + metadata: dict, ): save_file( state_dict, @@ -141,9 +141,9 @@ def save_single_safetensor( def save_sharded_safetensors( - state_dict: Dict, + state_dict: dict, save_directory: str, - metadata: Dict, + metadata: dict, max_shard_size: Union[int, str] = "5GB", ): filename_pattern = SAFE_WEIGHTS_NAME.replace(".bin", "{suffix}.bin").replace( diff --git a/src/transformers/models/bamba/modeling_bamba.py b/src/transformers/models/bamba/modeling_bamba.py index 5432260c7dd..95af1f25136 100644 --- a/src/transformers/models/bamba/modeling_bamba.py +++ b/src/transformers/models/bamba/modeling_bamba.py @@ -24,7 +24,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple, TypedDict, Union +from typing import Callable, Optional, TypedDict, Union import torch from torch import nn @@ -291,12 +291,12 @@ class BambaAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -965,9 +965,9 @@ class BambaDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[BambaFlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -982,7 +982,7 @@ class BambaDecoderLayer(GradientCheckpointingLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): diff --git a/src/transformers/models/bamba/modular_bamba.py b/src/transformers/models/bamba/modular_bamba.py index 810eef9868d..0d42d6e1432 100644 --- a/src/transformers/models/bamba/modular_bamba.py +++ b/src/transformers/models/bamba/modular_bamba.py @@ -19,7 +19,7 @@ # limitations under the License. """PyTorch Bamba model.""" -from typing import Optional, Tuple, TypedDict, Union +from typing import Optional, TypedDict, Union import torch import torch.utils.checkpoint @@ -738,9 +738,9 @@ class BambaDecoderLayer(JambaAttentionDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[BambaFlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -755,7 +755,7 @@ class BambaDecoderLayer(JambaAttentionDecoderLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): diff --git a/src/transformers/models/bark/configuration_bark.py b/src/transformers/models/bark/configuration_bark.py index e8e304d218a..5bbc0d5a77b 100644 --- a/src/transformers/models/bark/configuration_bark.py +++ b/src/transformers/models/bark/configuration_bark.py @@ -14,7 +14,7 @@ # limitations under the License. """BARK model configuration""" -from typing import Dict, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import add_start_docstrings, logging @@ -243,10 +243,10 @@ class BarkConfig(PretrainedConfig): def __init__( self, - semantic_config: Optional[Dict] = None, - coarse_acoustics_config: Optional[Dict] = None, - fine_acoustics_config: Optional[Dict] = None, - codec_config: Optional[Dict] = None, + semantic_config: Optional[dict] = None, + coarse_acoustics_config: Optional[dict] = None, + fine_acoustics_config: Optional[dict] = None, + codec_config: Optional[dict] = None, initializer_range=0.02, **kwargs, ): diff --git a/src/transformers/models/bark/generation_configuration_bark.py b/src/transformers/models/bark/generation_configuration_bark.py index bb1fc266550..0fa68184c88 100644 --- a/src/transformers/models/bark/generation_configuration_bark.py +++ b/src/transformers/models/bark/generation_configuration_bark.py @@ -15,7 +15,7 @@ """BARK model generation configuration""" import copy -from typing import Dict, Optional +from typing import Optional from ...generation.configuration_utils import GenerationConfig from ...utils import logging @@ -245,9 +245,9 @@ class BarkGenerationConfig(GenerationConfig): def __init__( self, - semantic_config: Optional[Dict] = None, - coarse_acoustics_config: Optional[Dict] = None, - fine_acoustics_config: Optional[Dict] = None, + semantic_config: Optional[dict] = None, + coarse_acoustics_config: Optional[dict] = None, + fine_acoustics_config: Optional[dict] = None, sample_rate=24_000, codebook_size=1024, **kwargs, @@ -318,7 +318,7 @@ class BarkGenerationConfig(GenerationConfig): Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) diff --git a/src/transformers/models/bark/modeling_bark.py b/src/transformers/models/bark/modeling_bark.py index 7a564393f9e..4ee608d9aec 100644 --- a/src/transformers/models/bark/modeling_bark.py +++ b/src/transformers/models/bark/modeling_bark.py @@ -16,7 +16,7 @@ import math import warnings -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -506,7 +506,7 @@ class BarkCausalModel(BarkPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, @@ -516,7 +516,7 @@ class BarkCausalModel(BarkPreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithPast]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithPast]: r""" input_embeds (`torch.FloatTensor` of shape `(batch_size, input_sequence_length, hidden_size)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. @@ -659,8 +659,8 @@ class BarkCausalModel(BarkPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -687,7 +687,7 @@ class BarkSemanticModel(BarkCausalModel): self, input_ids: torch.Tensor, semantic_generation_config: BarkSemanticGenerationConfig = None, - history_prompt: Optional[Dict[str, torch.Tensor]] = None, + history_prompt: Optional[dict[str, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, **kwargs, ) -> torch.LongTensor: @@ -701,7 +701,7 @@ class BarkSemanticModel(BarkCausalModel): long as the longest generation among the batch. semantic_generation_config (`BarkSemanticGenerationConfig`): Generation config indicating how to generate the semantic tokens. - history_prompt (`Optional[Dict[str,torch.Tensor]]`, *optional*): + history_prompt (`Optional[dict[str,torch.Tensor]]`, *optional*): Optional `Bark` speaker prompt. attention_mask (`Optional[torch.Tensor]`, *optional*): Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`: @@ -801,7 +801,7 @@ class BarkCoarseModel(BarkCausalModel): batch_size: int, semantic_generation_config: int, codebook_size: int, - history_prompt: Optional[Dict[str, torch.Tensor]] = None, + history_prompt: Optional[dict[str, torch.Tensor]] = None, ): """ Preprocess the optional `Bark` speaker prompts before `self.generate`. @@ -817,7 +817,7 @@ class BarkCoarseModel(BarkCausalModel): Generation config indicating how to generate the semantic tokens. codebook_size (`int`): Codebook channel size, i.e. the size of the output vocabulary per codebook channel. - history_prompt (`Optional[Dict[str,torch.Tensor]]`): + history_prompt (`Optional[dict[str,torch.Tensor]]`): Optional `Bark` speaker prompt. Returns: Returns: `tuple(torch.FloatTensor)`: @@ -874,10 +874,10 @@ class BarkCoarseModel(BarkCausalModel): semantic_generation_config: BarkSemanticGenerationConfig = None, coarse_generation_config: BarkCoarseGenerationConfig = None, codebook_size: int = 1024, - history_prompt: Optional[Dict[str, torch.Tensor]] = None, + history_prompt: Optional[dict[str, torch.Tensor]] = None, return_output_lengths: Optional[bool] = None, **kwargs, - ) -> Union[torch.LongTensor, Tuple[torch.LongTensor, torch.LongTensor]]: + ) -> Union[torch.LongTensor, tuple[torch.LongTensor, torch.LongTensor]]: """ Generates coarse acoustics tokens from input text semantic tokens and an additional optional `Bark` speaker prompt. @@ -891,7 +891,7 @@ class BarkCoarseModel(BarkCausalModel): Generation config indicating how to generate the coarse tokens. codebook_size (`int`, *optional*, defaults to 1024): Codebook channel size, i.e. the size of the output vocabulary per codebook channel. - history_prompt (`Optional[Dict[str,torch.Tensor]]`, *optional*): + history_prompt (`Optional[dict[str,torch.Tensor]]`, *optional*): Optional `Bark` speaker prompt. return_output_lengths (`bool`, *optional*): Whether or not to return the output lengths. Useful when batching. @@ -1179,7 +1179,7 @@ class BarkFineModel(BarkPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" codebook_idx (`int`): Index of the codebook that will be predicted. @@ -1295,7 +1295,7 @@ class BarkFineModel(BarkPreTrainedModel): coarse_generation_config: BarkCoarseGenerationConfig = None, fine_generation_config: BarkFineGenerationConfig = None, codebook_size: int = 1024, - history_prompt: Optional[Dict[str, torch.Tensor]] = None, + history_prompt: Optional[dict[str, torch.Tensor]] = None, **kwargs, ) -> torch.LongTensor: """ @@ -1313,7 +1313,7 @@ class BarkFineModel(BarkPreTrainedModel): Generation config indicating how to generate the fine tokens. codebook_size (`int`, *optional*, defaults to 1024): Codebook channel size, i.e. the size of the output vocabulary per codebook channel. - history_prompt (`Optional[Dict[str,torch.Tensor]]`, *optional*): + history_prompt (`Optional[dict[str,torch.Tensor]]`, *optional*): Optional `Bark` speaker prompt. Returns: torch.LongTensor: Output fine acoustics tokens. @@ -1563,7 +1563,7 @@ class BarkModel(BarkPreTrainedModel): def generate( self, input_ids: Optional[torch.Tensor] = None, - history_prompt: Optional[Dict[str, torch.Tensor]] = None, + history_prompt: Optional[dict[str, torch.Tensor]] = None, return_output_lengths: Optional[bool] = None, **kwargs, ) -> torch.LongTensor: @@ -1574,7 +1574,7 @@ class BarkModel(BarkPreTrainedModel): input_ids (`Optional[torch.Tensor]` of shape (batch_size, seq_len), *optional*): Input ids. Will be truncated up to 256 tokens. Note that the output audios will be as long as the longest generation among the batch. - history_prompt (`Optional[Dict[str,torch.Tensor]]`, *optional*): + history_prompt (`Optional[dict[str,torch.Tensor]]`, *optional*): Optional `Bark` speaker prompt. Note that for now, this model takes only one speaker prompt per batch. kwargs (*optional*): Remaining dictionary of keyword arguments. Keyword arguments are of two types: @@ -1710,7 +1710,7 @@ class BarkModel(BarkPreTrainedModel): cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, hard_check_only: bool = False, check_device_map: bool = False, ): diff --git a/src/transformers/models/bark/processing_bark.py b/src/transformers/models/bark/processing_bark.py index d2ec0629e42..e2b47dca6ac 100644 --- a/src/transformers/models/bark/processing_bark.py +++ b/src/transformers/models/bark/processing_bark.py @@ -39,7 +39,7 @@ class BarkProcessor(ProcessorMixin): Args: tokenizer ([`PreTrainedTokenizer`]): An instance of [`PreTrainedTokenizer`]. - speaker_embeddings (`Dict[Dict[str]]`, *optional*): + speaker_embeddings (`dict[dict[str]]`, *optional*): Optional nested speaker embeddings dictionary. The first level contains voice preset names (e.g `"en_speaker_4"`). The second level contains `"semantic_prompt"`, `"coarse_prompt"` and `"fine_prompt"` embeddings. The values correspond to the path of the corresponding `np.ndarray`. See @@ -240,11 +240,11 @@ class BarkProcessor(ProcessorMixin): to the tokenizer and to `cached_file` method if `voice_preset` is a valid filename. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - voice_preset (`str`, `Dict[np.ndarray]`): + voice_preset (`str`, `dict[np.ndarray]`): The voice preset, i.e the speaker embeddings. It can either be a valid voice_preset name, e.g `"en_speaker_1"`, or directly a dictionary of `np.ndarray` embeddings for each submodel of `Bark`. Or it can be a valid file name of a local `.npz` single voice preset. diff --git a/src/transformers/models/bart/modeling_bart.py b/src/transformers/models/bart/modeling_bart.py index 15c90e64d80..f0adc76924f 100755 --- a/src/transformers/models/bart/modeling_bart.py +++ b/src/transformers/models/bart/modeling_bart.py @@ -17,7 +17,7 @@ import copy import math import warnings -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -198,7 +198,7 @@ class BartAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -296,7 +296,7 @@ class BartEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -385,7 +385,7 @@ class BartDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -782,7 +782,7 @@ class BartEncoder(BartPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -956,14 +956,14 @@ class BartDecoder(BartPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1253,8 +1253,8 @@ class BartModel(BartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1262,7 +1262,7 @@ class BartModel(BartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqModelOutput]: + ) -> Union[tuple, Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1425,8 +1425,8 @@ class BartForConditionalGeneration(BartPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1435,7 +1435,7 @@ class BartForConditionalGeneration(BartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1612,7 +1612,7 @@ class BartForSequenceClassification(BartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1621,7 +1621,7 @@ class BartForSequenceClassification(BartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1757,7 +1757,7 @@ class BartForQuestionAnswering(BartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1767,7 +1767,7 @@ class BartForQuestionAnswering(BartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple, Seq2SeqQuestionAnsweringModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1925,7 +1925,7 @@ class BartForCausalLM(BartPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1933,7 +1933,7 @@ class BartForCausalLM(BartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/bart/modeling_flax_bart.py b/src/transformers/models/bart/modeling_flax_bart.py index a2d3e44c564..818254f3bfa 100644 --- a/src/transformers/models/bart/modeling_flax_bart.py +++ b/src/transformers/models/bart/modeling_flax_bart.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -203,7 +203,7 @@ BART_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -309,7 +309,7 @@ class FlaxBartAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -429,7 +429,7 @@ class FlaxBartEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -552,7 +552,7 @@ class FlaxBartDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states # Self Attention @@ -910,7 +910,7 @@ class FlaxBartPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BartConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -919,7 +919,7 @@ class FlaxBartPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") # make sure initialization pass will work for FlaxBartForSequenceClassificationModule @@ -1742,7 +1742,7 @@ class FlaxBartDecoderPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BartConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -1753,7 +1753,7 @@ class FlaxBartDecoderPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/bart/modeling_tf_bart.py b/src/transformers/models/bart/modeling_tf_bart.py index e345807d9df..d0535a57da8 100644 --- a/src/transformers/models/bart/modeling_tf_bart.py +++ b/src/transformers/models/bart/modeling_tf_bart.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -181,11 +181,11 @@ class TFBartAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -427,9 +427,9 @@ class TFBartDecoderLayer(keras.layers.Layer): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_value: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -710,7 +710,7 @@ BART_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -780,7 +780,7 @@ class TFBartEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: """ Args: input_ids (`tf.Tensor` of shape `(batch_size, sequence_length)`): @@ -938,13 +938,13 @@ class TFBartDecoder(keras.layers.Layer): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: r""" Args: input_ids (`tf.Tensor` of shape `(batch_size, sequence_length)`): @@ -988,7 +988,7 @@ class TFBartDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1167,8 +1167,8 @@ class TFBartMainLayer(keras.layers.Layer): head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1177,7 +1177,7 @@ class TFBartMainLayer(keras.layers.Layer): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFSeq2SeqModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqModelOutput, tuple[tf.Tensor]]: # different to other models, Bart automatically creates decoder_input_ids from # input_ids if no decoder_input_ids are provided if decoder_input_ids is None and decoder_inputs_embeds is None: @@ -1297,8 +1297,8 @@ class TFBartModel(TFBartPretrainedModel): head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1307,7 +1307,7 @@ class TFBartModel(TFBartPretrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1430,7 +1430,7 @@ class TFBartForConditionalGeneration(TFBartPretrainedModel, TFCausalLanguageMode decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: Optional[TFBaseModelOutput] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1439,7 +1439,7 @@ class TFBartForConditionalGeneration(TFBartPretrainedModel, TFCausalLanguageMode return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSeq2SeqLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1601,7 +1601,7 @@ class TFBartForSequenceClassification(TFBartPretrainedModel, TFSequenceClassific decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: Optional[TFBaseModelOutput] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1610,7 +1610,7 @@ class TFBartForSequenceClassification(TFBartPretrainedModel, TFSequenceClassific return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSeq2SeqSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/bart/tokenization_bart.py b/src/transformers/models/bart/tokenization_bart.py index 4c516cb81be..e5c216346c1 100644 --- a/src/transformers/models/bart/tokenization_bart.py +++ b/src/transformers/models/bart/tokenization_bart.py @@ -16,7 +16,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -279,7 +279,7 @@ class BartTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -309,8 +309,8 @@ class BartTokenizer(PreTrainedTokenizer): return vocab_file, merge_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BART sequence has the following format: @@ -319,13 +319,13 @@ class BartTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -334,22 +334,22 @@ class BartTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -361,20 +361,20 @@ class BartTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. BART does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/bart/tokenization_bart_fast.py b/src/transformers/models/bart/tokenization_bart_fast.py index f7c92b9d22c..88b002f5952 100644 --- a/src/transformers/models/bart/tokenization_bart_fast.py +++ b/src/transformers/models/bart/tokenization_bart_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -233,7 +233,7 @@ class BartTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -245,20 +245,20 @@ class BartTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. BART does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/barthez/tokenization_barthez.py b/src/transformers/models/barthez/tokenization_barthez.py index 0db6634d986..bc583e0cd5d 100644 --- a/src/transformers/models/barthez/tokenization_barthez.py +++ b/src/transformers/models/barthez/tokenization_barthez.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -117,7 +117,7 @@ class BarthezTokenizer(PreTrainedTokenizer): unk_token="", pad_token="", mask_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it. Will have normalized=False by default this way @@ -141,8 +141,8 @@ class BarthezTokenizer(PreTrainedTokenizer): ) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BARThez sequence has the following format: @@ -151,13 +151,13 @@ class BarthezTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -167,22 +167,22 @@ class BarthezTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -194,19 +194,19 @@ class BarthezTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -224,7 +224,7 @@ class BarthezTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -270,7 +270,7 @@ class BarthezTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/barthez/tokenization_barthez_fast.py b/src/transformers/models/barthez/tokenization_barthez_fast.py index 70c301eee91..64050ca8848 100644 --- a/src/transformers/models/barthez/tokenization_barthez_fast.py +++ b/src/transformers/models/barthez/tokenization_barthez_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -83,7 +83,7 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -123,8 +123,8 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BARThez sequence has the following format: @@ -133,13 +133,13 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -149,19 +149,19 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -170,7 +170,7 @@ class BarthezTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/bartpho/tokenization_bartpho.py b/src/transformers/models/bartpho/tokenization_bartpho.py index c3e121089e6..41a122bf913 100644 --- a/src/transformers/models/bartpho/tokenization_bartpho.py +++ b/src/transformers/models/bartpho/tokenization_bartpho.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -117,7 +117,7 @@ class BartphoTokenizer(PreTrainedTokenizer): unk_token="", pad_token="", mask_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -177,8 +177,8 @@ class BartphoTokenizer(PreTrainedTokenizer): self.sp_model.LoadFromSerializedProto(self.sp_model_proto) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An BARTPho sequence has the following format: @@ -187,13 +187,13 @@ class BartphoTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -203,22 +203,22 @@ class BartphoTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -231,20 +231,20 @@ class BartphoTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. BARTPho does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -264,7 +264,7 @@ class BartphoTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -283,7 +283,7 @@ class BartphoTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/beit/configuration_beit.py b/src/transformers/models/beit/configuration_beit.py index 75303ad01cd..4bea72b7bd0 100644 --- a/src/transformers/models/beit/configuration_beit.py +++ b/src/transformers/models/beit/configuration_beit.py @@ -76,7 +76,7 @@ class BeitConfig(BackboneConfigMixin, PretrainedConfig): use_mean_pooling (`bool`, *optional*, defaults to `True`): Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the CLS token, before applying the classification head. - pool_scales (`Tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): + pool_scales (`tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): Pooling scales used in Pooling Pyramid Module applied on the last feature map. use_auxiliary_head (`bool`, *optional*, defaults to `True`): Whether to use an auxiliary head during training. @@ -90,12 +90,12 @@ class BeitConfig(BackboneConfigMixin, PretrainedConfig): Whether to concatenate the output of the auxiliary head with the input before the classification layer. semantic_loss_ignore_index (`int`, *optional*, defaults to 255): The index that is ignored by the loss function of the semantic segmentation model. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/beit/image_processing_beit.py b/src/transformers/models/beit/image_processing_beit.py index a83cf10aad7..95e8ec54f19 100644 --- a/src/transformers/models/beit/image_processing_beit.py +++ b/src/transformers/models/beit/image_processing_beit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Beit.""" -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -64,7 +64,7 @@ class BeitImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): Size of the output image after resizing. Can be overridden by the `size` parameter in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): @@ -74,7 +74,7 @@ class BeitImageProcessor(BaseImageProcessor): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size when applying center-cropping. Only has an effect if `do_center_crop` is set to `True`. Can be overridden by the `crop_size` parameter in the `preprocess` method. rescale_factor (`int` or `float`, *optional*, defaults to `1/255`): @@ -86,10 +86,10 @@ class BeitImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): The mean to use if normalizing the image. This is a float or list of floats of length of the number of channels of the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): The standard deviation to use if normalizing the image. This is a float or list of floats of length of the number of channels of the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_reduce_labels (`bool`, *optional*, defaults to `False`): @@ -106,15 +106,15 @@ class BeitImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, rescale_factor: Union[int, float] = 1 / 255, do_rescale: bool = True, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_reduce_labels: bool = False, **kwargs, ) -> None: @@ -136,7 +136,7 @@ class BeitImageProcessor(BaseImageProcessor): self.do_reduce_labels = do_reduce_labels @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to save support of deprecated `reduce_labels` in old configs """ @@ -148,7 +148,7 @@ class BeitImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -160,7 +160,7 @@ class BeitImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PIL.Image.BICUBIC`): Resampling filter to use when resiizing the image. @@ -194,15 +194,15 @@ class BeitImageProcessor(BaseImageProcessor): image: ImageInput, do_reduce_labels: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_reduce_labels: @@ -226,15 +226,15 @@ class BeitImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -271,10 +271,10 @@ class BeitImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_reduce_labels: Optional[bool] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): @@ -320,15 +320,15 @@ class BeitImageProcessor(BaseImageProcessor): images: ImageInput, segmentation_maps: Optional[ImageInput] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_reduce_labels: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -346,14 +346,14 @@ class BeitImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after center crop. If one edge the image is smaller than `crop_size`, it will be padded with zeros and then cropped do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -362,9 +362,9 @@ class BeitImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 @@ -470,19 +470,19 @@ class BeitImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`BeitForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`BeitForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/beit/image_processing_beit_fast.py b/src/transformers/models/beit/image_processing_beit_fast.py index ce3a9707b85..6a4077008da 100644 --- a/src/transformers/models/beit/image_processing_beit_fast.py +++ b/src/transformers/models/beit/image_processing_beit_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for Beit.""" -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch from torchvision.transforms import functional as F @@ -72,7 +72,7 @@ class BeitImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to save support of deprecated `reduce_labels` in old configs """ @@ -240,19 +240,19 @@ class BeitImageProcessorFast(BaseImageProcessorFast): return BatchFeature(data=data) - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`BeitForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`BeitForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/beit/modeling_beit.py b/src/transformers/models/beit/modeling_beit.py index 69d9e991a58..70a6c78e065 100755 --- a/src/transformers/models/beit/modeling_beit.py +++ b/src/transformers/models/beit/modeling_beit.py @@ -18,7 +18,7 @@ import collections.abc import math import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -291,8 +291,8 @@ class BeitSelfAttention(nn.Module): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -346,8 +346,8 @@ class BeitSdpaSelfAttention(BeitSelfAttention): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: if output_attentions or head_mask is not None: logger.warning_once( "`BeitSdpaSelfAttention` is used but `torch.nn.functional.scaled_dot_product_attention` does not " @@ -456,8 +456,8 @@ class BeitAttention(nn.Module): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: self_outputs = self.attention( hidden_states, head_mask, output_attentions, relative_position_bias, interpolate_pos_encoding, resolution ) @@ -525,8 +525,8 @@ class BeitLayer(nn.Module): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in BEiT, layernorm is applied before self-attention head_mask, @@ -573,7 +573,7 @@ class BeitRelativePositionBias(nn.Module): # cls to token & token 2 cls & cls to cls @compile_compatible_method_lru_cache(maxsize=10) - def generate_relative_position_index(self, window_size: Tuple[int, int]) -> torch.Tensor: + def generate_relative_position_index(self, window_size: tuple[int, int]) -> torch.Tensor: """ This method creates the relative position index, modified to support arbitrary window sizes, as introduced in [MiDaS v3.1](https://huggingface.co/papers/2307.14460). @@ -674,7 +674,7 @@ class BeitEncoder(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int, int]] = None, + resolution: Optional[tuple[int, int]] = None, return_dict: bool = True, ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None @@ -1073,10 +1073,10 @@ class BeitConvModule(nn.Module): self, in_channels: int, out_channels: int, - kernel_size: Union[int, Tuple[int, int]], - padding: Union[int, Tuple[int, int], str] = 0, + kernel_size: Union[int, tuple[int, int]], + padding: Union[int, tuple[int, int], str] = 0, bias: bool = False, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, ) -> None: super().__init__() self.conv = nn.Conv2d( @@ -1129,7 +1129,7 @@ class BeitPyramidPoolingModule(nn.Module): Based on OpenMMLab's implementation, found in https://github.com/open-mmlab/mmsegmentation. """ - def __init__(self, pool_scales: Tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: + def __init__(self, pool_scales: tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: super().__init__() self.pool_scales = pool_scales self.align_corners = align_corners @@ -1141,7 +1141,7 @@ class BeitPyramidPoolingModule(nn.Module): self.blocks.append(block) self.add_module(str(i), block) - def forward(self, x: torch.Tensor) -> List[torch.Tensor]: + def forward(self, x: torch.Tensor) -> list[torch.Tensor]: ppm_outs = [] for ppm in self.blocks: ppm_out = ppm(x) @@ -1253,7 +1253,7 @@ class BeitFCNHead(nn.Module): """ def __init__( - self, config: BeitConfig, in_index: int = 2, kernel_size: int = 3, dilation: Union[int, Tuple[int, int]] = 1 + self, config: BeitConfig, in_index: int = 2, kernel_size: int = 3, dilation: Union[int, tuple[int, int]] = 1 ) -> None: super().__init__() self.in_channels = config.hidden_size diff --git a/src/transformers/models/beit/modeling_flax_beit.py b/src/transformers/models/beit/modeling_flax_beit.py index b51ff9fd094..c80deace6b3 100644 --- a/src/transformers/models/beit/modeling_flax_beit.py +++ b/src/transformers/models/beit/modeling_flax_beit.py @@ -14,7 +14,7 @@ # limitations under the License. -from typing import Callable, List, Optional, Tuple +from typing import Callable, Optional import flax import flax.linen as nn @@ -116,7 +116,7 @@ BEIT_INPUTS_DOCSTRING = r""" """ -def relative_position_index_init(window_size: Tuple[int, int]) -> jnp.ndarray: +def relative_position_index_init(window_size: tuple[int, int]) -> jnp.ndarray: """ get pair-wise relative position index for each token inside the window """ @@ -240,7 +240,7 @@ class FlaxBeitEmbeddings(nn.Module): class FlaxBeitRelativePositionBias(nn.Module): config: BeitConfig - window_size: Tuple[int, int] + window_size: tuple[int, int] dtype: jnp.dtype = jnp.float32 # the dtype of the computation def setup(self): @@ -263,7 +263,7 @@ class FlaxBeitRelativePositionBias(nn.Module): class FlaxBeitSelfAttention(nn.Module): config: BeitConfig - window_size: Tuple[int, int] + window_size: tuple[int, int] dtype: jnp.dtype = jnp.float32 # the dtype of the computation def setup(self): @@ -366,7 +366,7 @@ class FlaxBeitSelfOutput(nn.Module): class FlaxBeitAttention(nn.Module): config: BeitConfig - window_size: Tuple[int, int] + window_size: tuple[int, int] dtype: jnp.dtype = jnp.float32 def setup(self): @@ -430,7 +430,7 @@ class FlaxBeitOutput(nn.Module): class FlaxBeitLayer(nn.Module): config: BeitConfig - window_size: Tuple[int, int] + window_size: tuple[int, int] drop_path_rate: float dtype: jnp.dtype = jnp.float32 # the dtype of the computation @@ -491,8 +491,8 @@ class FlaxBeitLayer(nn.Module): class FlaxBeitLayerCollection(nn.Module): config: BeitConfig - window_size: Tuple[int, int] - drop_path_rates: List[float] + window_size: tuple[int, int] + drop_path_rates: list[float] relative_position_bias: Callable[[], jnp.ndarray] dtype: jnp.dtype = jnp.float32 # the dtype of the computation @@ -546,7 +546,7 @@ class FlaxBeitLayerCollection(nn.Module): class FlaxBeitEncoder(nn.Module): config: BeitConfig - window_size: Tuple[int, int] + window_size: tuple[int, int] dtype: jnp.dtype = jnp.float32 # the dtype of the computation def setup(self): @@ -609,7 +609,7 @@ class FlaxBeitPreTrainedModel(FlaxPreTrainedModel): input_shape = (1, config.image_size, config.image_size, config.num_channels) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors pixel_values = jnp.zeros(input_shape, dtype=self.dtype) diff --git a/src/transformers/models/bert/modeling_bert.py b/src/transformers/models/bert/modeling_bert.py index f5fc09ce23a..12080dfff6f 100755 --- a/src/transformers/models/bert/modeling_bert.py +++ b/src/transformers/models/bert/modeling_bert.py @@ -19,7 +19,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -226,9 +226,9 @@ class BertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -335,9 +335,9 @@ class BertSdpaSelfAttention(BertSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -476,9 +476,9 @@ class BertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -544,9 +544,9 @@ class BertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -622,12 +622,12 @@ class BertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -845,8 +845,8 @@ class BertForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -908,12 +908,12 @@ class BertModel(BertPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1080,7 +1080,7 @@ class BertForPreTraining(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BertForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], BertForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1185,13 +1185,13 @@ class BertLMHeadModel(BertPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **loss_kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1288,7 +1288,7 @@ class BertForMaskedLM(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1385,7 +1385,7 @@ class BertForNextSentencePrediction(BertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], NextSentencePredictorOutput]: + ) -> Union[tuple[torch.Tensor], NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -1491,7 +1491,7 @@ class BertForSequenceClassification(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1579,7 +1579,7 @@ class BertForMultipleChoice(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1687,7 +1687,7 @@ class BertForTokenClassification(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1754,7 +1754,7 @@ class BertForQuestionAnswering(BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.bert( diff --git a/src/transformers/models/bert/modeling_flax_bert.py b/src/transformers/models/bert/modeling_flax_bert.py index 48b72193fa2..37828642eb4 100644 --- a/src/transformers/models/bert/modeling_flax_bert.py +++ b/src/transformers/models/bert/modeling_flax_bert.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax import flax.linen as nn @@ -84,8 +84,8 @@ class FlaxBertForPreTrainingOutput(ModelOutput): prediction_logits: jnp.ndarray = None seq_relationship_logits: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None BERT_START_DOCSTRING = r""" @@ -770,7 +770,7 @@ class FlaxBertPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BertConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -792,7 +792,7 @@ class FlaxBertPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.zeros_like(input_ids) diff --git a/src/transformers/models/bert/modeling_tf_bert.py b/src/transformers/models/bert/modeling_tf_bert.py index ba73faf23cf..d4f0b47e24e 100644 --- a/src/transformers/models/bert/modeling_tf_bert.py +++ b/src/transformers/models/bert/modeling_tf_bert.py @@ -20,7 +20,7 @@ from __future__ import annotations import math import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -243,10 +243,10 @@ class TFBertSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -379,10 +379,10 @@ class TFBertAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -493,10 +493,10 @@ class TFBertLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -588,13 +588,13 @@ class TFBertEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -749,7 +749,7 @@ class TFBertLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -851,13 +851,13 @@ class TFBertMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1050,8 +1050,8 @@ class TFBertForPreTrainingOutput(ModelOutput): loss: tf.Tensor | None = None prediction_logits: Optional[tf.Tensor] = None seq_relationship_logits: Optional[tf.Tensor] = None - hidden_states: Optional[Union[Tuple[tf.Tensor], tf.Tensor]] = None - attentions: Optional[Union[Tuple[tf.Tensor], tf.Tensor]] = None + hidden_states: Optional[Union[tuple[tf.Tensor], tf.Tensor]] = None + attentions: Optional[Union[tuple[tf.Tensor], tf.Tensor]] = None BERT_START_DOCSTRING = r""" @@ -1098,7 +1098,7 @@ BERT_START_DOCSTRING = r""" BERT_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1179,13 +1179,13 @@ class TFBertModel(TFBertPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1197,7 +1197,7 @@ class TFBertModel(TFBertPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1279,7 +1279,7 @@ class TFBertForPreTraining(TFBertPreTrainedModel, TFBertPreTrainingLoss): labels: np.ndarray | tf.Tensor | None = None, next_sentence_label: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFBertForPreTrainingOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBertForPreTrainingOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1291,7 +1291,7 @@ class TFBertForPreTraining(TFBertPreTrainedModel, TFBertPreTrainingLoss): - 0 indicates sequence B is a continuation of sequence A, - 1 indicates sequence B is a random sequence. - kwargs (`Dict[str, any]`, *optional*, defaults to `{}`): + kwargs (`dict[str, any]`, *optional*, defaults to `{}`): Used to hide legacy arguments that have been deprecated. Return: @@ -1410,7 +1410,7 @@ class TFBertForMaskedLM(TFBertPreTrainedModel, TFMaskedLanguageModelingLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1509,7 +1509,7 @@ class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1517,7 +1517,7 @@ class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss): labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1529,7 +1529,7 @@ class TFBertLMHeadModel(TFBertPreTrainedModel, TFCausalLanguageModelingLoss): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1622,7 +1622,7 @@ class TFBertForNextSentencePrediction(TFBertPreTrainedModel, TFNextSentencePredi return_dict: Optional[bool] = None, next_sentence_label: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFNextSentencePredictorOutput, Tuple[tf.Tensor]]: + ) -> Union[TFNextSentencePredictorOutput, tuple[tf.Tensor]]: r""" Return: @@ -1736,7 +1736,7 @@ class TFBertForSequenceClassification(TFBertPreTrainedModel, TFSequenceClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1825,7 +1825,7 @@ class TFBertForMultipleChoice(TFBertPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1951,7 +1951,7 @@ class TFBertForTokenClassification(TFBertPreTrainedModel, TFTokenClassificationL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -2051,7 +2051,7 @@ class TFBertForQuestionAnswering(TFBertPreTrainedModel, TFQuestionAnsweringLoss) start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/bert/tokenization_bert.py b/src/transformers/models/bert/tokenization_bert.py index 7789b6cc5a5..160fd99e993 100644 --- a/src/transformers/models/bert/tokenization_bert.py +++ b/src/transformers/models/bert/tokenization_bert.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -184,8 +184,8 @@ class BertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -209,8 +209,8 @@ class BertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -236,7 +236,7 @@ class BertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/bert/tokenization_bert_fast.py b/src/transformers/models/bert/tokenization_bert_fast.py index 52278ba8ccd..2cdc6129881 100644 --- a/src/transformers/models/bert/tokenization_bert_fast.py +++ b/src/transformers/models/bert/tokenization_bert_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization classes for Bert.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -138,7 +138,7 @@ class BertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/bert/tokenization_bert_tf.py b/src/transformers/models/bert/tokenization_bert_tf.py index e98fe340ee8..c8fca52c4cb 100644 --- a/src/transformers/models/bert/tokenization_bert_tf.py +++ b/src/transformers/models/bert/tokenization_bert_tf.py @@ -1,5 +1,5 @@ import os -from typing import List, Optional, Union +from typing import Optional, Union import tensorflow as tf from tensorflow_text import BertTokenizer as BertTokenizerLayer @@ -58,7 +58,7 @@ class TFBertTokenizer(keras.layers.Layer): def __init__( self, - vocab_list: List, + vocab_list: list, do_lower_case: bool, cls_token_id: Optional[int] = None, sep_token_id: Optional[int] = None, diff --git a/src/transformers/models/bert_generation/modeling_bert_generation.py b/src/transformers/models/bert_generation/modeling_bert_generation.py index 308a8f31ef0..959a3cce077 100755 --- a/src/transformers/models/bert_generation/modeling_bert_generation.py +++ b/src/transformers/models/bert_generation/modeling_bert_generation.py @@ -15,7 +15,7 @@ """PyTorch BERT model specific for generation.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -91,9 +91,9 @@ class BertGenerationSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -226,9 +226,9 @@ class BertGenerationAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -297,9 +297,9 @@ class BertGenerationLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -376,12 +376,12 @@ class BertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -658,13 +658,13 @@ class BertGenerationEncoder(BertGenerationPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -808,13 +808,13 @@ class BertGenerationDecoder(BertGenerationPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in diff --git a/src/transformers/models/bert_generation/tokenization_bert_generation.py b/src/transformers/models/bert_generation/tokenization_bert_generation.py index 727727d4a11..baeba9e4b38 100644 --- a/src/transformers/models/bert_generation/tokenization_bert_generation.py +++ b/src/transformers/models/bert_generation/tokenization_bert_generation.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -73,7 +73,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer): """ vocab_files_names = VOCAB_FILES_NAMES - prefix_tokens: List[int] = [] + prefix_tokens: list[int] = [] model_input_names = ["input_ids", "attention_mask"] def __init__( @@ -84,7 +84,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer): unk_token="", pad_token="", sep_token="<::::>", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -129,7 +129,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" return self.sp_model.encode(text, out_type=str) @@ -156,7 +156,7 @@ class BertGenerationTokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string.strip() - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/bert_japanese/tokenization_bert_japanese.py b/src/transformers/models/bert_japanese/tokenization_bert_japanese.py index e22a3fff84b..decf3dfa5c3 100644 --- a/src/transformers/models/bert_japanese/tokenization_bert_japanese.py +++ b/src/transformers/models/bert_japanese/tokenization_bert_japanese.py @@ -18,7 +18,7 @@ import collections import copy import os import unicodedata -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import is_sentencepiece_available, is_sudachi_projection_available, logging @@ -256,8 +256,8 @@ class BertJapaneseTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -282,8 +282,8 @@ class BertJapaneseTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -309,7 +309,7 @@ class BertJapaneseTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if os.path.isdir(save_directory): if self.subword_tokenizer_type == "sentencepiece": vocab_file = os.path.join( @@ -892,7 +892,7 @@ class SentencepieceTokenizer: do_lower_case=False, remove_space=True, keep_accents=True, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, ): self.vocab = vocab self.unk_token = unk_token diff --git a/src/transformers/models/bertweet/tokenization_bertweet.py b/src/transformers/models/bertweet/tokenization_bertweet.py index 499238e5955..3ce1a3182bf 100644 --- a/src/transformers/models/bertweet/tokenization_bertweet.py +++ b/src/transformers/models/bertweet/tokenization_bertweet.py @@ -19,7 +19,7 @@ import html import os import re from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional import regex @@ -165,8 +165,8 @@ class BertweetTokenizer(PreTrainedTokenizer): ) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERTweet sequence has the following format: @@ -175,13 +175,13 @@ class BertweetTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -191,22 +191,22 @@ class BertweetTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -219,20 +219,20 @@ class BertweetTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. BERTweet does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] @@ -370,7 +370,7 @@ class BertweetTokenizer(PreTrainedTokenizer): out_string = " ".join(tokens).replace("@@ ", "").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/big_bird/modeling_big_bird.py b/src/transformers/models/big_bird/modeling_big_bird.py index a6f6fbf1f22..e06c8f87d5f 100755 --- a/src/transformers/models/big_bird/modeling_big_bird.py +++ b/src/transformers/models/big_bird/modeling_big_bird.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -1572,7 +1572,7 @@ class BigBirdEncoder(nn.Module): to_mask=None, blocked_encoder_mask=None, return_dict=True, - ) -> Union[BaseModelOutputWithPastAndCrossAttentions, Tuple]: + ) -> Union[BaseModelOutputWithPastAndCrossAttentions, tuple]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -1788,8 +1788,8 @@ class BigBirdForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -1823,8 +1823,8 @@ class BigBirdForQuestionAnsweringModelOutput(ModelOutput): start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -1900,13 +1900,13 @@ class BigBirdModel(BigBirdPreTrainedModel): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[BaseModelOutputWithPoolingAndCrossAttentions, Tuple[torch.FloatTensor]]: + ) -> Union[BaseModelOutputWithPoolingAndCrossAttentions, tuple[torch.FloatTensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -2181,7 +2181,7 @@ class BigBirdForPreTraining(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BigBirdForPreTrainingOutput, Tuple[torch.FloatTensor]]: + ) -> Union[BigBirdForPreTrainingOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2290,7 +2290,7 @@ class BigBirdForMaskedLM(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MaskedLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2427,14 +2427,14 @@ class BigBirdForCausalLM(BigBirdPreTrainedModel, GenerationMixin): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[CausalLMOutputWithCrossAttentions, Tuple[torch.FloatTensor]]: + ) -> Union[CausalLMOutputWithCrossAttentions, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -2549,7 +2549,7 @@ class BigBirdForSequenceClassification(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -2668,7 +2668,7 @@ class BigBirdForMultipleChoice(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MultipleChoiceModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MultipleChoiceModelOutput, tuple[torch.FloatTensor]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -2776,7 +2776,7 @@ class BigBirdForTokenClassification(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[TokenClassifierOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -2869,7 +2869,7 @@ class BigBirdForQuestionAnswering(BigBirdPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BigBirdForQuestionAnsweringModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[BigBirdForQuestionAnsweringModelOutput, tuple[torch.FloatTensor]]: r""" question_lengths (`torch.LongTensor` of shape `(batch_size, 1)`, *optional*): The lengths of the questions in the batch. diff --git a/src/transformers/models/big_bird/modeling_flax_big_bird.py b/src/transformers/models/big_bird/modeling_flax_big_bird.py index 18913e930d5..11dcb30f3d4 100644 --- a/src/transformers/models/big_bird/modeling_flax_big_bird.py +++ b/src/transformers/models/big_bird/modeling_flax_big_bird.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax import flax.linen as nn @@ -81,8 +81,8 @@ class FlaxBigBirdForPreTrainingOutput(ModelOutput): prediction_logits: jnp.ndarray = None seq_relationship_logits: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -113,8 +113,8 @@ class FlaxBigBirdForQuestionAnsweringModelOutput(ModelOutput): start_logits: jnp.ndarray = None end_logits: jnp.ndarray = None pooled_output: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None BIG_BIRD_START_DOCSTRING = r""" @@ -1647,7 +1647,7 @@ class FlaxBigBirdPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.zeros_like(input_ids) diff --git a/src/transformers/models/big_bird/tokenization_big_bird.py b/src/transformers/models/big_bird/tokenization_big_bird.py index b498f57563b..56680972a63 100644 --- a/src/transformers/models/big_bird/tokenization_big_bird.py +++ b/src/transformers/models/big_bird/tokenization_big_bird.py @@ -17,7 +17,7 @@ import os import re from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -81,7 +81,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] + prefix_tokens: list[int] = [] def __init__( self, @@ -93,7 +93,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): sep_token="[SEP]", mask_token="[MASK]", cls_token="[CLS]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: bos_token = AddedToken(bos_token, lstrip=False, rstrip=False) if isinstance(bos_token, str) else bos_token @@ -149,7 +149,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" return self.sp_model.encode(text, out_type=str) @@ -184,7 +184,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, spaces_between_special_tokens: bool = True, @@ -230,7 +230,7 @@ class BigBirdTokenizer(PreTrainedTokenizer): else: return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -248,8 +248,8 @@ class BigBirdTokenizer(PreTrainedTokenizer): return (out_vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Big Bird sequence has the following format: @@ -273,8 +273,8 @@ class BigBirdTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. diff --git a/src/transformers/models/big_bird/tokenization_big_bird_fast.py b/src/transformers/models/big_bird/tokenization_big_bird_fast.py index 1af9be12b34..6148585a40b 100644 --- a/src/transformers/models/big_bird/tokenization_big_bird_fast.py +++ b/src/transformers/models/big_bird/tokenization_big_bird_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -79,7 +79,7 @@ class BigBirdTokenizerFast(PreTrainedTokenizerFast): vocab_files_names = VOCAB_FILES_NAMES slow_tokenizer_class = BigBirdTokenizer model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] + prefix_tokens: list[int] = [] def __init__( self, @@ -120,8 +120,8 @@ class BigBirdTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An BigBird sequence has the following format: @@ -145,8 +145,8 @@ class BigBirdTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -175,7 +175,7 @@ class BigBirdTokenizerFast(PreTrainedTokenizerFast): return [1] + ([0] * len(token_ids_0)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/bigbird_pegasus/convert_bigbird_pegasus_tf_to_pytorch.py b/src/transformers/models/bigbird_pegasus/convert_bigbird_pegasus_tf_to_pytorch.py index e17369e4804..686277b8e84 100644 --- a/src/transformers/models/bigbird_pegasus/convert_bigbird_pegasus_tf_to_pytorch.py +++ b/src/transformers/models/bigbird_pegasus/convert_bigbird_pegasus_tf_to_pytorch.py @@ -14,7 +14,6 @@ # limitations under the License. import argparse -from typing import Dict import tensorflow as tf import torch @@ -142,7 +141,7 @@ def convert_bigbird_pegasus(tf_weights: dict, config_update: dict) -> BigBirdPeg return torch_model -def get_tf_weights_as_numpy(path) -> Dict: +def get_tf_weights_as_numpy(path) -> dict: init_vars = tf.train.list_variables(path) tf_weights = {} ignore_name = ["global_step"] diff --git a/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py b/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py index ce26a24f84e..bc72d16bf54 100755 --- a/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py +++ b/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py @@ -16,7 +16,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -1261,7 +1261,7 @@ class BigBirdPegasusDecoderAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -2120,7 +2120,7 @@ class BigBirdPegasusDecoder(BigBirdPegasusPreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -2410,8 +2410,8 @@ class BigBirdPegasusModel(BigBirdPegasusPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -2419,7 +2419,7 @@ class BigBirdPegasusModel(BigBirdPegasusPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqModelOutput]: + ) -> Union[tuple, Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Provide for translation and summarization training. By default, the model will create this tensor by @@ -2572,8 +2572,8 @@ class BigBirdPegasusForConditionalGeneration(BigBirdPegasusPreTrainedModel, Gene head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2582,7 +2582,7 @@ class BigBirdPegasusForConditionalGeneration(BigBirdPegasusPreTrainedModel, Gene output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Provide for translation and summarization training. By default, the model will create this tensor by @@ -2730,7 +2730,7 @@ class BigBirdPegasusForSequenceClassification(BigBirdPegasusPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2739,7 +2739,7 @@ class BigBirdPegasusForSequenceClassification(BigBirdPegasusPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Provide for translation and summarization training. By default, the model will create this tensor by @@ -2863,7 +2863,7 @@ class BigBirdPegasusForQuestionAnswering(BigBirdPegasusPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -2873,7 +2873,7 @@ class BigBirdPegasusForQuestionAnswering(BigBirdPegasusPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple, Seq2SeqQuestionAnsweringModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Provide for translation and summarization training. By default, the model will create this tensor by @@ -3015,7 +3015,7 @@ class BigBirdPegasusForCausalLM(BigBirdPegasusPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -3023,7 +3023,7 @@ class BigBirdPegasusForCausalLM(BigBirdPegasusPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/biogpt/modeling_biogpt.py b/src/transformers/models/biogpt/modeling_biogpt.py index 06447e67ab2..a1fba008841 100755 --- a/src/transformers/models/biogpt/modeling_biogpt.py +++ b/src/transformers/models/biogpt/modeling_biogpt.py @@ -21,7 +21,7 @@ import math from functools import partial -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -176,7 +176,7 @@ class BioGptAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -283,7 +283,7 @@ class BioGptDecoderLayer(nn.Module): position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -538,7 +538,7 @@ class BioGptModel(BioGptPreTrainedModel): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, @@ -546,7 +546,7 @@ class BioGptModel(BioGptPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -737,7 +737,7 @@ class BioGptForCausalLM(BioGptPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, @@ -746,7 +746,7 @@ class BioGptForCausalLM(BioGptPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -828,7 +828,7 @@ class BioGptForTokenClassification(BioGptPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -837,7 +837,7 @@ class BioGptForTokenClassification(BioGptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -920,7 +920,7 @@ class BioGptForSequenceClassification(BioGptPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -929,7 +929,7 @@ class BioGptForSequenceClassification(BioGptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/biogpt/modular_biogpt.py b/src/transformers/models/biogpt/modular_biogpt.py index d7ba7bf617b..d639f44ffec 100644 --- a/src/transformers/models/biogpt/modular_biogpt.py +++ b/src/transformers/models/biogpt/modular_biogpt.py @@ -16,7 +16,7 @@ import math from functools import partial -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -110,7 +110,7 @@ class BioGptDecoderLayer(BartDecoderLayer): position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -365,7 +365,7 @@ class BioGptModel(BioGptPreTrainedModel): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, @@ -373,7 +373,7 @@ class BioGptModel(BioGptPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -564,7 +564,7 @@ class BioGptForCausalLM(BioGptPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, @@ -573,7 +573,7 @@ class BioGptForCausalLM(BioGptPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -655,7 +655,7 @@ class BioGptForTokenClassification(BioGptPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -664,7 +664,7 @@ class BioGptForTokenClassification(BioGptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -747,7 +747,7 @@ class BioGptForSequenceClassification(BioGptPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -756,7 +756,7 @@ class BioGptForSequenceClassification(BioGptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/biogpt/tokenization_biogpt.py b/src/transformers/models/biogpt/tokenization_biogpt.py index b1ca1d45f1c..f84403ca7dd 100644 --- a/src/transformers/models/biogpt/tokenization_biogpt.py +++ b/src/transformers/models/biogpt/tokenization_biogpt.py @@ -16,7 +16,7 @@ import json import os -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -231,8 +231,8 @@ class BioGptTokenizer(PreTrainedTokenizer): return text def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BioGPT sequence has the following format: @@ -255,8 +255,8 @@ class BioGptTokenizer(PreTrainedTokenizer): return sep + token_ids_0 + sep + token_ids_1 def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -281,7 +281,7 @@ class BioGptTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) return [1] + ([0] * len(token_ids_0)) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/bit/configuration_bit.py b/src/transformers/models/bit/configuration_bit.py index 238749f1fbe..2b1f24fa068 100644 --- a/src/transformers/models/bit/configuration_bit.py +++ b/src/transformers/models/bit/configuration_bit.py @@ -37,9 +37,9 @@ class BitConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"preactivation"`): The layer to use, it can be either `"preactivation"` or `"bottleneck"`. @@ -58,12 +58,12 @@ class BitConfig(BackboneConfigMixin, PretrainedConfig): The output stride of the model. width_factor (`int`, *optional*, defaults to 1): The width factor for the model. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/bit/image_processing_bit.py b/src/transformers/models/bit/image_processing_bit.py index aa2eb379551..0bcdb7075a2 100644 --- a/src/transformers/models/bit/image_processing_bit.py +++ b/src/transformers/models/bit/image_processing_bit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for BiT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -56,7 +56,7 @@ class BitImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -65,7 +65,7 @@ class BitImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -76,10 +76,10 @@ class BitImageProcessor(BaseImageProcessor): method. do_normalize: Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -92,15 +92,15 @@ class BitImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -126,7 +126,7 @@ class BitImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -139,7 +139,7 @@ class BitImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -177,15 +177,15 @@ class BitImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -200,7 +200,7 @@ class BitImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -208,7 +208,7 @@ class BitImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -216,9 +216,9 @@ class BitImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/bit/modeling_bit.py b/src/transformers/models/bit/modeling_bit.py index 8adb812820e..d95e3537819 100644 --- a/src/transformers/models/bit/modeling_bit.py +++ b/src/transformers/models/bit/modeling_bit.py @@ -16,7 +16,7 @@ import collections import math -from typing import Optional, Tuple +from typing import Optional import numpy as np import torch @@ -40,7 +40,7 @@ from .configuration_bit import BitConfig logger = logging.get_logger(__name__) -def get_padding_value(padding=None, kernel_size=7, stride=1, dilation=1) -> Tuple[Tuple, bool]: +def get_padding_value(padding=None, kernel_size=7, stride=1, dilation=1) -> tuple[tuple, bool]: r""" Utility function to get the tuple padding value given the kernel_size and padding. diff --git a/src/transformers/models/bitnet/modeling_bitnet.py b/src/transformers/models/bitnet/modeling_bitnet.py index 661a3c9bb60..f526802bfca 100644 --- a/src/transformers/models/bitnet/modeling_bitnet.py +++ b/src/transformers/models/bitnet/modeling_bitnet.py @@ -18,7 +18,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -181,12 +181,12 @@ class BitNetAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -244,9 +244,9 @@ class BitNetDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/bitnet/modular_bitnet.py b/src/transformers/models/bitnet/modular_bitnet.py index c57b7217f1d..952d23fc4c9 100644 --- a/src/transformers/models/bitnet/modular_bitnet.py +++ b/src/transformers/models/bitnet/modular_bitnet.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and """PyTorch BitNet model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch @@ -61,12 +61,12 @@ class BitNetAttention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) diff --git a/src/transformers/models/blenderbot/modeling_blenderbot.py b/src/transformers/models/blenderbot/modeling_blenderbot.py index b72a333edbc..0d699d01b59 100755 --- a/src/transformers/models/blenderbot/modeling_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_blenderbot.py @@ -18,7 +18,7 @@ import copy import math import os import warnings -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -197,7 +197,7 @@ class BlenderbotAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1209,8 +1209,8 @@ class BlenderbotModel(BlenderbotPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple, BaseModelOutput]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[Union[tuple, BaseModelOutput]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1218,7 +1218,7 @@ class BlenderbotModel(BlenderbotPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1386,8 +1386,8 @@ class BlenderbotForConditionalGeneration(BlenderbotPreTrainedModel, GenerationMi head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple, BaseModelOutput]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[Union[tuple, BaseModelOutput]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1396,7 +1396,7 @@ class BlenderbotForConditionalGeneration(BlenderbotPreTrainedModel, GenerationMi output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1577,7 +1577,7 @@ class BlenderbotForCausalLM(BlenderbotPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1585,7 +1585,7 @@ class BlenderbotForCausalLM(BlenderbotPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/blenderbot/modeling_flax_blenderbot.py b/src/transformers/models/blenderbot/modeling_flax_blenderbot.py index a977083e697..8b147211881 100644 --- a/src/transformers/models/blenderbot/modeling_flax_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_flax_blenderbot.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -189,7 +189,7 @@ BLENDERBOT_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -297,7 +297,7 @@ class FlaxBlenderbotAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -418,7 +418,7 @@ class FlaxBlenderbotEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -543,7 +543,7 @@ class FlaxBlenderbotDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) @@ -883,7 +883,7 @@ class FlaxBlenderbotPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BlenderbotConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -892,7 +892,7 @@ class FlaxBlenderbotPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") # make sure initialization pass will work for FlaxBlenderbotForSequenceClassificationModule diff --git a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py index f4eaec2ceeb..86e576c9828 100644 --- a/src/transformers/models/blenderbot/modeling_tf_blenderbot.py +++ b/src/transformers/models/blenderbot/modeling_tf_blenderbot.py @@ -19,7 +19,7 @@ from __future__ import annotations import os import random import warnings -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -176,11 +176,11 @@ class TFBlenderbotAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -424,9 +424,9 @@ class TFBlenderbotDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)* @@ -651,7 +651,7 @@ BLENDERBOT_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -943,7 +943,7 @@ class TFBlenderbotDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1128,7 +1128,7 @@ class TFBlenderbotMainLayer(keras.layers.Layer): head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, past_key_values=None, inputs_embeds=None, decoder_inputs_embeds=None, @@ -1262,8 +1262,8 @@ class TFBlenderbotModel(TFBlenderbotPreTrainedModel): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: List[tf.Tensor] | None = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: list[tf.Tensor] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1272,7 +1272,7 @@ class TFBlenderbotModel(TFBlenderbotPreTrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqModelOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqModelOutput]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1414,8 +1414,8 @@ class TFBlenderbotForConditionalGeneration(TFBlenderbotPreTrainedModel, TFCausal head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: List[tf.Tensor] | None = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: list[tf.Tensor] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1424,7 +1424,7 @@ class TFBlenderbotForConditionalGeneration(TFBlenderbotPreTrainedModel, TFCausal return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqLMOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqLMOutput]: r""" labels (`tf.tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/blenderbot/tokenization_blenderbot.py b/src/transformers/models/blenderbot/tokenization_blenderbot.py index 08b2a8c1283..02be9fd74ae 100644 --- a/src/transformers/models/blenderbot/tokenization_blenderbot.py +++ b/src/transformers/models/blenderbot/tokenization_blenderbot.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -302,7 +302,7 @@ class BlenderbotTokenizer(PreTrainedTokenizer): return text # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.save_vocabulary with Roberta->Blenderbot, RoBERTa->Blenderbot - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -333,22 +333,22 @@ class BlenderbotTokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.get_special_tokens_mask with Roberta->Blenderbot, RoBERTa->Blenderbot def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -361,20 +361,20 @@ class BlenderbotTokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.create_token_type_ids_from_sequences with Roberta->Blenderbot, RoBERTa->Blenderbot def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Blenderbot does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -390,19 +390,19 @@ class BlenderbotTokenizer(PreTrainedTokenizer): text = " " + text return (text, kwargs) - def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None): + def build_inputs_with_special_tokens(self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None): """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Blenderbot sequence has the following format: - single sequence: ` X ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Will be ignored Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ return token_ids_0 + [self.eos_token_id] diff --git a/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py b/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py index 8667fe76349..0b84200e02d 100644 --- a/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py +++ b/src/transformers/models/blenderbot/tokenization_blenderbot_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization class for Blenderbot.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -236,26 +236,26 @@ class BlenderbotTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.save_vocabulary with Roberta->Blenderbot, RoBERTa->Blenderbot - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) # Copied from transformers.models.roberta.tokenization_roberta_fast.RobertaTokenizerFast.create_token_type_ids_from_sequences with Roberta->Blenderbot, RoBERTa->Blenderbot def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Blenderbot does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -264,19 +264,19 @@ class BlenderbotTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None): + def build_inputs_with_special_tokens(self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None): """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Blenderbot sequence has the following format: - single sequence: ` X ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Will be ignored Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ return token_ids_0 + [self.eos_token_id] diff --git a/src/transformers/models/blenderbot_small/modeling_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_blenderbot_small.py index 31fcf9febb4..99666356d09 100755 --- a/src/transformers/models/blenderbot_small/modeling_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_blenderbot_small.py @@ -16,7 +16,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -181,7 +181,7 @@ class BlenderbotSmallAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -280,7 +280,7 @@ class BlenderbotSmallEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -370,7 +370,7 @@ class BlenderbotSmallDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1176,8 +1176,8 @@ class BlenderbotSmallModel(BlenderbotSmallPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple, BaseModelOutput]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[Union[tuple, BaseModelOutput]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1185,7 +1185,7 @@ class BlenderbotSmallModel(BlenderbotSmallPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1338,8 +1338,8 @@ class BlenderbotSmallForConditionalGeneration(BlenderbotSmallPreTrainedModel, Ge head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple, BaseModelOutput]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[Union[tuple, BaseModelOutput]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1348,7 +1348,7 @@ class BlenderbotSmallForConditionalGeneration(BlenderbotSmallPreTrainedModel, Ge output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1529,7 +1529,7 @@ class BlenderbotSmallForCausalLM(BlenderbotSmallPreTrainedModel, GenerationMixin encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1537,7 +1537,7 @@ class BlenderbotSmallForCausalLM(BlenderbotSmallPreTrainedModel, GenerationMixin output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/blenderbot_small/modeling_flax_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_flax_blenderbot_small.py index 5b845702ff5..ac30320bbdb 100644 --- a/src/transformers/models/blenderbot_small/modeling_flax_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_flax_blenderbot_small.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -200,7 +200,7 @@ BLENDERBOT_SMALL_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -308,7 +308,7 @@ class FlaxBlenderbotSmallAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -429,7 +429,7 @@ class FlaxBlenderbotSmallEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -555,7 +555,7 @@ class FlaxBlenderbotSmallDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states # Self Attention @@ -880,7 +880,7 @@ class FlaxBlenderbotSmallPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BlenderbotSmallConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -889,7 +889,7 @@ class FlaxBlenderbotSmallPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") # make sure initialization pass will work for FlaxBlenderbotSmallForSequenceClassificationModule diff --git a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py index b871d5bb820..459ef438c53 100644 --- a/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/modeling_tf_blenderbot_small.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -176,11 +176,11 @@ class TFBlenderbotSmallAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -424,9 +424,9 @@ class TFBlenderbotSmallDecoderLayer(keras.layers.Layer): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_value: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -655,7 +655,7 @@ BLENDERBOT_SMALL_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -947,7 +947,7 @@ class TFBlenderbotSmallDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1129,7 +1129,7 @@ class TFBlenderbotSmallMainLayer(keras.layers.Layer): head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, past_key_values=None, inputs_embeds=None, decoder_inputs_embeds=None, @@ -1247,8 +1247,8 @@ class TFBlenderbotSmallModel(TFBlenderbotSmallPreTrainedModel): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: List[tf.Tensor] | None = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: list[tf.Tensor] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1257,7 +1257,7 @@ class TFBlenderbotSmallModel(TFBlenderbotSmallPreTrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqModelOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqModelOutput]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1384,7 +1384,7 @@ class TFBlenderbotSmallForConditionalGeneration(TFBlenderbotSmallPreTrainedModel decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, encoder_outputs: Optional[TFBaseModelOutput] = None, - past_key_values: List[tf.Tensor] | None = None, + past_key_values: list[tf.Tensor] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1393,7 +1393,7 @@ class TFBlenderbotSmallForConditionalGeneration(TFBlenderbotSmallPreTrainedModel return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqLMOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqLMOutput]: r""" labels (`tf.tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py index be950f0dbe6..adb54025ce2 100644 --- a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py +++ b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small.py @@ -16,7 +16,7 @@ import json import os -from typing import Dict, List, Optional, Tuple +from typing import Optional import regex as re @@ -102,7 +102,7 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.encoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: return dict(self.encoder, **self.added_tokens_encoder) def bpe(self, token: str) -> str: @@ -165,7 +165,7 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): words.append(word) return " ".join(words) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Split a string into tokens using BPE.""" split_tokens = [] @@ -184,12 +184,12 @@ class BlenderbotSmallTokenizer(PreTrainedTokenizer): """Converts an index (integer) in a token (str) using the vocab.""" return self.decoder.get(index, self.unk_token) - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """Converts a sequence of tokens in a single string.""" out_string = " ".join(tokens).replace("@@ ", "").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py index ac98ce008ba..7d905dbbc5b 100644 --- a/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py +++ b/src/transformers/models/blenderbot_small/tokenization_blenderbot_small_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast tokenization class for BlenderbotSmall.""" -from typing import List, Optional +from typing import Optional from tokenizers import ByteLevelBPETokenizer @@ -77,20 +77,20 @@ class BlenderbotSmallTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. BlenderbotSmall does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/blip/image_processing_blip.py b/src/transformers/models/blip/image_processing_blip.py index ace61142ec8..4c3ec00d26f 100644 --- a/src/transformers/models/blip/image_processing_blip.py +++ b/src/transformers/models/blip/image_processing_blip.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for BLIP.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -66,11 +66,11 @@ class BlipImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -83,13 +83,13 @@ class BlipImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -111,7 +111,7 @@ class BlipImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -123,7 +123,7 @@ class BlipImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -161,13 +161,13 @@ class BlipImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -182,7 +182,7 @@ class BlipImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -195,9 +195,9 @@ class BlipImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. diff --git a/src/transformers/models/blip/modeling_blip.py b/src/transformers/models/blip/modeling_blip.py index 356f48eaf94..3967f8bce47 100644 --- a/src/transformers/models/blip/modeling_blip.py +++ b/src/transformers/models/blip/modeling_blip.py @@ -16,7 +16,7 @@ import warnings from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -76,12 +76,12 @@ class BlipForConditionalGenerationModelOutput(ModelOutput): heads. """ - loss: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + loss: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @property def decoder_logits(self): @@ -122,8 +122,8 @@ class BlipTextVisionModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -163,10 +163,10 @@ class BlipImageTextMatchingModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None vision_pooler_output: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - question_embeds: Optional[Tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + question_embeds: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -199,7 +199,7 @@ class BlipOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -350,7 +350,7 @@ class BlipAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -420,7 +420,7 @@ class BlipEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -515,7 +515,7 @@ class BlipEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -594,7 +594,7 @@ class BlipVisionModel(BlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -830,7 +830,7 @@ class BlipModel(BlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BlipOutput]: + ) -> Union[tuple, BlipOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. @@ -958,7 +958,7 @@ class BlipForConditionalGeneration(BlipPreTrainedModel, GenerationMixin): labels: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BlipForConditionalGenerationModelOutput]: + ) -> Union[tuple, BlipForConditionalGenerationModelOutput]: r""" Examples: @@ -1140,7 +1140,7 @@ class BlipForQuestionAnswering(BlipPreTrainedModel, GenerationMixin): labels: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BlipTextVisionModelOutput]: + ) -> Union[tuple, BlipTextVisionModelOutput]: r""" Examples: @@ -1381,7 +1381,7 @@ class BlipForImageTextRetrieval(BlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BlipTextVisionModelOutput]: + ) -> Union[tuple, BlipTextVisionModelOutput]: r""" use_itm_head (`bool`, *optional*, defaults to `True`): Whether or not to use the image-text matching head. diff --git a/src/transformers/models/blip/modeling_blip_text.py b/src/transformers/models/blip/modeling_blip_text.py index 7b19c20b2d1..151caef0b10 100644 --- a/src/transformers/models/blip/modeling_blip_text.py +++ b/src/transformers/models/blip/modeling_blip_text.py @@ -15,7 +15,7 @@ import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -148,9 +148,9 @@ class BlipTextSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -270,9 +270,9 @@ class BlipTextAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -338,9 +338,9 @@ class BlipTextLayer(GradientCheckpointingLayer): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -396,12 +396,12 @@ class BlipTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: if self.gradient_checkpointing and self.training: if use_cache: logger.warning( @@ -592,7 +592,7 @@ class BlipTextModel(BlipTextPreTrainedModel): self.encoder.layer[layer].attention.prune_heads(heads) def get_extended_attention_mask( - self, attention_mask: Tensor, input_shape: Tuple[int], device: device, is_decoder: bool + self, attention_mask: Tensor, input_shape: tuple[int], device: device, is_decoder: bool ) -> Tensor: """ Makes broadcastable attention and causal masks so that future and masked tokens are ignored. @@ -600,7 +600,7 @@ class BlipTextModel(BlipTextPreTrainedModel): Arguments: attention_mask (`torch.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. device (`torch.device`): The device of the input to the model. @@ -665,13 +665,13 @@ class BlipTextModel(BlipTextPreTrainedModel): encoder_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, is_decoder: Optional[bool] = False, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -827,7 +827,7 @@ class BlipTextLMHeadModel(BlipTextPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -835,7 +835,7 @@ class BlipTextLMHeadModel(BlipTextPreTrainedModel, GenerationMixin): return_logits: Optional[bool] = False, is_decoder: Optional[bool] = True, reduction: Optional[str] = "mean", - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is diff --git a/src/transformers/models/blip/modeling_tf_blip.py b/src/transformers/models/blip/modeling_tf_blip.py index 36e8a2da7a6..a0bf43892d0 100644 --- a/src/transformers/models/blip/modeling_tf_blip.py +++ b/src/transformers/models/blip/modeling_tf_blip.py @@ -18,7 +18,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import tensorflow as tf @@ -93,12 +93,12 @@ class TFBlipForConditionalGenerationModelOutput(ModelOutput): heads.` """ - loss: Tuple[tf.Tensor] | None = None - logits: Tuple[tf.Tensor] | None = None + loss: tuple[tf.Tensor] | None = None + logits: tuple[tf.Tensor] | None = None image_embeds: tf.Tensor | None = None last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @property def decoder_logits(self): @@ -139,8 +139,8 @@ class TFBlipTextVisionModelOutput(ModelOutput): loss: tf.Tensor | None = None image_embeds: tf.Tensor | None = None last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -180,10 +180,10 @@ class TFBlipImageTextMatchingModelOutput(ModelOutput): loss: tf.Tensor | None = None image_embeds: tf.Tensor | None = None last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None vision_pooler_output: tf.Tensor | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - question_embeds: Tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + question_embeds: tuple[tf.Tensor] | None = None @dataclass @@ -216,7 +216,7 @@ class TFBlipOutput(ModelOutput): text_model_output: TFBaseModelOutputWithPooling = None vision_model_output: TFBaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -369,7 +369,7 @@ class TFBlipAttention(keras.layers.Layer): head_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: Optional[bool] = None, - ) -> Tuple[tf.Tensor, tf.Tensor | None, Tuple[tf.Tensor] | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None, tuple[tf.Tensor] | None]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = shape_list(hidden_states) @@ -472,7 +472,7 @@ class TFBlipEncoderLayer(keras.layers.Layer): attention_mask: tf.Tensor, output_attentions: Optional[bool] = False, training: Optional[bool] = None, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -628,7 +628,7 @@ class TFBlipEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: r""" Args: inputs_embeds (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -727,7 +727,7 @@ class TFBlipVisionModel(TFBlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutputWithPooling]: + ) -> Union[tuple, TFBaseModelOutputWithPooling]: r""" Returns: @@ -866,7 +866,7 @@ class TFBlipMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBlipOutput]: + ) -> Union[tuple, TFBlipOutput]: # Use BLIP model's config for some fields (if specified) instead of those of vision & text components. output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -959,7 +959,7 @@ class TFBlipModel(TFBlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBlipOutput]: + ) -> Union[tuple, TFBlipOutput]: r""" Returns: @@ -1121,7 +1121,7 @@ class TFBlipForConditionalGeneration(TFBlipPreTrainedModel): labels: tf.Tensor | None = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBlipForConditionalGenerationModelOutput]: + ) -> Union[tuple, TFBlipForConditionalGenerationModelOutput]: r""" Returns: @@ -1338,7 +1338,7 @@ class TFBlipForQuestionAnswering(TFBlipPreTrainedModel): labels: tf.Tensor | None = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBlipTextVisionModelOutput]: + ) -> Union[tuple, TFBlipTextVisionModelOutput]: r""" Returns: @@ -1592,7 +1592,7 @@ class TFBlipForImageTextRetrieval(TFBlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBlipImageTextMatchingModelOutput]: + ) -> Union[tuple, TFBlipImageTextMatchingModelOutput]: r""" Returns: diff --git a/src/transformers/models/blip/modeling_tf_blip_text.py b/src/transformers/models/blip/modeling_tf_blip_text.py index 5ddf23670c8..e85d3e627ce 100644 --- a/src/transformers/models/blip/modeling_tf_blip_text.py +++ b/src/transformers/models/blip/modeling_tf_blip_text.py @@ -17,7 +17,7 @@ from __future__ import annotations import math -from typing import Optional, Tuple +from typing import Optional import tensorflow as tf @@ -337,7 +337,7 @@ class TFBlipTextAttention(keras.layers.Layer): head_mask: tf.Tensor | None = None, encoder_hidden_states: tf.Tensor | None = None, encoder_attention_mask: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, output_attentions: Optional[bool] = False, training: Optional[bool] = None, ): @@ -751,7 +751,7 @@ class TFBlipTextModel(TFBlipTextPreTrainedModel): @tf.function def get_extended_attention_mask( - self, attention_mask: tf.Tensor, input_shape: Tuple[int], is_decoder: bool + self, attention_mask: tf.Tensor, input_shape: tuple[int], is_decoder: bool ) -> tf.Tensor: """ Makes broadcastable attention and causal masks so that future and masked tokens are ignored. @@ -759,7 +759,7 @@ class TFBlipTextModel(TFBlipTextPreTrainedModel): Arguments: attention_mask (`tf.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. is_decoder (`bool`): Whether the model is used as a decoder. @@ -826,14 +826,14 @@ class TFBlipTextModel(TFBlipTextPreTrainedModel): encoder_embeds: tf.Tensor | None = None, encoder_hidden_states: tf.Tensor | None = None, encoder_attention_mask: tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, use_cache: bool | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, is_decoder: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor] | TFBaseModelOutputWithPoolingAndCrossAttentions: + ) -> tuple[tf.Tensor] | TFBaseModelOutputWithPoolingAndCrossAttentions: r""" encoder_hidden_states (`tf.Tensor`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if diff --git a/src/transformers/models/blip/processing_blip.py b/src/transformers/models/blip/processing_blip.py index 5970e5edbb1..2dfe4d1f887 100644 --- a/src/transformers/models/blip/processing_blip.py +++ b/src/transformers/models/blip/processing_blip.py @@ -16,7 +16,7 @@ Processor class for Blip. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack @@ -66,7 +66,7 @@ class BlipProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[BlipProcessorKwargs], @@ -80,7 +80,7 @@ class BlipProcessor(ProcessorMixin): images (`ImageInput`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/blip_2/modeling_blip_2.py b/src/transformers/models/blip_2/modeling_blip_2.py index 5945f4f48ce..970ee7ad0c4 100644 --- a/src/transformers/models/blip_2/modeling_blip_2.py +++ b/src/transformers/models/blip_2/modeling_blip_2.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -62,13 +62,13 @@ class Blip2ForConditionalGenerationModelOutput(ModelOutput): Outputs of the language model. """ - loss: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + loss: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None vision_outputs: Optional[torch.FloatTensor] = None - qformer_outputs: Optional[Tuple[torch.FloatTensor]] = None - language_model_outputs: Optional[Tuple[torch.FloatTensor]] = None + qformer_outputs: Optional[tuple[torch.FloatTensor]] = None + language_model_outputs: Optional[tuple[torch.FloatTensor]] = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["vision_outputs", "qformer_outputs", "language_model_outputs"] @@ -107,7 +107,7 @@ class Blip2ImageTextMatchingModelOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -140,8 +140,8 @@ class Blip2TextModelOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -170,8 +170,8 @@ class Blip2VisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None # Copied from transformers.models.blip.modeling_blip.BlipVisionEmbeddings with Blip->Blip2 @@ -316,7 +316,7 @@ class Blip2Attention(nn.Module): head_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -388,7 +388,7 @@ class Blip2EncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -494,7 +494,7 @@ class Blip2Encoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -575,7 +575,7 @@ class Blip2VisionModel(Blip2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -793,9 +793,9 @@ class Blip2QFormerAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.attention( hidden_states, attention_mask, @@ -1116,7 +1116,7 @@ class Blip2QFormerModel(Blip2PreTrainedModel): def get_extended_attention_mask( self, attention_mask: torch.Tensor, - input_shape: Tuple[int], + input_shape: tuple[int], device: torch.device, has_query: bool = False, ) -> torch.Tensor: @@ -1126,7 +1126,7 @@ class Blip2QFormerModel(Blip2PreTrainedModel): Arguments: attention_mask (`torch.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. device (`torch.device`): The device of the input to the model. @@ -1167,12 +1167,12 @@ class Blip2QFormerModel(Blip2PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" query_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): Hidden states to be used in the attention computation. If cross-attention, @@ -1531,7 +1531,7 @@ class Blip2Model(Blip2PreTrainedModel): return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Blip2ForConditionalGenerationModelOutput]: + ) -> Union[tuple, Blip2ForConditionalGenerationModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the language model. Input tokens can optionally be @@ -1697,7 +1697,7 @@ class Blip2TextModelWithProjection(Blip2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Blip2TextModelOutput]: + ) -> Union[tuple, Blip2TextModelOutput]: r""" Examples: @@ -1785,7 +1785,7 @@ class Blip2VisionModelWithProjection(Blip2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Blip2VisionModelOutput]: + ) -> Union[tuple, Blip2VisionModelOutput]: r""" Examples: @@ -2004,7 +2004,7 @@ class Blip2ForConditionalGeneration(Blip2PreTrainedModel, GenerationMixin): interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Blip2ForConditionalGenerationModelOutput]: + ) -> Union[tuple, Blip2ForConditionalGenerationModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the language model. Input tokens can optionally be @@ -2308,7 +2308,7 @@ class Blip2ForImageTextRetrieval(Blip2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Blip2ImageTextMatchingModelOutput]: + ) -> Union[tuple, Blip2ImageTextMatchingModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the language model. Input tokens can optionally be diff --git a/src/transformers/models/blip_2/processing_blip_2.py b/src/transformers/models/blip_2/processing_blip_2.py index d94525f6b6f..810311d492a 100644 --- a/src/transformers/models/blip_2/processing_blip_2.py +++ b/src/transformers/models/blip_2/processing_blip_2.py @@ -16,7 +16,7 @@ Processor class for BLIP-2. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput @@ -80,7 +80,7 @@ class Blip2Processor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[Blip2ProcessorKwargs], @@ -94,7 +94,7 @@ class Blip2Processor(ProcessorMixin): images (`ImageInput`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/bloom/configuration_bloom.py b/src/transformers/models/bloom/configuration_bloom.py index 0e14e2e44ca..74748c11304 100644 --- a/src/transformers/models/bloom/configuration_bloom.py +++ b/src/transformers/models/bloom/configuration_bloom.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import TYPE_CHECKING, Any, List, Optional +from typing import TYPE_CHECKING, Any, Optional from packaging import version @@ -149,7 +149,7 @@ class BloomOnnxConfig(OnnxConfigWithPast): self, config: PretrainedConfig, task: str = "default", - patching_specs: Optional[List[PatchingSpec]] = None, + patching_specs: Optional[list[PatchingSpec]] = None, use_past: bool = False, ): super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past) diff --git a/src/transformers/models/bloom/modeling_bloom.py b/src/transformers/models/bloom/modeling_bloom.py index 9c35c9c68d0..66dfc0c1fa8 100644 --- a/src/transformers/models/bloom/modeling_bloom.py +++ b/src/transformers/models/bloom/modeling_bloom.py @@ -16,7 +16,7 @@ import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -211,7 +211,7 @@ class BloomAttention(nn.Module): self.dense = nn.Linear(self.hidden_size, self.hidden_size) self.attention_dropout = nn.Dropout(config.attention_dropout) - def _reshape(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + def _reshape(self, fused_qkv: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Split the last dimension into (num_heads, head_dim) and reshapes to (bs, heads, len, dim) shape without making any copies, results share same memory storage as `fused_qkv` @@ -506,7 +506,7 @@ class BloomModel(BloomPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.LongTensor] = None, @@ -516,7 +516,7 @@ class BloomModel(BloomPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **deprecated_arguments, - ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -873,7 +873,7 @@ class BloomForCausalLM(BloomPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -884,7 +884,7 @@ class BloomForCausalLM(BloomPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **deprecated_arguments, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -957,8 +957,8 @@ class BloomForCausalLM(BloomPreTrainedModel, GenerationMixin): ) def _reorder_cache( - self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor - ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]: + self, past: tuple[tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor + ) -> tuple[tuple[torch.Tensor, torch.Tensor], ...]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1008,7 +1008,7 @@ class BloomForSequenceClassification(BloomPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1018,7 +1018,7 @@ class BloomForSequenceClassification(BloomPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **deprecated_arguments, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1144,7 +1144,7 @@ class BloomForTokenClassification(BloomPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1154,7 +1154,7 @@ class BloomForTokenClassification(BloomPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **deprecated_arguments, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1245,7 +1245,7 @@ class BloomForQuestionAnswering(BloomPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` diff --git a/src/transformers/models/bloom/modeling_flax_bloom.py b/src/transformers/models/bloom/modeling_flax_bloom.py index 0c002728f63..c7bb1cc9c9a 100644 --- a/src/transformers/models/bloom/modeling_flax_bloom.py +++ b/src/transformers/models/bloom/modeling_flax_bloom.py @@ -16,7 +16,7 @@ import math from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -94,7 +94,7 @@ BLOOM_INPUTS_DOCSTRING = r""" - 0 for tokens that are **masked**. [What are attention masks?](../glossary#attention-mask) - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -412,7 +412,7 @@ class FlaxBloomPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: BloomConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -421,7 +421,7 @@ class FlaxBloomPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/bloom/tokenization_bloom_fast.py b/src/transformers/models/bloom/tokenization_bloom_fast.py index c84322637cb..b7a9f7449a4 100644 --- a/src/transformers/models/bloom/tokenization_bloom_fast.py +++ b/src/transformers/models/bloom/tokenization_bloom_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for Bloom.""" import pickle -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -144,7 +144,7 @@ class BloomTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/bridgetower/image_processing_bridgetower.py b/src/transformers/models/bridgetower/image_processing_bridgetower.py index a88b7d3abf6..98ac0434d36 100644 --- a/src/transformers/models/bridgetower/image_processing_bridgetower.py +++ b/src/transformers/models/bridgetower/image_processing_bridgetower.py @@ -15,7 +15,7 @@ """Image processor class for BridgeTower.""" from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -45,7 +45,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.vilt.image_processing_vilt.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -54,7 +54,7 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.vilt.image_processing_vilt.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -62,7 +62,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -73,8 +73,8 @@ def make_pixel_mask( # Copied from transformers.models.vilt.image_processing_vilt.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -97,7 +97,7 @@ def get_resize_output_image_size( longer: int = 1333, size_divisor: int = 32, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: input_height, input_width = get_image_size(input_image, input_data_format) min_size, max_size = shorter, longer @@ -130,7 +130,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{'shortest_edge': 288}`): + size (`dict[str, int]` *optional*, defaults to `{'shortest_edge': 288}`): Resize the shorter side of the input to `size["shortest_edge"]`. The longer side will be limited to under `int((1333 / 800) * size["shortest_edge"])` while preserving the aspect ratio. Only has an effect if `do_resize` is set to `True`. Can be overridden by the `size` parameter in the `preprocess` method. @@ -149,18 +149,18 @@ class BridgeTowerImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*): + crop_size (`dict[str, int]`, *optional*): Desired output size when applying center-cropping. Only has an effect if `do_center_crop` is set to `True`. Can be overridden by the `crop_size` parameter in the `preprocess` method. If unset defaults to `size`, do_pad (`bool`, *optional*, defaults to `True`): @@ -173,16 +173,16 @@ class BridgeTowerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_pad: bool = True, **kwargs, ) -> None: @@ -210,7 +210,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -227,7 +227,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Controls the size of the output image. Should be of the form `{"shortest_edge": int}`. size_divisor (`int`, *optional*, defaults to 32): The image is resized to a size that is a multiple of this value. @@ -258,7 +258,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): def center_crop( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -270,7 +270,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image in the form `{"height": h, "width": w}`. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format of the image. If not provided, it will be the same as the input image. @@ -291,7 +291,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -318,7 +318,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): # Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessor.pad def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -376,17 +376,17 @@ class BridgeTowerImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -400,7 +400,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -415,9 +415,9 @@ class BridgeTowerImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): Whether to pad the image to the (max_height, max_width) in the batch. If `True`, a pixel mask is also @@ -425,7 +425,7 @@ class BridgeTowerImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after center crop. If one edge the image is smaller than `crop_size`, it will be padded with zeros and then cropped return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/bridgetower/image_processing_bridgetower_fast.py b/src/transformers/models/bridgetower/image_processing_bridgetower_fast.py index bfc7f548da2..9e986f8a2d7 100644 --- a/src/transformers/models/bridgetower/image_processing_bridgetower_fast.py +++ b/src/transformers/models/bridgetower/image_processing_bridgetower_fast.py @@ -15,7 +15,7 @@ """Fast Image processor class for BridgeTower.""" from collections.abc import Iterable -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union from ...image_processing_utils_fast import ( BaseImageProcessorFast, @@ -45,7 +45,7 @@ if is_torchvision_available(): def make_pixel_mask( image: "torch.Tensor", - output_size: Tuple[int, int], + output_size: tuple[int, int], ) -> "torch.Tensor": """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -53,7 +53,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = image.shape[-2:] @@ -68,7 +68,7 @@ def get_resize_output_image_size( shorter: int = 800, longer: int = 1333, size_divisor: int = 32, -) -> Tuple[int, int]: +) -> tuple[int, int]: input_height, input_width = input_image.shape[-2:] min_size, max_size = shorter, longer @@ -176,7 +176,7 @@ class BridgeTowerImageProcessorFast(BaseImageProcessorFast): def center_crop( self, image: "torch.Tensor", - size: Dict[str, int], + size: dict[str, int], **kwargs, ) -> "torch.Tensor": """ @@ -186,7 +186,7 @@ class BridgeTowerImageProcessorFast(BaseImageProcessorFast): Args: image (`torch.Tensor`): Image to center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image in the form `{"height": h, "width": w}`. """ output_size = size.shortest_edge @@ -199,7 +199,7 @@ class BridgeTowerImageProcessorFast(BaseImageProcessorFast): def _pad_image( self, image: "torch.Tensor", - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, ) -> "torch.Tensor": """ diff --git a/src/transformers/models/bridgetower/modeling_bridgetower.py b/src/transformers/models/bridgetower/modeling_bridgetower.py index 52ada3b0ac7..10db0bbb62f 100644 --- a/src/transformers/models/bridgetower/modeling_bridgetower.py +++ b/src/transformers/models/bridgetower/modeling_bridgetower.py @@ -17,7 +17,7 @@ import math from collections import OrderedDict from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -71,8 +71,8 @@ class BridgeTowerModelOutput(ModelOutput): text_features: Optional[torch.FloatTensor] = None image_features: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -102,11 +102,11 @@ class BridgeTowerContrastiveOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - text_embeds: Optional[Tuple[torch.FloatTensor]] = None - image_embeds: Optional[Tuple[torch.FloatTensor]] = None - cross_embeds: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + text_embeds: Optional[tuple[torch.FloatTensor]] = None + image_embeds: Optional[tuple[torch.FloatTensor]] = None + cross_embeds: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class BridgeTowerResidualAttention(nn.Module): @@ -448,9 +448,9 @@ class BridgeTowerSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -583,9 +583,9 @@ class BridgeTowerAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -684,9 +684,9 @@ class BridgeTowerTextLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -763,12 +763,12 @@ class BridgeTowerTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -1058,12 +1058,12 @@ class BridgeTowerTextModel(BridgeTowerPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1244,7 +1244,7 @@ class BridgeTowerModel(BridgeTowerPreTrainedModel): return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple[torch.Tensor], BridgeTowerModelOutput]: + ) -> Union[tuple[torch.Tensor], BridgeTowerModelOutput]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1552,7 +1552,7 @@ class BridgeTowerForMaskedLM(BridgeTowerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MaskedLMOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1654,7 +1654,7 @@ class BridgeTowerForImageAndTextRetrieval(BridgeTowerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1768,7 +1768,7 @@ class BridgeTowerForContrastiveLearning(BridgeTowerPreTrainedModel): output_hidden_states: Optional[bool] = True, return_dict: Optional[bool] = None, return_loss: Optional[bool] = None, - ) -> Union[BridgeTowerContrastiveOutput, Tuple[torch.FloatTensor]]: + ) -> Union[BridgeTowerContrastiveOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. diff --git a/src/transformers/models/bridgetower/processing_bridgetower.py b/src/transformers/models/bridgetower/processing_bridgetower.py index 5519d0a34ce..b388c3b22b2 100644 --- a/src/transformers/models/bridgetower/processing_bridgetower.py +++ b/src/transformers/models/bridgetower/processing_bridgetower.py @@ -16,7 +16,7 @@ Processor class for BridgeTower. """ -from typing import List, Union +from typing import Union from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import BatchEncoding, PreTokenizedInput, TextInput @@ -67,7 +67,7 @@ class BridgeTowerProcessor(ProcessorMixin): def __call__( self, images, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[BridgeTowerProcessorKwargs], diff --git a/src/transformers/models/bros/modeling_bros.py b/src/transformers/models/bros/modeling_bros.py index 100337d96b7..94d3a9d985d 100755 --- a/src/transformers/models/bros/modeling_bros.py +++ b/src/transformers/models/bros/modeling_bros.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -66,8 +66,8 @@ class BrosSpadeOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None initial_token_logits: Optional[torch.FloatTensor] = None subsequent_token_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class BrosPositionalEmbedding1D(nn.Module): @@ -232,9 +232,9 @@ class BrosSelfAttention(nn.Module): head_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[torch.Tensor] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -380,9 +380,9 @@ class BrosAttention(nn.Module): head_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states=hidden_states, bbox_pos_emb=bbox_pos_emb, @@ -451,9 +451,9 @@ class BrosLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -532,12 +532,12 @@ class BrosEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -727,12 +727,12 @@ class BrosModel(BrosPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'): Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values @@ -893,7 +893,7 @@ class BrosForTokenClassification(BrosPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'): Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values @@ -1018,7 +1018,7 @@ class BrosSpadeEEForTokenClassification(BrosPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BrosSpadeOutput]: + ) -> Union[tuple[torch.Tensor], BrosSpadeOutput]: r""" bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'): Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values @@ -1159,7 +1159,7 @@ class BrosSpadeELForTokenClassification(BrosPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" bbox ('torch.FloatTensor' of shape '(batch_size, num_boxes, 4)'): Bounding box coordinates for each token in the input sequence. Each bounding box is a list of four values diff --git a/src/transformers/models/bros/processing_bros.py b/src/transformers/models/bros/processing_bros.py index 4687e7f8a86..42a638becdf 100644 --- a/src/transformers/models/bros/processing_bros.py +++ b/src/transformers/models/bros/processing_bros.py @@ -16,7 +16,7 @@ Processor class for Bros. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy @@ -46,7 +46,7 @@ class BrosProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/byt5/tokenization_byt5.py b/src/transformers/models/byt5/tokenization_byt5.py index b39ba254b38..2a9804db101 100644 --- a/src/transformers/models/byt5/tokenization_byt5.py +++ b/src/transformers/models/byt5/tokenization_byt5.py @@ -15,7 +15,7 @@ """Tokenization class for model ByT5.""" import warnings -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging @@ -53,7 +53,7 @@ class ByT5Tokenizer(PreTrainedTokenizer): indexed from the end of the vocabulary up to beginning ("" is the last token in the vocabulary like in ByT5 preprocessing see [here](https://github.com/google-research/text-to-text-transfer-transformer/blob/9fd7b14a769417be33bc6c850f9598764913c833/t5/data/preprocessors.py#L2117)). - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. """ @@ -108,22 +108,22 @@ class ByT5Tokenizer(PreTrainedTokenizer): return vocab def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -135,7 +135,7 @@ class ByT5Tokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] - def _add_eos_if_not_present(self, token_ids: List[int]) -> List[int]: + def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]: """Do not add eos again if user already added it.""" if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( @@ -147,20 +147,20 @@ class ByT5Tokenizer(PreTrainedTokenizer): return token_ids + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. ByT5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] @@ -169,8 +169,8 @@ class ByT5Tokenizer(PreTrainedTokenizer): return len(token_ids_0 + eos + token_ids_1 + eos) * [0] def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -179,13 +179,13 @@ class ByT5Tokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = self._add_eos_if_not_present(token_ids_0) if token_ids_1 is None: @@ -194,7 +194,7 @@ class ByT5Tokenizer(PreTrainedTokenizer): token_ids_1 = self._add_eos_if_not_present(token_ids_1) return token_ids_0 + token_ids_1 - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" tokens = [chr(i) for i in text.encode("utf-8")] return tokens @@ -229,7 +229,7 @@ class ByT5Tokenizer(PreTrainedTokenizer): return string # ByT5Tokenizer has no vocab file - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: return () diff --git a/src/transformers/models/camembert/modeling_camembert.py b/src/transformers/models/camembert/modeling_camembert.py index b8b855db67f..1b4a52295f2 100644 --- a/src/transformers/models/camembert/modeling_camembert.py +++ b/src/transformers/models/camembert/modeling_camembert.py @@ -16,7 +16,7 @@ """PyTorch CamemBERT model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -176,9 +176,9 @@ class CamembertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -286,9 +286,9 @@ class CamembertSdpaSelfAttention(CamembertSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -429,9 +429,9 @@ class CamembertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -500,9 +500,9 @@ class CamembertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -579,12 +579,12 @@ class CamembertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -821,12 +821,12 @@ class CamembertModel(CamembertPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -995,7 +995,7 @@ class CamembertForMaskedLM(CamembertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1080,7 +1080,7 @@ class CamembertForSequenceClassification(CamembertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1175,7 +1175,7 @@ class CamembertForMultipleChoice(CamembertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1286,7 +1286,7 @@ class CamembertForTokenClassification(CamembertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1365,7 +1365,7 @@ class CamembertForQuestionAnswering(CamembertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1467,13 +1467,13 @@ class CamembertForCausalLM(CamembertPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: diff --git a/src/transformers/models/camembert/modeling_tf_camembert.py b/src/transformers/models/camembert/modeling_tf_camembert.py index 6f456723dea..fae297889f0 100644 --- a/src/transformers/models/camembert/modeling_tf_camembert.py +++ b/src/transformers/models/camembert/modeling_tf_camembert.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -343,10 +343,10 @@ class TFCamembertSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -481,10 +481,10 @@ class TFCamembertAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -598,10 +598,10 @@ class TFCamembertLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -694,13 +694,13 @@ class TFCamembertEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -809,13 +809,13 @@ class TFCamembertMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1008,13 +1008,13 @@ class TFCamembertModel(TFCamembertPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1026,7 +1026,7 @@ class TFCamembertModel(TFCamembertPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1169,7 +1169,7 @@ class TFCamembertForMaskedLM(TFCamembertPreTrainedModel, TFMaskedLanguageModelin return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1299,7 +1299,7 @@ class TFCamembertForSequenceClassification(TFCamembertPreTrainedModel, TFSequenc return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1395,7 +1395,7 @@ class TFCamembertForTokenClassification(TFCamembertPreTrainedModel, TFTokenClass return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1487,7 +1487,7 @@ class TFCamembertForMultipleChoice(TFCamembertPreTrainedModel, TFMultipleChoiceL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1592,7 +1592,7 @@ class TFCamembertForQuestionAnswering(TFCamembertPreTrainedModel, TFQuestionAnsw start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -1706,14 +1706,14 @@ class TFCamembertForCausalLM(TFCamembertPreTrainedModel, TFCausalLanguageModelin inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1725,7 +1725,7 @@ class TFCamembertForCausalLM(TFCamembertPreTrainedModel, TFCausalLanguageModelin - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all diff --git a/src/transformers/models/camembert/tokenization_camembert.py b/src/transformers/models/camembert/tokenization_camembert.py index 76f95b5f8cd..cd6e399f208 100644 --- a/src/transformers/models/camembert/tokenization_camembert.py +++ b/src/transformers/models/camembert/tokenization_camembert.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -81,7 +81,7 @@ class CamembertTokenizer(PreTrainedTokenizer): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `['NOTUSED', 'NOTUSED', 'NOTUSED']`): + additional_special_tokens (`list[str]`, *optional*, defaults to `['NOTUSED', 'NOTUSED', 'NOTUSED']`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -118,7 +118,7 @@ class CamembertTokenizer(PreTrainedTokenizer): pad_token="", mask_token="", additional_special_tokens=["NOTUSED", "NOTUSED", "NOTUSED"], - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -176,7 +176,7 @@ class CamembertTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -226,7 +226,7 @@ class CamembertTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -244,8 +244,8 @@ class CamembertTokenizer(PreTrainedTokenizer): return (out_vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An CamemBERT sequence has the following format: @@ -254,13 +254,13 @@ class CamembertTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -270,22 +270,22 @@ class CamembertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -297,20 +297,20 @@ class CamembertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. CamemBERT, like RoBERTa, does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/camembert/tokenization_camembert_fast.py b/src/transformers/models/camembert/tokenization_camembert_fast.py index 05d0073da6b..423058ed959 100644 --- a/src/transformers/models/camembert/tokenization_camembert_fast.py +++ b/src/transformers/models/camembert/tokenization_camembert_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -85,7 +85,7 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -126,8 +126,8 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An CamemBERT sequence has the following format: @@ -136,13 +136,13 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -152,20 +152,20 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. CamemBERT, like RoBERTa, does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -174,7 +174,7 @@ class CamembertTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/canine/modeling_canine.py b/src/transformers/models/canine/modeling_canine.py index ae0bed5bd31..d55c600d05d 100644 --- a/src/transformers/models/canine/modeling_canine.py +++ b/src/transformers/models/canine/modeling_canine.py @@ -18,7 +18,7 @@ import copy import math import os from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -78,8 +78,8 @@ class CanineModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None def load_tf_weights_in_canine(model, config, tf_checkpoint_path): @@ -419,7 +419,7 @@ class CanineSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: mixed_query_layer = self.query(from_tensor) # If this is instantiated as a cross-attention module, the keys @@ -492,8 +492,8 @@ class CanineSelfOutput(nn.Module): self.dropout = nn.Dropout(config.hidden_dropout_prob) def forward( - self, hidden_states: Tuple[torch.FloatTensor], input_tensor: torch.FloatTensor - ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: + self, hidden_states: tuple[torch.FloatTensor], input_tensor: torch.FloatTensor + ) -> tuple[torch.FloatTensor, torch.FloatTensor]: hidden_states = self.dense(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) @@ -570,11 +570,11 @@ class CanineAttention(nn.Module): def forward( self, - hidden_states: Tuple[torch.FloatTensor], + hidden_states: tuple[torch.FloatTensor], attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: if not self.local: self_outputs = self.self(hidden_states, hidden_states, attention_mask, head_mask, output_attentions) attention_output = self_outputs[0] @@ -665,7 +665,7 @@ class CanineOutput(nn.Module): self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) self.dropout = nn.Dropout(config.hidden_dropout_prob) - def forward(self, hidden_states: Tuple[torch.FloatTensor], input_tensor: torch.FloatTensor) -> torch.FloatTensor: + def forward(self, hidden_states: tuple[torch.FloatTensor], input_tensor: torch.FloatTensor) -> torch.FloatTensor: hidden_states = self.dense(hidden_states) hidden_states = self.dropout(hidden_states) hidden_states = self.LayerNorm(hidden_states + input_tensor) @@ -702,11 +702,11 @@ class CanineLayer(nn.Module): def forward( self, - hidden_states: Tuple[torch.FloatTensor], + hidden_states: tuple[torch.FloatTensor], attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: self_attention_outputs = self.attention( hidden_states, attention_mask, @@ -763,13 +763,13 @@ class CanineEncoder(nn.Module): def forward( self, - hidden_states: Tuple[torch.FloatTensor], + hidden_states: tuple[torch.FloatTensor], attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -812,7 +812,7 @@ class CaninePooler(nn.Module): self.dense = nn.Linear(config.hidden_size, config.hidden_size) self.activation = nn.Tanh() - def forward(self, hidden_states: Tuple[torch.FloatTensor]) -> torch.FloatTensor: + def forward(self, hidden_states: tuple[torch.FloatTensor]) -> torch.FloatTensor: # We "pool" the model by simply taking the hidden state corresponding # to the first token. first_token_tensor = hidden_states[:, 0] @@ -831,7 +831,7 @@ class CaninePredictionHeadTransform(nn.Module): self.transform_act_fn = config.hidden_act self.LayerNorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps) - def forward(self, hidden_states: Tuple[torch.FloatTensor]) -> torch.FloatTensor: + def forward(self, hidden_states: tuple[torch.FloatTensor]) -> torch.FloatTensor: hidden_states = self.dense(hidden_states) hidden_states = self.transform_act_fn(hidden_states) hidden_states = self.LayerNorm(hidden_states) @@ -852,7 +852,7 @@ class CanineLMPredictionHead(nn.Module): # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings` self.decoder.bias = self.bias - def forward(self, hidden_states: Tuple[torch.FloatTensor]) -> torch.FloatTensor: + def forward(self, hidden_states: tuple[torch.FloatTensor]) -> torch.FloatTensor: hidden_states = self.transform(hidden_states) hidden_states = self.decoder(hidden_states) return hidden_states @@ -865,8 +865,8 @@ class CanineOnlyMLMHead(nn.Module): def forward( self, - sequence_output: Tuple[torch.Tensor], - ) -> Tuple[torch.Tensor]: + sequence_output: tuple[torch.Tensor], + ) -> tuple[torch.Tensor]: prediction_scores = self.predictions(sequence_output) return prediction_scores @@ -1020,7 +1020,7 @@ class CanineModel(CaninePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CanineModelOutputWithPooling]: + ) -> Union[tuple, CanineModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1199,7 +1199,7 @@ class CanineForSequenceClassification(CaninePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1284,7 +1284,7 @@ class CanineForMultipleChoice(CaninePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1389,7 +1389,7 @@ class CanineForTokenClassification(CaninePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1486,7 +1486,7 @@ class CanineForQuestionAnswering(CaninePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.canine( diff --git a/src/transformers/models/canine/tokenization_canine.py b/src/transformers/models/canine/tokenization_canine.py index c32410b5fd4..f6b2a8bfd96 100644 --- a/src/transformers/models/canine/tokenization_canine.py +++ b/src/transformers/models/canine/tokenization_canine.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization classes for CANINE.""" -from typing import Dict, List, Optional +from typing import Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging @@ -36,7 +36,7 @@ MASK = 0xE003 RESERVED = 0xE004 # Maps special codepoints to human-readable names. -SPECIAL_CODEPOINTS: Dict[int, str] = { +SPECIAL_CODEPOINTS: dict[int, str] = { # Special symbols are represented using codepoints values that are valid, # but designated as "Private Use", meaning that they will never be assigned # characters by the Unicode Consortium, and are thus safe for use here. @@ -52,7 +52,7 @@ SPECIAL_CODEPOINTS: Dict[int, str] = { } # Maps special codepoint human-readable names to their codepoint values. -SPECIAL_CODEPOINTS_BY_NAME: Dict[str, int] = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} +SPECIAL_CODEPOINTS_BY_NAME: dict[str, int] = {name: codepoint for codepoint, name in SPECIAL_CODEPOINTS.items()} class CanineTokenizer(PreTrainedTokenizer): @@ -91,12 +91,12 @@ class CanineTokenizer(PreTrainedTokenizer): mask_token = AddedToken(mask_token, lstrip=True, rstrip=False) if isinstance(mask_token, str) else mask_token # Creates a mapping for looking up the IDs of special symbols. - self._special_codepoints: Dict[str, int] = {} + self._special_codepoints: dict[str, int] = {} for codepoint, name in SPECIAL_CODEPOINTS.items(): self._special_codepoints[name] = codepoint # Creates a mapping for looking up the string forms of special symbol IDs. - self._special_codepoint_strings: Dict[int, str] = { + self._special_codepoint_strings: dict[int, str] = { codepoint: name for name, codepoint in self._special_codepoints.items() } @@ -124,7 +124,7 @@ class CanineTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string (i.e. perform character splitting).""" return list(text) @@ -151,8 +151,8 @@ class CanineTokenizer(PreTrainedTokenizer): return "".join(tokens) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A CANINE sequence has the following format: @@ -178,8 +178,8 @@ class CanineTokenizer(PreTrainedTokenizer): return result def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. diff --git a/src/transformers/models/chameleon/configuration_chameleon.py b/src/transformers/models/chameleon/configuration_chameleon.py index efb6da999d0..34436a5288c 100644 --- a/src/transformers/models/chameleon/configuration_chameleon.py +++ b/src/transformers/models/chameleon/configuration_chameleon.py @@ -14,7 +14,7 @@ # limitations under the License. """chameleon model configuration""" -from typing import List, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -47,11 +47,11 @@ class ChameleonVQVAEConfig(PretrainedConfig): Number of input channels. base_channels (`int`, *optional*, defaults to 128): Base channel count. - channel_multiplier (`List[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): + channel_multiplier (`list[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): Channel multipliers for each resolution. num_res_blocks (`int`, *optional*, defaults to 2): Number of residual blocks. - attn_resolutions (`List[int]`, *optional*): + attn_resolutions (`list[int]`, *optional*): Resolutions to apply attention. dropout (`float`, *optional*, defaults to 0.0): Dropout rate. @@ -73,9 +73,9 @@ class ChameleonVQVAEConfig(PretrainedConfig): resolution: int = 512, in_channels: int = 3, base_channels: int = 128, - channel_multiplier: List[int] = [1, 1, 2, 2, 4], + channel_multiplier: list[int] = [1, 1, 2, 2, 4], num_res_blocks: int = 2, - attn_resolutions: Optional[List[int]] = None, + attn_resolutions: Optional[list[int]] = None, dropout: float = 0.0, attn_type: str = "vanilla", initializer_range=0.02, diff --git a/src/transformers/models/chameleon/image_processing_chameleon.py b/src/transformers/models/chameleon/image_processing_chameleon.py index e694cee7bb8..3ff1a9139eb 100644 --- a/src/transformers/models/chameleon/image_processing_chameleon.py +++ b/src/transformers/models/chameleon/image_processing_chameleon.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Chameleon.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -52,7 +52,7 @@ class ChameleonImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 512}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 512}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -61,7 +61,7 @@ class ChameleonImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to {"height": 512, "width": 512}): + crop_size (`dict[str, int]` *optional*, defaults to {"height": 512, "width": 512}): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -72,10 +72,10 @@ class ChameleonImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[1.0, 1.0, 1.0]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[1.0, 1.0, 1.0]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[1.0, 1.0, 1.0]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[1.0, 1.0, 1.0]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -88,15 +88,15 @@ class ChameleonImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PIL.Image.LANCZOS, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 0.0078, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -122,7 +122,7 @@ class ChameleonImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -135,7 +135,7 @@ class ChameleonImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -173,15 +173,15 @@ class ChameleonImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -196,7 +196,7 @@ class ChameleonImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -204,7 +204,7 @@ class ChameleonImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -212,9 +212,9 @@ class ChameleonImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/chameleon/modeling_chameleon.py b/src/transformers/models/chameleon/modeling_chameleon.py index ad01558c6f7..d6575a8751d 100644 --- a/src/transformers/models/chameleon/modeling_chameleon.py +++ b/src/transformers/models/chameleon/modeling_chameleon.py @@ -15,7 +15,7 @@ """PyTorch Chameleon model.""" from functools import cached_property -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -330,7 +330,7 @@ class ChameleonAttention(nn.Module): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -408,7 +408,7 @@ class ChameleonDecoderLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -483,7 +483,7 @@ class ChameleonSwinDecoderLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): @@ -949,7 +949,7 @@ class ChameleonModel(ChameleonPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1252,7 +1252,7 @@ class ChameleonForConditionalGeneration(ChameleonPreTrainedModel, GenerationMixi output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/chameleon/processing_chameleon.py b/src/transformers/models/chameleon/processing_chameleon.py index 5a364cdc34d..f8dfcff33cf 100644 --- a/src/transformers/models/chameleon/processing_chameleon.py +++ b/src/transformers/models/chameleon/processing_chameleon.py @@ -16,7 +16,7 @@ Processor class for Chameleon. """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -92,7 +92,7 @@ class ChameleonProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[ChameleonProcessorKwargs], @@ -105,10 +105,10 @@ class ChameleonProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -176,7 +176,7 @@ class ChameleonProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/chinese_clip/image_processing_chinese_clip.py b/src/transformers/models/chinese_clip/image_processing_chinese_clip.py index e8f8ba1e8d5..701168625c4 100644 --- a/src/transformers/models/chinese_clip/image_processing_chinese_clip.py +++ b/src/transformers/models/chinese_clip/image_processing_chinese_clip.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Chinese-CLIP.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -60,7 +60,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -69,7 +69,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -80,10 +80,10 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -96,15 +96,15 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -129,7 +129,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -142,7 +142,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -170,15 +170,15 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -193,7 +193,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -201,7 +201,7 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -209,9 +209,9 @@ class ChineseCLIPImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/chinese_clip/modeling_chinese_clip.py b/src/transformers/models/chinese_clip/modeling_chinese_clip.py index 0a5527ca8aa..5de98397cad 100644 --- a/src/transformers/models/chinese_clip/modeling_chinese_clip.py +++ b/src/transformers/models/chinese_clip/modeling_chinese_clip.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -82,7 +82,7 @@ class ChineseCLIPOutput(ModelOutput): text_model_output: BaseModelOutputWithPoolingAndCrossAttentions = None vision_model_output: BaseModelOutputWithPoolingAndCrossAttentions = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -278,9 +278,9 @@ class ChineseCLIPTextSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -428,9 +428,9 @@ class ChineseCLIPTextAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -474,7 +474,7 @@ class ChineseCLIPVisionAttention(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -599,9 +599,9 @@ class ChineseCLIPTextLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -676,7 +676,7 @@ class ChineseCLIPVisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -791,12 +791,12 @@ class ChineseCLIPTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -891,7 +891,7 @@ class ChineseCLIPVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -966,7 +966,7 @@ class ChineseCLIPVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1063,12 +1063,12 @@ class ChineseCLIPTextModel(ChineseCLIPPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1191,7 +1191,7 @@ class ChineseCLIPVisionModel(ChineseCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -1374,7 +1374,7 @@ class ChineseCLIPModel(ChineseCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ChineseCLIPOutput]: + ) -> Union[tuple, ChineseCLIPOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. diff --git a/src/transformers/models/chinese_clip/processing_chinese_clip.py b/src/transformers/models/chinese_clip/processing_chinese_clip.py index 3523c782f3a..0cc7d2c5409 100644 --- a/src/transformers/models/chinese_clip/processing_chinese_clip.py +++ b/src/transformers/models/chinese_clip/processing_chinese_clip.py @@ -17,7 +17,7 @@ Image/Text processor class for Chinese-CLIP """ import warnings -from typing import List, Union +from typing import Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack @@ -68,7 +68,7 @@ class ChineseCLIPProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, images: ImageInput = None, audio=None, videos=None, @@ -82,11 +82,11 @@ class ChineseCLIPProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. diff --git a/src/transformers/models/clap/feature_extraction_clap.py b/src/transformers/models/clap/feature_extraction_clap.py index 7f9c3f60015..710b4ed551e 100644 --- a/src/transformers/models/clap/feature_extraction_clap.py +++ b/src/transformers/models/clap/feature_extraction_clap.py @@ -15,7 +15,7 @@ """Feature extractor class for CLAP.""" import copy -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import numpy as np import torch @@ -136,12 +136,12 @@ class ClapFeatureExtractor(SequenceFeatureExtractor): mel_scale="slaney", ) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, except for the + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, except for the mel filter banks, which do not need to be saved or printed as they are too long. """ output = copy.deepcopy(self.__dict__) @@ -259,7 +259,7 @@ class ClapFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], truncation: Optional[str] = None, padding: Optional[str] = None, max_length: Optional[int] = None, @@ -271,7 +271,7 @@ class ClapFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -349,7 +349,7 @@ class ClapFeatureExtractor(SequenceFeatureExtractor): rand_idx = np.random.randint(0, len(input_mel)) is_longer[rand_idx] = True - if isinstance(input_mel[0], List): + if isinstance(input_mel[0], list): input_mel = [np.asarray(feature, dtype=np.float64) for feature in input_mel] # is_longer is a list of bool diff --git a/src/transformers/models/clap/modeling_clap.py b/src/transformers/models/clap/modeling_clap.py index a382982e2f6..0f8058ad76a 100644 --- a/src/transformers/models/clap/modeling_clap.py +++ b/src/transformers/models/clap/modeling_clap.py @@ -17,7 +17,7 @@ import collections import math from dataclasses import dataclass -from typing import Any, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn.functional as F @@ -146,8 +146,8 @@ class ClapTextModelOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -175,8 +175,8 @@ class ClapAudioModelOutput(ModelOutput): audio_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -210,7 +210,7 @@ class ClapOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None audio_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "audio_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -431,7 +431,7 @@ class ClapAudioSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -528,7 +528,7 @@ class ClapAudioAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -625,11 +625,11 @@ class ClapAudioLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: if not always_partition: self.set_shift_and_window_size(input_dimensions) else: @@ -721,11 +721,11 @@ class ClapAudioStage(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: height, width = input_dimensions for i, layer_module in enumerate(self.blocks): layer_head_mask = head_mask[i] if head_mask is not None else None @@ -757,7 +757,7 @@ class ClapAudioPatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -765,7 +765,7 @@ class ClapAudioPatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -780,7 +780,7 @@ class ClapAudioPatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -893,7 +893,7 @@ class ClapAudioEncoder(nn.Module): output_hidden_states_before_downsampling: Optional[bool] = False, always_partition: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, ClapAudioModelOutput]: + ) -> Union[tuple, ClapAudioModelOutput]: input_features = input_features.transpose(1, 3) normalized_input_features = self.batch_norm(input_features) normalized_input_features = normalized_input_features.transpose(1, 3) @@ -1156,9 +1156,9 @@ class ClapTextSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -1306,9 +1306,9 @@ class ClapTextAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -1377,9 +1377,9 @@ class ClapTextLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -1456,12 +1456,12 @@ class ClapTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -1601,7 +1601,7 @@ class ClapAudioModel(ClapPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Input audio features. This should be returned by the [`ClapFeatureExtractor`] class that you can also @@ -1692,12 +1692,12 @@ class ClapTextModel(ClapPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1942,7 +1942,7 @@ class ClapModel(ClapPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ClapOutput]: + ) -> Union[tuple, ClapOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Input audio features. This should be returned by the [`ClapFeatureExtractor`] class that you can also @@ -2060,7 +2060,7 @@ class ClapTextModelWithProjection(ClapPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ClapTextModelOutput]: + ) -> Union[tuple, ClapTextModelOutput]: r""" Examples: @@ -2125,7 +2125,7 @@ class ClapAudioModelWithProjection(ClapPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ClapAudioModelOutput]: + ) -> Union[tuple, ClapAudioModelOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Input audio features. This should be returned by the [`ClapFeatureExtractor`] class that you can also diff --git a/src/transformers/models/clap/processing_clap.py b/src/transformers/models/clap/processing_clap.py index 126fc384ebf..65c25f7663b 100644 --- a/src/transformers/models/clap/processing_clap.py +++ b/src/transformers/models/clap/processing_clap.py @@ -49,11 +49,11 @@ class ClapProcessor(ProcessorMixin): docstring of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - audios (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + audios (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The audio or batch of audios to be prepared. Each audio can be NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each audio should be of shape (C, T), where C is a number of channels, and T the sample length of the audio. diff --git a/src/transformers/models/clip/image_processing_clip.py b/src/transformers/models/clip/image_processing_clip.py index a506da423de..df96f0f64b8 100644 --- a/src/transformers/models/clip/image_processing_clip.py +++ b/src/transformers/models/clip/image_processing_clip.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for CLIP.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -59,7 +59,7 @@ class CLIPImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -68,7 +68,7 @@ class CLIPImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -79,10 +79,10 @@ class CLIPImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -95,15 +95,15 @@ class CLIPImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -153,7 +153,7 @@ class CLIPImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -166,7 +166,7 @@ class CLIPImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -203,15 +203,15 @@ class CLIPImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -227,7 +227,7 @@ class CLIPImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -235,7 +235,7 @@ class CLIPImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -243,9 +243,9 @@ class CLIPImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/clip/modeling_clip.py b/src/transformers/models/clip/modeling_clip.py index d50efcb7556..b93f63bcea9 100644 --- a/src/transformers/models/clip/modeling_clip.py +++ b/src/transformers/models/clip/modeling_clip.py @@ -15,7 +15,7 @@ """PyTorch CLIP model.""" from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch from torch import nn @@ -80,8 +80,8 @@ class CLIPVisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -109,8 +109,8 @@ class CLIPTextModelOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -143,7 +143,7 @@ class CLIPOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -326,7 +326,7 @@ class CLIPAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -408,7 +408,7 @@ class CLIPEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` diff --git a/src/transformers/models/clip/modeling_flax_clip.py b/src/transformers/models/clip/modeling_flax_clip.py index c8eb6cf02ed..0394974d064 100644 --- a/src/transformers/models/clip/modeling_flax_clip.py +++ b/src/transformers/models/clip/modeling_flax_clip.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import flax import flax.linen as nn @@ -182,8 +182,8 @@ class FlaxCLIPTextModelOutput(ModelOutput): text_embeds: jnp.ndarray = None last_hidden_state: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray, ...]] = None - attentions: Optional[Tuple[jnp.ndarray, ...]] = None + hidden_states: Optional[tuple[jnp.ndarray, ...]] = None + attentions: Optional[tuple[jnp.ndarray, ...]] = None @flax.struct.dataclass @@ -215,7 +215,7 @@ class FlaxCLIPOutput(ModelOutput): text_model_output: FlaxBaseModelOutputWithPooling = None vision_model_output: FlaxBaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -641,7 +641,7 @@ class FlaxCLIPTextPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensor input_ids = jnp.zeros(input_shape, dtype="i4") position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_shape) @@ -712,7 +712,7 @@ class FlaxCLIPVisionPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: CLIPVisionConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -723,7 +723,7 @@ class FlaxCLIPVisionPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensor pixel_values = jax.random.normal(rng, input_shape) @@ -783,7 +783,7 @@ class FlaxCLIPPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: CLIPConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -794,7 +794,7 @@ class FlaxCLIPPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensor input_ids = jnp.zeros(input_shape[0], dtype="i4") position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_shape[0]) diff --git a/src/transformers/models/clip/modeling_tf_clip.py b/src/transformers/models/clip/modeling_tf_clip.py index 6afdadd2529..90ff2652ac3 100644 --- a/src/transformers/models/clip/modeling_tf_clip.py +++ b/src/transformers/models/clip/modeling_tf_clip.py @@ -18,7 +18,7 @@ from __future__ import annotations import math from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -115,7 +115,7 @@ class TFCLIPOutput(ModelOutput): text_model_output: TFBaseModelOutputWithPooling = None vision_model_output: TFBaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -306,7 +306,7 @@ class TFCLIPAttention(keras.layers.Layer): causal_attention_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """Input shape: Batch x Time x Channel""" batch_size = shape_list(hidden_states)[0] @@ -423,7 +423,7 @@ class TFCLIPEncoderLayer(keras.layers.Layer): causal_attention_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -498,7 +498,7 @@ class TFCLIPEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -560,7 +560,7 @@ class TFCLIPTextTransformer(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: input_shape = shape_list(input_ids) embedding_output = self.embeddings(input_ids=input_ids, position_ids=position_ids) @@ -681,7 +681,7 @@ class TFCLIPTextMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if input_ids is None: raise ValueError("You have to specify input_ids") @@ -728,7 +728,7 @@ class TFCLIPVisionTransformer(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: embedding_output = self.embeddings(pixel_values=pixel_values) embedding_output = self.pre_layernorm(inputs=embedding_output) @@ -794,7 +794,7 @@ class TFCLIPVisionMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if pixel_values is None: raise ValueError("You have to specify pixel_values") @@ -957,7 +957,7 @@ class TFCLIPMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFCLIPOutput, Tuple[tf.Tensor]]: + ) -> Union[TFCLIPOutput, tuple[tf.Tensor]]: if input_ids is None: raise ValueError("You have to specify either input_ids") if pixel_values is None: @@ -1077,7 +1077,7 @@ CLIP_START_DOCSTRING = r""" CLIP_TEXT_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1114,7 +1114,7 @@ CLIP_TEXT_INPUTS_DOCSTRING = r""" CLIP_VISION_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more @@ -1134,14 +1134,14 @@ CLIP_VISION_INPUTS_DOCSTRING = r""" CLIP_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details. attention_mask (`np.ndarray` or `tf.Tensor` of shape `({0})`, *optional*): @@ -1195,7 +1195,7 @@ class TFCLIPTextModel(TFCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -1254,7 +1254,7 @@ class TFCLIPVisionModel(TFCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -1402,7 +1402,7 @@ class TFCLIPModel(TFCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFCLIPOutput, Tuple[tf.Tensor]]: + ) -> Union[TFCLIPOutput, tuple[tf.Tensor]]: r""" Returns: diff --git a/src/transformers/models/clip/processing_clip.py b/src/transformers/models/clip/processing_clip.py index 6f835fb313b..eb21cc8fc2e 100644 --- a/src/transformers/models/clip/processing_clip.py +++ b/src/transformers/models/clip/processing_clip.py @@ -67,11 +67,11 @@ class CLIPProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. diff --git a/src/transformers/models/clip/tokenization_clip.py b/src/transformers/models/clip/tokenization_clip.py index 41a73db8c1e..e4a142f0d3b 100644 --- a/src/transformers/models/clip/tokenization_clip.py +++ b/src/transformers/models/clip/tokenization_clip.py @@ -18,7 +18,7 @@ import json import os import unicodedata from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -337,8 +337,8 @@ class CLIPTokenizer(PreTrainedTokenizer): return dict(self.encoder, **self.added_tokens_encoder) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A CLIP sequence has the following format: @@ -348,13 +348,13 @@ class CLIPTokenizer(PreTrainedTokenizer): Pairs of sequences are not the expected use case, but they will be handled without a separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ bos_token = [self.bos_token_id] eos_token = [self.eos_token_id] @@ -364,22 +364,22 @@ class CLIPTokenizer(PreTrainedTokenizer): return bos_token + token_ids_0 + eos_token + eos_token + token_ids_1 + eos_token def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -392,20 +392,20 @@ class CLIPTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + [1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed. CLIP does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ bos_token = [self.bos_token_id] eos_token = [self.eos_token_id] @@ -486,7 +486,7 @@ class CLIPTokenizer(PreTrainedTokenizer): text = byte_array.decode("utf-8", errors=self.errors).replace("", " ").strip() return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) return diff --git a/src/transformers/models/clip/tokenization_clip_fast.py b/src/transformers/models/clip/tokenization_clip_fast.py index 89e7c836031..4de1504a806 100644 --- a/src/transformers/models/clip/tokenization_clip_fast.py +++ b/src/transformers/models/clip/tokenization_clip_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization classes for OpenAI GPT.""" -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import pre_tokenizers @@ -107,8 +107,8 @@ class CLIPTokenizerFast(PreTrainedTokenizerFast): self.backend_tokenizer.decode = new_decode_method def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A CLIP sequence has the following format: @@ -118,13 +118,13 @@ class CLIPTokenizerFast(PreTrainedTokenizerFast): Pairs of sequences are not the expected use case, but they will be handled without a separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ bos_token = [self.bos_token_id] eos_token = [self.eos_token_id] @@ -134,20 +134,20 @@ class CLIPTokenizerFast(PreTrainedTokenizerFast): return bos_token + token_ids_0 + eos_token + eos_token + token_ids_1 + eos_token def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed. CLIP does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ bos_token = [self.bos_token_id] eos_token = [self.eos_token_id] @@ -156,7 +156,7 @@ class CLIPTokenizerFast(PreTrainedTokenizerFast): return len(bos_token + token_ids_0 + eos_token) * [0] return len(bos_token + token_ids_0 + eos_token + eos_token + token_ids_1 + eos_token) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/clipseg/configuration_clipseg.py b/src/transformers/models/clipseg/configuration_clipseg.py index 7be9bd4d55e..55c29c2b7dd 100644 --- a/src/transformers/models/clipseg/configuration_clipseg.py +++ b/src/transformers/models/clipseg/configuration_clipseg.py @@ -223,7 +223,7 @@ class CLIPSegConfig(PretrainedConfig): Dimensionality of text and vision projection layers. logit_scale_init_value (`float`, *optional*, defaults to 2.6592): The initial value of the *logit_scale* parameter. Default is used as per the original CLIPSeg implementation. - extract_layers (`List[int]`, *optional*, defaults to `[3, 6, 9]`): + extract_layers (`list[int]`, *optional*, defaults to `[3, 6, 9]`): Layers to extract when forwarding the query image through the frozen visual backbone of CLIP. reduce_dim (`int`, *optional*, defaults to 64): Dimensionality to reduce the CLIP vision embedding. diff --git a/src/transformers/models/clipseg/modeling_clipseg.py b/src/transformers/models/clipseg/modeling_clipseg.py index 2321932812b..c68404cb66c 100644 --- a/src/transformers/models/clipseg/modeling_clipseg.py +++ b/src/transformers/models/clipseg/modeling_clipseg.py @@ -17,7 +17,7 @@ import copy import math from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -78,7 +78,7 @@ class CLIPSegOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -101,8 +101,8 @@ class CLIPSegDecoderOutput(ModelOutput): """ logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -123,7 +123,7 @@ class CLIPSegImageSegmentationOutput(ModelOutput): vision_model_output: BaseModelOutputWithPooling = None decoder_output: CLIPSegDecoderOutput = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["vision_model_output", "decoder_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -306,7 +306,7 @@ class CLIPSegAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -389,7 +389,7 @@ class CLIPSegEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -496,7 +496,7 @@ class CLIPSegEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -591,7 +591,7 @@ class CLIPSegTextTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -687,7 +687,7 @@ class CLIPSegTextModel(CLIPSegPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -733,7 +733,7 @@ class CLIPSegVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -786,7 +786,7 @@ class CLIPSegVisionModel(CLIPSegPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = True, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -962,7 +962,7 @@ class CLIPSegModel(CLIPSegPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = True, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CLIPSegOutput]: + ) -> Union[tuple, CLIPSegOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. @@ -1067,7 +1067,7 @@ class CLIPSegDecoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1148,7 +1148,7 @@ class CLIPSegDecoder(CLIPSegPreTrainedModel): def forward( self, - hidden_states: Tuple[torch.Tensor], + hidden_states: tuple[torch.Tensor], conditional_embeddings: torch.Tensor, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1267,7 +1267,7 @@ class CLIPSegForImageSegmentation(CLIPSegPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = True, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CLIPSegOutput]: + ) -> Union[tuple, CLIPSegOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/clipseg/processing_clipseg.py b/src/transformers/models/clipseg/processing_clipseg.py index 5a4c10930f7..e4dc04d30f7 100644 --- a/src/transformers/models/clipseg/processing_clipseg.py +++ b/src/transformers/models/clipseg/processing_clipseg.py @@ -67,14 +67,14 @@ class CLIPSegProcessor(ProcessorMixin): the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - visual_prompt (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + visual_prompt (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The visual prompt image or batch of images to be prepared. Each visual prompt image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. diff --git a/src/transformers/models/clvp/feature_extraction_clvp.py b/src/transformers/models/clvp/feature_extraction_clvp.py index 6a4965971c7..6fe4a52aa5e 100644 --- a/src/transformers/models/clvp/feature_extraction_clvp.py +++ b/src/transformers/models/clvp/feature_extraction_clvp.py @@ -17,7 +17,7 @@ Feature extractor class for CLVP """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -130,7 +130,7 @@ class ClvpFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], sampling_rate: Optional[int] = None, truncation: bool = True, pad_to_multiple_of: Optional[int] = None, @@ -148,7 +148,7 @@ class ClvpFeatureExtractor(SequenceFeatureExtractor): seconds long and then the log-mel spectrogram is extracted from it. Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -230,7 +230,7 @@ class ClvpFeatureExtractor(SequenceFeatureExtractor): self._np_extract_fbank_features(waveform).astype(np.float32) for waveform in input_features[0] ] - if isinstance(input_features[0], List): + if isinstance(input_features[0], list): padded_inputs["input_features"] = [np.asarray(feature) for feature in input_features] else: padded_inputs["input_features"] = input_features diff --git a/src/transformers/models/clvp/modeling_clvp.py b/src/transformers/models/clvp/modeling_clvp.py index 24520791f19..a2e42fc2aea 100644 --- a/src/transformers/models/clvp/modeling_clvp.py +++ b/src/transformers/models/clvp/modeling_clvp.py @@ -18,7 +18,7 @@ import copy import math from dataclasses import dataclass -from typing import Callable, Dict, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -169,8 +169,8 @@ class ClvpEncoderOutput(ModelOutput): embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -308,11 +308,11 @@ class ClvpSelfAttention(nn.Module): rotary_pos_emb: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, use_cache: Optional[bool] = False, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor], Optional[Tuple[torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor], Optional[tuple[torch.FloatTensor]]]: # Raise error when position_ids is None but rotary_pos_emb is provided, because we need that when applying # rotary_pos_emb to query and key states. if rotary_pos_emb is not None and position_ids is None: @@ -451,7 +451,7 @@ class ClvpEncoderLayer(nn.Module): attention_mask: torch.LongTensor, position_ids: torch.LongTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor` of shape `(batch, seq_len, embed_dim)`): @@ -605,7 +605,7 @@ class ClvpDecoderMLP(nn.Module): self.act = ACT2FN[config.activation_function] self.dropout = nn.Dropout(config.resid_pdrop) - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -627,14 +627,14 @@ class ClvpDecoderLayer(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], - past_key_value: Optional[Tuple[torch.Tensor]] = None, + hidden_states: Optional[tuple[torch.FloatTensor]], + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) attn_outputs = self.attn( @@ -884,7 +884,7 @@ class ClvpEncoder(ClvpPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`, *optional*): @@ -1042,13 +1042,13 @@ class ClvpDecoder(ClvpPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1205,13 +1205,13 @@ class ClvpModel(ClvpPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1274,8 +1274,8 @@ class ClvpForCausalLM(ClvpPreTrainedModel, GenerationMixin): self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, torch.Tensor]] = None, - ) -> Tuple[torch.Tensor, Optional[str], Dict[str, torch.Tensor]]: + model_kwargs: Optional[dict[str, torch.Tensor]] = None, + ) -> tuple[torch.Tensor, Optional[str], dict[str, torch.Tensor]]: """ This function extracts the model-specific `inputs` for generation. """ @@ -1394,7 +1394,7 @@ class ClvpForCausalLM(ClvpPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -1405,7 +1405,7 @@ class ClvpForCausalLM(ClvpPreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -1464,8 +1464,8 @@ class ClvpForCausalLM(ClvpPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1716,7 +1716,7 @@ class ClvpModelForConditionalGeneration(ClvpPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ClvpOutput]: + ) -> Union[tuple, ClvpOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, time_dim)`): Indicates log mel-spectrogram representations for audio returned by [`ClvpFeatureExtractor`]. diff --git a/src/transformers/models/clvp/tokenization_clvp.py b/src/transformers/models/clvp/tokenization_clvp.py index 85ae1d6991e..39e7429dfb2 100644 --- a/src/transformers/models/clvp/tokenization_clvp.py +++ b/src/transformers/models/clvp/tokenization_clvp.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -258,22 +258,22 @@ class ClvpTokenizer(PreTrainedTokenizer): # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -334,7 +334,7 @@ class ClvpTokenizer(PreTrainedTokenizer): return text # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/code_llama/tokenization_code_llama.py b/src/transformers/models/code_llama/tokenization_code_llama.py index 99bdcb5e64d..94d1b4d6598 100644 --- a/src/transformers/models/code_llama/tokenization_code_llama.py +++ b/src/transformers/models/code_llama/tokenization_code_llama.py @@ -18,7 +18,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -108,7 +108,7 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): Whether to add an end of sequence token at the end of sequences. clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`): Whether or not to clean up the tokenization spaces. - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. use_default_system_prompt (`bool`, *optional*, defaults to `False`): Whether or not the default system prompt for Llama should be used. @@ -129,7 +129,7 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): eot_token="▁", fill_token="", suffix_first=False, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, @@ -248,7 +248,7 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def tokenize(self, prefix, suffix=None, suffix_first=False, **kwargs) -> List[int]: + def tokenize(self, prefix, suffix=None, suffix_first=False, **kwargs) -> list[int]: # add a prefix space to `prefix` if self.fill_token is not None and self.fill_token in prefix and suffix is None: prefix, suffix = prefix.split(self.fill_token) @@ -328,7 +328,7 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): return out_string # Copied from transformers.models.llama.tokenization_llama.LlamaTokenizer.save_vocabulary - def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> tuple[str]: """ Save the vocabulary and special tokens file to a directory. @@ -369,22 +369,22 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): # Copied from transformers.models.llama.tokenization_llama.LlamaTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -407,8 +407,8 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): # Copied from transformers.models.llama.tokenization_llama.LlamaTokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format: @@ -421,13 +421,13 @@ class CodeLlamaTokenizer(PreTrainedTokenizer): if token_ids_1 is None, only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ bos_token_id = [self.bos_token_id] if self.add_bos_token else [] eos_token_id = [self.eos_token_id] if self.add_eos_token else [] diff --git a/src/transformers/models/code_llama/tokenization_code_llama_fast.py b/src/transformers/models/code_llama/tokenization_code_llama_fast.py index 089c5c066e7..b3978587e7f 100644 --- a/src/transformers/models/code_llama/tokenization_code_llama_fast.py +++ b/src/transformers/models/code_llama/tokenization_code_llama_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import normalizers, processors @@ -98,7 +98,7 @@ class CodeLlamaTokenizerFast(PreTrainedTokenizerFast): End of text token used for infilling. fill_token (`str`, *optional*, defaults to `""`): The token used to split the input between the prefix and suffix. - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. add_bos_token (`bool`, *optional*, defaults to `True`): Whether to add a beginning of sequence token at the start of sequences. @@ -323,7 +323,7 @@ class CodeLlamaTokenizerFast(PreTrainedTokenizerFast): return tokens # Copied from transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " @@ -343,8 +343,8 @@ class CodeLlamaTokenizerFast(PreTrainedTokenizerFast): return (out_vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. The special tokens depend on calling set_lang. @@ -358,13 +358,13 @@ class CodeLlamaTokenizerFast(PreTrainedTokenizerFast): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.bos_token_id + token_ids_0 + self.eos_token_id diff --git a/src/transformers/models/codegen/configuration_codegen.py b/src/transformers/models/codegen/configuration_codegen.py index 550274a5585..6a9ab842710 100644 --- a/src/transformers/models/codegen/configuration_codegen.py +++ b/src/transformers/models/codegen/configuration_codegen.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import Any, List, Optional +from typing import Any, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig @@ -152,7 +152,7 @@ class CodeGenOnnxConfig(OnnxConfigWithPast): self, config: PretrainedConfig, task: str = "default", - patching_specs: Optional[List[PatchingSpec]] = None, + patching_specs: Optional[list[PatchingSpec]] = None, use_past: bool = False, ): super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past) diff --git a/src/transformers/models/codegen/modeling_codegen.py b/src/transformers/models/codegen/modeling_codegen.py index a1bef381ce7..6a99d0fa390 100644 --- a/src/transformers/models/codegen/modeling_codegen.py +++ b/src/transformers/models/codegen/modeling_codegen.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch CodeGen model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -156,8 +156,8 @@ class CodeGenAttention(nn.Module): output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, ) -> Union[ - Tuple[torch.Tensor, Tuple[torch.Tensor]], - Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], + tuple[torch.Tensor, tuple[torch.Tensor]], + Optional[tuple[torch.Tensor, tuple[torch.Tensor], tuple[torch.Tensor, ...]]], ]: qkv = self.qkv_proj(hidden_states) # TODO(enijkamp): factor out number of logical TPU-v4 cores or make forward pass agnostic @@ -264,7 +264,7 @@ class CodeGenBlock(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_outputs = self.attn( @@ -350,7 +350,7 @@ class CodeGenModel(CodeGenPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -362,7 +362,7 @@ class CodeGenModel(CodeGenPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This @@ -643,7 +643,7 @@ class CodeGenForCausalLM(CodeGenPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -656,7 +656,7 @@ class CodeGenForCausalLM(CodeGenPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This @@ -718,8 +718,8 @@ class CodeGenForCausalLM(CodeGenPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PretrainedModel.beam_search`] or [`~PretrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct diff --git a/src/transformers/models/codegen/tokenization_codegen.py b/src/transformers/models/codegen/tokenization_codegen.py index b6fdda509ac..99f1facb1f8 100644 --- a/src/transformers/models/codegen/tokenization_codegen.py +++ b/src/transformers/models/codegen/tokenization_codegen.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np import regex as re @@ -276,7 +276,7 @@ class CodeGenTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -313,10 +313,10 @@ class CodeGenTokenizer(PreTrainedTokenizer): def decode( self, - token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], + token_ids: Union[int, list[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, - truncate_before_pattern: Optional[List[str]] = None, + truncate_before_pattern: Optional[list[str]] = None, **kwargs, ) -> str: """ diff --git a/src/transformers/models/codegen/tokenization_codegen_fast.py b/src/transformers/models/codegen/tokenization_codegen_fast.py index 0d14c82773c..7bac0db7de4 100644 --- a/src/transformers/models/codegen/tokenization_codegen_fast.py +++ b/src/transformers/models/codegen/tokenization_codegen_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for OpenAI GPT.""" import re -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -154,16 +154,16 @@ class CodeGenTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) def decode( self, - token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], + token_ids: Union[int, list[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, - truncate_before_pattern: Optional[List[str]] = None, + truncate_before_pattern: Optional[list[str]] = None, **kwargs, ) -> str: """ diff --git a/src/transformers/models/cohere/configuration_cohere.py b/src/transformers/models/cohere/configuration_cohere.py index ba5de252d17..c78d1e9bf8a 100644 --- a/src/transformers/models/cohere/configuration_cohere.py +++ b/src/transformers/models/cohere/configuration_cohere.py @@ -105,11 +105,11 @@ class CohereConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/cohere/modeling_cohere.py b/src/transformers/models/cohere/modeling_cohere.py index 0700eb8e9f6..88ca4e31de1 100644 --- a/src/transformers/models/cohere/modeling_cohere.py +++ b/src/transformers/models/cohere/modeling_cohere.py @@ -27,7 +27,7 @@ # This file is based on the LLama model definition file in transformers -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -230,12 +230,12 @@ class CohereAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -296,9 +296,9 @@ class CohereDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -314,7 +314,7 @@ class CohereDecoderLayer(GradientCheckpointingLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -547,7 +547,7 @@ class CohereForCausalLM(CoherePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/cohere/modular_cohere.py b/src/transformers/models/cohere/modular_cohere.py index e37c875be38..248ada3227f 100644 --- a/src/transformers/models/cohere/modular_cohere.py +++ b/src/transformers/models/cohere/modular_cohere.py @@ -22,7 +22,7 @@ """PyTorch Cohere model.""" -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -153,12 +153,12 @@ class CohereAttention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -219,9 +219,9 @@ class CohereDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -237,7 +237,7 @@ class CohereDecoderLayer(GradientCheckpointingLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -311,7 +311,7 @@ class CohereForCausalLM(LlamaForCausalLM): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/cohere/tokenization_cohere_fast.py b/src/transformers/models/cohere/tokenization_cohere_fast.py index d679f0c4e3a..6b1de3dd317 100644 --- a/src/transformers/models/cohere/tokenization_cohere_fast.py +++ b/src/transformers/models/cohere/tokenization_cohere_fast.py @@ -16,7 +16,7 @@ # This file is based on the tokenization_llama_fast.py file in transformers import pickle -from typing import Dict, List, Literal, Union +from typing import Literal, Union from tokenizers import processors @@ -227,10 +227,10 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): def apply_tool_use_template( self, - conversation: Union[List[Dict[str, str]]], - tools: List[Dict], + conversation: Union[list[dict[str, str]]], + tools: list[dict], **kwargs, - ) -> Union[str, List[int]]: + ) -> Union[str, list[int]]: """Create a Command-R tool-use prompt. Once rendered, the prompt instructs the model to generate a list of actions to perform on a set of user supplied tools @@ -244,16 +244,16 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): You can override the default template using the `tool_use_template` kwarg but the quality of your results may decrease. Args: - conversation (Union[List[Dict[str, str]]]): A list of dicts + conversation (Union[list[dict[str, str]]]): A list of dicts with "role" and "content" keys, representing the chat history so far. - tools (List[Dict]): a list of tools to render into the prompt for the model to choose from. + tools (list[Dict]): a list of tools to render into the prompt for the model to choose from. See an example at the bottom of the docstring. The format should be: * name (str): The name of the tool to be called. Valid names contain only the characters a-z, A-Z, 0-9, _ and must not begin with a digit. * description (str): The description of what the tool does, the model uses the description to choose when and how to call the function. - * parameter_definitions (List[Dict]): The input parameters of the tool. Accepts a dictionary + * parameter_definitions (list[Dict]): The input parameters of the tool. Accepts a dictionary where the key is the name of the parameter and the value is the parameter spec. Valid parameter names contain only the characters a-z, A-Z, 0-9, _ and must not begin with a digit. Parameter specs are as follows: @@ -287,7 +287,7 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): Returns: `str`: A rendered prompt string. or if tokenize=True: - `List[int]`: A list of token ids representing the tokenized chat so far, including control tokens. This + `list[int]`: A list of token ids representing the tokenized chat so far, including control tokens. This output is ready to pass to the model, either directly or via methods like `generate()`. Examples: @@ -336,7 +336,7 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): Here is a list of tools that you have available to you: \\`\\`\\`python - def internet_search(query: str) -> List[Dict]: + def internet_search(query: str) -> list[Dict]: \"\"\"Returns a list of relevant document snippets for a textual query retrieved from the internet Args: @@ -346,7 +346,7 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): \\`\\`\\` \\`\\`\\`python - def directly_answer() -> List[Dict]: + def directly_answer() -> list[Dict]: \"\"\"Calls a standard (un-augmented) AI chatbot to generate a response given the conversation history \"\"\" pass @@ -382,11 +382,11 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): def apply_grounded_generation_template( self, - conversation: Union[List[Dict[str, str]]], - documents: List[Dict], + conversation: Union[list[dict[str, str]]], + documents: list[dict], citation_mode: Literal["fast", "accurate"] = "accurate", **kwargs, - ) -> Union[str, List[int]]: + ) -> Union[str, list[int]]: """Create a Command-R grounded generation (aka RAG) prompt. Once rendered, the prompt instructs the model to generate a response with citations in, based on supplied documents. @@ -400,9 +400,9 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): You can override the default template using the `grounded_generation_template` kwarg but the quality of your results may decrease. Args: - conversation (Union[List[Dict[str, str]]]): A list of dicts + conversation (Union[list[dict[str, str]]]): A list of dicts with "role" and "content" keys, representing the chat history so far. - documents (List[Dict[str, str]): A list of dicts, representing documents or tool outputs to ground your + documents (list[dict[str, str]): A list of dicts, representing documents or tool outputs to ground your generation on. A document is a semistructured dict, with a string to string mapping. Common fields are `url`, `title`, `snippet` etc but should be descriptive of the key. They will get rendered into the prompt. citation_mode: either "accurate" (prompt the model to generate an answer first, then rewrite it with citation @@ -435,7 +435,7 @@ class CohereTokenizerFast(PreTrainedTokenizerFast): Returns: `str`: A rendered prompt string. or if tokenize=True: - `List[int]`: A list of token ids representing the tokenized chat so far, including control tokens. This + `list[int]`: A list of token ids representing the tokenized chat so far, including control tokens. This output is ready to pass to the model, either directly or via methods like `generate()`. Examples: diff --git a/src/transformers/models/cohere2/configuration_cohere2.py b/src/transformers/models/cohere2/configuration_cohere2.py index 8d8d3aa9824..747781075a0 100644 --- a/src/transformers/models/cohere2/configuration_cohere2.py +++ b/src/transformers/models/cohere2/configuration_cohere2.py @@ -101,11 +101,11 @@ class Cohere2Config(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/cohere2/modeling_cohere2.py b/src/transformers/models/cohere2/modeling_cohere2.py index 5690864cfc5..6999f1632f9 100644 --- a/src/transformers/models/cohere2/modeling_cohere2.py +++ b/src/transformers/models/cohere2/modeling_cohere2.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -199,12 +199,12 @@ class Cohere2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -270,18 +270,18 @@ class Cohere2DecoderLayer(GradientCheckpointingLayer): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. attention_mask (`torch.FloatTensor`, *optional*): @@ -528,7 +528,7 @@ class Cohere2ForCausalLM(Cohere2PreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/cohere2/modular_cohere2.py b/src/transformers/models/cohere2/modular_cohere2.py index 5139611ff5b..b32e4d94dd5 100644 --- a/src/transformers/models/cohere2/modular_cohere2.py +++ b/src/transformers/models/cohere2/modular_cohere2.py @@ -13,7 +13,7 @@ # 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. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.nn as nn @@ -123,11 +123,11 @@ class Cohere2Config(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -286,12 +286,12 @@ class Cohere2Attention(CohereAttention, nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -337,18 +337,18 @@ class Cohere2DecoderLayer(CohereDecoderLayer): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. attention_mask (`torch.FloatTensor`, *optional*): diff --git a/src/transformers/models/colpali/convert_colpali_weights_to_hf.py b/src/transformers/models/colpali/convert_colpali_weights_to_hf.py index 1b30f3f97ac..b9c55f120d4 100644 --- a/src/transformers/models/colpali/convert_colpali_weights_to_hf.py +++ b/src/transformers/models/colpali/convert_colpali_weights_to_hf.py @@ -39,7 +39,7 @@ python src/transformers/models/colpali/convert_colpali_weights_to_hf.py \ import argparse import glob from pathlib import Path -from typing import Any, Dict, Optional +from typing import Any, Optional import torch from huggingface_hub import snapshot_download @@ -58,7 +58,7 @@ logger = logging.get_logger(__name__) ORIGINAL_DTYPE = torch.bfloat16 -def rename_state_dict_keys(state_dict: Dict[str, Any]) -> Dict[str, Any]: +def rename_state_dict_keys(state_dict: dict[str, Any]) -> dict[str, Any]: new_state_dict = {} for key, value in state_dict.items(): new_key = key @@ -70,7 +70,7 @@ def rename_state_dict_keys(state_dict: Dict[str, Any]) -> Dict[str, Any]: return new_state_dict -def load_original_state_dict(model_id: str, revision: Optional[str] = None) -> Dict[str, torch.Tensor]: +def load_original_state_dict(model_id: str, revision: Optional[str] = None) -> dict[str, torch.Tensor]: directory_path = snapshot_download( repo_id=model_id, revision=revision, diff --git a/src/transformers/models/colpali/modeling_colpali.py b/src/transformers/models/colpali/modeling_colpali.py index 82eda38072f..077ec0a88de 100644 --- a/src/transformers/models/colpali/modeling_colpali.py +++ b/src/transformers/models/colpali/modeling_colpali.py @@ -15,7 +15,7 @@ """PyTorch ColPali model""" from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -84,9 +84,9 @@ class ColPaliForRetrievalOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None embeddings: Optional[torch.Tensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None diff --git a/src/transformers/models/colpali/modular_colpali.py b/src/transformers/models/colpali/modular_colpali.py index 513af889228..a090fb5dfed 100644 --- a/src/transformers/models/colpali/modular_colpali.py +++ b/src/transformers/models/colpali/modular_colpali.py @@ -14,7 +14,7 @@ # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union from transformers.models.paligemma.processing_paligemma import IMAGE_TOKEN, PaliGemmaProcessor, build_string_from_input @@ -90,7 +90,7 @@ class ColPaliProcessor(PaliGemmaProcessor): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[ColPaliProcessorKwargs], @@ -107,11 +107,11 @@ class ColPaliProcessor(PaliGemmaProcessor): Please refer to the docstring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -197,7 +197,7 @@ class ColPaliProcessor(PaliGemmaProcessor): if suffix is None: suffix = self.query_augmentation_token * 10 - texts_query: List[str] = [] + texts_query: list[str] = [] for query in text: query = self.tokenizer.bos_token + self.query_prefix + query + suffix + "\n" texts_query.append(query) @@ -224,7 +224,7 @@ class ColPaliProcessor(PaliGemmaProcessor): This method forwards the `images` and `kwargs` arguments to the image processor. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. @@ -249,7 +249,7 @@ class ColPaliProcessor(PaliGemmaProcessor): def process_queries( self, - text: Union[TextInput, List[TextInput]], + text: Union[TextInput, list[TextInput]], **kwargs: Unpack[ColPaliProcessorKwargs], ) -> BatchFeature: """ @@ -259,7 +259,7 @@ class ColPaliProcessor(PaliGemmaProcessor): This method forwards the `text` and `kwargs` arguments to the tokenizer. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -283,8 +283,8 @@ class ColPaliProcessor(PaliGemmaProcessor): def score_retrieval( self, - query_embeddings: Union["torch.Tensor", List["torch.Tensor"]], - passage_embeddings: Union["torch.Tensor", List["torch.Tensor"]], + query_embeddings: Union["torch.Tensor", list["torch.Tensor"]], + passage_embeddings: Union["torch.Tensor", list["torch.Tensor"]], batch_size: int = 128, output_dtype: Optional["torch.dtype"] = None, output_device: Union["torch.device", str] = "cpu", @@ -301,8 +301,8 @@ class ColPaliProcessor(PaliGemmaProcessor): obtained by padding the list of tensors. Args: - query_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Query embeddings. - passage_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Passage embeddings. + query_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Query embeddings. + passage_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Passage embeddings. batch_size (`int`, *optional*, defaults to 128): Batch size for computing scores. output_dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The dtype of the output tensor. If `None`, the dtype of the input embeddings is used. @@ -327,10 +327,10 @@ class ColPaliProcessor(PaliGemmaProcessor): if output_dtype is None: output_dtype = query_embeddings[0].dtype - scores: List[torch.Tensor] = [] + scores: list[torch.Tensor] = [] for i in range(0, len(query_embeddings), batch_size): - batch_scores: List[torch.Tensor] = [] + batch_scores: list[torch.Tensor] = [] batch_queries = torch.nn.utils.rnn.pad_sequence( query_embeddings[i : i + batch_size], batch_first=True, padding_value=0 ) diff --git a/src/transformers/models/colpali/processing_colpali.py b/src/transformers/models/colpali/processing_colpali.py index a0f1c124d63..759209beacf 100644 --- a/src/transformers/models/colpali/processing_colpali.py +++ b/src/transformers/models/colpali/processing_colpali.py @@ -20,7 +20,7 @@ # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, is_valid_image, make_flat_list_of_images @@ -63,7 +63,7 @@ def build_string_from_input(prompt, bos_token, image_seq_len, image_token, num_i The output will be: "Initial str" Args: - prompt (`List[Union[str, ImageInput]]`): The input prompt. + prompt (`list[Union[str, ImageInput]]`): The input prompt. bos_token (`str`): The beginning of sentence token. image_seq_len (`int`): The length of the image sequence. image_token (`str`): The image token. @@ -134,7 +134,7 @@ class ColPaliProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[ColPaliProcessorKwargs], @@ -151,11 +151,11 @@ class ColPaliProcessor(ProcessorMixin): Please refer to the docstring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -241,7 +241,7 @@ class ColPaliProcessor(ProcessorMixin): if suffix is None: suffix = self.query_augmentation_token * 10 - texts_query: List[str] = [] + texts_query: list[str] = [] for query in text: query = self.tokenizer.bos_token + self.query_prefix + query + suffix + "\n" texts_query.append(query) @@ -261,10 +261,10 @@ class ColPaliProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (List[List[str]], *optional*): + image_sizes (list[list[str]], *optional*): The input sizes formatted as (height, width) per each image. Returns: - Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio") + dict[str, list[int]]: A dictionary mapping each modality ("image", "video", "audio") to a list containing the number of placeholder tokens required. If the model doesn't accept a certain modality or no input sizes are provided, the dict value is set to an empty list. """ @@ -316,7 +316,7 @@ class ColPaliProcessor(ProcessorMixin): This method forwards the `images` and `kwargs` arguments to the image processor. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. @@ -341,7 +341,7 @@ class ColPaliProcessor(ProcessorMixin): def process_queries( self, - text: Union[TextInput, List[TextInput]], + text: Union[TextInput, list[TextInput]], **kwargs: Unpack[ColPaliProcessorKwargs], ) -> BatchFeature: """ @@ -351,7 +351,7 @@ class ColPaliProcessor(ProcessorMixin): This method forwards the `text` and `kwargs` arguments to the tokenizer. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -375,8 +375,8 @@ class ColPaliProcessor(ProcessorMixin): def score_retrieval( self, - query_embeddings: Union["torch.Tensor", List["torch.Tensor"]], - passage_embeddings: Union["torch.Tensor", List["torch.Tensor"]], + query_embeddings: Union["torch.Tensor", list["torch.Tensor"]], + passage_embeddings: Union["torch.Tensor", list["torch.Tensor"]], batch_size: int = 128, output_dtype: Optional["torch.dtype"] = None, output_device: Union["torch.device", str] = "cpu", @@ -393,8 +393,8 @@ class ColPaliProcessor(ProcessorMixin): obtained by padding the list of tensors. Args: - query_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Query embeddings. - passage_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Passage embeddings. + query_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Query embeddings. + passage_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Passage embeddings. batch_size (`int`, *optional*, defaults to 128): Batch size for computing scores. output_dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The dtype of the output tensor. If `None`, the dtype of the input embeddings is used. @@ -419,10 +419,10 @@ class ColPaliProcessor(ProcessorMixin): if output_dtype is None: output_dtype = query_embeddings[0].dtype - scores: List[torch.Tensor] = [] + scores: list[torch.Tensor] = [] for i in range(0, len(query_embeddings), batch_size): - batch_scores: List[torch.Tensor] = [] + batch_scores: list[torch.Tensor] = [] batch_queries = torch.nn.utils.rnn.pad_sequence( query_embeddings[i : i + batch_size], batch_first=True, padding_value=0 ) diff --git a/src/transformers/models/colqwen2/configuration_colqwen2.py b/src/transformers/models/colqwen2/configuration_colqwen2.py index 39035d87736..bab31fae74f 100644 --- a/src/transformers/models/colqwen2/configuration_colqwen2.py +++ b/src/transformers/models/colqwen2/configuration_colqwen2.py @@ -14,7 +14,7 @@ from copy import deepcopy -from typing import Any, Dict +from typing import Any from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -54,7 +54,7 @@ class ColQwen2Config(PretrainedConfig): """ model_type = "colqwen2" - sub_configs: Dict[str, Any] = {"vlm_config": PretrainedConfig} + sub_configs: dict[str, Any] = {"vlm_config": PretrainedConfig} def __init__( self, diff --git a/src/transformers/models/colqwen2/convert_colqwen2_weights_to_hf.py b/src/transformers/models/colqwen2/convert_colqwen2_weights_to_hf.py index 3b652470ccd..455643b1ac5 100644 --- a/src/transformers/models/colqwen2/convert_colqwen2_weights_to_hf.py +++ b/src/transformers/models/colqwen2/convert_colqwen2_weights_to_hf.py @@ -35,7 +35,7 @@ python src/transformers/models/colqwen2/convert_colqwen2_weights_to_hf.py \ import argparse import glob from pathlib import Path -from typing import Any, Dict, Optional +from typing import Any, Optional import torch from huggingface_hub import snapshot_download @@ -54,7 +54,7 @@ logger = logging.get_logger(__name__) ORIGINAL_DTYPE = torch.bfloat16 -def load_original_state_dict(model_id: str, revision: Optional[str] = None) -> Dict[str, torch.Tensor]: +def load_original_state_dict(model_id: str, revision: Optional[str] = None) -> dict[str, torch.Tensor]: directory_path = snapshot_download( repo_id=model_id, revision=revision, @@ -75,8 +75,8 @@ def load_original_state_dict(model_id: str, revision: Optional[str] = None) -> D return original_state_dict -def rename_state_dict_keys(state_dict: Dict[str, Any]) -> Dict[str, Any]: - new_state_dict: Dict[str, Any] = {} +def rename_state_dict_keys(state_dict: dict[str, Any]) -> dict[str, Any]: + new_state_dict: dict[str, Any] = {} for key, value in state_dict.items(): if key.startswith("custom_text_proj"): new_key = key.replace("custom_text_proj", "embedding_proj_layer") diff --git a/src/transformers/models/colqwen2/modeling_colqwen2.py b/src/transformers/models/colqwen2/modeling_colqwen2.py index 1982f9e1365..a38746fe12d 100644 --- a/src/transformers/models/colqwen2/modeling_colqwen2.py +++ b/src/transformers/models/colqwen2/modeling_colqwen2.py @@ -20,7 +20,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from torch import nn @@ -92,9 +92,9 @@ class ColQwen2ForRetrievalOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None embeddings: Optional[torch.Tensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -137,7 +137,7 @@ class ColQwen2ForRetrieval(ColQwen2PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, labels: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/colqwen2/modular_colqwen2.py b/src/transformers/models/colqwen2/modular_colqwen2.py index a2357f6befd..892a2a68700 100644 --- a/src/transformers/models/colqwen2/modular_colqwen2.py +++ b/src/transformers/models/colqwen2/modular_colqwen2.py @@ -14,7 +14,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from transformers.models.colpali.modeling_colpali import ColPaliForRetrieval, ColPaliPreTrainedModel from transformers.models.colpali.processing_colpali import ColPaliProcessor @@ -93,7 +93,7 @@ class ColQwen2Processor(ColPaliProcessor): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[ColQwen2ProcessorKwargs], @@ -110,11 +110,11 @@ class ColQwen2Processor(ColPaliProcessor): Please refer to the doctsring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -209,7 +209,7 @@ class ColQwen2Processor(ColPaliProcessor): if suffix is None: suffix = self.query_augmentation_token * 10 - texts_query: List[str] = [] + texts_query: list[str] = [] for query in text: augmented_query = self.query_prefix + query + suffix @@ -261,9 +261,9 @@ class ColQwen2ForRetrievalOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None embeddings: Optional[torch.Tensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -288,7 +288,7 @@ class ColQwen2ForRetrieval(ColPaliForRetrieval): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, labels: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/colqwen2/processing_colqwen2.py b/src/transformers/models/colqwen2/processing_colqwen2.py index 30e2d8882e9..e283f573960 100644 --- a/src/transformers/models/colqwen2/processing_colqwen2.py +++ b/src/transformers/models/colqwen2/processing_colqwen2.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, is_valid_image @@ -93,7 +93,7 @@ class ColQwen2Processor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[ColQwen2ProcessorKwargs], @@ -110,11 +110,11 @@ class ColQwen2Processor(ProcessorMixin): Please refer to the doctsring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -209,7 +209,7 @@ class ColQwen2Processor(ProcessorMixin): if suffix is None: suffix = self.query_augmentation_token * 10 - texts_query: List[str] = [] + texts_query: list[str] = [] for query in text: augmented_query = self.query_prefix + query + suffix @@ -228,10 +228,10 @@ class ColQwen2Processor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (List[List[str]], *optional*): + image_sizes (list[list[str]], *optional*): The input sizes formatted as (height, width) per each image. Returns: - Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio") + dict[str, list[int]]: A dictionary mapping each modality ("image", "video", "audio") to a list containing the number of placeholder tokens required. If the model doesn't accept a certain modality or no input sizes are provided, the dict value is set to an empty list. """ @@ -283,7 +283,7 @@ class ColQwen2Processor(ProcessorMixin): This method forwards the `images` and `kwargs` arguments to the image processor. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. @@ -308,7 +308,7 @@ class ColQwen2Processor(ProcessorMixin): def process_queries( self, - text: Union[TextInput, List[TextInput]], + text: Union[TextInput, list[TextInput]], **kwargs: Unpack[ColQwen2ProcessorKwargs], ) -> BatchFeature: """ @@ -318,7 +318,7 @@ class ColQwen2Processor(ProcessorMixin): This method forwards the `text` and `kwargs` arguments to the tokenizer. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -342,8 +342,8 @@ class ColQwen2Processor(ProcessorMixin): def score_retrieval( self, - query_embeddings: Union["torch.Tensor", List["torch.Tensor"]], - passage_embeddings: Union["torch.Tensor", List["torch.Tensor"]], + query_embeddings: Union["torch.Tensor", list["torch.Tensor"]], + passage_embeddings: Union["torch.Tensor", list["torch.Tensor"]], batch_size: int = 128, output_dtype: Optional["torch.dtype"] = None, output_device: Union["torch.device", str] = "cpu", @@ -360,8 +360,8 @@ class ColQwen2Processor(ProcessorMixin): obtained by padding the list of tensors. Args: - query_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Query embeddings. - passage_embeddings (`Union[torch.Tensor, List[torch.Tensor]`): Passage embeddings. + query_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Query embeddings. + passage_embeddings (`Union[torch.Tensor, list[torch.Tensor]`): Passage embeddings. batch_size (`int`, *optional*, defaults to 128): Batch size for computing scores. output_dtype (`torch.dtype`, *optional*, defaults to `torch.float32`): The dtype of the output tensor. If `None`, the dtype of the input embeddings is used. @@ -386,10 +386,10 @@ class ColQwen2Processor(ProcessorMixin): if output_dtype is None: output_dtype = query_embeddings[0].dtype - scores: List[torch.Tensor] = [] + scores: list[torch.Tensor] = [] for i in range(0, len(query_embeddings), batch_size): - batch_scores: List[torch.Tensor] = [] + batch_scores: list[torch.Tensor] = [] batch_queries = torch.nn.utils.rnn.pad_sequence( query_embeddings[i : i + batch_size], batch_first=True, padding_value=0 ) diff --git a/src/transformers/models/conditional_detr/image_processing_conditional_detr.py b/src/transformers/models/conditional_detr/image_processing_conditional_detr.py index 9634452de8c..c0f41e93e6e 100644 --- a/src/transformers/models/conditional_detr/image_processing_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_conditional_detr.py @@ -18,7 +18,7 @@ import io import pathlib from collections import defaultdict from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -89,12 +89,12 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationForma # Copied from transformers.models.detr.image_processing_detr.get_size_with_aspect_ratio -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -131,10 +131,10 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in # Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -143,7 +143,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -163,7 +163,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -233,7 +233,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.normalize_annotation -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -248,7 +248,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -257,8 +257,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -276,7 +276,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -284,7 +284,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -299,7 +299,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -427,11 +427,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.prepare_coco_panoptic_annotation with DETR->ConditionalDetr def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for ConditionalDetr. """ @@ -468,7 +468,7 @@ def prepare_coco_panoptic_annotation( # Copied from transformers.models.detr.image_processing_detr.get_segmentation_image def get_segmentation_image( - masks: np.ndarray, input_size: Tuple, target_size: Tuple, stuff_equiv_classes, deduplicate=False + masks: np.ndarray, input_size: tuple, target_size: tuple, stuff_equiv_classes, deduplicate=False ): h, w = input_size final_h, final_w = target_size @@ -493,7 +493,7 @@ def get_segmentation_image( # Copied from transformers.models.detr.image_processing_detr.get_mask_area -def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: int) -> np.ndarray: +def get_mask_area(seg_img: np.ndarray, target_size: tuple[int, int], n_classes: int) -> np.ndarray: final_h, final_w = target_size np_seg_img = seg_img.astype(np.uint8) np_seg_img = np_seg_img.reshape(final_h, final_w, 3) @@ -503,7 +503,7 @@ def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: # Copied from transformers.models.detr.image_processing_detr.score_labels_from_class_probabilities -def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def score_labels_from_class_probabilities(logits: np.ndarray) -> tuple[np.ndarray, np.ndarray]: probs = scipy.special.softmax(logits, axis=-1) labels = probs.argmax(-1, keepdims=True) scores = np.take_along_axis(probs, labels, axis=-1) @@ -516,11 +516,11 @@ def post_process_panoptic_sample( out_logits: np.ndarray, masks: np.ndarray, boxes: np.ndarray, - processed_size: Tuple[int, int], - target_size: Tuple[int, int], - is_thing_map: Dict, + processed_size: tuple[int, int], + target_size: tuple[int, int], + is_thing_map: dict, threshold=0.85, -) -> Dict: +) -> dict: """ Converts the output of [`ConditionalDetrForSegmentation`] into panoptic segmentation predictions for a single sample. @@ -532,10 +532,10 @@ def post_process_panoptic_sample( boxes (`torch.Tensor`): The prediced bounding boxes for this sample. The boxes are in the normalized format `(center_x, center_y, width, height)` and values between `[0, 1]`, relative to the size the image (disregarding padding). - processed_size (`Tuple[int, int]`): + processed_size (`tuple[int, int]`): The processed size of the image `(height, width)`, as returned by the preprocessing step i.e. the size after data augmentation but before batching. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, `(height, width)` corresponding to the requested final size of the prediction. is_thing_map (`Dict`): @@ -599,9 +599,9 @@ def post_process_panoptic_sample( # Copied from transformers.models.detr.image_processing_detr.resize_annotation def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -609,11 +609,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -681,7 +681,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -712,7 +712,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -749,14 +749,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -770,7 +770,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -814,7 +814,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -836,10 +836,10 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): do_normalize: Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -851,7 +851,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -864,16 +864,16 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -932,7 +932,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): @classmethod # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.from_dict with Detr->ConditionalDetr - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `ConditionalDetrImageProcessor.from_pretrained(checkpoint, size=600, @@ -949,12 +949,12 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into ConditionalDetr model. """ @@ -982,7 +982,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -995,7 +995,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1054,7 +1054,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -1091,7 +1091,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -1101,12 +1101,12 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor._update_annotation_for_padded_image def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -1146,8 +1146,8 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1179,24 +1179,24 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.pad def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -1217,7 +1217,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1266,24 +1266,24 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, do_convert_annotations: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -1293,15 +1293,15 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -1310,7 +1310,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1332,9 +1332,9 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): Whether to convert the annotations to the format expected by the model. Converts the bounding boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)` and in relative coordinates. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_pad (`bool`, *optional*, defaults to self.do_pad): Whether to pad the image. If `True`, padding will be applied to the bottom and right of @@ -1355,7 +1355,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1541,7 +1541,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logging.warning_once( @@ -1575,7 +1575,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.deformable_detr.image_processing_deformable_detr.DeformableDetrImageProcessor.post_process_object_detection with DeformableDetr->ConditionalDetr def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`ConditionalDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -1586,14 +1586,14 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1616,7 +1616,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: @@ -1634,18 +1634,18 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): return results # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.post_process_semantic_segmentation with Detr->ConditionalDetr - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None): """ Converts the output of [`ConditionalDetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`ConditionalDetrForSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - A list of tuples (`Tuple[int, int]`) containing the target size (height, width) of each image in the + target_sizes (`list[tuple[int, int]]`, *optional*): + A list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If unset, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1688,9 +1688,9 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`ConditionalDetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch. @@ -1704,16 +1704,16 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. return_coco_annotation (`bool`, *optional*): Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. - **id** -- An integer representing the `segment_id`. @@ -1732,7 +1732,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( @@ -1772,9 +1772,9 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`ConditionalDetrForSegmentation`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1793,11 +1793,11 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1825,7 +1825,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/conditional_detr/image_processing_conditional_detr_fast.py b/src/transformers/models/conditional_detr/image_processing_conditional_detr_fast.py index c51356ddf56..36f87b53921 100644 --- a/src/transformers/models/conditional_detr/image_processing_conditional_detr_fast.py +++ b/src/transformers/models/conditional_detr/image_processing_conditional_detr_fast.py @@ -5,7 +5,7 @@ # modular_conditional_detr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import pathlib -from typing import Any, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -78,7 +78,7 @@ class ConditionalDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -89,7 +89,7 @@ class ConditionalDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -102,7 +102,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int, device: to Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -246,11 +246,11 @@ def rgb_to_id(color): def prepare_coco_panoptic_annotation( image: torch.Tensor, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for CONDITIONAL_DETR. """ @@ -335,7 +335,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `ConditionalDetrImageProcessorFast.from_pretrained(checkpoint, size=600, @@ -351,12 +351,12 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into CONDITIONAL_DETR model. """ @@ -436,9 +436,9 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -446,11 +446,11 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -487,7 +487,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -504,12 +504,12 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -540,8 +540,8 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -571,20 +571,20 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[ConditionalDetrFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -608,8 +608,8 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -619,10 +619,10 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -735,7 +735,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logging.warning_once( @@ -768,7 +768,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`ConditionalDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -779,14 +779,14 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -809,7 +809,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: @@ -826,18 +826,18 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): return results - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None): """ Converts the output of [`ConditionalDetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`ConditionalDetrForSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - A list of tuples (`Tuple[int, int]`) containing the target size (height, width) of each image in the + target_sizes (`list[tuple[int, int]]`, *optional*): + A list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If unset, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -879,9 +879,9 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`ConditionalDetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch. @@ -895,16 +895,16 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. return_coco_annotation (`bool`, *optional*): Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. - **id** -- An integer representing the `segment_id`. @@ -923,7 +923,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( @@ -962,9 +962,9 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`ConditionalDetrForSegmentation`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -983,11 +983,11 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1015,7 +1015,7 @@ class ConditionalDetrImageProcessorFast(BaseImageProcessorFast): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/conditional_detr/modeling_conditional_detr.py b/src/transformers/models/conditional_detr/modeling_conditional_detr.py index dfde0d9548b..19b1439302e 100644 --- a/src/transformers/models/conditional_detr/modeling_conditional_detr.py +++ b/src/transformers/models/conditional_detr/modeling_conditional_detr.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -68,7 +68,7 @@ class ConditionalDetrDecoderOutput(BaseModelOutputWithCrossAttentions): """ intermediate_hidden_states: Optional[torch.FloatTensor] = None - reference_points: Optional[Tuple[torch.FloatTensor]] = None + reference_points: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -112,7 +112,7 @@ class ConditionalDetrModelOutput(Seq2SeqModelOutput): """ intermediate_hidden_states: Optional[torch.FloatTensor] = None - reference_points: Optional[Tuple[torch.FloatTensor]] = None + reference_points: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -166,17 +166,17 @@ class ConditionalDetrObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -236,18 +236,18 @@ class ConditionalDetrSegmentationOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None pred_masks: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.detr.modeling_detr.DetrFrozenBatchNorm2d with Detr->ConditionalDetr @@ -554,7 +554,7 @@ class DetrAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, spatial_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder @@ -688,7 +688,7 @@ class ConditionalDetrAttention(nn.Module): key_states: Optional[torch.Tensor] = None, value_states: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, _ = hidden_states.size() @@ -1421,7 +1421,7 @@ class ConditionalDetrModel(ConditionalDetrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], ConditionalDetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], ConditionalDetrModelOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1606,11 +1606,11 @@ class ConditionalDetrForObjectDetection(ConditionalDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], ConditionalDetrObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], ConditionalDetrObjectDetectionOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1620,7 +1620,7 @@ class ConditionalDetrForObjectDetection(ConditionalDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -1770,11 +1770,11 @@ class ConditionalDetrForSegmentation(ConditionalDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], ConditionalDetrSegmentationOutput]: + ) -> Union[tuple[torch.FloatTensor], ConditionalDetrSegmentationOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1784,7 +1784,7 @@ class ConditionalDetrForSegmentation(ConditionalDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss, DICE/F-1 loss and Focal loss. List of dicts, each dictionary containing at least the following 3 keys: 'class_labels', 'boxes' and 'masks' (the class labels, bounding boxes and segmentation masks of an image in the batch respectively). The class labels themselves @@ -1993,7 +1993,7 @@ class ConditionalDetrMaskHeadSmallConv(nn.Module): nn.init.kaiming_uniform_(m.weight, a=1) nn.init.constant_(m.bias, 0) - def forward(self, x: Tensor, bbox_mask: Tensor, fpns: List[Tensor]): + def forward(self, x: Tensor, bbox_mask: Tensor, fpns: list[Tensor]): # here we concatenate x, the projected feature map, of shape (batch_size, d_model, height/32, width/32) with # the bbox_mask = the attention maps of shape (batch_size, n_queries, n_heads, height/32, width/32). # We expand the projected feature map to match the number of heads. diff --git a/src/transformers/models/conditional_detr/modular_conditional_detr.py b/src/transformers/models/conditional_detr/modular_conditional_detr.py index 878e52e14e2..a0ab099704e 100644 --- a/src/transformers/models/conditional_detr/modular_conditional_detr.py +++ b/src/transformers/models/conditional_detr/modular_conditional_detr.py @@ -1,4 +1,4 @@ -from typing import List, Tuple, Union +from typing import Union from transformers.models.detr.image_processing_detr_fast import DetrImageProcessorFast @@ -33,7 +33,7 @@ class ConditionalDetrImageProcessorFast(DetrImageProcessorFast): image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logging.warning_once( @@ -66,7 +66,7 @@ class ConditionalDetrImageProcessorFast(DetrImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`ConditionalDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -77,14 +77,14 @@ class ConditionalDetrImageProcessorFast(DetrImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -107,7 +107,7 @@ class ConditionalDetrImageProcessorFast(DetrImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/convbert/modeling_convbert.py b/src/transformers/models/convbert/modeling_convbert.py index 0a8f142d1d3..1a443c575ab 100755 --- a/src/transformers/models/convbert/modeling_convbert.py +++ b/src/transformers/models/convbert/modeling_convbert.py @@ -17,7 +17,7 @@ import math import os from operator import attrgetter -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -336,7 +336,7 @@ class ConvBertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: mixed_query_layer = self.query(hidden_states) batch_size = hidden_states.size(0) # If this is instantiated as a cross-attention module, the keys @@ -458,7 +458,7 @@ class ConvBertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.Tensor, Optional[torch.FloatTensor]]: self_outputs = self.self( hidden_states, attention_mask, @@ -555,7 +555,7 @@ class ConvBertLayer(nn.Module): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.Tensor, Optional[torch.FloatTensor]]: self_attention_outputs = self.attention( hidden_states, attention_mask, @@ -610,7 +610,7 @@ class ConvBertEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -819,7 +819,7 @@ class ConvBertModel(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -922,7 +922,7 @@ class ConvBertForMaskedLM(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1019,7 +1019,7 @@ class ConvBertForSequenceClassification(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1103,7 +1103,7 @@ class ConvBertForMultipleChoice(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1212,7 +1212,7 @@ class ConvBertForTokenClassification(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1279,7 +1279,7 @@ class ConvBertForQuestionAnswering(ConvBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.convbert( diff --git a/src/transformers/models/convbert/modeling_tf_convbert.py b/src/transformers/models/convbert/modeling_tf_convbert.py index d9318f2b128..192b8839927 100644 --- a/src/transformers/models/convbert/modeling_tf_convbert.py +++ b/src/transformers/models/convbert/modeling_tf_convbert.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -869,7 +869,7 @@ class TFConvBertModel(TFConvBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.convbert( input_ids=input_ids, attention_mask=attention_mask, @@ -1000,7 +1000,7 @@ class TFConvBertForMaskedLM(TFConvBertPreTrainedModel, TFMaskedLanguageModelingL return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFMaskedLMOutput]: + ) -> Union[tuple, TFMaskedLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1125,7 +1125,7 @@ class TFConvBertForSequenceClassification(TFConvBertPreTrainedModel, TFSequenceC return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSequenceClassifierOutput]: + ) -> Union[tuple, TFSequenceClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1213,7 +1213,7 @@ class TFConvBertForMultipleChoice(TFConvBertPreTrainedModel, TFMultipleChoiceLos return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFMultipleChoiceModelOutput]: + ) -> Union[tuple, TFMultipleChoiceModelOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1321,7 +1321,7 @@ class TFConvBertForTokenClassification(TFConvBertPreTrainedModel, TFTokenClassif return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFTokenClassifierOutput]: + ) -> Union[tuple, TFTokenClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1405,7 +1405,7 @@ class TFConvBertForQuestionAnswering(TFConvBertPreTrainedModel, TFQuestionAnswer start_positions: tf.Tensor | None = None, end_positions: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFQuestionAnsweringModelOutput]: + ) -> Union[tuple, TFQuestionAnsweringModelOutput]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/convbert/tokenization_convbert.py b/src/transformers/models/convbert/tokenization_convbert.py index 81474361741..e00bf20b17f 100644 --- a/src/transformers/models/convbert/tokenization_convbert.py +++ b/src/transformers/models/convbert/tokenization_convbert.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -187,8 +187,8 @@ class ConvBertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A ConvBERT sequence has the following format: @@ -212,8 +212,8 @@ class ConvBertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -239,7 +239,7 @@ class ConvBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/convbert/tokenization_convbert_fast.py b/src/transformers/models/convbert/tokenization_convbert_fast.py index 854ceaa09ba..e328e7490f4 100644 --- a/src/transformers/models/convbert/tokenization_convbert_fast.py +++ b/src/transformers/models/convbert/tokenization_convbert_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for ConvBERT.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -139,7 +139,7 @@ class ConvBertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/convnext/configuration_convnext.py b/src/transformers/models/convnext/configuration_convnext.py index 2fa8ef54477..f54cba58cf2 100644 --- a/src/transformers/models/convnext/configuration_convnext.py +++ b/src/transformers/models/convnext/configuration_convnext.py @@ -45,9 +45,9 @@ class ConvNextConfig(BackboneConfigMixin, PretrainedConfig): Patch size to use in the patch embedding layer. num_stages (`int`, *optional*, defaults to 4): The number of stages in the model. - hidden_sizes (`List[int]`, *optional*, defaults to [96, 192, 384, 768]): + hidden_sizes (`list[int]`, *optional*, defaults to [96, 192, 384, 768]): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to [3, 3, 9, 3]): + depths (`list[int]`, *optional*, defaults to [3, 3, 9, 3]): Depth (number of blocks) for each stage. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): The non-linear activation function (function or string) in each block. If string, `"gelu"`, `"relu"`, @@ -60,12 +60,12 @@ class ConvNextConfig(BackboneConfigMixin, PretrainedConfig): The initial value for the layer scale. drop_path_rate (`float`, *optional*, defaults to 0.0): The drop rate for stochastic depth. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/convnext/image_processing_convnext.py b/src/transformers/models/convnext/image_processing_convnext.py index 5093c9d33b9..487913d56eb 100644 --- a/src/transformers/models/convnext/image_processing_convnext.py +++ b/src/transformers/models/convnext/image_processing_convnext.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for ConvNeXT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -58,7 +58,7 @@ class ConvNextImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 384}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 384}`): Resolution of the output image after `resize` is applied. If `size["shortest_edge"]` >= 384, the image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"]/crop_pct)`, after which the image is cropped to @@ -78,10 +78,10 @@ class ConvNextImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -91,14 +91,14 @@ class ConvNextImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_pct: Optional[float] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -119,7 +119,7 @@ class ConvNextImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], crop_pct: float, resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -132,7 +132,7 @@ class ConvNextImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary of the form `{"shortest_edge": int}`, specifying the size of the output image. If `size["shortest_edge"]` >= 384 image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"] / crop_pct)`, @@ -190,14 +190,14 @@ class ConvNextImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_pct: Optional[float] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -211,7 +211,7 @@ class ConvNextImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the output image after `resize` has been applied. If `size["shortest_edge"]` >= 384, the image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"]/ crop_pct)`, after which the image is cropped to @@ -227,9 +227,9 @@ class ConvNextImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/convnext/image_processing_convnext_fast.py b/src/transformers/models/convnext/image_processing_convnext_fast.py index 0d38e7f4204..ef06dce9710 100644 --- a/src/transformers/models/convnext/image_processing_convnext_fast.py +++ b/src/transformers/models/convnext/image_processing_convnext_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for ConvNeXT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -84,7 +84,7 @@ class ConvNextImageProcessorFast(BaseImageProcessorFast): def resize( self, image: "torch.Tensor", - size: Dict[str, int], + size: dict[str, int], crop_pct: float, interpolation: PILImageResampling = PILImageResampling.BICUBIC, **kwargs, @@ -95,7 +95,7 @@ class ConvNextImageProcessorFast(BaseImageProcessorFast): Args: image (`torch.Tensor`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary of the form `{"shortest_edge": int}`, specifying the size of the output image. If `size["shortest_edge"]` >= 384 image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"] / crop_pct)`, @@ -141,9 +141,9 @@ class ConvNextImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, - size: Dict[str, int], + size: dict[str, int], crop_pct: float, interpolation: Optional["F.InterpolationMode"], do_center_crop: bool, @@ -151,8 +151,8 @@ class ConvNextImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: # Group images by size for batched resizing diff --git a/src/transformers/models/convnext/modeling_convnext.py b/src/transformers/models/convnext/modeling_convnext.py index 0f36abfb8fc..ccb78451031 100755 --- a/src/transformers/models/convnext/modeling_convnext.py +++ b/src/transformers/models/convnext/modeling_convnext.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch ConvNext model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -179,7 +179,7 @@ class ConvNextStage(nn.Module): in_channels (`int`): Number of input channels. out_channels (`int`): Number of output channels. depth (`int`): Number of residual blocks. - drop_path_rates(`List[float]`): Stochastic depth rates for each layer. + drop_path_rates(`list[float]`): Stochastic depth rates for each layer. """ def __init__(self, config, in_channels, out_channels, kernel_size=2, stride=2, depth=2, drop_path_rates=None): @@ -230,7 +230,7 @@ class ConvNextEncoder(nn.Module): hidden_states: torch.FloatTensor, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: all_hidden_states = () if output_hidden_states else None for i, layer_module in enumerate(self.stages): @@ -295,7 +295,7 @@ class ConvNextModel(ConvNextPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -355,7 +355,7 @@ class ConvNextForImageClassification(ConvNextPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/convnext/modeling_tf_convnext.py b/src/transformers/models/convnext/modeling_tf_convnext.py index af1bae81db4..d2e5b0c6f4a 100644 --- a/src/transformers/models/convnext/modeling_tf_convnext.py +++ b/src/transformers/models/convnext/modeling_tf_convnext.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -226,7 +226,7 @@ class TFConvNextStage(keras.layers.Layer): Number of output channels. depth (`int`): Number of residual blocks. - drop_path_rates(`List[float]`): + drop_path_rates(`list[float]`): Stochastic depth rates for each layer. """ @@ -238,7 +238,7 @@ class TFConvNextStage(keras.layers.Layer): kernel_size: int = 2, stride: int = 2, depth: int = 2, - drop_path_rates: Optional[List[float]] = None, + drop_path_rates: Optional[list[float]] = None, **kwargs, ): super().__init__(**kwargs) @@ -368,7 +368,7 @@ class TFConvNextMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -474,7 +474,7 @@ CONVNEXT_START_DOCSTRING = r""" CONVNEXT_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. @@ -506,7 +506,7 @@ class TFConvNextModel(TFConvNextPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -593,7 +593,7 @@ class TFConvNextForImageClassification(TFConvNextPreTrainedModel, TFSequenceClas return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/convnextv2/configuration_convnextv2.py b/src/transformers/models/convnextv2/configuration_convnextv2.py index 60b631a340c..53f1825ca57 100644 --- a/src/transformers/models/convnextv2/configuration_convnextv2.py +++ b/src/transformers/models/convnextv2/configuration_convnextv2.py @@ -39,9 +39,9 @@ class ConvNextV2Config(BackboneConfigMixin, PretrainedConfig): Patch size to use in the patch embedding layer. num_stages (`int`, *optional*, defaults to 4): The number of stages in the model. - hidden_sizes (`List[int]`, *optional*, defaults to `[96, 192, 384, 768]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[96, 192, 384, 768]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 3, 9, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 3, 9, 3]`): Depth (number of blocks) for each stage. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): The non-linear activation function (function or string) in each block. If string, `"gelu"`, `"relu"`, @@ -54,12 +54,12 @@ class ConvNextV2Config(BackboneConfigMixin, PretrainedConfig): The drop rate for stochastic depth. image_size (`int`, *optional*, defaults to 224): The size (resolution) of each image. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/convnextv2/modeling_convnextv2.py b/src/transformers/models/convnextv2/modeling_convnextv2.py index 194651f3cc0..ef9d282b9ef 100644 --- a/src/transformers/models/convnextv2/modeling_convnextv2.py +++ b/src/transformers/models/convnextv2/modeling_convnextv2.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch ConvNextV2 model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -198,7 +198,7 @@ class ConvNextV2Stage(nn.Module): in_channels (`int`): Number of input channels. out_channels (`int`): Number of output channels. depth (`int`): Number of residual blocks. - drop_path_rates(`List[float]`): Stochastic depth rates for each layer. + drop_path_rates(`list[float]`): Stochastic depth rates for each layer. """ def __init__(self, config, in_channels, out_channels, kernel_size=2, stride=2, depth=2, drop_path_rates=None): @@ -250,7 +250,7 @@ class ConvNextV2Encoder(nn.Module): hidden_states: torch.FloatTensor, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: all_hidden_states = () if output_hidden_states else None for i, layer_module in enumerate(self.stages): @@ -316,7 +316,7 @@ class ConvNextV2Model(ConvNextV2PreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -377,7 +377,7 @@ class ConvNextV2ForImageClassification(ConvNextV2PreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/convnextv2/modeling_tf_convnextv2.py b/src/transformers/models/convnextv2/modeling_tf_convnextv2.py index c27ba2da453..9c2484a1ba7 100644 --- a/src/transformers/models/convnextv2/modeling_tf_convnextv2.py +++ b/src/transformers/models/convnextv2/modeling_tf_convnextv2.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -267,7 +267,7 @@ class TFConvNextV2Stage(keras.layers.Layer): Number of output channels. depth (`int`): Number of residual blocks. - drop_path_rates(`List[float]`): + drop_path_rates(`list[float]`): Stochastic depth rates for each layer. """ @@ -279,7 +279,7 @@ class TFConvNextV2Stage(keras.layers.Layer): kernel_size: int = 2, stride: int = 2, depth: int = 2, - drop_path_rates: Optional[List[float]] = None, + drop_path_rates: Optional[list[float]] = None, **kwargs, ): super().__init__(**kwargs) @@ -369,7 +369,7 @@ class TFConvNextV2Encoder(keras.layers.Layer): hidden_states: tf.Tensor, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, TFBaseModelOutputWithNoAttention]: + ) -> Union[tuple, TFBaseModelOutputWithNoAttention]: all_hidden_states = () if output_hidden_states else None for i, layer_module in enumerate(self.stages): @@ -414,7 +414,7 @@ class TFConvNextV2MainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -522,7 +522,7 @@ CONVNEXTV2_START_DOCSTRING = r""" CONVNEXTV2_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]`, `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]`, `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ConvNextImageProcessor.__call__`] for details. @@ -560,7 +560,7 @@ class TFConvNextV2Model(TFConvNextV2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, tuple[tf.Tensor]]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -631,7 +631,7 @@ class TFConvNextV2ForImageClassification(TFConvNextV2PreTrainedModel, TFSequence return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFImageClassifierOutputWithNoAttention, Tuple[tf.Tensor]]: + ) -> Union[TFImageClassifierOutputWithNoAttention, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/cpm/tokenization_cpm.py b/src/transformers/models/cpm/tokenization_cpm.py index 496c0b1ccb3..fa367aceb5e 100644 --- a/src/transformers/models/cpm/tokenization_cpm.py +++ b/src/transformers/models/cpm/tokenization_cpm.py @@ -17,7 +17,7 @@ import os import unicodedata from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -51,7 +51,7 @@ class CpmTokenizer(PreTrainedTokenizer): cls_token="", mask_token="", additional_special_tokens=["", ""], - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: """ @@ -108,7 +108,7 @@ class CpmTokenizer(PreTrainedTokenizer): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["", ""]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["", ""]`): Additional special tokens used by the tokenizer. Attributes: @@ -201,7 +201,7 @@ class CpmTokenizer(PreTrainedTokenizer): return outputs # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer._tokenize - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string.""" text = self.preprocess_text(text) pieces = self.sp_model.encode(text, out_type=str) @@ -239,8 +239,8 @@ class CpmTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLNet sequence has the following format: @@ -249,13 +249,13 @@ class CpmTokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -265,22 +265,22 @@ class CpmTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -294,8 +294,8 @@ class CpmTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet sequence pair mask has the following format: @@ -308,13 +308,13 @@ class CpmTokenizer(PreTrainedTokenizer): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls_segment_id = [2] @@ -324,7 +324,7 @@ class CpmTokenizer(PreTrainedTokenizer): return len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] + cls_segment_id # Copied from transformers.models.xlnet.tokenization_xlnet.XLNetTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/cpm/tokenization_cpm_fast.py b/src/transformers/models/cpm/tokenization_cpm_fast.py index 48caf28c0a1..adfe804960c 100644 --- a/src/transformers/models/cpm/tokenization_cpm_fast.py +++ b/src/transformers/models/cpm/tokenization_cpm_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import AddedToken, PreTrainedTokenizerFast from ...utils import logging @@ -101,7 +101,7 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["", ""]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["", ""]`): Additional special tokens used by the tokenizer. Attributes: @@ -146,8 +146,8 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.xlnet.tokenization_xlnet_fast.XLNetTokenizerFast.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLNet sequence has the following format: @@ -156,13 +156,13 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -172,8 +172,8 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.xlnet.tokenization_xlnet_fast.XLNetTokenizerFast.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet sequence pair mask has the following format: @@ -186,13 +186,13 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls_segment_id = [2] @@ -202,7 +202,7 @@ class CpmTokenizerFast(PreTrainedTokenizerFast): return len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] + cls_segment_id # Copied from transformers.models.xlnet.tokenization_xlnet_fast.XLNetTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/cpmant/modeling_cpmant.py b/src/transformers/models/cpmant/modeling_cpmant.py index ff7ec674530..f3ecf4930f6 100755 --- a/src/transformers/models/cpmant/modeling_cpmant.py +++ b/src/transformers/models/cpmant/modeling_cpmant.py @@ -15,7 +15,7 @@ """PyTorch CPMAnt""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -86,7 +86,7 @@ class CpmAntAttention(nn.Module): attention_mask: torch.BoolTensor, position_bias: torch.Tensor, output_attentions: Optional[bool] = False, - past_key_values: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor, torch.Tensor]] = None, use_cache: Optional[bool] = None, ): """ @@ -101,7 +101,7 @@ class CpmAntAttention(nn.Module): Provide positional information to self-attention block. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. - past_key_values (`Tuple[torch.Tensor, torch.Tensor]`, *optional*): + past_key_values (`tuple[torch.Tensor, torch.Tensor]`, *optional*): Cached past key and value projection states. use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding @@ -179,7 +179,7 @@ class CpmAntSelfAttentionBlock(nn.Module): attention_mask: torch.Tensor, position_bias: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - past_key_values: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor, torch.Tensor]] = None, use_cache: Optional[bool] = None, ): """ @@ -297,7 +297,7 @@ class CpmAntTransformerBlock(nn.Module): attention_mask: torch.Tensor, position_bias: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - past_key_values: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor, torch.Tensor]] = None, use_cache: Optional[bool] = None, ): """ @@ -310,7 +310,7 @@ class CpmAntTransformerBlock(nn.Module): Provides position information to attention mechanism of shape `(num_heads, seq_len, seq_len)` output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. - past_key_values (`Tuple[torch.Tensor, torch.Tensor])`, *optional*): + past_key_values (`tuple[torch.Tensor, torch.Tensor])`, *optional*): Cached past key and value projection states use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding @@ -347,7 +347,7 @@ class CpmAntEncoder(nn.Module): position_bias: torch.Tensor, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - past_key_values: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor, torch.Tensor]] = None, use_cache: Optional[bool] = None, ): """ @@ -362,7 +362,7 @@ class CpmAntEncoder(nn.Module): Whether or not to return the attentions tensors of all attention layers. output_hidden_states (`bool`, *optional*): Whether or not to return the hidden states of all layers. - past_key_values (`Tuple[torch.Tensor, torch.Tensor])`, *optional*): + past_key_values (`tuple[torch.Tensor, torch.Tensor])`, *optional*): Cached past key and value projection states use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding @@ -589,11 +589,11 @@ class CpmAntModel(CpmAntPreTrainedModel): input_ids: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPast]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPast]: r""" input_ids (`torch.Tensor` of shape `(batch_size, seq_len)`): Indices of input sequence tokens in the vocabulary. @@ -712,7 +712,7 @@ class CpmAntForCausalLM(CpmAntPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[List[Tuple[torch.Tensor, torch.Tensor]]] = None, + past_key_values: Optional[list[tuple[torch.Tensor, torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -720,7 +720,7 @@ class CpmAntForCausalLM(CpmAntPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, attention_mask: Optional[torch.Tensor] = None, # dummy parameter for text-generation pipeline **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" input_ids (`torch.Tensor` of shape `(batch_size, seq_len)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/cpmant/tokenization_cpmant.py b/src/transformers/models/cpmant/tokenization_cpmant.py index e5cc353cc57..98fd4df6add 100644 --- a/src/transformers/models/cpmant/tokenization_cpmant.py +++ b/src/transformers/models/cpmant/tokenization_cpmant.py @@ -16,7 +16,7 @@ import collections import os -from typing import List, Optional, Tuple +from typing import Optional from transformers.utils import is_jieba_available, requires_backends @@ -184,7 +184,7 @@ class CpmAntTokenizer(PreTrainedTokenizer): def check(self, token): return token in self.encoder - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: return "".join(tokens) def _convert_token_to_id(self, token): @@ -195,7 +195,7 @@ class CpmAntTokenizer(PreTrainedTokenizer): """Converts an index (integer) in a token (str) using the vocab.""" return self.decoder.get(index, self.unk_token) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if os.path.isdir(save_directory): vocab_file = os.path.join( save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] @@ -223,8 +223,8 @@ class CpmAntTokenizer(PreTrainedTokenizer): return (vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A CPMAnt sequence has the following format: @@ -232,31 +232,31 @@ class CpmAntTokenizer(PreTrainedTokenizer): - single sequence: `[BOS] Sequence`. Args: - token_ids_0 (`List[int]`): The first tokenized sequence that special tokens will be added. - token_ids_1 (`List[int]`): The optional second tokenized sequence that special tokens will be added. + token_ids_0 (`list[int]`): The first tokenized sequence that special tokens will be added. + token_ids_1 (`list[int]`): The optional second tokenized sequence that special tokens will be added. Returns: - `List[int]`: The model input with special tokens. + `list[int]`: The model input with special tokens. """ if token_ids_1 is None: return [self.bos_token_id] + token_ids_0 return [self.bos_token_id] + token_ids_0 + [self.bos_token_id] + token_ids_1 def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): Optional second list of IDs for sequence pairs. + token_ids_0 (`list[int]`): List of IDs. + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: diff --git a/src/transformers/models/csm/configuration_csm.py b/src/transformers/models/csm/configuration_csm.py index 7882ac0fbf8..6e56c5f7686 100644 --- a/src/transformers/models/csm/configuration_csm.py +++ b/src/transformers/models/csm/configuration_csm.py @@ -101,11 +101,11 @@ class CsmDepthDecoderConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -289,11 +289,11 @@ class CsmConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/csm/generation_csm.py b/src/transformers/models/csm/generation_csm.py index 7afc7c2d60c..c33fd10b3d2 100644 --- a/src/transformers/models/csm/generation_csm.py +++ b/src/transformers/models/csm/generation_csm.py @@ -15,7 +15,7 @@ import os from dataclasses import dataclass -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import torch import torch.nn as nn @@ -68,7 +68,7 @@ class CsmGenerateOutput(GenerateDecoderOnlyOutput): The generated audio. """ - audio: Optional[List[torch.Tensor]] = None + audio: Optional[list[torch.Tensor]] = None class CsmGenerationMixin(GenerationMixin): @@ -90,8 +90,8 @@ class CsmGenerationMixin(GenerationMixin): return kept_criteria def _prepare_generation_config( - self, generation_config: Optional[GenerationConfig], use_model_defaults: Optional[bool] = None, **kwargs: Dict - ) -> Tuple[GenerationConfig, Dict]: + self, generation_config: Optional[GenerationConfig], use_model_defaults: Optional[bool] = None, **kwargs: dict + ) -> tuple[GenerationConfig, dict]: """ This method overrides [~generation.utils.GenerationMixin._prepare_generation_config]. It ensures that the depth decoder generation config is initialized and that passed args as depth_decoder_* are properly handled. @@ -400,14 +400,14 @@ class CsmGenerationMixin(GenerationMixin): through `streamer.put(token_ids)` and the streamer is responsible for any further processing. output_audio (`bool`, *optional*): Whether to return the generated audio. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generation_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. Depth decoder specific kwargs should be prefixed with *depth_decoder_*. Return: - [`CsmGenerateOutput`] or `torch.LongTensor` or `List[torch.FloatTensor]`: A [`CsmGenerateOutput`] + [`CsmGenerateOutput`] or `torch.LongTensor` or `list[torch.FloatTensor]`: A [`CsmGenerateOutput`] (if `return_dict_in_generate=True` or when `config.return_dict_in_generate=True`) or a `torch.LongTensor` when `output_audio=False` - or a `List[torch.FloatTensor]` otherwise. + or a `list[torch.FloatTensor]` otherwise. Example: diff --git a/src/transformers/models/csm/modeling_csm.py b/src/transformers/models/csm/modeling_csm.py index c0c4f5927a5..ad374c86e7a 100644 --- a/src/transformers/models/csm/modeling_csm.py +++ b/src/transformers/models/csm/modeling_csm.py @@ -20,7 +20,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -93,14 +93,14 @@ class CsmOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None depth_decoder_loss: Optional[torch.FloatTensor] = None depth_decoder_logits: torch.FloatTensor = None - depth_decoder_past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - depth_decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - depth_decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + depth_decoder_past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + depth_decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + depth_decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None backbone_loss: Optional[torch.FloatTensor] = None @@ -315,12 +315,12 @@ class CsmAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -376,9 +376,9 @@ class CsmDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -450,7 +450,7 @@ class CsmDepthDecoderModel(CsmPreTrainedModel): output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" backbone_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, backbone_hidden_size)`, *optional*): The last hidden state of the backbone model. Such input is required when the first codebook token (the one generated by the backbone model) @@ -623,7 +623,7 @@ class CsmDepthDecoderForCausalLM(CsmPreTrainedModel, GenerationMixin): backbone_last_hidden_state: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -632,7 +632,7 @@ class CsmDepthDecoderForCausalLM(CsmPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" backbone_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, backbone_hidden_size)`, *optional*): The last hidden state of the backbone model. Such input is required when the first codebook token (the one generated by the backbone model) @@ -1067,7 +1067,7 @@ class CsmForConditionalGeneration(CsmPreTrainedModel, CsmGenerationMixin): attention_mask: Optional[torch.Tensor] = None, input_values_cutoffs: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1076,7 +1076,7 @@ class CsmForConditionalGeneration(CsmPreTrainedModel, CsmGenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CsmOutputWithPast]: + ) -> Union[tuple, CsmOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, num_codebooks) or (batch_size, sequence_length)`): 1. (batch_size, sequence_length): corresponds to the input sequence prepared with the processor from the text prompt. Such input diff --git a/src/transformers/models/csm/modular_csm.py b/src/transformers/models/csm/modular_csm.py index 4322a2a07f8..4bfc6850406 100644 --- a/src/transformers/models/csm/modular_csm.py +++ b/src/transformers/models/csm/modular_csm.py @@ -14,7 +14,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -93,14 +93,14 @@ class CsmOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None depth_decoder_loss: Optional[torch.FloatTensor] = None depth_decoder_logits: torch.FloatTensor = None - depth_decoder_past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - depth_decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - depth_decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + depth_decoder_past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + depth_decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + depth_decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None backbone_loss: Optional[torch.FloatTensor] = None @@ -188,7 +188,7 @@ class CsmDepthDecoderModel(LlamaModel): output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" backbone_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, backbone_hidden_size)`, *optional*): The last hidden state of the backbone model. Such input is required when the first codebook token (the one generated by the backbone model) @@ -371,7 +371,7 @@ class CsmDepthDecoderForCausalLM(LlamaForCausalLM, GenerationMixin): backbone_last_hidden_state: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -380,7 +380,7 @@ class CsmDepthDecoderForCausalLM(LlamaForCausalLM, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" backbone_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, backbone_hidden_size)`, *optional*): The last hidden state of the backbone model. Such input is required when the first codebook token (the one generated by the backbone model) @@ -681,7 +681,7 @@ class CsmForConditionalGeneration(CsmPreTrainedModel, CsmGenerationMixin): attention_mask: Optional[torch.Tensor] = None, input_values_cutoffs: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -690,7 +690,7 @@ class CsmForConditionalGeneration(CsmPreTrainedModel, CsmGenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CsmOutputWithPast]: + ) -> Union[tuple, CsmOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, num_codebooks) or (batch_size, sequence_length)`): 1. (batch_size, sequence_length): corresponds to the input sequence prepared with the processor from the text prompt. Such input diff --git a/src/transformers/models/csm/processing_csm.py b/src/transformers/models/csm/processing_csm.py index 955f73cb363..5cd40dbe559 100644 --- a/src/transformers/models/csm/processing_csm.py +++ b/src/transformers/models/csm/processing_csm.py @@ -15,7 +15,7 @@ import math from pathlib import Path -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import numpy as np @@ -35,7 +35,7 @@ from ...tokenization_utils_base import PreTokenizedInput, TextInput class CsmAudioKwargs(AudioKwargs, total=False): - encoded_length_kwargs: Optional[Dict[str, Any]] + encoded_length_kwargs: Optional[dict[str, Any]] class CsmProcessorKwargs(ProcessingKwargs, total=False): @@ -128,8 +128,8 @@ class CsmProcessor(ProcessorMixin): Args: audio_length (int): The length of the audio sequence. - kernel_sizes (List[int]): The kernel sizes for the convolutional layers. - strides (List[int]): The strides for the convolutional layers. + kernel_sizes (list[int]): The kernel sizes for the convolutional layers. + strides (list[int]): The strides for the convolutional layers. use_causal_conv (bool): Whether to use causal convolutions. """ cur_length = audio_length @@ -163,7 +163,7 @@ class CsmProcessor(ProcessorMixin): def save_audio( self, audio: AudioInput, - saving_path: Union[str, Path, List[Union[str, Path]]], + saving_path: Union[str, Path, list[Union[str, Path]]], **kwargs: Unpack[CsmProcessorKwargs], ): # TODO: @eustlb, this should be in AudioProcessor @@ -196,7 +196,7 @@ class CsmProcessor(ProcessorMixin): def __call__( self, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]], + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]], audio: Optional[AudioInput] = None, output_labels: Optional[bool] = False, depth_decoder_labels_ratio: Optional[float] = 1.0, @@ -210,10 +210,10 @@ class CsmProcessor(ProcessorMixin): to the docstring of the above two methods for more information. Args: - audio (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + audio (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The audio or batch of audio to be prepared. Each audio can be a NumPy array or PyTorch tensor. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/ctrl/modeling_ctrl.py b/src/transformers/models/ctrl/modeling_ctrl.py index 1896d6ea413..883803741d7 100644 --- a/src/transformers/models/ctrl/modeling_ctrl.py +++ b/src/transformers/models/ctrl/modeling_ctrl.py @@ -15,7 +15,7 @@ # limitations under the License. """PyTorch CTRL model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -266,7 +266,7 @@ class CTRLModel(CTRLPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -277,7 +277,7 @@ class CTRLModel(CTRLPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPast]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0].shape[-2]` @@ -451,7 +451,7 @@ class CTRLLMHeadModel(CTRLPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -463,7 +463,7 @@ class CTRLLMHeadModel(CTRLPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithPast]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0].shape[-2]` @@ -567,8 +567,8 @@ class CTRLLMHeadModel(CTRLPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -605,7 +605,7 @@ class CTRLForSequenceClassification(CTRLPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -616,7 +616,7 @@ class CTRLForSequenceClassification(CTRLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0].shape[-2]` diff --git a/src/transformers/models/ctrl/modeling_tf_ctrl.py b/src/transformers/models/ctrl/modeling_tf_ctrl.py index ddb1be17b29..6209ad29cc7 100644 --- a/src/transformers/models/ctrl/modeling_tf_ctrl.py +++ b/src/transformers/models/ctrl/modeling_tf_ctrl.py @@ -17,7 +17,7 @@ from __future__ import annotations -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -303,7 +303,7 @@ class TFCTRLMainLayer(keras.layers.Layer): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -314,7 +314,7 @@ class TFCTRLMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPast]: + ) -> Union[tuple, TFBaseModelOutputWithPast]: # If using past key value states, only the last tokens # should be given as an input if past_key_values is not None: @@ -522,7 +522,7 @@ CTRL_INPUTS_DOCSTRING = r""" [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - past (`List[tf.Tensor]` of length `config.n_layers`): + past (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see `past` output below). Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -594,7 +594,7 @@ class TFCTRLModel(TFCTRLPreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -605,7 +605,7 @@ class TFCTRLModel(TFCTRLPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPast]: + ) -> Union[tuple, TFBaseModelOutputWithPast]: outputs = self.transformer( input_ids=input_ids, past_key_values=past_key_values, @@ -722,7 +722,7 @@ class TFCTRLLMHeadModel(TFCTRLPreTrainedModel, TFCausalLanguageModelingLoss): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -734,7 +734,7 @@ class TFCTRLLMHeadModel(TFCTRLPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFCausalLMOutputWithPast]: + ) -> Union[tuple, TFCausalLMOutputWithPast]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., @@ -835,7 +835,7 @@ class TFCTRLForSequenceClassification(TFCTRLPreTrainedModel, TFSequenceClassific def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -847,7 +847,7 @@ class TFCTRLForSequenceClassification(TFCTRLPreTrainedModel, TFSequenceClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSequenceClassifierOutput]: + ) -> Union[tuple, TFSequenceClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/ctrl/tokenization_ctrl.py b/src/transformers/models/ctrl/tokenization_ctrl.py index 66dae2b05fa..5b7935e6404 100644 --- a/src/transformers/models/ctrl/tokenization_ctrl.py +++ b/src/transformers/models/ctrl/tokenization_ctrl.py @@ -16,7 +16,7 @@ import json import os -from typing import Optional, Tuple +from typing import Optional import regex as re @@ -212,7 +212,7 @@ class CTRLTokenizer(PreTrainedTokenizer): out_string = " ".join(tokens).replace("@@ ", "").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/cvt/configuration_cvt.py b/src/transformers/models/cvt/configuration_cvt.py index 38cba6874f6..ba3f6b33962 100644 --- a/src/transformers/models/cvt/configuration_cvt.py +++ b/src/transformers/models/cvt/configuration_cvt.py @@ -34,43 +34,43 @@ class CvtConfig(PretrainedConfig): Args: num_channels (`int`, *optional*, defaults to 3): The number of input channels. - patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3]`): + patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3]`): The kernel size of each encoder's patch embedding. - patch_stride (`List[int]`, *optional*, defaults to `[4, 2, 2]`): + patch_stride (`list[int]`, *optional*, defaults to `[4, 2, 2]`): The stride size of each encoder's patch embedding. - patch_padding (`List[int]`, *optional*, defaults to `[2, 1, 1]`): + patch_padding (`list[int]`, *optional*, defaults to `[2, 1, 1]`): The padding size of each encoder's patch embedding. - embed_dim (`List[int]`, *optional*, defaults to `[64, 192, 384]`): + embed_dim (`list[int]`, *optional*, defaults to `[64, 192, 384]`): Dimension of each of the encoder blocks. - num_heads (`List[int]`, *optional*, defaults to `[1, 3, 6]`): + num_heads (`list[int]`, *optional*, defaults to `[1, 3, 6]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - depth (`List[int]`, *optional*, defaults to `[1, 2, 10]`): + depth (`list[int]`, *optional*, defaults to `[1, 2, 10]`): The number of layers in each encoder block. - mlp_ratios (`List[float]`, *optional*, defaults to `[4.0, 4.0, 4.0, 4.0]`): + mlp_ratios (`list[float]`, *optional*, defaults to `[4.0, 4.0, 4.0, 4.0]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. - attention_drop_rate (`List[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`): + attention_drop_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`): The dropout ratio for the attention probabilities. - drop_rate (`List[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`): + drop_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.0]`): The dropout ratio for the patch embeddings probabilities. - drop_path_rate (`List[float]`, *optional*, defaults to `[0.0, 0.0, 0.1]`): + drop_path_rate (`list[float]`, *optional*, defaults to `[0.0, 0.0, 0.1]`): The dropout probability for stochastic depth, used in the blocks of the Transformer encoder. - qkv_bias (`List[bool]`, *optional*, defaults to `[True, True, True]`): + qkv_bias (`list[bool]`, *optional*, defaults to `[True, True, True]`): The bias bool for query, key and value in attentions - cls_token (`List[bool]`, *optional*, defaults to `[False, False, True]`): + cls_token (`list[bool]`, *optional*, defaults to `[False, False, True]`): Whether or not to add a classification token to the output of each of the last 3 stages. - qkv_projection_method (`List[string]`, *optional*, defaults to ["dw_bn", "dw_bn", "dw_bn"]`): + qkv_projection_method (`list[string]`, *optional*, defaults to ["dw_bn", "dw_bn", "dw_bn"]`): The projection method for query, key and value Default is depth-wise convolutions with batch norm. For Linear projection use "avg". - kernel_qkv (`List[int]`, *optional*, defaults to `[3, 3, 3]`): + kernel_qkv (`list[int]`, *optional*, defaults to `[3, 3, 3]`): The kernel size for query, key and value in attention layer - padding_kv (`List[int]`, *optional*, defaults to `[1, 1, 1]`): + padding_kv (`list[int]`, *optional*, defaults to `[1, 1, 1]`): The padding size for key and value in attention layer - stride_kv (`List[int]`, *optional*, defaults to `[2, 2, 2]`): + stride_kv (`list[int]`, *optional*, defaults to `[2, 2, 2]`): The stride size for key and value in attention layer - padding_q (`List[int]`, *optional*, defaults to `[1, 1, 1]`): + padding_q (`list[int]`, *optional*, defaults to `[1, 1, 1]`): The padding size for query in attention layer - stride_q (`List[int]`, *optional*, defaults to `[1, 1, 1]`): + stride_q (`list[int]`, *optional*, defaults to `[1, 1, 1]`): The stride size for query in attention layer initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. diff --git a/src/transformers/models/cvt/modeling_cvt.py b/src/transformers/models/cvt/modeling_cvt.py index eb9294dd432..d0cd4b554b1 100644 --- a/src/transformers/models/cvt/modeling_cvt.py +++ b/src/transformers/models/cvt/modeling_cvt.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -50,7 +50,7 @@ class BaseModelOutputWithCLSToken(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None cls_token_value: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None # Copied from transformers.models.beit.modeling_beit.drop_path @@ -560,7 +560,7 @@ class CvtModel(CvtPreTrainedModel): pixel_values: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithCLSToken]: + ) -> Union[tuple, BaseModelOutputWithCLSToken]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -614,7 +614,7 @@ class CvtForImageClassification(CvtPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/cvt/modeling_tf_cvt.py b/src/transformers/models/cvt/modeling_tf_cvt.py index 74202d88062..54303e7048f 100644 --- a/src/transformers/models/cvt/modeling_tf_cvt.py +++ b/src/transformers/models/cvt/modeling_tf_cvt.py @@ -18,7 +18,7 @@ from __future__ import annotations import collections.abc from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -67,7 +67,7 @@ class TFBaseModelOutputWithCLSToken(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None cls_token_value: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None class TFCvtDropPath(keras.layers.Layer): @@ -769,7 +769,7 @@ class TFCvtEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithCLSToken, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithCLSToken, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None hidden_state = pixel_values # When running on CPU, `keras.layers.Conv2D` doesn't support (batch_size, num_channels, height, width) @@ -824,7 +824,7 @@ class TFCvtMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithCLSToken, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithCLSToken, tuple[tf.Tensor]]: if pixel_values is None: raise ValueError("You have to specify pixel_values") @@ -896,7 +896,7 @@ TFCVT_START_DOCSTRING = r""" TFCVT_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`CvtImageProcessor.__call__`] for details. @@ -932,7 +932,7 @@ class TFCvtModel(TFCvtPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithCLSToken, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithCLSToken, tuple[tf.Tensor]]: r""" Returns: @@ -1018,7 +1018,7 @@ class TFCvtForImageClassification(TFCvtPreTrainedModel, TFSequenceClassification output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFImageClassifierOutputWithNoAttention, Tuple[tf.Tensor]]: + ) -> Union[TFImageClassifierOutputWithNoAttention, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/d_fine/configuration_d_fine.py b/src/transformers/models/d_fine/configuration_d_fine.py index 8ca98e348bb..3f408cb7859 100644 --- a/src/transformers/models/d_fine/configuration_d_fine.py +++ b/src/transformers/models/d_fine/configuration_d_fine.py @@ -67,7 +67,7 @@ class DFineConfig(PretrainedConfig): Dimension of the layers in hybrid encoder. encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`): Multi level features input for encoder. - feat_strides (`List[int]`, *optional*, defaults to `[8, 16, 32]`): + feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`): Strides used in each feature map. encoder_layers (`int`, *optional*, defaults to 1): Total of layers to be used by the encoder. @@ -79,7 +79,7 @@ class DFineConfig(PretrainedConfig): The ratio for all dropout layers. activation_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for activations inside the fully connected layer. - encode_proj_layers (`List[int]`, *optional*, defaults to `[2]`): + encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`): Indexes of the projected layers to be used in the encoder. positional_encoding_temperature (`int`, *optional*, defaults to 10000): The temperature parameter used to create the positional encodings. @@ -89,7 +89,7 @@ class DFineConfig(PretrainedConfig): activation_function (`str`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the general layer. If string, `"gelu"`, `"relu"`, `"silu"` and `"gelu_new"` are supported. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to computes the effective height and width of the position embeddings after taking into account the stride. normalize_before (`bool`, *optional*, defaults to `False`): @@ -126,7 +126,7 @@ class DFineConfig(PretrainedConfig): Scale or magnitude of noise to be added to the bounding boxes. learn_initial_query (`bool`, *optional*, defaults to `False`): Indicates whether the initial query embeddings for the decoder should be learned during training - anchor_image_size (`Tuple[int, int]`, *optional*): + anchor_image_size (`tuple[int, int]`, *optional*): Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied. with_box_refine (`bool`, *optional*, defaults to `True`): Whether to apply iterative bounding box refinement, where each decoder layer refines the bounding boxes diff --git a/src/transformers/models/d_fine/modeling_d_fine.py b/src/transformers/models/d_fine/modeling_d_fine.py index 439bf9d20f4..b2c27619092 100644 --- a/src/transformers/models/d_fine/modeling_d_fine.py +++ b/src/transformers/models/d_fine/modeling_d_fine.py @@ -20,7 +20,7 @@ # limitations under the License. import math from dataclasses import dataclass -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn.functional as F @@ -42,7 +42,7 @@ def multi_scale_deformable_attention_v2( value_spatial_shapes: Tensor, sampling_locations: Tensor, attention_weights: Tensor, - num_points_list: List[int], + num_points_list: list[int], method="default", ) -> Tensor: batch_size, _, num_heads, hidden_dim = value.shape @@ -259,7 +259,7 @@ class DFineMultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -492,18 +492,18 @@ class DFineModelOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None init_reference_points: Optional[torch.FloatTensor] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None @dataclass @@ -580,28 +580,28 @@ class DFineObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_logits: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - init_reference_points: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + init_reference_points: Optional[tuple[torch.FloatTensor]] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None class DFineFrozenBatchNorm2d(nn.Module): @@ -802,7 +802,7 @@ def get_contrastive_denoising_training_group( Creates a contrastive denoising training group using ground-truth samples. It adds noise to labels and boxes. Args: - targets (`List[dict]`): + targets (`list[dict]`): The target objects, each containing 'class_labels' and 'boxes' for objects in an image. num_classes (`int`): Total number of classes in the dataset. @@ -1048,9 +1048,9 @@ class DFineDecoderOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None def weighting_function(max_num_bins: int, up: torch.Tensor, reg_scale: int) -> torch.Tensor: @@ -1443,11 +1443,11 @@ class DFineModel(DFinePreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DFineModelOutput]: + ) -> Union[tuple[torch.FloatTensor], DFineModelOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1455,7 +1455,7 @@ class DFineModel(DFinePreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -1712,12 +1712,12 @@ class DFineForObjectDetection(DFinePreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **loss_kwargs, - ) -> Union[Tuple[torch.FloatTensor], DFineObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], DFineObjectDetectionOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1725,7 +1725,7 @@ class DFineForObjectDetection(DFinePreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/d_fine/modular_d_fine.py b/src/transformers/models/d_fine/modular_d_fine.py index e7c6b5a81c5..03791838c27 100644 --- a/src/transformers/models/d_fine/modular_d_fine.py +++ b/src/transformers/models/d_fine/modular_d_fine.py @@ -86,7 +86,7 @@ class DFineConfig(PretrainedConfig): Dimension of the layers in hybrid encoder. encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`): Multi level features input for encoder. - feat_strides (`List[int]`, *optional*, defaults to `[8, 16, 32]`): + feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`): Strides used in each feature map. encoder_layers (`int`, *optional*, defaults to 1): Total of layers to be used by the encoder. @@ -98,7 +98,7 @@ class DFineConfig(PretrainedConfig): The ratio for all dropout layers. activation_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for activations inside the fully connected layer. - encode_proj_layers (`List[int]`, *optional*, defaults to `[2]`): + encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`): Indexes of the projected layers to be used in the encoder. positional_encoding_temperature (`int`, *optional*, defaults to 10000): The temperature parameter used to create the positional encodings. @@ -108,7 +108,7 @@ class DFineConfig(PretrainedConfig): activation_function (`str`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the general layer. If string, `"gelu"`, `"relu"`, `"silu"` and `"gelu_new"` are supported. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to computes the effective height and width of the position embeddings after taking into account the stride. normalize_before (`bool`, *optional*, defaults to `False`): @@ -145,7 +145,7 @@ class DFineConfig(PretrainedConfig): Scale or magnitude of noise to be added to the bounding boxes. learn_initial_query (`bool`, *optional*, defaults to `False`): Indicates whether the initial query embeddings for the decoder should be learned during training - anchor_image_size (`Tuple[int, int]`, *optional*): + anchor_image_size (`tuple[int, int]`, *optional*): Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied. with_box_refine (`bool`, *optional*, defaults to `True`): Whether to apply iterative bounding box refinement, where each decoder layer refines the bounding boxes diff --git a/src/transformers/models/dab_detr/modeling_dab_detr.py b/src/transformers/models/dab_detr/modeling_dab_detr.py index 03c513f0d58..a9369b8700d 100644 --- a/src/transformers/models/dab_detr/modeling_dab_detr.py +++ b/src/transformers/models/dab_detr/modeling_dab_detr.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -69,7 +69,7 @@ class DabDetrDecoderOutput(BaseModelOutputWithCrossAttentions): """ intermediate_hidden_states: Optional[torch.FloatTensor] = None - reference_points: Optional[Tuple[torch.FloatTensor]] = None + reference_points: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -114,7 +114,7 @@ class DabDetrModelOutput(Seq2SeqModelOutput): """ intermediate_hidden_states: Optional[torch.FloatTensor] = None - reference_points: Optional[Tuple[torch.FloatTensor]] = None + reference_points: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -168,17 +168,17 @@ class DabDetrObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.detr.modeling_detr.DetrFrozenBatchNorm2d with Detr->DabDetr @@ -432,7 +432,7 @@ class DetrAttention(nn.Module): object_queries: Optional[torch.Tensor] = None, key_value_states: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, q_len, embed_dim = hidden_states.size() # add position embeddings to the hidden states before projecting to queries and keys @@ -511,7 +511,7 @@ class DabDetrAttention(nn.Module): key_states: Optional[torch.Tensor] = None, value_states: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, q_len, _ = hidden_states.size() @@ -1290,7 +1290,7 @@ class DabDetrModel(DabDetrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DabDetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], DabDetrModelOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1541,11 +1541,11 @@ class DabDetrForObjectDetection(DabDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DabDetrObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], DabDetrObjectDetectionOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1555,7 +1555,7 @@ class DabDetrForObjectDetection(DabDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/dac/configuration_dac.py b/src/transformers/models/dac/configuration_dac.py index dbbefd3ccb5..bdf07cef45b 100644 --- a/src/transformers/models/dac/configuration_dac.py +++ b/src/transformers/models/dac/configuration_dac.py @@ -38,7 +38,7 @@ class DacConfig(PretrainedConfig): Args: encoder_hidden_size (`int`, *optional*, defaults to 64): Intermediate representation dimension for the encoder. - downsampling_ratios (`List[int]`, *optional*, defaults to `[2, 4, 8, 8]`): + downsampling_ratios (`list[int]`, *optional*, defaults to `[2, 4, 8, 8]`): Ratios for downsampling in the encoder. These are used in reverse order for upsampling in the decoder. decoder_hidden_size (`int`, *optional*, defaults to 1536): Intermediate representation dimension for the decoder. diff --git a/src/transformers/models/dac/feature_extraction_dac.py b/src/transformers/models/dac/feature_extraction_dac.py index 8e5bfadb614..516beb16719 100644 --- a/src/transformers/models/dac/feature_extraction_dac.py +++ b/src/transformers/models/dac/feature_extraction_dac.py @@ -14,7 +14,7 @@ # limitations under the License. """Feature extractor class for DAC""" -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -59,7 +59,7 @@ class DacFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_audio: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_audio: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Optional[Union[bool, str, PaddingStrategy]] = None, truncation: Optional[bool] = False, max_length: Optional[int] = None, @@ -70,7 +70,7 @@ class DacFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_audio (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_audio (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be processed. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. The numpy array must be of shape `(num_samples,)` for mono audio (`feature_size = 1`), or `(2, num_samples)` for stereo audio diff --git a/src/transformers/models/data2vec/configuration_data2vec_audio.py b/src/transformers/models/data2vec/configuration_data2vec_audio.py index 36ed814bb7e..3d88a9de654 100644 --- a/src/transformers/models/data2vec/configuration_data2vec_audio.py +++ b/src/transformers/models/data2vec/configuration_data2vec_audio.py @@ -71,13 +71,13 @@ class Data2VecAudioConfig(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -124,13 +124,13 @@ class Data2VecAudioConfig(PretrainedConfig): instance of [`Data2VecAudioForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 256): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/data2vec/configuration_data2vec_vision.py b/src/transformers/models/data2vec/configuration_data2vec_vision.py index 0b2a4563939..2de256f9d7d 100644 --- a/src/transformers/models/data2vec/configuration_data2vec_vision.py +++ b/src/transformers/models/data2vec/configuration_data2vec_vision.py @@ -75,9 +75,9 @@ class Data2VecVisionConfig(PretrainedConfig): use_mean_pooling (`bool`, *optional*, defaults to `True`): Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the CLS token, before applying the classification head. - out_indices (`List[int]`, *optional*, defaults to `[3, 5, 7, 11]`): + out_indices (`list[int]`, *optional*, defaults to `[3, 5, 7, 11]`): Indices of the feature maps to use for semantic segmentation. - pool_scales (`Tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): + pool_scales (`tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): Pooling scales used in Pooling Pyramid Module applied on the last feature map. use_auxiliary_head (`bool`, *optional*, defaults to `True`): Whether to use an auxiliary head during training. diff --git a/src/transformers/models/data2vec/modeling_data2vec_audio.py b/src/transformers/models/data2vec/modeling_data2vec_audio.py index 1968749fd83..940af308cd7 100755 --- a/src/transformers/models/data2vec/modeling_data2vec_audio.py +++ b/src/transformers/models/data2vec/modeling_data2vec_audio.py @@ -21,7 +21,7 @@ import math import warnings -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -249,14 +249,14 @@ class Data2VecAudioAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -623,7 +623,7 @@ class Data2VecAudioPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -825,7 +825,7 @@ class Data2VecAudioModel(Data2VecAudioPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Data2VecAudioBaseModelOutput]: + ) -> Union[tuple, Data2VecAudioBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -939,7 +939,7 @@ class Data2VecAudioForCTC(Data2VecAudioPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1062,11 +1062,11 @@ class Data2VecAudioForSequenceClassification(Data2VecAudioPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Data2VecAudioProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1176,11 +1176,11 @@ class Data2VecAudioForAudioFrameClassification(Data2VecAudioPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Data2VecAudioProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1358,11 +1358,11 @@ class Data2VecAudioForXVector(Data2VecAudioPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Data2VecAudioProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/data2vec/modeling_data2vec_text.py b/src/transformers/models/data2vec/modeling_data2vec_text.py index d957e642354..0d7e8513490 100644 --- a/src/transformers/models/data2vec/modeling_data2vec_text.py +++ b/src/transformers/models/data2vec/modeling_data2vec_text.py @@ -15,7 +15,7 @@ """PyTorch Data2VecText model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -176,9 +176,9 @@ class Data2VecTextSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -326,9 +326,9 @@ class Data2VecTextAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -397,9 +397,9 @@ class Data2VecTextLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -476,12 +476,12 @@ class Data2VecTextEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -656,12 +656,12 @@ class Data2VecTextModel(Data2VecTextPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -795,13 +795,13 @@ class Data2VecTextForCausalLM(Data2VecTextPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -918,7 +918,7 @@ class Data2VecTextForMaskedLM(Data2VecTextPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1025,7 +1025,7 @@ class Data2VecTextForSequenceClassification(Data2VecTextPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1110,7 +1110,7 @@ class Data2VecTextForMultipleChoice(Data2VecTextPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1219,7 +1219,7 @@ class Data2VecTextForTokenClassification(Data2VecTextPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1311,7 +1311,7 @@ class Data2VecTextForQuestionAnswering(Data2VecTextPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.data2vec_text( diff --git a/src/transformers/models/data2vec/modeling_data2vec_vision.py b/src/transformers/models/data2vec/modeling_data2vec_vision.py index 18f589773fd..8762fedb102 100644 --- a/src/transformers/models/data2vec/modeling_data2vec_vision.py +++ b/src/transformers/models/data2vec/modeling_data2vec_vision.py @@ -18,7 +18,7 @@ import collections.abc import math import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -283,8 +283,8 @@ class Data2VecVisionSelfAttention(nn.Module): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -339,8 +339,8 @@ class Data2VecVisionSdpaSelfAttention(Data2VecVisionSelfAttention): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: if output_attentions or head_mask is not None: logger.warning_once( "`Data2VecVisionSdpaSelfAttention` is used but `torch.nn.functional.scaled_dot_product_attention` does not " @@ -453,8 +453,8 @@ class Data2VecVisionAttention(nn.Module): output_attentions: bool = False, relative_position_bias: Optional["Data2VecVisionRelativePositionBias"] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: self_outputs = self.attention( hidden_states, head_mask, output_attentions, relative_position_bias, interpolate_pos_encoding, resolution ) @@ -527,8 +527,8 @@ class Data2VecVisionLayer(nn.Module): output_attentions: bool = False, relative_position_bias: Optional[torch.Tensor] = None, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int]] = None, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + resolution: Optional[tuple[int]] = None, + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in Data2VecVision, layernorm is applied before self-attention head_mask, @@ -576,7 +576,7 @@ class Data2VecVisionRelativePositionBias(nn.Module): # cls to token & token 2 cls & cls to cls @compile_compatible_method_lru_cache(maxsize=10) - def generate_relative_position_index(self, window_size: Tuple[int, int]) -> torch.Tensor: + def generate_relative_position_index(self, window_size: tuple[int, int]) -> torch.Tensor: """ This method creates the relative position index, modified to support arbitrary window sizes, as introduced in [MiDaS v3.1](https://huggingface.co/papers/2307.14460). @@ -678,7 +678,7 @@ class Data2VecVisionEncoder(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, interpolate_pos_encoding: bool = False, - resolution: Optional[Tuple[int, int]] = None, + resolution: Optional[tuple[int, int]] = None, return_dict: bool = True, ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None @@ -983,10 +983,10 @@ class Data2VecVisionConvModule(nn.Module): self, in_channels: int, out_channels: int, - kernel_size: Union[int, Tuple[int, int]], - padding: Union[int, Tuple[int, int], str] = 0, + kernel_size: Union[int, tuple[int, int]], + padding: Union[int, tuple[int, int], str] = 0, bias: bool = False, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, ) -> None: super().__init__() self.conv = nn.Conv2d( @@ -1041,7 +1041,7 @@ class Data2VecVisionPyramidPoolingModule(nn.Module): Based on OpenMMLab's implementation, found in https://github.com/open-mmlab/mmsegmentation. """ - def __init__(self, pool_scales: Tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: + def __init__(self, pool_scales: tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: super().__init__() self.pool_scales = pool_scales self.align_corners = align_corners @@ -1055,7 +1055,7 @@ class Data2VecVisionPyramidPoolingModule(nn.Module): self.blocks.append(block) self.add_module(str(i), block) - def forward(self, x: torch.Tensor) -> List[torch.Tensor]: + def forward(self, x: torch.Tensor) -> list[torch.Tensor]: ppm_outs = [] for ppm in self.blocks: ppm_out = ppm(x) @@ -1173,7 +1173,7 @@ class Data2VecVisionFCNHead(nn.Module): config: Data2VecVisionConfig, in_index: int = 2, kernel_size: int = 3, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, ) -> None: super().__init__() self.in_channels = config.hidden_size diff --git a/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py b/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py index 8f8558889d4..399da1d9d83 100644 --- a/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py +++ b/src/transformers/models/data2vec/modeling_tf_data2vec_vision.py @@ -19,7 +19,7 @@ from __future__ import annotations import collections.abc import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -92,8 +92,8 @@ class TFData2VecVisionModelOutputWithPooling(TFBaseModelOutputWithPooling): last_hidden_state: Optional[tf.Tensor] = None pooler_output: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None class TFData2VecVisionDropPath(keras.layers.Layer): @@ -308,7 +308,7 @@ class TFData2VecVisionSelfAttention(keras.layers.Layer): output_attentions: bool, relative_position_bias: Optional["TFData2VecVisionRelativePositionBias"] = None, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) mixed_key_layer = self.key(inputs=hidden_states) @@ -418,7 +418,7 @@ class TFData2VecVisionAttention(keras.layers.Layer): output_attentions: bool, relative_position_bias: Optional["TFData2VecVisionRelativePositionBias"] = None, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.attention( hidden_states=input_tensor, head_mask=head_mask, @@ -572,7 +572,7 @@ class TFData2VecVisionLayer(keras.layers.Layer): output_attentions: bool, relative_position_bias: Optional["TFData2VecVisionRelativePositionBias"] = None, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_attention_outputs = self.attention( # in Data2VecVision, layernorm is applied before self-attention input_tensor=self.layernorm_before(inputs=hidden_states), @@ -938,7 +938,7 @@ DATA2VEC_VISION_START_DOCSTRING = r""" DATA2VEC_VISION_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`BeitImageProcessor.__call__`] for details. @@ -1121,10 +1121,10 @@ class TFData2VecVisionConvModule(keras.layers.Layer): self, in_channels: int, out_channels: int, - kernel_size: Union[int, Tuple[int, int]], + kernel_size: Union[int, tuple[int, int]], padding: str = "valid", bias: bool = False, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, **kwargs, ) -> None: super().__init__(**kwargs) @@ -1160,7 +1160,7 @@ class TFData2VecVisionConvModule(keras.layers.Layer): class TFAdaptiveAvgPool2D(keras.layers.Layer): - def __init__(self, output_dims: Tuple[int, int], input_ordering: str = "NHWC", **kwargs): + def __init__(self, output_dims: tuple[int, int], input_ordering: str = "NHWC", **kwargs): super().__init__(**kwargs) self.output_dims = output_dims self.input_ordering = input_ordering @@ -1295,7 +1295,7 @@ class TFData2VecVisionPyramidPoolingModule(keras.layers.Layer): Based on OpenMMLab's implementation, found in https://github.com/open-mmlab/mmsegmentation. """ - def __init__(self, pool_scales: Tuple[int, ...], in_channels: int, out_channels: int, **kwargs) -> None: + def __init__(self, pool_scales: tuple[int, ...], in_channels: int, out_channels: int, **kwargs) -> None: super().__init__(**kwargs) self.pool_scales = pool_scales self.in_channels = in_channels @@ -1313,7 +1313,7 @@ class TFData2VecVisionPyramidPoolingModule(keras.layers.Layer): ] ) - def call(self, x: tf.Tensor) -> List[tf.Tensor]: + def call(self, x: tf.Tensor) -> list[tf.Tensor]: ppm_outs = [] inputs = x @@ -1462,7 +1462,7 @@ class TFData2VecVisionFCNHead(keras.layers.Layer): config: Data2VecVisionConfig, in_index: int = 2, kernel_size: int = 3, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, **kwargs, ) -> None: super().__init__(**kwargs) diff --git a/src/transformers/models/dbrx/modeling_dbrx.py b/src/transformers/models/dbrx/modeling_dbrx.py index 725397a1228..097370a46cd 100644 --- a/src/transformers/models/dbrx/modeling_dbrx.py +++ b/src/transformers/models/dbrx/modeling_dbrx.py @@ -15,7 +15,7 @@ """PyTorch DBRX model.""" import math -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -135,7 +135,7 @@ def load_balancing_loss_func( experts is too unbalanced. Args: - gate_logits (Union[`torch.Tensor`, Tuple[torch.Tensor]): + gate_logits (Union[`torch.Tensor`, tuple[torch.Tensor]): Logits from the `gate`, should be a tuple of model.config.num_hidden_layers tensors of shape [batch_size X sequence_length, num_experts]. num_experts (`int`): @@ -248,7 +248,7 @@ class DbrxAttention(nn.Module): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Any, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: bsz, q_len, _ = hidden_states.size() qkv_states = self.Wqkv(hidden_states) @@ -333,7 +333,7 @@ class DbrxFlashAttention2(DbrxAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Any, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -443,7 +443,7 @@ class DbrxSdpaAttention(DbrxAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -551,7 +551,7 @@ class DbrxNormAttentionNorm(nn.Module): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Any, - ) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: + ) -> tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: residual_states = hidden_states hidden_states = self.norm_1(hidden_states).to(hidden_states.dtype) @@ -593,7 +593,7 @@ class DbrxRouter(nn.Module): self.layer = nn.Linear(self.hidden_size, self.moe_num_experts, bias=False) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.LongTensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.LongTensor]: if self.training and self.moe_jitter_eps is not None: hidden_states *= torch.empty_like(hidden_states).uniform_( 1.0 - self.moe_jitter_eps, 1.0 + self.moe_jitter_eps @@ -713,7 +713,7 @@ class DbrxFFN(nn.Module): ffn_act_fn=ffn_config.ffn_act_fn, ) - def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: weights, top_weights, top_experts = self.router(x) out = self.experts(x, weights, top_weights, top_experts) return out, weights @@ -743,13 +743,13 @@ class DbrxBlock(nn.Module): cache_position: Optional[torch.LongTensor] = None, **kwargs: Any, ) -> Union[ - Tuple[torch.Tensor], - Tuple[torch.Tensor, Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[Cache]], - Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]], - Tuple[torch.Tensor, Optional[torch.Tensor], Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[Cache], Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache], Optional[torch.Tensor]], + tuple[torch.Tensor], + tuple[torch.Tensor, Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[Cache]], + tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]], + tuple[torch.Tensor, Optional[torch.Tensor], Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[Cache], Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache], Optional[torch.Tensor]], ]: """Forward function for DbrxBlock. @@ -877,7 +877,7 @@ class DbrxModel(DbrxPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple, MoeModelOutputWithPast]: + ) -> Union[tuple, MoeModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1180,7 +1180,7 @@ class DbrxForCausalLM(DbrxPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + ) -> Union[tuple, MoeCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/deberta/configuration_deberta.py b/src/transformers/models/deberta/configuration_deberta.py index 191ba7c9bc4..3e23a73a8c3 100644 --- a/src/transformers/models/deberta/configuration_deberta.py +++ b/src/transformers/models/deberta/configuration_deberta.py @@ -78,7 +78,7 @@ class DebertaConfig(PretrainedConfig): The value used to pad input_ids. position_biased_input (`bool`, *optional*, defaults to `True`): Whether add absolute position embedding to content embedding. - pos_att_type (`List[str]`, *optional*): + pos_att_type (`list[str]`, *optional*): The type of relative position attention, it can be a combination of `["p2c", "c2p"]`, e.g. `["p2c"]`, `["p2c", "c2p"]`. layer_norm_eps (`float`, *optional*, defaults to 1e-12): diff --git a/src/transformers/models/deberta/modeling_deberta.py b/src/transformers/models/deberta/modeling_deberta.py index b23c189befd..eef11f7ec34 100644 --- a/src/transformers/models/deberta/modeling_deberta.py +++ b/src/transformers/models/deberta/modeling_deberta.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch DeBERTa model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -210,7 +210,7 @@ class DisentangledSelfAttention(nn.Module): query_states: Optional[torch.Tensor] = None, relative_pos: Optional[torch.Tensor] = None, rel_embeddings: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ Call the module @@ -442,7 +442,7 @@ class DebertaAttention(nn.Module): query_states=None, relative_pos=None, rel_embeddings=None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: self_output, att_matrix = self.self( hidden_states, attention_mask, @@ -507,7 +507,7 @@ class DebertaLayer(nn.Module): relative_pos=None, rel_embeddings=None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: attention_output, att_matrix = self.attention( hidden_states, attention_mask, @@ -573,7 +573,7 @@ class DebertaEncoder(nn.Module): attention_mask = self.get_attention_mask(attention_mask) relative_pos = self.get_rel_pos(hidden_states, query_states, relative_pos) - all_hidden_states: Optional[Tuple[torch.Tensor]] = (hidden_states,) if output_hidden_states else None + all_hidden_states: Optional[tuple[torch.Tensor]] = (hidden_states,) if output_hidden_states else None all_attentions = () if output_attentions else None next_kv = hidden_states @@ -683,7 +683,7 @@ class DebertaModel(DebertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -889,7 +889,7 @@ class DebertaForMaskedLM(DebertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -998,7 +998,7 @@ class DebertaForSequenceClassification(DebertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1092,7 +1092,7 @@ class DebertaForTokenClassification(DebertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1154,7 +1154,7 @@ class DebertaForQuestionAnswering(DebertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.deberta( diff --git a/src/transformers/models/deberta/modeling_tf_deberta.py b/src/transformers/models/deberta/modeling_tf_deberta.py index f2ab00622fd..19a43279f65 100644 --- a/src/transformers/models/deberta/modeling_tf_deberta.py +++ b/src/transformers/models/deberta/modeling_tf_deberta.py @@ -18,7 +18,7 @@ from __future__ import annotations import math from collections.abc import Sequence -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -213,7 +213,7 @@ class TFDebertaAttention(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self( hidden_states=input_tensor, attention_mask=attention_mask, @@ -324,7 +324,7 @@ class TFDebertaLayer(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( input_tensor=hidden_states, attention_mask=attention_mask, @@ -415,7 +415,7 @@ class TFDebertaEncoder(keras.layers.Layer): output_hidden_states: bool = False, return_dict: bool = True, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -656,7 +656,7 @@ class TFDebertaDisentangledSelfAttention(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Call the module @@ -1003,7 +1003,7 @@ class TFDebertaLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -1078,7 +1078,7 @@ class TFDebertaMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -1189,7 +1189,7 @@ DEBERTA_START_DOCSTRING = r""" DEBERTA_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and @@ -1259,7 +1259,7 @@ class TFDebertaModel(TFDebertaPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.deberta( input_ids=input_ids, attention_mask=attention_mask, @@ -1319,7 +1319,7 @@ class TFDebertaForMaskedLM(TFDebertaPreTrainedModel, TFMaskedLanguageModelingLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1409,7 +1409,7 @@ class TFDebertaForSequenceClassification(TFDebertaPreTrainedModel, TFSequenceCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1502,7 +1502,7 @@ class TFDebertaForTokenClassification(TFDebertaPreTrainedModel, TFTokenClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1585,7 +1585,7 @@ class TFDebertaForQuestionAnswering(TFDebertaPreTrainedModel, TFQuestionAnswerin start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/deberta/tokenization_deberta.py b/src/transformers/models/deberta/tokenization_deberta.py index af807b425f8..74e958c8030 100644 --- a/src/transformers/models/deberta/tokenization_deberta.py +++ b/src/transformers/models/deberta/tokenization_deberta.py @@ -16,7 +16,7 @@ import json import os -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -247,8 +247,8 @@ class DebertaTokenizer(PreTrainedTokenizer): return word def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A DeBERTa sequence has the following format: @@ -272,8 +272,8 @@ class DebertaTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. @@ -327,7 +327,7 @@ class DebertaTokenizer(PreTrainedTokenizer): return text # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/deberta/tokenization_deberta_fast.py b/src/transformers/models/deberta/tokenization_deberta_fast.py index d91e2434458..c2f2e6552d9 100644 --- a/src/transformers/models/deberta/tokenization_deberta_fast.py +++ b/src/transformers/models/deberta/tokenization_deberta_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Tokenization class for model DeBERTa.""" -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_base import AddedToken, BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -155,8 +155,8 @@ class DebertaTokenizerFast(PreTrainedTokenizerFast): self._mask_token = value def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A DeBERTa sequence has the following format: @@ -201,7 +201,7 @@ class DebertaTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) # Copied from transformers.models.gpt2.tokenization_gpt2_fast.GPT2TokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/deberta_v2/configuration_deberta_v2.py b/src/transformers/models/deberta_v2/configuration_deberta_v2.py index 3d74086aa25..5189cfd53ae 100644 --- a/src/transformers/models/deberta_v2/configuration_deberta_v2.py +++ b/src/transformers/models/deberta_v2/configuration_deberta_v2.py @@ -78,7 +78,7 @@ class DebertaV2Config(PretrainedConfig): The value used to pad input_ids. position_biased_input (`bool`, *optional*, defaults to `True`): Whether add absolute position embedding to content embedding. - pos_att_type (`List[str]`, *optional*): + pos_att_type (`list[str]`, *optional*): The type of relative position attention, it can be a combination of `["p2c", "c2p"]`, e.g. `["p2c"]`, `["p2c", "c2p"]`, `["p2c", "c2p"]`. layer_norm_eps (`float`, *optional*, defaults to 1e-12): diff --git a/src/transformers/models/deberta_v2/modeling_deberta_v2.py b/src/transformers/models/deberta_v2/modeling_deberta_v2.py index 06462ed4775..5073d1de526 100644 --- a/src/transformers/models/deberta_v2/modeling_deberta_v2.py +++ b/src/transformers/models/deberta_v2/modeling_deberta_v2.py @@ -15,7 +15,7 @@ """PyTorch DeBERTa-v2 model.""" from collections.abc import Sequence -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -366,7 +366,7 @@ class DebertaV2Attention(nn.Module): query_states=None, relative_pos=None, rel_embeddings=None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: self_output, att_matrix = self.self( hidden_states, attention_mask, @@ -433,7 +433,7 @@ class DebertaV2Layer(nn.Module): relative_pos=None, rel_embeddings=None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: attention_output, att_matrix = self.attention( hidden_states, attention_mask, @@ -649,7 +649,7 @@ class DebertaV2Encoder(nn.Module): attention_mask = self.get_attention_mask(attention_mask) relative_pos = self.get_rel_pos(hidden_states, query_states, relative_pos) - all_hidden_states: Optional[Tuple[torch.Tensor]] = (hidden_states,) if output_hidden_states else None + all_hidden_states: Optional[tuple[torch.Tensor]] = (hidden_states,) if output_hidden_states else None all_attentions = () if output_attentions else None next_kv = hidden_states @@ -761,7 +761,7 @@ class DebertaV2Model(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -966,7 +966,7 @@ class DebertaV2ForMaskedLM(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1077,7 +1077,7 @@ class DebertaV2ForSequenceClassification(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1172,7 +1172,7 @@ class DebertaV2ForTokenClassification(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1235,7 +1235,7 @@ class DebertaV2ForQuestionAnswering(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.deberta( @@ -1323,7 +1323,7 @@ class DebertaV2ForMultipleChoice(DebertaV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py b/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py index 4abd334b12a..ca83a7f02c5 100644 --- a/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py +++ b/src/transformers/models/deberta_v2/modeling_tf_deberta_v2.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -197,7 +197,7 @@ class TFDebertaV2Attention(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self( hidden_states=input_tensor, attention_mask=attention_mask, @@ -311,7 +311,7 @@ class TFDebertaV2Layer(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( input_tensor=hidden_states, attention_mask=attention_mask, @@ -491,7 +491,7 @@ class TFDebertaV2Encoder(keras.layers.Layer): output_hidden_states: bool = False, return_dict: bool = True, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: if len(shape_list(attention_mask)) <= 2: input_mask = attention_mask else: @@ -723,7 +723,7 @@ class TFDebertaV2DisentangledSelfAttention(keras.layers.Layer): rel_embeddings: Optional[tf.Tensor] = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Call the module @@ -1109,7 +1109,7 @@ class TFDebertaV2LMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -1185,7 +1185,7 @@ class TFDebertaV2MainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -1297,7 +1297,7 @@ DEBERTA_START_DOCSTRING = r""" DEBERTA_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and @@ -1368,7 +1368,7 @@ class TFDebertaV2Model(TFDebertaV2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.deberta( input_ids=input_ids, attention_mask=attention_mask, @@ -1429,7 +1429,7 @@ class TFDebertaV2ForMaskedLM(TFDebertaV2PreTrainedModel, TFMaskedLanguageModelin return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1520,7 +1520,7 @@ class TFDebertaV2ForSequenceClassification(TFDebertaV2PreTrainedModel, TFSequenc return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1614,7 +1614,7 @@ class TFDebertaV2ForTokenClassification(TFDebertaV2PreTrainedModel, TFTokenClass return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1698,7 +1698,7 @@ class TFDebertaV2ForQuestionAnswering(TFDebertaV2PreTrainedModel, TFQuestionAnsw start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -1798,7 +1798,7 @@ class TFDebertaV2ForMultipleChoice(TFDebertaV2PreTrainedModel, TFMultipleChoiceL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` diff --git a/src/transformers/models/deberta_v2/tokenization_deberta_v2.py b/src/transformers/models/deberta_v2/tokenization_deberta_v2.py index 1bf6e956590..59a8c7b3ea2 100644 --- a/src/transformers/models/deberta_v2/tokenization_deberta_v2.py +++ b/src/transformers/models/deberta_v2/tokenization_deberta_v2.py @@ -16,7 +16,7 @@ import os import unicodedata -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as sp @@ -95,7 +95,7 @@ class DebertaV2Tokenizer(PreTrainedTokenizer): pad_token="[PAD]", cls_token="[CLS]", mask_token="[MASK]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -140,7 +140,7 @@ class DebertaV2Tokenizer(PreTrainedTokenizer): vocab.update(self.get_added_vocab()) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" if self.do_lower_case: text = text.lower() @@ -214,7 +214,7 @@ class DebertaV2Tokenizer(PreTrainedTokenizer): text = " " + text return (text, kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: return self._tokenizer.save_pretrained(save_directory, filename_prefix=filename_prefix) @@ -244,7 +244,7 @@ class SPMTokenizer: """ def __init__( - self, vocab_file, special_tokens, split_by_punct=False, sp_model_kwargs: Optional[Dict[str, Any]] = None + self, vocab_file, special_tokens, split_by_punct=False, sp_model_kwargs: Optional[dict[str, Any]] = None ): self.split_by_punct = split_by_punct self.vocab_file = vocab_file diff --git a/src/transformers/models/deberta_v2/tokenization_deberta_v2_fast.py b/src/transformers/models/deberta_v2/tokenization_deberta_v2_fast.py index 44aa71c6856..1bad9684dd8 100644 --- a/src/transformers/models/deberta_v2/tokenization_deberta_v2_fast.py +++ b/src/transformers/models/deberta_v2/tokenization_deberta_v2_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Optional, Tuple +from typing import Optional from ...file_utils import is_sentencepiece_available from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -169,7 +169,7 @@ class DebertaV2TokenizerFast(PreTrainedTokenizerFast): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/decision_transformer/modeling_decision_transformer.py b/src/transformers/models/decision_transformer/modeling_decision_transformer.py index 84844a77a7b..d34e989986d 100755 --- a/src/transformers/models/decision_transformer/modeling_decision_transformer.py +++ b/src/transformers/models/decision_transformer/modeling_decision_transformer.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -258,7 +258,7 @@ class DecisionTransformerGPT2Attention(nn.Module): @deprecate_kwarg("layer_past", new_name="past_key_value", version="4.53.0", raise_if_both_names=True) def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], + hidden_states: Optional[tuple[torch.FloatTensor]], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, @@ -267,7 +267,7 @@ class DecisionTransformerGPT2Attention(nn.Module): encoder_attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, **kwargs, - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]: is_cross_attention = encoder_hidden_states is not None if is_cross_attention: if not hasattr(self, "q_attn"): @@ -351,7 +351,7 @@ class DecisionTransformerGPT2MLP(nn.Module): self.act = ACT2FN[config.activation_function] self.dropout = nn.Dropout(config.resid_pdrop) - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -382,7 +382,7 @@ class DecisionTransformerGPT2Block(nn.Module): @deprecate_kwarg("layer_past", new_name="past_key_value", version="4.53.0", raise_if_both_names=True) def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], + hidden_states: Optional[tuple[torch.FloatTensor]], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, @@ -392,7 +392,7 @@ class DecisionTransformerGPT2Block(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, **kwargs, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_output, self_attn_weights = self.attn( @@ -517,7 +517,7 @@ class DecisionTransformerGPT2Model(DecisionTransformerGPT2PreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, @@ -530,7 +530,7 @@ class DecisionTransformerGPT2Model(DecisionTransformerGPT2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -836,7 +836,7 @@ class DecisionTransformerModel(DecisionTransformerPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DecisionTransformerOutput]: + ) -> Union[tuple[torch.FloatTensor], DecisionTransformerOutput]: r""" states (`torch.FloatTensor` of shape `(batch_size, episode_length, state_dim)`): The states for each step in the trajectory diff --git a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py index 99d39be02a0..9a768bfd9f9 100644 --- a/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py +++ b/src/transformers/models/deepseek_v3/modeling_deepseek_v3.py @@ -5,7 +5,7 @@ # modular_deepseek_v3.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import math -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -374,12 +374,12 @@ class DeepseekV3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: batch_size, seq_length = hidden_states.shape[:-1] query_shape = (batch_size, seq_length, -1, self.qk_head_dim) key_shape = (batch_size, seq_length, -1, self.qk_nope_head_dim + self.v_head_dim) @@ -464,9 +464,9 @@ class DeepseekV3DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/deepseek_v3/modular_deepseek_v3.py b/src/transformers/models/deepseek_v3/modular_deepseek_v3.py index 463a2eaed55..b25ae45c06d 100644 --- a/src/transformers/models/deepseek_v3/modular_deepseek_v3.py +++ b/src/transformers/models/deepseek_v3/modular_deepseek_v3.py @@ -1,5 +1,5 @@ import math -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.nn.functional as F @@ -255,12 +255,12 @@ class DeepseekV3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: batch_size, seq_length = hidden_states.shape[:-1] query_shape = (batch_size, seq_length, -1, self.qk_head_dim) key_shape = (batch_size, seq_length, -1, self.qk_nope_head_dim + self.v_head_dim) diff --git a/src/transformers/models/deformable_detr/image_processing_deformable_detr.py b/src/transformers/models/deformable_detr/image_processing_deformable_detr.py index 77252cac77a..4863442c177 100644 --- a/src/transformers/models/deformable_detr/image_processing_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_deformable_detr.py @@ -18,7 +18,7 @@ import io import pathlib from collections import defaultdict from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -87,12 +87,12 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationForma # Copied from transformers.models.detr.image_processing_detr.get_size_with_aspect_ratio -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -129,10 +129,10 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in # Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -141,7 +141,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -161,7 +161,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -231,7 +231,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.normalize_annotation -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -246,7 +246,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -255,8 +255,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -274,7 +274,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -282,7 +282,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -297,7 +297,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -425,11 +425,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.prepare_coco_panoptic_annotation with DETR->DeformableDetr def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for DeformableDetr. """ @@ -466,7 +466,7 @@ def prepare_coco_panoptic_annotation( # Copied from transformers.models.detr.image_processing_detr.get_segmentation_image def get_segmentation_image( - masks: np.ndarray, input_size: Tuple, target_size: Tuple, stuff_equiv_classes, deduplicate=False + masks: np.ndarray, input_size: tuple, target_size: tuple, stuff_equiv_classes, deduplicate=False ): h, w = input_size final_h, final_w = target_size @@ -491,7 +491,7 @@ def get_segmentation_image( # Copied from transformers.models.detr.image_processing_detr.get_mask_area -def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: int) -> np.ndarray: +def get_mask_area(seg_img: np.ndarray, target_size: tuple[int, int], n_classes: int) -> np.ndarray: final_h, final_w = target_size np_seg_img = seg_img.astype(np.uint8) np_seg_img = np_seg_img.reshape(final_h, final_w, 3) @@ -501,7 +501,7 @@ def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: # Copied from transformers.models.detr.image_processing_detr.score_labels_from_class_probabilities -def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def score_labels_from_class_probabilities(logits: np.ndarray) -> tuple[np.ndarray, np.ndarray]: probs = scipy.special.softmax(logits, axis=-1) labels = probs.argmax(-1, keepdims=True) scores = np.take_along_axis(probs, labels, axis=-1) @@ -514,11 +514,11 @@ def post_process_panoptic_sample( out_logits: np.ndarray, masks: np.ndarray, boxes: np.ndarray, - processed_size: Tuple[int, int], - target_size: Tuple[int, int], - is_thing_map: Dict, + processed_size: tuple[int, int], + target_size: tuple[int, int], + is_thing_map: dict, threshold=0.85, -) -> Dict: +) -> dict: """ Converts the output of [`DetrForSegmentation`] into panoptic segmentation predictions for a single sample. @@ -530,10 +530,10 @@ def post_process_panoptic_sample( boxes (`torch.Tensor`): The prediced bounding boxes for this sample. The boxes are in the normalized format `(center_x, center_y, width, height)` and values between `[0, 1]`, relative to the size the image (disregarding padding). - processed_size (`Tuple[int, int]`): + processed_size (`tuple[int, int]`): The processed size of the image `(height, width)`, as returned by the preprocessing step i.e. the size after data augmentation but before batching. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, `(height, width)` corresponding to the requested final size of the prediction. is_thing_map (`Dict`): @@ -597,9 +597,9 @@ def post_process_panoptic_sample( # Copied from transformers.models.detr.image_processing_detr.resize_annotation def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -607,11 +607,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -679,7 +679,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -710,7 +710,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -747,14 +747,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -768,7 +768,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -812,7 +812,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -834,10 +834,10 @@ class DeformableDetrImageProcessor(BaseImageProcessor): do_normalize: Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -849,7 +849,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -862,16 +862,16 @@ class DeformableDetrImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -930,7 +930,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): @classmethod # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.from_dict with Detr->DeformableDetr - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `DeformableDetrImageProcessor.from_pretrained(checkpoint, size=600, @@ -947,12 +947,12 @@ class DeformableDetrImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DeformableDetr model. """ @@ -980,7 +980,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -993,7 +993,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1052,7 +1052,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -1089,7 +1089,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -1099,12 +1099,12 @@ class DeformableDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor._update_annotation_for_padded_image def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -1144,8 +1144,8 @@ class DeformableDetrImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1177,24 +1177,24 @@ class DeformableDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.pad def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -1215,7 +1215,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1264,24 +1264,24 @@ class DeformableDetrImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, do_convert_annotations: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -1291,15 +1291,15 @@ class DeformableDetrImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -1308,7 +1308,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1330,9 +1330,9 @@ class DeformableDetrImageProcessor(BaseImageProcessor): Whether to convert the annotations to the format expected by the model. Converts the bounding boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)` and in relative coordinates. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_pad (`bool`, *optional*, defaults to self.do_pad): Whether to pad the image. If `True`, padding will be applied to the bottom and right of @@ -1353,7 +1353,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1539,7 +1539,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1572,7 +1572,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`DeformableDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -1583,14 +1583,14 @@ class DeformableDetrImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1613,7 +1613,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/deformable_detr/image_processing_deformable_detr_fast.py b/src/transformers/models/deformable_detr/image_processing_deformable_detr_fast.py index 5bdf903f211..93c50a9135f 100644 --- a/src/transformers/models/deformable_detr/image_processing_deformable_detr_fast.py +++ b/src/transformers/models/deformable_detr/image_processing_deformable_detr_fast.py @@ -5,7 +5,7 @@ # modular_deformable_detr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import pathlib -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -69,7 +69,7 @@ class DeformableDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -80,7 +80,7 @@ class DeformableDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -93,7 +93,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int, device: to Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -237,11 +237,11 @@ def rgb_to_id(color): def prepare_coco_panoptic_annotation( image: torch.Tensor, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for DEFORMABLE_DETR. """ @@ -326,7 +326,7 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `DeformableDetrImageProcessorFast.from_pretrained(checkpoint, size=600, @@ -342,12 +342,12 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DEFORMABLE_DETR model. """ @@ -427,9 +427,9 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -437,11 +437,11 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -478,7 +478,7 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -495,12 +495,12 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -531,8 +531,8 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -562,20 +562,20 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[DeformableDetrFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -599,8 +599,8 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -610,10 +610,10 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -726,7 +726,7 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -759,7 +759,7 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`DeformableDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -770,14 +770,14 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -800,7 +800,7 @@ class DeformableDetrImageProcessorFast(BaseImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/deformable_detr/modeling_deformable_detr.py b/src/transformers/models/deformable_detr/modeling_deformable_detr.py index 1dbcb2ffb3f..e36da6da89b 100755 --- a/src/transformers/models/deformable_detr/modeling_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modeling_deformable_detr.py @@ -18,7 +18,7 @@ import copy import math import warnings from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -57,7 +57,7 @@ class MultiScaleDeformableAttention(nn.Module): self, value: Tensor, value_spatial_shapes: Tensor, - value_spatial_shapes_list: List[Tuple], + value_spatial_shapes_list: list[tuple], level_start_index: Tensor, sampling_locations: Tensor, attention_weights: Tensor, @@ -138,9 +138,9 @@ class DeformableDetrDecoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -191,12 +191,12 @@ class DeformableDetrModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None @@ -263,20 +263,20 @@ class DeformableDetrObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None init_reference_points: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None enc_outputs_class: Optional = None enc_outputs_coord_logits: Optional = None @@ -681,7 +681,7 @@ class DeformableDetrMultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -1480,7 +1480,7 @@ class DeformableDetrModel(DeformableDetrPreTrainedModel): Args: enc_output (Tensor[batch_size, sequence_length, hidden_size]): Output of the encoder. padding_mask (Tensor[batch_size, sequence_length]): Padding mask for `enc_output`. - spatial_shapes (List[Tuple[int, int]]): Spatial shapes of the feature maps. + spatial_shapes (list[tuple[int, int]]): Spatial shapes of the feature maps. Returns: `tuple(torch.FloatTensor)`: A tuple of feature map and bbox prediction. @@ -1547,7 +1547,7 @@ class DeformableDetrModel(DeformableDetrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DeformableDetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], DeformableDetrModelOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1822,11 +1822,11 @@ class DeformableDetrForObjectDetection(DeformableDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DeformableDetrObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], DeformableDetrObjectDetectionOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1836,7 +1836,7 @@ class DeformableDetrForObjectDetection(DeformableDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/deformable_detr/modular_deformable_detr.py b/src/transformers/models/deformable_detr/modular_deformable_detr.py index 5dfb6048c34..3f41c5163b9 100644 --- a/src/transformers/models/deformable_detr/modular_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modular_deformable_detr.py @@ -1,4 +1,4 @@ -from typing import List, Tuple, Union +from typing import Union from transformers.models.detr.image_processing_detr_fast import DetrImageProcessorFast @@ -31,7 +31,7 @@ class DeformableDetrImageProcessorFast(DetrImageProcessorFast): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -64,7 +64,7 @@ class DeformableDetrImageProcessorFast(DetrImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`DeformableDetrForObjectDetection`] into final bounding boxes in (top_left_x, @@ -75,14 +75,14 @@ class DeformableDetrImageProcessorFast(DetrImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -105,7 +105,7 @@ class DeformableDetrImageProcessorFast(DetrImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/deit/image_processing_deit.py b/src/transformers/models/deit/image_processing_deit.py index 7b198f5200a..b5a7641b654 100644 --- a/src/transformers/models/deit/image_processing_deit.py +++ b/src/transformers/models/deit/image_processing_deit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for DeiT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -53,14 +53,14 @@ class DeiTImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): Size of the image after `resize`. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling` filter, *optional*, defaults to `Resampling.BICUBIC`): Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by `do_center_crop` in `preprocess`. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size when applying center-cropping. Can be overridden by `crop_size` in `preprocess`. rescale_factor (`int` or `float`, *optional*, defaults to `1/255`): Scale factor to use if rescaling the image. Can be overridden by the `rescale_factor` parameter in the @@ -71,10 +71,10 @@ class DeiTImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -84,15 +84,15 @@ class DeiTImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PIL.Image.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, rescale_factor: Union[int, float] = 1 / 255, do_rescale: bool = True, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -116,7 +116,7 @@ class DeiTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -128,7 +128,7 @@ class DeiTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -166,15 +166,15 @@ class DeiTImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -188,14 +188,14 @@ class DeiTImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after `resize`. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): PILImageResampling filter to use if resizing the image Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after center crop. If one edge the image is smaller than `crop_size`, it will be padded with zeros and then cropped do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -204,9 +204,9 @@ class DeiTImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/deit/modeling_deit.py b/src/transformers/models/deit/modeling_deit.py index 07df603a948..6b6284995fc 100644 --- a/src/transformers/models/deit/modeling_deit.py +++ b/src/transformers/models/deit/modeling_deit.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Callable, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -220,7 +220,7 @@ class DeiTSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -281,7 +281,7 @@ class DeiTAttention(nn.Module): self.output = DeiTSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -304,7 +304,7 @@ class DeiTAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -365,7 +365,7 @@ class DeiTLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in DeiT, layernorm is applied before self-attention head_mask, @@ -516,7 +516,7 @@ class DeiTModel(DeiTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -840,8 +840,8 @@ class DeiTForImageClassificationWithTeacherOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None cls_logits: Optional[torch.FloatTensor] = None distillation_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( diff --git a/src/transformers/models/deit/modeling_tf_deit.py b/src/transformers/models/deit/modeling_tf_deit.py index b052d32f03c..6dc29ae7fb0 100644 --- a/src/transformers/models/deit/modeling_tf_deit.py +++ b/src/transformers/models/deit/modeling_tf_deit.py @@ -19,7 +19,7 @@ from __future__ import annotations import collections.abc import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -91,8 +91,8 @@ class TFDeiTForImageClassificationWithTeacherOutput(ModelOutput): logits: Optional[tf.Tensor] = None cls_logits: Optional[tf.Tensor] = None distillation_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None class TFDeiTEmbeddings(keras.layers.Layer): @@ -290,7 +290,7 @@ class TFDeiTSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) mixed_key_layer = self.key(inputs=hidden_states) @@ -388,7 +388,7 @@ class TFDeiTAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, head_mask=head_mask, output_attentions=output_attentions, training=training ) @@ -488,7 +488,7 @@ class TFDeiTLayer(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( # in DeiT, layernorm is applied before self-attention input_tensor=self.layernorm_before(inputs=hidden_states, training=training), @@ -550,7 +550,7 @@ class TFDeiTEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -635,7 +635,7 @@ class TFDeiTMainLayer(keras.layers.Layer): return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor, ...]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor, ...]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -785,7 +785,7 @@ class TFDeiTModel(TFDeiTPreTrainedModel): return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutputWithPooling]: + ) -> Union[tuple, TFBaseModelOutputWithPooling]: outputs = self.deit( pixel_values=pixel_values, bool_masked_pos=bool_masked_pos, diff --git a/src/transformers/models/deprecated/deta/image_processing_deta.py b/src/transformers/models/deprecated/deta/image_processing_deta.py index 80dcc97a477..df8a62428db 100644 --- a/src/transformers/models/deprecated/deta/image_processing_deta.py +++ b/src/transformers/models/deprecated/deta/image_processing_deta.py @@ -16,7 +16,7 @@ import pathlib from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -79,12 +79,12 @@ logger = logging.get_logger(__name__) # pylint: disable=invalid-name SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationFormat.COCO_PANOPTIC) -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -120,10 +120,10 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -132,7 +132,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -151,7 +151,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -218,7 +218,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: return arr -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -232,7 +232,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: return norm_annotation -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -240,8 +240,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -258,7 +258,7 @@ def get_max_height_width( def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -266,7 +266,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -280,7 +280,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -405,11 +405,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for DETA. """ @@ -445,9 +445,9 @@ def prepare_coco_panoptic_annotation( def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -455,11 +455,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -505,7 +505,7 @@ class DetaImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -527,10 +527,10 @@ class DetaImageProcessor(BaseImageProcessor): do_normalize: Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -542,7 +542,7 @@ class DetaImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -554,16 +554,16 @@ class DetaImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: bool = True, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -592,12 +592,12 @@ class DetaImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DETA model. """ @@ -624,7 +624,7 @@ class DetaImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -637,7 +637,7 @@ class DetaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -683,7 +683,7 @@ class DetaImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -718,7 +718,7 @@ class DetaImageProcessor(BaseImageProcessor): """ return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -727,12 +727,12 @@ class DetaImageProcessor(BaseImageProcessor): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -771,8 +771,8 @@ class DetaImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -803,24 +803,24 @@ class DetaImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -841,7 +841,7 @@ class DetaImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -889,24 +889,24 @@ class DetaImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[List[Dict], List[List[Dict]]]] = None, + annotations: Optional[Union[list[dict], list[list[dict]]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -916,15 +916,15 @@ class DetaImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`List[Dict]` or `List[List[Dict]]`, *optional*): + annotations (`list[Dict]` or `list[list[Dict]]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -933,7 +933,7 @@ class DetaImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -951,9 +951,9 @@ class DetaImageProcessor(BaseImageProcessor): Rescale factor to use when rescaling the image. do_normalize (`bool`, *optional*, defaults to self.do_normalize): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_convert_annotations (`bool`, *optional*, defaults to self.do_convert_annotations): Whether to convert the annotations to the format expected by the model. Converts the bounding @@ -978,7 +978,7 @@ class DetaImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1145,7 +1145,7 @@ class DetaImageProcessor(BaseImageProcessor): self, outputs, threshold: float = 0.5, - target_sizes: Union[TensorType, List[Tuple]] = None, + target_sizes: Union[TensorType, list[tuple]] = None, nms_threshold: float = 0.7, ): """ @@ -1157,14 +1157,14 @@ class DetaImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.5): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. nms_threshold (`float`, *optional*, defaults to 0.7): NMS threshold. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1188,7 +1188,7 @@ class DetaImageProcessor(BaseImageProcessor): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/deprecated/deta/modeling_deta.py b/src/transformers/models/deprecated/deta/modeling_deta.py index 69810c38899..ae44b5e1b12 100644 --- a/src/transformers/models/deprecated/deta/modeling_deta.py +++ b/src/transformers/models/deprecated/deta/modeling_deta.py @@ -20,7 +20,7 @@ import os import warnings from dataclasses import dataclass from pathlib import Path -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -181,9 +181,9 @@ class DetaDecoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -236,12 +236,12 @@ class DetaModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None output_proposals: Optional[torch.FloatTensor] = None @@ -311,20 +311,20 @@ class DetaObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None init_reference_points: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None enc_outputs_class: Optional = None enc_outputs_coord_logits: Optional = None output_proposals: Optional[torch.FloatTensor] = None @@ -748,7 +748,7 @@ class DetaMultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -1602,7 +1602,7 @@ class DetaModel(DetaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DetaModelOutput]: + ) -> Union[tuple[torch.FloatTensor], DetaModelOutput]: r""" Returns: @@ -1915,13 +1915,13 @@ class DetaForObjectDetection(DetaPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DetaObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], DetaObjectDetectionOutput]: r""" - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -2152,7 +2152,7 @@ class DetaLoss(nn.Module): Number of object categories, omitting the special no-object category. focal_alpha (`float`): Alpha parameter in focal loss. - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. See `get_loss` for a list of all available losses. """ @@ -2281,7 +2281,7 @@ class DetaLoss(nn.Module): Args: outputs (`dict`, *optional*): Dictionary of tensors, see the output specification of the model for the format. - targets (`List[dict]`, *optional*): + targets (`list[dict]`, *optional*): List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the losses applied, see each loss' doc. """ @@ -2392,7 +2392,7 @@ class DetaHungarianMatcher(nn.Module): A dictionary that contains at least these entries: * "logits": Tensor of dim [batch_size, num_queries, num_classes] with the classification logits * "pred_boxes": Tensor of dim [batch_size, num_queries, 4] with the predicted box coordinates. - targets (`List[dict]`): + targets (`list[dict]`): A list of targets (len(targets) = batch_size), where each target is a dict containing: * "class_labels": Tensor of dim [num_target_boxes] (where num_target_boxes is the number of ground-truth @@ -2400,7 +2400,7 @@ class DetaHungarianMatcher(nn.Module): * "boxes": Tensor of dim [num_target_boxes, 4] containing the target box coordinates. Returns: - `List[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: + `list[Tuple]`: A list of size `batch_size`, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected targets (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes) @@ -2529,7 +2529,7 @@ class DetaMatcher: matches to prediction n in [0, N). (b) a vector of length N containing the labels for each prediction. """ - def __init__(self, thresholds: List[float], labels: List[int], allow_low_quality_matches: bool = False): + def __init__(self, thresholds: list[float], labels: list[int], allow_low_quality_matches: bool = False): """ Args: thresholds (`list[float]`): diff --git a/src/transformers/models/deprecated/efficientformer/configuration_efficientformer.py b/src/transformers/models/deprecated/efficientformer/configuration_efficientformer.py index 5e2dca8bcd0..9aaee649ba0 100644 --- a/src/transformers/models/deprecated/efficientformer/configuration_efficientformer.py +++ b/src/transformers/models/deprecated/efficientformer/configuration_efficientformer.py @@ -14,8 +14,6 @@ # limitations under the License. """EfficientFormer model configuration""" -from typing import List - from ....configuration_utils import PretrainedConfig from ....utils import logging @@ -107,9 +105,9 @@ class EfficientFormerConfig(PretrainedConfig): def __init__( self, - depths: List[int] = [3, 2, 6, 4], - hidden_sizes: List[int] = [48, 96, 224, 448], - downsamples: List[bool] = [True, True, True, True], + depths: list[int] = [3, 2, 6, 4], + hidden_sizes: list[int] = [48, 96, 224, 448], + downsamples: list[bool] = [True, True, True, True], dim: int = 448, key_dim: int = 32, attention_ratio: int = 4, diff --git a/src/transformers/models/deprecated/efficientformer/image_processing_efficientformer.py b/src/transformers/models/deprecated/efficientformer/image_processing_efficientformer.py index 83a78ac65fa..39267aa2e6c 100644 --- a/src/transformers/models/deprecated/efficientformer/image_processing_efficientformer.py +++ b/src/transformers/models/deprecated/efficientformer/image_processing_efficientformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for EfficientFormer.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -61,7 +61,7 @@ class EfficientFormerImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -73,10 +73,10 @@ class EfficientFormerImageProcessor(BaseImageProcessor): do_normalize: Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -86,15 +86,15 @@ class EfficientFormerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -133,7 +133,7 @@ class EfficientFormerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -145,7 +145,7 @@ class EfficientFormerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample: `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -179,15 +179,15 @@ class EfficientFormerImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -202,7 +202,7 @@ class EfficientFormerImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -214,13 +214,13 @@ class EfficientFormerImageProcessor(BaseImageProcessor): Whether to rescale the image values between [0 - 1]. rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`): Rescale factor to rescale the image by if `do_rescale` is set to `True`. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/deprecated/efficientformer/modeling_efficientformer.py b/src/transformers/models/deprecated/efficientformer/modeling_efficientformer.py index a45fe7da5da..fa84afc00d3 100644 --- a/src/transformers/models/deprecated/efficientformer/modeling_efficientformer.py +++ b/src/transformers/models/deprecated/efficientformer/modeling_efficientformer.py @@ -16,7 +16,7 @@ import itertools from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -117,7 +117,7 @@ class EfficientFormerSelfAttention(nn.Module): else: self.ab = self.attention_biases[:, self.attention_bias_idxs] - def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> tuple[torch.Tensor]: batch_size, sequence_length, num_channels = hidden_states.shape qkv = self.qkv(hidden_states) query_layer, key_layer, value_layer = qkv.reshape(batch_size, sequence_length, self.num_heads, -1).split( @@ -277,7 +277,7 @@ class EfficientFormerFlat(nn.Module): def __init__(self): super().__init__() - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor]: hidden_states = hidden_states.flatten(2).transpose(1, 2) return hidden_states @@ -306,7 +306,7 @@ class EfficientFormerMeta3D(nn.Module): self.layer_scale_1 = nn.Parameter(config.layer_scale_init_value * torch.ones((dim)), requires_grad=True) self.layer_scale_2 = nn.Parameter(config.layer_scale_init_value * torch.ones((dim)), requires_grad=True) - def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> tuple[torch.Tensor]: self_attention_outputs = self.token_mixer(self.layernorm1(hidden_states), output_attentions) attention_output = self_attention_outputs[0] outputs = self_attention_outputs[1:] # add self attentions if we output attention weights @@ -338,7 +338,7 @@ class EfficientFormerMeta3DLayers(nn.Module): [EfficientFormerMeta3D(config, config.hidden_sizes[-1], drop_path=drop_path) for drop_path in drop_paths] ) - def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> tuple[torch.Tensor]: all_attention_outputs = () if output_attentions else None for layer_module in self.blocks: @@ -373,7 +373,7 @@ class EfficientFormerMeta4D(nn.Module): self.layer_scale_1 = nn.Parameter(config.layer_scale_init_value * torch.ones((dim)), requires_grad=True) self.layer_scale_2 = nn.Parameter(config.layer_scale_init_value * torch.ones((dim)), requires_grad=True) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor]: outputs = self.token_mixer(hidden_states) if self.use_layer_scale: @@ -406,7 +406,7 @@ class EfficientFormerMeta4DLayers(nn.Module): ] ) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor]: for layer_module in self.blocks: hidden_states = layer_module(hidden_states) return hidden_states @@ -417,7 +417,7 @@ class EfficientFormerIntermediateStage(nn.Module): super().__init__() self.meta4D_layers = EfficientFormerMeta4DLayers(config, index) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor]: hidden_states = self.meta4D_layers(hidden_states) return hidden_states @@ -429,7 +429,7 @@ class EfficientFormerLastStage(nn.Module): self.flat = EfficientFormerFlat() self.meta3D_layers = EfficientFormerMeta3DLayers(config) - def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor, output_attentions: bool = False) -> tuple[torch.Tensor]: hidden_states = self.meta4D_layers(hidden_states) hidden_states = self.flat(hidden_states) hidden_states = self.meta3D_layers(hidden_states, output_attentions) @@ -720,8 +720,8 @@ class EfficientFormerForImageClassificationWithTeacherOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None cls_logits: Optional[torch.FloatTensor] = None distillation_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @add_start_docstrings( diff --git a/src/transformers/models/deprecated/efficientformer/modeling_tf_efficientformer.py b/src/transformers/models/deprecated/efficientformer/modeling_tf_efficientformer.py index e11fa1edf9d..12d7c942ae5 100644 --- a/src/transformers/models/deprecated/efficientformer/modeling_tf_efficientformer.py +++ b/src/transformers/models/deprecated/efficientformer/modeling_tf_efficientformer.py @@ -16,7 +16,7 @@ import itertools from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -181,7 +181,7 @@ class TFEfficientFormerSelfAttention(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, output_attentions: bool = False, training: bool = False - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size, sequence_length, *_ = shape_list(hidden_states) qkv = self.qkv(inputs=hidden_states) @@ -427,7 +427,7 @@ class TFEfficientFormerFlat(keras.layers.Layer): def __init__(self, **kwargs): super().__init__(**kwargs) - def call(self, hidden_states: tf.Tensor) -> Tuple[tf.Tensor]: + def call(self, hidden_states: tf.Tensor) -> tuple[tf.Tensor]: batch_size, _, _, in_channels = shape_list(hidden_states) hidden_states = tf.reshape(hidden_states, shape=[batch_size, -1, in_channels]) return hidden_states @@ -501,7 +501,7 @@ class TFEfficientFormerMeta3D(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, output_attentions: bool = False, training: bool = False - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_attention_outputs = self.token_mixer( hidden_states=self.layernorm1(hidden_states, training=training), output_attentions=output_attentions, @@ -547,7 +547,7 @@ class TFEfficientFormerMeta3DLayers(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, output_attentions: bool = False, training: bool = False - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: all_attention_outputs = () if output_attentions else None for i, layer_module in enumerate(self.blocks): @@ -625,7 +625,7 @@ class TFEfficientFormerMeta4D(keras.layers.Layer): with tf.name_scope(self.drop_path.name): self.drop_path.build(None) - def call(self, hidden_states: tf.Tensor, training: bool = False) -> Tuple[tf.Tensor]: + def call(self, hidden_states: tf.Tensor, training: bool = False) -> tuple[tf.Tensor]: outputs = self.token_mixer(hidden_states) if self.config.use_layer_scale: @@ -666,7 +666,7 @@ class TFEfficientFormerMeta4DLayers(keras.layers.Layer): for i in range(len(drop_paths)) ] - def call(self, hidden_states: tf.Tensor, training: bool = False) -> Tuple[tf.Tensor]: + def call(self, hidden_states: tf.Tensor, training: bool = False) -> tuple[tf.Tensor]: for layer_module in self.blocks: hidden_states = layer_module(hidden_states=hidden_states, training=training) return hidden_states @@ -686,7 +686,7 @@ class TFEfficientFormerIntermediateStage(keras.layers.Layer): super().__init__(**kwargs) self.meta4D_layers = TFEfficientFormerMeta4DLayers(config=config, stage_idx=index, name="meta4D_layers") - def call(self, hidden_states: tf.Tensor, training: bool = False) -> Tuple[tf.Tensor]: + def call(self, hidden_states: tf.Tensor, training: bool = False) -> tuple[tf.Tensor]: hidden_states = self.meta4D_layers(hidden_states=hidden_states, training=training) return hidden_states @@ -708,7 +708,7 @@ class TFEfficientFormerLastStage(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, output_attentions: bool = False, training: bool = False - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: hidden_states = self.meta4D_layers(hidden_states=hidden_states, training=training) hidden_states = self.flat(hidden_states=hidden_states) hidden_states = self.meta3D_layers( @@ -832,7 +832,7 @@ class TFEfficientFormerMainLayer(keras.layers.Layer): output_hidden_states: Optional[tf.Tensor] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor, ...]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( @@ -961,7 +961,7 @@ class TFEfficientFormerModel(TFEfficientFormerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: outputs = self.efficientformer( pixel_values=pixel_values, output_attentions=output_attentions, @@ -1090,8 +1090,8 @@ class TFEfficientFormerForImageClassificationWithTeacherOutput(ModelOutput): logits: Optional[tf.Tensor] = None cls_logits: Optional[tf.Tensor] = None distillation_logits: Optional[tf.Tensor] = None - hidden_states: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[tf.Tensor]] = None + hidden_states: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tf.Tensor]] = None @add_start_docstrings( diff --git a/src/transformers/models/deprecated/ernie_m/configuration_ernie_m.py b/src/transformers/models/deprecated/ernie_m/configuration_ernie_m.py index 7a451061318..839b17b11bf 100644 --- a/src/transformers/models/deprecated/ernie_m/configuration_ernie_m.py +++ b/src/transformers/models/deprecated/ernie_m/configuration_ernie_m.py @@ -17,8 +17,6 @@ from __future__ import annotations -from typing import Dict - from ....configuration_utils import PretrainedConfig @@ -75,7 +73,7 @@ class ErnieMConfig(PretrainedConfig): """ model_type = "ernie_m" - attribute_map: Dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} + attribute_map: dict[str, str] = {"dropout": "classifier_dropout", "num_classes": "num_labels"} def __init__( self, diff --git a/src/transformers/models/deprecated/ernie_m/modeling_ernie_m.py b/src/transformers/models/deprecated/ernie_m/modeling_ernie_m.py index 28c17afa3f7..9f0d8e676ad 100755 --- a/src/transformers/models/deprecated/ernie_m/modeling_ernie_m.py +++ b/src/transformers/models/deprecated/ernie_m/modeling_ernie_m.py @@ -15,7 +15,7 @@ """PyTorch ErnieM model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -125,9 +125,9 @@ class ErnieMSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.q_proj(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -252,9 +252,9 @@ class ErnieMAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self_attn( hidden_states, attention_mask, @@ -294,7 +294,7 @@ class ErnieMEncoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = True, ): residual = hidden_states @@ -343,11 +343,11 @@ class ErnieMEncoder(nn.Module): input_embeds: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: hidden_states = () if output_hidden_states else None attentions = () if output_attentions else None @@ -519,12 +519,12 @@ class ErnieMModel(ErnieMPreTrainedModel): attention_mask: Optional[tensor] = None, head_mask: Optional[tensor] = None, inputs_embeds: Optional[tensor] = None, - past_key_values: Optional[Tuple[Tuple[tensor]]] = None, + past_key_values: Optional[tuple[tuple[tensor]]] = None, use_cache: Optional[bool] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time.") @@ -626,13 +626,13 @@ class ErnieMForSequenceClassification(ErnieMPreTrainedModel): position_ids: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, use_cache: Optional[bool] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = True, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.FloatTensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -728,7 +728,7 @@ class ErnieMForMultipleChoice(ErnieMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.FloatTensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.FloatTensor], MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., @@ -815,12 +815,12 @@ class ErnieMForTokenClassification(ErnieMPreTrainedModel): position_ids: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = True, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.FloatTensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.FloatTensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -896,7 +896,7 @@ class ErnieMForQuestionAnswering(ErnieMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.FloatTensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.FloatTensor], QuestionAnsweringModelOutput]: r""" start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -984,7 +984,7 @@ class ErnieMForInformationExtraction(ErnieMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.FloatTensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.FloatTensor], QuestionAnsweringModelOutput]: r""" start_positions (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for position (index) for computing the start_positions loss. Position outside of the sequence are diff --git a/src/transformers/models/deprecated/ernie_m/tokenization_ernie_m.py b/src/transformers/models/deprecated/ernie_m/tokenization_ernie_m.py index 44bc197a4f7..e6f2c212217 100644 --- a/src/transformers/models/deprecated/ernie_m/tokenization_ernie_m.py +++ b/src/transformers/models/deprecated/ernie_m/tokenization_ernie_m.py @@ -17,7 +17,7 @@ import io import os import unicodedata -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -67,7 +67,7 @@ class ErnieMTokenizer(PreTrainedTokenizer): """ # Ernie-M model doesn't have token_type embedding. - model_input_names: List[str] = ["input_ids"] + model_input_names: list[str] = ["input_ids"] vocab_files_names = VOCAB_FILES_NAMES resource_files_names = RESOURCE_FILES_NAMES @@ -83,7 +83,7 @@ class ErnieMTokenizer(PreTrainedTokenizer): pad_token="[PAD]", cls_token="[CLS]", mask_token="[MASK]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it and @@ -248,12 +248,12 @@ class ErnieMTokenizer(PreTrainedTokenizer): - pair of sequences: `[CLS] A [SEP] [SEP] B [SEP]` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of input_id with the appropriate special tokens. + `list[int]`: List of input_id with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -270,12 +270,12 @@ class ErnieMTokenizer(PreTrainedTokenizer): - pair of sequences: `(0,0) A (0,0) (0,0) B (0,0)` Args: - offset_mapping_ids_0 (`List[tuple]`): + offset_mapping_ids_0 (`list[tuple]`): List of char offsets to which the special tokens will be added. - offset_mapping_ids_1 (`List[tuple]`, *optional*): + offset_mapping_ids_1 (`list[tuple]`, *optional*): Optional second list of wordpiece offsets for offset mapping pairs. Returns: - `List[tuple]`: List of wordpiece offsets with the appropriate offsets of special tokens. + `list[tuple]`: List of wordpiece offsets with the appropriate offsets of special tokens. """ if offset_mapping_1 is None: return [(0, 0)] + offset_mapping_0 + [(0, 0)] @@ -288,14 +288,14 @@ class ErnieMTokenizer(PreTrainedTokenizer): special tokens using the tokenizer `encode` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids of the first sequence. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`str`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: + `list[int]`: The list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ @@ -312,20 +312,20 @@ class ErnieMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create the token type IDs corresponding to the sequences passed. [What are token type IDs?](../glossary#token-type-ids) Should be overridden in a subclass if the model has a special way of building: those. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): The first tokenized sequence. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): The second tokenized sequence. Returns: - `List[int]`: The token type ids. + `list[int]`: The token type ids. """ # called when `add_special_tokens` is True, so align with `build_inputs_with_special_tokens` method if token_ids_1 is None: @@ -380,7 +380,7 @@ class ErnieMTokenizer(PreTrainedTokenizer): return token_to_idx - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/deprecated/gptsan_japanese/modeling_gptsan_japanese.py b/src/transformers/models/deprecated/gptsan_japanese/modeling_gptsan_japanese.py index d387b86875a..6974c002470 100644 --- a/src/transformers/models/deprecated/gptsan_japanese/modeling_gptsan_japanese.py +++ b/src/transformers/models/deprecated/gptsan_japanese/modeling_gptsan_japanese.py @@ -15,7 +15,7 @@ """PyTorch GPTSANJapanese model.""" import copy -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -158,7 +158,7 @@ class GPTSanJapaneseTop1Router(nn.Module): self.ignore_padding_tokens = config.router_ignore_padding_tokens self.dtype = getattr(torch, config.router_dtype) - def _compute_router_probabilities(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def _compute_router_probabilities(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: r""" Computes router probabilities from input hidden states. @@ -199,7 +199,7 @@ class GPTSanJapaneseTop1Router(nn.Module): if not (hasattr(self.classifier, "SCB") or hasattr(self.classifier, "CB")): self.classifier = self.classifier.to(self.dtype) - def forward(self, hidden_states: torch.Tensor) -> Tuple: + def forward(self, hidden_states: torch.Tensor) -> tuple: r""" Generic forward function for every Router class. Each Router expects to have the same input hidden states (`hidden_states`) corresponding to the hidden states for each token, the `expert_capacity` corresponding to the @@ -213,7 +213,7 @@ class GPTSanJapaneseTop1Router(nn.Module): hidden_states (`torch.Tensor`) : [num_groups, tokens_per_group, hidden_dim] inputs to send to experts. Returns: - Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`] Tuple containing the expert index, the router probs + tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`] Tuple containing the expert index, the router probs and the router logits. The router probabilities and logits are required to compute the loss. """ router_probs, router_logits = self._compute_router_probabilities(hidden_states) @@ -382,11 +382,11 @@ class GPTSanJapaneseAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -516,13 +516,13 @@ class GPTSanJapaneseLayerSelfAttention(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], - past_key_value: Optional[Tuple[torch.Tensor]] = None, + hidden_states: Optional[tuple[torch.FloatTensor]], + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]: r""" Self-attention and normalize block. @@ -555,7 +555,7 @@ class GPTSanJapaneseLayerSelfAttention(nn.Module): Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail. Returns: - Tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...] + tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...] """ # Self Attention # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 @@ -597,14 +597,14 @@ class GPTSanJapaneseBlock(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], - past_key_value: Optional[Tuple[torch.Tensor]] = None, + hidden_states: Optional[tuple[torch.FloatTensor]], + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, output_router_tuple: Optional[bool] = False, - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]: r""" GPTSAN transformer block. @@ -638,7 +638,7 @@ class GPTSanJapaneseBlock(nn.Module): output_router_tuple: output experts router logits and expert id. Returns: - Tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...] + tuple[torch.Tensor[num_groups, tokens_per_group, hidden_dim],...] """ atten_out = self.self_attn( hidden_states=hidden_states, @@ -886,7 +886,7 @@ class GPTSanJapaneseModel(GPTSanJapanesePreTrainedModel): attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.FloatTensor] = None, spout: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -896,7 +896,7 @@ class GPTSanJapaneseModel(GPTSanJapanesePreTrainedModel): return_dict: Optional[bool] = None, output_router_logits: Optional[bool] = None, num_precontext: Optional[torch.LongTensor] = None, - ) -> Union[MoEModelOutputWithPastAndCrossAttentions, Tuple[torch.FloatTensor]]: + ) -> Union[MoEModelOutputWithPastAndCrossAttentions, tuple[torch.FloatTensor]]: r""" num_precontext (`torch.LongTensor` of shape `(batch_size,1)`): length of `hybrid` input tokens in the input. Tokens up to this length refer to both front and back like @@ -1112,7 +1112,7 @@ class GPTSanJapaneseForConditionalGeneration(GPTSanJapanesePreTrainedModel): attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.FloatTensor] = None, spout: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, head_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = False, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1122,7 +1122,7 @@ class GPTSanJapaneseForConditionalGeneration(GPTSanJapanesePreTrainedModel): return_dict: Optional[bool] = None, output_router_logits: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], MoECausalLMOutputWithPast]: + ) -> Union[tuple[torch.FloatTensor], MoECausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification loss. Indices should be in `[-100, 0, ..., @@ -1268,8 +1268,8 @@ class GPTSanJapaneseForConditionalGeneration(GPTSanJapanesePreTrainedModel): input_ids: torch.LongTensor, attention_mask: torch.FloatTensor, token_type_ids: Optional[torch.FloatTensor] = None, - spout: Optional[Union[List, torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + spout: Optional[Union[list, torch.FloatTensor]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, **kwargs, ): if isinstance(spout, list): diff --git a/src/transformers/models/deprecated/gptsan_japanese/tokenization_gptsan_japanese.py b/src/transformers/models/deprecated/gptsan_japanese/tokenization_gptsan_japanese.py index 9ffe4d9b14a..5eab135c058 100644 --- a/src/transformers/models/deprecated/gptsan_japanese/tokenization_gptsan_japanese.py +++ b/src/transformers/models/deprecated/gptsan_japanese/tokenization_gptsan_japanese.py @@ -19,7 +19,7 @@ import json import os import re import sys -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -237,7 +237,7 @@ class GPTSanJapaneseTokenizer(PreTrainedTokenizer): text = "".join(words) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( @@ -268,8 +268,8 @@ class GPTSanJapaneseTokenizer(PreTrainedTokenizer): return vocab_file, emoji_file def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: # docstyle-ignore """ The tokenizer returns token_type_ids as separators between the Prefix part and the rest. @@ -318,7 +318,7 @@ class GPTSanJapaneseTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], List[TextInputPair], List[PreTokenizedInput], List[PreTokenizedInputPair] + list[TextInput], list[TextInputPair], list[PreTokenizedInput], list[PreTokenizedInputPair] ], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, diff --git a/src/transformers/models/deprecated/graphormer/collating_graphormer.py b/src/transformers/models/deprecated/graphormer/collating_graphormer.py index 70e9e235dc0..a5300fe8a56 100644 --- a/src/transformers/models/deprecated/graphormer/collating_graphormer.py +++ b/src/transformers/models/deprecated/graphormer/collating_graphormer.py @@ -2,7 +2,7 @@ # Licensed under the MIT License. from collections.abc import Mapping -from typing import Any, Dict, List +from typing import Any import numpy as np import torch @@ -79,7 +79,7 @@ class GraphormerDataCollator: self.spatial_pos_max = spatial_pos_max self.on_the_fly_processing = on_the_fly_processing - def __call__(self, features: List[dict]) -> Dict[str, Any]: + def __call__(self, features: list[dict]) -> dict[str, Any]: if self.on_the_fly_processing: features = [preprocess_item(i) for i in features] diff --git a/src/transformers/models/deprecated/graphormer/modeling_graphormer.py b/src/transformers/models/deprecated/graphormer/modeling_graphormer.py index 21e001c1f76..337e127683c 100755 --- a/src/transformers/models/deprecated/graphormer/modeling_graphormer.py +++ b/src/transformers/models/deprecated/graphormer/modeling_graphormer.py @@ -16,7 +16,7 @@ import math from collections.abc import Iterable, Iterator -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -369,7 +369,7 @@ class GraphormerMultiheadAttention(nn.Module): attn_mask: Optional[torch.Tensor] = None, before_softmax: bool = False, need_head_weights: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ Args: key_padding_mask (Bytetorch.Tensor, optional): mask to exclude @@ -530,7 +530,7 @@ class GraphormerGraphEncoderLayer(nn.Module): self_attn_bias: Optional[torch.Tensor] = None, self_attn_mask: Optional[torch.Tensor] = None, self_attn_padding_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ nn.LayerNorm is applied either before or after the self-attention/ffn modules similar to the original Transformer implementation. @@ -628,7 +628,7 @@ class GraphormerGraphEncoder(nn.Module): last_state_only: bool = False, token_embeddings: Optional[torch.Tensor] = None, attn_mask: Optional[torch.Tensor] = None, - ) -> Tuple[Union[torch.Tensor, List[torch.LongTensor]], torch.Tensor]: + ) -> tuple[Union[torch.Tensor, list[torch.LongTensor]], torch.Tensor]: # compute padding mask. This is needed for multi-head attention data_x = input_nodes n_graph, n_node = data_x.size()[:2] @@ -808,7 +808,7 @@ class GraphormerModel(GraphormerPreTrainedModel): masked_tokens: None = None, return_dict: Optional[bool] = None, **unused, - ) -> Union[Tuple[torch.LongTensor], BaseModelOutputWithNoAttention]: + ) -> Union[tuple[torch.LongTensor], BaseModelOutputWithNoAttention]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict inner_states, graph_rep = self.graph_encoder( @@ -872,7 +872,7 @@ class GraphormerForGraphClassification(GraphormerPreTrainedModel): labels: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, **unused, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict encoder_outputs = self.encoder( diff --git a/src/transformers/models/deprecated/jukebox/configuration_jukebox.py b/src/transformers/models/deprecated/jukebox/configuration_jukebox.py index c1d8a78e3e9..2088a695baf 100644 --- a/src/transformers/models/deprecated/jukebox/configuration_jukebox.py +++ b/src/transformers/models/deprecated/jukebox/configuration_jukebox.py @@ -15,7 +15,7 @@ """Jukebox configuration""" import os -from typing import List, Union +from typing import Union from ....configuration_utils import PretrainedConfig from ....utils import logging @@ -592,7 +592,7 @@ class JukeboxConfig(PretrainedConfig): super().__init__(**kwargs) @classmethod - def from_configs(cls, prior_configs: List[JukeboxPriorConfig], vqvae_config: JukeboxVQVAEConfig, **kwargs): + def from_configs(cls, prior_configs: list[JukeboxPriorConfig], vqvae_config: JukeboxVQVAEConfig, **kwargs): r""" Instantiate a [`JukeboxConfig`] (or a derived class) from clip text model configuration and clip vision model configuration. diff --git a/src/transformers/models/deprecated/jukebox/modeling_jukebox.py b/src/transformers/models/deprecated/jukebox/modeling_jukebox.py index 11064c1e46e..779519c9468 100755 --- a/src/transformers/models/deprecated/jukebox/modeling_jukebox.py +++ b/src/transformers/models/deprecated/jukebox/modeling_jukebox.py @@ -16,7 +16,7 @@ import math import os -from typing import List, Optional, Tuple +from typing import Optional import numpy as np import torch @@ -80,7 +80,7 @@ def get_relevant_lyric_tokens(full_tokens, max_n_lyric_tokens, total_length, off the most relevant tokens (in time) for the sequence. Args: - full_tokens (`List[int]`): + full_tokens (`list[int]`): List containing the token ids of the entire lyrics. total_length (`int`): Total expected length of the music (not all of it is generated, see duration), in samples. @@ -734,7 +734,7 @@ class JukeboxVQVAE(PreTrainedModel): ] return self.decode(music_tokens) - def forward(self, raw_audio: torch.FloatTensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, raw_audio: torch.FloatTensor) -> tuple[torch.Tensor, torch.Tensor]: """ Forward pass of the VQ-VAE, encodes the `raw_audio` to latent states, which are then decoded for each level. The commit loss, which ensure that the encoder's computed embeddings are close to the codebook vectors, is @@ -745,7 +745,7 @@ class JukeboxVQVAE(PreTrainedModel): Audio input which will be encoded and decoded. Returns: - `Tuple[torch.Tensor, torch.Tensor]` + `tuple[torch.Tensor, torch.Tensor]` Example: @@ -2072,12 +2072,12 @@ class JukeboxPrior(PreTrainedModel): Args: n_samples (`int`): Number of samples to generate. - music_tokens (`List[torch.LongTensor]`, *optional*): + music_tokens (`list[torch.LongTensor]`, *optional*): Previously generated tokens at the current level. Used as context for the generation. - music_tokens_conds (`List[torch.FloatTensor]`, *optional*): + music_tokens_conds (`list[torch.FloatTensor]`, *optional*): Upper-level music tokens generated by the previous prior model. Is `None` if the generation is not conditioned on the upper-level tokens. - metadata (`List[torch.LongTensor]`, *optional*): + metadata (`list[torch.LongTensor]`, *optional*): List containing the metadata tensor with the artist, genre and the lyric tokens. temp (`float`, *optional*, defaults to 1.0): Sampling temperature. @@ -2225,10 +2225,10 @@ class JukeboxPrior(PreTrainedModel): def forward( self, hidden_states: torch.Tensor, - metadata: Optional[List[torch.LongTensor]], + metadata: Optional[list[torch.LongTensor]], decode: Optional[bool] = False, get_preds: Optional[bool] = False, - ) -> List[torch.Tensor]: + ) -> list[torch.Tensor]: """ Encode the hidden states using the `vqvae` encoder, and then predicts the next token in the `forward_tokens` function. The loss is the sum of the `encoder` loss and the `decoder` loss. @@ -2236,7 +2236,7 @@ class JukeboxPrior(PreTrainedModel): Args: hidden_states (`torch.Tensor`): Hidden states which should be raw audio - metadata (`List[torch.LongTensor]`, *optional*): + metadata (`list[torch.LongTensor]`, *optional*): List containing the metadata conditioning tensor with the lyric and the metadata tokens. decode (`bool`, *optional*, defaults to `False`): Whether or not to decode the encoded to tokens. @@ -2277,10 +2277,10 @@ class JukeboxPreTrainedModel(PreTrainedModel): JUKEBOX_SAMPLING_INPUT_DOCSTRING = r""" - labels (`List[torch.LongTensor]` of length `n_sample`, and shape `(self.levels, self.config.max_nb_genre + lyric_sequence_length)` : + labels (`list[torch.LongTensor]` of length `n_sample`, and shape `(self.levels, self.config.max_nb_genre + lyric_sequence_length)` : List of metadata such as `artist_id`, `genre_id` and the full list of lyric tokens which are used to condition the generation. - sampling_kwargs (`Dict[Any]`): + sampling_kwargs (`dict[Any]`): Various additional sampling arguments that are used by the `_sample` function. A detail list of the arguments can bee seen in the [`_sample`] function documentation. """ @@ -2446,24 +2446,24 @@ class JukeboxModel(JukeboxPreTrainedModel): offset=0, save_results=True, sample_length=None, - ) -> List[torch.LongTensor]: + ) -> list[torch.LongTensor]: """ Core sampling function used to generate music tokens. Iterates over the provided list of levels, while saving the generated raw audio at each step. Args: - music_tokens (`List[torch.LongTensor]`): + music_tokens (`list[torch.LongTensor]`): A sequence of music tokens of length `self.levels` which will be used as context to continue the sampling process. Should have `self.levels` tensors, each corresponding to the generation at a certain level. - labels (`List[torch.LongTensor]`): + labels (`list[torch.LongTensor]`): List of length `n_sample`, and shape `(self.levels, 4 + self.config.max_nb_genre + lyric_sequence_length)` metadata such as `artist_id`, `genre_id` and the full list of lyric tokens which are used to condition the generation. - sample_levels (`List[int]`): + sample_levels (`list[int]`): List of the desired levels at which the sampling will be done. A level is equivalent to the index of the prior in the list of priors - metas (`List[Any]`, *optional*): + metas (`list[Any]`, *optional*): Metadatas used to generate the `labels` chunk_size (`int`, *optional*, defaults to 32): Size of a chunk of audio, used to fill up the memory in chunks to prevent OOM errors. Bigger chunks @@ -2575,7 +2575,7 @@ class JukeboxModel(JukeboxPreTrainedModel): the VQ-VAE decoder to convert the music tokens to raw audio. Args: - labels (`List[torch.LongTensor]`) : + labels (`list[torch.LongTensor]`) : List of length `n_sample`, and shape `(self.levels, 4 + self.config.max_nb_genre + lyric_sequence_length)` metadata such as `artist_id`, `genre_id` and the full list of lyric tokens which are used to condition the generation. @@ -2583,7 +2583,7 @@ class JukeboxModel(JukeboxPreTrainedModel): Number of samples to be generated in parallel. """, ) - def ancestral_sample(self, labels, n_samples=1, **sampling_kwargs) -> List[torch.LongTensor]: + def ancestral_sample(self, labels, n_samples=1, **sampling_kwargs) -> list[torch.LongTensor]: """ Example: @@ -2618,13 +2618,13 @@ class JukeboxModel(JukeboxPreTrainedModel): """Generates a continuation of the previously generated tokens. Args: - music_tokens (`List[torch.LongTensor]` of length `self.levels` ) : + music_tokens (`list[torch.LongTensor]` of length `self.levels` ) : A sequence of music tokens which will be used as context to continue the sampling process. Should have `self.levels` tensors, each corresponding to the generation at a certain level. """, JUKEBOX_SAMPLING_INPUT_DOCSTRING, ) - def continue_sample(self, music_tokens, labels, **sampling_kwargs) -> List[torch.LongTensor]: + def continue_sample(self, music_tokens, labels, **sampling_kwargs) -> list[torch.LongTensor]: sample_levels = sampling_kwargs.pop("sample_levels", list(range(len(self.priors)))) music_tokens = self._sample(music_tokens, labels, sample_levels, **sampling_kwargs) return music_tokens @@ -2633,13 +2633,13 @@ class JukeboxModel(JukeboxPreTrainedModel): """Upsamples a sequence of music tokens using the prior at level `level`. Args: - music_tokens (`List[torch.LongTensor]` of length `self.levels` ) : + music_tokens (`list[torch.LongTensor]` of length `self.levels` ) : A sequence of music tokens which will be used as context to continue the sampling process. Should have `self.levels` tensors, each corresponding to the generation at a certain level. """, JUKEBOX_SAMPLING_INPUT_DOCSTRING, ) - def upsample(self, music_tokens, labels, **sampling_kwargs) -> List[torch.LongTensor]: + def upsample(self, music_tokens, labels, **sampling_kwargs) -> list[torch.LongTensor]: sample_levels = sampling_kwargs.pop("sample_levels", list(range(len(self.priors) - 1))) music_tokens = self._sample(music_tokens, labels, sample_levels, **sampling_kwargs) return music_tokens @@ -2650,13 +2650,13 @@ class JukeboxModel(JukeboxPreTrainedModel): used: as conditioning for each level, which means that no ancestral sampling is required. Args: - raw_audio (`List[torch.Tensor]` of length `n_samples` ) : + raw_audio (`list[torch.Tensor]` of length `n_samples` ) : A list of raw audio that will be used as conditioning information for each samples that will be generated. """, JUKEBOX_SAMPLING_INPUT_DOCSTRING, ) - def primed_sample(self, raw_audio, labels, **sampling_kwargs) -> List[torch.LongTensor]: + def primed_sample(self, raw_audio, labels, **sampling_kwargs) -> list[torch.LongTensor]: sample_levels = sampling_kwargs.pop("sample_levels", list(range(len(self.priors)))) self.vqvae.to(raw_audio.device).float() with torch.no_grad(): diff --git a/src/transformers/models/deprecated/jukebox/tokenization_jukebox.py b/src/transformers/models/deprecated/jukebox/tokenization_jukebox.py index e08ab179a80..ec2162db2cc 100644 --- a/src/transformers/models/deprecated/jukebox/tokenization_jukebox.py +++ b/src/transformers/models/deprecated/jukebox/tokenization_jukebox.py @@ -19,7 +19,7 @@ import os import re import unicodedata from json.encoder import INFINITY -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import regex @@ -82,7 +82,7 @@ class JukeboxTokenizer(PreTrainedTokenizer): Path to the vocabulary file which contain a mapping between genres and ids. lyrics_file (`str`): Path to the vocabulary file which contains the accepted characters for the lyrics tokenization. - version (`List[str]`, `optional`, default to `["v3", "v2", "v2"]`) : + version (`list[str]`, `optional`, default to `["v3", "v2", "v2"]`) : List of the tokenizer versions. The `5b-lyrics`'s top level prior model was trained using `v3` instead of `v2`. n_genres (`int`, `optional`, defaults to 1): @@ -184,7 +184,7 @@ class JukeboxTokenizer(PreTrainedTokenizer): def prepare_for_tokenization( self, artists: str, genres: str, lyrics: str, is_split_into_words: bool = False - ) -> Tuple[str, str, str, Dict[str, Any]]: + ) -> tuple[str, str, str, dict[str, Any]]: """ Performs any necessary transformations before tokenization. @@ -259,7 +259,7 @@ class JukeboxTokenizer(PreTrainedTokenizer): text = pattern.sub("_", text).strip("_") return text - def convert_lyric_tokens_to_string(self, lyrics: List[str]) -> str: + def convert_lyric_tokens_to_string(self, lyrics: list[str]) -> str: return " ".join(lyrics) def convert_to_tensors( @@ -350,7 +350,7 @@ class JukeboxTokenizer(PreTrainedTokenizer): ] return BatchEncoding({"input_ids": input_ids, "attention_masks": attention_masks}) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: """ Saves the tokenizer's vocabulary dictionary to the provided save_directory. @@ -393,9 +393,9 @@ class JukeboxTokenizer(PreTrainedTokenizer): Args: artists_index (`int`): Index of the artist in its corresponding dictionary. - genres_index (`Union[List[int], int]`): + genres_index (`Union[list[int], int]`): Index of the genre in its corresponding dictionary. - lyric_index (`List[int]`): + lyric_index (`list[int]`): List of character indices, which each correspond to a character. """ artist = self.artists_decoder.get(artists_index) diff --git a/src/transformers/models/deprecated/mctct/configuration_mctct.py b/src/transformers/models/deprecated/mctct/configuration_mctct.py index 9cba190a0f4..984dca4a62b 100644 --- a/src/transformers/models/deprecated/mctct/configuration_mctct.py +++ b/src/transformers/models/deprecated/mctct/configuration_mctct.py @@ -85,7 +85,7 @@ class MCTCTConfig(PretrainedConfig): Feature dimensions of the channels of the input to the Conv1D layer. input_channels (`int`, *optional*, defaults to 1): Number of input channels of the input to the Conv1D layer. - conv_channels (`List[int]`, *optional*): + conv_channels (`list[int]`, *optional*): Channel sizes of intermediate Conv1D layers. ctc_loss_reduction (`str`, *optional*, defaults to `"sum"`): Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training an diff --git a/src/transformers/models/deprecated/mctct/feature_extraction_mctct.py b/src/transformers/models/deprecated/mctct/feature_extraction_mctct.py index f210031f309..65d18c78821 100644 --- a/src/transformers/models/deprecated/mctct/feature_extraction_mctct.py +++ b/src/transformers/models/deprecated/mctct/feature_extraction_mctct.py @@ -16,7 +16,7 @@ Feature extractor class for M-CTC-T """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -153,14 +153,14 @@ class MCTCTFeatureExtractor(SequenceFeatureExtractor): return x def normalize( - self, input_features: List[np.ndarray], attention_mask: Optional[np.ndarray] = None - ) -> List[np.ndarray]: + self, input_features: list[np.ndarray], attention_mask: Optional[np.ndarray] = None + ) -> list[np.ndarray]: lengths = attention_mask.sum(-1) if attention_mask is not None else [x.shape[0] for x in input_features] return [self._normalize_one(x, n, self.padding_value) for x, n in zip(input_features, lengths)] def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, truncation: bool = False, @@ -175,7 +175,7 @@ class MCTCTFeatureExtractor(SequenceFeatureExtractor): log-mel spectrogram of the input audio, as implemented in the original Flashlight MFSC feature extraction code. Args: - raw_speech (`torch.Tensor`, `np.ndarray`, `List[float]`, `List[torch.Tensor]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`torch.Tensor`, `np.ndarray`, `list[float]`, `list[torch.Tensor]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a tensor, a numpy array, a list of float values, a list of tensors, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. diff --git a/src/transformers/models/deprecated/mctct/modeling_mctct.py b/src/transformers/models/deprecated/mctct/modeling_mctct.py index 5f80743c4d8..e4852ff78f8 100755 --- a/src/transformers/models/deprecated/mctct/modeling_mctct.py +++ b/src/transformers/models/deprecated/mctct/modeling_mctct.py @@ -15,7 +15,7 @@ """PyTorch M-CTC-T model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -547,7 +547,7 @@ class MCTCTEncoder(MCTCTPreTrainedModel): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -654,7 +654,7 @@ class MCTCTModel(MCTCTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -725,7 +725,7 @@ class MCTCTForCTC(MCTCTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to diff --git a/src/transformers/models/deprecated/mega/modeling_mega.py b/src/transformers/models/deprecated/mega/modeling_mega.py index b73fa9c020b..d6ac15bebf7 100644 --- a/src/transformers/models/deprecated/mega/modeling_mega.py +++ b/src/transformers/models/deprecated/mega/modeling_mega.py @@ -15,7 +15,7 @@ """PyTorch MEGA model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -629,10 +629,10 @@ class MegaGatedCrossAttention(nn.Module): key: Optional[torch.Tensor], value: Optional[torch.Tensor], key_padding_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ Gated cross-attention used in Mega @@ -914,7 +914,7 @@ class MegaMovingAverageGatedAttention(nn.Module): input, padding_mask: Optional[torch.Tensor] = None, causal_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[torch.Tensor]] = None, + past_key_values: Optional[tuple[torch.Tensor]] = None, output_attentions=False, use_cache=False, ): @@ -1185,10 +1185,10 @@ class MegaBlock(nn.Module): causal_mask: Optional[torch.LongTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[torch.FloatTensor]] = None, + past_key_value: Optional[tuple[torch.FloatTensor]] = None, output_attentions: Optional[bool] = False, use_cache: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: """ A single Mega layer: either encoder or decoder, with optional cross-attention and optional normalized feed-forward layer @@ -1493,12 +1493,12 @@ class MegaModel(MegaPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1684,12 +1684,12 @@ class MegaForCausalLM(MegaPreTrainedModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1857,13 +1857,13 @@ class MegaForMaskedLM(MegaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` - kwargs (`Dict[str, any]`, optional, defaults to *{}*): + kwargs (`dict[str, any]`, optional, defaults to *{}*): Used to hide legacy arguments that have been deprecated. """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1937,7 +1937,7 @@ class MegaForSequenceClassification(MegaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -2027,7 +2027,7 @@ class MegaForMultipleChoice(MegaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., @@ -2116,7 +2116,7 @@ class MegaForTokenClassification(MegaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -2213,7 +2213,7 @@ class MegaForQuestionAnswering(MegaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/deprecated/nat/configuration_nat.py b/src/transformers/models/deprecated/nat/configuration_nat.py index 85961aa2fe8..002eaa7f82b 100644 --- a/src/transformers/models/deprecated/nat/configuration_nat.py +++ b/src/transformers/models/deprecated/nat/configuration_nat.py @@ -39,9 +39,9 @@ class NatConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embed_dim (`int`, *optional*, defaults to 64): Dimensionality of patch embedding. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 5]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 5]`): Number of layers in each level of the encoder. - num_heads (`List[int]`, *optional*, defaults to `[2, 4, 8, 16]`): + num_heads (`list[int]`, *optional*, defaults to `[2, 4, 8, 16]`): Number of attention heads in each layer of the Transformer encoder. kernel_size (`int`, *optional*, defaults to 7): Neighborhood Attention kernel size. @@ -64,12 +64,12 @@ class NatConfig(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. layer_scale_init_value (`float`, *optional*, defaults to 0.0): The initial value for the layer scale. Disabled if <=0. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/deprecated/nat/modeling_nat.py b/src/transformers/models/deprecated/nat/modeling_nat.py index 70ecffcf51e..930c8f29821 100644 --- a/src/transformers/models/deprecated/nat/modeling_nat.py +++ b/src/transformers/models/deprecated/nat/modeling_nat.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -98,9 +98,9 @@ class NatEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -134,9 +134,9 @@ class NatModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -170,9 +170,9 @@ class NatImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None class NatEmbeddings(nn.Module): @@ -188,7 +188,7 @@ class NatEmbeddings(nn.Module): self.norm = nn.LayerNorm(config.embed_dim) self.dropout = nn.Dropout(config.hidden_dropout_prob) - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor]: embeddings = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) @@ -321,7 +321,7 @@ class NeighborhoodAttention(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(hidden_states)) key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) @@ -393,7 +393,7 @@ class NeighborhoodAttentionModule(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -459,7 +459,7 @@ class NatLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: batch_size, height, width, channels = hidden_states.size() shortcut = hidden_states @@ -523,7 +523,7 @@ class NatStage(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: _, height, width, _ = hidden_states.size() for i, layer_module in enumerate(self.layers): layer_outputs = layer_module(hidden_states, output_attentions) @@ -567,7 +567,7 @@ class NatEncoder(nn.Module): output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, NatEncoderOutput]: + ) -> Union[tuple, NatEncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -709,7 +709,7 @@ class NatModel(NatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, NatModelOutput]: + ) -> Union[tuple, NatModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -788,7 +788,7 @@ class NatForImageClassification(NatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, NatImageClassifierOutput]: + ) -> Union[tuple, NatImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deprecated/nezha/modeling_nezha.py b/src/transformers/models/deprecated/nezha/modeling_nezha.py index 73655d46477..d1c3fd8dbaa 100644 --- a/src/transformers/models/deprecated/nezha/modeling_nezha.py +++ b/src/transformers/models/deprecated/nezha/modeling_nezha.py @@ -18,7 +18,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -248,9 +248,9 @@ class NezhaSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -392,9 +392,9 @@ class NezhaAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -460,9 +460,9 @@ class NezhaLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -538,12 +538,12 @@ class NezhaEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -762,8 +762,8 @@ class NezhaForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None NEZHA_START_DOCSTRING = r""" @@ -885,12 +885,12 @@ class NezhaModel(NezhaPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1044,7 +1044,7 @@ class NezhaForPreTraining(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], NezhaForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], NezhaForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1056,7 +1056,7 @@ class NezhaForPreTraining(NezhaPreTrainedModel): - 0 indicates sequence B is a continuation of sequence A, - 1 indicates sequence B is a random sequence. - kwargs (`Dict[str, any]`, optional, defaults to *{}*): + kwargs (`dict[str, any]`, optional, defaults to *{}*): Used to hide legacy arguments that have been deprecated. Returns: @@ -1158,7 +1158,7 @@ class NezhaForMaskedLM(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1245,7 +1245,7 @@ class NezhaForNextSentencePrediction(NezhaPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], NextSentencePredictorOutput]: + ) -> Union[tuple[torch.Tensor], NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -1357,7 +1357,7 @@ class NezhaForSequenceClassification(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1454,7 +1454,7 @@ class NezhaForMultipleChoice(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., @@ -1547,7 +1547,7 @@ class NezhaForTokenClassification(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1623,7 +1623,7 @@ class NezhaForQuestionAnswering(NezhaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/deprecated/open_llama/modeling_open_llama.py b/src/transformers/models/deprecated/open_llama/modeling_open_llama.py index e3726fd9a22..100d02e2285 100644 --- a/src/transformers/models/deprecated/open_llama/modeling_open_llama.py +++ b/src/transformers/models/deprecated/open_llama/modeling_open_llama.py @@ -20,7 +20,7 @@ """PyTorch Open-Llama model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -267,10 +267,10 @@ class OpenLlamaAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states).view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2) @@ -358,10 +358,10 @@ class OpenLlamaDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -555,13 +555,13 @@ class OpenLlamaModel(OpenLlamaPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -713,14 +713,14 @@ class OpenLlamaForCausalLM(OpenLlamaPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -886,14 +886,14 @@ class OpenLlamaForSequenceClassification(OpenLlamaPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deprecated/qdqbert/modeling_qdqbert.py b/src/transformers/models/deprecated/qdqbert/modeling_qdqbert.py index fdeeb424abe..f30a0757009 100755 --- a/src/transformers/models/deprecated/qdqbert/modeling_qdqbert.py +++ b/src/transformers/models/deprecated/qdqbert/modeling_qdqbert.py @@ -18,7 +18,7 @@ import math import os import warnings -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -848,7 +848,7 @@ class QDQBertModel(QDQBertPreTrainedModel): def set_input_embeddings(self, value): self.embeddings.word_embeddings = value - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]): + def _prune_heads(self, heads_to_prune: dict[int, list[int]]): """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -872,12 +872,12 @@ class QDQBertModel(QDQBertPreTrainedModel): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1032,12 +1032,12 @@ class QDQBertLMHeadModel(QDQBertPreTrainedModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.LongTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.LongTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1206,7 +1206,7 @@ class QDQBertForMaskedLM(QDQBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1297,7 +1297,7 @@ class QDQBertForNextSentencePrediction(QDQBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, NextSentencePredictorOutput]: + ) -> Union[tuple, NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -1406,7 +1406,7 @@ class QDQBertForSequenceClassification(QDQBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1502,7 +1502,7 @@ class QDQBertForMultipleChoice(QDQBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., @@ -1594,7 +1594,7 @@ class QDQBertForTokenClassification(QDQBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1672,7 +1672,7 @@ class QDQBertForQuestionAnswering(QDQBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/deprecated/realm/modeling_realm.py b/src/transformers/models/deprecated/realm/modeling_realm.py index ac25a177333..5714bf52a0e 100644 --- a/src/transformers/models/deprecated/realm/modeling_realm.py +++ b/src/transformers/models/deprecated/realm/modeling_realm.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -253,9 +253,9 @@ class RealmSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -401,9 +401,9 @@ class RealmAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -469,9 +469,9 @@ class RealmLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -547,12 +547,12 @@ class RealmEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -664,8 +664,8 @@ class RealmEmbedderOutput(ModelOutput): """ projected_score: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -733,8 +733,8 @@ class RealmReaderOutput(ModelOutput): candidate: Optional[torch.LongTensor] = None start_pos: torch.int32 = None end_pos: torch.int32 = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -1161,7 +1161,7 @@ class RealmEmbedder(RealmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, RealmEmbedderOutput]: + ) -> Union[tuple, RealmEmbedderOutput]: r""" Returns: @@ -1247,7 +1247,7 @@ class RealmScorer(RealmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, RealmScorerOutput]: + ) -> Union[tuple, RealmScorerOutput]: r""" candidate_input_ids (`torch.LongTensor` of shape `(batch_size, num_candidates, sequence_length)`): Indices of candidate input sequence tokens in the vocabulary. @@ -1402,7 +1402,7 @@ class RealmKnowledgeAugEncoder(RealmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" relevance_score (`torch.FloatTensor` of shape `(batch_size, num_candidates)`, *optional*): Relevance score derived from RealmScorer, must be specified if you want to compute the masked language @@ -1541,7 +1541,7 @@ class RealmReader(RealmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, RealmReaderOutput]: + ) -> Union[tuple, RealmReaderOutput]: r""" relevance_score (`torch.FloatTensor` of shape `(searcher_beam_size,)`, *optional*): Relevance score, which must be specified if you want to compute the logits and marginal log loss. @@ -1758,7 +1758,7 @@ class RealmForOpenQA(RealmPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, answer_ids: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, RealmForOpenQAOutput]: + ) -> Union[tuple, RealmForOpenQAOutput]: r""" Returns: diff --git a/src/transformers/models/deprecated/realm/tokenization_realm.py b/src/transformers/models/deprecated/realm/tokenization_realm.py index df152b64c13..d1c9bcc35bb 100644 --- a/src/transformers/models/deprecated/realm/tokenization_realm.py +++ b/src/transformers/models/deprecated/realm/tokenization_realm.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ....tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ....tokenization_utils_base import BatchEncoding @@ -252,8 +252,8 @@ class RealmTokenizer(PreTrainedTokenizer): return BatchEncoding(output_data, tensor_type=return_tensors) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A REALM sequence has the following format: @@ -277,8 +277,8 @@ class RealmTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -304,7 +304,7 @@ class RealmTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/deprecated/realm/tokenization_realm_fast.py b/src/transformers/models/deprecated/realm/tokenization_realm_fast.py index 64c8400729f..53a1d99e0ac 100644 --- a/src/transformers/models/deprecated/realm/tokenization_realm_fast.py +++ b/src/transformers/models/deprecated/realm/tokenization_realm_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization classes for REALM.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -215,7 +215,7 @@ class RealmTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/deprecated/retribert/tokenization_retribert.py b/src/transformers/models/deprecated/retribert/tokenization_retribert.py index 1e094a5a1a3..3702e7b02f7 100644 --- a/src/transformers/models/deprecated/retribert/tokenization_retribert.py +++ b/src/transformers/models/deprecated/retribert/tokenization_retribert.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ....tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ....utils import logging @@ -181,8 +181,8 @@ class RetriBertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -206,8 +206,8 @@ class RetriBertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -233,7 +233,7 @@ class RetriBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/deprecated/retribert/tokenization_retribert_fast.py b/src/transformers/models/deprecated/retribert/tokenization_retribert_fast.py index 395d7cd55e8..5fcbc395a1b 100644 --- a/src/transformers/models/deprecated/retribert/tokenization_retribert_fast.py +++ b/src/transformers/models/deprecated/retribert/tokenization_retribert_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for RetriBERT.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -142,7 +142,7 @@ class RetriBertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/deprecated/speech_to_text_2/modeling_speech_to_text_2.py b/src/transformers/models/deprecated/speech_to_text_2/modeling_speech_to_text_2.py index 4d1d4c3da12..ce4fdd1bb2a 100755 --- a/src/transformers/models/deprecated/speech_to_text_2/modeling_speech_to_text_2.py +++ b/src/transformers/models/deprecated/speech_to_text_2/modeling_speech_to_text_2.py @@ -16,7 +16,7 @@ import copy import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -146,11 +146,11 @@ class Speech2Text2Attention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -301,7 +301,7 @@ class Speech2Text2DecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ): @@ -733,14 +733,14 @@ class Speech2Text2ForCausalLM(Speech2Text2PreTrainedModel): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.FloatTensor], CausalLMOutputWithCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): diff --git a/src/transformers/models/deprecated/speech_to_text_2/tokenization_speech_to_text_2.py b/src/transformers/models/deprecated/speech_to_text_2/tokenization_speech_to_text_2.py index f5aa7ef8067..05c10080b77 100644 --- a/src/transformers/models/deprecated/speech_to_text_2/tokenization_speech_to_text_2.py +++ b/src/transformers/models/deprecated/speech_to_text_2/tokenization_speech_to_text_2.py @@ -16,7 +16,7 @@ import json import os -from typing import Dict, List, Optional, Tuple +from typing import Optional from ....tokenization_utils import PreTrainedTokenizer from ....utils import logging @@ -121,7 +121,7 @@ class Speech2Text2Tokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.decoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: return dict(self.encoder, **self.added_tokens_encoder) def bpe(self, token): @@ -205,7 +205,7 @@ class Speech2Text2Tokenizer(PreTrainedTokenizer): result = self.decoder.get(index, self.unk_token) return result - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """ Converts a list of output tokens into a single string. """ @@ -217,7 +217,7 @@ class Speech2Text2Tokenizer(PreTrainedTokenizer): return string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/deprecated/tapex/tokenization_tapex.py b/src/transformers/models/deprecated/tapex/tokenization_tapex.py index 719d3c59f32..654eba14a82 100644 --- a/src/transformers/models/deprecated/tapex/tokenization_tapex.py +++ b/src/transformers/models/deprecated/tapex/tokenization_tapex.py @@ -18,7 +18,7 @@ import json import os import random from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import regex as re @@ -140,7 +140,7 @@ class IndexedRowTableLinearize: FORMAT: col: col1 | col2 | col 3 row 1 : val1 | val2 | val3 row 2 : ... """ - def process_table(self, table_content: Dict): + def process_table(self, table_content: dict): """ Given a table, TableLinearize aims at converting it into a flatten sequence with special symbols. """ @@ -153,13 +153,13 @@ class IndexedRowTableLinearize: table_str += self.process_row(row_example, row_index=i + 1) + " " return table_str.strip() - def process_header(self, headers: List): + def process_header(self, headers: list): """ Given a list of headers, TableLinearize aims at converting it into a flatten sequence with special symbols. """ return "col : " + " | ".join(headers) - def process_row(self, row: List, row_index: int): + def process_row(self, row: list, row_index: int): """ Given a row, TableLinearize aims at converting it into a flatten sequence with special symbols. """ @@ -316,8 +316,8 @@ class TapexTokenizer(PreTrainedTokenizer): self.table_linearize = IndexedRowTableLinearize() def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A TAPEX sequence has the following format: @@ -325,12 +325,12 @@ class TapexTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -339,20 +339,20 @@ class TapexTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Args: Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -364,18 +364,18 @@ class TapexTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Args: Create a mask from the two sequences passed to be used in a sequence-pair classification task. TAPEX does not: make use of token type ids, therefore a list of zeros is returned. - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -463,7 +463,7 @@ class TapexTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -495,9 +495,9 @@ class TapexTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, TAPEX_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - table: Union["pd.DataFrame", List["pd.DataFrame"]] = None, - query: Optional[Union[TextInput, List[TextInput]]] = None, - answer: Optional[Union[str, List[str]]] = None, + table: Union["pd.DataFrame", list["pd.DataFrame"]] = None, + query: Optional[Union[TextInput, list[TextInput]]] = None, + answer: Optional[Union[str, list[str]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -518,12 +518,12 @@ class TapexTokenizer(PreTrainedTokenizer): Main method to tokenize and prepare for the model one or several table-sequence pair(s). Args: - table (`pd.DataFrame`, `List[pd.DataFrame]`): + table (`pd.DataFrame`, `list[pd.DataFrame]`): Table(s) containing tabular data. - query (`str` or `List[str]`, *optional*): + query (`str` or `list[str]`, *optional*): Sentence or batch of sentences related to one or more table(s) to be encoded. Note that the number of sentences must match the number of tables. - answer (`str` or `List[str]`, *optional*): + answer (`str` or `list[str]`, *optional*): Optionally, the corresponding answer to the questions as supervision. """ @@ -572,9 +572,9 @@ class TapexTokenizer(PreTrainedTokenizer): def source_call_func( self, - table: Union["pd.DataFrame", List["pd.DataFrame"]], - query: Optional[Union[TextInput, List[TextInput]]] = None, - answer: Optional[Union[str, List[str]]] = None, + table: Union["pd.DataFrame", list["pd.DataFrame"]], + query: Optional[Union[TextInput, list[TextInput]]] = None, + answer: Optional[Union[str, list[str]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -610,10 +610,10 @@ class TapexTokenizer(PreTrainedTokenizer): if not valid_table: raise ValueError( - "table input must of type `pd.DataFrame` (single example), `List[pd.DataFrame]` (batch of examples). " + "table input must of type `pd.DataFrame` (single example), `list[pd.DataFrame]` (batch of examples). " ) if not valid_query: - raise ValueError("query input must of type `str` (single example), `List[str]` (batch of examples). ") + raise ValueError("query input must of type `str` (single example), `list[str]` (batch of examples). ") is_batched = isinstance(table, (list, tuple)) or isinstance(query, (list, tuple)) if is_batched: @@ -660,9 +660,9 @@ class TapexTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, TAPEX_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def batch_encode_plus( self, - table: Union["pd.DataFrame", List["pd.DataFrame"]], - query: Optional[List[TextInput]] = None, - answer: Optional[List[str]] = None, + table: Union["pd.DataFrame", list["pd.DataFrame"]], + query: Optional[list[TextInput]] = None, + answer: Optional[list[str]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Optional[Union[bool, str]] = None, @@ -717,9 +717,9 @@ class TapexTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, - table: Union["pd.DataFrame", List["pd.DataFrame"]], - query: Optional[List[TextInput]] = None, - answer: Optional[List[str]] = None, + table: Union["pd.DataFrame", list["pd.DataFrame"]], + query: Optional[list[TextInput]] = None, + answer: Optional[list[str]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -776,9 +776,9 @@ class TapexTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, TAPEX_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def _batch_prepare_for_model( self, - table: Union["pd.DataFrame", List["pd.DataFrame"]], - query: Optional[Union[TextInput, List[TextInput]]] = None, - answer: Optional[Union[str, List[str]]] = None, + table: Union["pd.DataFrame", list["pd.DataFrame"]], + query: Optional[Union[TextInput, list[TextInput]]] = None, + answer: Optional[Union[str, list[str]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -856,7 +856,7 @@ class TapexTokenizer(PreTrainedTokenizer): max_length: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Prepare a table, a string and possible answer for the model. This method does not return token type IDs, attention masks, etc. which are necessary for the model to work correctly. Use this method if you want to build @@ -985,7 +985,7 @@ class TapexTokenizer(PreTrainedTokenizer): def target_call_func( self, - answer: Union[str, List[str]], + answer: Union[str, list[str]], add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1006,7 +1006,7 @@ class TapexTokenizer(PreTrainedTokenizer): The method tokenizes and prepares the answer label for the model. Args: - answer (`str` or `List[str]`): + answer (`str` or `list[str]`): Corresponding answer supervision to the queries for training the model. """ is_batched = isinstance(answer, (list, tuple)) @@ -1050,7 +1050,7 @@ class TapexTokenizer(PreTrainedTokenizer): def target_batch_encode_plus( self, - answer: List[str], + answer: list[str], add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Optional[Union[bool, str]] = None, @@ -1070,7 +1070,7 @@ class TapexTokenizer(PreTrainedTokenizer): Prepare answer strings for the model. Args: - answer `List[str]`: + answer `list[str]`: Corresponding answer supervision to the queries for training the model. """ # Backward compatibility for 'truncation_strategy', 'pad_to_max_length' @@ -1103,7 +1103,7 @@ class TapexTokenizer(PreTrainedTokenizer): def _target_batch_encode_plus( self, - answer: List[str], + answer: list[str], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1170,7 +1170,7 @@ class TapexTokenizer(PreTrainedTokenizer): max_length: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Prepare the answer string for the model. This method does not return token type IDs, attention masks, etc. which are necessary for the model to work correctly. Use this method if you want to build your processing on @@ -1341,7 +1341,7 @@ class TapexTokenizer(PreTrainedTokenizer): return joint_input - def truncate_table_cells(self, table_content: Dict, question: str, answer: List): + def truncate_table_cells(self, table_content: dict, question: str, answer: list): # TODO (Qian): is it possible to revert the original cell if it is in the final answer? cell_mapping = {} for row in table_content["rows"]: @@ -1373,7 +1373,7 @@ class TapexTokenizer(PreTrainedTokenizer): return cell_value def truncate_table_rows( - self, table_content: Dict, question: str, answer: Optional[Union[str, List[str]]] = None, max_length=None + self, table_content: dict, question: str, answer: Optional[Union[str, list[str]]] = None, max_length=None ): """ Args: @@ -1401,7 +1401,7 @@ class TapexTokenizer(PreTrainedTokenizer): maximum_keep_rows += 1 del table_content["rows"][maximum_keep_rows:] - def estimate_delete_ratio(self, table_content: Dict, question: str, max_length=None): + def estimate_delete_ratio(self, table_content: dict, question: str, max_length=None): if "header" not in table_content or "rows" not in table_content: raise ValueError("The table content should contain both 'header' and 'rows' keys.") # calculate the tokens of header, special tokens will only be pre-prepended into question @@ -1427,7 +1427,7 @@ class TapexTokenizer(PreTrainedTokenizer): # calc a roughly delete rate return 1.0 - remain_token_len / value_token_len, remain_token_len - def delete_unrelated_rows(self, table_content: Dict, question: str, answer: List, delete_ratio: float): + def delete_unrelated_rows(self, table_content: dict, question: str, answer: list, delete_ratio: float): """ The argument answer is used only during training. """ diff --git a/src/transformers/models/deprecated/trajectory_transformer/modeling_trajectory_transformer.py b/src/transformers/models/deprecated/trajectory_transformer/modeling_trajectory_transformer.py index 0a9a0111d22..a06a52d9f3c 100644 --- a/src/transformers/models/deprecated/trajectory_transformer/modeling_trajectory_transformer.py +++ b/src/transformers/models/deprecated/trajectory_transformer/modeling_trajectory_transformer.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -125,7 +125,7 @@ class TrajectoryTransformerOutput(ModelOutput): Language modeling loss. logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`Tuple[Tuple[torch.Tensor]]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`tuple[tuple[torch.Tensor]]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of length `config.n_layers`, containing tuples of tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). Contains pre-computed hidden-states (key and values in the attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding. @@ -141,9 +141,9 @@ class TrajectoryTransformerOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class TrajectoryTransformerPreTrainedModel(PreTrainedModel): @@ -190,7 +190,7 @@ TRAJECTORY_TRANSFORMER_INPUTS_DOCSTRING = r""" Args: trajectories (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Batch of trajectories, where a trajectory is a sequence of states, actions and rewards. - past_key_values (`Tuple[Tuple[torch.Tensor]]` of length `config.n_layers`, *optional*): + past_key_values (`tuple[tuple[torch.Tensor]]` of length `config.n_layers`, *optional*): Contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The `input_ids` which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -286,8 +286,8 @@ class CausalSelfAttention(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], - layer_past: Optional[Tuple[torch.Tensor]] = None, + hidden_states: Optional[tuple[torch.FloatTensor]], + layer_past: Optional[tuple[torch.Tensor]] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ): @@ -361,8 +361,8 @@ class Block(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], - layer_past: Optional[Tuple[torch.Tensor]] = None, + hidden_states: Optional[tuple[torch.FloatTensor]], + layer_past: Optional[tuple[torch.Tensor]] = None, use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ): @@ -463,14 +463,14 @@ class TrajectoryTransformerModel(TrajectoryTransformerPreTrainedModel): def forward( self, trajectories: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, targets: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TrajectoryTransformerOutput]: + ) -> Union[tuple[torch.Tensor], TrajectoryTransformerOutput]: r""" Returns: diff --git a/src/transformers/models/deprecated/transfo_xl/configuration_transfo_xl.py b/src/transformers/models/deprecated/transfo_xl/configuration_transfo_xl.py index 7d82659b5a6..b7926246c4c 100644 --- a/src/transformers/models/deprecated/transfo_xl/configuration_transfo_xl.py +++ b/src/transformers/models/deprecated/transfo_xl/configuration_transfo_xl.py @@ -36,7 +36,7 @@ class TransfoXLConfig(PretrainedConfig): vocab_size (`int`, *optional*, defaults to 267735): Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling [`TransfoXLModel`] or [`TFTransfoXLModel`]. - cutoffs (`List[int]`, *optional*, defaults to `[20000, 40000, 200000]`): + cutoffs (`list[int]`, *optional*, defaults to `[20000, 40000, 200000]`): Cutoffs for the adaptive softmax. d_model (`int`, *optional*, defaults to 1024): Dimensionality of the model's hidden states. diff --git a/src/transformers/models/deprecated/transfo_xl/modeling_tf_transfo_xl.py b/src/transformers/models/deprecated/transfo_xl/modeling_tf_transfo_xl.py index 90e2ebc3436..dd9626125f7 100644 --- a/src/transformers/models/deprecated/transfo_xl/modeling_tf_transfo_xl.py +++ b/src/transformers/models/deprecated/transfo_xl/modeling_tf_transfo_xl.py @@ -20,7 +20,7 @@ TF 2.0 Transformer XL model. from __future__ import annotations from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -540,7 +540,7 @@ class TFTransfoXLMainLayer(keras.layers.Layer): def call( self, input_ids: TFModelInputType | None = None, - mems: List[tf.Tensor] | None = None, + mems: list[tf.Tensor] | None = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -673,7 +673,7 @@ class TFTransfoXLModelOutput(ModelOutput): Args: last_hidden_state (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the model. - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -691,9 +691,9 @@ class TFTransfoXLModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - mems: List[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + mems: list[tf.Tensor] = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -706,7 +706,7 @@ class TFTransfoXLLMHeadModelOutput(ModelOutput): Language modeling losses (not reduced). prediction_scores (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -724,9 +724,9 @@ class TFTransfoXLLMHeadModelOutput(ModelOutput): """ prediction_scores: Optional[tf.Tensor] = None - mems: List[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + mems: list[tf.Tensor] = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -739,7 +739,7 @@ class TFTransfoXLSequenceClassifierOutputWithPast(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -758,9 +758,9 @@ class TFTransfoXLSequenceClassifierOutputWithPast(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + mems: list[tf.Tensor] = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None TRANSFO_XL_START_DOCSTRING = r""" @@ -814,7 +814,7 @@ TRANSFO_XL_INPUTS_DOCSTRING = r""" [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see `mems` output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as `input_ids` as they have already been computed. @@ -863,14 +863,14 @@ class TFTransfoXLModel(TFTransfoXLPreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - mems: List[tf.Tensor] | None = None, + mems: list[tf.Tensor] | None = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> TFTransfoXLModelOutput | Tuple[tf.Tensor]: + ) -> TFTransfoXLModelOutput | tuple[tf.Tensor]: outputs = self.transformer( input_ids=input_ids, mems=mems, @@ -931,7 +931,7 @@ class TFTransfoXLLMHeadModel(TFTransfoXLPreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - mems: List[tf.Tensor] | None = None, + mems: list[tf.Tensor] | None = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: bool | None = None, @@ -939,7 +939,7 @@ class TFTransfoXLLMHeadModel(TFTransfoXLPreTrainedModel): return_dict: bool | None = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> TFTransfoXLLMHeadModelOutput | Tuple[tf.Tensor]: + ) -> TFTransfoXLLMHeadModelOutput | tuple[tf.Tensor]: if input_ids is not None: bsz, tgt_len = shape_list(input_ids)[:2] else: @@ -1044,7 +1044,7 @@ class TFTransfoXLForSequenceClassification(TFTransfoXLPreTrainedModel, TFSequenc def call( self, input_ids: TFModelInputType | None = None, - mems: List[tf.Tensor] | None = None, + mems: list[tf.Tensor] | None = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1052,7 +1052,7 @@ class TFTransfoXLForSequenceClassification(TFTransfoXLPreTrainedModel, TFSequenc return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFTransfoXLSequenceClassifierOutputWithPast]: + ) -> Union[tuple, TFTransfoXLSequenceClassifierOutputWithPast]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deprecated/transfo_xl/modeling_transfo_xl.py b/src/transformers/models/deprecated/transfo_xl/modeling_transfo_xl.py index cf843850c05..664bc577b1c 100644 --- a/src/transformers/models/deprecated/transfo_xl/modeling_transfo_xl.py +++ b/src/transformers/models/deprecated/transfo_xl/modeling_transfo_xl.py @@ -20,7 +20,7 @@ https://github.com/kimiyoung/transformer-xl/blob/master/pytorch/mem_transformer. import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -600,7 +600,7 @@ class TransfoXLModelOutput(ModelOutput): Args: last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the model. - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -618,9 +618,9 @@ class TransfoXLModelOutput(ModelOutput): """ last_hidden_state: torch.FloatTensor - mems: List[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + mems: list[torch.FloatTensor] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -633,7 +633,7 @@ class TransfoXLSequenceClassifierOutputWithPast(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -652,9 +652,9 @@ class TransfoXLSequenceClassifierOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - mems: List[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + mems: list[torch.FloatTensor] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -667,7 +667,7 @@ class TransfoXLLMHeadModelOutput(ModelOutput): Language modeling losses (not reduced). prediction_scores (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token after SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -688,9 +688,9 @@ class TransfoXLLMHeadModelOutput(ModelOutput): losses: Optional[torch.FloatTensor] = None prediction_scores: Optional[torch.FloatTensor] = None - mems: List[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + mems: list[torch.FloatTensor] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None loss: Optional[torch.FloatTensor] = None @property @@ -728,7 +728,7 @@ TRANSFO_XL_INPUTS_DOCSTRING = r""" [`PreTrainedTokenizer.__call__`] for details. [What are input IDs?](../glossary#input-ids) - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see `mems` output below). Can be used to speed up sequential decoding. The token ids which have their mems given to this model should not be passed as `input_ids` as they have already been computed. @@ -869,13 +869,13 @@ class TransfoXLModel(TransfoXLPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - mems: Optional[List[torch.FloatTensor]] = None, + mems: Optional[list[torch.FloatTensor]] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TransfoXLModelOutput]: + ) -> Union[tuple, TransfoXLModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1064,14 +1064,14 @@ class TransfoXLLMHeadModel(TransfoXLPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - mems: Optional[List[torch.FloatTensor]] = None, + mems: Optional[list[torch.FloatTensor]] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TransfoXLLMHeadModelOutput]: + ) -> Union[tuple, TransfoXLLMHeadModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -1163,7 +1163,7 @@ class TransfoXLLMHeadModel(TransfoXLPreTrainedModel): self.crit.n_token = new_num_tokens @staticmethod - def _reorder_cache(mems: List[torch.Tensor], beam_idx: torch.Tensor) -> List[torch.Tensor]: + def _reorder_cache(mems: list[torch.Tensor], beam_idx: torch.Tensor) -> list[torch.Tensor]: """ This function is used to re-order the `mems` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `mems` with the correct beam_idx at every @@ -1205,14 +1205,14 @@ class TransfoXLForSequenceClassification(TransfoXLPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - mems: Optional[List[torch.FloatTensor]] = None, + mems: Optional[list[torch.FloatTensor]] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TransfoXLSequenceClassifierOutputWithPast]: + ) -> Union[tuple, TransfoXLSequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deprecated/transfo_xl/tokenization_transfo_xl.py b/src/transformers/models/deprecated/transfo_xl/tokenization_transfo_xl.py index 44a3c85addf..70e2da01855 100644 --- a/src/transformers/models/deprecated/transfo_xl/tokenization_transfo_xl.py +++ b/src/transformers/models/deprecated/transfo_xl/tokenization_transfo_xl.py @@ -22,7 +22,7 @@ import os import pickle import re from collections import Counter, OrderedDict -from typing import List, Optional, Tuple +from typing import Optional import numpy as np @@ -65,7 +65,7 @@ MATCH_NUMBERS = r"(?<=\d)[,.](?=\d)", r" @\g<0>@ " DETOKENIZE_NUMBERS = [(r" @\,@ ", r","), (r" @\.@ ", r".")] -def tokenize_numbers(text_array: List[str]) -> List[str]: +def tokenize_numbers(text_array: list[str]) -> list[str]: """ Splits large comma-separated numbers and floating point values. This is done by replacing commas with ' @,@ ' and dots with ' @.@ '. @@ -122,7 +122,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer): this superclass for more information regarding those methods. Args: - special (`List[str]`, *optional*): + special (`list[str]`, *optional*): A list of special tokens (to be treated by the original implementation of this tokenizer). min_freq (`int`, *optional*, defaults to 0): The minimum number of times a token has to be present in order to be kept in the vocabulary (otherwise it @@ -138,7 +138,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer): File containing the vocabulary (from the original implementation). pretrained_vocab_file (`str`, *optional*): File containing the vocabulary as saved with the `save_pretrained()` method. - never_split (`List[str]`, *optional*): + never_split (`list[str]`, *optional*): List of tokens that should never be split. If no list is specified, will simply use the existing special tokens. unk_token (`str`, *optional*, defaults to `""`): @@ -146,7 +146,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer): token instead. eos_token (`str`, *optional*, defaults to `""`): The end of sequence token. - additional_special_tokens (`List[str]`, *optional*, defaults to `['']`): + additional_special_tokens (`list[str]`, *optional*, defaults to `['']`): A list of additional special tokens (for the HuggingFace functionality). language (`str`, *optional*, defaults to `"en"`): The language of this tokenizer (used for mose preprocessing). @@ -315,7 +315,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer): else: raise ValueError("Token not in vocabulary and no token in vocabulary for replacement.") - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if os.path.isdir(save_directory): vocab_file = os.path.join( save_directory, @@ -425,7 +425,7 @@ class TransfoXLTokenizer(PreTrainedTokenizer): text, aggressive_dash_splits=True, return_str=False, escape=False, protected_patterns=self.never_split ) - def moses_pipeline(self, text: str) -> List[str]: + def moses_pipeline(self, text: str) -> list[str]: """ Does basic tokenization using [`sacremoses.MosesPunctNormalizer`] and [`sacremoses.MosesTokenizer`] with *aggressive_dash_splits=True* (see [`sacremoses.tokenize.MosesTokenizer.tokenize`]). Additionally, large diff --git a/src/transformers/models/deprecated/tvlt/configuration_tvlt.py b/src/transformers/models/deprecated/tvlt/configuration_tvlt.py index bf159fa7e0b..57144e349f5 100644 --- a/src/transformers/models/deprecated/tvlt/configuration_tvlt.py +++ b/src/transformers/models/deprecated/tvlt/configuration_tvlt.py @@ -38,9 +38,9 @@ class TvltConfig(PretrainedConfig): The time length of each audio spectrogram. frequency_length (`int`, *optional*, defaults to 128): The frequency length of audio spectrogram. - image_patch_size (`List[int]`, *optional*, defaults to `[16, 16]`): + image_patch_size (`list[int]`, *optional*, defaults to `[16, 16]`): The size (resolution) of each image patch. - audio_patch_size (`List[int]`, *optional*, defaults to `[16, 16]`): + audio_patch_size (`list[int]`, *optional*, defaults to `[16, 16]`): The size (resolution) of each audio patch. num_image_channels (`int`, *optional*, defaults to 3): The number of input image channels. diff --git a/src/transformers/models/deprecated/tvlt/feature_extraction_tvlt.py b/src/transformers/models/deprecated/tvlt/feature_extraction_tvlt.py index 6e1c49d708c..086c411feb0 100644 --- a/src/transformers/models/deprecated/tvlt/feature_extraction_tvlt.py +++ b/src/transformers/models/deprecated/tvlt/feature_extraction_tvlt.py @@ -15,7 +15,7 @@ """Feature extractor class for TVLT.""" from math import ceil -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -35,11 +35,11 @@ class TvltFeatureExtractor(SequenceFeatureExtractor): should refer to this superclass for more information regarding those methods. Args: - spectrogram_length (`Dict[str, int]` *optional*, defaults to 2048): + spectrogram_length (`dict[str, int]` *optional*, defaults to 2048): The time length of each audio spectrogram. num_channels (`int` *optional*, defaults to 1): Number of audio channels. - patch_size (`List[int]` *optional*, defaults to `[16, 16]`): + patch_size (`list[int]` *optional*, defaults to `[16, 16]`): The patch size of audio patch embedding. feature_size (`int`, *optional*, defaults to 128): The frequency length of audio spectrogram. @@ -115,7 +115,7 @@ class TvltFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], return_tensors: Optional[Union[str, TensorType]] = None, return_attention_mask: Optional[bool] = True, sampling_rate: Optional[int] = None, @@ -127,7 +127,7 @@ class TvltFeatureExtractor(SequenceFeatureExtractor): Main method to prepare one or several audio(s) for the model. Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -197,7 +197,7 @@ class TvltFeatureExtractor(SequenceFeatureExtractor): audio_features = [ self._np_extract_fbank_features(waveform.squeeze()).T[: self.spectrogram_length] for waveform in raw_speech ] - if isinstance(audio_features[0], List): + if isinstance(audio_features[0], list): audio_features = [np.asarray(feature, dtype=np.float32) for feature in audio_features] # Create audio attention mask diff --git a/src/transformers/models/deprecated/tvlt/image_processing_tvlt.py b/src/transformers/models/deprecated/tvlt/image_processing_tvlt.py index a10b9b3b211..d142939ee39 100644 --- a/src/transformers/models/deprecated/tvlt/image_processing_tvlt.py +++ b/src/transformers/models/deprecated/tvlt/image_processing_tvlt.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for TVLT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -44,7 +44,7 @@ from ....utils import TensorType, logging logger = logging.get_logger(__name__) -def make_batched(videos) -> List[List[ImageInput]]: +def make_batched(videos) -> list[list[ImageInput]]: if isinstance(videos, (list, tuple)) and isinstance(videos[0], (list, tuple)): return videos @@ -77,11 +77,11 @@ class TvltImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the output image after resizing. The shortest edge of the image will be resized to `size["shortest_edge"]` while maintaining the aspect ratio of the original image. Can be overridden by `size` in the `preprocess` method. - patch_size (`List[int]` *optional*, defaults to [16,16]): + patch_size (`list[int]` *optional*, defaults to [16,16]): The patch size of image patch embedding. num_frames (`int` *optional*, defaults to 8): The maximum number of video frames. @@ -91,7 +91,7 @@ class TvltImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after applying the center crop. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -103,10 +103,10 @@ class TvltImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -121,17 +121,17 @@ class TvltImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - patch_size: List[int] = [16, 16], + size: Optional[dict[str, int]] = None, + patch_size: list[int] = [16, 16], num_frames: int = 8, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = IMAGENET_STANDARD_MEAN, - image_std: Optional[Union[float, List[float]]] = IMAGENET_STANDARD_STD, + image_mean: Optional[Union[float, list[float]]] = IMAGENET_STANDARD_MEAN, + image_std: Optional[Union[float, list[float]]] = IMAGENET_STANDARD_STD, init_mask_generator=False, **kwargs, ) -> None: @@ -176,7 +176,7 @@ class TvltImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -188,7 +188,7 @@ class TvltImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. If `size` is of the form `{"height": h, "width": w}`, the output image will have the size `(h, w)`. If `size` is of the form `{"shortest_edge": s}`, the output image will have its shortest edge of length `s` while keeping the aspect ratio of the original image. @@ -221,15 +221,15 @@ class TvltImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -278,17 +278,17 @@ class TvltImageProcessor(BaseImageProcessor): self, videos: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, - patch_size: Optional[List[int]] = None, + size: Optional[dict[str, int]] = None, + patch_size: Optional[list[int]] = None, num_frames: Optional[int] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, is_mixed: bool = False, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -304,9 +304,9 @@ class TvltImageProcessor(BaseImageProcessor): 255. If passing in frames with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after applying resize. - patch_size (`List[int]` *optional*, defaults to self.patch_size): + patch_size (`list[int]` *optional*, defaults to self.patch_size): The patch size of image patch embedding. num_frames (`int` *optional*, defaults to self.num_frames): The maximum number of video frames. @@ -315,7 +315,7 @@ class TvltImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_centre_crop`): Whether to centre crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after applying the centre crop. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -323,9 +323,9 @@ class TvltImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. is_mixed (`bool`, *optional*): If the input video has negative samples. diff --git a/src/transformers/models/deprecated/tvlt/modeling_tvlt.py b/src/transformers/models/deprecated/tvlt/modeling_tvlt.py index 279224ac4d2..d0083211bd3 100644 --- a/src/transformers/models/deprecated/tvlt/modeling_tvlt.py +++ b/src/transformers/models/deprecated/tvlt/modeling_tvlt.py @@ -18,7 +18,7 @@ import collections.abc import math from copy import deepcopy from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -82,8 +82,8 @@ class TvltModelOutput(ModelOutput): audio_label_masks: Optional[torch.LongTensor] = None pixel_ids_restore: Optional[torch.LongTensor] = None audio_ids_restore: Optional[torch.LongTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -105,8 +105,8 @@ class TvltDecoderOutput(ModelOutput): """ logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -139,8 +139,8 @@ class TvltForPreTrainingOutput(ModelOutput): matching_logits: Optional[torch.FloatTensor] = None pixel_logits: Optional[torch.FloatTensor] = None audio_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None def generate_pixel_mask_noise(pixel_values, pixel_mask=None, mask_ratio=0.75): @@ -701,7 +701,7 @@ class TvltModel(TvltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], TvltModelOutput]: + ) -> Union[tuple[torch.FloatTensor], TvltModelOutput]: r""" Returns: @@ -1030,7 +1030,7 @@ class TvltForPreTraining(TvltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], TvltForPreTrainingOutput]: + ) -> Union[tuple[torch.FloatTensor], TvltForPreTrainingOutput]: r""" pixel_values_mixed (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, height, width)`): Pixel values that mix positive and negative samples in Tvlt vision-audio matching. Audio values can be @@ -1229,7 +1229,7 @@ class TvltForAudioVisualClassification(TvltPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.FloatTensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, num_labels)`, *optional*): Labels for computing the audiovisual loss. Indices should be in `[0, ..., num_classes-1]` where num_classes diff --git a/src/transformers/models/deprecated/van/configuration_van.py b/src/transformers/models/deprecated/van/configuration_van.py index 08f1db7a4b4..be94e45ec63 100644 --- a/src/transformers/models/deprecated/van/configuration_van.py +++ b/src/transformers/models/deprecated/van/configuration_van.py @@ -36,15 +36,15 @@ class VanConfig(PretrainedConfig): The size (resolution) of each image. num_channels (`int`, *optional*, defaults to 3): The number of input channels. - patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3, 3]`): + patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3, 3]`): Patch size to use in each stage's embedding layer. - strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + strides (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Stride size to use in each stage's embedding layer to downsample the input. - hidden_sizes (`List[int]`, *optional*, defaults to `[64, 128, 320, 512]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[64, 128, 320, 512]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 3, 12, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 3, 12, 3]`): Depth (number of layers) for each stage. - mlp_ratios (`List[int]`, *optional*, defaults to `[8, 8, 4, 4]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[8, 8, 4, 4]`): The expansion ratio for mlp layer at each stage. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): The non-linear activation function (function or string) in each layer. If string, `"gelu"`, `"relu"`, diff --git a/src/transformers/models/deprecated/van/convert_van_to_pytorch.py b/src/transformers/models/deprecated/van/convert_van_to_pytorch.py index b509d60d124..bdd357f04a7 100644 --- a/src/transformers/models/deprecated/van/convert_van_to_pytorch.py +++ b/src/transformers/models/deprecated/van/convert_van_to_pytorch.py @@ -22,7 +22,7 @@ import sys from dataclasses import dataclass, field from functools import partial from pathlib import Path -from typing import List, Optional +from typing import Optional import torch import torch.nn as nn @@ -41,7 +41,7 @@ logger = logging.get_logger(__name__) @dataclass class Tracker: module: nn.Module - traced: List[nn.Module] = field(default_factory=list) + traced: list[nn.Module] = field(default_factory=list) handles: list = field(default_factory=list) def _forward_hook(self, m, inputs: Tensor, outputs: Tensor): @@ -68,8 +68,8 @@ class ModuleTransfer: src: nn.Module dest: nn.Module verbose: int = 0 - src_skip: List = field(default_factory=list) - dest_skip: List = field(default_factory=list) + src_skip: list = field(default_factory=list) + dest_skip: list = field(default_factory=list) def __call__(self, x: Tensor): """ diff --git a/src/transformers/models/deprecated/van/modeling_van.py b/src/transformers/models/deprecated/van/modeling_van.py index 8a488588524..b31b0616ffa 100644 --- a/src/transformers/models/deprecated/van/modeling_van.py +++ b/src/transformers/models/deprecated/van/modeling_van.py @@ -16,7 +16,7 @@ import math from collections import OrderedDict -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -340,7 +340,7 @@ class VanEncoder(nn.Module): hidden_state: torch.Tensor, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: all_hidden_states = () if output_hidden_states else None for _, stage_module in enumerate(self.stages): @@ -436,7 +436,7 @@ class VanModel(VanPreTrainedModel): pixel_values: Optional[torch.FloatTensor], output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -493,7 +493,7 @@ class VanForImageClassification(VanPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/deprecated/vit_hybrid/configuration_vit_hybrid.py b/src/transformers/models/deprecated/vit_hybrid/configuration_vit_hybrid.py index 65b6a3e5ef5..c66367cc0ad 100644 --- a/src/transformers/models/deprecated/vit_hybrid/configuration_vit_hybrid.py +++ b/src/transformers/models/deprecated/vit_hybrid/configuration_vit_hybrid.py @@ -34,7 +34,7 @@ class ViTHybridConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - backbone_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + backbone_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the backbone in a dictionary or the config object of the backbone. backbone (`str`, *optional*): Name of backbone to use when `backbone_config` is `None`. If `use_pretrained_backbone` is `True`, this @@ -73,7 +73,7 @@ class ViTHybridConfig(PretrainedConfig): The size (resolution) of each patch. num_channels (`int`, *optional*, defaults to 3): The number of input channels. - backbone_featmap_shape (`List[int]`, *optional*, defaults to `[1, 1024, 24, 24]`): + backbone_featmap_shape (`list[int]`, *optional*, defaults to `[1, 1024, 24, 24]`): Used only for the `hybrid` embedding type. The shape of the feature maps of the backbone. qkv_bias (`bool`, *optional*, defaults to `True`): Whether to add a bias to the queries, keys and values. diff --git a/src/transformers/models/deprecated/vit_hybrid/image_processing_vit_hybrid.py b/src/transformers/models/deprecated/vit_hybrid/image_processing_vit_hybrid.py index 9f644bfc563..f1e5a9c8e6d 100644 --- a/src/transformers/models/deprecated/vit_hybrid/image_processing_vit_hybrid.py +++ b/src/transformers/models/deprecated/vit_hybrid/image_processing_vit_hybrid.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for ViT hybrid.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -57,7 +57,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -66,7 +66,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -77,10 +77,10 @@ class ViTHybridImageProcessor(BaseImageProcessor): method. do_normalize: Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -93,15 +93,15 @@ class ViTHybridImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -143,7 +143,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -156,7 +156,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -193,15 +193,15 @@ class ViTHybridImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -217,7 +217,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -225,7 +225,7 @@ class ViTHybridImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -233,9 +233,9 @@ class ViTHybridImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/deprecated/vit_hybrid/modeling_vit_hybrid.py b/src/transformers/models/deprecated/vit_hybrid/modeling_vit_hybrid.py index c4d25110193..553b0a7bb3b 100644 --- a/src/transformers/models/deprecated/vit_hybrid/modeling_vit_hybrid.py +++ b/src/transformers/models/deprecated/vit_hybrid/modeling_vit_hybrid.py @@ -16,7 +16,7 @@ import collections.abc import math -from typing import Dict, List, Optional, Set, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -225,7 +225,7 @@ class ViTHybridSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -266,7 +266,7 @@ class ViTHybridSdpaSelfAttention(ViTHybridSelfAttention): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -315,7 +315,7 @@ class ViTHybridAttention(nn.Module): self.output = ViTHybridSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -338,7 +338,7 @@ class ViTHybridAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -408,7 +408,7 @@ class ViTHybridLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViTHybrid, layernorm is applied before self-attention head_mask, @@ -579,7 +579,7 @@ class ViTHybridModel(ViTHybridPreTrainedModel): def get_input_embeddings(self) -> ViTHybridPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -604,7 +604,7 @@ class ViTHybridModel(ViTHybridPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). diff --git a/src/transformers/models/deprecated/xlm_prophetnet/modeling_xlm_prophetnet.py b/src/transformers/models/deprecated/xlm_prophetnet/modeling_xlm_prophetnet.py index 17bc9ffada6..e8b23b961e5 100644 --- a/src/transformers/models/deprecated/xlm_prophetnet/modeling_xlm_prophetnet.py +++ b/src/transformers/models/deprecated/xlm_prophetnet/modeling_xlm_prophetnet.py @@ -18,7 +18,7 @@ import copy import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -259,7 +259,7 @@ class XLMProphetNetSeq2SeqLMOutput(ModelOutput): logits_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Prediction scores of the predict stream language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -310,15 +310,15 @@ class XLMProphetNetSeq2SeqLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None logits_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @property def decoder_cross_attentions(self): @@ -344,7 +344,7 @@ class XLMProphetNetSeq2SeqModelOutput(ModelOutput): hidden_size)` is output. last_hidden_state_ngram (`torch.FloatTensor` of shape `(batch_size,ngram * decoder_sequence_length, config.vocab_size)`, *optional*): Sequence of predict stream hidden-states at the output of the last layer of the decoder of the model. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -396,15 +396,15 @@ class XLMProphetNetSeq2SeqModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor last_hidden_state_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @property def decoder_cross_attentions(self): @@ -429,7 +429,7 @@ class XLMProphetNetDecoderModelOutput(ModelOutput): hidden_size)` is output. last_hidden_state_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Sequence of predict stream hidden-states at the output of the last layer of the decoder of the model. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -468,12 +468,12 @@ class XLMProphetNetDecoderModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor last_hidden_state_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_ngram: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_ngram: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -490,7 +490,7 @@ class XLMProphetNetDecoderLMOutput(ModelOutput): logits_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Prediction scores of the predict stream language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -530,12 +530,12 @@ class XLMProphetNetDecoderLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None logits_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_ngram: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_ngram: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None class XLMProphetNetPreTrainedModel(PreTrainedModel): @@ -655,9 +655,9 @@ class XLMProphetNetAttention(nn.Module): key_value_states: Optional[Tensor] = None, attention_mask: Optional[Tensor] = None, layer_head_mask: Optional[Tensor] = None, - past_key_value: Optional[Tuple[Tensor]] = None, + past_key_value: Optional[tuple[Tensor]] = None, output_attentions: bool = False, - ) -> Tuple[Tensor, Optional[Tensor]]: + ) -> tuple[Tensor, Optional[Tensor]]: batch_size, tgt_len, hidden_size = hidden_states.size() # if key_value_states are provided this layer is used as a cross-attention layer @@ -810,7 +810,7 @@ class XLMProphetNetNgramSelfAttention(nn.Module): def forward( self, hidden_states, - past_key_value: Optional[Tuple[Tensor]] = None, + past_key_value: Optional[tuple[Tensor]] = None, attention_mask=None, layer_head_mask=None, extended_predict_attention_mask=None, @@ -1262,7 +1262,7 @@ class XLMProphetNetEncoder(XLMProphetNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Returns: @@ -1402,13 +1402,13 @@ class XLMProphetNetDecoder(XLMProphetNetPreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XLMProphetNetDecoderModelOutput]: + ) -> Union[tuple, XLMProphetNetDecoderModelOutput]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1773,15 +1773,15 @@ class XLMProphetNetModel(XLMProphetNetPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XLMProphetNetSeq2SeqModelOutput]: + ) -> Union[tuple, XLMProphetNetSeq2SeqModelOutput]: r""" Returns: @@ -1896,7 +1896,7 @@ class XLMProphetNetForConditionalGeneration(XLMProphetNetPreTrainedModel): decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, encoder_outputs: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -1904,7 +1904,7 @@ class XLMProphetNetForConditionalGeneration(XLMProphetNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XLMProphetNetSeq2SeqLMOutput]: + ) -> Union[tuple, XLMProphetNetSeq2SeqLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[-100, 0, ..., @@ -2128,14 +2128,14 @@ class XLMProphetNetForCausalLM(XLMProphetNetPreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XLMProphetNetDecoderLMOutput]: + ) -> Union[tuple, XLMProphetNetDecoderLMOutput]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if diff --git a/src/transformers/models/deprecated/xlm_prophetnet/tokenization_xlm_prophetnet.py b/src/transformers/models/deprecated/xlm_prophetnet/tokenization_xlm_prophetnet.py index 4b452554ea0..77431b13c49 100644 --- a/src/transformers/models/deprecated/xlm_prophetnet/tokenization_xlm_prophetnet.py +++ b/src/transformers/models/deprecated/xlm_prophetnet/tokenization_xlm_prophetnet.py @@ -16,7 +16,7 @@ import collections import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional from ....tokenization_utils import PreTrainedTokenizer from ....utils import logging @@ -120,7 +120,7 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): pad_token="[PAD]", cls_token="[CLS]", mask_token="[MASK]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -193,22 +193,22 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): self.sp_model.Load(self.vocab_file) def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -221,20 +221,20 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLMProphetNet does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -276,7 +276,7 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -294,8 +294,8 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): return (out_vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A XLMProphetNet sequence has the following format: @@ -304,13 +304,13 @@ class XLMProphetNetTokenizer(PreTrainedTokenizer): - pair of sequences: `A [SEP] B [SEP]` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: diff --git a/src/transformers/models/depth_anything/configuration_depth_anything.py b/src/transformers/models/depth_anything/configuration_depth_anything.py index 3bbe621a443..b28508cbe9f 100644 --- a/src/transformers/models/depth_anything/configuration_depth_anything.py +++ b/src/transformers/models/depth_anything/configuration_depth_anything.py @@ -36,7 +36,7 @@ class DepthAnythingConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - backbone_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + backbone_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the backbone model. Only used in case `is_hybrid` is `True` or in case you want to leverage the [`AutoBackbone`] API. backbone (`str`, *optional*): @@ -57,9 +57,9 @@ class DepthAnythingConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. reassemble_hidden_size (`int`, *optional*, defaults to 384): The number of input channels of the reassemble layers. - reassemble_factors (`List[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): + reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): The up/downsampling factors of the reassemble layers. - neck_hidden_sizes (`List[str]`, *optional*, defaults to `[48, 96, 192, 384]`): + neck_hidden_sizes (`list[str]`, *optional*, defaults to `[48, 96, 192, 384]`): The hidden sizes to project to for the feature maps of the backbone. fusion_hidden_size (`int`, *optional*, defaults to 64): The number of channels before fusion. @@ -154,7 +154,7 @@ class DepthAnythingConfig(PretrainedConfig): def to_dict(self): """ Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) diff --git a/src/transformers/models/depth_anything/modeling_depth_anything.py b/src/transformers/models/depth_anything/modeling_depth_anything.py index 71ff5df6ba8..f5ace462120 100644 --- a/src/transformers/models/depth_anything/modeling_depth_anything.py +++ b/src/transformers/models/depth_anything/modeling_depth_anything.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Depth Anything model.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -77,10 +77,10 @@ class DepthAnythingReassembleStage(nn.Module): for channels, factor in zip(config.neck_hidden_sizes, config.reassemble_factors): self.layers.append(DepthAnythingReassembleLayer(config, channels=channels, factor=factor)) - def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height=None, patch_width=None) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): List of hidden states from the backbone. """ out = [] @@ -254,10 +254,10 @@ class DepthAnythingNeck(nn.Module): # fusion self.fusion_stage = DepthAnythingFeatureFusionStage(config) - def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height=None, patch_width=None) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): List of hidden states from the backbone. """ if not isinstance(hidden_states, (tuple, list)): @@ -304,7 +304,7 @@ class DepthAnythingDepthEstimationHead(nn.Module): raise ValueError(f"Unknown depth estimation type: {config.depth_estimation_type}") self.max_depth = config.max_depth - def forward(self, hidden_states: List[torch.Tensor], patch_height, patch_width) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor], patch_height, patch_width) -> torch.Tensor: hidden_states = hidden_states[self.head_in_index] predicted_depth = self.conv1(hidden_states) @@ -349,7 +349,7 @@ class DepthAnythingForDepthEstimation(DepthAnythingPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth depth estimation maps for computing the loss. diff --git a/src/transformers/models/depth_pro/configuration_depth_pro.py b/src/transformers/models/depth_pro/configuration_depth_pro.py index 64b58d55206..bbd2eea60b9 100644 --- a/src/transformers/models/depth_pro/configuration_depth_pro.py +++ b/src/transformers/models/depth_pro/configuration_depth_pro.py @@ -41,15 +41,15 @@ class DepthProConfig(PretrainedConfig): The size (resolution) of each patch. This is also the image_size for backbone model. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - intermediate_hook_ids (`List[int]`, *optional*, defaults to `[11, 5]`): + intermediate_hook_ids (`list[int]`, *optional*, defaults to `[11, 5]`): Indices of the intermediate hidden states from the patch encoder to use for fusion. - intermediate_feature_dims (`List[int]`, *optional*, defaults to `[256, 256]`): + intermediate_feature_dims (`list[int]`, *optional*, defaults to `[256, 256]`): Hidden state dimensions during upsampling for each intermediate hidden state in `intermediate_hook_ids`. - scaled_images_ratios (`List[float]`, *optional*, defaults to `[0.25, 0.5, 1]`): + scaled_images_ratios (`list[float]`, *optional*, defaults to `[0.25, 0.5, 1]`): Ratios of scaled images to be used by the patch encoder. - scaled_images_overlap_ratios (`List[float]`, *optional*, defaults to `[0.0, 0.5, 0.25]`): + scaled_images_overlap_ratios (`list[float]`, *optional*, defaults to `[0.0, 0.5, 0.25]`): Overlap ratios between patches for each scaled image in `scaled_images_ratios`. - scaled_images_feature_dims (`List[int]`, *optional*, defaults to `[1024, 1024, 512]`): + scaled_images_feature_dims (`list[int]`, *optional*, defaults to `[1024, 1024, 512]`): Hidden state dimensions during upsampling for each scaled image in `scaled_images_ratios`. merge_padding_value (`int`, *optional*, defaults to 3): When merging smaller patches back to the image size, overlapping sections of this size are removed. @@ -61,13 +61,13 @@ class DepthProConfig(PretrainedConfig): Whether to use `DepthProFovModel` to generate the field of view. num_fov_head_layers (`int`, *optional*, defaults to 2): Number of convolution layers in the head of `DepthProFovModel`. - image_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + image_model_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the image encoder model, which is loaded using the [`AutoModel`] API. By default, Dinov2 model is used as backbone. - patch_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + patch_model_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the patch encoder model, which is loaded using the [`AutoModel`] API. By default, Dinov2 model is used as backbone. - fov_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + fov_model_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the fov encoder model, which is loaded using the [`AutoModel`] API. By default, Dinov2 model is used as backbone. diff --git a/src/transformers/models/depth_pro/image_processing_depth_pro.py b/src/transformers/models/depth_pro/image_processing_depth_pro.py index 7d3440bde33..f4002a9e4c7 100644 --- a/src/transformers/models/depth_pro/image_processing_depth_pro.py +++ b/src/transformers/models/depth_pro/image_processing_depth_pro.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for DepthPro.""" -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -76,10 +76,10 @@ class DepthProImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -89,13 +89,13 @@ class DepthProImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ): super().__init__(**kwargs) @@ -113,7 +113,7 @@ class DepthProImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -125,7 +125,7 @@ class DepthProImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -170,13 +170,13 @@ class DepthProImageProcessor(BaseImageProcessor): def _validate_input_arguments( self, do_resize: bool, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling, do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Union[float, List[float]], - image_std: Union[float, List[float]], + image_mean: Union[float, list[float]], + image_std: Union[float, list[float]], data_format: Union[str, ChannelDimension], ): if do_resize and None in (size, resample): @@ -193,13 +193,13 @@ class DepthProImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: Optional[PILImageResampling] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -213,7 +213,7 @@ class DepthProImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -225,9 +225,9 @@ class DepthProImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: @@ -317,8 +317,8 @@ class DepthProImageProcessor(BaseImageProcessor): def post_process_depth_estimation( self, outputs: "DepthProDepthEstimatorOutput", - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - ) -> Dict[str, List[TensorType]]: + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + ) -> dict[str, list[TensorType]]: """ Post-processes the raw depth predictions from the model to generate final depth predictions which is caliberated using the field of view if provided @@ -327,13 +327,13 @@ class DepthProImageProcessor(BaseImageProcessor): Args: outputs ([`DepthProDepthEstimatorOutput`]): Raw outputs of the model. - target_sizes (`Optional[Union[TensorType, List[Tuple[int, int]], None]]`, *optional*, defaults to `None`): + target_sizes (`Optional[Union[TensorType, list[tuple[int, int]], None]]`, *optional*, defaults to `None`): Target sizes to resize the depth predictions. Can be a tensor of shape `(batch_size, 2)` or a list of tuples `(height, width)` for each image in the batch. If `None`, no resizing is performed. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions, and field of view (degrees) and focal length (pixels) if `field_of_view` is given in `outputs`. Raises: diff --git a/src/transformers/models/depth_pro/image_processing_depth_pro_fast.py b/src/transformers/models/depth_pro/image_processing_depth_pro_fast.py index 669fcc00eb8..338bf506c17 100644 --- a/src/transformers/models/depth_pro/image_processing_depth_pro_fast.py +++ b/src/transformers/models/depth_pro/image_processing_depth_pro_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for DepthPro.""" -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...image_processing_base import BatchFeature from ...image_processing_utils_fast import ( @@ -74,7 +74,7 @@ class DepthProImageProcessorFast(BaseImageProcessorFast): # which makes it different from BaseImageProcessorFast._preprocess def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, interpolation: Optional["F.InterpolationMode"], @@ -83,8 +83,8 @@ class DepthProImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: # Group images by size for batched scaling @@ -113,8 +113,8 @@ class DepthProImageProcessorFast(BaseImageProcessorFast): def post_process_depth_estimation( self, outputs: "DepthProDepthEstimatorOutput", - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - ) -> Dict[str, List[TensorType]]: + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + ) -> dict[str, list[TensorType]]: """ Post-processes the raw depth predictions from the model to generate final depth predictions which is caliberated using the field of view if provided @@ -123,13 +123,13 @@ class DepthProImageProcessorFast(BaseImageProcessorFast): Args: outputs ([`DepthProDepthEstimatorOutput`]): Raw outputs of the model. - target_sizes (`Optional[Union[TensorType, List[Tuple[int, int]], None]]`, *optional*, defaults to `None`): + target_sizes (`Optional[Union[TensorType, list[tuple[int, int]], None]]`, *optional*, defaults to `None`): Target sizes to resize the depth predictions. Can be a tensor of shape `(batch_size, 2)` or a list of tuples `(height, width)` for each image in the batch. If `None`, no resizing is performed. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions, and field of view (degrees) and focal length (pixels) if `field_of_view` is given in `outputs`. Raises: diff --git a/src/transformers/models/depth_pro/modeling_depth_pro.py b/src/transformers/models/depth_pro/modeling_depth_pro.py index 9eef406a669..e9d513991aa 100644 --- a/src/transformers/models/depth_pro/modeling_depth_pro.py +++ b/src/transformers/models/depth_pro/modeling_depth_pro.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -39,7 +39,7 @@ class DepthProOutput(ModelOutput): Args: last_hidden_state (`torch.FloatTensor` of shape `(batch_size, n_patches_per_batch, sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the model. - features (`Union[torch.FloatTensor, List[torch.FloatTensor]]`, *optional*): + features (`Union[torch.FloatTensor, list[torch.FloatTensor]]`, *optional*): Features from encoders. Can be a single feature or a list of features. hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + @@ -55,9 +55,9 @@ class DepthProOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - features: Union[torch.FloatTensor, List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + features: Union[torch.FloatTensor, list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -88,8 +88,8 @@ class DepthProDepthEstimatorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None predicted_depth: Optional[torch.FloatTensor] = None field_of_view: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None def split_to_patches(pixel_values: torch.Tensor, patch_size: int, overlap_ratio: float) -> torch.Tensor: @@ -200,7 +200,7 @@ def merge_patches(patches: torch.Tensor, batch_size: int, padding: int) -> torch def reconstruct_feature_maps( - hidden_state: torch.Tensor, batch_size: int, padding: int, output_size: Tuple[float, float] + hidden_state: torch.Tensor, batch_size: int, padding: int, output_size: tuple[float, float] ) -> torch.Tensor: """ Reconstructs feature maps from the hidden state produced by any of the encoder. Converts the hidden state of shape @@ -212,7 +212,7 @@ def reconstruct_feature_maps( representing the encoded patches. batch_size (int): The number of samples in a batch. padding (int): The amount of padding to be removed when merging patches. - output_size (Tuple[float, float]): The desired output size for the feature maps, specified as `(height, width)`. + output_size (tuple[float, float]): The desired output size for the feature maps, specified as `(height, width)`. Returns: torch.Tensor: Reconstructed feature maps of shape `(batch_size, hidden_size, output_size[0], output_size[1])`. @@ -260,7 +260,7 @@ class DepthProPatchEncoder(nn.Module): self, pixel_values: torch.Tensor, head_mask: Optional[torch.Tensor] = None, - ) -> List[torch.Tensor]: + ) -> list[torch.Tensor]: batch_size, num_channels, height, width = pixel_values.shape if min(self.scaled_images_ratios) * min(height, width) < self.config.patch_size: @@ -553,7 +553,7 @@ class DepthProFeatureUpsample(nn.Module): ) self.intermediate.append(block) - def forward(self, features: List[torch.Tensor]) -> List[torch.Tensor]: + def forward(self, features: list[torch.Tensor]) -> list[torch.Tensor]: features[0] = self.image_block(features[0]) for i in range(self.n_scaled_images): @@ -588,7 +588,7 @@ class DepthProFeatureProjection(nn.Module): ) ) - def forward(self, features: List[torch.Tensor]) -> List[torch.Tensor]: + def forward(self, features: list[torch.Tensor]) -> list[torch.Tensor]: projected_features = [] for i, projection in enumerate(self.projections): upsampled_feature = projection(features[i]) @@ -612,7 +612,7 @@ class DepthProNeck(nn.Module): ) self.feature_projection = DepthProFeatureProjection(config) - def forward(self, features: List[torch.Tensor]) -> List[torch.Tensor]: + def forward(self, features: list[torch.Tensor]) -> list[torch.Tensor]: features = self.feature_upsample(features) # global features = low res features + image features global_features = torch.cat((features[1], features[0]), dim=1) @@ -673,7 +673,7 @@ class DepthProModel(DepthProPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, DepthProOutput]: + ) -> Union[tuple, DepthProOutput]: r""" Examples: @@ -839,7 +839,7 @@ class DepthProFeatureFusionStage(nn.Module): # final layer does not require deconvolution self.final = DepthProFeatureFusionLayer(config, use_deconv=False) - def forward(self, hidden_states: List[torch.Tensor]) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor]) -> list[torch.Tensor]: if self.num_layers != len(hidden_states): raise ValueError( f"num_layers={self.num_layers} in DepthProFeatureFusionStage" @@ -1060,7 +1060,7 @@ class DepthProForDepthEstimation(DepthProPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthProDepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthProDepthEstimatorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth depth estimation maps for computing the loss. diff --git a/src/transformers/models/detr/image_processing_detr.py b/src/transformers/models/detr/image_processing_detr.py index 50a4b27341c..4fd1fef6eaf 100644 --- a/src/transformers/models/detr/image_processing_detr.py +++ b/src/transformers/models/detr/image_processing_detr.py @@ -18,7 +18,7 @@ import io import pathlib from collections import defaultdict from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -87,12 +87,12 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationForma # From the original repo: https://github.com/facebookresearch/detr/blob/3af9fa878e73b6894ce3596450a8d9b89d918ca9/datasets/transforms.py#L76 -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -131,7 +131,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -163,10 +163,10 @@ def get_image_size_for_max_height_width( def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -175,7 +175,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -226,7 +226,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: return arr -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -241,7 +241,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.vilt.image_processing_vilt.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -250,8 +250,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.vilt.image_processing_vilt.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -269,7 +269,7 @@ def get_max_height_width( # Copied from transformers.models.vilt.image_processing_vilt.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -277,7 +277,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -292,7 +292,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -418,11 +418,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for DETR. """ @@ -458,7 +458,7 @@ def prepare_coco_panoptic_annotation( def get_segmentation_image( - masks: np.ndarray, input_size: Tuple, target_size: Tuple, stuff_equiv_classes, deduplicate=False + masks: np.ndarray, input_size: tuple, target_size: tuple, stuff_equiv_classes, deduplicate=False ): h, w = input_size final_h, final_w = target_size @@ -482,7 +482,7 @@ def get_segmentation_image( return seg_img -def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: int) -> np.ndarray: +def get_mask_area(seg_img: np.ndarray, target_size: tuple[int, int], n_classes: int) -> np.ndarray: final_h, final_w = target_size np_seg_img = seg_img.astype(np.uint8) np_seg_img = np_seg_img.reshape(final_h, final_w, 3) @@ -491,7 +491,7 @@ def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: return area -def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def score_labels_from_class_probabilities(logits: np.ndarray) -> tuple[np.ndarray, np.ndarray]: probs = scipy.special.softmax(logits, axis=-1) labels = probs.argmax(-1, keepdims=True) scores = np.take_along_axis(probs, labels, axis=-1) @@ -503,11 +503,11 @@ def post_process_panoptic_sample( out_logits: np.ndarray, masks: np.ndarray, boxes: np.ndarray, - processed_size: Tuple[int, int], - target_size: Tuple[int, int], - is_thing_map: Dict, + processed_size: tuple[int, int], + target_size: tuple[int, int], + is_thing_map: dict, threshold=0.85, -) -> Dict: +) -> dict: """ Converts the output of [`DetrForSegmentation`] into panoptic segmentation predictions for a single sample. @@ -519,10 +519,10 @@ def post_process_panoptic_sample( boxes (`torch.Tensor`): The prediced bounding boxes for this sample. The boxes are in the normalized format `(center_x, center_y, width, height)` and values between `[0, 1]`, relative to the size the image (disregarding padding). - processed_size (`Tuple[int, int]`): + processed_size (`tuple[int, int]`): The processed size of the image `(height, width)`, as returned by the preprocessing step i.e. the size after data augmentation but before batching. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, `(height, width)` corresponding to the requested final size of the prediction. is_thing_map (`Dict`): @@ -585,9 +585,9 @@ def post_process_panoptic_sample( def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -595,11 +595,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -667,7 +667,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -697,7 +697,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -732,14 +732,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -753,7 +753,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -797,7 +797,7 @@ class DetrImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's `(height, width)` dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -819,10 +819,10 @@ class DetrImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to True): Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -834,7 +834,7 @@ class DetrImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -846,16 +846,16 @@ class DetrImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -913,7 +913,7 @@ class DetrImageProcessor(BaseImageProcessor): ] @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `DetrImageProcessor.from_pretrained(checkpoint, size=600, @@ -929,12 +929,12 @@ class DetrImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DETR model. """ @@ -961,7 +961,7 @@ class DetrImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -974,7 +974,7 @@ class DetrImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1032,7 +1032,7 @@ class DetrImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -1068,7 +1068,7 @@ class DetrImageProcessor(BaseImageProcessor): """ return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -1077,12 +1077,12 @@ class DetrImageProcessor(BaseImageProcessor): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -1121,8 +1121,8 @@ class DetrImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1153,24 +1153,24 @@ class DetrImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -1191,7 +1191,7 @@ class DetrImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1239,24 +1239,24 @@ class DetrImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, do_convert_annotations: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -1266,15 +1266,15 @@ class DetrImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -1283,7 +1283,7 @@ class DetrImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1305,9 +1305,9 @@ class DetrImageProcessor(BaseImageProcessor): Whether to convert the annotations to the format expected by the model. Converts the bounding boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)` and in relative coordinates. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_pad (`bool`, *optional*, defaults to self.do_pad): Whether to pad the image. If `True`, padding will be applied to the bottom and right of @@ -1328,7 +1328,7 @@ class DetrImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1515,7 +1515,7 @@ class DetrImageProcessor(BaseImageProcessor): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1550,14 +1550,14 @@ class DetrImageProcessor(BaseImageProcessor): Args: outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. - target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`): + target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`): Torch Tensor (or list) corresponding to the requested final size (h, w) of each prediction. threshold (`float`, *optional*, defaults to 0.9): Threshold to use to filter out queries. mask_threshold (`float`, *optional*, defaults to 0.5): Threshold to use when turning the predicted masks into binary values. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, and masks for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, and masks for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1594,7 +1594,7 @@ class DetrImageProcessor(BaseImageProcessor): PyTorch. Args: - results (`List[Dict]`): + results (`list[Dict]`): Results list obtained by [`~DetrImageProcessor.post_process`], to which "masks" results will be added. outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. @@ -1607,7 +1607,7 @@ class DetrImageProcessor(BaseImageProcessor): threshold (`float`, *optional*, defaults to 0.5): Threshold to use when turning the predicted masks into binary values. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, boxes and masks for an + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, boxes and masks for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1641,10 +1641,10 @@ class DetrImageProcessor(BaseImageProcessor): Args: outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. - processed_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`): + processed_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`): Torch Tensor (or list) containing the size (h, w) of each image of the batch, i.e. the size after data augmentation but before batching. - target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`, *optional*): Torch Tensor (or list) corresponding to the requested final size `(height, width)` of each prediction. If left to None, it will default to the `processed_sizes`. is_thing_map (`torch.Tensor` of shape `(batch_size, 2)`, *optional*): @@ -1653,7 +1653,7 @@ class DetrImageProcessor(BaseImageProcessor): threshold (`float`, *optional*, defaults to 0.85): Threshold to use to filter out queries. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing a PNG string and segments_info values for + `list[Dict]`: A list of dictionaries, each dictionary containing a PNG string and segments_info values for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1773,7 +1773,7 @@ class DetrImageProcessor(BaseImageProcessor): # inspired by https://github.com/facebookresearch/detr/blob/master/models/detr.py#L258 def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None ): """ Converts the raw output of [`DetrForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -1784,11 +1784,11 @@ class DetrImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1807,7 +1807,7 @@ class DetrImageProcessor(BaseImageProcessor): # Convert from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: @@ -1825,18 +1825,18 @@ class DetrImageProcessor(BaseImageProcessor): return results - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None): """ Converts the output of [`DetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`DetrForSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - A list of tuples (`Tuple[int, int]`) containing the target size (height, width) of each image in the + target_sizes (`list[tuple[int, int]]`, *optional*): + A list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If unset, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1879,9 +1879,9 @@ class DetrImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`DetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch. @@ -1895,16 +1895,16 @@ class DetrImageProcessor(BaseImageProcessor): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. return_coco_annotation (`bool`, *optional*): Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. - **id** -- An integer representing the `segment_id`. @@ -1923,7 +1923,7 @@ class DetrImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( @@ -1963,9 +1963,9 @@ class DetrImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`DetrForSegmentation`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1984,11 +1984,11 @@ class DetrImageProcessor(BaseImageProcessor): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -2016,7 +2016,7 @@ class DetrImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/detr/image_processing_detr_fast.py b/src/transformers/models/detr/image_processing_detr_fast.py index 6840d367c63..55f9c6c13f6 100644 --- a/src/transformers/models/detr/image_processing_detr_fast.py +++ b/src/transformers/models/detr/image_processing_detr_fast.py @@ -17,7 +17,7 @@ import io import pathlib from collections import defaultdict -from typing import Any, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -86,7 +86,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int, device: to Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -230,11 +230,11 @@ def rgb_to_id(color): def prepare_coco_panoptic_annotation( image: torch.Tensor, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for DETR. """ @@ -290,7 +290,7 @@ class DetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -301,7 +301,7 @@ class DetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -347,7 +347,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `DetrImageProcessorFast.from_pretrained(checkpoint, size=600, @@ -363,12 +363,12 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DETR model. """ @@ -448,9 +448,9 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -458,11 +458,11 @@ class DetrImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -499,7 +499,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -516,12 +516,12 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -552,8 +552,8 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -583,20 +583,20 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[DetrFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -620,8 +620,8 @@ class DetrImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -631,10 +631,10 @@ class DetrImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -748,7 +748,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -784,14 +784,14 @@ class DetrImageProcessorFast(BaseImageProcessorFast): Args: outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. - target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`): + target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`): Torch Tensor (or list) corresponding to the requested final size (h, w) of each prediction. threshold (`float`, *optional*, defaults to 0.9): Threshold to use to filter out queries. mask_threshold (`float`, *optional*, defaults to 0.5): Threshold to use when turning the predicted masks into binary values. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, and masks for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, and masks for an image in the batch as predicted by the model. """ logger.warning_once( @@ -828,7 +828,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): PyTorch. Args: - results (`List[Dict]`): + results (`list[Dict]`): Results list obtained by [`~DetrImageProcessor.post_process`], to which "masks" results will be added. outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. @@ -841,7 +841,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): threshold (`float`, *optional*, defaults to 0.5): Threshold to use when turning the predicted masks into binary values. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, boxes and masks for an + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels, boxes and masks for an image in the batch as predicted by the model. """ logger.warning_once( @@ -875,10 +875,10 @@ class DetrImageProcessorFast(BaseImageProcessorFast): Args: outputs ([`DetrSegmentationOutput`]): Raw outputs of the model. - processed_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`): + processed_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`): Torch Tensor (or list) containing the size (h, w) of each image of the batch, i.e. the size after data augmentation but before batching. - target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`torch.Tensor` of shape `(batch_size, 2)` or `list[Tuple]` of length `batch_size`, *optional*): Torch Tensor (or list) corresponding to the requested final size `(height, width)` of each prediction. If left to None, it will default to the `processed_sizes`. is_thing_map (`torch.Tensor` of shape `(batch_size, 2)`, *optional*): @@ -887,7 +887,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): threshold (`float`, *optional*, defaults to 0.85): Threshold to use to filter out queries. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing a PNG string and segments_info values for + `list[Dict]`: A list of dictionaries, each dictionary containing a PNG string and segments_info values for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1007,7 +1007,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.post_process_object_detection def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None ): """ Converts the raw output of [`DetrForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -1018,11 +1018,11 @@ class DetrImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1041,7 +1041,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): # Convert from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: @@ -1060,18 +1060,18 @@ class DetrImageProcessorFast(BaseImageProcessorFast): return results # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.post_process_semantic_segmentation - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None): """ Converts the output of [`DetrForSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`DetrForSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - A list of tuples (`Tuple[int, int]`) containing the target size (height, width) of each image in the + target_sizes (`list[tuple[int, int]]`, *optional*): + A list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If unset, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1114,9 +1114,9 @@ class DetrImageProcessorFast(BaseImageProcessorFast): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`DetrForSegmentation`] into instance segmentation predictions. Only supports PyTorch. @@ -1130,16 +1130,16 @@ class DetrImageProcessorFast(BaseImageProcessorFast): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. return_coco_annotation (`bool`, *optional*): Defaults to `False`. If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id` or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. - **id** -- An integer representing the `segment_id`. @@ -1158,7 +1158,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( @@ -1198,9 +1198,9 @@ class DetrImageProcessorFast(BaseImageProcessorFast): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`DetrForSegmentation`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1219,11 +1219,11 @@ class DetrImageProcessorFast(BaseImageProcessorFast): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id` or `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1251,7 +1251,7 @@ class DetrImageProcessorFast(BaseImageProcessorFast): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/detr/modeling_detr.py b/src/transformers/models/detr/modeling_detr.py index dd6264d1832..9f8ea167ab7 100644 --- a/src/transformers/models/detr/modeling_detr.py +++ b/src/transformers/models/detr/modeling_detr.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -163,17 +163,17 @@ class DetrObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -232,18 +232,18 @@ class DetrSegmentationOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None pred_masks: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None # BELOW: utilities copied from @@ -522,7 +522,7 @@ class DetrAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, spatial_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder @@ -1155,7 +1155,7 @@ class DetrModel(DetrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], DetrModelOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1331,11 +1331,11 @@ class DetrForObjectDetection(DetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DetrObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], DetrObjectDetectionOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1345,7 +1345,7 @@ class DetrForObjectDetection(DetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -1477,11 +1477,11 @@ class DetrForSegmentation(DetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], DetrSegmentationOutput]: + ) -> Union[tuple[torch.FloatTensor], DetrSegmentationOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1491,7 +1491,7 @@ class DetrForSegmentation(DetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss, DICE/F-1 loss and Focal loss. List of dicts, each dictionary containing at least the following 3 keys: 'class_labels', 'boxes' and 'masks' (the class labels, bounding boxes and segmentation masks of an image in the batch respectively). The class labels themselves @@ -1692,7 +1692,7 @@ class DetrMaskHeadSmallConv(nn.Module): nn.init.kaiming_uniform_(m.weight, a=1) nn.init.constant_(m.bias, 0) - def forward(self, x: Tensor, bbox_mask: Tensor, fpns: List[Tensor]): + def forward(self, x: Tensor, bbox_mask: Tensor, fpns: list[Tensor]): # here we concatenate x, the projected feature map, of shape (batch_size, d_model, height/32, width/32) with # the bbox_mask = the attention maps of shape (batch_size, n_queries, n_heads, height/32, width/32). # We expand the projected feature map to match the number of heads. diff --git a/src/transformers/models/diffllama/configuration_diffllama.py b/src/transformers/models/diffllama/configuration_diffllama.py index d0b32c97242..21060727192 100644 --- a/src/transformers/models/diffllama/configuration_diffllama.py +++ b/src/transformers/models/diffllama/configuration_diffllama.py @@ -97,11 +97,11 @@ class DiffLlamaConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/diffllama/modeling_diffllama.py b/src/transformers/models/diffllama/modeling_diffllama.py index 68aa54180ca..43e8e4584bd 100644 --- a/src/transformers/models/diffllama/modeling_diffllama.py +++ b/src/transformers/models/diffllama/modeling_diffllama.py @@ -22,7 +22,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -161,7 +161,7 @@ class DiffLlamaAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -169,7 +169,7 @@ class DiffLlamaAttention(nn.Module): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, target_len, _ = hidden_states.size() q_len = target_len @@ -245,14 +245,14 @@ class DiffLlamaFlashAttention2(DiffLlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -388,7 +388,7 @@ class DiffLlamaSdpaAttention(DiffLlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -396,7 +396,7 @@ class DiffLlamaSdpaAttention(DiffLlamaAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -511,9 +511,9 @@ class DiffLlamaDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/diffllama/modular_diffllama.py b/src/transformers/models/diffllama/modular_diffllama.py index b772a9f04d5..934baf9c0b1 100644 --- a/src/transformers/models/diffllama/modular_diffllama.py +++ b/src/transformers/models/diffllama/modular_diffllama.py @@ -16,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import math -from typing import Optional, Tuple +from typing import Optional import torch import torch.utils.checkpoint @@ -94,7 +94,7 @@ class DiffLlamaAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -102,7 +102,7 @@ class DiffLlamaAttention(nn.Module): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, target_len, _ = hidden_states.size() q_len = target_len @@ -178,14 +178,14 @@ class DiffLlamaFlashAttention2(DiffLlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -321,7 +321,7 @@ class DiffLlamaSdpaAttention(DiffLlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -329,7 +329,7 @@ class DiffLlamaSdpaAttention(DiffLlamaAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) diff --git a/src/transformers/models/dinat/configuration_dinat.py b/src/transformers/models/dinat/configuration_dinat.py index 7b432e37c85..f7d7fa509c5 100644 --- a/src/transformers/models/dinat/configuration_dinat.py +++ b/src/transformers/models/dinat/configuration_dinat.py @@ -39,13 +39,13 @@ class DinatConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embed_dim (`int`, *optional*, defaults to 64): Dimensionality of patch embedding. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 5]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 5]`): Number of layers in each level of the encoder. - num_heads (`List[int]`, *optional*, defaults to `[2, 4, 8, 16]`): + num_heads (`list[int]`, *optional*, defaults to `[2, 4, 8, 16]`): Number of attention heads in each layer of the Transformer encoder. kernel_size (`int`, *optional*, defaults to 7): Neighborhood Attention kernel size. - dilations (`List[List[int]]`, *optional*, defaults to `[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]`): + dilations (`list[list[int]]`, *optional*, defaults to `[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]`): Dilation value of each NA layer in the Transformer encoder. mlp_ratio (`float`, *optional*, defaults to 3.0): Ratio of MLP hidden dimensionality to embedding dimensionality. @@ -66,12 +66,12 @@ class DinatConfig(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. layer_scale_init_value (`float`, *optional*, defaults to 0.0): The initial value for the layer scale. Disabled if <=0. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/dinat/modeling_dinat.py b/src/transformers/models/dinat/modeling_dinat.py index 79e46bd112a..25a1a3ae5c6 100644 --- a/src/transformers/models/dinat/modeling_dinat.py +++ b/src/transformers/models/dinat/modeling_dinat.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -84,9 +84,9 @@ class DinatEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -120,9 +120,9 @@ class DinatModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -156,9 +156,9 @@ class DinatImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None class DinatEmbeddings(nn.Module): @@ -174,7 +174,7 @@ class DinatEmbeddings(nn.Module): self.norm = nn.LayerNorm(config.embed_dim) self.dropout = nn.Dropout(config.hidden_dropout_prob) - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor]: embeddings = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) @@ -310,7 +310,7 @@ class NeighborhoodAttention(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(hidden_states)) key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) @@ -382,7 +382,7 @@ class NeighborhoodAttentionModule(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -452,7 +452,7 @@ class DinatLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: batch_size, height, width, channels = hidden_states.size() shortcut = hidden_states @@ -517,7 +517,7 @@ class DinatStage(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: _, height, width, _ = hidden_states.size() for i, layer_module in enumerate(self.layers): layer_outputs = layer_module(hidden_states, output_attentions) @@ -562,7 +562,7 @@ class DinatEncoder(nn.Module): output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, DinatEncoderOutput]: + ) -> Union[tuple, DinatEncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -665,7 +665,7 @@ class DinatModel(DinatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, DinatModelOutput]: + ) -> Union[tuple, DinatModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -737,7 +737,7 @@ class DinatForImageClassification(DinatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, DinatImageClassifierOutput]: + ) -> Union[tuple, DinatImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/dinov2/configuration_dinov2.py b/src/transformers/models/dinov2/configuration_dinov2.py index c629cde12c4..55fa0539a23 100644 --- a/src/transformers/models/dinov2/configuration_dinov2.py +++ b/src/transformers/models/dinov2/configuration_dinov2.py @@ -72,12 +72,12 @@ class Dinov2Config(BackboneConfigMixin, PretrainedConfig): Stochastic depth rate per sample (when applied in the main path of residual layers). use_swiglu_ffn (`bool`, *optional*, defaults to `False`): Whether to use the SwiGLU feedforward neural network. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/dinov2/modeling_dinov2.py b/src/transformers/models/dinov2/modeling_dinov2.py index 69911e40bc3..8dfd7fc4097 100644 --- a/src/transformers/models/dinov2/modeling_dinov2.py +++ b/src/transformers/models/dinov2/modeling_dinov2.py @@ -15,7 +15,7 @@ """PyTorch DINOv2 model.""" import collections.abc -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -208,7 +208,7 @@ class Dinov2SelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -269,7 +269,7 @@ class Dinov2Attention(nn.Module): self.output = Dinov2SelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -292,7 +292,7 @@ class Dinov2Attention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -406,7 +406,7 @@ class Dinov2Layer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.norm1(hidden_states), # in Dinov2, layernorm is applied before self-attention head_mask, @@ -546,7 +546,7 @@ class Dinov2Model(Dinov2PreTrainedModel): def get_input_embeddings(self) -> Dinov2PatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -563,7 +563,7 @@ class Dinov2Model(Dinov2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, sequence_length)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). Only relevant for diff --git a/src/transformers/models/dinov2/modeling_flax_dinov2.py b/src/transformers/models/dinov2/modeling_flax_dinov2.py index 48afecde5e1..b9ea2eaa3eb 100644 --- a/src/transformers/models/dinov2/modeling_flax_dinov2.py +++ b/src/transformers/models/dinov2/modeling_flax_dinov2.py @@ -16,7 +16,7 @@ import collections.abc import math -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -568,7 +568,7 @@ class FlaxDinov2PreTrainedModel(FlaxPreTrainedModel): input_shape = (1, config.image_size, config.image_size, config.num_channels) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors pixel_values = jnp.zeros(input_shape, dtype=self.dtype) diff --git a/src/transformers/models/dinov2_with_registers/configuration_dinov2_with_registers.py b/src/transformers/models/dinov2_with_registers/configuration_dinov2_with_registers.py index a0295899fcd..ec4f446fc68 100644 --- a/src/transformers/models/dinov2_with_registers/configuration_dinov2_with_registers.py +++ b/src/transformers/models/dinov2_with_registers/configuration_dinov2_with_registers.py @@ -70,12 +70,12 @@ class Dinov2WithRegistersConfig(BackboneConfigMixin, PretrainedConfig): Whether to use the SwiGLU feedforward neural network. num_register_tokens (`int`, *optional*, defaults to 4): Number of register tokens to use. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/dinov2_with_registers/modeling_dinov2_with_registers.py b/src/transformers/models/dinov2_with_registers/modeling_dinov2_with_registers.py index db2d2b01d13..ba3ecf08748 100644 --- a/src/transformers/models/dinov2_with_registers/modeling_dinov2_with_registers.py +++ b/src/transformers/models/dinov2_with_registers/modeling_dinov2_with_registers.py @@ -21,7 +21,7 @@ # limitations under the License. import collections.abc -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -229,7 +229,7 @@ class Dinov2WithRegistersSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -288,7 +288,7 @@ class Dinov2WithRegistersAttention(nn.Module): self.output = Dinov2WithRegistersSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -311,7 +311,7 @@ class Dinov2WithRegistersAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -425,7 +425,7 @@ class Dinov2WithRegistersLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.norm1(hidden_states), # in Dinov2WithRegisters, layernorm is applied before self-attention head_mask, @@ -564,7 +564,7 @@ class Dinov2WithRegistersModel(Dinov2WithRegistersPreTrainedModel): def get_input_embeddings(self) -> Dinov2WithRegistersPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -581,7 +581,7 @@ class Dinov2WithRegistersModel(Dinov2WithRegistersPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, sequence_length)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). Only relevant for diff --git a/src/transformers/models/dinov2_with_registers/modular_dinov2_with_registers.py b/src/transformers/models/dinov2_with_registers/modular_dinov2_with_registers.py index 226653cd409..5145221e036 100644 --- a/src/transformers/models/dinov2_with_registers/modular_dinov2_with_registers.py +++ b/src/transformers/models/dinov2_with_registers/modular_dinov2_with_registers.py @@ -84,12 +84,12 @@ class Dinov2WithRegistersConfig(BackboneConfigMixin, PretrainedConfig): Whether to use the SwiGLU feedforward neural network. num_register_tokens (`int`, *optional*, defaults to 4): Number of register tokens to use. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/distilbert/modeling_distilbert.py b/src/transformers/models/distilbert/modeling_distilbert.py index 1a241884145..1a84544bee3 100755 --- a/src/transformers/models/distilbert/modeling_distilbert.py +++ b/src/transformers/models/distilbert/modeling_distilbert.py @@ -19,7 +19,7 @@ part from HuggingFace PyTorch version of Google AI Bert model (https://github.co """ import math -from typing import Dict, List, Optional, Set, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -148,10 +148,10 @@ class MultiHeadSelfAttention(nn.Module): self.v_lin = nn.Linear(in_features=config.dim, out_features=config.dim) self.out_lin = nn.Linear(in_features=config.dim, out_features=config.dim) - self.pruned_heads: Set[int] = set() + self.pruned_heads: set[int] = set() self.attention_head_size = self.dim // self.n_heads - def prune_heads(self, heads: List[int]): + def prune_heads(self, heads: list[int]): if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -175,7 +175,7 @@ class MultiHeadSelfAttention(nn.Module): mask: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: """ Parameters: query: torch.tensor(bs, seq_length, dim) @@ -255,7 +255,7 @@ class DistilBertFlashAttention2(MultiHeadSelfAttention): mask: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: """ Parameters: query: torch.tensor(bs, seq_length, dim) @@ -342,7 +342,7 @@ class DistilBertSdpaAttention(MultiHeadSelfAttention): mask: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: """ Parameters: query: torch.tensor(bs, seq_length, dim) @@ -456,7 +456,7 @@ class TransformerBlock(nn.Module): attn_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: """ Parameters: x: torch.tensor(bs, seq_length, dim) @@ -509,7 +509,7 @@ class Transformer(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutput, Tuple[torch.Tensor, ...]]: # docstyle-ignore + ) -> Union[BaseModelOutput, tuple[torch.Tensor, ...]]: # docstyle-ignore """ Parameters: x: torch.tensor(bs, seq_length, dim) Input sequence embedded. @@ -517,10 +517,10 @@ class Transformer(nn.Module): Returns: hidden_state: torch.tensor(bs, seq_length, dim) Sequence of hidden states in the last (top) - layer all_hidden_states: Tuple[torch.tensor(bs, seq_length, dim)] + layer all_hidden_states: tuple[torch.tensor(bs, seq_length, dim)] Tuple of length n_layers with the hidden states from each layer. Optional: only if output_hidden_states=True - all_attentions: Tuple[torch.tensor(bs, n_heads, seq_length, seq_length)] + all_attentions: tuple[torch.tensor(bs, n_heads, seq_length, seq_length)] Tuple of length n_layers with the attention weights from each layer Optional: only if output_attentions=True """ @@ -669,7 +669,7 @@ class DistilBertModel(DistilBertPreTrainedModel): def set_input_embeddings(self, new_embeddings: nn.Embedding): self.embeddings.word_embeddings = new_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[List[int]]]): + def _prune_heads(self, heads_to_prune: dict[int, list[list[int]]]): """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -687,7 +687,7 @@ class DistilBertModel(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[BaseModelOutput, tuple[torch.Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices)`): Indices of input sequence tokens in the vocabulary. @@ -806,7 +806,7 @@ class DistilBertForMaskedLM(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[MaskedLMOutput, tuple[torch.Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices)`): Indices of input sequence tokens in the vocabulary. @@ -908,7 +908,7 @@ class DistilBertForSequenceClassification(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.Tensor, ...]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1015,7 +1015,7 @@ class DistilBertForQuestionAnswering(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[QuestionAnsweringModelOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[QuestionAnsweringModelOutput, tuple[torch.Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices)`): Indices of input sequence tokens in the vocabulary. @@ -1122,7 +1122,7 @@ class DistilBertForTokenClassification(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[TokenClassifierOutput, tuple[torch.Tensor, ...]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1205,7 +1205,7 @@ class DistilBertForMultipleChoice(DistilBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MultipleChoiceModelOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[MultipleChoiceModelOutput, tuple[torch.Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/distilbert/modeling_flax_distilbert.py b/src/transformers/models/distilbert/modeling_flax_distilbert.py index e9c12c4b088..fba3dfd9d33 100644 --- a/src/transformers/models/distilbert/modeling_flax_distilbert.py +++ b/src/transformers/models/distilbert/modeling_flax_distilbert.py @@ -14,7 +14,7 @@ # limitations under the License. import math -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -424,7 +424,7 @@ class FlaxDistilBertPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: DistilBertConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -433,7 +433,7 @@ class FlaxDistilBertPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/distilbert/modeling_tf_distilbert.py b/src/transformers/models/distilbert/modeling_tf_distilbert.py index d0ee2f84835..8a72ebf4436 100644 --- a/src/transformers/models/distilbert/modeling_tf_distilbert.py +++ b/src/transformers/models/distilbert/modeling_tf_distilbert.py @@ -19,7 +19,7 @@ TF 2.0 DistilBERT model from __future__ import annotations import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -344,10 +344,10 @@ class TFTransformer(keras.layers.Layer): Returns: hidden_state: tf.Tensor(bs, seq_length, dim) Sequence of hidden states in the last (top) layer - all_hidden_states: Tuple[tf.Tensor(bs, seq_length, dim)] + all_hidden_states: tuple[tf.Tensor(bs, seq_length, dim)] Tuple of length n_layers with the hidden states from each layer. Optional: only if output_hidden_states=True - all_attentions: Tuple[tf.Tensor(bs, n_heads, seq_length, seq_length)] + all_attentions: tuple[tf.Tensor(bs, n_heads, seq_length, seq_length)] Tuple of length n_layers with the attention weights from each layer Optional: only if output_attentions=True """ @@ -598,7 +598,7 @@ class TFDistilBertModel(TFDistilBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.distilbert( input_ids=input_ids, attention_mask=attention_mask, @@ -702,7 +702,7 @@ class TFDistilBertForMaskedLM(TFDistilBertPreTrainedModel, TFMaskedLanguageModel return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -799,7 +799,7 @@ class TFDistilBertForSequenceClassification(TFDistilBertPreTrainedModel, TFSeque return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -887,7 +887,7 @@ class TFDistilBertForTokenClassification(TFDistilBertPreTrainedModel, TFTokenCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -974,7 +974,7 @@ class TFDistilBertForMultipleChoice(TFDistilBertPreTrainedModel, TFMultipleChoic return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1077,7 +1077,7 @@ class TFDistilBertForQuestionAnswering(TFDistilBertPreTrainedModel, TFQuestionAn start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/distilbert/tokenization_distilbert.py b/src/transformers/models/distilbert/tokenization_distilbert.py index 9cc632f6856..d2a629052f0 100644 --- a/src/transformers/models/distilbert/tokenization_distilbert.py +++ b/src/transformers/models/distilbert/tokenization_distilbert.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -194,8 +194,8 @@ class DistilBertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -220,8 +220,8 @@ class DistilBertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -248,7 +248,7 @@ class DistilBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/distilbert/tokenization_distilbert_fast.py b/src/transformers/models/distilbert/tokenization_distilbert_fast.py index 4c19e0f534e..c174804dc53 100644 --- a/src/transformers/models/distilbert/tokenization_distilbert_fast.py +++ b/src/transformers/models/distilbert/tokenization_distilbert_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for DistilBERT.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -141,7 +141,7 @@ class DistilBertTokenizerFast(PreTrainedTokenizerFast): return output # Copied from transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/donut/image_processing_donut.py b/src/transformers/models/donut/image_processing_donut.py index c45e11430ff..e8a48dcabef 100644 --- a/src/transformers/models/donut/image_processing_donut.py +++ b/src/transformers/models/donut/image_processing_donut.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Donut.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -60,7 +60,7 @@ class DonutImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -82,10 +82,10 @@ class DonutImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Image standard deviation. """ @@ -94,7 +94,7 @@ class DonutImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_thumbnail: bool = True, do_align_long_axis: bool = False, @@ -102,8 +102,8 @@ class DonutImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -129,7 +129,7 @@ class DonutImageProcessor(BaseImageProcessor): def align_long_axis( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -139,7 +139,7 @@ class DonutImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be aligned. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to align the long axis to. data_format (`str` or `ChannelDimension`, *optional*): The data format of the output image. If unset, the same format as the input image is used. @@ -176,7 +176,7 @@ class DonutImageProcessor(BaseImageProcessor): def pad_image( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], random_padding: bool = False, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -187,7 +187,7 @@ class DonutImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be padded. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to pad the image to. random_padding (`bool`, *optional*, defaults to `False`): Whether to use random padding or not. @@ -222,7 +222,7 @@ class DonutImageProcessor(BaseImageProcessor): def thumbnail( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -235,7 +235,7 @@ class DonutImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be resized. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to resize the image to. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): The resampling filter to use. @@ -272,7 +272,7 @@ class DonutImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -284,7 +284,7 @@ class DonutImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -313,7 +313,7 @@ class DonutImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_thumbnail: Optional[bool] = None, do_align_long_axis: Optional[bool] = None, @@ -322,8 +322,8 @@ class DonutImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -337,7 +337,7 @@ class DonutImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to min(size["height"], size["width"]) with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -360,9 +360,9 @@ class DonutImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/donut/image_processing_donut_fast.py b/src/transformers/models/donut/image_processing_donut_fast.py index 56bb290cccc..862828af8cb 100644 --- a/src/transformers/models/donut/image_processing_donut_fast.py +++ b/src/transformers/models/donut/image_processing_donut_fast.py @@ -111,7 +111,7 @@ class DonutImageProcessorFast(BaseImageProcessorFast): Args: image (`torch.Tensor`): The image to be aligned. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to align the long axis to. Returns: @@ -140,7 +140,7 @@ class DonutImageProcessorFast(BaseImageProcessorFast): Args: image (`torch.Tensor`): The image to be padded. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to pad the image to. random_padding (`bool`, *optional*, defaults to `False`): Whether to use random padding or not. @@ -184,7 +184,7 @@ class DonutImageProcessorFast(BaseImageProcessorFast): Args: image (`torch.Tensor`): The image to be resized. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to resize the image to. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): The resampling filter to use. diff --git a/src/transformers/models/donut/modeling_donut_swin.py b/src/transformers/models/donut/modeling_donut_swin.py index c1fd629466d..222b0986423 100644 --- a/src/transformers/models/donut/modeling_donut_swin.py +++ b/src/transformers/models/donut/modeling_donut_swin.py @@ -20,7 +20,7 @@ states.""" import collections.abc import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -65,9 +65,9 @@ class DonutSwinEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -102,9 +102,9 @@ class DonutSwinModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -139,9 +139,9 @@ class DonutSwinImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None # Copied from transformers.models.swin.modeling_swin.window_partition @@ -238,7 +238,7 @@ class DonutSwinEmbeddings(nn.Module): pixel_values: Optional[torch.FloatTensor], bool_masked_pos: Optional[torch.BoolTensor] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: _, num_channels, height, width = pixel_values.shape embeddings, output_dimensions = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) @@ -293,7 +293,7 @@ class DonutSwinPatchEmbeddings(nn.Module): pixel_values = nn.functional.pad(pixel_values, pad_values) return pixel_values - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: _, num_channels, height, width = pixel_values.shape # pad the input to be divisible by self.patch_size, if needed pixel_values = self.maybe_pad(pixel_values, height, width) @@ -311,7 +311,7 @@ class DonutSwinPatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -319,7 +319,7 @@ class DonutSwinPatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -334,7 +334,7 @@ class DonutSwinPatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -446,7 +446,7 @@ class DonutSwinSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -543,7 +543,7 @@ class DonutSwinAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -640,11 +640,11 @@ class DonutSwinLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: if not always_partition: self.set_shift_and_window_size(input_dimensions) else: @@ -736,11 +736,11 @@ class DonutSwinStage(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: height, width = input_dimensions for i, layer_module in enumerate(self.blocks): layer_head_mask = head_mask[i] if head_mask is not None else None @@ -793,14 +793,14 @@ class DonutSwinEncoder(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, always_partition: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, DonutSwinEncoderOutput]: + ) -> Union[tuple, DonutSwinEncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -940,7 +940,7 @@ class DonutSwinModel(DonutSwinPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, DonutSwinModelOutput]: + ) -> Union[tuple, DonutSwinModelOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1035,7 +1035,7 @@ class DonutSwinForImageClassification(DonutSwinPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, DonutSwinImageClassifierOutput]: + ) -> Union[tuple, DonutSwinImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/donut/processing_donut.py b/src/transformers/models/donut/processing_donut.py index d6438df1be6..edadc7b1267 100644 --- a/src/transformers/models/donut/processing_donut.py +++ b/src/transformers/models/donut/processing_donut.py @@ -19,7 +19,7 @@ Processor class for Donut. import re import warnings from contextlib import contextmanager -from typing import List, Optional, Union +from typing import Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack @@ -77,7 +77,7 @@ class DonutProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[DonutProcessorKwargs], diff --git a/src/transformers/models/dpr/modeling_dpr.py b/src/transformers/models/dpr/modeling_dpr.py index c707d4fcfe7..0dfe85f4e06 100644 --- a/src/transformers/models/dpr/modeling_dpr.py +++ b/src/transformers/models/dpr/modeling_dpr.py @@ -15,7 +15,7 @@ """PyTorch DPR model for Open Domain Question Answering.""" from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -63,8 +63,8 @@ class DPRContextEncoderOutput(ModelOutput): """ pooler_output: torch.FloatTensor - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -91,8 +91,8 @@ class DPRQuestionEncoderOutput(ModelOutput): """ pooler_output: torch.FloatTensor - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -124,8 +124,8 @@ class DPRReaderOutput(ModelOutput): start_logits: torch.FloatTensor end_logits: Optional[torch.FloatTensor] = None relevance_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring @@ -172,7 +172,7 @@ class DPREncoder(DPRPreTrainedModel): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = False, - ) -> Union[BaseModelOutputWithPooling, Tuple[Tensor, ...]]: + ) -> Union[BaseModelOutputWithPooling, tuple[Tensor, ...]]: outputs = self.bert_model( input_ids=input_ids, attention_mask=attention_mask, @@ -224,7 +224,7 @@ class DPRSpanPredictor(DPRPreTrainedModel): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = False, - ) -> Union[DPRReaderOutput, Tuple[Tensor, ...]]: + ) -> Union[DPRReaderOutput, tuple[Tensor, ...]]: # notations: N - number of questions in a batch, M - number of passages per questions, L - sequence length n_passages, sequence_length = input_ids.size() if input_ids is not None else inputs_embeds.size()[:2] # feed encoder @@ -328,7 +328,7 @@ class DPRContextEncoder(DPRPretrainedContextEncoder): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[DPRContextEncoderOutput, Tuple[Tensor, ...]]: + ) -> Union[DPRContextEncoderOutput, tuple[Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. To match pretraining, DPR input sequence should be @@ -433,7 +433,7 @@ class DPRQuestionEncoder(DPRPretrainedQuestionEncoder): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[DPRQuestionEncoderOutput, Tuple[Tensor, ...]]: + ) -> Union[DPRQuestionEncoderOutput, tuple[Tensor, ...]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. To match pretraining, DPR input sequence should be @@ -538,9 +538,9 @@ class DPRReader(DPRPretrainedReader): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[DPRReaderOutput, Tuple[Tensor, ...]]: + ) -> Union[DPRReaderOutput, tuple[Tensor, ...]]: r""" - input_ids (`Tuple[torch.LongTensor]` of shapes `(n_passages, sequence_length)`): + input_ids (`tuple[torch.LongTensor]` of shapes `(n_passages, sequence_length)`): Indices of input sequence tokens in the vocabulary. It has to be a sequence triplet with 1) the question and 2) the passages titles and 3) the passages texts To match pretraining, DPR `input_ids` sequence should be formatted with [CLS] and [SEP] with the format: diff --git a/src/transformers/models/dpr/modeling_tf_dpr.py b/src/transformers/models/dpr/modeling_tf_dpr.py index 303b03ec244..1cb4e0ebad0 100644 --- a/src/transformers/models/dpr/modeling_tf_dpr.py +++ b/src/transformers/models/dpr/modeling_tf_dpr.py @@ -18,7 +18,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -69,8 +69,8 @@ class TFDPRContextEncoderOutput(ModelOutput): """ pooler_output: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -97,8 +97,8 @@ class TFDPRQuestionEncoderOutput(ModelOutput): """ pooler_output: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -130,8 +130,8 @@ class TFDPRReaderOutput(ModelOutput): start_logits: Optional[tf.Tensor] = None end_logits: Optional[tf.Tensor] = None relevance_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None class TFDPREncoderLayer(keras.layers.Layer): @@ -163,7 +163,7 @@ class TFDPREncoderLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor, ...]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor, ...]]: outputs = self.bert_model( input_ids=input_ids, attention_mask=attention_mask, @@ -233,7 +233,7 @@ class TFDPRSpanPredictorLayer(keras.layers.Layer): output_hidden_states: bool = False, return_dict: bool = False, training: bool = False, - ) -> Union[TFDPRReaderOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFDPRReaderOutput, tuple[tf.Tensor, ...]]: # notations: N - number of questions in a batch, M - number of passages per questions, L - sequence length n_passages, sequence_length = shape_list(input_ids) if input_ids is not None else shape_list(inputs_embeds)[:2] # feed encoder @@ -304,7 +304,7 @@ class TFDPRSpanPredictor(TFPreTrainedModel): output_hidden_states: bool = False, return_dict: bool = False, training: bool = False, - ) -> Union[TFDPRReaderOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFDPRReaderOutput, tuple[tf.Tensor, ...]]: outputs = self.encoder( input_ids=input_ids, attention_mask=attention_mask, @@ -337,7 +337,7 @@ class TFDPREncoder(TFPreTrainedModel): output_hidden_states: bool = False, return_dict: bool = False, training: bool = False, - ) -> Union[TFDPRReaderOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFDPRReaderOutput, tuple[tf.Tensor, ...]]: outputs = self.encoder( input_ids=input_ids, attention_mask=attention_mask, @@ -560,7 +560,7 @@ class TFDPRContextEncoder(TFDPRPretrainedContextEncoder): output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> TFDPRContextEncoderOutput | Tuple[tf.Tensor, ...]: + ) -> TFDPRContextEncoderOutput | tuple[tf.Tensor, ...]: r""" Return: @@ -649,7 +649,7 @@ class TFDPRQuestionEncoder(TFDPRPretrainedQuestionEncoder): output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> TFDPRQuestionEncoderOutput | Tuple[tf.Tensor, ...]: + ) -> TFDPRQuestionEncoderOutput | tuple[tf.Tensor, ...]: r""" Return: @@ -736,7 +736,7 @@ class TFDPRReader(TFDPRPretrainedReader): output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> TFDPRReaderOutput | Tuple[tf.Tensor, ...]: + ) -> TFDPRReaderOutput | tuple[tf.Tensor, ...]: r""" Return: diff --git a/src/transformers/models/dpr/tokenization_dpr.py b/src/transformers/models/dpr/tokenization_dpr.py index 00b8dedfa7e..020b235cb6b 100644 --- a/src/transformers/models/dpr/tokenization_dpr.py +++ b/src/transformers/models/dpr/tokenization_dpr.py @@ -15,7 +15,7 @@ """Tokenization classes for DPR.""" import collections -from typing import List, Optional, Union +from typing import Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging @@ -71,13 +71,13 @@ CUSTOM_DPR_READER_DOCSTRING = r""" ``` Args: - questions (`str` or `List[str]`): + questions (`str` or `list[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. - titles (`str` or `List[str]`): + titles (`str` or `list[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. - texts (`str` or `List[str]`): + texts (`str` or `list[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: @@ -122,7 +122,7 @@ CUSTOM_DPR_READER_DOCSTRING = r""" [What are attention masks?](../glossary#attention-mask) Returns: - `Dict[str, List[List[int]]]`: A dictionary with the following keys: + `dict[str, list[list[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. @@ -197,7 +197,7 @@ class CustomDPRReaderTokenizerMixin: num_spans: int = 16, max_answer_length: int = 64, num_spans_per_passage: int = 4, - ) -> List[DPRSpanPrediction]: + ) -> list[DPRSpanPrediction]: """ Get the span predictions for the extractive Q&A model. @@ -233,7 +233,7 @@ class CustomDPRReaderTokenizerMixin: start_logits, end_logits, relevance_logits = reader_output[:3] n_passages = len(relevance_logits) sorted_docs = sorted(range(n_passages), reverse=True, key=relevance_logits.__getitem__) - nbest_spans_predictions: List[DPRReaderOutput] = [] + nbest_spans_predictions: list[DPRReaderOutput] = [] for doc_id in sorted_docs: sequence_ids = list(input_ids[doc_id]) # assuming question & title information is at the beginning of the sequence @@ -268,11 +268,11 @@ class CustomDPRReaderTokenizerMixin: def _get_best_spans( self, - start_logits: List[int], - end_logits: List[int], + start_logits: list[int], + end_logits: list[int], max_answer_length: int, top_spans: int, - ) -> List[DPRSpanPrediction]: + ) -> list[DPRSpanPrediction]: """ Finds the best answer span for the extractive Q&A model for one passage. It returns the best span by descending `span_score` order and keeping max `top_spans` spans. Spans longer that `max_answer_length` are ignored. diff --git a/src/transformers/models/dpr/tokenization_dpr_fast.py b/src/transformers/models/dpr/tokenization_dpr_fast.py index f4e7c0fdcdb..dbf74529174 100644 --- a/src/transformers/models/dpr/tokenization_dpr_fast.py +++ b/src/transformers/models/dpr/tokenization_dpr_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for DPR.""" import collections -from typing import List, Optional, Union +from typing import Optional, Union from ...tokenization_utils_base import BatchEncoding from ...utils import TensorType, add_end_docstrings, add_start_docstrings, logging @@ -72,13 +72,13 @@ CUSTOM_DPR_READER_DOCSTRING = r""" [CLS] [SEP] [SEP] Args: - questions (`str` or `List[str]`): + questions (`str` or `list[str]`): The questions to be encoded. You can specify one question for many passages. In this case, the question will be duplicated like `[questions] * n_passages`. Otherwise you have to specify as many questions as in `titles` or `texts`. - titles (`str` or `List[str]`): + titles (`str` or `list[str]`): The passages titles to be encoded. This can be a string or a list of strings if there are several passages. - texts (`str` or `List[str]`): + texts (`str` or `list[str]`): The passages texts to be encoded. This can be a string or a list of strings if there are several passages. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): Activates and controls padding. Accepts the following values: @@ -123,7 +123,7 @@ CUSTOM_DPR_READER_DOCSTRING = r""" [What are attention masks?](../glossary#attention-mask) Return: - `Dict[str, List[List[int]]]`: A dictionary with the following keys: + `dict[str, list[list[int]]]`: A dictionary with the following keys: - `input_ids`: List of token ids to be fed to a model. - `attention_mask`: List of indices specifying which tokens should be attended to by the model. @@ -197,7 +197,7 @@ class CustomDPRReaderTokenizerMixin: num_spans: int = 16, max_answer_length: int = 64, num_spans_per_passage: int = 4, - ) -> List[DPRSpanPrediction]: + ) -> list[DPRSpanPrediction]: """ Get the span predictions for the extractive Q&A model. @@ -233,7 +233,7 @@ class CustomDPRReaderTokenizerMixin: start_logits, end_logits, relevance_logits = reader_output[:3] n_passages = len(relevance_logits) sorted_docs = sorted(range(n_passages), reverse=True, key=relevance_logits.__getitem__) - nbest_spans_predictions: List[DPRReaderOutput] = [] + nbest_spans_predictions: list[DPRReaderOutput] = [] for doc_id in sorted_docs: sequence_ids = list(input_ids[doc_id]) # assuming question & title information is at the beginning of the sequence @@ -268,11 +268,11 @@ class CustomDPRReaderTokenizerMixin: def _get_best_spans( self, - start_logits: List[int], - end_logits: List[int], + start_logits: list[int], + end_logits: list[int], max_answer_length: int, top_spans: int, - ) -> List[DPRSpanPrediction]: + ) -> list[DPRSpanPrediction]: """ Finds the best answer span for the extractive Q&A model for one passage. It returns the best span by descending `span_score` order and keeping max `top_spans` spans. Spans longer that `max_answer_length` are ignored. diff --git a/src/transformers/models/dpt/configuration_dpt.py b/src/transformers/models/dpt/configuration_dpt.py index 22f25e18423..be789b9a5bb 100644 --- a/src/transformers/models/dpt/configuration_dpt.py +++ b/src/transformers/models/dpt/configuration_dpt.py @@ -67,7 +67,7 @@ class DPTConfig(PretrainedConfig): Whether to use a hybrid backbone. Useful in the context of loading DPT-Hybrid models. qkv_bias (`bool`, *optional*, defaults to `True`): Whether to add a bias to the queries, keys and values. - backbone_out_indices (`List[int]`, *optional*, defaults to `[2, 5, 8, 11]`): + backbone_out_indices (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`): Indices of the intermediate hidden states to use from backbone. readout_type (`str`, *optional*, defaults to `"project"`): The readout type to use when processing the readout token (CLS token) of the intermediate hidden states of @@ -78,9 +78,9 @@ class DPTConfig(PretrainedConfig): - "project" passes information to the other tokens by concatenating the readout to all other tokens before projecting the representation to the original feature dimension D using a linear layer followed by a GELU non-linearity. - reassemble_factors (`List[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): + reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): The up/downsampling factors of the reassemble layers. - neck_hidden_sizes (`List[str]`, *optional*, defaults to `[96, 192, 384, 768]`): + neck_hidden_sizes (`list[str]`, *optional*, defaults to `[96, 192, 384, 768]`): The hidden sizes to project to for the feature maps of the backbone. fusion_hidden_size (`int`, *optional*, defaults to 256): The number of channels before fusion. @@ -100,11 +100,11 @@ class DPTConfig(PretrainedConfig): The index that is ignored by the loss function of the semantic segmentation model. semantic_classifier_dropout (`float`, *optional*, defaults to 0.1): The dropout ratio for the semantic classification head. - backbone_featmap_shape (`List[int]`, *optional*, defaults to `[1, 1024, 24, 24]`): + backbone_featmap_shape (`list[int]`, *optional*, defaults to `[1, 1024, 24, 24]`): Used only for the `hybrid` embedding type. The shape of the feature maps of the backbone. - neck_ignore_stages (`List[int]`, *optional*, defaults to `[0, 1]`): + neck_ignore_stages (`list[int]`, *optional*, defaults to `[0, 1]`): Used only for the `hybrid` embedding type. The stages of the readout layers to ignore. - backbone_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + backbone_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the backbone model. Only used in case `is_hybrid` is `True` or in case you want to leverage the [`AutoBackbone`] API. backbone (`str`, *optional*): @@ -282,7 +282,7 @@ class DPTConfig(PretrainedConfig): def to_dict(self): """ Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) diff --git a/src/transformers/models/dpt/image_processing_dpt.py b/src/transformers/models/dpt/image_processing_dpt.py index 5eebeebbe1c..9535a700c72 100644 --- a/src/transformers/models/dpt/image_processing_dpt.py +++ b/src/transformers/models/dpt/image_processing_dpt.py @@ -16,7 +16,7 @@ import math from collections.abc import Iterable -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...utils.import_utils import requires @@ -69,7 +69,7 @@ def get_resize_output_image_size( keep_aspect_ratio: bool, multiple: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: def constrain_to_multiple_of(val, multiple, min_val=0, max_val=None): x = round(val / multiple) * multiple @@ -113,7 +113,7 @@ class DPTImageProcessor(BaseImageProcessor): Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 384, "width": 384}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 384, "width": 384}`): Size of the image after resizing. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): Defines the resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. @@ -131,10 +131,10 @@ class DPTImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_pad (`bool`, *optional*, defaults to `False`): @@ -155,15 +155,15 @@ class DPTImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = False, size_divisor: Optional[int] = None, do_reduce_labels: bool = False, @@ -189,7 +189,7 @@ class DPTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, resample: PILImageResampling = PILImageResampling.BICUBIC, @@ -205,7 +205,7 @@ class DPTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Target size of the output image. keep_aspect_ratio (`bool`, *optional*, defaults to `False`): If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. @@ -298,15 +298,15 @@ class DPTImageProcessor(BaseImageProcessor): image: ImageInput, do_reduce_labels: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, keep_aspect_ratio: Optional[bool] = None, ensure_multiple_of: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, size_divisor: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -339,15 +339,15 @@ class DPTImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, keep_aspect_ratio: Optional[bool] = None, ensure_multiple_of: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, size_divisor: Optional[int] = None, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -390,7 +390,7 @@ class DPTImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, keep_aspect_ratio: Optional[bool] = None, ensure_multiple_of: Optional[int] = None, @@ -446,8 +446,8 @@ class DPTImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, size_divisor: Optional[int] = None, do_reduce_labels: Optional[bool] = None, @@ -466,7 +466,7 @@ class DPTImageProcessor(BaseImageProcessor): Segmentation map to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after reszing. If `keep_aspect_ratio` is `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. If `ensure_multiple_of` is set, the image is resized to a size that is a multiple of this value. @@ -484,9 +484,9 @@ class DPTImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 @@ -591,19 +591,19 @@ class DPTImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) # Copied from transformers.models.beit.image_processing_beit.BeitImageProcessor.post_process_semantic_segmentation with Beit->DPT - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`DPTForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`DPTForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ @@ -637,8 +637,8 @@ class DPTImageProcessor(BaseImageProcessor): def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - ) -> List[Dict[str, TensorType]]: + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + ) -> list[dict[str, TensorType]]: """ Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images. Only supports PyTorch. @@ -646,12 +646,12 @@ class DPTImageProcessor(BaseImageProcessor): Args: outputs ([`DepthEstimatorOutput`]): Raw outputs of the model. - target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ requires_backends(self, "torch") diff --git a/src/transformers/models/dpt/modeling_dpt.py b/src/transformers/models/dpt/modeling_dpt.py index 18cc873b552..d3c1703b9ee 100755 --- a/src/transformers/models/dpt/modeling_dpt.py +++ b/src/transformers/models/dpt/modeling_dpt.py @@ -21,7 +21,7 @@ https://github.com/open-mmlab/mmsegmentation/blob/master/mmseg/models/decode_hea import collections.abc from dataclasses import dataclass -from typing import Callable, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -54,7 +54,7 @@ class BaseModelOutputWithIntermediateActivations(ModelOutput): """ last_hidden_states: Optional[torch.FloatTensor] = None - intermediate_activations: Optional[Tuple[torch.FloatTensor, ...]] = None + intermediate_activations: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -88,9 +88,9 @@ class BaseModelOutputWithPoolingAndIntermediateActivations(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - intermediate_activations: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + intermediate_activations: Optional[tuple[torch.FloatTensor, ...]] = None class DPTViTHybridEmbeddings(nn.Module): @@ -341,7 +341,7 @@ class DPTSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -402,7 +402,7 @@ class DPTViTAttention(nn.Module): self.pruned_heads = set() # Copied from transformers.models.vit.modeling_vit.ViTAttention.prune_heads - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -426,7 +426,7 @@ class DPTViTAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -487,7 +487,7 @@ class DPTViTLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViT, layernorm is applied before self-attention head_mask, @@ -629,10 +629,10 @@ class DPTReassembleStage(nn.Module): nn.Sequential(nn.Linear(2 * hidden_size, hidden_size), ACT2FN[config.hidden_act]) ) - def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height=None, patch_width=None) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): List of hidden states from the backbone. """ out = [] @@ -890,7 +890,7 @@ class DPTModel(DPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndIntermediateActivations]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndIntermediateActivations]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -978,10 +978,10 @@ class DPTNeck(nn.Module): # fusion self.fusion_stage = DPTFeatureFusionStage(config) - def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height=None, patch_width=None) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): List of hidden states from the backbone. """ if not isinstance(hidden_states, (tuple, list)): @@ -1028,7 +1028,7 @@ class DPTDepthEstimationHead(nn.Module): nn.ReLU(), ) - def forward(self, hidden_states: List[torch.Tensor]) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor]) -> torch.Tensor: # use last features hidden_states = hidden_states[self.config.head_in_index] @@ -1076,7 +1076,7 @@ class DPTForDepthEstimation(DPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth depth estimation maps for computing the loss. @@ -1195,7 +1195,7 @@ class DPTSemanticSegmentationHead(nn.Module): nn.Upsample(scale_factor=2, mode="bilinear", align_corners=True), ) - def forward(self, hidden_states: List[torch.Tensor]) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor]) -> torch.Tensor: # use last features hidden_states = hidden_states[self.config.head_in_index] @@ -1249,7 +1249,7 @@ class DPTForSemanticSegmentation(DPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SemanticSegmenterOutput]: + ) -> Union[tuple[torch.Tensor], SemanticSegmenterOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth semantic segmentation maps for computing the loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/efficientnet/configuration_efficientnet.py b/src/transformers/models/efficientnet/configuration_efficientnet.py index f76e49b7e6c..54931190290 100644 --- a/src/transformers/models/efficientnet/configuration_efficientnet.py +++ b/src/transformers/models/efficientnet/configuration_efficientnet.py @@ -16,7 +16,6 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import List from packaging import version @@ -49,19 +48,19 @@ class EfficientNetConfig(PretrainedConfig): Scaling coefficient for network depth at each stage. depth_divisor `int`, *optional*, defaults to 8): A unit of network width. - kernel_sizes (`List[int]`, *optional*, defaults to `[3, 3, 5, 3, 5, 5, 3]`): + kernel_sizes (`list[int]`, *optional*, defaults to `[3, 3, 5, 3, 5, 5, 3]`): List of kernel sizes to be used in each block. - in_channels (`List[int]`, *optional*, defaults to `[32, 16, 24, 40, 80, 112, 192]`): + in_channels (`list[int]`, *optional*, defaults to `[32, 16, 24, 40, 80, 112, 192]`): List of input channel sizes to be used in each block for convolutional layers. - out_channels (`List[int]`, *optional*, defaults to `[16, 24, 40, 80, 112, 192, 320]`): + out_channels (`list[int]`, *optional*, defaults to `[16, 24, 40, 80, 112, 192, 320]`): List of output channel sizes to be used in each block for convolutional layers. - depthwise_padding (`List[int]`, *optional*, defaults to `[]`): + depthwise_padding (`list[int]`, *optional*, defaults to `[]`): List of block indices with square padding. - strides (`List[int]`, *optional*, defaults to `[1, 2, 2, 2, 1, 2, 1]`): + strides (`list[int]`, *optional*, defaults to `[1, 2, 2, 2, 1, 2, 1]`): List of stride sizes to be used in each block for convolutional layers. - num_block_repeats (`List[int]`, *optional*, defaults to `[1, 2, 2, 3, 3, 4, 1]`): + num_block_repeats (`list[int]`, *optional*, defaults to `[1, 2, 2, 3, 3, 4, 1]`): List of the number of times each block is to repeated. - expand_ratios (`List[int]`, *optional*, defaults to `[1, 6, 6, 6, 6, 6, 6]`): + expand_ratios (`list[int]`, *optional*, defaults to `[1, 6, 6, 6, 6, 6, 6]`): List of scaling coefficient of each block. squeeze_expansion_ratio (`float`, *optional*, defaults to 0.25): Squeeze expansion ratio. @@ -107,13 +106,13 @@ class EfficientNetConfig(PretrainedConfig): width_coefficient: float = 2.0, depth_coefficient: float = 3.1, depth_divisor: int = 8, - kernel_sizes: List[int] = [3, 3, 5, 3, 5, 5, 3], - in_channels: List[int] = [32, 16, 24, 40, 80, 112, 192], - out_channels: List[int] = [16, 24, 40, 80, 112, 192, 320], - depthwise_padding: List[int] = [], - strides: List[int] = [1, 2, 2, 2, 1, 2, 1], - num_block_repeats: List[int] = [1, 2, 2, 3, 3, 4, 1], - expand_ratios: List[int] = [1, 6, 6, 6, 6, 6, 6], + kernel_sizes: list[int] = [3, 3, 5, 3, 5, 5, 3], + in_channels: list[int] = [32, 16, 24, 40, 80, 112, 192], + out_channels: list[int] = [16, 24, 40, 80, 112, 192, 320], + depthwise_padding: list[int] = [], + strides: list[int] = [1, 2, 2, 2, 1, 2, 1], + num_block_repeats: list[int] = [1, 2, 2, 3, 3, 4, 1], + expand_ratios: list[int] = [1, 6, 6, 6, 6, 6, 6], squeeze_expansion_ratio: float = 0.25, hidden_act: str = "swish", hidden_dim: int = 2560, diff --git a/src/transformers/models/efficientnet/image_processing_efficientnet.py b/src/transformers/models/efficientnet/image_processing_efficientnet.py index 6aa42f18ce9..880bfd21995 100644 --- a/src/transformers/models/efficientnet/image_processing_efficientnet.py +++ b/src/transformers/models/efficientnet/image_processing_efficientnet.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for EfficientNet.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -51,14 +51,14 @@ class EfficientNetImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 346, "width": 346}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 346, "width": 346}`): Size of the image after `resize`. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling` filter, *optional*, defaults to 0): Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. do_center_crop (`bool`, *optional*, defaults to `False`): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by `do_center_crop` in `preprocess`. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 289, "width": 289}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 289, "width": 289}`): Desired output size when applying center-cropping. Can be overridden by `crop_size` in `preprocess`. rescale_factor (`int` or `float`, *optional*, defaults to `1/255`): Scale factor to use if rescaling the image. Can be overridden by the `rescale_factor` parameter in the @@ -72,10 +72,10 @@ class EfficientNetImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. include_top (`bool`, *optional*, defaults to `True`): @@ -87,16 +87,16 @@ class EfficientNetImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PIL.Image.NEAREST, do_center_crop: bool = False, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, rescale_factor: Union[int, float] = 1 / 255, rescale_offset: bool = False, do_rescale: bool = True, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, include_top: bool = True, **kwargs, ) -> None: @@ -123,7 +123,7 @@ class EfficientNetImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.NEAREST, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -135,7 +135,7 @@ class EfficientNetImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.NEAREST`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.NEAREST`. @@ -213,16 +213,16 @@ class EfficientNetImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, rescale_offset: Optional[bool] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, include_top: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -237,14 +237,14 @@ class EfficientNetImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after `resize`. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): PILImageResampling filter to use if resizing the image Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after center crop. If one edge the image is smaller than `crop_size`, it will be padded with zeros and then cropped do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -255,9 +255,9 @@ class EfficientNetImageProcessor(BaseImageProcessor): Whether to rescale the image between [-scale_range, scale_range] instead of [0, scale_range]. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. include_top (`bool`, *optional*, defaults to `self.include_top`): Rescales the image again for image classification if set to True. diff --git a/src/transformers/models/efficientnet/modeling_efficientnet.py b/src/transformers/models/efficientnet/modeling_efficientnet.py index d629cf622c4..26410fc120a 100644 --- a/src/transformers/models/efficientnet/modeling_efficientnet.py +++ b/src/transformers/models/efficientnet/modeling_efficientnet.py @@ -15,7 +15,7 @@ """PyTorch EfficientNet model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -51,7 +51,7 @@ def round_filters(config: EfficientNetConfig, num_channels: int): return int(new_dim) -def correct_pad(kernel_size: Union[int, Tuple], adjust: bool = True): +def correct_pad(kernel_size: Union[int, tuple], adjust: bool = True): r""" Utility function to get the tuple padding value for the depthwise convolution. @@ -475,7 +475,7 @@ class EfficientNetModel(EfficientNetPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -533,7 +533,7 @@ class EfficientNetForImageClassification(EfficientNetPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/electra/modeling_electra.py b/src/transformers/models/electra/modeling_electra.py index 0ebfa3d479c..dfe5849a5a7 100644 --- a/src/transformers/models/electra/modeling_electra.py +++ b/src/transformers/models/electra/modeling_electra.py @@ -17,7 +17,7 @@ import math import os from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -237,9 +237,9 @@ class ElectraSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -387,9 +387,9 @@ class ElectraAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -458,9 +458,9 @@ class ElectraLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -537,12 +537,12 @@ class ElectraEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -702,8 +702,8 @@ class ElectraForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -745,12 +745,12 @@ class ElectraModel(ElectraPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -978,7 +978,7 @@ class ElectraForSequenceClassification(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1066,7 +1066,7 @@ class ElectraForPreTraining(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], ElectraForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], ElectraForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the ELECTRA loss. Input should be a sequence of tokens (see `input_ids` docstring) @@ -1178,7 +1178,7 @@ class ElectraForMaskedLM(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1255,7 +1255,7 @@ class ElectraForTokenClassification(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1324,7 +1324,7 @@ class ElectraForQuestionAnswering(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict discriminator_hidden_states = self.electra( @@ -1403,7 +1403,7 @@ class ElectraForMultipleChoice(ElectraPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1520,13 +1520,13 @@ class ElectraForCausalLM(ElectraPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in diff --git a/src/transformers/models/electra/modeling_flax_electra.py b/src/transformers/models/electra/modeling_flax_electra.py index 7cc20ec27fc..14d845476d6 100644 --- a/src/transformers/models/electra/modeling_flax_electra.py +++ b/src/transformers/models/electra/modeling_flax_electra.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax import flax.linen as nn @@ -78,8 +78,8 @@ class FlaxElectraForPreTrainingOutput(ModelOutput): """ logits: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None ELECTRA_START_DOCSTRING = r""" @@ -686,7 +686,7 @@ class FlaxElectraPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: ElectraConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -705,7 +705,7 @@ class FlaxElectraPreTrainedModel(FlaxPreTrainedModel): ) # Copied from transformers.models.bert.modeling_flax_bert.FlaxBertPreTrainedModel.init_weights - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.zeros_like(input_ids) diff --git a/src/transformers/models/electra/modeling_tf_electra.py b/src/transformers/models/electra/modeling_tf_electra.py index 6dc3ac8ebf8..b3af0f3afc4 100644 --- a/src/transformers/models/electra/modeling_tf_electra.py +++ b/src/transformers/models/electra/modeling_tf_electra.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -109,10 +109,10 @@ class TFElectraSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -247,10 +247,10 @@ class TFElectraAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -364,10 +364,10 @@ class TFElectraLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -460,13 +460,13 @@ class TFElectraEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -806,13 +806,13 @@ class TFElectraMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -932,8 +932,8 @@ class TFElectraForPreTrainingOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None ELECTRA_START_DOCSTRING = r""" @@ -1057,13 +1057,13 @@ class TFElectraModel(TFElectraPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1075,7 +1075,7 @@ class TFElectraModel(TFElectraPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1143,7 +1143,7 @@ class TFElectraForPreTraining(TFElectraPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFElectraForPreTrainingOutput, Tuple[tf.Tensor]]: + ) -> Union[TFElectraForPreTrainingOutput, tuple[tf.Tensor]]: r""" Returns: @@ -1286,7 +1286,7 @@ class TFElectraForMaskedLM(TFElectraPreTrainedModel, TFMaskedLanguageModelingLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1415,7 +1415,7 @@ class TFElectraForSequenceClassification(TFElectraPreTrainedModel, TFSequenceCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1501,7 +1501,7 @@ class TFElectraForMultipleChoice(TFElectraPreTrainedModel, TFMultipleChoiceLoss) return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1612,7 +1612,7 @@ class TFElectraForTokenClassification(TFElectraPreTrainedModel, TFTokenClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1701,7 +1701,7 @@ class TFElectraForQuestionAnswering(TFElectraPreTrainedModel, TFQuestionAnswerin start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/electra/tokenization_electra.py b/src/transformers/models/electra/tokenization_electra.py index f9c0aea392d..e252a199104 100644 --- a/src/transformers/models/electra/tokenization_electra.py +++ b/src/transformers/models/electra/tokenization_electra.py @@ -16,7 +16,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -186,8 +186,8 @@ class ElectraTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Electra sequence has the following format: @@ -211,8 +211,8 @@ class ElectraTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -238,7 +238,7 @@ class ElectraTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/electra/tokenization_electra_fast.py b/src/transformers/models/electra/tokenization_electra_fast.py index 6767e3e3517..db0285581ed 100644 --- a/src/transformers/models/electra/tokenization_electra_fast.py +++ b/src/transformers/models/electra/tokenization_electra_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -135,7 +135,7 @@ class ElectraTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/emu3/configuration_emu3.py b/src/transformers/models/emu3/configuration_emu3.py index 22ab66f6d0c..35c83937c8b 100644 --- a/src/transformers/models/emu3/configuration_emu3.py +++ b/src/transformers/models/emu3/configuration_emu3.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, List, Optional, Union +from typing import Optional, Union from ...configuration_utils import PretrainedConfig from ...modeling_rope_utils import rope_config_validation @@ -45,11 +45,11 @@ class Emu3VQVAEConfig(PretrainedConfig): Temporal downsample factor. base_channels (`int`, *optional*, defaults to 256): Basic channel number of the intermediate blocks. - channel_multiplier (`List[int]`, *optional*, defaults to `[1, 2, 2, 4]`): + channel_multiplier (`list[int]`, *optional*, defaults to `[1, 2, 2, 4]`): Channel scaling factor of the intermediate blocks. num_res_blocks (`int`, *optional*, defaults to 2): Residual block number in each stage. - attn_resolutions (`List[int]`, *optional*, defaults to `[3]`): + attn_resolutions (`list[int]`, *optional*, defaults to `[3]`): Stage indices to apply attention. hidden_size (`int`, *optional*, defaults to 1024): Dimension of the hidden representations in the attention layer. @@ -84,9 +84,9 @@ class Emu3VQVAEConfig(PretrainedConfig): out_channels: int = 3, temporal_downsample_factor: int = 4, base_channels: int = 256, - channel_multiplier: List[int] = [1, 2, 2, 4], + channel_multiplier: list[int] = [1, 2, 2, 4], num_res_blocks: int = 2, - attn_resolutions: List[int] = [3], + attn_resolutions: list[int] = [3], hidden_size: int = 1024, num_attention_heads: int = 1, attention_dropout: float = 0.0, @@ -185,11 +185,11 @@ class Emu3TextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -302,9 +302,9 @@ class Emu3Config(PretrainedConfig): def __init__( self, - vq_config: Union[Dict, Emu3VQVAEConfig] = None, - text_config: Union[Dict, Emu3TextConfig] = None, - vocabulary_map: Optional[Dict[int, int]] = None, + vq_config: Union[dict, Emu3VQVAEConfig] = None, + text_config: Union[dict, Emu3TextConfig] = None, + vocabulary_map: Optional[dict[int, int]] = None, **kwargs, ): if vq_config is None: diff --git a/src/transformers/models/emu3/convert_emu3_weights_to_hf.py b/src/transformers/models/emu3/convert_emu3_weights_to_hf.py index ddc907adb4c..697ccf68f7c 100644 --- a/src/transformers/models/emu3/convert_emu3_weights_to_hf.py +++ b/src/transformers/models/emu3/convert_emu3_weights_to_hf.py @@ -15,7 +15,7 @@ import argparse import json import os import re -from typing import Dict, Optional +from typing import Optional import requests import torch @@ -66,7 +66,7 @@ def token_bytes_to_string(b): # Adapted from https://github.com/openai/tiktoken/issues/60#issuecomment-1499977960 -def bpe(mergeable_ranks: Dict[bytes, int], token: bytes, max_rank: Optional[int] = None): +def bpe(mergeable_ranks: dict[bytes, int], token: bytes, max_rank: Optional[int] = None): parts = [bytes([b]) for b in token] while True: min_idx = None diff --git a/src/transformers/models/emu3/image_processing_emu3.py b/src/transformers/models/emu3/image_processing_emu3.py index c82f2dc42ae..b96983b0e85 100644 --- a/src/transformers/models/emu3/image_processing_emu3.py +++ b/src/transformers/models/emu3/image_processing_emu3.py @@ -16,7 +16,7 @@ import math from collections.abc import Iterable -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -46,12 +46,12 @@ if is_vision_available(): logger = logging.get_logger(__name__) -def make_batched_images(images) -> List[List[ImageInput]]: +def make_batched_images(images) -> list[list[ImageInput]]: """ Accepts images in list or nested list format, and makes a list of images for preprocessing. Args: - images (`Union[List[List[ImageInput]], List[ImageInput], ImageInput]`): + images (`Union[list[list[ImageInput]], list[ImageInput], ImageInput]`): The input image. Returns: @@ -113,9 +113,9 @@ class Emu3ImageProcessor(BaseImageProcessor): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats for each channel in the image. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats for each channel in the image. do_convert_rgb (`bool`, *optional*, defaults to `True`): Whether to convert the image to RGB. @@ -139,8 +139,8 @@ class Emu3ImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, do_pad: bool = True, min_pixels: int = 512 * 512, @@ -170,8 +170,8 @@ class Emu3ImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -182,7 +182,7 @@ class Emu3ImageProcessor(BaseImageProcessor): Args: images (`ImageInput`): Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`. - vision_info (`List[Dict]`, *optional*): + vision_info (`list[Dict]`, *optional*): Optional list of dictionaries containing additional information about vision inputs. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. @@ -194,9 +194,9 @@ class Emu3ImageProcessor(BaseImageProcessor): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. @@ -260,8 +260,8 @@ class Emu3ImageProcessor(BaseImageProcessor): def _pad_for_batching( self, - pixel_values: List[np.ndarray], - image_sizes: List[List[int]], + pixel_values: list[np.ndarray], + image_sizes: list[list[int]], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): @@ -269,9 +269,9 @@ class Emu3ImageProcessor(BaseImageProcessor): Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[np.ndarray]`): + pixel_values (`list[np.ndarray]`): An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`) - image_sizes (`List[List[int]]`): + image_sizes (`list[list[int]]`): A list of sizes for each image in `pixel_values` in (height, width) format. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. Can be one of: @@ -285,7 +285,7 @@ class Emu3ImageProcessor(BaseImageProcessor): If unset, will use the inferred format of the input image. Returns: - List[`np.ndarray`]: The padded images. + list[`np.ndarray`]: The padded images. """ max_shape = ( @@ -307,13 +307,13 @@ class Emu3ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, do_pad: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -327,7 +327,7 @@ class Emu3ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -339,9 +339,9 @@ class Emu3ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): @@ -431,8 +431,8 @@ class Emu3ImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Union[str, TensorType] = "PIL.Image.Image", input_data_format: Optional[Union[str, ChannelDimension]] = None, ): @@ -448,9 +448,9 @@ class Emu3ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/emu3/modeling_emu3.py b/src/transformers/models/emu3/modeling_emu3.py index 348c0ea8d7a..105bcb4f22b 100644 --- a/src/transformers/models/emu3/modeling_emu3.py +++ b/src/transformers/models/emu3/modeling_emu3.py @@ -22,7 +22,7 @@ import math from functools import cached_property -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -184,12 +184,12 @@ class Emu3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -246,9 +246,9 @@ class Emu3DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -362,8 +362,8 @@ class Emu3VQVAEConv3d(nn.Module): self, in_channel: int, out_channel: int, - kernel_size: Tuple[int], - stride: Tuple[int], + kernel_size: tuple[int], + stride: tuple[int], ): super().__init__() @@ -607,7 +607,7 @@ class Emu3VQVAEAttentionBlock(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -1114,7 +1114,7 @@ class Emu3ImageVocabularyMapping: mapping[k] = v return mapping - def convert_img2bpe(self, img_batch: List[torch.Tensor]) -> torch.Tensor: + def convert_img2bpe(self, img_batch: list[torch.Tensor]) -> torch.Tensor: device = img_batch.device eol_row = torch.ones((img_batch.shape[0], 1), dtype=torch.int) * self.eol_token_id img_tokens = self.img2bpe_mapping_tensor[img_batch.to("cpu")] @@ -1522,7 +1522,7 @@ class Emu3Model(Emu3PreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`): The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using @@ -1639,7 +1639,7 @@ class Emu3ForConditionalGeneration(Emu3PreTrainedModel, GenerationMixin): labels: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`): The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using diff --git a/src/transformers/models/emu3/modular_emu3.py b/src/transformers/models/emu3/modular_emu3.py index 58461f8bd61..ed58e43f86f 100644 --- a/src/transformers/models/emu3/modular_emu3.py +++ b/src/transformers/models/emu3/modular_emu3.py @@ -16,7 +16,7 @@ import math from functools import cached_property -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -54,9 +54,9 @@ class Emu3DecoderLayer(LlamaDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -162,8 +162,8 @@ class Emu3VQVAEConv3d(nn.Module): self, in_channel: int, out_channel: int, - kernel_size: Tuple[int], - stride: Tuple[int], + kernel_size: tuple[int], + stride: tuple[int], ): super().__init__() @@ -848,7 +848,7 @@ class Emu3ImageVocabularyMapping: mapping[k] = v return mapping - def convert_img2bpe(self, img_batch: List[torch.Tensor]) -> torch.Tensor: + def convert_img2bpe(self, img_batch: list[torch.Tensor]) -> torch.Tensor: device = img_batch.device eol_row = torch.ones((img_batch.shape[0], 1), dtype=torch.int) * self.eol_token_id img_tokens = self.img2bpe_mapping_tensor[img_batch.to("cpu")] @@ -1009,7 +1009,7 @@ class Emu3Model(Emu3PreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`): The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using @@ -1126,7 +1126,7 @@ class Emu3ForConditionalGeneration(Emu3PreTrainedModel, GenerationMixin): labels: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_sizes (`torch.LongTensor` of shape `(batch_size, 2)`): The sizes of the images in the batch, being (height, width) for each image. Image sizes can be obtained using diff --git a/src/transformers/models/emu3/processing_emu3.py b/src/transformers/models/emu3/processing_emu3.py index 61b40217723..6516aab85fc 100644 --- a/src/transformers/models/emu3/processing_emu3.py +++ b/src/transformers/models/emu3/processing_emu3.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -94,7 +94,7 @@ class Emu3Processor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[Emu3ProcessorKwargs], @@ -107,10 +107,10 @@ class Emu3Processor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -203,7 +203,7 @@ class Emu3Processor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/encodec/configuration_encodec.py b/src/transformers/models/encodec/configuration_encodec.py index c00b5ebd1ca..a4443e2b5a0 100644 --- a/src/transformers/models/encodec/configuration_encodec.py +++ b/src/transformers/models/encodec/configuration_encodec.py @@ -37,7 +37,7 @@ class EncodecConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - target_bandwidths (`List[float]`, *optional*, defaults to `[1.5, 3.0, 6.0, 12.0, 24.0]`): + target_bandwidths (`list[float]`, *optional*, defaults to `[1.5, 3.0, 6.0, 12.0, 24.0]`): The range of different bandwidths the model can encode audio with. sampling_rate (`int`, *optional*, defaults to 24000): The sampling rate at which the audio waveform should be digitalized expressed in hertz (Hz). diff --git a/src/transformers/models/encodec/feature_extraction_encodec.py b/src/transformers/models/encodec/feature_extraction_encodec.py index f33191862e4..3cc8d523f7f 100644 --- a/src/transformers/models/encodec/feature_extraction_encodec.py +++ b/src/transformers/models/encodec/feature_extraction_encodec.py @@ -14,7 +14,7 @@ # limitations under the License. """Feature extractor class for EnCodec.""" -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -83,7 +83,7 @@ class EncodecFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_audio: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_audio: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Optional[Union[bool, str, PaddingStrategy]] = None, truncation: Optional[bool] = False, max_length: Optional[int] = None, @@ -94,7 +94,7 @@ class EncodecFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_audio (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_audio (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be processed. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. The numpy array must be of shape `(num_samples,)` for mono audio (`feature_size = 1`), or `(2, num_samples)` for stereo audio diff --git a/src/transformers/models/encodec/modeling_encodec.py b/src/transformers/models/encodec/modeling_encodec.py index cc7b3bc72f1..bf276b4811a 100644 --- a/src/transformers/models/encodec/modeling_encodec.py +++ b/src/transformers/models/encodec/modeling_encodec.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -133,7 +133,7 @@ class EncodecConv1d(nn.Module): return ideal_length - length @staticmethod - def _pad1d(hidden_states: torch.Tensor, paddings: Tuple[int, int], mode: str = "zero", value: float = 0.0): + def _pad1d(hidden_states: torch.Tensor, paddings: tuple[int, int], mode: str = "zero", value: float = 0.0): """Tiny wrapper around torch.nn.functional.pad, just to allow for reflect padding on small input. If this is the case, we insert extra 0 padding to the right before the reflection happens. """ @@ -251,7 +251,7 @@ class EncodecResnetBlock(nn.Module): Residual block from SEANet model as used by EnCodec. """ - def __init__(self, config: EncodecConfig, dim: int, dilations: List[int]): + def __init__(self, config: EncodecConfig, dim: int, dilations: list[int]): super().__init__() kernel_sizes = (config.residual_kernel_size, 1) if len(kernel_sizes) != len(dilations): @@ -506,7 +506,7 @@ class EncodecModel(EncodecPreTrainedModel): def _encode_frame( self, input_values: torch.Tensor, bandwidth: float, padding_mask: int - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ Encodes the given input using the underlying VQVAE. If `config.normalize` is set to `True` the input is first normalized. The padding mask is required to compute the correct scale. @@ -536,7 +536,7 @@ class EncodecModel(EncodecPreTrainedModel): padding_mask: Optional[torch.Tensor] = None, bandwidth: Optional[float] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, Optional[torch.Tensor]], EncodecEncoderOutput]: + ) -> Union[tuple[torch.Tensor, Optional[torch.Tensor]], EncodecEncoderOutput]: """ Encodes the input audio waveform into discrete codes. @@ -603,7 +603,7 @@ class EncodecModel(EncodecPreTrainedModel): return EncodecEncoderOutput(encoded_frames, scales) @staticmethod - def _linear_overlap_add(frames: List[torch.Tensor], stride: int): + def _linear_overlap_add(frames: list[torch.Tensor], stride: int): # Generic overlap add, with linear fade-in/fade-out, supporting complex scenario # e.g., more than 2 frames per position. # The core idea is to use a weight function that is a triangle, @@ -663,7 +663,7 @@ class EncodecModel(EncodecPreTrainedModel): audio_scales: torch.Tensor, padding_mask: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], EncodecDecoderOutput]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], EncodecDecoderOutput]: """ Decodes the given frames into an output audio waveform. @@ -714,7 +714,7 @@ class EncodecModel(EncodecPreTrainedModel): audio_codes: Optional[torch.Tensor] = None, audio_scales: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], EncodecOutput]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], EncodecOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, channels, sequence_length)`, *optional*): Raw audio input converted to Float and padded to the appropriate length in order to be encoded using chunks diff --git a/src/transformers/models/encoder_decoder/modeling_encoder_decoder.py b/src/transformers/models/encoder_decoder/modeling_encoder_decoder.py index 8aaced73980..796a104023d 100644 --- a/src/transformers/models/encoder_decoder/modeling_encoder_decoder.py +++ b/src/transformers/models/encoder_decoder/modeling_encoder_decoder.py @@ -19,7 +19,7 @@ import inspect import os import tempfile import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -452,8 +452,8 @@ class EncoderDecoderModel(PreTrainedModel, GenerationMixin): attention_mask: Optional[torch.FloatTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -462,7 +462,7 @@ class EncoderDecoderModel(PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/encoder_decoder/modeling_flax_encoder_decoder.py b/src/transformers/models/encoder_decoder/modeling_flax_encoder_decoder.py index 38551ddc209..13da12b1aa7 100644 --- a/src/transformers/models/encoder_decoder/modeling_flax_encoder_decoder.py +++ b/src/transformers/models/encoder_decoder/modeling_flax_encoder_decoder.py @@ -15,7 +15,7 @@ """Classes to support Flax Encoder-Decoder architectures""" import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import flax.linen as nn import jax @@ -188,7 +188,7 @@ ENCODER_DECODER_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.decoder.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -313,7 +313,7 @@ class FlaxEncoderDecoderModel(FlaxPreTrainedModel): def __init__( self, config: EncoderDecoderConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -339,7 +339,7 @@ class FlaxEncoderDecoderModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: encoder_input_shape, decoder_input_shape = input_shape # init input tensors diff --git a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py index bf375dbd40e..bd0a75e96f4 100644 --- a/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py +++ b/src/transformers/models/encoder_decoder/modeling_tf_encoder_decoder.py @@ -19,7 +19,7 @@ from __future__ import annotations import inspect import re import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -89,7 +89,7 @@ ENCODER_DECODER_START_DOCSTRING = r""" ENCODER_DECODER_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`PreTrainedTokenizer`]. See [`PreTrainedTokenizer.encode`] and @@ -461,7 +461,7 @@ class TFEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLoss): decoder_input_ids: np.ndarray | tf.Tensor | None = None, decoder_attention_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, labels: np.ndarray | tf.Tensor | None = None, @@ -471,7 +471,7 @@ class TFEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, training: bool = False, **kwargs, - ) -> Union[TFSeq2SeqLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqLMOutput, tuple[tf.Tensor]]: r""" Returns: diff --git a/src/transformers/models/ernie/modeling_ernie.py b/src/transformers/models/ernie/modeling_ernie.py index bd52f56cf5c..21a79a3fabc 100644 --- a/src/transformers/models/ernie/modeling_ernie.py +++ b/src/transformers/models/ernie/modeling_ernie.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -162,9 +162,9 @@ class ErnieSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -312,9 +312,9 @@ class ErnieAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -383,9 +383,9 @@ class ErnieLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -462,12 +462,12 @@ class ErnieEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -688,8 +688,8 @@ class ErnieForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -750,12 +750,12 @@ class ErnieModel(ErniePreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -902,7 +902,7 @@ class ErnieForPreTraining(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], ErnieForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], ErnieForPreTrainingOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1017,13 +1017,13 @@ class ErnieForCausalLM(ErniePreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1136,7 +1136,7 @@ class ErnieForMaskedLM(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1242,7 +1242,7 @@ class ErnieForNextSentencePrediction(ErniePreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], NextSentencePredictorOutput]: + ) -> Union[tuple[torch.Tensor], NextSentencePredictorOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1356,7 +1356,7 @@ class ErnieForSequenceClassification(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1452,7 +1452,7 @@ class ErnieForMultipleChoice(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1568,7 +1568,7 @@ class ErnieForTokenClassification(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as @@ -1643,7 +1643,7 @@ class ErnieForQuestionAnswering(ErniePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" task_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Task type embedding is a special embedding to represent the characteristic of different tasks, such as diff --git a/src/transformers/models/esm/configuration_esm.py b/src/transformers/models/esm/configuration_esm.py index a7d8efab640..fabfb4ebd6d 100644 --- a/src/transformers/models/esm/configuration_esm.py +++ b/src/transformers/models/esm/configuration_esm.py @@ -162,7 +162,7 @@ class EsmConfig(PretrainedConfig): Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = super().to_dict() if isinstance(self.esmfold_config, EsmFoldConfig): @@ -196,7 +196,7 @@ class EsmFoldConfig: Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = asdict(self) output["trunk"] = self.trunk.to_dict() @@ -261,7 +261,7 @@ class TrunkConfig: Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = asdict(self) output["structure_module"] = self.structure_module.to_dict() diff --git a/src/transformers/models/esm/modeling_esm.py b/src/transformers/models/esm/modeling_esm.py index 3af89dcfc36..dbf260fb216 100755 --- a/src/transformers/models/esm/modeling_esm.py +++ b/src/transformers/models/esm/modeling_esm.py @@ -16,7 +16,7 @@ """PyTorch ESM model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -113,7 +113,7 @@ class RotaryEmbedding(torch.nn.Module): return self._cos_cached, self._sin_cached - def forward(self, q: torch.Tensor, k: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, q: torch.Tensor, k: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: self._cos_cached, self._sin_cached = self._update_cos_sin_tables(k, seq_dimension=-2) return ( @@ -292,9 +292,9 @@ class EsmSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -424,9 +424,9 @@ class EsmFlashAttention2(EsmSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # Flash attention doesn't support output_attentions or cross attention if output_attentions or head_mask is not None or encoder_hidden_states is not None: logger.warning_once( @@ -879,12 +879,12 @@ class EsmModel(EsmPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `((batch_size, sequence_length))`): Indices of input sequence tokens in the vocabulary. @@ -1045,7 +1045,7 @@ class EsmForMaskedLM(EsmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1143,7 +1143,7 @@ class EsmForSequenceClassification(EsmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1228,7 +1228,7 @@ class EsmForTokenClassification(EsmPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. diff --git a/src/transformers/models/esm/modeling_esmfold.py b/src/transformers/models/esm/modeling_esmfold.py index b29423a3f9c..9012a2ef814 100644 --- a/src/transformers/models/esm/modeling_esmfold.py +++ b/src/transformers/models/esm/modeling_esmfold.py @@ -17,7 +17,7 @@ import sys from collections.abc import Sequence from dataclasses import dataclass from functools import partial -from typing import Callable, Dict, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -154,7 +154,7 @@ def is_deepspeed_initialized(): return False -def collate_dense_tensors(samples: List[torch.Tensor], pad_v: float = 0) -> torch.Tensor: +def collate_dense_tensors(samples: list[torch.Tensor], pad_v: float = 0) -> torch.Tensor: """ Takes a list of tensors with the following dimensions: [(d_11, ..., d_1K), @@ -181,7 +181,7 @@ def flatten_final_dims(t: torch.Tensor, no_dims: int): return t.reshape(t.shape[:-no_dims] + (-1,)) -def permute_final_dims(tensor: torch.Tensor, inds: List[int]): +def permute_final_dims(tensor: torch.Tensor, inds: list[int]): zero_index = -1 * len(inds) first_inds = list(range(len(tensor.shape[:zero_index]))) return tensor.permute(first_inds + [zero_index + i for i in inds]) @@ -361,7 +361,7 @@ class EsmFoldAttention(nn.Module): self.sigmoid = nn.Sigmoid() - def _prep_qkv(self, q_x: torch.Tensor, kv_x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + def _prep_qkv(self, q_x: torch.Tensor, kv_x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: # [*, Q/K/V, H * C_hidden] q = self.linear_q(q_x) k = self.linear_k(kv_x) @@ -401,7 +401,7 @@ class EsmFoldAttention(nn.Module): self, q_x: torch.Tensor, kv_x: torch.Tensor, - biases: Optional[List[torch.Tensor]] = None, + biases: Optional[list[torch.Tensor]] = None, use_memory_efficient_kernel: bool = False, use_lma: bool = False, lma_q_chunk_size: int = 1024, @@ -490,7 +490,7 @@ class EsmFoldTriangleAttention(nn.Module): def _chunk( self, x: torch.Tensor, - biases: List[torch.Tensor], + biases: list[torch.Tensor], chunk_size: int, use_memory_efficient_kernel: bool = False, use_lma: bool = False, @@ -1019,7 +1019,7 @@ class EsmFoldDropout(nn.Module): Implementation of dropout with the ability to share the dropout mask along a particular dimension. """ - def __init__(self, r: float, batch_dim: Union[int, List[int]]): + def __init__(self, r: float, batch_dim: Union[int, list[int]]): super().__init__() self.r = r @@ -1338,7 +1338,7 @@ class EsmFoldAngleResnet(nn.Module): self.relu = nn.ReLU() - def forward(self, s: torch.Tensor, s_initial: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, s: torch.Tensor, s_initial: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """ Args: s: @@ -1589,7 +1589,7 @@ class EsmFoldBackboneUpdate(nn.Module): self.linear = EsmFoldLinear(config.sequence_dim, 6, init="final") - def forward(self, s: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, s: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """ Args: [*, N_res, C_s] single representation @@ -2039,7 +2039,7 @@ class EsmForProteinFolding(EsmPreTrainedModel): ) @staticmethod - def _af2_to_esm_from_vocab_list(vocab_list: List[str]) -> torch.Tensor: + def _af2_to_esm_from_vocab_list(vocab_list: list[str]) -> torch.Tensor: # Remember that t is shifted from residue_constants by 1 (0 is padding). esm_reorder = [vocab_list.index("")] + [vocab_list.index(v) for v in residue_constants.restypes_with_x] return torch.tensor(esm_reorder) @@ -2222,7 +2222,7 @@ class EsmForProteinFolding(EsmPreTrainedModel): @torch.no_grad() def infer( self, - seqs: Union[str, List[str]], + seqs: Union[str, list[str]], position_ids=None, ): if isinstance(seqs, str): @@ -2260,7 +2260,7 @@ class EsmForProteinFolding(EsmPreTrainedModel): ) @staticmethod - def output_to_pdb(output: Dict) -> List[str]: + def output_to_pdb(output: dict) -> list[str]: """Returns the pbd (file) string from the model given the model output.""" output = {k: v.to("cpu").numpy() for k, v in output.items()} pdbs = [] @@ -2287,7 +2287,7 @@ class EsmForProteinFolding(EsmPreTrainedModel): output = self.infer(seqs, *args, **kwargs) return self.output_to_pdb(output)[0] - def infer_pdbs(self, seqs: List[str], *args, **kwargs) -> List[str]: + def infer_pdbs(self, seqs: list[str], *args, **kwargs) -> list[str]: """Returns the pdb (file) string from the model given an input sequence.""" output = self.infer(seqs, *args, **kwargs) return self.output_to_pdb(output) diff --git a/src/transformers/models/esm/modeling_tf_esm.py b/src/transformers/models/esm/modeling_tf_esm.py index 53b7e273bab..44a1e0bb2b5 100644 --- a/src/transformers/models/esm/modeling_tf_esm.py +++ b/src/transformers/models/esm/modeling_tf_esm.py @@ -17,7 +17,7 @@ from __future__ import annotations import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -116,7 +116,7 @@ class TFRotaryEmbedding(keras.layers.Layer): return tf.cos(emb), tf.sin(emb) - def call(self, q: tf.Tensor, k: tf.Tensor) -> Tuple[tf.Tensor, tf.Tensor]: + def call(self, q: tf.Tensor, k: tf.Tensor) -> tuple[tf.Tensor, tf.Tensor]: cos_emb, sin_emb = self._compute_cos_sin(k, seq_dimension=-2) return ( @@ -330,10 +330,10 @@ class TFEsmSelfAttention(keras.layers.Layer): head_mask: tf.Tensor | None = None, encoder_hidden_states: tf.Tensor | None = None, encoder_attention_mask: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -934,13 +934,13 @@ class TFEsmMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1117,13 +1117,13 @@ class TFEsmModel(TFEsmPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1135,7 +1135,7 @@ class TFEsmModel(TFEsmPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1226,13 +1226,13 @@ class TFEsmForMaskedLM(TFEsmPreTrainedModel, TFMaskedLanguageModelingLoss): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` - kwargs (`Dict[str, any]`, *optional*, defaults to `{}`): + kwargs (`dict[str, any]`, *optional*, defaults to `{}`): Used to hide legacy arguments that have been deprecated. """ return_dict = return_dict if return_dict is not None else self.config.use_return_dict @@ -1374,7 +1374,7 @@ class TFEsmForSequenceClassification(TFEsmPreTrainedModel, TFSequenceClassificat output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1461,7 +1461,7 @@ class TFEsmForTokenClassification(TFEsmPreTrainedModel, TFTokenClassificationLos output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. diff --git a/src/transformers/models/esm/openfold_utils/chunk_utils.py b/src/transformers/models/esm/openfold_utils/chunk_utils.py index 4cce395daf5..930543c444c 100644 --- a/src/transformers/models/esm/openfold_utils/chunk_utils.py +++ b/src/transformers/models/esm/openfold_utils/chunk_utils.py @@ -15,14 +15,14 @@ import logging import math from collections.abc import Iterable, Sequence from functools import partial -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch from .tensor_utils import tensor_tree_map, tree_map -def _fetch_dims(tree: Union[dict, list, tuple, torch.Tensor]) -> List[Tuple[int, ...]]: +def _fetch_dims(tree: Union[dict, list, tuple, torch.Tensor]) -> list[tuple[int, ...]]: shapes = [] if isinstance(tree, dict): for v in tree.values(): @@ -39,7 +39,7 @@ def _fetch_dims(tree: Union[dict, list, tuple, torch.Tensor]) -> List[Tuple[int, @torch.jit.ignore -def _flat_idx_to_idx(flat_idx: int, dims: Tuple[int, ...]) -> Tuple[int, ...]: +def _flat_idx_to_idx(flat_idx: int, dims: tuple[int, ...]) -> tuple[int, ...]: idx = [] for d in reversed(dims): idx.append(flat_idx % d) @@ -55,7 +55,7 @@ def _get_minimal_slice_set( dims: Sequence[int], start_edges: Optional[Sequence[bool]] = None, end_edges: Optional[Sequence[bool]] = None, -) -> List[Tuple[slice, ...]]: +) -> list[tuple[slice, ...]]: """ Produces an ordered sequence of tensor slices that, when used in sequence on a tensor with shape dims, yields tensors that contain every leaf in the contiguous range [start, end]. Care is taken to yield a short sequence of @@ -67,7 +67,7 @@ def _get_minimal_slice_set( # start_edges and end_edges both indicate whether, starting from any given # dimension, the start/end index is at the top/bottom edge of the # corresponding tensor, modeled as a tree - def reduce_edge_list(l: List[bool]) -> None: + def reduce_edge_list(l: list[bool]) -> None: tally = True for i in range(len(l)): reversed_idx = -1 * (i + 1) @@ -88,8 +88,8 @@ def _get_minimal_slice_set( elif len(start) == 1: return [(slice(start[0], end[0] + 1),)] - slices: List[Tuple[slice, ...]] = [] - path_list: List[slice] = [] + slices: list[tuple[slice, ...]] = [] + path_list: list[slice] = [] # Dimensions common to start and end can be selected directly for s, e in zip(start, end): @@ -98,14 +98,14 @@ def _get_minimal_slice_set( else: break - path: Tuple[slice, ...] = tuple(path_list) + path: tuple[slice, ...] = tuple(path_list) divergence_idx = len(path) # start == end, and we're done if divergence_idx == len(dims): return [path] - def upper() -> Tuple[Tuple[slice, ...], ...]: + def upper() -> tuple[tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None @@ -121,7 +121,7 @@ def _get_minimal_slice_set( ) ) - def lower() -> Tuple[Tuple[slice, ...], ...]: + def lower() -> tuple[tuple[slice, ...], ...]: assert start_edges is not None assert end_edges is not None @@ -194,7 +194,7 @@ def _chunk_slice(t: torch.Tensor, flat_start: int, flat_end: int, no_batch_dims: def chunk_layer( layer: Callable, - inputs: Dict[str, Any], + inputs: dict[str, Any], chunk_size: int, no_batch_dims: int, low_mem: bool = False, @@ -240,7 +240,7 @@ def chunk_layer( t = t.expand(orig_batch_dims + t.shape[no_batch_dims:]) return t - prepped_inputs: Dict[str, Any] = tensor_tree_map(_prep_inputs, inputs) + prepped_inputs: dict[str, Any] = tensor_tree_map(_prep_inputs, inputs) prepped_outputs = None if _out is not None: prepped_outputs = tensor_tree_map(lambda t: t.view([-1] + list(t.shape[no_batch_dims:])), _out) @@ -268,7 +268,7 @@ def chunk_layer( no_batch_dims=len(orig_batch_dims), ) - chunks: Dict[str, Any] = tensor_tree_map(select_chunk, prepped_inputs) + chunks: dict[str, Any] = tensor_tree_map(select_chunk, prepped_inputs) # Run the layer on the chunk output_chunk = layer(**chunks) @@ -329,7 +329,7 @@ class ChunkSizeTuner: if min_chunk_size >= self.max_chunk_size: return min_chunk_size - candidates: List[int] = [2**l for l in range(int(math.log(self.max_chunk_size, 2)) + 1)] + candidates: list[int] = [2**l for l in range(int(math.log(self.max_chunk_size, 2)) + 1)] candidates = [c for c in candidates if c > min_chunk_size] candidates = [min_chunk_size] + candidates candidates[-1] += 4 diff --git a/src/transformers/models/esm/openfold_utils/data_transforms.py b/src/transformers/models/esm/openfold_utils/data_transforms.py index 8d4c17589ae..fe71691323e 100644 --- a/src/transformers/models/esm/openfold_utils/data_transforms.py +++ b/src/transformers/models/esm/openfold_utils/data_transforms.py @@ -13,7 +13,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict import numpy as np import torch @@ -22,7 +21,7 @@ from . import residue_constants as rc from .tensor_utils import tensor_tree_map, tree_map -def make_atom14_masks(protein: Dict[str, torch.Tensor]) -> Dict[str, torch.Tensor]: +def make_atom14_masks(protein: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]: """Construct denser atom positions (14 dimensions instead of 37).""" restype_atom14_to_atom37_list = [] restype_atom37_to_atom14_list = [] @@ -87,7 +86,7 @@ def make_atom14_masks(protein: Dict[str, torch.Tensor]) -> Dict[str, torch.Tenso return protein -def make_atom14_masks_np(batch: Dict[str, torch.Tensor]) -> Dict[str, np.ndarray]: +def make_atom14_masks_np(batch: dict[str, torch.Tensor]) -> dict[str, np.ndarray]: batch = tree_map(lambda n: torch.tensor(n, device=batch["aatype"].device), batch, np.ndarray) out = tensor_tree_map(lambda t: np.array(t), make_atom14_masks(batch)) return out diff --git a/src/transformers/models/esm/openfold_utils/feats.py b/src/transformers/models/esm/openfold_utils/feats.py index ac7b90dfe79..366f3c47d94 100644 --- a/src/transformers/models/esm/openfold_utils/feats.py +++ b/src/transformers/models/esm/openfold_utils/feats.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Tuple, overload +from typing import overload import torch import torch.types @@ -31,7 +31,7 @@ def pseudo_beta_fn(aatype: torch.Tensor, all_atom_positions: torch.Tensor, all_a @overload def pseudo_beta_fn( aatype: torch.Tensor, all_atom_positions: torch.Tensor, all_atom_masks: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: ... +) -> tuple[torch.Tensor, torch.Tensor]: ... def pseudo_beta_fn(aatype, all_atom_positions, all_atom_masks): @@ -55,7 +55,7 @@ def pseudo_beta_fn(aatype, all_atom_positions, all_atom_masks): return pseudo_beta -def atom14_to_atom37(atom14: torch.Tensor, batch: Dict[str, torch.Tensor]) -> torch.Tensor: +def atom14_to_atom37(atom14: torch.Tensor, batch: dict[str, torch.Tensor]) -> torch.Tensor: atom37_data = batched_gather( atom14, batch["residx_atom37_to_atom14"], @@ -68,7 +68,7 @@ def atom14_to_atom37(atom14: torch.Tensor, batch: Dict[str, torch.Tensor]) -> to return atom37_data -def build_template_angle_feat(template_feats: Dict[str, torch.Tensor]) -> torch.Tensor: +def build_template_angle_feat(template_feats: dict[str, torch.Tensor]) -> torch.Tensor: template_aatype = template_feats["template_aatype"] torsion_angles_sin_cos = template_feats["template_torsion_angles_sin_cos"] alt_torsion_angles_sin_cos = template_feats["template_alt_torsion_angles_sin_cos"] @@ -87,7 +87,7 @@ def build_template_angle_feat(template_feats: Dict[str, torch.Tensor]) -> torch. def build_template_pair_feat( - batch: Dict[str, torch.Tensor], + batch: dict[str, torch.Tensor], min_bin: torch.types.Number, max_bin: torch.types.Number, no_bins: int, @@ -147,7 +147,7 @@ def build_template_pair_feat( return act -def build_extra_msa_feat(batch: Dict[str, torch.Tensor]) -> torch.Tensor: +def build_extra_msa_feat(batch: dict[str, torch.Tensor]) -> torch.Tensor: msa_1hot: torch.LongTensor = nn.functional.one_hot(batch["extra_msa"], 23) msa_feat = [ msa_1hot, diff --git a/src/transformers/models/esm/openfold_utils/loss.py b/src/transformers/models/esm/openfold_utils/loss.py index 8c442786dc8..3cd9f8c6b42 100644 --- a/src/transformers/models/esm/openfold_utils/loss.py +++ b/src/transformers/models/esm/openfold_utils/loss.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Dict, Optional, Tuple +from typing import Optional import torch @@ -28,7 +28,7 @@ def _calculate_bin_centers(boundaries: torch.Tensor) -> torch.Tensor: def _calculate_expected_aligned_error( alignment_confidence_breaks: torch.Tensor, aligned_distance_error_probs: torch.Tensor, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: bin_centers = _calculate_bin_centers(alignment_confidence_breaks) return ( torch.sum(aligned_distance_error_probs * bin_centers, dim=-1), @@ -41,7 +41,7 @@ def compute_predicted_aligned_error( max_bin: int = 31, no_bins: int = 64, **kwargs, -) -> Dict[str, torch.Tensor]: +) -> dict[str, torch.Tensor]: """Computes aligned confidence metrics from logits. Args: diff --git a/src/transformers/models/esm/openfold_utils/protein.py b/src/transformers/models/esm/openfold_utils/protein.py index 6e51642c198..a943eb7acf7 100644 --- a/src/transformers/models/esm/openfold_utils/protein.py +++ b/src/transformers/models/esm/openfold_utils/protein.py @@ -19,7 +19,7 @@ import dataclasses import re import string from collections.abc import Iterator, Mapping, Sequence -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import numpy as np @@ -71,10 +71,10 @@ class Protein: def from_proteinnet_string(proteinnet_str: str) -> Protein: tag_re = r"(\[[A-Z]+\]\n)" - tags: List[str] = [tag.strip() for tag in re.split(tag_re, proteinnet_str) if len(tag) > 0] - groups: Iterator[Tuple[str, List[str]]] = zip(tags[0::2], [l.split("\n") for l in tags[1::2]]) + tags: list[str] = [tag.strip() for tag in re.split(tag_re, proteinnet_str) if len(tag) > 0] + groups: Iterator[tuple[str, list[str]]] = zip(tags[0::2], [l.split("\n") for l in tags[1::2]]) - atoms: List[str] = ["N", "CA", "C"] + atoms: list[str] = ["N", "CA", "C"] aatype = None atom_positions = None atom_mask = None @@ -88,7 +88,7 @@ def from_proteinnet_string(proteinnet_str: str) -> Protein: [residue_constants.restype_order.get(res_symbol, residue_constants.restype_num) for res_symbol in seq] ) elif "[TERTIARY]" == g[0]: - tertiary: List[List[float]] = [] + tertiary: list[list[float]] = [] for axis in range(3): tertiary.append(list(map(float, g[1][axis].split()))) tertiary_np = np.array(tertiary) @@ -119,8 +119,8 @@ def from_proteinnet_string(proteinnet_str: str) -> Protein: ) -def get_pdb_headers(prot: Protein, chain_id: int = 0) -> List[str]: - pdb_headers: List[str] = [] +def get_pdb_headers(prot: Protein, chain_id: int = 0) -> list[str]: + pdb_headers: list[str] = [] remark = prot.remark if remark is not None: @@ -143,18 +143,18 @@ def add_pdb_headers(prot: Protein, pdb_str: str) -> str: """Add pdb headers to an existing PDB string. Useful during multi-chain recycling """ - out_pdb_lines: List[str] = [] + out_pdb_lines: list[str] = [] lines = pdb_str.split("\n") remark = prot.remark if remark is not None: out_pdb_lines.append(f"REMARK {remark}") - parents_per_chain: List[List[str]] + parents_per_chain: list[list[str]] if prot.parents is not None and len(prot.parents) > 0: parents_per_chain = [] if prot.parents_chain_index is not None: - parent_dict: Dict[str, List[str]] = {} + parent_dict: dict[str, list[str]] = {} for p, i in zip(prot.parents, prot.parents_chain_index): parent_dict.setdefault(str(i), []) parent_dict[str(i)].append(p) @@ -205,7 +205,7 @@ def to_pdb(prot: Protein) -> str: atom_types = residue_constants.atom_types - pdb_lines: List[str] = [] + pdb_lines: list[str] = [] atom_mask = prot.atom_mask aatype = prot.aatype diff --git a/src/transformers/models/esm/openfold_utils/residue_constants.py b/src/transformers/models/esm/openfold_utils/residue_constants.py index 13d5239f5c7..235e54bfe49 100644 --- a/src/transformers/models/esm/openfold_utils/residue_constants.py +++ b/src/transformers/models/esm/openfold_utils/residue_constants.py @@ -20,7 +20,6 @@ import copy import functools from collections.abc import Mapping, Sequence from importlib import resources -from typing import Dict, List, Tuple import numpy as np @@ -34,7 +33,7 @@ ca_ca = 3.80209737096 # Format: The list for each AA type contains chi1, chi2, chi3, chi4 in # this order (or a relevant subset from chi1 onwards). ALA and GLY don't have # chi angles so their chi angle lists are empty. -chi_angles_atoms: Dict[str, List[List[str]]] = { +chi_angles_atoms: dict[str, list[list[str]]] = { "ALA": [], # Chi5 in arginine is always 0 +- 5 degrees, so ignore it. "ARG": [["N", "CA", "CB", "CG"], ["CA", "CB", "CG", "CD"], ["CB", "CG", "CD", "NE"], ["CG", "CD", "NE", "CZ"]], @@ -60,7 +59,7 @@ chi_angles_atoms: Dict[str, List[List[str]]] = { # If chi angles given in fixed-length array, this matrix determines how to mask # them for each AA type. The order is as per restype_order (see below). -chi_angles_mask: List[List[float]] = [ +chi_angles_mask: list[list[float]] = [ [0.0, 0.0, 0.0, 0.0], # ALA [1.0, 1.0, 1.0, 1.0], # ARG [1.0, 1.0, 0.0, 0.0], # ASN @@ -85,7 +84,7 @@ chi_angles_mask: List[List[float]] = [ # The following chi angles are pi periodic: they can be rotated by a multiple # of pi without affecting the structure. -chi_pi_periodic: List[List[float]] = [ +chi_pi_periodic: list[list[float]] = [ [0.0, 0.0, 0.0, 0.0], # ALA [0.0, 0.0, 0.0, 0.0], # ARG [0.0, 0.0, 0.0, 0.0], # ASN @@ -121,7 +120,7 @@ chi_pi_periodic: List[List[float]] = [ # is defined such that the dihedral-angle-definiting atom (the last entry in # chi_angles_atoms above) is in the xy-plane (with a positive y-coordinate). # format: [atomname, group_idx, rel_position] -rigid_group_atom_positions: Dict[str, List[Tuple[str, int, Tuple[float, float, float]]]] = { +rigid_group_atom_positions: dict[str, list[tuple[str, int, tuple[float, float, float]]]] = { "ALA": [ ("N", 0, (-0.525, 1.363, 0.000)), ("CA", 0, (0.000, 0.000, 0.000)), @@ -333,7 +332,7 @@ rigid_group_atom_positions: Dict[str, List[Tuple[str, int, Tuple[float, float, f } # A list of atoms (excluding hydrogen) for each AA type. PDB naming convention. -residue_atoms: Dict[str, List[str]] = { +residue_atoms: dict[str, list[str]] = { "ALA": ["C", "CA", "CB", "N", "O"], "ARG": ["C", "CA", "CB", "CG", "CD", "CZ", "N", "NE", "O", "NH1", "NH2"], "ASP": ["C", "CA", "CB", "CG", "N", "O", "OD1", "OD2"], @@ -363,7 +362,7 @@ residue_atoms: Dict[str, List[str]] = { # in LEU, VAL and ARG can be resolved by using the 3d constellations of # the 'ambiguous' atoms and their neighbours) # TODO: ^ interpret this -residue_atom_renaming_swaps: Dict[str, Dict[str, str]] = { +residue_atom_renaming_swaps: dict[str, dict[str, str]] = { "ASP": {"OD1": "OD2"}, "GLU": {"OE1": "OE2"}, "PHE": {"CD1": "CD2", "CE1": "CE2"}, @@ -371,7 +370,7 @@ residue_atom_renaming_swaps: Dict[str, Dict[str, str]] = { } # Van der Waals radii [Angstroem] of the atoms (from Wikipedia) -van_der_waals_radius: Dict[str, float] = { +van_der_waals_radius: dict[str, float] = { "C": 1.7, "N": 1.55, "O": 1.52, @@ -400,10 +399,10 @@ def map_structure_with_atom_order(in_list: list, first_call: bool = True) -> lis @functools.lru_cache(maxsize=None) -def load_stereo_chemical_props() -> Tuple[ - Mapping[str, List[Bond]], - Mapping[str, List[Bond]], - Mapping[str, List[BondAngle]], +def load_stereo_chemical_props() -> tuple[ + Mapping[str, list[Bond]], + Mapping[str, list[Bond]], + Mapping[str, list[BondAngle]], ]: """Load stereo_chemical_props.txt into a nice structure. @@ -419,7 +418,7 @@ def load_stereo_chemical_props() -> Tuple[ lines_iter = iter(stereo_chemical_props.splitlines()) # Load bond lengths. - residue_bonds: Dict[str, List[Bond]] = {} + residue_bonds: dict[str, list[Bond]] = {} next(lines_iter) # Skip header line. for line in lines_iter: if line.strip() == "-": @@ -432,7 +431,7 @@ def load_stereo_chemical_props() -> Tuple[ residue_bonds["UNK"] = [] # Load bond angles. - residue_bond_angles: Dict[str, List[BondAngle]] = {} + residue_bond_angles: dict[str, list[BondAngle]] = {} next(lines_iter) # Skip empty line. next(lines_iter) # Skip header line. for line in lines_iter: @@ -458,10 +457,10 @@ def load_stereo_chemical_props() -> Tuple[ return "-".join(sorted([atom1_name, atom2_name])) # Translate bond angles into distances ("virtual bonds"). - residue_virtual_bonds: Dict[str, List[Bond]] = {} + residue_virtual_bonds: dict[str, list[Bond]] = {} for resname, bond_angles in residue_bond_angles.items(): # Create a fast lookup dict for bond lengths. - bond_cache: Dict[str, Bond] = {} + bond_cache: dict[str, Bond] = {} for b in residue_bonds[resname]: bond_cache[make_bond_key(b.atom1_name, b.atom2_name)] = b residue_virtual_bonds[resname] = [] @@ -489,16 +488,16 @@ def load_stereo_chemical_props() -> Tuple[ # Between-residue bond lengths for general bonds (first element) and for Proline # (second element). -between_res_bond_length_c_n: Tuple[float, float] = (1.329, 1.341) -between_res_bond_length_stddev_c_n: Tuple[float, float] = (0.014, 0.016) +between_res_bond_length_c_n: tuple[float, float] = (1.329, 1.341) +between_res_bond_length_stddev_c_n: tuple[float, float] = (0.014, 0.016) # Between-residue cos_angles. -between_res_cos_angles_c_n_ca: Tuple[float, float] = (-0.5203, 0.0353) # degrees: 121.352 +- 2.315 -between_res_cos_angles_ca_c_n: Tuple[float, float] = (-0.4473, 0.0311) # degrees: 116.568 +- 1.995 +between_res_cos_angles_c_n_ca: tuple[float, float] = (-0.5203, 0.0353) # degrees: 121.352 +- 2.315 +between_res_cos_angles_ca_c_n: tuple[float, float] = (-0.4473, 0.0311) # degrees: 116.568 +- 1.995 # This mapping is used when we need to store atom data in a format that requires # fixed atom data size for every residue (e.g. a numpy array). -atom_types: List[str] = [ +atom_types: list[str] = [ "N", "CA", "C", @@ -537,13 +536,13 @@ atom_types: List[str] = [ "NZ", "OXT", ] -atom_order: Dict[str, int] = {atom_type: i for i, atom_type in enumerate(atom_types)} +atom_order: dict[str, int] = {atom_type: i for i, atom_type in enumerate(atom_types)} atom_type_num = len(atom_types) # := 37. # A compact atom encoding with 14 columns # pylint: disable=line-too-long # pylint: disable=bad-whitespace -restype_name_to_atom14_names: Dict[str, List[str]] = { +restype_name_to_atom14_names: dict[str, list[str]] = { "ALA": ["N", "CA", "C", "O", "CB", "", "", "", "", "", "", "", "", ""], "ARG": ["N", "CA", "C", "O", "CB", "CG", "CD", "NE", "CZ", "NH1", "NH2", "", "", ""], "ASN": ["N", "CA", "C", "O", "CB", "CG", "OD1", "ND2", "", "", "", "", "", ""], @@ -572,7 +571,7 @@ restype_name_to_atom14_names: Dict[str, List[str]] = { # This is the standard residue order when coding AA type as a number. # Reproduce it by taking 3-letter AA codes and sorting them alphabetically. -restypes: List[str] = [ +restypes: list[str] = [ "A", "R", "N", @@ -594,12 +593,12 @@ restypes: List[str] = [ "Y", "V", ] -restype_order: Dict[str, int] = {restype: i for i, restype in enumerate(restypes)} +restype_order: dict[str, int] = {restype: i for i, restype in enumerate(restypes)} restype_num = len(restypes) # := 20. unk_restype_index = restype_num # Catch-all index for unknown restypes. -restypes_with_x: List[str] = restypes + ["X"] -restype_order_with_x: Dict[str, int] = {restype: i for i, restype in enumerate(restypes_with_x)} +restypes_with_x: list[str] = restypes + ["X"] +restype_order_with_x: dict[str, int] = {restype: i for i, restype in enumerate(restypes_with_x)} def sequence_to_onehot(sequence: str, mapping: Mapping[str, int], map_unknown_to_x: bool = False) -> np.ndarray: @@ -642,7 +641,7 @@ def sequence_to_onehot(sequence: str, mapping: Mapping[str, int], map_unknown_to return one_hot_arr -restype_1to3: Dict[str, str] = { +restype_1to3: dict[str, str] = { "A": "ALA", "R": "ARG", "N": "ASN", @@ -670,13 +669,13 @@ restype_1to3: Dict[str, str] = { # 1-to-1 mapping of 3 letter names to one letter names. The latter contains # many more, and less common, three letter names as keys and maps many of these # to the same one letter name (including 'X' and 'U' which we don't use here). -restype_3to1: Dict[str, str] = {v: k for k, v in restype_1to3.items()} +restype_3to1: dict[str, str] = {v: k for k, v in restype_1to3.items()} # Define a restype name for all unknown residues. unk_restype = "UNK" -resnames: List[str] = [restype_1to3[r] for r in restypes] + [unk_restype] -resname_to_idx: Dict[str, int] = {resname: i for i, resname in enumerate(resnames)} +resnames: list[str] = [restype_1to3[r] for r in restypes] + [unk_restype] +resname_to_idx: dict[str, int] = {resname: i for i, resname in enumerate(resnames)} # The mapping here uses hhblits convention, so that B is mapped to D, J and O @@ -686,7 +685,7 @@ resname_to_idx: Dict[str, int] = {resname: i for i, resname in enumerate(resname # "-" representing a missing amino acid in an alignment. The id for these # codes is put at the end (20 and 21) so that they can easily be ignored if # desired. -HHBLITS_AA_TO_ID: Dict[str, int] = { +HHBLITS_AA_TO_ID: dict[str, int] = { "A": 0, "B": 2, "C": 1, @@ -717,7 +716,7 @@ HHBLITS_AA_TO_ID: Dict[str, int] = { } # Partial inversion of HHBLITS_AA_TO_ID. -ID_TO_HHBLITS_AA: Dict[int, str] = { +ID_TO_HHBLITS_AA: dict[int, str] = { 0: "A", 1: "C", # Also U. 2: "D", # Also B. @@ -742,8 +741,8 @@ ID_TO_HHBLITS_AA: Dict[int, str] = { 21: "-", } -restypes_with_x_and_gap: List[str] = restypes + ["X", "-"] -MAP_HHBLITS_AATYPE_TO_OUR_AATYPE: Tuple[int, ...] = tuple( +restypes_with_x_and_gap: list[str] = restypes + ["X", "-"] +MAP_HHBLITS_AATYPE_TO_OUR_AATYPE: tuple[int, ...] = tuple( restypes_with_x_and_gap.index(ID_TO_HHBLITS_AA[i]) for i in range(len(restypes_with_x_and_gap)) ) @@ -792,7 +791,7 @@ chi_atom_1_one_hot = chi_angle_atom(1) chi_atom_2_one_hot = chi_angle_atom(2) # An array like chi_angles_atoms but using indices rather than names. -chi_angles_atom_indices_list: List[List[List[str]]] = [chi_angles_atoms[restype_1to3[r]] for r in restypes] +chi_angles_atom_indices_list: list[list[list[str]]] = [chi_angles_atoms[restype_1to3[r]] for r in restypes] chi_angles_atom_indices_ours: list = map_structure_with_atom_order(chi_angles_atom_indices_list) chi_angles_atom_indices = np.array( [chi_atoms + ([[0, 0, 0, 0]] * (4 - len(chi_atoms))) for chi_atoms in chi_angles_atom_indices_list] @@ -800,7 +799,7 @@ chi_angles_atom_indices = np.array( # Mapping from (res_name, atom_name) pairs to the atom's chi group index # and atom index within that group. -chi_groups_for_atom: Dict[Tuple[str, str], List[Tuple[int, int]]] = collections.defaultdict(list) +chi_groups_for_atom: dict[tuple[str, str], list[tuple[int, int]]] = collections.defaultdict(list) for res_name, chi_angle_atoms_for_res in chi_angles_atoms.items(): for chi_group_i, chi_group in enumerate(chi_angle_atoms_for_res): for atom_i, atom in enumerate(chi_group): @@ -854,7 +853,7 @@ def _make_rigid_group_constants() -> None: for restype, restype_letter in enumerate(restypes): resname = restype_1to3[restype_letter] - atom_positions: Dict[str, np.ndarray] = { + atom_positions: dict[str, np.ndarray] = { name: np.array(pos) for name, _, pos in rigid_group_atom_positions[resname] } @@ -914,7 +913,7 @@ _make_rigid_group_constants() def make_atom14_dists_bounds( overlap_tolerance: float = 1.5, bond_length_tolerance_factor: int = 15, -) -> Dict[str, np.ndarray]: +) -> dict[str, np.ndarray]: """compute upper and lower bounds for bonds to assess violations.""" restype_atom14_bond_lower_bound = np.zeros([21, 14, 14], np.float32) restype_atom14_bond_upper_bound = np.zeros([21, 14, 14], np.float32) diff --git a/src/transformers/models/esm/openfold_utils/rigid_utils.py b/src/transformers/models/esm/openfold_utils/rigid_utils.py index 5e5f2c50e88..53bef9a4437 100644 --- a/src/transformers/models/esm/openfold_utils/rigid_utils.py +++ b/src/transformers/models/esm/openfold_utils/rigid_utils.py @@ -17,7 +17,7 @@ from __future__ import annotations from collections.abc import Sequence from functools import lru_cache -from typing import Any, Callable, Dict, List, Optional, Tuple +from typing import Any, Callable, Optional import numpy as np import torch @@ -77,7 +77,7 @@ def rot_vec_mul(r: torch.Tensor, t: torch.Tensor) -> torch.Tensor: @lru_cache(maxsize=None) def identity_rot_mats( - batch_dims: Tuple[int, ...], + batch_dims: tuple[int, ...], dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, requires_grad: bool = True, @@ -92,7 +92,7 @@ def identity_rot_mats( @lru_cache(maxsize=None) def identity_trans( - batch_dims: Tuple[int, ...], + batch_dims: tuple[int, ...], dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, requires_grad: bool = True, @@ -103,7 +103,7 @@ def identity_trans( @lru_cache(maxsize=None) def identity_quats( - batch_dims: Tuple[int, ...], + batch_dims: tuple[int, ...], dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, requires_grad: bool = True, @@ -116,12 +116,12 @@ def identity_quats( return quat -_quat_elements: List[str] = ["a", "b", "c", "d"] -_qtr_keys: List[str] = [l1 + l2 for l1 in _quat_elements for l2 in _quat_elements] -_qtr_ind_dict: Dict[str, int] = {key: ind for ind, key in enumerate(_qtr_keys)} +_quat_elements: list[str] = ["a", "b", "c", "d"] +_qtr_keys: list[str] = [l1 + l2 for l1 in _quat_elements for l2 in _quat_elements] +_qtr_ind_dict: dict[str, int] = {key: ind for ind, key in enumerate(_qtr_keys)} -def _to_mat(pairs: List[Tuple[str, int]]) -> np.ndarray: +def _to_mat(pairs: list[tuple[str, int]]) -> np.ndarray: mat = np.zeros((4, 4)) for key, value in pairs: ind = _qtr_ind_dict[key] @@ -213,7 +213,7 @@ _QUAT_MULTIPLY[:, :, 3] = [[0, 0, 0, 1], [0, 0, 1, 0], [0, -1, 0, 0], [1, 0, 0, _QUAT_MULTIPLY_BY_VEC = _QUAT_MULTIPLY[:, 1:, :] -_CACHED_QUATS: Dict[str, np.ndarray] = { +_CACHED_QUATS: dict[str, np.ndarray] = { "_QTR_MAT": _QTR_MAT, "_QUAT_MULTIPLY": _QUAT_MULTIPLY, "_QUAT_MULTIPLY_BY_VEC": _QUAT_MULTIPLY_BY_VEC, @@ -785,7 +785,7 @@ class Rigid: @staticmethod def identity( - shape: Tuple[int, ...], + shape: tuple[int, ...], dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, requires_grad: bool = True, diff --git a/src/transformers/models/esm/openfold_utils/tensor_utils.py b/src/transformers/models/esm/openfold_utils/tensor_utils.py index efe72e4905b..449c810aed3 100644 --- a/src/transformers/models/esm/openfold_utils/tensor_utils.py +++ b/src/transformers/models/esm/openfold_utils/tensor_utils.py @@ -14,7 +14,7 @@ # limitations under the License. from functools import partial -from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload +from typing import Any, Callable, TypeVar, Union, overload import torch import torch.nn as nn @@ -32,7 +32,7 @@ def add(m1: torch.Tensor, m2: torch.Tensor, inplace: bool) -> torch.Tensor: return m1 -def permute_final_dims(tensor: torch.Tensor, inds: List[int]) -> torch.Tensor: +def permute_final_dims(tensor: torch.Tensor, inds: list[int]) -> torch.Tensor: zero_index = -1 * len(inds) first_inds = list(range(len(tensor.shape[:zero_index]))) return tensor.permute(first_inds + [zero_index + i for i in inds]) @@ -55,7 +55,7 @@ def pts_to_distogram( return torch.bucketize(dists, boundaries) -def dict_multimap(fn: Callable[[list], Any], dicts: List[dict]) -> dict: +def dict_multimap(fn: Callable[[list], Any], dicts: list[dict]) -> dict: first = dicts[0] new_dict = {} for k, v in first.items(): @@ -76,13 +76,13 @@ def one_hot(x: torch.Tensor, v_bins: torch.Tensor) -> torch.Tensor: def batched_gather(data: torch.Tensor, inds: torch.Tensor, dim: int = 0, no_batch_dims: int = 0) -> torch.Tensor: - ranges: List[Union[slice, torch.Tensor]] = [] + ranges: list[Union[slice, torch.Tensor]] = [] for i, s in enumerate(data.shape[:no_batch_dims]): r = torch.arange(s) r = r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape) - i - 1)))) ranges.append(r) - remaining_dims: List[Union[slice, torch.Tensor]] = [slice(None) for _ in range(len(data.shape) - no_batch_dims)] + remaining_dims: list[Union[slice, torch.Tensor]] = [slice(None) for _ in range(len(data.shape) - no_batch_dims)] remaining_dims[dim - no_batch_dims if dim >= 0 else dim] = inds ranges.extend(remaining_dims) # Matt note: Editing this to get around the behaviour of using a list as an array index changing @@ -95,9 +95,9 @@ T = TypeVar("T") # With tree_map, a poor man's JAX tree_map def dict_map( - fn: Callable[[T], Any], dic: Dict[Any, Union[dict, list, tuple, T]], leaf_type: Type[T] -) -> Dict[Any, Union[dict, list, tuple, Any]]: - new_dict: Dict[Any, Union[dict, list, tuple, Any]] = {} + fn: Callable[[T], Any], dic: dict[Any, Union[dict, list, tuple, T]], leaf_type: type[T] +) -> dict[Any, Union[dict, list, tuple, Any]]: + new_dict: dict[Any, Union[dict, list, tuple, Any]] = {} for k, v in dic.items(): if isinstance(v, dict): new_dict[k] = dict_map(fn, v, leaf_type) @@ -108,19 +108,19 @@ def dict_map( @overload -def tree_map(fn: Callable[[T], Any], tree: T, leaf_type: Type[T]) -> Any: ... +def tree_map(fn: Callable[[T], Any], tree: T, leaf_type: type[T]) -> Any: ... @overload -def tree_map(fn: Callable[[T], Any], tree: dict, leaf_type: Type[T]) -> dict: ... +def tree_map(fn: Callable[[T], Any], tree: dict, leaf_type: type[T]) -> dict: ... @overload -def tree_map(fn: Callable[[T], Any], tree: list, leaf_type: Type[T]) -> list: ... +def tree_map(fn: Callable[[T], Any], tree: list, leaf_type: type[T]) -> list: ... @overload -def tree_map(fn: Callable[[T], Any], tree: tuple, leaf_type: Type[T]) -> tuple: ... +def tree_map(fn: Callable[[T], Any], tree: tuple, leaf_type: type[T]) -> tuple: ... def tree_map(fn, tree, leaf_type): diff --git a/src/transformers/models/esm/tokenization_esm.py b/src/transformers/models/esm/tokenization_esm.py index 4bc433e350e..7d9705f7dbd 100644 --- a/src/transformers/models/esm/tokenization_esm.py +++ b/src/transformers/models/esm/tokenization_esm.py @@ -15,7 +15,7 @@ """Tokenization classes for ESM.""" import os -from typing import List, Optional +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -89,8 +89,8 @@ class EsmTokenizer(PreTrainedTokenizer): return self._id_to_token.get(index, self.unk_token) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: cls = [self.cls_token_id] sep = [self.eos_token_id] # No sep token in ESM vocabulary if token_ids_1 is None: @@ -103,16 +103,16 @@ class EsmTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep # Multiple inputs always have an EOS token def get_special_tokens_mask( - self, token_ids_0: List, token_ids_1: Optional[List] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list, token_ids_1: Optional[list] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids of the first sequence. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): List of ids of the second sequence. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. diff --git a/src/transformers/models/falcon/configuration_falcon.py b/src/transformers/models/falcon/configuration_falcon.py index 2d072b14f79..d3155c8eb9c 100644 --- a/src/transformers/models/falcon/configuration_falcon.py +++ b/src/transformers/models/falcon/configuration_falcon.py @@ -101,11 +101,11 @@ class FalconConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/falcon/modeling_falcon.py b/src/transformers/models/falcon/modeling_falcon.py index df87d36242e..455afbb2157 100644 --- a/src/transformers/models/falcon/modeling_falcon.py +++ b/src/transformers/models/falcon/modeling_falcon.py @@ -15,7 +15,7 @@ """PyTorch Falcon model.""" import math -from typing import TYPE_CHECKING, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import torch import torch.utils.checkpoint @@ -232,7 +232,7 @@ class FalconAttention(nn.Module): if config.rotary: self.rotary_emb = FalconRotaryEmbedding(config=self.config) - def _split_heads(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + def _split_heads(self, fused_qkv: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Split the last dimension into (num_heads, head_dim), results share same memory storage as `fused_qkv` @@ -300,7 +300,7 @@ class FalconAttention(nn.Module): use_cache: bool = False, output_attentions: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC ): fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size] num_kv_heads = self.num_heads if self.new_decoder_architecture else self.num_kv_heads @@ -450,7 +450,7 @@ class FalconFlashAttention2(FalconAttention): use_cache: bool = False, output_attentions: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC ): fused_qkv = self.query_key_value(hidden_states) # [batch_size, seq_length, 3 x hidden_size] num_kv_heads = self.num_heads if self.new_decoder_architecture else self.num_kv_heads @@ -583,12 +583,12 @@ class FalconDecoderLayer(nn.Module): alibi: Optional[torch.Tensor], attention_mask: torch.Tensor, position_ids: Optional[torch.LongTensor] = None, - layer_past: Optional[Union[Cache, Tuple[torch.Tensor, torch.Tensor]]] = None, + layer_past: Optional[Union[Cache, tuple[torch.Tensor, torch.Tensor]]] = None, head_mask: Optional[torch.Tensor] = None, use_cache: bool = False, output_attentions: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, ): residual = hidden_states @@ -729,7 +729,7 @@ class FalconModel(FalconPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.LongTensor] = None, @@ -739,7 +739,7 @@ class FalconModel(FalconPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1058,7 +1058,7 @@ class FalconForCausalLM(FalconPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor, torch.Tensor], ...]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor, torch.Tensor], ...]]] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, @@ -1071,7 +1071,7 @@ class FalconForCausalLM(FalconPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1132,8 +1132,8 @@ class FalconForCausalLM(FalconPreTrainedModel, GenerationMixin): ) def _reorder_cache( - self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor - ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]: + self, past: tuple[tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor + ) -> tuple[tuple[torch.Tensor, torch.Tensor], ...]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1184,7 +1184,7 @@ class FalconForSequenceClassification(FalconPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1193,7 +1193,7 @@ class FalconForSequenceClassification(FalconPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1310,7 +1310,7 @@ class FalconForTokenClassification(FalconPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, @@ -1319,7 +1319,7 @@ class FalconForTokenClassification(FalconPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -1398,7 +1398,7 @@ class FalconForQuestionAnswering(FalconPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` diff --git a/src/transformers/models/falcon_h1/configuration_falcon_h1.py b/src/transformers/models/falcon_h1/configuration_falcon_h1.py index 4b10b2e3f28..9c41264789c 100644 --- a/src/transformers/models/falcon_h1/configuration_falcon_h1.py +++ b/src/transformers/models/falcon_h1/configuration_falcon_h1.py @@ -111,7 +111,7 @@ class FalconH1Config(PretrainedConfig): The multiplier for the LM head. This is used to scale the output of the LM head. embedding_multiplier (`float`, *optional*, defaults to 1.0): The multiplier for the embedding layer. This is used to scale the output of the embedding layer. - mlp_multipliers (`List[float]`, *optional*): + mlp_multipliers (`list[float]`, *optional*): The multipliers for the MLP layers. This is used to scale the output of the MLP layers. The first value is the multiplier of gate layer, the second value is the multiplier of the down_proj layer. key_multiplier (`float`, *optional*): @@ -120,7 +120,7 @@ class FalconH1Config(PretrainedConfig): The multiplier for the attention output layer. This is used to scale the output of the attention output attention_in_multiplier (`float`, *optional*): The multiplier for the attention input layer. This is used to scale the output of the attention input layer. - ssm_multipliers (`List[float]`, *optional*): + ssm_multipliers (`list[float]`, *optional*): The multipliers for the SSM layers. This is used to scale the output of the SSM layers. ssm_in_multiplier (`float`, *optional*): The multiplier for the SSM input layer. This is used to scale the output of the SSM input layer. diff --git a/src/transformers/models/falcon_h1/modeling_falcon_h1.py b/src/transformers/models/falcon_h1/modeling_falcon_h1.py index 0a4d8f43277..662d6254c84 100644 --- a/src/transformers/models/falcon_h1/modeling_falcon_h1.py +++ b/src/transformers/models/falcon_h1/modeling_falcon_h1.py @@ -24,7 +24,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn.functional as F @@ -84,7 +84,7 @@ class FalconHybridMambaAttentionDynamicCache(DynamicCache): config: FalconH1Config, batch_size: int, dtype: torch.dtype = torch.float16, - devices: Optional[List[str]] = None, + devices: Optional[list[str]] = None, ): self.seqlen_offset = 0 self.dtype = dtype @@ -123,16 +123,16 @@ class FalconHybridMambaAttentionDynamicCache(DynamicCache): for i in range(config.num_hidden_layers): self.transformer_layers.append(i) - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] def update( self, key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. @@ -143,7 +143,7 @@ class FalconHybridMambaAttentionDynamicCache(DynamicCache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. No additional arguments are used in `DynamicCache`. Return: @@ -191,11 +191,11 @@ class FalconHybridMambaAttentionDynamicCache(DynamicCache): return 0 return self.key_cache[layer_idx].shape[-2] - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor], tuple[torch.Tensor]]: raise NotImplementedError("FalconHybridMambaAttentionDynamicCache does not have a legacy cache equivalent.") @classmethod - def from_legacy_cache(cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None) -> "DynamicCache": + def from_legacy_cache(cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None) -> "DynamicCache": raise NotImplementedError("FalconHybridMambaAttentionDynamicCache does not have a legacy cache equivalent.") def update_conv_state( @@ -357,12 +357,12 @@ class FalconH1Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -1084,9 +1084,9 @@ class FalconH1DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1101,7 +1101,7 @@ class FalconH1DecoderLayer(GradientCheckpointingLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -1268,7 +1268,7 @@ class FalconH1Model(FalconH1PreTrainedModel): output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1528,7 +1528,7 @@ class FalconH1ForCausalLM(FalconH1PreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/falcon_h1/modular_falcon_h1.py b/src/transformers/models/falcon_h1/modular_falcon_h1.py index bd0ecb1804d..85cd4db9116 100644 --- a/src/transformers/models/falcon_h1/modular_falcon_h1.py +++ b/src/transformers/models/falcon_h1/modular_falcon_h1.py @@ -19,7 +19,7 @@ # limitations under the License. """PyTorch FalconH1 model.""" -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn.functional as F @@ -92,7 +92,7 @@ class FalconHybridMambaAttentionDynamicCache(HybridMambaAttentionDynamicCache): config: FalconH1Config, batch_size: int, dtype: torch.dtype = torch.float16, - devices: Optional[List[str]] = None, + devices: Optional[list[str]] = None, ): self.seqlen_offset = 0 self.dtype = dtype @@ -131,16 +131,16 @@ class FalconHybridMambaAttentionDynamicCache(HybridMambaAttentionDynamicCache): for i in range(config.num_hidden_layers): self.transformer_layers.append(i) - self.key_cache: List[torch.Tensor] = [] - self.value_cache: List[torch.Tensor] = [] + self.key_cache: list[torch.Tensor] = [] + self.value_cache: list[torch.Tensor] = [] def update( self, key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: """ Updates the cache with the new `key_states` and `value_states` for the layer `layer_idx`. @@ -151,7 +151,7 @@ class FalconHybridMambaAttentionDynamicCache(HybridMambaAttentionDynamicCache): The new value states to cache. layer_idx (`int`): The index of the layer to cache the states for. - cache_kwargs (`Dict[str, Any]`, `optional`): + cache_kwargs (`dict[str, Any]`, `optional`): Additional arguments for the cache subclass. No additional arguments are used in `DynamicCache`. Return: @@ -213,12 +213,12 @@ class FalconH1Attention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -859,9 +859,9 @@ class FalconH1DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -876,7 +876,7 @@ class FalconH1DecoderLayer(GradientCheckpointingLayer): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -1043,7 +1043,7 @@ class FalconH1Model(FalconH1PreTrainedModel): output_hidden_states: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1269,7 +1269,7 @@ class FalconH1ForCausalLM(LlamaForCausalLM): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" Example: diff --git a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py index 7b588d8fa44..257f2c50cd9 100644 --- a/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py +++ b/src/transformers/models/falcon_mamba/modeling_falcon_mamba.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -513,7 +513,7 @@ class FalconMambaOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None cache_params: Optional[MambaCache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -542,7 +542,7 @@ class FalconMambaCausalLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None cache_params: Optional[MambaCache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -577,7 +577,7 @@ class FalconMambaModel(FalconMambaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, FalconMambaOutput]: + ) -> Union[tuple, FalconMambaOutput]: r""" cache_params (`MambaCache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the @@ -680,8 +680,8 @@ class FalconMambaForCausalLM(FalconMambaPreTrainedModel, GenerationMixin): return self.backbone.set_input_embeddings(new_embeddings) def _update_model_kwargs_for_generation( - self, outputs: ModelOutput, model_kwargs: Dict[str, Any], num_new_tokens: int = 1, **kwargs - ) -> Dict[str, Any]: + self, outputs: ModelOutput, model_kwargs: dict[str, Any], num_new_tokens: int = 1, **kwargs + ) -> dict[str, Any]: model_kwargs["cache_params"] = outputs.get("cache_params", None) if ( model_kwargs.get("use_cache", True) @@ -759,7 +759,7 @@ class FalconMambaForCausalLM(FalconMambaPreTrainedModel, GenerationMixin): use_cache: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **kwargs, # for now we need this for generation - ) -> Union[Tuple, FalconMambaCausalLMOutput]: + ) -> Union[tuple, FalconMambaCausalLMOutput]: r""" cache_params (`MambaCache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the diff --git a/src/transformers/models/fastspeech2_conformer/configuration_fastspeech2_conformer.py b/src/transformers/models/fastspeech2_conformer/configuration_fastspeech2_conformer.py index 5bb3f150f6b..89d65a261c6 100644 --- a/src/transformers/models/fastspeech2_conformer/configuration_fastspeech2_conformer.py +++ b/src/transformers/models/fastspeech2_conformer/configuration_fastspeech2_conformer.py @@ -14,7 +14,7 @@ # limitations under the License. """FastSpeech2Conformer model configuration""" -from typing import Dict, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -341,18 +341,18 @@ class FastSpeech2ConformerHifiGanConfig(PretrainedConfig): The number of frequency bins in the input log-mel spectrogram. upsample_initial_channel (`int`, *optional*, defaults to 512): The number of input channels into the upsampling network. - upsample_rates (`Tuple[int]` or `List[int]`, *optional*, defaults to `[8, 8, 2, 2]`): + upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 2, 2]`): A tuple of integers defining the stride of each 1D convolutional layer in the upsampling network. The length of *upsample_rates* defines the number of convolutional layers and has to match the length of *upsample_kernel_sizes*. - upsample_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[16, 16, 4, 4]`): + upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[16, 16, 4, 4]`): A tuple of integers defining the kernel size of each 1D convolutional layer in the upsampling network. The length of *upsample_kernel_sizes* defines the number of convolutional layers and has to match the length of *upsample_rates*. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`): A tuple of integers defining the kernel sizes of the 1D convolutional layers in the multi-receptive field fusion (MRF) module. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the multi-receptive field fusion (MRF) module. initializer_range (`float`, *optional*, defaults to 0.01): @@ -459,8 +459,8 @@ class FastSpeech2ConformerWithHifiGanConfig(PretrainedConfig): def __init__( self, - model_config: Optional[Dict] = None, - vocoder_config: Optional[Dict] = None, + model_config: Optional[dict] = None, + vocoder_config: Optional[dict] = None, **kwargs, ): if model_config is None: diff --git a/src/transformers/models/fastspeech2_conformer/modeling_fastspeech2_conformer.py b/src/transformers/models/fastspeech2_conformer/modeling_fastspeech2_conformer.py index 473e09ab209..60757a113a5 100644 --- a/src/transformers/models/fastspeech2_conformer/modeling_fastspeech2_conformer.py +++ b/src/transformers/models/fastspeech2_conformer/modeling_fastspeech2_conformer.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -80,10 +80,10 @@ class FastSpeech2ConformerModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None spectrogram: Optional[torch.FloatTensor] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None duration_outputs: Optional[torch.LongTensor] = None pitch_outputs: Optional[torch.FloatTensor] = None energy_outputs: Optional[torch.FloatTensor] = None @@ -455,7 +455,7 @@ class FastSpeech2ConformerAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, pos_emb: Optional[torch.Tensor] = None, output_attentions: Optional[torch.Tensor] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Compute 'Scaled Dot Product Attention' with rel. positional encoding. @@ -1133,7 +1133,7 @@ class FastSpeech2ConformerModel(FastSpeech2ConformerPreTrainedModel): return_dict: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, FastSpeech2ConformerModelOutput]: + ) -> Union[tuple, FastSpeech2ConformerModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Input sequence of text vectors. @@ -1529,7 +1529,7 @@ class FastSpeech2ConformerWithHifiGan(PreTrainedModel): return_dict: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, FastSpeech2ConformerModelOutput]: + ) -> Union[tuple, FastSpeech2ConformerModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Input sequence of text vectors. diff --git a/src/transformers/models/fastspeech2_conformer/tokenization_fastspeech2_conformer.py b/src/transformers/models/fastspeech2_conformer/tokenization_fastspeech2_conformer.py index faa1420d71c..004a1c36f59 100644 --- a/src/transformers/models/fastspeech2_conformer/tokenization_fastspeech2_conformer.py +++ b/src/transformers/models/fastspeech2_conformer/tokenization_fastspeech2_conformer.py @@ -16,7 +16,7 @@ import json import os -from typing import Optional, Tuple +from typing import Optional import regex @@ -143,7 +143,7 @@ class FastSpeech2ConformerTokenizer(PreTrainedTokenizer): ) return tokens - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: """ Save the vocabulary and special tokens file to a directory. diff --git a/src/transformers/models/flaubert/modeling_flaubert.py b/src/transformers/models/flaubert/modeling_flaubert.py index 8eb2fc4b1c2..e97f91a1113 100644 --- a/src/transformers/models/flaubert/modeling_flaubert.py +++ b/src/transformers/models/flaubert/modeling_flaubert.py @@ -17,7 +17,7 @@ import itertools import math from dataclasses import dataclass -from typing import Callable, Dict, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -482,7 +482,7 @@ class FlaubertSQuADHead(nn.Module): is_impossible: Optional[torch.LongTensor] = None, p_mask: Optional[torch.FloatTensor] = None, return_dict: bool = False, - ) -> Union[FlaubertSquadHeadOutput, Tuple[torch.FloatTensor]]: + ) -> Union[FlaubertSquadHeadOutput, tuple[torch.FloatTensor]]: r""" hidden_states (`torch.FloatTensor` of shape `(batch_size, seq_len, hidden_size)`): Final hidden states of the model on the sequence tokens. @@ -807,19 +807,19 @@ class FlaubertModel(FlaubertPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, lengths: Optional[torch.LongTensor] = None, - cache: Optional[Dict[str, torch.FloatTensor]] = None, + cache: Optional[dict[str, torch.FloatTensor]] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" lengths (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use `attention_mask` for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`: - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary strings to `torch.FloatTensor` that contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. The dictionary object will be modified in-place during the forward pass to add newly computed @@ -1031,20 +1031,20 @@ class FlaubertWithLMHeadModel(FlaubertPreTrainedModel, GenerationMixin): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" lengths (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use `attention_mask` for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`: - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary strings to `torch.FloatTensor` that contains precomputed hidden-states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. The dictionary object will be modified in-place during the forward pass to add newly computed @@ -1121,14 +1121,14 @@ class FlaubertForSequenceClassification(FlaubertPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1142,7 +1142,7 @@ class FlaubertForSequenceClassification(FlaubertPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1232,14 +1232,14 @@ class FlaubertForTokenClassification(FlaubertPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1253,7 +1253,7 @@ class FlaubertForTokenClassification(FlaubertPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1328,7 +1328,7 @@ class FlaubertForQuestionAnsweringSimple(FlaubertPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, start_positions: Optional[torch.Tensor] = None, @@ -1336,7 +1336,7 @@ class FlaubertForQuestionAnsweringSimple(FlaubertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1350,7 +1350,7 @@ class FlaubertForQuestionAnsweringSimple(FlaubertPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1452,8 +1452,8 @@ class FlaubertForQuestionAnsweringOutput(ModelOutput): end_top_log_probs: Optional[torch.FloatTensor] = None end_top_index: Optional[torch.LongTensor] = None cls_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -1477,7 +1477,7 @@ class FlaubertForQuestionAnswering(FlaubertPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, start_positions: Optional[torch.Tensor] = None, @@ -1488,7 +1488,7 @@ class FlaubertForQuestionAnswering(FlaubertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FlaubertForQuestionAnsweringOutput]: + ) -> Union[tuple, FlaubertForQuestionAnsweringOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1502,7 +1502,7 @@ class FlaubertForQuestionAnswering(FlaubertPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1602,14 +1602,14 @@ class FlaubertForMultipleChoice(FlaubertPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1643,7 +1643,7 @@ class FlaubertForMultipleChoice(FlaubertPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. diff --git a/src/transformers/models/flaubert/modeling_tf_flaubert.py b/src/transformers/models/flaubert/modeling_tf_flaubert.py index 43ce980aa7b..575273b3dec 100644 --- a/src/transformers/models/flaubert/modeling_tf_flaubert.py +++ b/src/transformers/models/flaubert/modeling_tf_flaubert.py @@ -22,7 +22,7 @@ import itertools import random import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -150,7 +150,7 @@ FLAUBERT_INPUTS_DOCSTRING = r""" Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility Indices selected in `[0, ..., input_ids.size(-1)]`: - cache (`Dict[str, tf.Tensor]`, *optional*): + cache (`dict[str, tf.Tensor]`, *optional*): Dictionary string to `tf.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -261,14 +261,14 @@ class TFFlaubertModel(TFFlaubertPreTrainedModel): token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: tf.Tensor | None = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -544,14 +544,14 @@ class TFFlaubertMainLayer(keras.layers.Layer): token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: tf.Tensor | None = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: # removed: src_enc=None, src_len=None if input_ids is not None and inputs_embeds is not None: @@ -809,8 +809,8 @@ class TFFlaubertWithLMHeadModelOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @add_start_docstrings( @@ -864,14 +864,14 @@ class TFFlaubertWithLMHeadModel(TFFlaubertPreTrainedModel): token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: tf.Tensor | None = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFFlaubertWithLMHeadModelOutput]: + ) -> Union[tuple, TFFlaubertWithLMHeadModelOutput]: transformer_outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -940,7 +940,7 @@ class TFFlaubertForSequenceClassification(TFFlaubertPreTrainedModel, TFSequenceC token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -948,7 +948,7 @@ class TFFlaubertForSequenceClassification(TFFlaubertPreTrainedModel, TFSequenceC return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1031,7 +1031,7 @@ class TFFlaubertForQuestionAnsweringSimple(TFFlaubertPreTrainedModel, TFQuestion token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1040,7 +1040,7 @@ class TFFlaubertForQuestionAnsweringSimple(TFFlaubertPreTrainedModel, TFQuestion start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -1138,7 +1138,7 @@ class TFFlaubertForTokenClassification(TFFlaubertPreTrainedModel, TFTokenClassif token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1146,7 +1146,7 @@ class TFFlaubertForTokenClassification(TFFlaubertPreTrainedModel, TFTokenClassif return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1251,7 +1251,7 @@ class TFFlaubertForMultipleChoice(TFFlaubertPreTrainedModel, TFMultipleChoiceLos token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1259,7 +1259,7 @@ class TFFlaubertForMultipleChoice(TFFlaubertPreTrainedModel, TFMultipleChoiceLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: if input_ids is not None: num_choices = shape_list(input_ids)[1] seq_length = shape_list(input_ids)[2] diff --git a/src/transformers/models/flaubert/tokenization_flaubert.py b/src/transformers/models/flaubert/tokenization_flaubert.py index cfcd3defa88..dee653450eb 100644 --- a/src/transformers/models/flaubert/tokenization_flaubert.py +++ b/src/transformers/models/flaubert/tokenization_flaubert.py @@ -18,7 +18,7 @@ import json import os import re import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -430,8 +430,8 @@ class FlaubertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM sequence has the following format: @@ -458,8 +458,8 @@ class FlaubertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -486,7 +486,7 @@ class FlaubertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/flava/configuration_flava.py b/src/transformers/models/flava/configuration_flava.py index 65520d48aff..c3ecf68a898 100644 --- a/src/transformers/models/flava/configuration_flava.py +++ b/src/transformers/models/flava/configuration_flava.py @@ -14,7 +14,7 @@ # limitations under the License. """FLAVA model configurations""" -from typing import Any, Dict, Optional +from typing import Any, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -472,10 +472,10 @@ class FlavaConfig(PretrainedConfig): def __init__( self, - image_config: Optional[Dict[str, Any]] = None, - text_config: Optional[Dict[str, Any]] = None, - multimodal_config: Optional[Dict[str, Any]] = None, - image_codebook_config: Optional[Dict[str, Any]] = None, + image_config: Optional[dict[str, Any]] = None, + text_config: Optional[dict[str, Any]] = None, + multimodal_config: Optional[dict[str, Any]] = None, + image_codebook_config: Optional[dict[str, Any]] = None, hidden_size: int = 768, layer_norm_eps: float = 1e-12, projection_dim: int = 768, diff --git a/src/transformers/models/flava/image_processing_flava.py b/src/transformers/models/flava/image_processing_flava.py index 8dc5ed7e087..70f29affdf6 100644 --- a/src/transformers/models/flava/image_processing_flava.py +++ b/src/transformers/models/flava/image_processing_flava.py @@ -18,7 +18,7 @@ import math import random from collections.abc import Iterable from functools import lru_cache -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -60,7 +60,7 @@ LOGIT_LAPLACE_EPS: float = 0.1 class FlavaMaskingGenerator: def __init__( self, - input_size: Union[int, Tuple[int, int]] = 14, + input_size: Union[int, tuple[int, int]] = 14, total_mask_patches: int = 75, mask_group_max_patches: Optional[int] = None, mask_group_min_patches: int = 16, @@ -144,14 +144,14 @@ class FlavaImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by the `size` parameter in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use if resizing the image. Can be overridden by the `resample` parameter in `preprocess`. do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the images. Can be overridden by the `do_center_crop` parameter in `preprocess`. - crop_size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of image after the center crop `(crop_size["height"], crop_size["width"])`. Can be overridden by the `crop_size` parameter in `preprocess`. do_rescale (`bool`, *optional*, defaults to `True`): @@ -162,10 +162,10 @@ class FlavaImageProcessor(BaseImageProcessor): `preprocess`. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in `preprocess`. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. return_image_mask (`bool`, *optional*, defaults to `False`): @@ -191,7 +191,7 @@ class FlavaImageProcessor(BaseImageProcessor): codebook_do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the input for codebook to a certain. Can be overridden by the `codebook_do_resize` parameter in `preprocess`. `codebook_size`. - codebook_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + codebook_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Resize the input for codebook to the given size. Can be overridden by the `codebook_size` parameter in `preprocess`. codebook_resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): @@ -201,7 +201,7 @@ class FlavaImageProcessor(BaseImageProcessor): Whether to crop the input for codebook at the center. If the input size is smaller than `codebook_crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `codebook_do_center_crop` parameter in `preprocess`. - codebook_crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + codebook_crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size for codebook input when applying center-cropping. Can be overridden by the `codebook_crop_size` parameter in `preprocess`. codebook_do_rescale (`bool`, *optional*, defaults to `True`): @@ -229,10 +229,10 @@ class FlavaImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, @@ -306,7 +306,7 @@ class FlavaImageProcessor(BaseImageProcessor): self.codebook_image_std = codebook_image_std if codebook_image_std is not None else FLAVA_CODEBOOK_STD @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `FlavaImageProcessor.from_pretrained(checkpoint, codebook_size=600)` @@ -341,7 +341,7 @@ class FlavaImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -353,7 +353,7 @@ class FlavaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -393,15 +393,15 @@ class FlavaImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_map_pixels: Optional[bool] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[ChannelDimension] = None, @@ -458,15 +458,15 @@ class FlavaImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, # Mask related params return_image_mask: Optional[bool] = None, input_size_patches: Optional[int] = None, @@ -478,10 +478,10 @@ class FlavaImageProcessor(BaseImageProcessor): # Codebook related params return_codebook_pixels: Optional[bool] = None, codebook_do_resize: Optional[bool] = None, - codebook_size: Optional[Dict[str, int]] = None, + codebook_size: Optional[dict[str, int]] = None, codebook_resample: Optional[int] = None, codebook_do_center_crop: Optional[bool] = None, - codebook_crop_size: Optional[Dict[str, int]] = None, + codebook_crop_size: Optional[dict[str, int]] = None, codebook_do_rescale: Optional[bool] = None, codebook_rescale_factor: Optional[float] = None, codebook_do_map_pixels: Optional[bool] = None, @@ -501,14 +501,14 @@ class FlavaImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -516,9 +516,9 @@ class FlavaImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_image_mask (`bool`, *optional*, defaults to `self.return_image_mask`): Whether to return the image mask. @@ -538,14 +538,14 @@ class FlavaImageProcessor(BaseImageProcessor): Whether to return the codebook pixels. codebook_do_resize (`bool`, *optional*, defaults to `self.codebook_do_resize`): Whether to resize the codebook pixels. - codebook_size (`Dict[str, int]`, *optional*, defaults to `self.codebook_size`): + codebook_size (`dict[str, int]`, *optional*, defaults to `self.codebook_size`): Size of the codebook pixels. codebook_resample (`int`, *optional*, defaults to `self.codebook_resample`): Resampling filter to use if resizing the codebook pixels. This can be one of the enum `PILImageResampling`, Only has an effect if `codebook_do_resize` is set to `True`. codebook_do_center_crop (`bool`, *optional*, defaults to `self.codebook_do_center_crop`): Whether to center crop the codebook pixels. - codebook_crop_size (`Dict[str, int]`, *optional*, defaults to `self.codebook_crop_size`): + codebook_crop_size (`dict[str, int]`, *optional*, defaults to `self.codebook_crop_size`): Size of the center crop of the codebook pixels. Only has an effect if `codebook_do_center_crop` is set to `True`. codebook_do_rescale (`bool`, *optional*, defaults to `self.codebook_do_rescale`): @@ -556,9 +556,9 @@ class FlavaImageProcessor(BaseImageProcessor): Whether to map the codebook pixels values. codebook_do_normalize (`bool`, *optional*, defaults to `self.codebook_do_normalize`): Whether to normalize the codebook pixels. - codebook_image_mean (`float` or `List[float]`, *optional*, defaults to `self.codebook_image_mean`): + codebook_image_mean (`float` or `list[float]`, *optional*, defaults to `self.codebook_image_mean`): Codebook pixels mean to normalize the codebook pixels by if `codebook_do_normalize` is set to `True`. - codebook_image_std (`float` or `List[float]`, *optional*, defaults to `self.codebook_image_std`): + codebook_image_std (`float` or `list[float]`, *optional*, defaults to `self.codebook_image_std`): Codebook pixels standard deviation to normalize the codebook pixels by if `codebook_do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/flava/image_processing_flava_fast.py b/src/transformers/models/flava/image_processing_flava_fast.py index 49fec8ed4fb..71ea422d90b 100644 --- a/src/transformers/models/flava/image_processing_flava_fast.py +++ b/src/transformers/models/flava/image_processing_flava_fast.py @@ -18,7 +18,7 @@ import math import random from collections.abc import Iterable from functools import lru_cache -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils_fast import ( BaseImageProcessorFast, @@ -60,7 +60,7 @@ if is_torchvision_available(): class FlavaMaskingGenerator: def __init__( self, - input_size: Union[int, Tuple[int, int]] = 14, + input_size: Union[int, tuple[int, int]] = 14, total_mask_patches: int = 75, mask_group_max_patches: Optional[int] = None, mask_group_min_patches: int = 16, @@ -161,7 +161,7 @@ class FlavaFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): codebook_do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the input for codebook to a certain. Can be overridden by the `codebook_do_resize` parameter in `preprocess`. `codebook_size`. - codebook_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + codebook_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Resize the input for codebook to the given size. Can be overridden by the `codebook_size` parameter in `preprocess`. codebook_resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): @@ -171,7 +171,7 @@ class FlavaFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): Whether to crop the input for codebook at the center. If the input size is smaller than `codebook_crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `codebook_do_center_crop` parameter in `preprocess`. - codebook_crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + codebook_crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size for codebook input when applying center-cropping. Can be overridden by the `codebook_crop_size` parameter in `preprocess`. codebook_do_rescale (`bool`, *optional*, defaults to `True`): @@ -261,7 +261,7 @@ class FlavaImageProcessorFast(BaseImageProcessorFast): return super().preprocess(images, **kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `FlavaImageProcessor.from_pretrained(checkpoint, codebook_size=600)` diff --git a/src/transformers/models/flava/modeling_flava.py b/src/transformers/models/flava/modeling_flava.py index f116d339084..99ffbaa9f30 100644 --- a/src/transformers/models/flava/modeling_flava.py +++ b/src/transformers/models/flava/modeling_flava.py @@ -18,7 +18,7 @@ import collections import math from collections import OrderedDict from dataclasses import dataclass -from typing import Any, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -78,7 +78,7 @@ class FlavaModelOutput(ModelOutput): multimodal_embeddings: Optional[torch.FloatTensor] = None multimodal_output: Optional[BaseModelOutputWithPooling] = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_output", "image_output", "multimodal_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -210,7 +210,7 @@ class FlavaForPreTrainingOutput(ModelOutput): mmm_image_logits: Optional[torch.FloatTensor] = None mmm_text_logits: Optional[torch.FloatTensor] = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: transformer_outputs = [ "text_output", "image_output", @@ -332,7 +332,7 @@ class PatchEmbeddings(nn.Module): def __init__( self, image_size: int = 224, - patch_size: Union[int, Tuple[int, int]] = 16, + patch_size: Union[int, tuple[int, int]] = 16, num_channels: int = 3, embed_dim: int = 768, ): @@ -447,7 +447,7 @@ class FlavaSelfAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -509,7 +509,7 @@ class FlavaAttention(nn.Module): self.output = FlavaSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -533,7 +533,7 @@ class FlavaAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention( hidden_states, attention_mask=attention_mask, head_mask=head_mask, output_attentions=output_attentions ) @@ -598,7 +598,7 @@ class FlavaLayer(nn.Module): attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViT, layernorm is applied before self-attention attention_mask=attention_mask, @@ -754,7 +754,7 @@ class FlavaImageModel(FlavaPreTrainedModel): def set_input_embeddings(self, value: nn.Module): self.embeddings.patch_embeddings = value - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -849,7 +849,7 @@ class FlavaTextModel(FlavaPreTrainedModel): def set_input_embeddings(self, value: nn.Module): self.embeddings.word_embeddings = value - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -959,7 +959,7 @@ class FlavaMultimodalModel(FlavaPreTrainedModel): self.post_init() - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -1201,7 +1201,7 @@ class FlavaModel(FlavaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: bool = True, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FlavaOutput]: + ) -> Union[tuple, FlavaOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, image_num_patches + text_seq_len)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -1678,7 +1678,7 @@ class FlavaForPreTraining(FlavaPreTrainedModel): output_hidden_states: bool = True, return_dict: Optional[bool] = None, return_loss: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], FlavaForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], FlavaForPreTrainingOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, text_seq_len)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See diff --git a/src/transformers/models/flava/processing_flava.py b/src/transformers/models/flava/processing_flava.py index 0e2a98cc103..349ae0fb263 100644 --- a/src/transformers/models/flava/processing_flava.py +++ b/src/transformers/models/flava/processing_flava.py @@ -17,7 +17,7 @@ Image/Text processor class for FLAVA """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessorMixin @@ -63,7 +63,7 @@ class FlavaProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = False, diff --git a/src/transformers/models/fnet/modeling_fnet.py b/src/transformers/models/fnet/modeling_fnet.py index b2e8572efec..619d6c9c5ad 100755 --- a/src/transformers/models/fnet/modeling_fnet.py +++ b/src/transformers/models/fnet/modeling_fnet.py @@ -17,7 +17,7 @@ import warnings from dataclasses import dataclass from functools import partial -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -433,7 +433,7 @@ class FNetForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -574,7 +574,7 @@ class FNetForPreTraining(FNetPreTrainedModel): next_sentence_label: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FNetForPreTrainingOutput]: + ) -> Union[tuple, FNetForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -663,7 +663,7 @@ class FNetForMaskedLM(FNetPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -722,7 +722,7 @@ class FNetForNextSentencePrediction(FNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, NextSentencePredictorOutput]: + ) -> Union[tuple, NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -814,7 +814,7 @@ class FNetForSequenceClassification(FNetPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -887,7 +887,7 @@ class FNetForMultipleChoice(FNetPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -981,7 +981,7 @@ class FNetForTokenClassification(FNetPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1039,7 +1039,7 @@ class FNetForQuestionAnswering(FNetPreTrainedModel): end_positions: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.fnet( diff --git a/src/transformers/models/fnet/tokenization_fnet.py b/src/transformers/models/fnet/tokenization_fnet.py index 3218cbff1eb..a03f1b42a98 100644 --- a/src/transformers/models/fnet/tokenization_fnet.py +++ b/src/transformers/models/fnet/tokenization_fnet.py @@ -17,7 +17,7 @@ import os import unicodedata from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -100,7 +100,7 @@ class FNetTokenizer(PreTrainedTokenizer): pad_token="", cls_token="[CLS]", mask_token="[MASK]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it and @@ -171,7 +171,7 @@ class FNetTokenizer(PreTrainedTokenizer): return outputs - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string.""" text = self.preprocess_text(text) pieces = self.sp_model.encode(text, out_type=str) @@ -221,7 +221,7 @@ class FNetTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, spaces_between_special_tokens: bool = False, @@ -241,8 +241,8 @@ class FNetTokenizer(PreTrainedTokenizer): return text def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An FNet sequence has the following format: @@ -266,8 +266,8 @@ class FNetTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -293,7 +293,7 @@ class FNetTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/fnet/tokenization_fnet_fast.py b/src/transformers/models/fnet/tokenization_fnet_fast.py index ca2c3ca3907..4aab7997650 100644 --- a/src/transformers/models/fnet/tokenization_fnet_fast.py +++ b/src/transformers/models/fnet/tokenization_fnet_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -114,8 +114,8 @@ class FNetTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An FNet sequence has the following format: @@ -138,7 +138,7 @@ class FNetTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep return cls + token_ids_0 + sep + token_ids_1 + sep - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/focalnet/configuration_focalnet.py b/src/transformers/models/focalnet/configuration_focalnet.py index 8fe5831a112..e412e3824e1 100644 --- a/src/transformers/models/focalnet/configuration_focalnet.py +++ b/src/transformers/models/focalnet/configuration_focalnet.py @@ -44,7 +44,7 @@ class FocalNetConfig(BackboneConfigMixin, PretrainedConfig): use_conv_embed (`bool`, *optional*, defaults to `False`): Whether to use convolutional embedding. The authors noted that using convolutional embedding usually improve the performance, but it's not used by default. - hidden_sizes (`List[int]`, *optional*, defaults to `[192, 384, 768, 768]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[192, 384, 768, 768]`): Dimensionality (hidden size) at each stage. depths (`list(int)`, *optional*, defaults to `[2, 2, 6, 2]`): Depth (number of layers) of each stage in the encoder. @@ -77,12 +77,12 @@ class FocalNetConfig(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. encoder_stride (`int`, *optional*, defaults to 32): Factor to increase the spatial resolution by in the decoder head for masked image modeling. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/focalnet/modeling_focalnet.py b/src/transformers/models/focalnet/modeling_focalnet.py index 6a9e38411c4..54abac6625b 100644 --- a/src/transformers/models/focalnet/modeling_focalnet.py +++ b/src/transformers/models/focalnet/modeling_focalnet.py @@ -17,7 +17,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -58,8 +58,8 @@ class FocalNetEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -87,8 +87,8 @@ class FocalNetModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -116,8 +116,8 @@ class FocalNetMaskedImageModelingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None reconstruction: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -145,8 +145,8 @@ class FocalNetImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor]] = None class FocalNetEmbeddings(nn.Module): @@ -174,7 +174,7 @@ class FocalNetEmbeddings(nn.Module): def forward( self, pixel_values: Optional[torch.FloatTensor], bool_masked_pos: Optional[torch.BoolTensor] = None - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: embeddings, output_dimensions = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) batch_size, seq_len, _ = embeddings.size() @@ -241,7 +241,7 @@ class FocalNetPatchEmbeddings(nn.Module): pixel_values = nn.functional.pad(pixel_values, pad_values) return pixel_values - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: _, num_channels, height, width = pixel_values.shape if num_channels != self.num_channels: raise ValueError( @@ -396,7 +396,7 @@ class FocalNetLayer(nn.Module): Layer index. dim (`int`): Number of input channels. - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Input resolution. drop_path (`float`, *optional*, defaults to 0.0): Stochastic depth rate. @@ -500,7 +500,7 @@ class FocalNetStage(nn.Module): self.pointing = False - def forward(self, hidden_states: torch.Tensor, input_dimensions: Tuple[int, int]) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor, input_dimensions: tuple[int, int]) -> tuple[torch.Tensor]: height, width = input_dimensions for layer_module in self.layers: hidden_states = layer_module(hidden_states, input_dimensions) @@ -543,11 +543,11 @@ class FocalNetEncoder(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, FocalNetEncoderOutput]: + ) -> Union[tuple, FocalNetEncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None @@ -664,7 +664,7 @@ class FocalNetModel(FocalNetPreTrainedModel): bool_masked_pos: Optional[torch.BoolTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FocalNetModelOutput]: + ) -> Union[tuple, FocalNetModelOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -746,7 +746,7 @@ class FocalNetForMaskedImageModeling(FocalNetPreTrainedModel): bool_masked_pos: Optional[torch.BoolTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FocalNetMaskedImageModelingOutput]: + ) -> Union[tuple, FocalNetMaskedImageModelingOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -848,7 +848,7 @@ class FocalNetForImageClassification(FocalNetPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FocalNetImageClassifierOutput]: + ) -> Union[tuple, FocalNetImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/fsmt/configuration_fsmt.py b/src/transformers/models/fsmt/configuration_fsmt.py index f8fce7b5c6e..f120d3c436c 100644 --- a/src/transformers/models/fsmt/configuration_fsmt.py +++ b/src/transformers/models/fsmt/configuration_fsmt.py @@ -46,7 +46,7 @@ class FSMTConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - langs (`List[str]`): + langs (`list[str]`): A list with source language and target_language (e.g., ['en', 'ru']). src_vocab_size (`int`): Vocabulary size of the encoder. Defines the number of different tokens that can be represented by the diff --git a/src/transformers/models/fsmt/modeling_fsmt.py b/src/transformers/models/fsmt/modeling_fsmt.py index 6be3ae909ab..9e3000ad133 100644 --- a/src/transformers/models/fsmt/modeling_fsmt.py +++ b/src/transformers/models/fsmt/modeling_fsmt.py @@ -28,7 +28,7 @@ """PyTorch Fairseq model, ported from https://github.com/pytorch/fairseq/tree/master/examples/wmt19""" import math -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch from torch import Tensor, nn @@ -351,7 +351,7 @@ class FSMTEncoder(nn.Module): self.embed_positions = SinusoidalPositionalEmbedding( config.max_position_embeddings + self.padding_idx + 1, embed_dim, self.padding_idx ) - self.layers = nn.ModuleList([EncoderLayer(config) for _ in range(config.encoder_layers)]) # type: List[EncoderLayer] + self.layers = nn.ModuleList([EncoderLayer(config) for _ in range(config.encoder_layers)]) # type: list[EncoderLayer] def forward( self, @@ -559,7 +559,7 @@ class FSMTDecoder(nn.Module): self.embed_positions = SinusoidalPositionalEmbedding( config.max_position_embeddings + self.padding_idx + 1, embed_dim, self.padding_idx ) - self.layers = nn.ModuleList([DecoderLayer(config) for _ in range(config.decoder_layers)]) # type: List[DecoderLayer] + self.layers = nn.ModuleList([DecoderLayer(config) for _ in range(config.decoder_layers)]) # type: list[DecoderLayer] if is_deepspeed_zero3_enabled(): import deepspeed @@ -584,7 +584,7 @@ class FSMTDecoder(nn.Module): head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: bool = False, output_attentions: bool = False, output_hidden_states: bool = False, @@ -765,11 +765,11 @@ class Attention(nn.Module): query, key: Optional[Tensor], key_padding_mask: Optional[Tensor] = None, - layer_state: Optional[Dict[str, Optional[Tensor]]] = None, + layer_state: Optional[dict[str, Optional[Tensor]]] = None, attn_mask: Optional[Tensor] = None, layer_head_mask: Optional[Tensor] = None, output_attentions=False, - ) -> Tuple[Tensor, Optional[Tensor]]: + ) -> tuple[Tensor, Optional[Tensor]]: """Input shape: Time(SeqLen) x Batch x Channel""" static_kv: bool = self.encoder_decoder_attention tgt_len, bsz, embed_dim = query.size() @@ -945,15 +945,15 @@ class FSMTModel(PretrainedFSMTModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1089,8 +1089,8 @@ class FSMTForConditionalGeneration(PretrainedFSMTModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1098,7 +1098,7 @@ class FSMTForConditionalGeneration(PretrainedFSMTModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/fsmt/tokenization_fsmt.py b/src/transformers/models/fsmt/tokenization_fsmt.py index d47efffd03e..5a4446d8e90 100644 --- a/src/transformers/models/fsmt/tokenization_fsmt.py +++ b/src/transformers/models/fsmt/tokenization_fsmt.py @@ -18,7 +18,7 @@ import json import os import re import unicodedata -from typing import Dict, List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -227,7 +227,7 @@ class FSMTTokenizer(PreTrainedTokenizer): ) # hack override - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: return self.get_src_vocab() # hack override @@ -377,8 +377,8 @@ class FSMTTokenizer(PreTrainedTokenizer): return text def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A FAIRSEQ Transformer sequence has the following format: @@ -403,8 +403,8 @@ class FSMTTokenizer(PreTrainedTokenizer): return token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -430,7 +430,7 @@ class FSMTTokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/funnel/configuration_funnel.py b/src/transformers/models/funnel/configuration_funnel.py index 5dd1b09088b..212a976f278 100644 --- a/src/transformers/models/funnel/configuration_funnel.py +++ b/src/transformers/models/funnel/configuration_funnel.py @@ -35,9 +35,9 @@ class FunnelConfig(PretrainedConfig): vocab_size (`int`, *optional*, defaults to 30522): Vocabulary size of the Funnel transformer. Defines the number of different tokens that can be represented by the `inputs_ids` passed when calling [`FunnelModel`] or [`TFFunnelModel`]. - block_sizes (`List[int]`, *optional*, defaults to `[4, 4, 4]`): + block_sizes (`list[int]`, *optional*, defaults to `[4, 4, 4]`): The sizes of the blocks used in the model. - block_repeats (`List[int]`, *optional*): + block_repeats (`list[int]`, *optional*): If passed along, each layer of each block is repeated the number of times indicated. num_decoder_layers (`int`, *optional*, defaults to 2): The number of layers in the decoder (when not using the base model). diff --git a/src/transformers/models/funnel/modeling_funnel.py b/src/transformers/models/funnel/modeling_funnel.py index 8e1f8d8d277..bb239566c0b 100644 --- a/src/transformers/models/funnel/modeling_funnel.py +++ b/src/transformers/models/funnel/modeling_funnel.py @@ -16,7 +16,7 @@ import os from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -171,7 +171,7 @@ class FunnelAttentionStructure(nn.Module): inputs_embeds: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: """Returns the attention inputs associated to the inputs of the model.""" # inputs_embeds has shape batch_size x seq_len x d_model # attention_mask and token_type_ids have shape batch_size x seq_len @@ -196,7 +196,7 @@ class FunnelAttentionStructure(nn.Module): def get_position_embeds( self, seq_len: int, dtype: torch.dtype, device: torch.device - ) -> Union[Tuple[torch.Tensor], List[List[torch.Tensor]]]: + ) -> Union[tuple[torch.Tensor], list[list[torch.Tensor]]]: """ Create and cache inputs related to relative position encoding. Those are very different depending on whether we are using the factorized or the relative shift attention: @@ -305,8 +305,8 @@ class FunnelAttentionStructure(nn.Module): def stride_pool( self, - tensor: Union[torch.Tensor, Tuple[torch.Tensor], List[torch.Tensor]], - axis: Union[int, Tuple[int], List[int]], + tensor: Union[torch.Tensor, tuple[torch.Tensor], list[torch.Tensor]], + axis: Union[int, tuple[int], list[int]], ) -> torch.Tensor: """ Perform pooling by stride slicing the tensor along the given axis. @@ -337,7 +337,7 @@ class FunnelAttentionStructure(nn.Module): return tensor[enc_slice] def pool_tensor( - self, tensor: Union[torch.Tensor, Tuple[torch.Tensor], List[torch.Tensor]], mode: str = "mean", stride: int = 2 + self, tensor: Union[torch.Tensor, tuple[torch.Tensor], list[torch.Tensor]], mode: str = "mean", stride: int = 2 ) -> torch.Tensor: """Apply 1D pooling to a tensor of size [B x T (x H)].""" if tensor is None: @@ -375,8 +375,8 @@ class FunnelAttentionStructure(nn.Module): return tensor def pre_attention_pooling( - self, output, attention_inputs: Tuple[torch.Tensor] - ) -> Tuple[torch.Tensor, Tuple[torch.Tensor]]: + self, output, attention_inputs: tuple[torch.Tensor] + ) -> tuple[torch.Tensor, tuple[torch.Tensor]]: """Pool `output` and the proper parts of `attention_inputs` before the attention layer.""" position_embeds, token_type_mat, attention_mask, cls_mask = attention_inputs if self.config.pool_q_only: @@ -396,7 +396,7 @@ class FunnelAttentionStructure(nn.Module): attention_inputs = (position_embeds, token_type_mat, attention_mask, cls_mask) return output, attention_inputs - def post_attention_pooling(self, attention_inputs: Tuple[torch.Tensor]) -> Tuple[torch.Tensor]: + def post_attention_pooling(self, attention_inputs: tuple[torch.Tensor]) -> tuple[torch.Tensor]: """Pool the proper parts of `attention_inputs` after the attention layer.""" position_embeds, token_type_mat, attention_mask, cls_mask = attention_inputs if self.config.pool_q_only: @@ -521,9 +521,9 @@ class FunnelRelMultiheadAttention(nn.Module): query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, - attention_inputs: Tuple[torch.Tensor], + attention_inputs: tuple[torch.Tensor], output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: # query has shape batch_size x seq_len x d_model # key and value have shapes batch_size x context_len x d_model position_embeds, token_type_mat, attention_mask, cls_mask = attention_inputs @@ -602,7 +602,7 @@ class FunnelLayer(nn.Module): value: torch.Tensor, attention_inputs, output_attentions: bool = False, - ) -> Tuple: + ) -> tuple: attn = self.attention(query, key, value, attention_inputs, output_attentions=output_attentions) output = self.ffn(attn[0]) return (output, attn[1]) if output_attentions else (output,) @@ -628,7 +628,7 @@ class FunnelEncoder(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: # The pooling is not implemented on long tensors, so we convert this mask. attention_mask = attention_mask.type_as(inputs_embeds) attention_inputs = self.attention_structure.init_attention_inputs( @@ -709,7 +709,7 @@ class FunnelDecoder(nn.Module): output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: upsampled_hidden = upsample( final_hidden, stride=2 ** (len(self.config.block_sizes) - 1), @@ -828,8 +828,8 @@ class FunnelForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -866,7 +866,7 @@ class FunnelBaseModel(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -933,7 +933,7 @@ class FunnelModel(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1025,7 +1025,7 @@ class FunnelForPreTraining(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, FunnelForPreTrainingOutput]: + ) -> Union[tuple, FunnelForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the ELECTRA-style loss. Input should be a sequence of tokens (see `input_ids` @@ -1114,7 +1114,7 @@ class FunnelForMaskedLM(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1181,7 +1181,7 @@ class FunnelForSequenceClassification(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1260,7 +1260,7 @@ class FunnelForMultipleChoice(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., @@ -1335,7 +1335,7 @@ class FunnelForTokenClassification(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1397,7 +1397,7 @@ class FunnelForQuestionAnswering(FunnelPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.funnel( diff --git a/src/transformers/models/funnel/modeling_tf_funnel.py b/src/transformers/models/funnel/modeling_tf_funnel.py index 8c144170899..8daa9145d21 100644 --- a/src/transformers/models/funnel/modeling_tf_funnel.py +++ b/src/transformers/models/funnel/modeling_tf_funnel.py @@ -18,7 +18,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -1105,8 +1105,8 @@ class TFFunnelForPreTrainingOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None FUNNEL_START_DOCSTRING = r""" @@ -1228,7 +1228,7 @@ class TFFunnelBaseModel(TFFunnelPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutput]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutput]: return self.funnel( input_ids=input_ids, attention_mask=attention_mask, @@ -1284,7 +1284,7 @@ class TFFunnelModel(TFFunnelPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutput]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutput]: return self.funnel( input_ids=input_ids, attention_mask=attention_mask, @@ -1341,7 +1341,7 @@ class TFFunnelForPreTraining(TFFunnelPreTrainedModel): return_dict: Optional[bool] = None, training: bool = False, **kwargs, - ) -> Union[Tuple[tf.Tensor], TFFunnelForPreTrainingOutput]: + ) -> Union[tuple[tf.Tensor], TFFunnelForPreTrainingOutput]: r""" Returns: @@ -1431,7 +1431,7 @@ class TFFunnelForMaskedLM(TFFunnelPreTrainedModel, TFMaskedLanguageModelingLoss) return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFMaskedLMOutput]: + ) -> Union[tuple[tf.Tensor], TFMaskedLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1514,7 +1514,7 @@ class TFFunnelForSequenceClassification(TFFunnelPreTrainedModel, TFSequenceClass return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFSequenceClassifierOutput]: + ) -> Union[tuple[tf.Tensor], TFSequenceClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1603,7 +1603,7 @@ class TFFunnelForMultipleChoice(TFFunnelPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFMultipleChoiceModelOutput]: + ) -> Union[tuple[tf.Tensor], TFMultipleChoiceModelOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1710,7 +1710,7 @@ class TFFunnelForTokenClassification(TFFunnelPreTrainedModel, TFTokenClassificat return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFTokenClassifierOutput]: + ) -> Union[tuple[tf.Tensor], TFTokenClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1799,7 +1799,7 @@ class TFFunnelForQuestionAnswering(TFFunnelPreTrainedModel, TFQuestionAnsweringL start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFQuestionAnsweringModelOutput]: + ) -> Union[tuple[tf.Tensor], TFQuestionAnsweringModelOutput]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/funnel/tokenization_funnel.py b/src/transformers/models/funnel/tokenization_funnel.py index d1c624a88d3..960e671e4ae 100644 --- a/src/transformers/models/funnel/tokenization_funnel.py +++ b/src/transformers/models/funnel/tokenization_funnel.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -215,8 +215,8 @@ class FunnelTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -241,8 +241,8 @@ class FunnelTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -269,8 +269,8 @@ class FunnelTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. A Funnel Transformer sequence pair mask has the following format: @@ -283,13 +283,13 @@ class FunnelTokenizer(PreTrainedTokenizer): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -298,7 +298,7 @@ class FunnelTokenizer(PreTrainedTokenizer): return len(cls) * [self.cls_token_type_id] + len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/funnel/tokenization_funnel_fast.py b/src/transformers/models/funnel/tokenization_funnel_fast.py index c3e45ed62ac..eeeb6f7bf6c 100644 --- a/src/transformers/models/funnel/tokenization_funnel_fast.py +++ b/src/transformers/models/funnel/tokenization_funnel_fast.py @@ -15,7 +15,7 @@ """Tokenization class for Funnel Transformer.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -166,8 +166,8 @@ class FunnelTokenizerFast(PreTrainedTokenizerFast): return output def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. A Funnel Transformer sequence pair mask has the following format: @@ -180,13 +180,13 @@ class FunnelTokenizerFast(PreTrainedTokenizerFast): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -195,7 +195,7 @@ class FunnelTokenizerFast(PreTrainedTokenizerFast): return len(cls) * [self.cls_token_type_id] + len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] # Copied from transformers.models.bert.tokenization_bert_fast.BertTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/fuyu/configuration_fuyu.py b/src/transformers/models/fuyu/configuration_fuyu.py index 4584184b0ae..90e150a9f96 100644 --- a/src/transformers/models/fuyu/configuration_fuyu.py +++ b/src/transformers/models/fuyu/configuration_fuyu.py @@ -87,7 +87,7 @@ class FuyuConfig(PretrainedConfig): The id of the *padding* token. bos_token_id (`int`, *optional*, defaults to 1): The id of the *beginning-of-sequence* token. - eos_token_id (`Union[int, List[int]]`, *optional*, defaults to 2): + eos_token_id (`Union[int, list[int]]`, *optional*, defaults to 2): The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. image_token_id (`int`, *optional*, defaults to 71011): The id of the image placeholder token. diff --git a/src/transformers/models/fuyu/image_processing_fuyu.py b/src/transformers/models/fuyu/image_processing_fuyu.py index 2984aac67da..bab6e3b47b6 100644 --- a/src/transformers/models/fuyu/image_processing_fuyu.py +++ b/src/transformers/models/fuyu/image_processing_fuyu.py @@ -15,7 +15,7 @@ """Image processor class for Fuyu.""" import math -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -56,8 +56,8 @@ logger = logging.get_logger(__name__) def make_list_of_list_of_images( - images: Union[List[List[ImageInput]], List[ImageInput], ImageInput], -) -> List[List[ImageInput]]: + images: Union[list[list[ImageInput]], list[ImageInput], ImageInput], +) -> list[list[ImageInput]]: if is_valid_image(images): return [[images]] @@ -110,10 +110,10 @@ class FuyuBatchFeature(BatchFeature): # Do the tensor conversion in batch for key, value in self.items(): if isinstance(value, list) and isinstance(value[0], list): - # List[List[Any]] -> List[List[Tensor]] + # list[list[Any]] -> list[list[Tensor]] self[key] = [[_safe_convert_tensor(elem) for elem in elems] for elems in value] elif isinstance(value, list): - # List[Any] -> List[Tensor] + # list[Any] -> list[Tensor] self[key] = [_safe_convert_tensor(elem) for elem in value] else: # Any -> Tensor @@ -201,7 +201,7 @@ class FuyuImageProcessor(BaseImageProcessor): Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image to `size`. - size (`Dict[str, int]`, *optional*, defaults to `{"height": 1080, "width": 1920}`): + size (`dict[str, int]`, *optional*, defaults to `{"height": 1080, "width": 1920}`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -221,7 +221,7 @@ class FuyuImageProcessor(BaseImageProcessor): Whether to rescale the image. rescale_factor (`float`, *optional*, defaults to `1 / 255`): The factor to use when rescaling the image. - patch_size (`Dict[str, int]`, *optional*, defaults to `{"height": 30, "width": 30}`): + patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 30, "width": 30}`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches. """ @@ -236,17 +236,17 @@ class FuyuImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_pad: bool = True, padding_value: float = 1.0, padding_mode: str = "constant", do_normalize: bool = True, - image_mean: Union[float, List[float]] = 0.5, - image_std: Union[float, List[float]] = 0.5, + image_mean: Union[float, list[float]] = 0.5, + image_std: Union[float, list[float]] = 0.5, do_rescale: bool = True, rescale_factor: float = 1 / 255, - patch_size: Optional[Dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, **kwargs, ): super().__init__(**kwargs) @@ -266,7 +266,7 @@ class FuyuImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -278,7 +278,7 @@ class FuyuImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -324,7 +324,7 @@ class FuyuImageProcessor(BaseImageProcessor): def pad_image( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], mode: str = "constant", constant_values: float = 1.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -336,7 +336,7 @@ class FuyuImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to pad. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. data_format (`ChannelDimension` or `str`, *optional*): The data format of the output image. If unset, the same format as the input image is used. @@ -364,7 +364,7 @@ class FuyuImageProcessor(BaseImageProcessor): self, images, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: Optional[PILImageResampling] = None, do_pad: Optional[bool] = None, padding_value: Optional[float] = None, @@ -374,7 +374,7 @@ class FuyuImageProcessor(BaseImageProcessor): image_std: Optional[float] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, - patch_size: Optional[Dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, data_format: Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, return_tensors: Optional[TensorType] = None, @@ -389,7 +389,7 @@ class FuyuImageProcessor(BaseImageProcessor): values range from 0 to 255, or between 0 and 1 if `do_rescale` is `False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image to `size`. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -409,7 +409,7 @@ class FuyuImageProcessor(BaseImageProcessor): Whether to rescale the image. rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`): The factor to use when rescaling the image. - patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`): + patch_size (`dict[str, int]`, *optional*, defaults to `self.patch_size`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: @@ -537,7 +537,7 @@ class FuyuImageProcessor(BaseImageProcessor): } return FuyuBatchFeature(data=data, tensor_type=return_tensors) - def get_num_patches(self, image_height: int, image_width: int, patch_size: Optional[Dict[str, int]] = None) -> int: + def get_num_patches(self, image_height: int, image_width: int, patch_size: Optional[dict[str, int]] = None) -> int: """ Calculate number of patches required to encode an image. @@ -546,7 +546,7 @@ class FuyuImageProcessor(BaseImageProcessor): Height of the image. image_width (`int`): Width of the image. - patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`): + patch_size (`dict[str, int]`, *optional*, defaults to `self.patch_size`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches. """ patch_size = patch_size if patch_size is not None else self.patch_size @@ -562,14 +562,14 @@ class FuyuImageProcessor(BaseImageProcessor): num_patches = num_patches_per_dim_h * num_patches_per_dim_w return num_patches - def patchify_image(self, image: "torch.Tensor", patch_size: Optional[Dict[str, int]] = None) -> "torch.Tensor": + def patchify_image(self, image: "torch.Tensor", patch_size: Optional[dict[str, int]] = None) -> "torch.Tensor": """ Convert an image into a tensor of patches. Args: image (`torch.Tensor`): Image to convert. Shape: [batch, channels, height, width] - patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`): + patch_size (`dict[str, int]`, *optional*, defaults to `self.patch_size`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the patches. """ requires_backends(self, ["torch"]) @@ -597,7 +597,7 @@ class FuyuImageProcessor(BaseImageProcessor): image_placeholder_id: int, image_newline_id: int, variable_sized: bool, - patch_size: Optional[Dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, ) -> FuyuBatchFeature: """Process images for model input. In particular, variable-sized images are handled here. @@ -616,7 +616,7 @@ class FuyuImageProcessor(BaseImageProcessor): The id of the image newline token. Comes from an associated tokenizer. variable_sized (bool): Whether to process images as variable-sized. - patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`): + patch_size (`dict[str, int]`, *optional*, defaults to `self.patch_size`): Size of the patches. """ requires_backends(self, ["torch"]) @@ -625,10 +625,10 @@ class FuyuImageProcessor(BaseImageProcessor): patch_height, patch_width = patch_size["height"], patch_size["width"] # Only images that are present. - images: List[List[torch.Tensor]] = [] - batch_image_patches: List[List[torch.Tensor]] = [] + images: list[list[torch.Tensor]] = [] + batch_image_patches: list[list[torch.Tensor]] = [] # Image input ids for every subsequence, including ones with no image present. - batch_image_input_ids: List[List[torch.Tensor]] = [] + batch_image_input_ids: list[list[torch.Tensor]] = [] for batch_index in range(image_input.shape[0]): image_input_ids = [] image_patches = [] @@ -680,8 +680,8 @@ class FuyuImageProcessor(BaseImageProcessor): # Create image_patch_input_indices, where non-negative values correspond to image patches to be inserted in # the stream. - image_patch_indices_per_batch: List[List[torch.Tensor]] = [] - image_patch_indices_per_subsequence: List[List[torch.Tensor]] = [] + image_patch_indices_per_batch: list[list[torch.Tensor]] = [] + image_patch_indices_per_subsequence: list[list[torch.Tensor]] = [] for sample_image_input_ids in batch_image_input_ids: index_offset = 0 diff --git a/src/transformers/models/fuyu/modeling_fuyu.py b/src/transformers/models/fuyu/modeling_fuyu.py index 78b7ae7d4db..c4ec5e82928 100644 --- a/src/transformers/models/fuyu/modeling_fuyu.py +++ b/src/transformers/models/fuyu/modeling_fuyu.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Fuyu model.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -89,7 +89,7 @@ class FuyuModel(FuyuPreTrainedModel): def gather_continuous_embeddings( self, word_embeddings: torch.Tensor, - continuous_embeddings: List[torch.Tensor], + continuous_embeddings: list[torch.Tensor], image_patch_input_indices: torch.Tensor, ) -> torch.Tensor: """This function places the continuous_embeddings into the word_embeddings at the locations @@ -152,14 +152,14 @@ class FuyuModel(FuyuPreTrainedModel): image_patches_indices: torch.Tensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_patches (`torch.FloatTensor` of shape `(batch_size, num_total_patches, patch_size_ x patch_size x num_channels)`, *optional*): Image patches to be used as continuous embeddings. The patches are flattened and then projected to the @@ -270,7 +270,7 @@ class FuyuForCausalLM(FuyuPreTrainedModel, GenerationMixin): image_patches_indices: torch.Tensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, labels: Optional[torch.Tensor] = None, @@ -279,7 +279,7 @@ class FuyuForCausalLM(FuyuPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, logits_to_keep: Optional[int] = 0, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" image_patches (`torch.FloatTensor` of shape `(batch_size, num_total_patches, patch_size_ x patch_size x num_channels)`, *optional*): Image patches to be used as continuous embeddings. The patches are flattened and then projected to the diff --git a/src/transformers/models/fuyu/processing_fuyu.py b/src/transformers/models/fuyu/processing_fuyu.py index 3c71c34c6f9..ee4b411c3ec 100644 --- a/src/transformers/models/fuyu/processing_fuyu.py +++ b/src/transformers/models/fuyu/processing_fuyu.py @@ -17,7 +17,7 @@ Image/Text processor class for GIT """ import re -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -77,8 +77,8 @@ class FuyuProcessorKwargs(ProcessingKwargs, total=False): def full_unpacked_stream_to_tensor( - all_bi_tokens_to_place: List[int], - full_unpacked_stream: List["torch.Tensor"], + all_bi_tokens_to_place: list[int], + full_unpacked_stream: list["torch.Tensor"], fill_value: int, batch_size: int, new_seq_len: int, @@ -108,12 +108,12 @@ def full_unpacked_stream_to_tensor( def construct_full_unpacked_stream( - num_real_text_tokens: Union[List[List[int]], "torch.Tensor"], + num_real_text_tokens: Union[list[list[int]], "torch.Tensor"], input_stream: "torch.Tensor", - image_tokens: List[List["torch.Tensor"]], + image_tokens: list[list["torch.Tensor"]], batch_size: int, num_sub_sequences: int, -) -> List["torch.Tensor"]: +) -> list["torch.Tensor"]: """Takes an input_stream tensor of shape B x S x ?. For each subsequence, adds any required padding to account for images and then unpacks the subsequences to create a single sequence per item in the batch. Returns a list of tensors, one for each item in the batch.""" @@ -143,12 +143,12 @@ def _replace_string_repr_with_token_tags(prompt: str) -> str: return prompt -def _segment_prompt_into_text_token_conversions(prompt: str) -> List: +def _segment_prompt_into_text_token_conversions(prompt: str) -> list: """ Given a string prompt, converts the prompt into a list of TextTokenConversions. """ # Wherever, we notice the [TOKEN_OPEN_STRING, TOKEN_CLOSE_STRING], we split the prompt - prompt_text_list: List = [] + prompt_text_list: list = [] regex_pattern = re.compile( f"({TOKEN_BBOX_OPEN_STRING}|{TOKEN_BBOX_CLOSE_STRING}|{TOKEN_POINT_OPEN_STRING}|{TOKEN_POINT_CLOSE_STRING})" ) @@ -168,7 +168,7 @@ def _segment_prompt_into_text_token_conversions(prompt: str) -> List: return prompt_text_list -def _transform_coordinates_and_tokenize(prompt: str, scale_factor: float, tokenizer) -> List[int]: +def _transform_coordinates_and_tokenize(prompt: str, scale_factor: float, tokenizer) -> list[int]: """ This function transforms the prompt in the following fashion: - and to their respective token mappings @@ -188,7 +188,7 @@ def _transform_coordinates_and_tokenize(prompt: str, scale_factor: float, tokeni # Tokenize the prompt # Convert prompt into a list split prompt_text_list = _segment_prompt_into_text_token_conversions(prompt) - transformed_prompt_tokens: List[int] = [] + transformed_prompt_tokens: list[int] = [] for elem in prompt_text_list: if elem[1]: # This is a location, we need to tokenize it @@ -200,7 +200,7 @@ def _transform_coordinates_and_tokenize(prompt: str, scale_factor: float, tokeni return transformed_prompt_tokens -def _transform_within_tags(text: str, scale_factor: float, tokenizer) -> List[int]: +def _transform_within_tags(text: str, scale_factor: float, tokenizer) -> list[int]: """ Given a bounding box of the fashion 1, 2, 3, 4 | 1, 2 This function is responsible for converting 1, 2, 3, 4 into tokens of 1 2 3 4 without any commas. @@ -237,13 +237,13 @@ def _transform_within_tags(text: str, scale_factor: float, tokenizer) -> List[in def _tokenize_prompts_with_image_and_batch( tokenizer, - prompts: List[List[str]], - scale_factors: Optional[List[List["torch.Tensor"]]], + prompts: list[list[str]], + scale_factors: Optional[list[list["torch.Tensor"]]], max_tokens_to_generate: int, max_position_embeddings: int, add_BOS: bool, # Same issue with types as above add_beginning_of_answer_token: bool, -) -> Tuple["torch.Tensor", "torch.Tensor"]: +) -> tuple["torch.Tensor", "torch.Tensor"]: """ Given a set of prompts and number of tokens to generate: - tokenize prompts @@ -318,7 +318,7 @@ def original_to_transformed_w_coords(original_coords, scale_w): return np.round(original_coords * scale_w).astype(np.int32) -def scale_point_to_transformed_image(x: float, y: float, scale_factor: float) -> List[int]: +def scale_point_to_transformed_image(x: float, y: float, scale_factor: float) -> list[int]: x_scaled = original_to_transformed_w_coords(np.array([x / 2]), scale_factor)[0] y_scaled = original_to_transformed_h_coords(np.array([y / 2]), scale_factor)[0] return [x_scaled, y_scaled] @@ -326,7 +326,7 @@ def scale_point_to_transformed_image(x: float, y: float, scale_factor: float) -> def scale_bbox_to_transformed_image( top: float, left: float, bottom: float, right: float, scale_factor: float -) -> List[int]: +) -> list[int]: top_scaled = original_to_transformed_w_coords(np.array([top / 2]), scale_factor)[0] left_scaled = original_to_transformed_h_coords(np.array([left / 2]), scale_factor)[0] bottom_scaled = original_to_transformed_w_coords(np.array([bottom / 2]), scale_factor)[0] @@ -364,7 +364,7 @@ class FuyuProcessor(ProcessorMixin): self.image_token_id = tokenizer.encode("|SPEAKER|", add_special_tokens=False)[1] self.image_newline_id = tokenizer.encode("|NEWLINE|", add_special_tokens=False)[1] - def _left_pad_inputs_with_attention_mask(self, model_inputs: List[Dict], return_attention_mask: bool): + def _left_pad_inputs_with_attention_mask(self, model_inputs: list[dict], return_attention_mask: bool): max_length_input_ids = max(entry["input_ids"].shape[1] for entry in model_inputs) max_length_image_patch_indices = max(entry["image_patches_indices"].shape[1] for entry in model_inputs) @@ -487,7 +487,7 @@ class FuyuProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[FuyuProcessorKwargs], @@ -500,10 +500,10 @@ class FuyuProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `List[PIL.Image.Image]`): + images (`PIL.Image.Image`, `list[PIL.Image.Image]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`): + text (`str`, `list[str]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -600,7 +600,7 @@ class FuyuProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: @@ -761,7 +761,7 @@ class FuyuProcessor(ProcessorMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text output. + `list[str]`: The decoded text output. """ beginning_of_answer = self.tokenizer.convert_tokens_to_ids(BEGINNING_OF_ANSWER_STRING) # get boa index for each outputted sequence tensor diff --git a/src/transformers/models/gemma/modeling_flax_gemma.py b/src/transformers/models/gemma/modeling_flax_gemma.py index 49b3ffc68af..0addcd7dde7 100644 --- a/src/transformers/models/gemma/modeling_flax_gemma.py +++ b/src/transformers/models/gemma/modeling_flax_gemma.py @@ -14,7 +14,7 @@ # limitations under the License. """Flax Gemma model.""" -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -108,7 +108,7 @@ GEMMA_INPUTS_DOCSTRING = r""" config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -431,7 +431,7 @@ class FlaxGemmaPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: GemmaConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -440,7 +440,7 @@ class FlaxGemmaPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/gemma/modeling_gemma.py b/src/transformers/models/gemma/modeling_gemma.py index 95b50039eb9..1f8da9ed0ec 100644 --- a/src/transformers/models/gemma/modeling_gemma.py +++ b/src/transformers/models/gemma/modeling_gemma.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -217,12 +217,12 @@ class GemmaAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -278,9 +278,9 @@ class GemmaDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/gemma/modular_gemma.py b/src/transformers/models/gemma/modular_gemma.py index f9f10e816cf..f20f2b7c999 100644 --- a/src/transformers/models/gemma/modular_gemma.py +++ b/src/transformers/models/gemma/modular_gemma.py @@ -13,7 +13,7 @@ # 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. -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Optional import sentencepiece as spm import torch @@ -202,7 +202,7 @@ class GemmaTokenizer(LlamaTokenizer, PreTrainedTokenizer): pad_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to `""`): A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by attention mechanisms or loss computation. - sp_model_kwargs (`Dict[str, Any]`, `Optional`, *optional*): + sp_model_kwargs (`dict[str, Any]`, `Optional`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things, to set: @@ -238,7 +238,7 @@ class GemmaTokenizer(LlamaTokenizer, PreTrainedTokenizer): bos_token="", eos_token="", pad_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, @@ -280,7 +280,7 @@ class GemmaTokenizer(LlamaTokenizer, PreTrainedTokenizer): def unk_token_length(self): raise AttributeError("Not needed for Gemma") - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Args: text: TextInput @@ -298,7 +298,7 @@ class GemmaTokenizer(LlamaTokenizer, PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, spaces_between_special_tokens: bool = False, **kwargs, diff --git a/src/transformers/models/gemma/tokenization_gemma.py b/src/transformers/models/gemma/tokenization_gemma.py index 9850fb532a4..3320968c291 100644 --- a/src/transformers/models/gemma/tokenization_gemma.py +++ b/src/transformers/models/gemma/tokenization_gemma.py @@ -21,7 +21,7 @@ # limitations under the License. import os from shutil import copyfile -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Optional import sentencepiece as spm @@ -59,7 +59,7 @@ class GemmaTokenizer(PreTrainedTokenizer): pad_token (`str` or `tokenizers.AddedToken`, *optional*, defaults to `""`): A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by attention mechanisms or loss computation. - sp_model_kwargs (`Dict[str, Any]`, `Optional`, *optional*): + sp_model_kwargs (`dict[str, Any]`, `Optional`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things, to set: @@ -98,7 +98,7 @@ class GemmaTokenizer(PreTrainedTokenizer): bos_token="", eos_token="", pad_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, @@ -155,7 +155,7 @@ class GemmaTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Args: text: TextInput @@ -194,7 +194,7 @@ class GemmaTokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string - def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> tuple[str]: """ Save the vocabulary and special tokens file to a directory. @@ -233,22 +233,22 @@ class GemmaTokenizer(PreTrainedTokenizer): return output def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -270,8 +270,8 @@ class GemmaTokenizer(PreTrainedTokenizer): ) def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format: @@ -284,13 +284,13 @@ class GemmaTokenizer(PreTrainedTokenizer): if token_ids_1 is None, only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ bos_token_id = [self.bos_token_id] if self.add_bos_token else [] eos_token_id = [self.eos_token_id] if self.add_eos_token else [] @@ -304,7 +304,7 @@ class GemmaTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, spaces_between_special_tokens: bool = False, **kwargs, diff --git a/src/transformers/models/gemma/tokenization_gemma_fast.py b/src/transformers/models/gemma/tokenization_gemma_fast.py index bc6e0c8ba7c..9fc6e3d3593 100644 --- a/src/transformers/models/gemma/tokenization_gemma_fast.py +++ b/src/transformers/models/gemma/tokenization_gemma_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import os from shutil import copyfile -from typing import Optional, Tuple +from typing import Optional from tokenizers import processors @@ -160,7 +160,7 @@ class GemmaTokenizerFast(PreTrainedTokenizerFast): self.update_post_processor() # Copied from transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/gemma2/modeling_gemma2.py b/src/transformers/models/gemma2/modeling_gemma2.py index 7bb865bc5dc..28a57ab9090 100644 --- a/src/transformers/models/gemma2/modeling_gemma2.py +++ b/src/transformers/models/gemma2/modeling_gemma2.py @@ -20,7 +20,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from functools import partial -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -139,7 +139,7 @@ def eager_attention_forward( scaling: Optional[float] = None, softcap: Optional[float] = None, **kwargs, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: if scaling is None: scaling = module.head_dim**-0.5 @@ -195,12 +195,12 @@ class Gemma2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -256,7 +256,7 @@ class Gemma2DecoderLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -264,7 +264,7 @@ class Gemma2DecoderLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/gemma2/modular_gemma2.py b/src/transformers/models/gemma2/modular_gemma2.py index 5ce710a6acb..9890711d3c1 100644 --- a/src/transformers/models/gemma2/modular_gemma2.py +++ b/src/transformers/models/gemma2/modular_gemma2.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from functools import partial -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -223,7 +223,7 @@ def eager_attention_forward( scaling: Optional[float] = None, softcap: Optional[float] = None, **kwargs, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: if scaling is None: scaling = module.head_dim**-0.5 @@ -260,12 +260,12 @@ class Gemma2Attention(GemmaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -321,7 +321,7 @@ class Gemma2DecoderLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -329,7 +329,7 @@ class Gemma2DecoderLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/gemma3/configuration_gemma3.py b/src/transformers/models/gemma3/configuration_gemma3.py index 895058f4655..c36557fa54d 100644 --- a/src/transformers/models/gemma3/configuration_gemma3.py +++ b/src/transformers/models/gemma3/configuration_gemma3.py @@ -19,7 +19,7 @@ # 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. -from typing import Any, Dict, Optional, Union +from typing import Any, Optional, Union from ...configuration_utils import PretrainedConfig, layer_type_validation from ...modeling_rope_utils import rope_config_validation @@ -121,11 +121,11 @@ class Gemma3TextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -302,8 +302,8 @@ class Gemma3Config(PretrainedConfig): def __init__( self, - text_config: Optional[Union[Gemma3TextConfig, Dict[str, Any]]] = None, - vision_config: Optional[Union[SiglipVisionConfig, Dict[str, Any]]] = None, + text_config: Optional[Union[Gemma3TextConfig, dict[str, Any]]] = None, + vision_config: Optional[Union[SiglipVisionConfig, dict[str, Any]]] = None, mm_tokens_per_image: int = 256, boi_token_index: int = 255_999, eoi_token_index: int = 256_000, diff --git a/src/transformers/models/gemma3/image_processing_gemma3.py b/src/transformers/models/gemma3/image_processing_gemma3.py index 91e2d0c66a4..1db36b3da5e 100644 --- a/src/transformers/models/gemma3/image_processing_gemma3.py +++ b/src/transformers/models/gemma3/image_processing_gemma3.py @@ -16,7 +16,7 @@ import itertools import math -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -58,7 +58,7 @@ class Gemma3ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by `size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. @@ -71,10 +71,10 @@ class Gemma3ImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image by the specified mean and standard deviation. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -95,13 +95,13 @@ class Gemma3ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, do_pan_and_scan: Optional[bool] = None, pan_and_scan_min_crop_size: Optional[int] = None, @@ -213,7 +213,7 @@ class Gemma3ImageProcessor(BaseImageProcessor): def _process_images_for_pan_and_scan( self, - images: List[np.ndarray], + images: list[np.ndarray], do_pan_and_scan: bool, pan_and_scan_min_crop_size: int, pan_and_scan_max_num_crops: int, @@ -241,13 +241,13 @@ class Gemma3ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -266,7 +266,7 @@ class Gemma3ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -277,9 +277,9 @@ class Gemma3ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/gemma3/image_processing_gemma3_fast.py b/src/transformers/models/gemma3/image_processing_gemma3_fast.py index d7a5e87e018..c116bfb515f 100644 --- a/src/transformers/models/gemma3/image_processing_gemma3_fast.py +++ b/src/transformers/models/gemma3/image_processing_gemma3_fast.py @@ -16,7 +16,7 @@ import itertools import math -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils_fast import ( BaseImageProcessorFast, @@ -165,7 +165,7 @@ class Gemma3ImageProcessorFast(BaseImageProcessorFast): def _process_images_for_pan_and_scan( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_pan_and_scan: bool, pan_and_scan_min_crop_size: int, pan_and_scan_max_num_crops: int, @@ -190,7 +190,7 @@ class Gemma3ImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List[List["torch.Tensor"]], + images: list[list["torch.Tensor"]], do_resize: bool, size: SizeDict, do_pan_and_scan: Optional[bool], @@ -203,8 +203,8 @@ class Gemma3ImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: # Group images by size for batched processing diff --git a/src/transformers/models/gemma3/modeling_gemma3.py b/src/transformers/models/gemma3/modeling_gemma3.py index dd8587e9d11..8d78ca92b4c 100644 --- a/src/transformers/models/gemma3/modeling_gemma3.py +++ b/src/transformers/models/gemma3/modeling_gemma3.py @@ -23,7 +23,7 @@ import copy from collections.abc import Callable from dataclasses import dataclass from functools import partial -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -114,9 +114,9 @@ class Gemma3CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -259,7 +259,7 @@ def eager_attention_forward( scaling: Optional[float] = None, softcap: Optional[float] = None, **kwargs, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: if scaling is None: scaling = module.head_dim**-0.5 @@ -851,7 +851,7 @@ class Gemma3Model(Gemma3PreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -861,7 +861,7 @@ class Gemma3Model(Gemma3PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **lm_kwargs, - ) -> Union[Tuple, Gemma3ModelOutputWithPast]: + ) -> Union[tuple, Gemma3ModelOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1042,7 +1042,7 @@ class Gemma3ForConditionalGeneration(Gemma3PreTrainedModel, GenerationMixin): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1053,7 +1053,7 @@ class Gemma3ForConditionalGeneration(Gemma3PreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **lm_kwargs, - ) -> Union[Tuple, Gemma3CausalLMOutputWithPast]: + ) -> Union[tuple, Gemma3CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/gemma3/modular_gemma3.py b/src/transformers/models/gemma3/modular_gemma3.py index f298d2736e6..d93d53c8e93 100644 --- a/src/transformers/models/gemma3/modular_gemma3.py +++ b/src/transformers/models/gemma3/modular_gemma3.py @@ -17,7 +17,7 @@ import copy from collections.abc import Callable from dataclasses import dataclass from functools import partial -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn as nn @@ -148,11 +148,11 @@ class Gemma3TextConfig(Gemma2Config, PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -314,8 +314,8 @@ class Gemma3Config(PretrainedConfig): def __init__( self, - text_config: Optional[Union[Gemma3TextConfig, Dict[str, Any]]] = None, - vision_config: Optional[Union[SiglipVisionConfig, Dict[str, Any]]] = None, + text_config: Optional[Union[Gemma3TextConfig, dict[str, Any]]] = None, + vision_config: Optional[Union[SiglipVisionConfig, dict[str, Any]]] = None, mm_tokens_per_image: int = 256, boi_token_index: int = 255_999, eoi_token_index: int = 256_000, @@ -769,7 +769,7 @@ class Gemma3Model(PaliGemmaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -779,7 +779,7 @@ class Gemma3Model(PaliGemmaModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **lm_kwargs, - ) -> Union[Tuple, Gemma3ModelOutputWithPast]: + ) -> Union[tuple, Gemma3ModelOutputWithPast]: if (input_ids is None) ^ (inputs_embeds is not None): raise ValueError("You must specify exactly one of input_ids or inputs_embeds") @@ -880,7 +880,7 @@ class Gemma3ForConditionalGeneration(PaliGemmaForConditionalGeneration): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -891,7 +891,7 @@ class Gemma3ForConditionalGeneration(PaliGemmaForConditionalGeneration): return_dict: Optional[bool] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **lm_kwargs, - ) -> Union[Tuple, Gemma3CausalLMOutputWithPast]: + ) -> Union[tuple, Gemma3CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/gemma3/processing_gemma3.py b/src/transformers/models/gemma3/processing_gemma3.py index ab6f03290a7..a4afbb18acd 100644 --- a/src/transformers/models/gemma3/processing_gemma3.py +++ b/src/transformers/models/gemma3/processing_gemma3.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import re -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -79,7 +79,7 @@ class Gemma3Processor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, videos=None, audio=None, **kwargs: Unpack[Gemma3ProcessorKwargs], @@ -155,7 +155,7 @@ class Gemma3Processor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/git/convert_git_to_pytorch.py b/src/transformers/models/git/convert_git_to_pytorch.py index 4a9d8a01599..34dc58299bc 100644 --- a/src/transformers/models/git/convert_git_to_pytorch.py +++ b/src/transformers/models/git/convert_git_to_pytorch.py @@ -200,7 +200,7 @@ def prepare_video(): Args: container (`av.container.input.InputContainer`): PyAV container. - indices (`List[int]`): List of frame indices to decode. + indices (`list[int]`): List of frame indices to decode. Returns: result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). @@ -226,7 +226,7 @@ def prepare_video(): seg_len (`int`): Maximum allowed index of sample's last frame. Returns: - indices (`List[int]`): List of sampled frame indices + indices (`list[int]`): List of sampled frame indices """ converted_len = int(clip_len * frame_sample_rate) end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/git/modeling_git.py b/src/transformers/models/git/modeling_git.py index c63b00fe6b3..6068ce169da 100644 --- a/src/transformers/models/git/modeling_git.py +++ b/src/transformers/models/git/modeling_git.py @@ -17,7 +17,7 @@ import math from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -73,8 +73,8 @@ class GitVisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None class GitEmbeddings(nn.Module): @@ -173,7 +173,7 @@ class GitSelfAttention(nn.Module): past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) cutoff = self.image_patch_tokens if pixel_values_present else 0 @@ -298,7 +298,7 @@ class GitAttention(nn.Module): past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -360,7 +360,7 @@ class GitLayer(nn.Module): past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, pixel_values_present: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attention_outputs = self.attention( hidden_states, @@ -404,13 +404,13 @@ class GitEncoder(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, pixel_values_present: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPast]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPast]: if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( @@ -672,7 +672,7 @@ class GitVisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -738,7 +738,7 @@ class GitVisionEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -797,7 +797,7 @@ class GitVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -891,7 +891,7 @@ class GitVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -952,7 +952,7 @@ class GitVisionModel(GitPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Examples: @@ -1100,13 +1100,13 @@ class GitModel(GitPreTrainedModel): pixel_values: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPooling]: r""" Examples: @@ -1291,14 +1291,14 @@ class GitForCausalLM(GitPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, List[torch.Tensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithPast]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1375,7 +1375,7 @@ class GitForCausalLM(GitPreTrainedModel, GenerationMixin): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -1399,7 +1399,7 @@ class GitForCausalLM(GitPreTrainedModel, GenerationMixin): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/git/processing_git.py b/src/transformers/models/git/processing_git.py index 98cc3b83cf1..51ac8c2de58 100644 --- a/src/transformers/models/git/processing_git.py +++ b/src/transformers/models/git/processing_git.py @@ -16,7 +16,7 @@ Image/Text processor class for GIT """ -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput @@ -57,7 +57,7 @@ class GitProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[GitProcessorKwargs], @@ -70,10 +70,10 @@ class GitProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`, *optional*): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/glm/modeling_glm.py b/src/transformers/models/glm/modeling_glm.py index 235f8258c10..2ee6273c00d 100644 --- a/src/transformers/models/glm/modeling_glm.py +++ b/src/transformers/models/glm/modeling_glm.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -179,12 +179,12 @@ class GlmAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -295,9 +295,9 @@ class GlmDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/glm4/modeling_glm4.py b/src/transformers/models/glm4/modeling_glm4.py index 9a22b256175..75487c5fccf 100644 --- a/src/transformers/models/glm4/modeling_glm4.py +++ b/src/transformers/models/glm4/modeling_glm4.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -86,9 +86,9 @@ class Glm4DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -237,12 +237,12 @@ class Glm4Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -539,7 +539,7 @@ class Glm4ForCausalLM(Glm4PreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/glm4/modular_glm4.py b/src/transformers/models/glm4/modular_glm4.py index 1cf98ce159c..6025e8637fe 100644 --- a/src/transformers/models/glm4/modular_glm4.py +++ b/src/transformers/models/glm4/modular_glm4.py @@ -13,7 +13,7 @@ # 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. -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch.utils.checkpoint @@ -59,9 +59,9 @@ class Glm4DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -107,7 +107,7 @@ class Glm4ForCausalLM(GlmForCausalLM): def forward( self, **super_kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/glpn/configuration_glpn.py b/src/transformers/models/glpn/configuration_glpn.py index 19eb04b19b8..6fb35bb0b08 100644 --- a/src/transformers/models/glpn/configuration_glpn.py +++ b/src/transformers/models/glpn/configuration_glpn.py @@ -36,19 +36,19 @@ class GLPNConfig(PretrainedConfig): The number of input channels. num_encoder_blocks (`int`, *optional*, defaults to 4): The number of encoder blocks (i.e. stages in the Mix Transformer encoder). - depths (`List[int]`, *optional*, defaults to `[2, 2, 2, 2]`): + depths (`list[int]`, *optional*, defaults to `[2, 2, 2, 2]`): The number of layers in each encoder block. - sr_ratios (`List[int]`, *optional*, defaults to `[8, 4, 2, 1]`): + sr_ratios (`list[int]`, *optional*, defaults to `[8, 4, 2, 1]`): Sequence reduction ratios in each encoder block. - hidden_sizes (`List[int]`, *optional*, defaults to `[32, 64, 160, 256]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[32, 64, 160, 256]`): Dimension of each of the encoder blocks. - patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3, 3]`): + patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3, 3]`): Patch size before each encoder block. - strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + strides (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Stride before each encoder block. - num_attention_heads (`List[int]`, *optional*, defaults to `[1, 2, 5, 8]`): + num_attention_heads (`list[int]`, *optional*, defaults to `[1, 2, 5, 8]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - mlp_ratios (`List[int]`, *optional*, defaults to `[4, 4, 4, 4]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[4, 4, 4, 4]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): diff --git a/src/transformers/models/glpn/image_processing_glpn.py b/src/transformers/models/glpn/image_processing_glpn.py index 60f339e0f13..958230a307f 100644 --- a/src/transformers/models/glpn/image_processing_glpn.py +++ b/src/transformers/models/glpn/image_processing_glpn.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for GLPN.""" -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...utils.import_utils import requires @@ -137,7 +137,7 @@ class GLPNImageProcessor(BaseImageProcessor): @filter_out_non_signature_kwargs() def preprocess( self, - images: Union["PIL.Image.Image", TensorType, List["PIL.Image.Image"], List[TensorType]], + images: Union["PIL.Image.Image", TensorType, list["PIL.Image.Image"], list[TensorType]], do_resize: Optional[bool] = None, size_divisor: Optional[int] = None, resample=None, @@ -150,7 +150,7 @@ class GLPNImageProcessor(BaseImageProcessor): Preprocess the given images. Args: - images (`PIL.Image.Image` or `TensorType` or `List[np.ndarray]` or `List[TensorType]`): + images (`PIL.Image.Image` or `TensorType` or `list[np.ndarray]` or `list[TensorType]`): Images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_normalize=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): @@ -234,8 +234,8 @@ class GLPNImageProcessor(BaseImageProcessor): def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - ) -> List[Dict[str, TensorType]]: + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + ) -> list[dict[str, TensorType]]: """ Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images. Only supports PyTorch. @@ -243,12 +243,12 @@ class GLPNImageProcessor(BaseImageProcessor): Args: outputs ([`DepthEstimatorOutput`]): Raw outputs of the model. - target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ requires_backends(self, "torch") diff --git a/src/transformers/models/glpn/modeling_glpn.py b/src/transformers/models/glpn/modeling_glpn.py index e23b86f0764..299eef53d1c 100755 --- a/src/transformers/models/glpn/modeling_glpn.py +++ b/src/transformers/models/glpn/modeling_glpn.py @@ -15,7 +15,7 @@ """PyTorch GLPN model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -453,7 +453,7 @@ class GLPNModel(GLPNPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -557,7 +557,7 @@ class GLPNDecoder(nn.Module): self.final_upsample = nn.Upsample(scale_factor=2, mode="bilinear", align_corners=False) - def forward(self, hidden_states: List[torch.Tensor]) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor]) -> list[torch.Tensor]: stage_hidden_states = [] stage_hidden_state = None for hidden_state, stage in zip(hidden_states[::-1], self.stages): @@ -603,7 +603,7 @@ class GLPNDepthEstimationHead(nn.Module): nn.Conv2d(channels, 1, kernel_size=3, stride=1, padding=1), ) - def forward(self, hidden_states: List[torch.Tensor]) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor]) -> torch.Tensor: # use last features of the decoder hidden_states = hidden_states[self.config.head_in_index] @@ -639,7 +639,7 @@ class GLPNForDepthEstimation(GLPNPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" labels (`torch.FloatTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth depth estimation maps for computing the loss. diff --git a/src/transformers/models/got_ocr2/configuration_got_ocr2.py b/src/transformers/models/got_ocr2/configuration_got_ocr2.py index 3e7dd7561f6..1c7a0de0c78 100644 --- a/src/transformers/models/got_ocr2/configuration_got_ocr2.py +++ b/src/transformers/models/got_ocr2/configuration_got_ocr2.py @@ -65,7 +65,7 @@ class GotOcr2VisionConfig(PretrainedConfig): Whether to use relative position embedding. window_size (`int`, *optional*, defaults to 14): Window size for relative position. - global_attn_indexes (`List[int]`, *optional*, defaults to `[2, 5, 8, 11]`): + global_attn_indexes (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`): The indexes of the global attention layers. mlp_dim (`int`, *optional*, defaults to 3072): The dimensionality of the MLP layer in the Transformer encoder. diff --git a/src/transformers/models/got_ocr2/convert_got_ocr2_weights_to_hf.py b/src/transformers/models/got_ocr2/convert_got_ocr2_weights_to_hf.py index 2bf4b3ac7d0..9cf873a2756 100644 --- a/src/transformers/models/got_ocr2/convert_got_ocr2_weights_to_hf.py +++ b/src/transformers/models/got_ocr2/convert_got_ocr2_weights_to_hf.py @@ -16,7 +16,7 @@ import argparse import gc import glob import os -from typing import List, Optional +from typing import Optional import regex as re import torch @@ -161,7 +161,7 @@ class GotOcr2Converter(TikTokenConverter): def __init__( self, vocab_file, - special_tokens: List[str], + special_tokens: list[str], pattern: str, model_max_length: int, chat_template: Optional[str] = None, diff --git a/src/transformers/models/got_ocr2/image_processing_got_ocr2.py b/src/transformers/models/got_ocr2/image_processing_got_ocr2.py index d706c0f3403..f2698310edb 100644 --- a/src/transformers/models/got_ocr2/image_processing_got_ocr2.py +++ b/src/transformers/models/got_ocr2/image_processing_got_ocr2.py @@ -15,7 +15,7 @@ """Image processor class for Got-OCR-2.""" from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -50,7 +50,7 @@ logger = logging.get_logger(__name__) # Similar to image_processing_mllama.get_all_supported_aspect_ratios @lru_cache(maxsize=10) -def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> List[Tuple[int, int]]: +def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) -> list[tuple[int, int]]: """ Computes all allowed aspect ratios for a given minimum and maximum number of input tiles. @@ -65,7 +65,7 @@ def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) The maximum number of tiles allowed. Returns: - `List[Tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) + `list[tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) configuration in terms of number of tiles. Example: @@ -86,11 +86,11 @@ def get_all_supported_aspect_ratios(min_image_tiles: int, max_image_tiles: int) @lru_cache(maxsize=100) def get_optimal_tiled_canvas( - original_image_size: Tuple[int, int], - target_tile_size: Tuple[int, int], + original_image_size: tuple[int, int], + target_tile_size: tuple[int, int], min_image_tiles: int, max_image_tiles: int, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Given a minimum and maximum number of tiles, find the canvas with the closest aspect ratio to the original image aspect ratio. @@ -155,11 +155,11 @@ class GotOcr2ImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -172,7 +172,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_to_patches: bool = False, min_patches: int = 1, max_patches: int = 12, @@ -180,8 +180,8 @@ class GotOcr2ImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -205,7 +205,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -217,7 +217,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -255,7 +255,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_to_patches: Optional[bool] = None, min_patches: Optional[int] = None, max_patches: Optional[int] = None, @@ -263,8 +263,8 @@ class GotOcr2ImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -279,7 +279,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -300,9 +300,9 @@ class GotOcr2ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. @@ -419,7 +419,7 @@ class GotOcr2ImageProcessor(BaseImageProcessor): min_patches: int, max_patches: int, use_thumbnail: bool = True, - patch_size: Optional[Union[Tuple, int, dict]] = None, + patch_size: Optional[Union[tuple, int, dict]] = None, data_format: ChannelDimension = None, ): """ @@ -437,13 +437,13 @@ class GotOcr2ImageProcessor(BaseImageProcessor): The maximum number of patches to be extracted from the image. use_thumbnail (`bool`, *optional*, defaults to `True`): Whether to add a thumbnail image to the list of cropped patches. - patch_size (`int`, `Tuple[int, int]`, `dict`, *optional*): + patch_size (`int`, `tuple[int, int]`, `dict`, *optional*): The size of the output patches. data_format (`ChannelDimension`, *optional*): The format of the image data. If `None`, the format is inferred from the input image. Returns: - List[`PIL.Image.Image`] or List[np.ndarray]: The list of cropped images. + list[`PIL.Image.Image`] or list[np.ndarray]: The list of cropped images. """ if data_format is None: data_format = infer_channel_dimension_format(images) diff --git a/src/transformers/models/got_ocr2/image_processing_got_ocr2_fast.py b/src/transformers/models/got_ocr2/image_processing_got_ocr2_fast.py index 95179d7a94c..333d609cd22 100644 --- a/src/transformers/models/got_ocr2/image_processing_got_ocr2_fast.py +++ b/src/transformers/models/got_ocr2/image_processing_got_ocr2_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for Got-OCR-2.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -97,7 +97,7 @@ class GotOcr2ImageProcessorFast(BaseImageProcessorFast): min_patches: int, max_patches: int, use_thumbnail: bool = True, - patch_size: Optional[Union[Tuple, int, dict]] = None, + patch_size: Optional[Union[tuple, int, dict]] = None, interpolation: Optional["F.InterpolationMode"] = None, ): """ @@ -115,12 +115,12 @@ class GotOcr2ImageProcessorFast(BaseImageProcessorFast): The maximum number of patches to be extracted from the image. use_thumbnail (`bool`, *optional*, defaults to `True`): Whether to add a thumbnail image to the list of cropped patches. - patch_size (`int`, `Tuple[int, int]`, `dict`, *optional*): + patch_size (`int`, `tuple[int, int]`, `dict`, *optional*): The size of the output patches. The format of the image data. If `None`, the format is inferred from the input image. Returns: - List[`PIL.Image.Image`] or List[np.ndarray]: The list of cropped images. + list[`PIL.Image.Image`] or list[np.ndarray]: The list of cropped images. """ patch_size_height, patch_size_width = patch_size.height, patch_size.width original_height, original_width = images.shape[-2:] @@ -163,7 +163,7 @@ class GotOcr2ImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, crop_to_patches: bool, @@ -175,8 +175,8 @@ class GotOcr2ImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: if crop_to_patches: diff --git a/src/transformers/models/got_ocr2/modeling_got_ocr2.py b/src/transformers/models/got_ocr2/modeling_got_ocr2.py index 044013bed45..bcf30f58525 100644 --- a/src/transformers/models/got_ocr2/modeling_got_ocr2.py +++ b/src/transformers/models/got_ocr2/modeling_got_ocr2.py @@ -22,7 +22,7 @@ import collections from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -118,8 +118,8 @@ class GotOcr2VisionAttention(nn.Module): query: torch.Tensor, rel_pos_h: torch.Tensor, rel_pos_w: torch.Tensor, - q_size: Tuple[int, int], - k_size: Tuple[int, int], + q_size: tuple[int, int], + k_size: tuple[int, int], ) -> torch.Tensor: """ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`. @@ -201,7 +201,7 @@ class GotOcr2VisionLayer(nn.Module): self.mlp = GotOcr2MLPBlock(config) self.window_size = window_size - def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> Tuple[torch.Tensor, Tuple[int, int]]: + def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> tuple[torch.Tensor, tuple[int, int]]: """ Args: Partition into non-overlapping windows with padding if needed. @@ -226,7 +226,7 @@ class GotOcr2VisionLayer(nn.Module): return windows, (pad_height, pad_width) def window_unpartition( - self, windows: torch.Tensor, window_size: int, padding_shape: Tuple[int, int], original_shape: Tuple[int, int] + self, windows: torch.Tensor, window_size: int, padding_shape: tuple[int, int], original_shape: tuple[int, int] ) -> torch.Tensor: """ Args: @@ -259,7 +259,7 @@ class GotOcr2VisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) @@ -313,8 +313,8 @@ class GotOcr2VisionEncoderOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None class GotOcr2PatchEmbeddings(nn.Module): @@ -543,9 +543,9 @@ class GotOcr2CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -660,7 +660,7 @@ class GotOcr2Model(GotOcr2PreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -668,7 +668,7 @@ class GotOcr2Model(GotOcr2PreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, GotOcr2ModelOutputWithPast]: + ) -> Union[tuple, GotOcr2ModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -765,7 +765,7 @@ class GotOcr2ForConditionalGeneration(GotOcr2PreTrainedModel, GenerationMixin): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -797,7 +797,7 @@ class GotOcr2ForConditionalGeneration(GotOcr2PreTrainedModel, GenerationMixin): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -807,7 +807,7 @@ class GotOcr2ForConditionalGeneration(GotOcr2PreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, GotOcr2CausalLMOutputWithPast]: + ) -> Union[tuple, GotOcr2CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/got_ocr2/modular_got_ocr2.py b/src/transformers/models/got_ocr2/modular_got_ocr2.py index 80a2a23c2f1..d8fdd7249f4 100644 --- a/src/transformers/models/got_ocr2/modular_got_ocr2.py +++ b/src/transformers/models/got_ocr2/modular_got_ocr2.py @@ -14,7 +14,7 @@ # limitations under the License. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -84,7 +84,7 @@ class GotOcr2VisionConfig(PretrainedConfig): Whether to use relative position embedding. window_size (`int`, *optional*, defaults to 14): Window size for relative position. - global_attn_indexes (`List[int]`, *optional*, defaults to `[2, 5, 8, 11]`): + global_attn_indexes (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`): The indexes of the global attention layers. mlp_dim (`int`, *optional*, defaults to 3072): The dimensionality of the MLP layer in the Transformer encoder. @@ -324,7 +324,7 @@ class GotOcr2Model(LlavaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -332,7 +332,7 @@ class GotOcr2Model(LlavaModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, GotOcr2ModelOutputWithPast]: + ) -> Union[tuple, GotOcr2ModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -394,7 +394,7 @@ class GotOcr2ForConditionalGeneration(LlavaForConditionalGeneration): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -404,7 +404,7 @@ class GotOcr2ForConditionalGeneration(LlavaForConditionalGeneration): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, GotOcr2CausalLMOutputWithPast]: + ) -> Union[tuple, GotOcr2CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/got_ocr2/processing_got_ocr2.py b/src/transformers/models/got_ocr2/processing_got_ocr2.py index b712245a64c..dd1d2e61687 100644 --- a/src/transformers/models/got_ocr2/processing_got_ocr2.py +++ b/src/transformers/models/got_ocr2/processing_got_ocr2.py @@ -14,7 +14,7 @@ # limitations under the License. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -37,7 +37,7 @@ class GotOcr2TextKwargs(TextKwargs, total=False): class GotOcr2ImagesKwargs(ImagesKwargs, total=False): - box: Optional[Union[List, Tuple[float, float], Tuple[float, float, float, float]]] + box: Optional[Union[list, tuple[float, float], tuple[float, float, float, float]]] color: Optional[str] num_image_tokens: Optional[int] multi_page: Optional[bool] @@ -64,7 +64,7 @@ class GotOcr2ProcessorKwargs(ProcessingKwargs, total=False): } -def preprocess_box_annotation(box: Union[List, Tuple], image_size: Tuple[int, int]) -> List: +def preprocess_box_annotation(box: Union[list, tuple], image_size: tuple[int, int]) -> list: """ Convert box annotation to the format [x1, y1, x2, y2] in the range [0, 1000]. """ @@ -135,7 +135,7 @@ class GotOcr2Processor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[GotOcr2ProcessorKwargs], @@ -148,16 +148,16 @@ class GotOcr2Processor(ProcessorMixin): GotOcr2ImageProcessor's [`~GotOcr2ImageProcessor.__call__`] if `images` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). format (`bool`, *optional*): If set, will add the format token to the query, and the model will return the OCR result with formatting. - box (`List[float]`, `List[Tuple[float, float]]`, `List[Tuple[float, float, float, float]]`, *optional*): + box (`list[float]`, `list[tuple[float, float]]`, `list[tuple[float, float, float, float]]`, *optional*): The box annotation to be added to the query. If a list of floats or a tuple of floats is provided, it will be interpreted as [x1, y1, x2, y2]. If a list of tuples is provided, each tuple should be in the form (x1, y1, x2, y2). diff --git a/src/transformers/models/gpt2/configuration_gpt2.py b/src/transformers/models/gpt2/configuration_gpt2.py index ea06a002290..db5151a2ba1 100644 --- a/src/transformers/models/gpt2/configuration_gpt2.py +++ b/src/transformers/models/gpt2/configuration_gpt2.py @@ -17,7 +17,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import Any, List, Optional +from typing import Any, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig @@ -195,7 +195,7 @@ class GPT2OnnxConfig(OnnxConfigWithPast): self, config: PretrainedConfig, task: str = "default", - patching_specs: Optional[List[PatchingSpec]] = None, + patching_specs: Optional[list[PatchingSpec]] = None, use_past: bool = False, ): super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past) diff --git a/src/transformers/models/gpt2/modeling_flax_gpt2.py b/src/transformers/models/gpt2/modeling_flax_gpt2.py index 1382a255612..8e419217c5a 100644 --- a/src/transformers/models/gpt2/modeling_flax_gpt2.py +++ b/src/transformers/models/gpt2/modeling_flax_gpt2.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Optional, Tuple +from typing import Any, Optional import flax.linen as nn import jax @@ -93,7 +93,7 @@ GPT2_INPUTS_DOCSTRING = r""" position_ids (`numpy.ndarray` of shape `(batch_size, input_ids_length)`, *optional*): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -390,7 +390,7 @@ class FlaxGPT2PreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: GPT2Config, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -399,7 +399,7 @@ class FlaxGPT2PreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/gpt2/modeling_gpt2.py b/src/transformers/models/gpt2/modeling_gpt2.py index 4b3853b4369..584d21c4108 100644 --- a/src/transformers/models/gpt2/modeling_gpt2.py +++ b/src/transformers/models/gpt2/modeling_gpt2.py @@ -19,7 +19,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -268,7 +268,7 @@ class GPT2Attention(nn.Module): @deprecate_kwarg("layer_past", new_name="past_key_value", version="4.53.0", raise_if_both_names=True) def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], + hidden_states: Optional[tuple[torch.FloatTensor]], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, @@ -277,7 +277,7 @@ class GPT2Attention(nn.Module): encoder_attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, **kwargs, - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]], ...]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]], ...]: is_cross_attention = encoder_hidden_states is not None if is_cross_attention: if not hasattr(self, "q_attn"): @@ -360,7 +360,7 @@ class GPT2MLP(nn.Module): self.act = ACT2FN[config.activation_function] self.dropout = nn.Dropout(config.resid_pdrop) - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -387,7 +387,7 @@ class GPT2Block(nn.Module): @deprecate_kwarg("layer_past", new_name="past_key_value", version="4.53.0", raise_if_both_names=True) def forward( self, - hidden_states: Optional[Tuple[torch.FloatTensor]], + hidden_states: Optional[tuple[torch.FloatTensor]], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, @@ -397,7 +397,7 @@ class GPT2Block(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, **kwargs, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_output, self_attn_weights = self.attn( @@ -609,7 +609,7 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). mc_logits (`torch.FloatTensor` of shape `(batch_size, num_choices)`): Prediction scores of the multiple choice classification head (scores for each choice before SoftMax). - past_key_values (`Tuple[Tuple[torch.Tensor]]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`tuple[tuple[torch.Tensor]]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): Tuple of length `config.n_layers`, containing tuples of tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -632,9 +632,9 @@ class GPT2DoubleHeadsModelOutput(ModelOutput): mc_loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None mc_logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None PARALLELIZE_DOCSTRING = r""" @@ -644,7 +644,7 @@ PARALLELIZE_DOCSTRING = r""" it will evenly distribute blocks across all devices. Args: - device_map (`Dict[int, list]`, *optional*): + device_map (`dict[int, list]`, *optional*): A dictionary that maps attention modules to devices. Note that the embedding module and LMHead are always automatically mapped to the first device (for esoteric reasons). That means that the first device should have fewer attention modules mapped to it than other devices. For reference, the gpt2 models have the @@ -775,7 +775,7 @@ class GPT2Model(GPT2PreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Tuple[Tuple[torch.Tensor]], Cache]] = None, + past_key_values: Optional[Union[tuple[tuple[torch.Tensor]], Cache]] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, @@ -789,7 +789,7 @@ class GPT2Model(GPT2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1171,7 +1171,7 @@ class GPT2LMHeadModel(GPT2PreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, @@ -1186,7 +1186,7 @@ class GPT2LMHeadModel(GPT2PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1324,7 +1324,7 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, @@ -1339,7 +1339,7 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, GPT2DoubleHeadsModelOutput]: + ) -> Union[tuple, GPT2DoubleHeadsModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1446,8 +1446,8 @@ class GPT2DoubleHeadsModel(GPT2PreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1491,7 +1491,7 @@ class GPT2ForSequenceClassification(GPT2PreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -1502,7 +1502,7 @@ class GPT2ForSequenceClassification(GPT2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1624,7 +1624,7 @@ class GPT2ForTokenClassification(GPT2PreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -1635,7 +1635,7 @@ class GPT2ForTokenClassification(GPT2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1721,7 +1721,7 @@ class GPT2ForQuestionAnswering(GPT2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else diff --git a/src/transformers/models/gpt2/modeling_tf_gpt2.py b/src/transformers/models/gpt2/modeling_tf_gpt2.py index 03b16eead54..39eac39497a 100644 --- a/src/transformers/models/gpt2/modeling_tf_gpt2.py +++ b/src/transformers/models/gpt2/modeling_tf_gpt2.py @@ -18,7 +18,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -396,7 +396,7 @@ class TFGPT2MainLayer(keras.layers.Layer): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -409,7 +409,7 @@ class TFGPT2MainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -609,7 +609,7 @@ class TFGPT2DoubleHeadsModelOutput(ModelOutput): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). mc_logits (`tf.Tensor` of shape `(batch_size, num_choices)`): Prediction scores of the multiple choice classification head (scores for each choice before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -630,9 +630,9 @@ class TFGPT2DoubleHeadsModelOutput(ModelOutput): logits: Optional[tf.Tensor] = None mc_logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None GPT2_START_DOCSTRING = r""" @@ -690,7 +690,7 @@ GPT2_INPUTS_DOCSTRING = r""" [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`List[tf.Tensor]` of length `config.n_layers`): + past_key_values (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see `past_key_values` output below). Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -764,7 +764,7 @@ class TFGPT2Model(TFGPT2PreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -777,7 +777,7 @@ class TFGPT2Model(TFGPT2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -789,7 +789,7 @@ class TFGPT2Model(TFGPT2PreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -880,7 +880,7 @@ class TFGPT2LMHeadModel(TFGPT2PreTrainedModel, TFCausalLanguageModelingLoss): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -894,7 +894,7 @@ class TFGPT2LMHeadModel(TFGPT2PreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -906,7 +906,7 @@ class TFGPT2LMHeadModel(TFGPT2PreTrainedModel, TFCausalLanguageModelingLoss): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -991,7 +991,7 @@ class TFGPT2DoubleHeadsModel(TFGPT2PreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -1003,7 +1003,7 @@ class TFGPT2DoubleHeadsModel(TFGPT2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFGPT2DoubleHeadsModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFGPT2DoubleHeadsModelOutput, tuple[tf.Tensor]]: r""" mc_token_ids (`tf.Tensor` or `Numpy array` of shape `(batch_size, num_choices)`, *optional*, default to index of the last token of the input): Index of the classification token in each input sequence. Selected in the range `[0, input_ids.size(-1) - @@ -1145,7 +1145,7 @@ class TFGPT2ForSequenceClassification(TFGPT2PreTrainedModel, TFSequenceClassific def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -1157,7 +1157,7 @@ class TFGPT2ForSequenceClassification(TFGPT2PreTrainedModel, TFSequenceClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutputWithPast, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/gpt2/tokenization_gpt2.py b/src/transformers/models/gpt2/tokenization_gpt2.py index 709bcec5b61..dc3dda8a0b1 100644 --- a/src/transformers/models/gpt2/tokenization_gpt2.py +++ b/src/transformers/models/gpt2/tokenization_gpt2.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -240,22 +240,22 @@ class GPT2Tokenizer(PreTrainedTokenizer): return output + bos_token_ids + token_ids_1 def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -295,7 +295,7 @@ class GPT2Tokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/gpt2/tokenization_gpt2_fast.py b/src/transformers/models/gpt2/tokenization_gpt2_fast.py index 81e67a818de..f81c155e864 100644 --- a/src/transformers/models/gpt2/tokenization_gpt2_fast.py +++ b/src/transformers/models/gpt2/tokenization_gpt2_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization classes for OpenAI GPT.""" -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils_base import BatchEncoding from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -125,7 +125,7 @@ class GPT2TokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/gpt2/tokenization_gpt2_tf.py b/src/transformers/models/gpt2/tokenization_gpt2_tf.py index 4a8454a8bc4..a1facd95708 100644 --- a/src/transformers/models/gpt2/tokenization_gpt2_tf.py +++ b/src/transformers/models/gpt2/tokenization_gpt2_tf.py @@ -1,5 +1,5 @@ import os -from typing import Dict, List, Optional, Union +from typing import Optional, Union import tensorflow as tf from tensorflow_text import pad_model_inputs @@ -26,14 +26,14 @@ class TFGPT2Tokenizer(keras.layers.Layer): straight from `tf.string` inputs to outputs. Args: - vocab (Dict[str, int]): Vocabulary dict for Byte Pair Tokenizer - merges (List[str]): Merges list for Byte Pair Tokenizer + vocab (dict[str, int]): Vocabulary dict for Byte Pair Tokenizer + merges (list[str]): Merges list for Byte Pair Tokenizer """ def __init__( self, - vocab: Dict[str, int], - merges: List[str], + vocab: dict[str, int], + merges: list[str], max_length: Optional[int] = None, pad_token_id: Optional[int] = None, ): diff --git a/src/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py b/src/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py index 7f40aabefb5..297c30cb06a 100644 --- a/src/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py +++ b/src/transformers/models/gpt_bigcode/modeling_gpt_bigcode.py @@ -14,7 +14,7 @@ """PyTorch GPTBigCode model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -216,8 +216,8 @@ class GPTBigCodeAttention(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ) -> Union[ - Tuple[torch.Tensor, Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[torch.Tensor], Tuple[torch.Tensor, ...]], + tuple[torch.Tensor, Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[torch.Tensor], tuple[torch.Tensor, ...]], ]: if encoder_hidden_states is not None: if not hasattr(self, "q_attn") or not self.is_cross_attention: @@ -291,8 +291,8 @@ class GPTBigCodeFlashAttention2(GPTBigCodeAttention): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ) -> Union[ - Tuple[torch.Tensor, Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[torch.Tensor], Tuple[torch.Tensor, ...]], + tuple[torch.Tensor, Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[torch.Tensor], tuple[torch.Tensor, ...]], ]: if encoder_hidden_states is not None: if not hasattr(self, "q_attn") or not self.is_cross_attention: @@ -469,8 +469,8 @@ class GPTBigCodeSdpaAttention(GPTBigCodeAttention): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, ) -> Union[ - Tuple[torch.Tensor, Optional[torch.Tensor]], - Tuple[torch.Tensor, Optional[torch.Tensor], Tuple[torch.Tensor, ...]], + tuple[torch.Tensor, Optional[torch.Tensor]], + tuple[torch.Tensor, Optional[torch.Tensor], tuple[torch.Tensor, ...]], ]: if encoder_hidden_states is not None: if not hasattr(self, "q_attn") or not self.is_cross_attention: @@ -538,7 +538,7 @@ class GPTBigCodeMLP(nn.Module): self.dropout = nn.Dropout(config.resid_pdrop) # Copied from transformers.models.gpt2.modeling_gpt2.GPT2MLP.forward - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -579,7 +579,7 @@ class GPTBigCodeBlock(nn.Module): def forward( self, - hidden_states: Optional[Tuple[torch.Tensor]], + hidden_states: Optional[tuple[torch.Tensor]], layer_past: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, @@ -589,7 +589,7 @@ class GPTBigCodeBlock(nn.Module): output_attentions: Optional[bool] = False, **kwargs, ) -> Union[ - Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, torch.Tensor, torch.Tensor] + tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor, torch.Tensor, torch.Tensor] ]: residual = hidden_states hidden_states = self.ln_1(hidden_states) @@ -720,7 +720,7 @@ class GPTBigCodeModel(GPTBigCodePreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -732,7 +732,7 @@ class GPTBigCodeModel(GPTBigCodePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.Tensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1038,7 +1038,7 @@ class GPTBigCodeForCausalLM(GPTBigCodePreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1052,7 +1052,7 @@ class GPTBigCodeForCausalLM(GPTBigCodePreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.Tensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1116,8 +1116,8 @@ class GPTBigCodeForCausalLM(GPTBigCodePreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1154,7 +1154,7 @@ class GPTBigCodeForSequenceClassification(GPTBigCodePreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1165,7 +1165,7 @@ class GPTBigCodeForSequenceClassification(GPTBigCodePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" input_ids (`torch.Tensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1285,7 +1285,7 @@ class GPTBigCodeForTokenClassification(GPTBigCodePreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1296,7 +1296,7 @@ class GPTBigCodeForTokenClassification(GPTBigCodePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_ids (`torch.Tensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else diff --git a/src/transformers/models/gpt_neo/modeling_flax_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_flax_gpt_neo.py index f7371d08b03..a6cdc50b359 100644 --- a/src/transformers/models/gpt_neo/modeling_flax_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_flax_gpt_neo.py @@ -14,7 +14,7 @@ # limitations under the License. from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -91,7 +91,7 @@ GPT_NEO_INPUTS_DOCSTRING = r""" position_ids (`numpy.ndarray` of shape `(batch_size, input_ids_length)`, *optional*): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -350,7 +350,7 @@ class FlaxGPTNeoPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: GPTNeoConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -359,7 +359,7 @@ class FlaxGPTNeoPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/gpt_neo/modeling_gpt_neo.py b/src/transformers/models/gpt_neo/modeling_gpt_neo.py index 95de9e82d5e..417eb11ab0f 100755 --- a/src/transformers/models/gpt_neo/modeling_gpt_neo.py +++ b/src/transformers/models/gpt_neo/modeling_gpt_neo.py @@ -15,7 +15,7 @@ """PyTorch GPT Neo model.""" import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -535,7 +535,7 @@ class GPTNeoModel(GPTNeoPreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -546,7 +546,7 @@ class GPTNeoModel(GPTNeoPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -835,7 +835,7 @@ class GPTNeoForCausalLM(GPTNeoPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -848,7 +848,7 @@ class GPTNeoForCausalLM(GPTNeoPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -920,8 +920,8 @@ class GPTNeoForCausalLM(GPTNeoPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PretrainedModel.beam_search`] or [`~PretrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -961,7 +961,7 @@ class GPTNeoForSequenceClassification(GPTNeoPreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[Cache, Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, tuple[torch.FloatTensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -972,7 +972,7 @@ class GPTNeoForSequenceClassification(GPTNeoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1084,7 +1084,7 @@ class GPTNeoForTokenClassification(GPTNeoPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -1095,7 +1095,7 @@ class GPTNeoForTokenClassification(GPTNeoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -1177,7 +1177,7 @@ class GPTNeoForQuestionAnswering(GPTNeoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else diff --git a/src/transformers/models/gpt_neox/configuration_gpt_neox.py b/src/transformers/models/gpt_neox/configuration_gpt_neox.py index c7855c07ea3..80323914db3 100644 --- a/src/transformers/models/gpt_neox/configuration_gpt_neox.py +++ b/src/transformers/models/gpt_neox/configuration_gpt_neox.py @@ -99,11 +99,11 @@ class GPTNeoXConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/gpt_neox/modeling_gpt_neox.py b/src/transformers/models/gpt_neox/modeling_gpt_neox.py index 16de0f23db9..6b08c27a306 100755 --- a/src/transformers/models/gpt_neox/modeling_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modeling_gpt_neox.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_gpt_neox.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -144,7 +144,7 @@ class GPTNeoXAttention(nn.Module): layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], ): input_shape = hidden_states.shape[:-1] @@ -211,7 +211,7 @@ class GPTNeoXLayer(nn.Module): layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], ): attn_output, attn_weights = self.attention( @@ -496,7 +496,7 @@ class GPTNeoXForCausalLM(GPTNeoXPreTrainedModel, GenerationMixin): position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -504,7 +504,7 @@ class GPTNeoXForCausalLM(GPTNeoXPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -593,7 +593,7 @@ class GPTNeoXForSequenceClassification(GPTNeoXPreTrainedModel): position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -669,7 +669,7 @@ class GPTNeoXForTokenClassification(GPTNeoXPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/gpt_neox/modular_gpt_neox.py b/src/transformers/models/gpt_neox/modular_gpt_neox.py index e7d67a97644..4922a4e3b4c 100644 --- a/src/transformers/models/gpt_neox/modular_gpt_neox.py +++ b/src/transformers/models/gpt_neox/modular_gpt_neox.py @@ -1,4 +1,4 @@ -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -131,7 +131,7 @@ class GPTNeoXAttention(nn.Module): layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], ): input_shape = hidden_states.shape[:-1] @@ -198,7 +198,7 @@ class GPTNeoXLayer(nn.Module): layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], ): attn_output, attn_weights = self.attention( @@ -443,7 +443,7 @@ class GPTNeoXForCausalLM(GPTNeoXPreTrainedModel, GenerationMixin): position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -451,7 +451,7 @@ class GPTNeoXForCausalLM(GPTNeoXPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -540,7 +540,7 @@ class GPTNeoXForSequenceClassification(GPTNeoXPreTrainedModel): position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -616,7 +616,7 @@ class GPTNeoXForTokenClassification(GPTNeoXPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py b/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py index 767a8a68f63..a3b190a60eb 100644 --- a/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py +++ b/src/transformers/models/gpt_neox/tokenization_gpt_neox_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization classes for GPTNeoX.""" -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -168,22 +168,22 @@ class GPTNeoXTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.llama.tokenization_llama.LlamaTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -216,7 +216,7 @@ class GPTNeoXTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/gpt_neox_japanese/configuration_gpt_neox_japanese.py b/src/transformers/models/gpt_neox_japanese/configuration_gpt_neox_japanese.py index 650c6124949..32015733453 100644 --- a/src/transformers/models/gpt_neox_japanese/configuration_gpt_neox_japanese.py +++ b/src/transformers/models/gpt_neox_japanese/configuration_gpt_neox_japanese.py @@ -85,11 +85,11 @@ class GPTNeoXJapaneseConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/gpt_neox_japanese/modeling_gpt_neox_japanese.py b/src/transformers/models/gpt_neox_japanese/modeling_gpt_neox_japanese.py index f4a073cc4a7..dbcb831ea21 100755 --- a/src/transformers/models/gpt_neox_japanese/modeling_gpt_neox_japanese.py +++ b/src/transformers/models/gpt_neox_japanese/modeling_gpt_neox_japanese.py @@ -15,7 +15,7 @@ """PyTorch GPTNeoX model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -105,7 +105,7 @@ class GPTNeoXJapaneseAttention(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC ): # Compute QKV # Attention heads [batch, seq_len, hidden_size] @@ -353,7 +353,7 @@ class GPTNeoXJapaneseLayer(nn.Module): layer_past: Optional[Cache] = None, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC ): residual = hidden_states ln_out = self.input_layernorm(hidden_states) @@ -424,13 +424,13 @@ class GPTNeoXJapaneseModel(GPTNeoXJapanesePreTrainedModel): position_ids: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" Example: @@ -698,7 +698,7 @@ class GPTNeoXJapaneseForCausalLM(GPTNeoXJapanesePreTrainedModel, GenerationMixin position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.FloatTensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.FloatTensor]]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -706,7 +706,7 @@ class GPTNeoXJapaneseForCausalLM(GPTNeoXJapanesePreTrainedModel, GenerationMixin return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in diff --git a/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py b/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py index 194fff7dd68..b65d9b53bee 100644 --- a/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py +++ b/src/transformers/models/gpt_neox_japanese/tokenization_gpt_neox_japanese.py @@ -19,7 +19,7 @@ import json import os import re import sys -from typing import Optional, Tuple +from typing import Optional import numpy as np @@ -162,7 +162,7 @@ class GPTNeoXJapaneseTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py index 37eaecd8447..3019acfd5bc 100644 --- a/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py +++ b/src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py @@ -4,7 +4,7 @@ import os import re import unicodedata from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece as spm @@ -97,7 +97,7 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): unk_token=None, eos_token=None, bos_token=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -187,7 +187,7 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): text = unicodedata.normalize("NFC", text) return text - def _tokenize(self, text: str, **kwargs) -> List[str]: + def _tokenize(self, text: str, **kwargs) -> list[str]: text = self.preprocess_text(text) return self.sp_model.encode(text, out_type=str) @@ -204,7 +204,7 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): """Returns the input string, this function is overridden to remove the default clean up.""" return out_string - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """Converts a sequence of tokens (strings) to a single string. Special tokens remain intact.""" current_sub_tokens = [] out_string = "" @@ -227,13 +227,13 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): return out_string # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.get_vocab - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab # Copied from transformers.models.albert.tokenization_albert.AlbertTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -251,8 +251,8 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): return (out_vocab_file,) def encode_fast( - self, text: Union[str, List[str]], return_tensors: Union[str, bool] = False - ) -> Union[List[int], List[List[int]], "torch.Tensor"]: + self, text: Union[str, list[str]], return_tensors: Union[str, bool] = False + ) -> Union[list[int], list[list[int]], "torch.Tensor"]: """ Encodes a text or batch of texts to token ids using preprocessing and the raw SP tokenizer. This has reduced functionality but is often much faster. @@ -264,11 +264,11 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): Use default HuggingFace tokenization methods for full functionality. Args: - text (`str` or `List[str]`): One or several text(s) to convert to token ids. + text (`str` or `list[str]`): One or several text(s) to convert to token ids. return_tensors (`str` or `bool`): Returns PyTorch tensors if set to True or "pt" Returns: - `List[int]`, `List[List[int]]`, or `torch.Tensor`: The encoded text(s) as token ids. + `list[int]`, `list[list[int]]`, or `torch.Tensor`: The encoded text(s) as token ids. """ if isinstance(text, str): @@ -283,13 +283,13 @@ class GPTSw3Tokenizer(PreTrainedTokenizer): return token_ids - def decode_fast(self, token_ids: Union[int, List[int]]) -> str: + def decode_fast(self, token_ids: Union[int, list[int]]) -> str: """ Encodes a text or batch of texts to token ids using preprocessing and the raw SP tokenizer. This has reduced functionality but is often much faster. Args: - token_ids (`int` or `List[int]`): Encoded token or text as token id(s). + token_ids (`int` or `list[int]`): Encoded token or text as token id(s). Returns: `str`: Decoded text diff --git a/src/transformers/models/gptj/configuration_gptj.py b/src/transformers/models/gptj/configuration_gptj.py index 3ca7e4e94fd..68c69099688 100644 --- a/src/transformers/models/gptj/configuration_gptj.py +++ b/src/transformers/models/gptj/configuration_gptj.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import Any, List, Optional +from typing import Any, Optional from ... import PreTrainedTokenizer, TensorType, is_torch_available from ...configuration_utils import PretrainedConfig @@ -141,7 +141,7 @@ class GPTJOnnxConfig(OnnxConfigWithPast): self, config: PretrainedConfig, task: str = "default", - patching_specs: Optional[List[PatchingSpec]] = None, + patching_specs: Optional[list[PatchingSpec]] = None, use_past: bool = False, ): super().__init__(config, task=task, patching_specs=patching_specs, use_past=use_past) diff --git a/src/transformers/models/gptj/modeling_flax_gptj.py b/src/transformers/models/gptj/modeling_flax_gptj.py index ee88f69cc18..12ea7a4fffb 100644 --- a/src/transformers/models/gptj/modeling_flax_gptj.py +++ b/src/transformers/models/gptj/modeling_flax_gptj.py @@ -14,7 +14,7 @@ # limitations under the License. from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -92,7 +92,7 @@ GPTJ_INPUTS_DOCSTRING = r""" position_ids (`numpy.ndarray` of shape `(batch_size, input_ids_length)`, *optional*): Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -369,7 +369,7 @@ class FlaxGPTJPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: GPTJConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -378,7 +378,7 @@ class FlaxGPTJPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/gptj/modeling_gptj.py b/src/transformers/models/gptj/modeling_gptj.py index 093daaef193..1e9c0ef5332 100644 --- a/src/transformers/models/gptj/modeling_gptj.py +++ b/src/transformers/models/gptj/modeling_gptj.py @@ -15,7 +15,7 @@ """PyTorch GPT-J model.""" import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.fx @@ -196,8 +196,8 @@ class GPTJAttention(nn.Module): output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, ) -> Union[ - Tuple[torch.Tensor, Tuple[torch.Tensor]], - Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], + tuple[torch.Tensor, tuple[torch.Tensor]], + Optional[tuple[torch.Tensor, tuple[torch.Tensor], tuple[torch.Tensor, ...]]], ]: query = self.q_proj(hidden_states) key = self.k_proj(hidden_states) @@ -286,8 +286,8 @@ class GPTJFlashAttention2(GPTJAttention): output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, ) -> Union[ - Tuple[torch.Tensor, Tuple[torch.Tensor]], - Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], + tuple[torch.Tensor, tuple[torch.Tensor]], + Optional[tuple[torch.Tensor, tuple[torch.Tensor], tuple[torch.Tensor, ...]]], ]: query = self.q_proj(hidden_states) key = self.k_proj(hidden_states) @@ -447,7 +447,7 @@ class GPTJBlock(nn.Module): use_cache: Optional[bool] = False, output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_outputs = self.attn( @@ -514,7 +514,7 @@ PARALLELIZE_DOCSTRING = r""" across all devices. Args: - device_map (`Dict[int, list]`, *optional*): + device_map (`dict[int, list]`, *optional*): A dictionary that maps attention modules to devices. Note that the embedding module and LMHead are always automatically mapped to the first device (for esoteric reasons). That means that the first device should have fewer attention modules mapped to it than other devices. For reference, the GPT-J models have the @@ -631,7 +631,7 @@ class GPTJModel(GPTJPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -642,7 +642,7 @@ class GPTJModel(GPTJPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This @@ -978,7 +978,7 @@ class GPTJForCausalLM(GPTJPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, Tuple[Tuple[torch.Tensor]]]] = None, + past_key_values: Optional[Union[Cache, tuple[tuple[torch.Tensor]]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -991,7 +991,7 @@ class GPTJForCausalLM(GPTJPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This @@ -1058,8 +1058,8 @@ class GPTJForCausalLM(GPTJPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PretrainedModel.beam_search`] or [`~PretrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -1103,7 +1103,7 @@ class GPTJForSequenceClassification(GPTJPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.FloatTensor] = None, token_type_ids: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, @@ -1114,7 +1114,7 @@ class GPTJForSequenceClassification(GPTJPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This @@ -1231,7 +1231,7 @@ class GPTJForQuestionAnswering(GPTJPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_dim)`, *optional*): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This diff --git a/src/transformers/models/gptj/modeling_tf_gptj.py b/src/transformers/models/gptj/modeling_tf_gptj.py index 403ca926edf..57a6496b01a 100644 --- a/src/transformers/models/gptj/modeling_tf_gptj.py +++ b/src/transformers/models/gptj/modeling_tf_gptj.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -170,7 +170,7 @@ class TFGPTJAttention(keras.layers.Layer): value: tf.Tensor, attention_mask: tf.Tensor | None = None, head_mask: tf.Tensor | None = None, - ) -> Tuple[tf.Tensor, tf.Tensor]: + ) -> tuple[tf.Tensor, tf.Tensor]: # compute causal mask from causal mask buffer query_length, key_length = shape_list(query)[-2], shape_list(key)[-2] causal_mask = self.get_causal_mask(key_length, query_length) @@ -203,7 +203,7 @@ class TFGPTJAttention(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, - layer_past: Optional[Tuple[tf.Tensor, tf.Tensor]] = None, + layer_past: Optional[tuple[tf.Tensor, tf.Tensor]] = None, attention_mask: tf.Tensor | None = None, position_ids: tf.Tensor | None = None, head_mask: tf.Tensor | None = None, @@ -428,7 +428,7 @@ class TFGPTJMainLayer(keras.layers.Layer): output_hidden_states=None, return_dict=None, training=False, - ) -> Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPast, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -624,7 +624,7 @@ GPTJ_INPUTS_DOCSTRING = r""" [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - past_key_values (`List[tf.Tensor]` of length `config.n_layers`): + past_key_values (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states (key and values in the attention blocks) as computed by the model (see `past` output below). Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as input ids as they have already been computed. @@ -694,7 +694,7 @@ class TFGPTJModel(TFGPTJPreTrainedModel): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -705,7 +705,7 @@ class TFGPTJModel(TFGPTJPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPast, tuple[tf.Tensor]]: r""" use_cache (`bool`, *optional*, defaults to `True`): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see @@ -794,7 +794,7 @@ class TFGPTJForCausalLM(TFGPTJPreTrainedModel, TFCausalLanguageModelingLoss): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -806,7 +806,7 @@ class TFGPTJForCausalLM(TFGPTJPreTrainedModel, TFCausalLanguageModelingLoss): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithPast, tuple[tf.Tensor]]: r""" labels (`np.ndarray` or `tf.Tensor` of shape `(batch_size, input_ids_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -902,7 +902,7 @@ class TFGPTJForSequenceClassification(TFGPTJPreTrainedModel, TFSequenceClassific def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -914,7 +914,7 @@ class TFGPTJForSequenceClassification(TFGPTJPreTrainedModel, TFSequenceClassific output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutputWithPast, tuple[tf.Tensor]]: r""" labels (`np.ndarray` or `tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1019,7 +1019,7 @@ class TFGPTJForQuestionAnswering(TFGPTJPreTrainedModel, TFQuestionAnsweringLoss) def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, @@ -1031,7 +1031,7 @@ class TFGPTJForQuestionAnswering(TFGPTJPreTrainedModel, TFQuestionAnsweringLoss) output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`np.ndarray` or `tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/granite/modeling_granite.py b/src/transformers/models/granite/modeling_granite.py index 11f2873f3df..d1d69f9579c 100644 --- a/src/transformers/models/granite/modeling_granite.py +++ b/src/transformers/models/granite/modeling_granite.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -143,12 +143,12 @@ class GraniteAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -241,9 +241,9 @@ class GraniteDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -259,7 +259,7 @@ class GraniteDecoderLayer(GradientCheckpointingLayer): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -528,7 +528,7 @@ class GraniteForCausalLM(GranitePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/granite/modular_granite.py b/src/transformers/models/granite/modular_granite.py index 33f3b3363e9..0432b9e2a5c 100644 --- a/src/transformers/models/granite/modular_granite.py +++ b/src/transformers/models/granite/modular_granite.py @@ -13,7 +13,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -61,9 +61,9 @@ class GraniteDecoderLayer(LlamaDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -79,7 +79,7 @@ class GraniteDecoderLayer(LlamaDecoderLayer): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -236,7 +236,7 @@ class GraniteForCausalLM(LlamaForCausalLM): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/granite_speech/modeling_granite_speech.py b/src/transformers/models/granite_speech/modeling_granite_speech.py index abcb3cfce2b..377904accbf 100644 --- a/src/transformers/models/granite_speech/modeling_granite_speech.py +++ b/src/transformers/models/granite_speech/modeling_granite_speech.py @@ -15,7 +15,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -63,9 +63,9 @@ class GraniteSpeechCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None ### Projector @@ -366,7 +366,7 @@ class GraniteSpeechForConditionalGeneration(GraniteSpeechPreTrainedModel, Genera input_features_mask: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -376,7 +376,7 @@ class GraniteSpeechForConditionalGeneration(GraniteSpeechPreTrainedModel, Genera cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **lm_kwargs, - ) -> Union[Tuple[torch.Tensor], GraniteSpeechCausalLMOutputWithPast]: + ) -> Union[tuple[torch.Tensor], GraniteSpeechCausalLMOutputWithPast]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, audio seq len, mel feat dim)): The tensors corresponding to the input audios. input features can be obtained using diff --git a/src/transformers/models/granite_speech/processing_granite_speech.py b/src/transformers/models/granite_speech/processing_granite_speech.py index 9032601a6b2..948cc8d4c47 100644 --- a/src/transformers/models/granite_speech/processing_granite_speech.py +++ b/src/transformers/models/granite_speech/processing_granite_speech.py @@ -14,7 +14,7 @@ # limitations under the License. """Processor class for Granite Speech.""" -from typing import List, Union +from typing import Union from ...feature_extraction_utils import BatchFeature from ...processing_utils import ProcessorMixin @@ -46,8 +46,8 @@ class GraniteSpeechProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - audio: Union["torch.Tensor", List["torch.Tensor"]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + audio: Union["torch.Tensor", list["torch.Tensor"]] = None, device: str = "cpu", images=None, videos=None, @@ -91,7 +91,7 @@ class GraniteSpeechProcessor(ProcessorMixin): text_inputs = self.tokenizer(prompt_strings, padding=True, **kwargs) return BatchFeature(data={**text_inputs, **audio_inputs}) - def _get_validated_text(self, text: Union[str, list]) -> List[str]: + def _get_validated_text(self, text: Union[str, list]) -> list[str]: if isinstance(text, str): return [text] elif isinstance(text, list) and isinstance(text[0], str): diff --git a/src/transformers/models/granitemoe/modeling_granitemoe.py b/src/transformers/models/granitemoe/modeling_granitemoe.py index 37519fe435a..ffaf86697a6 100644 --- a/src/transformers/models/granitemoe/modeling_granitemoe.py +++ b/src/transformers/models/granitemoe/modeling_granitemoe.py @@ -13,7 +13,7 @@ # 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. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -43,7 +43,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.jetmoe.modeling_jetmoe.load_balancing_loss_func def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -428,9 +428,9 @@ class GraniteMoeAttention(nn.Module): past_key_value: Optional[Cache] = None, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -521,9 +521,9 @@ class GraniteMoeDecoderLayer(GradientCheckpointingLayer): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, output_router_logits: Optional[bool] = False, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -542,7 +542,7 @@ class GraniteMoeDecoderLayer(GradientCheckpointingLayer): output_router_logits (`bool`, *optional*): Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -656,7 +656,7 @@ class GraniteMoeModel(GraniteMoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -664,7 +664,7 @@ class GraniteMoeModel(GraniteMoePreTrainedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -934,7 +934,7 @@ class GraniteMoeForCausalLM(GraniteMoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -945,7 +945,7 @@ class GraniteMoeForCausalLM(GraniteMoePreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + ) -> Union[tuple, MoeCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py index b1480085601..51785d48f4c 100644 --- a/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/modeling_granitemoehybrid.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -179,9 +179,9 @@ class GraniteMoeHybridAttention(nn.Module): past_key_value: Optional[Cache] = None, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -1078,9 +1078,9 @@ class GraniteMoeHybridDecoderLayer(GradientCheckpointingLayer): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, output_router_logits: Optional[bool] = False, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1099,7 +1099,7 @@ class GraniteMoeHybridDecoderLayer(GradientCheckpointingLayer): output_router_logits (`bool`, *optional*): Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -1273,7 +1273,7 @@ class GraniteMoeHybridModel(GraniteMoeHybridPreTrainedModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1281,7 +1281,7 @@ class GraniteMoeHybridModel(GraniteMoeHybridPreTrainedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1522,7 +1522,7 @@ class GraniteMoeHybridModel(GraniteMoeHybridPreTrainedModel): def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -1643,7 +1643,7 @@ class GraniteMoeHybridForCausalLM(GraniteMoeHybridPreTrainedModel, GenerationMix input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1654,7 +1654,7 @@ class GraniteMoeHybridForCausalLM(GraniteMoeHybridPreTrainedModel, GenerationMix cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + ) -> Union[tuple, MoeCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/granitemoehybrid/modular_granitemoehybrid.py b/src/transformers/models/granitemoehybrid/modular_granitemoehybrid.py index 160e0aa1bf3..b61c4ad61b8 100644 --- a/src/transformers/models/granitemoehybrid/modular_granitemoehybrid.py +++ b/src/transformers/models/granitemoehybrid/modular_granitemoehybrid.py @@ -13,7 +13,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -83,9 +83,9 @@ class GraniteMoeHybridDecoderLayer(GraniteMoeSharedDecoderLayer): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, output_router_logits: Optional[bool] = False, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -104,7 +104,7 @@ class GraniteMoeHybridDecoderLayer(GraniteMoeSharedDecoderLayer): output_router_logits (`bool`, *optional*): Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -198,7 +198,7 @@ class GraniteMoeHybridModel(GraniteMoeSharedModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -206,7 +206,7 @@ class GraniteMoeHybridModel(GraniteMoeSharedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py index da7ade3cf48..8cd13f8a8c5 100644 --- a/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py +++ b/src/transformers/models/granitemoeshared/modeling_granitemoeshared.py @@ -19,7 +19,7 @@ # 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. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -363,9 +363,9 @@ class GraniteMoeSharedAttention(nn.Module): past_key_value: Optional[Cache] = None, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # None or rope embeddings **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -430,9 +430,9 @@ class GraniteMoeSharedDecoderLayer(GradientCheckpointingLayer): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, output_router_logits: Optional[bool] = False, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -451,7 +451,7 @@ class GraniteMoeSharedDecoderLayer(GradientCheckpointingLayer): output_router_logits (`bool`, *optional*): Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -604,7 +604,7 @@ class GraniteMoeSharedModel(GraniteMoeSharedPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -612,7 +612,7 @@ class GraniteMoeSharedModel(GraniteMoeSharedPreTrainedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -841,7 +841,7 @@ class GraniteMoeSharedModel(GraniteMoeSharedPreTrainedModel): def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -962,7 +962,7 @@ class GraniteMoeSharedForCausalLM(GraniteMoeSharedPreTrainedModel, GenerationMix input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -973,7 +973,7 @@ class GraniteMoeSharedForCausalLM(GraniteMoeSharedPreTrainedModel, GenerationMix cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + ) -> Union[tuple, MoeCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/granitemoeshared/modular_granitemoeshared.py b/src/transformers/models/granitemoeshared/modular_granitemoeshared.py index b89775955b0..45dfa59ea7c 100644 --- a/src/transformers/models/granitemoeshared/modular_granitemoeshared.py +++ b/src/transformers/models/granitemoeshared/modular_granitemoeshared.py @@ -13,7 +13,7 @@ # 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. -from typing import Optional, Tuple +from typing import Optional import torch from torch import nn @@ -74,9 +74,9 @@ class GraniteMoeSharedDecoderLayer(GraniteMoeDecoderLayer): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, output_router_logits: Optional[bool] = False, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -95,7 +95,7 @@ class GraniteMoeSharedDecoderLayer(GraniteMoeDecoderLayer): output_router_logits (`bool`, *optional*): Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): diff --git a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py index f23be1626c6..cf18dd43a1a 100644 --- a/src/transformers/models/grounding_dino/image_processing_grounding_dino.py +++ b/src/transformers/models/grounding_dino/image_processing_grounding_dino.py @@ -18,7 +18,7 @@ import io import pathlib from collections import defaultdict from collections.abc import Iterable -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import numpy as np @@ -84,7 +84,7 @@ if TYPE_CHECKING: logger = logging.get_logger(__name__) # pylint: disable=invalid-name -AnnotationType = Dict[str, Union[int, str, List[Dict]]] +AnnotationType = dict[str, Union[int, str, list[dict]]] class AnnotationFormat(ExplicitEnum): @@ -96,12 +96,12 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationForma # Copied from transformers.models.detr.image_processing_detr.get_size_with_aspect_ratio -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -138,10 +138,10 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in # Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -150,7 +150,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -170,7 +170,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -240,7 +240,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.normalize_annotation -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -255,7 +255,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -264,8 +264,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -283,7 +283,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -291,7 +291,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -306,7 +306,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -434,11 +434,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.prepare_coco_panoptic_annotation with DETR->GroundingDino def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for GroundingDino. """ @@ -475,7 +475,7 @@ def prepare_coco_panoptic_annotation( # Copied from transformers.models.detr.image_processing_detr.get_segmentation_image def get_segmentation_image( - masks: np.ndarray, input_size: Tuple, target_size: Tuple, stuff_equiv_classes, deduplicate=False + masks: np.ndarray, input_size: tuple, target_size: tuple, stuff_equiv_classes, deduplicate=False ): h, w = input_size final_h, final_w = target_size @@ -500,7 +500,7 @@ def get_segmentation_image( # Copied from transformers.models.detr.image_processing_detr.get_mask_area -def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: int) -> np.ndarray: +def get_mask_area(seg_img: np.ndarray, target_size: tuple[int, int], n_classes: int) -> np.ndarray: final_h, final_w = target_size np_seg_img = seg_img.astype(np.uint8) np_seg_img = np_seg_img.reshape(final_h, final_w, 3) @@ -510,7 +510,7 @@ def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: # Copied from transformers.models.detr.image_processing_detr.score_labels_from_class_probabilities -def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def score_labels_from_class_probabilities(logits: np.ndarray) -> tuple[np.ndarray, np.ndarray]: probs = scipy.special.softmax(logits, axis=-1) labels = probs.argmax(-1, keepdims=True) scores = np.take_along_axis(probs, labels, axis=-1) @@ -523,11 +523,11 @@ def post_process_panoptic_sample( out_logits: np.ndarray, masks: np.ndarray, boxes: np.ndarray, - processed_size: Tuple[int, int], - target_size: Tuple[int, int], - is_thing_map: Dict, + processed_size: tuple[int, int], + target_size: tuple[int, int], + is_thing_map: dict, threshold=0.85, -) -> Dict: +) -> dict: """ Converts the output of [`DetrForSegmentation`] into panoptic segmentation predictions for a single sample. @@ -539,10 +539,10 @@ def post_process_panoptic_sample( boxes (`torch.Tensor`): The prediced bounding boxes for this sample. The boxes are in the normalized format `(center_x, center_y, width, height)` and values between `[0, 1]`, relative to the size the image (disregarding padding). - processed_size (`Tuple[int, int]`): + processed_size (`tuple[int, int]`): The processed size of the image `(height, width)`, as returned by the preprocessing step i.e. the size after data augmentation but before batching. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, `(height, width)` corresponding to the requested final size of the prediction. is_thing_map (`Dict`): @@ -606,9 +606,9 @@ def post_process_panoptic_sample( # Copied from transformers.models.detr.image_processing_detr.resize_annotation def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -616,11 +616,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -688,7 +688,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -719,7 +719,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -756,14 +756,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -777,7 +777,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -818,7 +818,7 @@ def _scale_boxes(boxes, target_sizes): Args: boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. - target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. Returns: @@ -849,7 +849,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -872,10 +872,10 @@ class GroundingDinoImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -887,7 +887,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -900,16 +900,16 @@ class GroundingDinoImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -968,7 +968,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): @classmethod # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.from_dict with Detr->GroundingDino - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `GroundingDinoImageProcessor.from_pretrained(checkpoint, size=600, @@ -985,12 +985,12 @@ class GroundingDinoImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into GroundingDino model. """ @@ -1018,7 +1018,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1031,7 +1031,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1090,7 +1090,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -1127,7 +1127,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -1137,12 +1137,12 @@ class GroundingDinoImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor._update_annotation_for_padded_image def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -1182,8 +1182,8 @@ class GroundingDinoImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1215,24 +1215,24 @@ class GroundingDinoImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.pad def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -1253,7 +1253,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1302,24 +1302,24 @@ class GroundingDinoImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, do_convert_annotations: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -1329,15 +1329,15 @@ class GroundingDinoImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -1346,7 +1346,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1368,9 +1368,9 @@ class GroundingDinoImageProcessor(BaseImageProcessor): Whether to convert the annotations to the format expected by the model. Converts the bounding boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)` and in relative coordinates. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_pad (`bool`, *optional*, defaults to self.do_pad): Whether to pad the image. If `True`, padding will be applied to the bottom and right of @@ -1391,7 +1391,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1568,7 +1568,7 @@ class GroundingDinoImageProcessor(BaseImageProcessor): self, outputs: "GroundingDinoObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`GroundingDinoForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -1579,12 +1579,12 @@ class GroundingDinoImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. diff --git a/src/transformers/models/grounding_dino/image_processing_grounding_dino_fast.py b/src/transformers/models/grounding_dino/image_processing_grounding_dino_fast.py index 775f648def1..93f68f415e9 100644 --- a/src/transformers/models/grounding_dino/image_processing_grounding_dino_fast.py +++ b/src/transformers/models/grounding_dino/image_processing_grounding_dino_fast.py @@ -5,7 +5,7 @@ # modular_grounding_dino.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import pathlib -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -72,7 +72,7 @@ class GroundingDinoFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -83,7 +83,7 @@ class GroundingDinoFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -96,7 +96,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int, device: to Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -240,11 +240,11 @@ def rgb_to_id(color): def prepare_coco_panoptic_annotation( image: torch.Tensor, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for GROUNDING_DINO. """ @@ -294,7 +294,7 @@ def _scale_boxes(boxes, target_sizes): Args: boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. - target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. Returns: @@ -357,7 +357,7 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `GroundingDinoImageProcessorFast.from_pretrained(checkpoint, size=600, @@ -373,12 +373,12 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into GROUNDING_DINO model. """ @@ -458,9 +458,9 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -468,11 +468,11 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -509,7 +509,7 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -526,12 +526,12 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -562,8 +562,8 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -593,20 +593,20 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[GroundingDinoFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -630,8 +630,8 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -641,10 +641,10 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -748,7 +748,7 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): self, outputs: "GroundingDinoObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`GroundingDinoForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -759,12 +759,12 @@ class GroundingDinoImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. diff --git a/src/transformers/models/grounding_dino/modeling_grounding_dino.py b/src/transformers/models/grounding_dino/modeling_grounding_dino.py index 27ac107f679..9603651fafc 100644 --- a/src/transformers/models/grounding_dino/modeling_grounding_dino.py +++ b/src/transformers/models/grounding_dino/modeling_grounding_dino.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -52,7 +52,7 @@ class MultiScaleDeformableAttention(nn.Module): self, value: Tensor, value_spatial_shapes: Tensor, - value_spatial_shapes_list: List[Tuple], + value_spatial_shapes_list: list[tuple], level_start_index: Tensor, sampling_locations: Tensor, attention_weights: Tensor, @@ -129,8 +129,8 @@ class GroundingDinoDecoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None @dataclass @@ -162,9 +162,9 @@ class GroundingDinoEncoderOutput(ModelOutput): last_hidden_state_vision: Optional[torch.FloatTensor] = None last_hidden_state_text: Optional[torch.FloatTensor] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - text_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + text_hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None @dataclass @@ -223,13 +223,13 @@ class GroundingDinoModelOutput(ModelOutput): init_reference_points: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None encoder_last_hidden_state_vision: Optional[torch.FloatTensor] = None encoder_last_hidden_state_text: Optional[torch.FloatTensor] = None - encoder_vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_text_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + encoder_vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_text_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None encoder_logits: Optional[torch.FloatTensor] = None @@ -255,7 +255,7 @@ class GroundingDinoObjectDetectionOutput(ModelOutput): values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). You can use [`~GroundingDinoProcessor.post_process_grounded_object_detection`] to retrieve the unnormalized bounding boxes. - auxiliary_outputs (`List[Dict]`, *optional*): + auxiliary_outputs (`list[Dict]`, *optional*): Optional, only returned when auxiliary losses are activated (i.e. `config.auxiliary_loss` is set to `True`) and labels are provided. It is a list of dictionaries containing the two above keys (`logits` and `pred_boxes`) for each decoder layer. @@ -307,21 +307,21 @@ class GroundingDinoObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None init_reference_points: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None encoder_last_hidden_state_vision: Optional[torch.FloatTensor] = None encoder_last_hidden_state_text: Optional[torch.FloatTensor] = None - encoder_vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_text_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + encoder_vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_text_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None encoder_logits: Optional[torch.FloatTensor] = None @@ -683,7 +683,7 @@ class GroundingDinoTextEnhancerLayer(nn.Module): hidden_states: torch.FloatTensor, attention_masks: Optional[torch.BoolTensor] = None, position_embeddings: Optional[torch.FloatTensor] = None, - ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: + ) -> tuple[torch.FloatTensor, torch.FloatTensor]: """Text self-attention to enhance projection of text features generated by the text encoder (AutoModel based on text_config) within GroundingDinoEncoderLayer @@ -776,7 +776,7 @@ class GroundingDinoBiMultiHeadAttention(nn.Module): text_features: torch.FloatTensor, vision_attention_mask: Optional[torch.BoolTensor] = None, text_attention_mask: Optional[torch.BoolTensor] = None, - ) -> Tuple[Tuple[torch.FloatTensor, torch.FloatTensor], Tuple[torch.FloatTensor, torch.FloatTensor]]: + ) -> tuple[tuple[torch.FloatTensor, torch.FloatTensor], tuple[torch.FloatTensor, torch.FloatTensor]]: """Image-to-text and text-to-image cross-attention Args: @@ -946,7 +946,7 @@ class GroundingDinoFusionLayer(nn.Module): text_features: torch.FloatTensor, attention_mask_vision: Optional[torch.BoolTensor] = None, attention_mask_text: Optional[torch.BoolTensor] = None, - ) -> Tuple[Tuple[torch.FloatTensor, torch.FloatTensor], Tuple[torch.FloatTensor, torch.FloatTensor]]: + ) -> tuple[tuple[torch.FloatTensor, torch.FloatTensor], tuple[torch.FloatTensor, torch.FloatTensor]]: """Image and text features fusion Args: @@ -1025,7 +1025,7 @@ class GroundingDinoDeformableLayer(nn.Module): Reference points. spatial_shapes (`torch.LongTensor`, *optional*): Spatial shapes of the backbone feature maps. - spatial_shapes_list (`List[Tuple[int, int]]`, *optional*): + spatial_shapes_list (`list[tuple[int, int]]`, *optional*): Spatial shapes of the backbone feature maps (but as list for export compatibility). level_start_index (`torch.LongTensor`, *optional*): Level start index. @@ -1145,7 +1145,7 @@ class GroundingDinoEncoderLayer(nn.Module): vision_features: Tensor, vision_position_embedding: Tensor, spatial_shapes: Tensor, - spatial_shapes_list: List[Tuple[int, int]], + spatial_shapes_list: list[tuple[int, int]], level_start_index: Tensor, key_padding_mask: Tensor, reference_points: Tensor, @@ -1223,7 +1223,7 @@ class GroundingDinoMultiheadAttention(nn.Module): values: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(queries)) key_layer = self.transpose_for_scores(self.key(keys)) value_layer = self.transpose_for_scores(self.value(values)) @@ -1528,7 +1528,7 @@ class GroundingDinoEncoder(GroundingDinoPreTrainedModel): vision_attention_mask: Tensor, vision_position_embedding: Tensor, spatial_shapes: Tensor, - spatial_shapes_list: List[Tuple[int, int]], + spatial_shapes_list: list[tuple[int, int]], level_start_index: Tensor, valid_ratios=None, text_features: Optional[Tensor] = None, @@ -1553,7 +1553,7 @@ class GroundingDinoEncoder(GroundingDinoPreTrainedModel): Position embeddings that are added to the queries and keys in each self-attention layer. spatial_shapes (`torch.LongTensor` of shape `(num_feature_levels, 2)`): Spatial shapes of each feature map. - spatial_shapes_list (`List[Tuple[int, int]]`): + spatial_shapes_list (`list[tuple[int, int]]`): Spatial shapes of each feature map (but as list for export compatibility). level_start_index (`torch.LongTensor` of shape `(num_feature_levels)`): Starting index of each feature map. @@ -1714,7 +1714,7 @@ class GroundingDinoDecoder(GroundingDinoPreTrainedModel): Reference point in range `[0, 1]`, top-left (0,0), bottom-right (1, 1), including padding area. spatial_shapes (`torch.FloatTensor` of shape `(num_feature_levels, 2)`): Spatial shapes of the feature maps. - spatial_shapes_list (`List[Tuple[int, int]]`): + spatial_shapes_list (`list[tuple[int, int]]`): Spatial shapes of the feature maps (but as list for export compatibility). level_start_index (`torch.LongTensor` of shape `(num_feature_levels)`, *optional*): Indexes for the start of each feature level. In range `[0, sequence_length]`. @@ -1886,7 +1886,7 @@ class GroundingDinoDecoder(GroundingDinoPreTrainedModel): SPECIAL_TOKENS = [101, 102, 1012, 1029] -def generate_masks_with_special_tokens_and_transfer_map(input_ids: torch.LongTensor) -> Tuple[Tensor, Tensor]: +def generate_masks_with_special_tokens_and_transfer_map(input_ids: torch.LongTensor) -> tuple[Tensor, Tensor]: """Generate attention mask between each pair of special tokens and positional ids. Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -2369,7 +2369,7 @@ class GroundingDinoMLPPredictionHead(nn.Module): return x -def build_label_maps(logits: torch.FloatTensor, input_ids: torch.LongTensor) -> Tuple[torch.FloatTensor]: +def build_label_maps(logits: torch.FloatTensor, input_ids: torch.LongTensor) -> tuple[torch.FloatTensor]: """ Computes a mapping between tokens and their corresponding labels, where `num_labels` is determined by the number of classes in the input prompt. The function identifies segments of tokens between specific delimiter tokens and generates label maps for those segments. @@ -2490,11 +2490,11 @@ class GroundingDinoForObjectDetection(GroundingDinoPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.LongTensor] = None, pixel_mask: Optional[torch.BoolTensor] = None, - encoder_outputs: Optional[Union[GroundingDinoEncoderOutput, Tuple]] = None, + encoder_outputs: Optional[Union[GroundingDinoEncoderOutput, tuple]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - labels: Optional[List[Dict[str, Union[torch.LongTensor, torch.FloatTensor]]]] = None, + labels: Optional[list[dict[str, Union[torch.LongTensor, torch.FloatTensor]]]] = None, ): r""" input_ids (`torch.LongTensor` of shape `(batch_size, text_sequence_length)`): @@ -2507,7 +2507,7 @@ class GroundingDinoForObjectDetection(GroundingDinoPreTrainedModel): 1]`: 0 corresponds to a `sentence A` token, 1 corresponds to a `sentence B` token [What are token type IDs?](../glossary#token-type-ids) - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/grounding_dino/modular_grounding_dino.py b/src/transformers/models/grounding_dino/modular_grounding_dino.py index 2aacd353239..7ebd170d428 100644 --- a/src/transformers/models/grounding_dino/modular_grounding_dino.py +++ b/src/transformers/models/grounding_dino/modular_grounding_dino.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from transformers.models.detr.image_processing_detr_fast import DetrImageProcessorFast @@ -27,7 +27,7 @@ def _scale_boxes(boxes, target_sizes): Args: boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. - target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. Returns: @@ -53,7 +53,7 @@ class GroundingDinoImageProcessorFast(DetrImageProcessorFast): self, outputs: "GroundingDinoObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`GroundingDinoForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -64,12 +64,12 @@ class GroundingDinoImageProcessorFast(DetrImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. diff --git a/src/transformers/models/grounding_dino/processing_grounding_dino.py b/src/transformers/models/grounding_dino/processing_grounding_dino.py index f21846ab189..5d97185d940 100644 --- a/src/transformers/models/grounding_dino/processing_grounding_dino.py +++ b/src/transformers/models/grounding_dino/processing_grounding_dino.py @@ -18,7 +18,7 @@ Processor class for Grounding DINO. import pathlib import warnings -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...image_processing_utils import BatchFeature from ...image_transforms import center_to_corners_format @@ -36,7 +36,7 @@ if TYPE_CHECKING: from .modeling_grounding_dino import GroundingDinoObjectDetectionOutput -AnnotationType = Dict[str, Union[int, str, List[Dict]]] +AnnotationType = dict[str, Union[int, str, list[dict]]] def get_phrases_from_posmap(posmaps, input_ids): @@ -74,7 +74,7 @@ def _is_list_of_candidate_labels(text) -> bool: return False -def _merge_candidate_labels_text(text: List[str]) -> str: +def _merge_candidate_labels_text(text: list[str]) -> str: """ Merge candidate labels text into a single string. Ensure all labels are lowercase. For example, ["A cat", "a dog"] -> "a cat. a dog." @@ -102,7 +102,7 @@ class DictWithDeprecationWarning(dict): class GroundingDinoImagesKwargs(ImagesKwargs, total=False): - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] return_segmentation_masks: Optional[bool] masks_path: Optional[Union[str, pathlib.Path]] do_convert_annotations: Optional[bool] @@ -152,7 +152,7 @@ class GroundingDinoProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[GroundingDinoProcessorKwargs], @@ -162,9 +162,9 @@ class GroundingDinoProcessor(ProcessorMixin): [`BertTokenizerFast.__call__`] to prepare text for the model. Args: - images (`ImageInput`, `List[ImageInput]`, *optional*): + images (`ImageInput`, `list[ImageInput]`, *optional*): The image or batch of images to be processed. The image might be either PIL image, numpy array or a torch tensor. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`, *optional*): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`, *optional*): Candidate labels to be detected on the image. The text might be one of the following: - A list of candidate labels (strings) to be detected on the image (e.g. ["a cat", "a dog"]). - A batch of candidate labels to be detected on the batch of images (e.g. [["a cat", "a dog"], ["a car", "a person"]]). @@ -246,8 +246,8 @@ class GroundingDinoProcessor(ProcessorMixin): input_ids: Optional[TensorType] = None, threshold: float = 0.25, text_threshold: float = 0.25, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, - text_labels: Optional[List[List[str]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, + text_labels: Optional[list[list[str]]] = None, ): """ Converts the raw output of [`GroundingDinoForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -262,16 +262,16 @@ class GroundingDinoProcessor(ProcessorMixin): Threshold to keep object detection predictions based on confidence score. text_threshold (`float`, *optional*, defaults to 0.25): Score threshold to keep text detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. - text_labels (`List[List[str]]`, *optional*): + text_labels (`list[list[str]]`, *optional*): List of candidate labels to be detected on each image. At the moment it's *NOT used*, but required to be in signature for the zero-shot object detection pipeline. Text labels are instead extracted from the `input_ids` tensor provided in `outputs`. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the + `list[Dict]`: A list of dictionaries, each dictionary containing the - **scores**: tensor of confidence scores for detected objects - **boxes**: tensor of bounding boxes in [x0, y0, x1, y1] format - **labels**: list of text labels for each detected object (will be replaced with integer ids in v4.51.0) @@ -291,7 +291,7 @@ class GroundingDinoProcessor(ProcessorMixin): # Convert from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/groupvit/configuration_groupvit.py b/src/transformers/models/groupvit/configuration_groupvit.py index 43a37684826..ffe8aa55004 100644 --- a/src/transformers/models/groupvit/configuration_groupvit.py +++ b/src/transformers/models/groupvit/configuration_groupvit.py @@ -138,11 +138,11 @@ class GroupViTVisionConfig(PretrainedConfig): Dimensionality of the encoder layers and the pooler layer. intermediate_size (`int`, *optional*, defaults to 1536): Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder. - depths (`List[int]`, *optional*, defaults to [6, 3, 3]): + depths (`list[int]`, *optional*, defaults to [6, 3, 3]): The number of layers in each encoder block. - num_group_tokens (`List[int]`, *optional*, defaults to [64, 8, 0]): + num_group_tokens (`list[int]`, *optional*, defaults to [64, 8, 0]): The number of group tokens for each stage. - num_output_groups (`List[int]`, *optional*, defaults to [64, 8, 8]): + num_output_groups (`list[int]`, *optional*, defaults to [64, 8, 8]): The number of output groups for each stage, 0 means no group. num_attention_heads (`int`, *optional*, defaults to 6): Number of attention heads for each attention layer in the Transformer encoder. diff --git a/src/transformers/models/groupvit/modeling_groupvit.py b/src/transformers/models/groupvit/modeling_groupvit.py index 69628249b3b..100fd2dd85f 100644 --- a/src/transformers/models/groupvit/modeling_groupvit.py +++ b/src/transformers/models/groupvit/modeling_groupvit.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import torch @@ -301,7 +301,7 @@ class GroupViTModelOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -316,7 +316,7 @@ class GroupViTPatchEmbeddings(nn.Module): def __init__( self, image_size: int = 224, - patch_size: Union[int, Tuple[int, int]] = 16, + patch_size: Union[int, tuple[int, int]] = 16, num_channels: int = 3, embed_dim: int = 768, ): @@ -514,7 +514,7 @@ class GroupViTStage(nn.Module): hidden_states: torch.Tensor, prev_group_token: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -613,7 +613,7 @@ class GroupViTAttention(nn.Module): causal_attention_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -707,7 +707,7 @@ class GroupViTEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -863,7 +863,7 @@ class GroupViTTextEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -958,7 +958,7 @@ class GroupViTTextTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1053,7 +1053,7 @@ class GroupViTTextModel(GroupViTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -1096,7 +1096,7 @@ class GroupViTVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1152,7 +1152,7 @@ class GroupViTVisionModel(GroupViTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -1336,7 +1336,7 @@ class GroupViTModel(GroupViTPreTrainedModel): output_hidden_states: Optional[bool] = None, output_segmentation: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, GroupViTModelOutput]: + ) -> Union[tuple, GroupViTModelOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. diff --git a/src/transformers/models/groupvit/modeling_tf_groupvit.py b/src/transformers/models/groupvit/modeling_tf_groupvit.py index a6b62ae70ca..2d107774a5a 100644 --- a/src/transformers/models/groupvit/modeling_tf_groupvit.py +++ b/src/transformers/models/groupvit/modeling_tf_groupvit.py @@ -19,7 +19,7 @@ from __future__ import annotations import collections.abc import math from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -189,7 +189,7 @@ def resize_attention_map(attentions: tf.Tensor, height: int, width: int, align_c return attentions -def get_grouping_from_attentions(attentions: Tuple[tf.Tensor], hw_shape: Tuple[int]) -> tf.Tensor: +def get_grouping_from_attentions(attentions: tuple[tf.Tensor], hw_shape: tuple[int]) -> tf.Tensor: """ Args: attentions (`tuple(tf.Tensor)`: tuple of attention maps returned by `TFGroupViTVisionTransformer` @@ -261,7 +261,7 @@ class TFGroupViTModelOutput(ModelOutput): text_model_output: TFBaseModelOutputWithPooling = None vision_model_output: TFBaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -761,7 +761,7 @@ class TFGroupViTStage(keras.layers.Layer): prev_group_token: tf.Tensor | None = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -903,7 +903,7 @@ class TFGroupViTAttention(keras.layers.Layer): output_attentions: Optional[bool] = None, encoder_hidden_states: Optional[tf.Tensor] = None, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """Input shape: Batch x Time x Channel""" batch_size = shape_list(hidden_states)[0] @@ -992,7 +992,7 @@ class TFGroupViTEncoderLayer(keras.layers.Layer): causal_attention_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1060,7 +1060,7 @@ class TFGroupViTTextEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: encoder_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -1180,7 +1180,7 @@ class TFGroupViTTextTransformer(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: input_shape = shape_list(input_ids) embedding_output = self.embeddings(input_ids=input_ids, position_ids=position_ids) @@ -1292,7 +1292,7 @@ class TFGroupViTVisionTransformer(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutputWithPooling]: + ) -> Union[tuple, TFBaseModelOutputWithPooling]: embedding_output = self.embeddings(pixel_values) encoder_outputs = self.encoder( @@ -1360,7 +1360,7 @@ class TFGroupViTTextMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if input_ids is None: raise ValueError("You have to specify input_ids") @@ -1411,7 +1411,7 @@ class TFGroupViTVisionMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if pixel_values is None: raise ValueError("You have to specify pixel_values") @@ -1588,7 +1588,7 @@ class TFGroupViTMainLayer(keras.layers.Layer): output_segmentation: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFGroupViTModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFGroupViTModelOutput, tuple[tf.Tensor]]: if input_ids is None: raise ValueError("You have to specify either input_ids") if pixel_values is None: @@ -1751,7 +1751,7 @@ GROUPVIT_START_DOCSTRING = r""" GROUPVIT_TEXT_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1788,7 +1788,7 @@ GROUPVIT_TEXT_INPUTS_DOCSTRING = r""" GROUPVIT_VISION_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]`, `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]`, `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details. output_attentions (`bool`, *optional*): @@ -1809,14 +1809,14 @@ GROUPVIT_VISION_INPUTS_DOCSTRING = r""" GROUPVIT_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and [`PreTrainedTokenizer.encode`] for details. [What are input IDs?](../glossary#input-ids) - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details. attention_mask (`np.ndarray` or `tf.Tensor` of shape `({0})`, *optional*): @@ -1871,7 +1871,7 @@ class TFGroupViTTextModel(TFGroupViTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -1930,7 +1930,7 @@ class TFGroupViTVisionModel(TFGroupViTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -2081,7 +2081,7 @@ class TFGroupViTModel(TFGroupViTPreTrainedModel): output_segmentation: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFGroupViTModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFGroupViTModelOutput, tuple[tf.Tensor]]: r""" Returns: diff --git a/src/transformers/models/helium/modeling_helium.py b/src/transformers/models/helium/modeling_helium.py index b9cb3bafc13..31d9f963049 100644 --- a/src/transformers/models/helium/modeling_helium.py +++ b/src/transformers/models/helium/modeling_helium.py @@ -20,7 +20,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import math -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -219,12 +219,12 @@ class HeliumAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -280,9 +280,9 @@ class HeliumDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/herbert/tokenization_herbert.py b/src/transformers/models/herbert/tokenization_herbert.py index 9ce5caa9f04..3025f3c8f6e 100644 --- a/src/transformers/models/herbert/tokenization_herbert.py +++ b/src/transformers/models/herbert/tokenization_herbert.py @@ -16,7 +16,7 @@ import json import os import re import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -509,8 +509,8 @@ class HerbertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM sequence has the following format: @@ -537,8 +537,8 @@ class HerbertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -565,7 +565,7 @@ class HerbertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] # Copied from transformers.models.xlm.tokenization_xlm.XLMTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/herbert/tokenization_herbert_fast.py b/src/transformers/models/herbert/tokenization_herbert_fast.py index fd13353146b..fdc24e3c6a6 100644 --- a/src/transformers/models/herbert/tokenization_herbert_fast.py +++ b/src/transformers/models/herbert/tokenization_herbert_fast.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging @@ -72,8 +72,8 @@ class HerbertTokenizerFast(PreTrainedTokenizerFast): ) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An HerBERT, like BERT sequence has the following format: @@ -99,8 +99,8 @@ class HerbertTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -125,7 +125,7 @@ class HerbertTokenizerFast(PreTrainedTokenizerFast): return [1] + ([0] * len(token_ids_0)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/hgnet_v2/configuration_hgnet_v2.py b/src/transformers/models/hgnet_v2/configuration_hgnet_v2.py index 80a59087395..09ce13dec29 100644 --- a/src/transformers/models/hgnet_v2/configuration_hgnet_v2.py +++ b/src/transformers/models/hgnet_v2/configuration_hgnet_v2.py @@ -39,49 +39,49 @@ class HGNetV2Config(BackboneConfigMixin, PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. hidden_act (`str`, *optional*, defaults to `"relu"`): The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - stem_channels (`List[int]`, *optional*, defaults to `[3, 32, 48]`): + stem_channels (`list[int]`, *optional*, defaults to `[3, 32, 48]`): Channel dimensions for the stem layers: - First number (3) is input image channels - Second number (32) is intermediate stem channels - Third number (48) is output stem channels - stage_in_channels (`List[int]`, *optional*, defaults to `[48, 128, 512, 1024]`): + stage_in_channels (`list[int]`, *optional*, defaults to `[48, 128, 512, 1024]`): Input channel dimensions for each stage of the backbone. This defines how many channels the input to each stage will have. - stage_mid_channels (`List[int]`, *optional*, defaults to `[48, 96, 192, 384]`): + stage_mid_channels (`list[int]`, *optional*, defaults to `[48, 96, 192, 384]`): Mid-channel dimensions for each stage of the backbone. This defines the number of channels used in the intermediate layers of each stage. - stage_out_channels (`List[int]`, *optional*, defaults to `[128, 512, 1024, 2048]`): + stage_out_channels (`list[int]`, *optional*, defaults to `[128, 512, 1024, 2048]`): Output channel dimensions for each stage of the backbone. This defines how many channels the output of each stage will have. - stage_num_blocks (`List[int]`, *optional*, defaults to `[1, 1, 3, 1]`): + stage_num_blocks (`list[int]`, *optional*, defaults to `[1, 1, 3, 1]`): Number of blocks to be used in each stage of the backbone. This controls the depth of each stage by specifying how many convolutional blocks to stack. - stage_downsample (`List[bool]`, *optional*, defaults to `[False, True, True, True]`): + stage_downsample (`list[bool]`, *optional*, defaults to `[False, True, True, True]`): Indicates whether to downsample the feature maps at each stage. If `True`, the spatial dimensions of the feature maps will be reduced. - stage_light_block (`List[bool]`, *optional*, defaults to `[False, False, True, True]`): + stage_light_block (`list[bool]`, *optional*, defaults to `[False, False, True, True]`): Indicates whether to use light blocks in each stage. Light blocks are a variant of convolutional blocks that may have fewer parameters. - stage_kernel_size (`List[int]`, *optional*, defaults to `[3, 3, 5, 5]`): + stage_kernel_size (`list[int]`, *optional*, defaults to `[3, 3, 5, 5]`): Kernel sizes for the convolutional layers in each stage. - stage_numb_of_layers (`List[int]`, *optional*, defaults to `[6, 6, 6, 6]`): + stage_numb_of_layers (`list[int]`, *optional*, defaults to `[6, 6, 6, 6]`): Number of layers to be used in each block of the stage. use_learnable_affine_block (`bool`, *optional*, defaults to `False`): Whether to use Learnable Affine Blocks (LAB) in the network. diff --git a/src/transformers/models/hgnet_v2/modular_hgnet_v2.py b/src/transformers/models/hgnet_v2/modular_hgnet_v2.py index 2b4c73a1a5a..dca1f875947 100644 --- a/src/transformers/models/hgnet_v2/modular_hgnet_v2.py +++ b/src/transformers/models/hgnet_v2/modular_hgnet_v2.py @@ -50,49 +50,49 @@ class HGNetV2Config(BackboneConfigMixin, PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. hidden_act (`str`, *optional*, defaults to `"relu"`): The non-linear activation function in each block. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - stem_channels (`List[int]`, *optional*, defaults to `[3, 32, 48]`): + stem_channels (`list[int]`, *optional*, defaults to `[3, 32, 48]`): Channel dimensions for the stem layers: - First number (3) is input image channels - Second number (32) is intermediate stem channels - Third number (48) is output stem channels - stage_in_channels (`List[int]`, *optional*, defaults to `[48, 128, 512, 1024]`): + stage_in_channels (`list[int]`, *optional*, defaults to `[48, 128, 512, 1024]`): Input channel dimensions for each stage of the backbone. This defines how many channels the input to each stage will have. - stage_mid_channels (`List[int]`, *optional*, defaults to `[48, 96, 192, 384]`): + stage_mid_channels (`list[int]`, *optional*, defaults to `[48, 96, 192, 384]`): Mid-channel dimensions for each stage of the backbone. This defines the number of channels used in the intermediate layers of each stage. - stage_out_channels (`List[int]`, *optional*, defaults to `[128, 512, 1024, 2048]`): + stage_out_channels (`list[int]`, *optional*, defaults to `[128, 512, 1024, 2048]`): Output channel dimensions for each stage of the backbone. This defines how many channels the output of each stage will have. - stage_num_blocks (`List[int]`, *optional*, defaults to `[1, 1, 3, 1]`): + stage_num_blocks (`list[int]`, *optional*, defaults to `[1, 1, 3, 1]`): Number of blocks to be used in each stage of the backbone. This controls the depth of each stage by specifying how many convolutional blocks to stack. - stage_downsample (`List[bool]`, *optional*, defaults to `[False, True, True, True]`): + stage_downsample (`list[bool]`, *optional*, defaults to `[False, True, True, True]`): Indicates whether to downsample the feature maps at each stage. If `True`, the spatial dimensions of the feature maps will be reduced. - stage_light_block (`List[bool]`, *optional*, defaults to `[False, False, True, True]`): + stage_light_block (`list[bool]`, *optional*, defaults to `[False, False, True, True]`): Indicates whether to use light blocks in each stage. Light blocks are a variant of convolutional blocks that may have fewer parameters. - stage_kernel_size (`List[int]`, *optional*, defaults to `[3, 3, 5, 5]`): + stage_kernel_size (`list[int]`, *optional*, defaults to `[3, 3, 5, 5]`): Kernel sizes for the convolutional layers in each stage. - stage_numb_of_layers (`List[int]`, *optional*, defaults to `[6, 6, 6, 6]`): + stage_numb_of_layers (`list[int]`, *optional*, defaults to `[6, 6, 6, 6]`): Number of layers to be used in each block of the stage. use_learnable_affine_block (`bool`, *optional*, defaults to `False`): Whether to use Learnable Affine Blocks (LAB) in the network. diff --git a/src/transformers/models/hiera/configuration_hiera.py b/src/transformers/models/hiera/configuration_hiera.py index ebd6fe594c0..2342d7e562a 100644 --- a/src/transformers/models/hiera/configuration_hiera.py +++ b/src/transformers/models/hiera/configuration_hiera.py @@ -84,12 +84,12 @@ class HieraConfig(BackboneConfigMixin, PretrainedConfig): Whether to normalize the pixel loss by the number of pixels. mask_ratio (`float`, *optional*, defaults to 0.6): The ratio of masked tokens in the input. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/hiera/convert_hiera_to_hf.py b/src/transformers/models/hiera/convert_hiera_to_hf.py index eed27645b34..fb23803c65f 100644 --- a/src/transformers/models/hiera/convert_hiera_to_hf.py +++ b/src/transformers/models/hiera/convert_hiera_to_hf.py @@ -20,7 +20,6 @@ URL: https://github.com/facebookresearch/hiera import argparse import json import math -from typing import Dict, Tuple import requests import torch @@ -156,7 +155,7 @@ def prepare_img(): return im -def get_labels_for_classifier(model_name: str) -> Tuple[Dict[int, str], Dict[str, int], int]: +def get_labels_for_classifier(model_name: str) -> tuple[dict[int, str], dict[str, int], int]: repo_id = "huggingface/label-files" filename = "imagenet-1k-id2label.json" diff --git a/src/transformers/models/hiera/modeling_hiera.py b/src/transformers/models/hiera/modeling_hiera.py index 613d8796c86..e542c7b69a5 100644 --- a/src/transformers/models/hiera/modeling_hiera.py +++ b/src/transformers/models/hiera/modeling_hiera.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -68,9 +68,9 @@ class HieraEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -110,9 +110,9 @@ class HieraModelOutput(ModelOutput): pooler_output: Optional[torch.FloatTensor] = None bool_masked_pos: torch.BoolTensor = None ids_restore: Optional[torch.LongTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -146,9 +146,9 @@ class HieraForImageClassificationOutput(ImageClassifierOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -183,9 +183,9 @@ class HieraForPreTrainingOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None bool_masked_pos: torch.BoolTensor = None ids_restore: Optional[torch.LongTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor]] = None class HieraPatchEmbeddings(nn.Module): @@ -235,7 +235,7 @@ class HieraPatchEmbeddings(nn.Module): def random_masking( self, pixel_values: torch.FloatTensor, noise: Optional[torch.FloatTensor] = None - ) -> Tuple[torch.BoolTensor, torch.LongTensor]: + ) -> tuple[torch.BoolTensor, torch.LongTensor]: """ Perform per-sample random masking by per-sample shuffling. Per-sample shuffling is done by argsort random noise. @@ -271,7 +271,7 @@ class HieraPatchEmbeddings(nn.Module): self, pixel_values: torch.FloatTensor, noise: Optional[torch.FloatTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.BoolTensor], Optional[torch.LongTensor]]: + ) -> tuple[torch.Tensor, Optional[torch.BoolTensor], Optional[torch.LongTensor]]: (bool_masked_pos, ids_restore) = ( self.random_masking(pixel_values, noise=noise) if self.is_mae else (None, None) ) @@ -351,7 +351,7 @@ class HieraEmbeddings(nn.Module): pixel_values: torch.FloatTensor, noise: Optional[torch.FloatTensor] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.BoolTensor], Optional[torch.LongTensor]]: + ) -> tuple[torch.Tensor, Optional[torch.BoolTensor], Optional[torch.LongTensor]]: height, width = pixel_values.shape[-2:] embeddings, bool_masked_pos, ids_restore = self.patch_embeddings(pixel_values, noise=noise) embeddings = embeddings + self.get_position_embedding(embeddings, height, width, interpolate_pos_encoding) @@ -393,7 +393,7 @@ class HieraMaskUnitAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input should be of shape [batch, tokens, channels].""" batch_size, seq_len, _ = hidden_states.shape @@ -516,7 +516,7 @@ class HieraLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.FloatTensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: batch_size, seq_len, _ = hidden_states.shape # Attention + Q Pooling hidden_states_norm = self.layernorm_before(hidden_states) @@ -548,8 +548,8 @@ class HieraStage(nn.Module): hidden_size: int, hidden_size_output: int, num_heads: int, - drop_path: List[float], - query_stride: List[int], + drop_path: list[float], + query_stride: list[int], window_size: int, use_mask_unit_attn: bool, stage_num: Optional[int] = None, @@ -580,7 +580,7 @@ class HieraStage(nn.Module): def forward( self, hidden_states: torch.Tensor, head_mask: Optional[torch.FloatTensor], output_attentions: bool = False - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: for i, layer_module in enumerate(self.layers): layer_head_mask = head_mask[i] if head_mask is not None else None (hidden_states, attn_weights) = layer_module( @@ -590,14 +590,14 @@ class HieraStage(nn.Module): return hidden_states, attn_weights -def undo_windowing(hidden_states: torch.Tensor, shape: List[int], mask_unit_shape: List[int]) -> torch.Tensor: +def undo_windowing(hidden_states: torch.Tensor, shape: list[int], mask_unit_shape: list[int]) -> torch.Tensor: """ Restore spatial organization by undoing windowed organization of mask units. Args: hidden_states (`torch.Tensor`): The hidden states tensor of shape `[batch_size, num_mask_unit_height*num_mask_unit_width, hidden_size]`. - shape (`List[int]`): The original shape of the hidden states tensor before windowing. - mask_unit_shape (`List[int]`): The shape of the mask units used for windowing. + shape (`list[int]`): The original shape of the hidden states tensor before windowing. + mask_unit_shape (`list[int]`): The shape of the mask units used for windowing. Returns: torch.Tensor: The restored hidden states tensor of shape [batch_size, num_mask_unit_height*mask_unit_height, num_mask_unit_width*mask_unit_width, hidden_size]. @@ -766,7 +766,7 @@ class HieraEncoder(nn.Module): def unroll( - hidden_states: torch.Tensor, image_shape: Tuple[int, int], patch_stride: Tuple[int, int], schedule: List[List[int]] + hidden_states: torch.Tensor, image_shape: tuple[int, int], patch_stride: tuple[int, int], schedule: list[list[int]] ) -> torch.Tensor: """ Reorders the tokens such that patches are contiguous in memory. @@ -887,7 +887,7 @@ class HieraModel(HieraPreTrainedModel): def get_input_embeddings(self) -> HieraPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -905,7 +905,7 @@ class HieraModel(HieraPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" noise (`torch.FloatTensor` of shape `(batch_size, num_mask_units)`, *optional*): Mainly used for testing purposes to control randomness and maintain the reproducibility @@ -1023,7 +1023,7 @@ class HieraDecoder(nn.Module): bool_masked_pos: torch.BoolTensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, torch.BoolTensor]: + ) -> tuple[torch.Tensor, torch.BoolTensor]: # Embed tokens hidden_states = self.decoder_embeddings(encoder_hidden_states) @@ -1130,7 +1130,7 @@ class HieraMultiScaleHead(nn.Module): return hidden_states - def forward(self, feature_maps: List[torch.Tensor]) -> torch.Tensor: + def forward(self, feature_maps: list[torch.Tensor]) -> torch.Tensor: # Multi-scale fusion hidden_states = 0.0 for head, feature_map in zip(self.multi_scale_fusion_heads, feature_maps): diff --git a/src/transformers/models/hubert/configuration_hubert.py b/src/transformers/models/hubert/configuration_hubert.py index 486487da3db..c8af7b5a0f3 100644 --- a/src/transformers/models/hubert/configuration_hubert.py +++ b/src/transformers/models/hubert/configuration_hubert.py @@ -77,13 +77,13 @@ class HubertConfig(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. diff --git a/src/transformers/models/hubert/modeling_hubert.py b/src/transformers/models/hubert/modeling_hubert.py index 15f874c40aa..faa0ff48c68 100755 --- a/src/transformers/models/hubert/modeling_hubert.py +++ b/src/transformers/models/hubert/modeling_hubert.py @@ -20,7 +20,7 @@ # limitations under the License. import warnings -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -309,14 +309,14 @@ class HubertAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -802,7 +802,7 @@ class HubertPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -995,7 +995,7 @@ class HubertModel(HubertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1155,7 +1155,7 @@ class HubertForCTC(HubertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1278,11 +1278,11 @@ class HubertForSequenceClassification(HubertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`HubertProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/hubert/modeling_tf_hubert.py b/src/transformers/models/hubert/modeling_tf_hubert.py index d12c343de61..a701252f63e 100644 --- a/src/transformers/models/hubert/modeling_tf_hubert.py +++ b/src/transformers/models/hubert/modeling_tf_hubert.py @@ -17,7 +17,7 @@ from __future__ import annotations import warnings -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -78,7 +78,7 @@ def _scatter_values_on_batch_indices(values, batch_indices, output_shape): # Copied from transformers.models.wav2vec2.modeling_tf_wav2vec2._compute_mask_indices def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, min_masks: int = 0, @@ -698,11 +698,11 @@ class TFHubertAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -898,7 +898,7 @@ class TFHubertEncoderLayer(keras.layers.Layer): attention_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attn_residual = hidden_states hidden_states, attn_weights, _ = self.attention( hidden_states, attention_mask=attention_mask, training=training @@ -958,7 +958,7 @@ class TFHubertEncoderLayerStableLayerNorm(keras.layers.Layer): attention_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attn_residual = hidden_states hidden_states = self.layer_norm(hidden_states) hidden_states, attn_weights, _ = self.attention( @@ -1011,7 +1011,7 @@ class TFHubertEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1094,7 +1094,7 @@ class TFHubertEncoderStableLayerNorm(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1370,7 +1370,7 @@ HUBERT_START_DOCSTRING = r""" HUBERT_INPUTS_DOCSTRING = r""" Args: - input_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1449,7 +1449,7 @@ class TFHubertModel(TFHubertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: """ Returns: @@ -1556,7 +1556,7 @@ class TFHubertForCTC(TFHubertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., diff --git a/src/transformers/models/hubert/modular_hubert.py b/src/transformers/models/hubert/modular_hubert.py index c0454452f02..ec13ad60900 100644 --- a/src/transformers/models/hubert/modular_hubert.py +++ b/src/transformers/models/hubert/modular_hubert.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Hubert model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -228,7 +228,7 @@ class HubertModel(Wav2Vec2Model, HubertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict diff --git a/src/transformers/models/ibert/modeling_ibert.py b/src/transformers/models/ibert/modeling_ibert.py index 006bfcc40bd..89fd716f885 100644 --- a/src/transformers/models/ibert/modeling_ibert.py +++ b/src/transformers/models/ibert/modeling_ibert.py @@ -18,7 +18,7 @@ """PyTorch I-BERT model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -705,7 +705,7 @@ class IBertModel(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutputWithPoolingAndCrossAttentions, Tuple[torch.FloatTensor]]: + ) -> Union[BaseModelOutputWithPoolingAndCrossAttentions, tuple[torch.FloatTensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -805,7 +805,7 @@ class IBertForMaskedLM(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MaskedLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -906,7 +906,7 @@ class IBertForSequenceClassification(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -988,7 +988,7 @@ class IBertForMultipleChoice(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MultipleChoiceModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MultipleChoiceModelOutput, tuple[torch.FloatTensor]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1092,7 +1092,7 @@ class IBertForTokenClassification(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[TokenClassifierOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1178,7 +1178,7 @@ class IBertForQuestionAnswering(IBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[QuestionAnsweringModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[QuestionAnsweringModelOutput, tuple[torch.FloatTensor]]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.ibert( diff --git a/src/transformers/models/idefics/image_processing_idefics.py b/src/transformers/models/idefics/image_processing_idefics.py index 17b7fb4f39f..74a13ff5d11 100644 --- a/src/transformers/models/idefics/image_processing_idefics.py +++ b/src/transformers/models/idefics/image_processing_idefics.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Idefics.""" -from typing import Callable, Dict, List, Optional, Union +from typing import Callable, Optional, Union from PIL import Image @@ -55,11 +55,11 @@ class IdeficsImageProcessor(BaseImageProcessor): Args: image_size (`int`, *optional*, defaults to 224): Resize to image size - image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -78,8 +78,8 @@ class IdeficsImageProcessor(BaseImageProcessor): def __init__( self, image_size: int = 224, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, image_num_channels: Optional[int] = 3, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, @@ -98,9 +98,9 @@ class IdeficsImageProcessor(BaseImageProcessor): self, images: ImageInput, image_num_channels: Optional[int] = 3, - image_size: Optional[Dict[str, int]] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_size: Optional[dict[str, int]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, transform: Optional[Callable] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, @@ -117,11 +117,11 @@ class IdeficsImageProcessor(BaseImageProcessor): Resize to image size image_num_channels (`int`, *optional*, defaults to `self.image_num_channels`): Number of image channels. - image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. diff --git a/src/transformers/models/idefics/modeling_idefics.py b/src/transformers/models/idefics/modeling_idefics.py index 9aabc686795..f4a6715d6cf 100644 --- a/src/transformers/models/idefics/modeling_idefics.py +++ b/src/transformers/models/idefics/modeling_idefics.py @@ -20,7 +20,7 @@ """PyTorch Idefics model.""" from dataclasses import dataclass -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn.functional as F @@ -90,10 +90,10 @@ class IdeficsBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -132,10 +132,10 @@ class IdeficsCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None def expand_inputs_for_generation( @@ -611,12 +611,12 @@ class IdeficsAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # if key_value_states are provided this layer is used as a cross-attention layer is_cross_attention = self.is_cross_attention or key_value_states is not None @@ -708,12 +708,12 @@ class IdeficsDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -840,9 +840,9 @@ class IdeficsGatedCrossAttentionLayer(nn.Module): cross_attention_gate: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1045,7 +1045,7 @@ class IdeficsModel(IdeficsPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, image_encoder_embeddings: Optional[torch.FloatTensor] = None, @@ -1058,7 +1058,7 @@ class IdeficsModel(IdeficsPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, IdeficsBaseModelOutputWithPast]: + ) -> Union[tuple, IdeficsBaseModelOutputWithPast]: r""" image_encoder_embeddings (`torch.FloatTensor`, *optional*): The output of the image encoder. @@ -1506,7 +1506,7 @@ class IdeficsForVisionText2Text(IdeficsPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, image_encoder_embeddings: Optional[torch.FloatTensor] = None, @@ -1520,7 +1520,7 @@ class IdeficsForVisionText2Text(IdeficsPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, IdeficsCausalLMOutputWithPast]: + ) -> Union[tuple, IdeficsCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1649,10 +1649,10 @@ class IdeficsForVisionText2Text(IdeficsPreTrainedModel, GenerationMixin): def _update_model_kwargs_for_generation( self, outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], is_encoder_decoder: bool = False, **kwargs, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: model_kwargs = super()._update_model_kwargs_for_generation( outputs, model_kwargs, diff --git a/src/transformers/models/idefics/modeling_tf_idefics.py b/src/transformers/models/idefics/modeling_tf_idefics.py index 4f4ef97aa56..662fe4d88d1 100644 --- a/src/transformers/models/idefics/modeling_tf_idefics.py +++ b/src/transformers/models/idefics/modeling_tf_idefics.py @@ -22,7 +22,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -92,10 +92,10 @@ class TFIdeficsBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - past_key_values: Optional[Tuple[Tuple[tf.Tensor]]] = None - hidden_states: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[tf.Tensor]] = None - image_hidden_states: Optional[Tuple[tf.Tensor]] = None + past_key_values: Optional[tuple[tuple[tf.Tensor]]] = None + hidden_states: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tf.Tensor]] = None + image_hidden_states: Optional[tuple[tf.Tensor]] = None @dataclass @@ -134,10 +134,10 @@ class TFIdeficsCausalLMOutputWithPast(ModelOutput): loss: Optional[tf.Tensor] = None logits: Optional[tf.Tensor] = None - past_key_values: Optional[List[tf.Tensor]] = None - hidden_states: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[tf.Tensor]] = None - image_hidden_states: Optional[Tuple[tf.Tensor]] = None + past_key_values: Optional[list[tf.Tensor]] = None + hidden_states: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tf.Tensor]] = None + image_hidden_states: Optional[tuple[tf.Tensor]] = None def expand_inputs_for_generation( @@ -672,10 +672,10 @@ class TFIdeficsAttention(tf.keras.layers.Layer): key_value_states: Optional[tf.Tensor] = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_value: Optional[Tuple[tf.Tensor]] = None, + past_key_value: Optional[tuple[tf.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, - ) -> Tuple[tf.Tensor, Optional[tf.Tensor], Optional[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, Optional[tf.Tensor], Optional[tuple[tf.Tensor]]]: # if key_value_states are provided this layer is used as a cross-attention layer is_cross_attention = self.is_cross_attention or key_value_states is not None @@ -804,11 +804,11 @@ class TFIdeficsDecoderLayer(tf.keras.layers.Layer): hidden_states: tf.Tensor, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_value: Optional[Tuple[tf.Tensor]] = None, + past_key_value: Optional[tuple[tf.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, training=False, - ) -> Tuple[tf.Tensor, Optional[Tuple[tf.Tensor, tf.Tensor]]]: + ) -> tuple[tf.Tensor, Optional[tuple[tf.Tensor, tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -996,8 +996,8 @@ class TFIdeficsGatedCrossAttentionLayer(tf.keras.layers.Layer): cross_attention_gate: Optional[tf.Tensor] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, - past_key_value: Optional[Tuple[tf.Tensor]] = None, - ) -> Tuple[tf.Tensor, Optional[Tuple[tf.Tensor, tf.Tensor]]]: + past_key_value: Optional[tuple[tf.Tensor]] = None, + ) -> tuple[tf.Tensor, Optional[tuple[tf.Tensor, tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1268,7 +1268,7 @@ class TFIdeficsMainLayer(tf.keras.layers.Layer): input_ids: TFModelInputType | None = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, pixel_values: Optional[tf.Tensor] = None, image_encoder_embeddings: Optional[tf.Tensor] = None, @@ -1280,7 +1280,7 @@ class TFIdeficsMainLayer(tf.keras.layers.Layer): interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[TFIdeficsBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFIdeficsBaseModelOutputWithPast, tuple[tf.Tensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1567,7 +1567,7 @@ class TFIdeficsModel(TFIdeficsPreTrainedModel): input_ids: TFModelInputType | None = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, pixel_values: Optional[tf.Tensor] = None, image_encoder_embeddings: Optional[tf.Tensor] = None, @@ -1579,7 +1579,7 @@ class TFIdeficsModel(TFIdeficsPreTrainedModel): interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[TFIdeficsBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFIdeficsBaseModelOutputWithPast, tuple[tf.Tensor]]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1672,7 +1672,7 @@ class TFIdeficsForVisionText2Text(TFPreTrainedModel, TFCausalLanguageModelingLos input_ids: TFModelInputType | None = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, pixel_values: Optional[tf.Tensor] = None, image_encoder_embeddings: Optional[tf.Tensor] = None, @@ -1685,7 +1685,7 @@ class TFIdeficsForVisionText2Text(TFPreTrainedModel, TFCausalLanguageModelingLos interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, training=False, - ) -> Union[TFIdeficsCausalLMOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFIdeficsCausalLMOutputWithPast, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/idefics/perceiver.py b/src/transformers/models/idefics/perceiver.py index 91e80f85164..c7372c6e724 100644 --- a/src/transformers/models/idefics/perceiver.py +++ b/src/transformers/models/idefics/perceiver.py @@ -37,7 +37,7 @@ References: """ -from typing import Optional, Tuple +from typing import Optional import torch import torch.nn as nn @@ -180,7 +180,7 @@ class IdeficsMLP(nn.Module): self.act = nn.ReLU() self.c_proj = nn.Linear(intermediate_size, self.embed_dim, bias=False) - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.ln(hidden_states) hidden_states = self.fc(hidden_states) hidden_states = self.act(hidden_states) diff --git a/src/transformers/models/idefics/perceiver_tf.py b/src/transformers/models/idefics/perceiver_tf.py index 0f1c6153490..a4de96b68e7 100644 --- a/src/transformers/models/idefics/perceiver_tf.py +++ b/src/transformers/models/idefics/perceiver_tf.py @@ -37,7 +37,7 @@ References: """ -from typing import Optional, Tuple +from typing import Optional import tensorflow as tf @@ -186,7 +186,7 @@ class TFIdeficsMLP(tf.keras.layers.Layer): self.act = tf.keras.layers.ReLU(name="act") self.c_proj = tf.keras.layers.Dense(self.embed_dim, use_bias=False, name="c_proj") - def call(self, hidden_states: Optional[Tuple[tf.Tensor]]) -> tf.Tensor: + def call(self, hidden_states: Optional[tuple[tf.Tensor]]) -> tf.Tensor: hidden_states = self.ln(hidden_states) hidden_states = self.fc(hidden_states) hidden_states = self.act(hidden_states) diff --git a/src/transformers/models/idefics/processing_idefics.py b/src/transformers/models/idefics/processing_idefics.py index e226e15da19..2ff60ac4e32 100644 --- a/src/transformers/models/idefics/processing_idefics.py +++ b/src/transformers/models/idefics/processing_idefics.py @@ -16,7 +16,7 @@ Processor class for IDEFICS. """ -from typing import Callable, Dict, List, Optional, Union +from typing import Callable, Optional, Union from urllib.parse import urlparse from ...feature_extraction_utils import BatchFeature @@ -45,9 +45,9 @@ IMAGE_TOKEN = "" class IdeficsImagesKwargs(ImagesKwargs, total=False): transform: Optional[Callable] - image_size: Optional[Dict[str, int]] - image_mean: Optional[Union[float, List[float]]] - image_std: Optional[Union[float, List[float]]] + image_size: Optional[dict[str, int]] + image_mean: Optional[Union[float, list[float]]] + image_std: Optional[Union[float, list[float]]] class IdeficsTextKwargs(TextKwargs, total=False): @@ -243,14 +243,14 @@ class IdeficsProcessor(ProcessorMixin): @deprecate_kwarg(old_name="prompts", version="5.0.0", new_name="text", raise_if_both_names=True) def __call__( self, - images: Union[ImageInput, List[ImageInput], str, List[str], List[List[str]]] = None, + images: Union[ImageInput, list[ImageInput], str, list[str], list[list[str]]] = None, text: Union[ TextInput, PreTokenizedInput, - List[TextInput], - List[PreTokenizedInput], - List[List[TextInput]], - List[List[PreTokenizedInput]], + list[TextInput], + list[PreTokenizedInput], + list[list[TextInput]], + list[list[PreTokenizedInput]], ] = None, audio=None, videos=None, @@ -260,10 +260,10 @@ class IdeficsProcessor(ProcessorMixin): the model was trained on and prepares the image pixel values for the model to process. Args: - images (`Union[ImageInput, List[ImageInput], str, List[str], List[List[str]]]`): + images (`Union[ImageInput, list[ImageInput], str, list[str], list[list[str]]]`): either a single image or a batched list of images - can be passed in when text contains only text prompts, in order to use the image-text-to-text behavior. - text (`Union[List[TextInput], [List[List[TextInput]]]]`): + text (`Union[list[TextInput], [list[list[TextInput]]]]`): either a single prompt or a batched list of prompts - see the detailed description immediately after the end of the arguments doc section. return_tensors (`str` or `TensorType`, *optional*, defaults to `TensorType.PYTORCH`): diff --git a/src/transformers/models/idefics/vision.py b/src/transformers/models/idefics/vision.py index 5b2ef5ae3ee..815b902d3fb 100644 --- a/src/transformers/models/idefics/vision.py +++ b/src/transformers/models/idefics/vision.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -60,8 +60,8 @@ class IdeficsVisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None # Adapted from transformers.models.clip.modeling_clip.CLIPVisionEmbeddings @@ -216,7 +216,7 @@ class IdeficsVisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -298,7 +298,7 @@ class IdeficsVisionEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -357,7 +357,7 @@ class IdeficsVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -451,7 +451,7 @@ class IdeficsVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Returns: diff --git a/src/transformers/models/idefics/vision_tf.py b/src/transformers/models/idefics/vision_tf.py index c01e1c2e1fa..9db18f265db 100644 --- a/src/transformers/models/idefics/vision_tf.py +++ b/src/transformers/models/idefics/vision_tf.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -56,8 +56,8 @@ class TFIdeficsVisionModelOutput(ModelOutput): image_embeds: Optional[tf.Tensor] = None last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Optional[Tuple[tf.Tensor]] = None - attentions: Optional[Tuple[tf.Tensor]] = None + hidden_states: Optional[tuple[tf.Tensor]] = None + attentions: Optional[tuple[tf.Tensor]] = None class TFIdeficsVisionEmbeddings(tf.keras.layers.Layer): @@ -204,7 +204,7 @@ class TFIdeficsVisionAttention(tf.keras.layers.Layer): attention_mask: Optional[tf.Tensor] = None, causal_attention_mask: Optional[tf.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[tf.Tensor, Optional[tf.Tensor], Optional[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, Optional[tf.Tensor], Optional[tuple[tf.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = shape_list(hidden_states) @@ -335,7 +335,7 @@ class TFIdeficsVisionEncoderLayer(tf.keras.layers.Layer): attention_mask: tf.Tensor, causal_attention_mask: tf.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -407,7 +407,7 @@ class TFIdeficsVisionEncoder(tf.keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: r""" Args: inputs_embeds (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -516,7 +516,7 @@ class TFIdeficsVisionTransformer(TFPreTrainedModel): interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPooling]: + ) -> Union[tuple, TFBaseModelOutputWithPooling]: r""" Returns: diff --git a/src/transformers/models/idefics2/image_processing_idefics2.py b/src/transformers/models/idefics2/image_processing_idefics2.py index 197e2fbf3f9..8ad3fe1e142 100644 --- a/src/transformers/models/idefics2/image_processing_idefics2.py +++ b/src/transformers/models/idefics2/image_processing_idefics2.py @@ -15,7 +15,7 @@ from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -46,14 +46,14 @@ if is_vision_available(): from PIL import Image -def get_resize_output_image_size(image, size, input_data_format) -> Tuple[int, int]: +def get_resize_output_image_size(image, size, input_data_format) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image containing the keys "shortest_edge" and "longest_edge". input_data_format (`ChannelDimension` or `str`): The channel dimension format of the input image. @@ -79,7 +79,7 @@ def get_resize_output_image_size(image, size, input_data_format) -> Tuple[int, i # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -87,8 +87,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: def get_max_height_width( - images_list: List[List[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images_list: list[list[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -106,7 +106,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -114,7 +114,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -169,11 +169,11 @@ class Idefics2ImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. If set to `True`, the image is normalized to have a mean of `image_mean` and a standard deviation of `image_std`. - image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -191,13 +191,13 @@ class Idefics2ImageProcessor(BaseImageProcessor): self, do_convert_rgb: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, do_image_splitting: bool = False, **kwargs, @@ -218,7 +218,7 @@ class Idefics2ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -231,7 +231,7 @@ class Idefics2ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -256,7 +256,7 @@ class Idefics2ImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -282,7 +282,7 @@ class Idefics2ImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -396,13 +396,13 @@ class Idefics2ImageProcessor(BaseImageProcessor): images: ImageInput, do_convert_rgb: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, do_image_splitting: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -419,7 +419,7 @@ class Idefics2ImageProcessor(BaseImageProcessor): Whether to convert the image to RGB. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -431,9 +431,9 @@ class Idefics2ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): diff --git a/src/transformers/models/idefics2/modeling_idefics2.py b/src/transformers/models/idefics2/modeling_idefics2.py index 663a4b43a3f..d9b5d5e6833 100644 --- a/src/transformers/models/idefics2/modeling_idefics2.py +++ b/src/transformers/models/idefics2/modeling_idefics2.py @@ -15,7 +15,7 @@ """PyTorch Idefics2 model.""" from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -70,10 +70,10 @@ class Idefics2BaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -108,10 +108,10 @@ class Idefics2CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None class Idefics2VisionEmbeddings(nn.Module): @@ -231,7 +231,7 @@ class Idefics2VisionAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -354,7 +354,7 @@ class Idefics2EncoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -412,7 +412,7 @@ class Idefics2Encoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -549,7 +549,7 @@ class Idefics2VisionTransformer(Idefics2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" patch_attention_mask (`torch.BoolTensor` of shape `(batch_size, num_patches_height, num_patches_width)`, *optional*): The attention mask for the patches. @@ -665,10 +665,10 @@ class Idefics2PerceiverAttention(nn.Module): context: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: bool = False, use_cache: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """ Runs Perceiver Self-Attention, with special (context, latents) appended along the `seq` dimension! @@ -677,7 +677,7 @@ class Idefics2PerceiverAttention(nn.Module): context (`torch.Tensor`): Tensor of shape [bsz, seq, embed_dim] representing long-form context to resample. attention_mask (`torch.Tensor`, *optional*): Tensor of shape [bsz, 1, seq, n_latents] representing attention mask. position_ids (`torch.LongTensor`, *optional*): Tensor of shape [bsz, seq] representing position indices of each input token. - past_key_value (`Tuple[torch.Tensor]`, *optional*): Tuple of tensors containing cached key and value states. + past_key_value (`tuple[torch.Tensor]`, *optional*): Tuple of tensors containing cached key and value states. output_attentions (`bool`, *optional*, defaults to `False`): Whether to return attention weights. use_cache (`bool`, *optional*, defaults to `False`): Whether to use past_key_value for caching. """ @@ -754,11 +754,11 @@ class Idefics2PerceiverLayer(nn.Module): context: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: latents (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1030,7 +1030,7 @@ class Idefics2Model(Idefics2PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -1041,7 +1041,7 @@ class Idefics2Model(Idefics2PreTrainedModel): cache_position: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, Idefics2BaseModelOutputWithPast]: + ) -> Union[tuple, Idefics2BaseModelOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. @@ -1196,7 +1196,7 @@ class Idefics2ForConditionalGeneration(Idefics2PreTrainedModel, GenerationMixin) input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -1209,7 +1209,7 @@ class Idefics2ForConditionalGeneration(Idefics2PreTrainedModel, GenerationMixin) cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Idefics2CausalLMOutputWithPast]: + ) -> Union[tuple, Idefics2CausalLMOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. diff --git a/src/transformers/models/idefics2/processing_idefics2.py b/src/transformers/models/idefics2/processing_idefics2.py index 5be15d8cd8b..9998a4b0108 100644 --- a/src/transformers/models/idefics2/processing_idefics2.py +++ b/src/transformers/models/idefics2/processing_idefics2.py @@ -17,7 +17,7 @@ Processor class for IDEFICS2. """ from itertools import accumulate -from typing import TYPE_CHECKING, List, Optional, Union +from typing import TYPE_CHECKING, Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, is_valid_image, load_image @@ -127,8 +127,8 @@ class Idefics2Processor(ProcessorMixin): def __call__( self, - images: Union[ImageInput, List[ImageInput], List[List[ImageInput]]] = None, - text: Union[TextInput, "PreTokenizedInput", List[TextInput], List["PreTokenizedInput"]] = None, + images: Union[ImageInput, list[ImageInput], list[list[ImageInput]]] = None, + text: Union[TextInput, "PreTokenizedInput", list[TextInput], list["PreTokenizedInput"]] = None, audio=None, videos=None, **kwargs: Unpack[Idefics2ProcessorKwargs], @@ -164,10 +164,10 @@ class Idefics2Processor(ProcessorMixin): ``` Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, *optional*): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`, *optional*): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch - tensor. If is of type `List[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. - text (`Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]`, *optional*): + tensor. If is of type `list[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. + text (`Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/idefics3/image_processing_idefics3.py b/src/transformers/models/idefics3/image_processing_idefics3.py index 912c168ccee..dfd4d17266a 100644 --- a/src/transformers/models/idefics3/image_processing_idefics3.py +++ b/src/transformers/models/idefics3/image_processing_idefics3.py @@ -15,7 +15,7 @@ import math from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -49,7 +49,7 @@ if is_vision_available(): def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: Optional[int] = 1, max_len: Optional[int] = None -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -85,8 +85,8 @@ def _resize_output_size_rescale_to_max_len( def _resize_output_size_scale_below_upper_bound( - height: int, width: int, max_len: Optional[Dict[str, int]] = None -) -> Tuple[int, int]: + height: int, width: int, max_len: Optional[dict[str, int]] = None +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -94,7 +94,7 @@ def _resize_output_size_scale_below_upper_bound( Height of the input image. width (`int`): Width of the input image. - max_len (`Dict[str, int]`, *optional*, defaults to the maximum size of the image): + max_len (`dict[str, int]`, *optional*, defaults to the maximum size of the image): Defines the maximum dimensions of the image. Returns: The output size of the image after resizing. @@ -119,7 +119,7 @@ def get_resize_output_image_size( image, resolution_max_side: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -143,7 +143,7 @@ def get_resize_output_image_size( # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -151,8 +151,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: def get_max_height_width( - images_list: List[List[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images_list: list[list[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -170,14 +170,14 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -197,7 +197,7 @@ def convert_to_rgb( Args: image (`np.ndarray`): The image to convert. - palette (List[int], *optional*): + palette (list[int], *optional*): The palette to use if given. data_format (ChannelDimension or str, *optional*): The channel dimension format for the output image. If not provided, it will be the same as the input image. @@ -277,11 +277,11 @@ class Idefics3ImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. If set to `True`, the image is normalized to have a mean of `image_mean` and a standard deviation of `image_std`. - image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -296,15 +296,15 @@ class Idefics3ImageProcessor(BaseImageProcessor): self, do_convert_rgb: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.LANCZOS, do_image_splitting: bool = True, - max_image_size: Optional[Dict[str, int]] = None, + max_image_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, **kwargs, ) -> None: @@ -325,7 +325,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.LANCZOS, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -337,7 +337,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): Resampling filter to use when resizing the image. @@ -382,7 +382,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): def split_image( self, image, - max_image_size: Dict[str, int], + max_image_size: dict[str, int], resample: PILImageResampling = PILImageResampling.LANCZOS, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -398,7 +398,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Images to split. - max_image_size (`Dict[str, int]`): + max_image_size (`dict[str, int]`): Maximum size of the output image. If the image is larger than this size, it will be split into patches of this size, and the original image will be concatenated with the patches, resized to max_size. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): @@ -500,7 +500,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -526,7 +526,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -537,7 +537,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): For a list of images, for each images, pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width. For each sample in the batch, pads the sample with empty images to the max_number of images per sample in the batch. Optionally returns a pixel mask. Args: - images (`List[np.ndarray]`): + images (`list[np.ndarray]`): List of list of images to pad. Pads to the largest height and width in the batch. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -605,15 +605,15 @@ class Idefics3ImageProcessor(BaseImageProcessor): images: ImageInput, do_convert_rgb: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_image_splitting: Optional[bool] = None, do_rescale: Optional[bool] = None, - max_image_size: Optional[Dict[str, int]] = None, + max_image_size: Optional[dict[str, int]] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, return_row_col_info: bool = False, @@ -629,7 +629,7 @@ class Idefics3ImageProcessor(BaseImageProcessor): Whether to convert the image to RGB. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. With the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -645,9 +645,9 @@ class Idefics3ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): diff --git a/src/transformers/models/idefics3/modeling_idefics3.py b/src/transformers/models/idefics3/modeling_idefics3.py index 70c803268ae..53b3cc2e304 100644 --- a/src/transformers/models/idefics3/modeling_idefics3.py +++ b/src/transformers/models/idefics3/modeling_idefics3.py @@ -15,7 +15,7 @@ """PyTorch Idefics3 model.""" from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -70,10 +70,10 @@ class Idefics3BaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -108,10 +108,10 @@ class Idefics3CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.idefics2.modeling_idefics2.Idefics2VisionEmbeddings with Idefics2->Idefics3 @@ -229,7 +229,7 @@ class Idefics3VisionAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -315,7 +315,7 @@ class Idefics3EncoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -373,7 +373,7 @@ class Idefics3Encoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -562,7 +562,7 @@ class Idefics3VisionTransformer(Idefics3PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -763,7 +763,7 @@ class Idefics3Model(Idefics3PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -774,7 +774,7 @@ class Idefics3Model(Idefics3PreTrainedModel): cache_position: Optional[torch.LongTensor] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, Idefics3BaseModelOutputWithPast]: + ) -> Union[tuple, Idefics3BaseModelOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. @@ -919,7 +919,7 @@ class Idefics3ForConditionalGeneration(Idefics3PreTrainedModel, GenerationMixin) input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -932,7 +932,7 @@ class Idefics3ForConditionalGeneration(Idefics3PreTrainedModel, GenerationMixin) return_dict: Optional[bool] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Idefics3CausalLMOutputWithPast]: + ) -> Union[tuple, Idefics3CausalLMOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. diff --git a/src/transformers/models/idefics3/processing_idefics3.py b/src/transformers/models/idefics3/processing_idefics3.py index 5f4450df8b4..7019e76f5a9 100644 --- a/src/transformers/models/idefics3/processing_idefics3.py +++ b/src/transformers/models/idefics3/processing_idefics3.py @@ -19,7 +19,7 @@ Processor class for Idefics3. import math import re from itertools import accumulate -from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -90,7 +90,7 @@ def get_image_prompt_string( class Idefics3ImagesKwargs(ImagesKwargs, total=False): return_row_col_info: Optional[bool] - max_image_size: Optional[Dict[str, int]] + max_image_size: Optional[dict[str, int]] class Idefics3ProcessorKwargs(ProcessingKwargs, total=False): @@ -186,8 +186,8 @@ class Idefics3Processor(ProcessorMixin): def __call__( self, - images: Union[ImageInput, List[ImageInput], List[List[ImageInput]]] = None, - text: Union[TextInput, "PreTokenizedInput", List[TextInput], List["PreTokenizedInput"]] = None, + images: Union[ImageInput, list[ImageInput], list[list[ImageInput]]] = None, + text: Union[TextInput, "PreTokenizedInput", list[TextInput], list["PreTokenizedInput"]] = None, audio=None, videos=None, image_seq_len: Optional[int] = None, @@ -224,10 +224,10 @@ class Idefics3Processor(ProcessorMixin): ``` Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, *optional*): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`, *optional*): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch - tensor. If is of type `List[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. - text (`Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]`, *optional*): + tensor. If is of type `list[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. + text (`Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -377,7 +377,7 @@ class Idefics3Processor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/ijepa/modeling_ijepa.py b/src/transformers/models/ijepa/modeling_ijepa.py index dafef6d5666..c7ce7c29b4c 100644 --- a/src/transformers/models/ijepa/modeling_ijepa.py +++ b/src/transformers/models/ijepa/modeling_ijepa.py @@ -5,7 +5,7 @@ # modular_ijepa.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import collections.abc -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -235,7 +235,7 @@ class IJepaSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -294,7 +294,7 @@ class IJepaAttention(nn.Module): self.output = IJepaSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -317,7 +317,7 @@ class IJepaAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -375,7 +375,7 @@ class IJepaLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in IJepa, layernorm is applied before self-attention head_mask, @@ -488,7 +488,7 @@ class IJepaModel(IJepaPreTrainedModel): def get_input_embeddings(self) -> IJepaPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -506,7 +506,7 @@ class IJepaModel(IJepaPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). diff --git a/src/transformers/models/imagegpt/image_processing_imagegpt.py b/src/transformers/models/imagegpt/image_processing_imagegpt.py index 5b941a6c779..5d3e207dd3e 100644 --- a/src/transformers/models/imagegpt/image_processing_imagegpt.py +++ b/src/transformers/models/imagegpt/image_processing_imagegpt.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for ImageGPT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -65,13 +65,13 @@ class ImageGPTImageProcessor(BaseImageProcessor): (color clusters). Args: - clusters (`np.ndarray` or `List[List[int]]`, *optional*): + clusters (`np.ndarray` or `list[list[int]]`, *optional*): The color clusters to use, of shape `(n_clusters, 3)` when color quantizing. Can be overridden by `clusters` in `preprocess`. do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's dimensions to `(size["height"], size["width"])`. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 256, "width": 256}`): Size of the image after resizing. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. @@ -87,9 +87,9 @@ class ImageGPTImageProcessor(BaseImageProcessor): def __init__( self, # clusters is a first argument to maintain backwards compatibility with the old ImageGPTImageProcessor - clusters: Optional[Union[List[List[int]], np.ndarray]] = None, + clusters: Optional[Union[list[list[int]], np.ndarray]] = None, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_normalize: bool = True, do_color_quantize: bool = True, @@ -109,7 +109,7 @@ class ImageGPTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -121,7 +121,7 @@ class ImageGPTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -180,11 +180,11 @@ class ImageGPTImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_normalize: Optional[bool] = None, do_color_quantize: Optional[bool] = None, - clusters: Optional[Union[List[List[int]], np.ndarray]] = None, + clusters: Optional[Union[list[list[int]], np.ndarray]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[Union[str, ChannelDimension]] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -198,7 +198,7 @@ class ImageGPTImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_normalize=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only @@ -207,7 +207,7 @@ class ImageGPTImageProcessor(BaseImageProcessor): Whether to normalize the image do_color_quantize (`bool`, *optional*, defaults to `self.do_color_quantize`): Whether to color quantize the image. - clusters (`np.ndarray` or `List[List[int]]`, *optional*, defaults to `self.clusters`): + clusters (`np.ndarray` or `list[list[int]]`, *optional*, defaults to `self.clusters`): Clusters used to quantize the image of shape `(n_clusters, 3)`. Only has an effect if `do_color_quantize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/imagegpt/modeling_imagegpt.py b/src/transformers/models/imagegpt/modeling_imagegpt.py index 68f6a04c5a2..ebcec593fe1 100755 --- a/src/transformers/models/imagegpt/modeling_imagegpt.py +++ b/src/transformers/models/imagegpt/modeling_imagegpt.py @@ -17,7 +17,7 @@ import math import os import warnings -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -154,7 +154,7 @@ def load_tf_weights_in_imagegpt(model, config, imagegpt_checkpoint_path): class ImageGPTLayerNorm(nn.Module): - def __init__(self, hidden_size: Tuple[int], eps: float = 1e-5): + def __init__(self, hidden_size: tuple[int], eps: float = 1e-5): super().__init__() self.eps = eps self.weight = nn.Parameter(torch.Tensor(hidden_size)) @@ -554,7 +554,7 @@ class ImageGPTModel(ImageGPTPreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -567,7 +567,7 @@ class ImageGPTModel(ImageGPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Any, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -812,7 +812,7 @@ class ImageGPTForCausalImageModeling(ImageGPTPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -826,7 +826,7 @@ class ImageGPTForCausalImageModeling(ImageGPTPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Any, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -936,8 +936,8 @@ class ImageGPTForCausalImageModeling(ImageGPTPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -969,7 +969,7 @@ class ImageGPTForImageClassification(ImageGPTPreTrainedModel): def forward( self, input_ids: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, attention_mask: Optional[torch.Tensor] = None, token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -981,7 +981,7 @@ class ImageGPTForImageClassification(ImageGPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Any, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else diff --git a/src/transformers/models/informer/configuration_informer.py b/src/transformers/models/informer/configuration_informer.py index 5cb34dc8970..559c482ea36 100644 --- a/src/transformers/models/informer/configuration_informer.py +++ b/src/transformers/models/informer/configuration_informer.py @@ -14,7 +14,7 @@ # limitations under the License. """Informer model configuration""" -from typing import List, Optional, Union +from typing import Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -141,14 +141,14 @@ class InformerConfig(PretrainedConfig): distribution_output: str = "student_t", loss: str = "nll", input_size: int = 1, - lags_sequence: Optional[List[int]] = None, + lags_sequence: Optional[list[int]] = None, scaling: Optional[Union[str, bool]] = "mean", num_dynamic_real_features: int = 0, num_static_real_features: int = 0, num_static_categorical_features: int = 0, num_time_features: int = 0, - cardinality: Optional[List[int]] = None, - embedding_dimension: Optional[List[int]] = None, + cardinality: Optional[list[int]] = None, + embedding_dimension: Optional[list[int]] = None, d_model: int = 64, encoder_ffn_dim: int = 32, decoder_ffn_dim: int = 32, diff --git a/src/transformers/models/informer/modeling_informer.py b/src/transformers/models/informer/modeling_informer.py index e0cd58eec77..6f59e0ee14b 100644 --- a/src/transformers/models/informer/modeling_informer.py +++ b/src/transformers/models/informer/modeling_informer.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -66,7 +66,7 @@ class InformerFeatureEmbedder(nn.Module): List of embedding dimensions of the categorical features. """ - def __init__(self, cardinalities: List[int], embedding_dims: List[int]) -> None: + def __init__(self, cardinalities: list[int], embedding_dims: list[int]) -> None: super().__init__() self.num_features = len(cardinalities) @@ -103,7 +103,7 @@ class InformerStdScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -139,7 +139,7 @@ class InformerMeanScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -190,7 +190,7 @@ class InformerNOPScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: Optional[torch.Tensor] = None - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -452,7 +452,7 @@ class InformerAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -567,12 +567,12 @@ class InformerProbSparseAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -800,7 +800,7 @@ class InformerEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -898,7 +898,7 @@ class InformerDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1016,7 +1016,7 @@ class InformerEncoder(InformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1157,14 +1157,14 @@ class InformerDecoder(InformerPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1515,14 +1515,14 @@ class InformerModel(InformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Seq2SeqTSModelOutput, Tuple]: + ) -> Union[Seq2SeqTSModelOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`): Past values of the time series, that serve as context in order to predict the future. The sequence size of @@ -1809,14 +1809,14 @@ class InformerForPrediction(InformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Seq2SeqTSModelOutput, Tuple]: + ) -> Union[Seq2SeqTSModelOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`): Past values of the time series, that serve as context in order to predict the future. The sequence size of diff --git a/src/transformers/models/informer/modular_informer.py b/src/transformers/models/informer/modular_informer.py index 183ba27c3a9..755fcd68853 100644 --- a/src/transformers/models/informer/modular_informer.py +++ b/src/transformers/models/informer/modular_informer.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Informer model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -256,12 +256,12 @@ class InformerProbSparseAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -540,7 +540,7 @@ class InformerEncoder(TimeSeriesTransformerEncoder): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): diff --git a/src/transformers/models/instructblip/modeling_instructblip.py b/src/transformers/models/instructblip/modeling_instructblip.py index 8018dbe76a9..1708a86082b 100644 --- a/src/transformers/models/instructblip/modeling_instructblip.py +++ b/src/transformers/models/instructblip/modeling_instructblip.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -62,13 +62,13 @@ class InstructBlipForConditionalGenerationModelOutput(ModelOutput): Outputs of the language model. """ - loss: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + loss: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None vision_outputs: Optional[torch.FloatTensor] = None - qformer_outputs: Optional[Tuple[torch.FloatTensor]] = None - language_model_outputs: Optional[Tuple[torch.FloatTensor]] = None + qformer_outputs: Optional[tuple[torch.FloatTensor]] = None + language_model_outputs: Optional[tuple[torch.FloatTensor]] = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["vision_outputs", "qformer_outputs", "language_model_outputs"] @@ -220,7 +220,7 @@ class InstructBlipAttention(nn.Module): head_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -292,7 +292,7 @@ class InstructBlipEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -390,7 +390,7 @@ class InstructBlipEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -470,7 +470,7 @@ class InstructBlipVisionModel(InstructBlipPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -690,9 +690,9 @@ class InstructBlipQFormerAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.attention( hidden_states, attention_mask, @@ -1017,7 +1017,7 @@ class InstructBlipQFormerModel(InstructBlipPreTrainedModel): def get_extended_attention_mask( self, attention_mask: torch.Tensor, - input_shape: Tuple[int], + input_shape: tuple[int], device: torch.device, has_query: bool = False, ) -> torch.Tensor: @@ -1027,7 +1027,7 @@ class InstructBlipQFormerModel(InstructBlipPreTrainedModel): Arguments: attention_mask (`torch.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. device: (`torch.device`): The device of the input to the model. @@ -1066,12 +1066,12 @@ class InstructBlipQFormerModel(InstructBlipPreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1259,7 +1259,7 @@ class InstructBlipModel(InstructBlipPreTrainedModel): interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, InstructBlipForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipForConditionalGenerationModelOutput]: r""" qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided @@ -1518,7 +1518,7 @@ class InstructBlipForConditionalGeneration(InstructBlipPreTrainedModel, Generati interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, InstructBlipForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipForConditionalGenerationModelOutput]: r""" qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided diff --git a/src/transformers/models/instructblip/processing_instructblip.py b/src/transformers/models/instructblip/processing_instructblip.py index d3df6f4ef90..1b2026f6dd3 100644 --- a/src/transformers/models/instructblip/processing_instructblip.py +++ b/src/transformers/models/instructblip/processing_instructblip.py @@ -17,7 +17,7 @@ Processor class for InstructBLIP. Largely copy of Blip2Processor with addition o """ import os -from typing import List, Union +from typing import Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput @@ -83,7 +83,7 @@ class InstructBlipProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[InstructBlipProcessorKwargs], @@ -97,7 +97,7 @@ class InstructBlipProcessor(ProcessorMixin): images (`ImageInput`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/instructblipvideo/image_processing_instructblipvideo.py b/src/transformers/models/instructblipvideo/image_processing_instructblipvideo.py index 436ce86eb43..68c812422af 100644 --- a/src/transformers/models/instructblipvideo/image_processing_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/image_processing_instructblipvideo.py @@ -17,7 +17,7 @@ Image processor class for InstructBLIPVideo. Largely copy of Blip2Processor with addition of a video processing abilities """ -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -67,11 +67,11 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -84,13 +84,13 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -112,7 +112,7 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -124,7 +124,7 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -164,13 +164,13 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): self, images: VideoInput = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -185,7 +185,7 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): ranging from 0 to 255. If passing in video with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the video. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the video after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -198,9 +198,9 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): Rescale factor to rescale the video by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the video. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the video by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the video by if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. @@ -287,13 +287,13 @@ class InstructBlipVideoImageProcessor(BaseImageProcessor): self, image: ImageInput = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, diff --git a/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py b/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py index cc18bbf90b6..8c0d5c05a3b 100644 --- a/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/modeling_instructblipvideo.py @@ -21,7 +21,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch from torch import nn @@ -192,7 +192,7 @@ class InstructBlipVideoAttention(nn.Module): head_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -262,7 +262,7 @@ class InstructBlipVideoEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -319,7 +319,7 @@ class InstructBlipVideoEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -554,9 +554,9 @@ class InstructBlipVideoQFormerAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.attention( hidden_states, attention_mask, @@ -906,7 +906,7 @@ class InstructBlipVideoVisionModel(InstructBlipVideoPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -983,7 +983,7 @@ class InstructBlipVideoQFormerModel(InstructBlipVideoPreTrainedModel): def get_extended_attention_mask( self, attention_mask: torch.Tensor, - input_shape: Tuple[int], + input_shape: tuple[int], device: torch.device, has_query: bool = False, ) -> torch.Tensor: @@ -993,7 +993,7 @@ class InstructBlipVideoQFormerModel(InstructBlipVideoPreTrainedModel): Arguments: attention_mask (`torch.Tensor`): Mask with ones indicating tokens to attend to, zeros for tokens to ignore. - input_shape (`Tuple[int]`): + input_shape (`tuple[int]`): The shape of the input to the model. device: (`torch.device`): The device of the input to the model. @@ -1032,12 +1032,12 @@ class InstructBlipVideoQFormerModel(InstructBlipVideoPreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1164,13 +1164,13 @@ class InstructBlipVideoForConditionalGenerationModelOutput(ModelOutput): Outputs of the language model. """ - loss: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + loss: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None vision_outputs: Optional[torch.FloatTensor] = None - qformer_outputs: Optional[Tuple[torch.FloatTensor]] = None - language_model_outputs: Optional[Tuple[torch.FloatTensor]] = None + qformer_outputs: Optional[tuple[torch.FloatTensor]] = None + language_model_outputs: Optional[tuple[torch.FloatTensor]] = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["vision_outputs", "qformer_outputs", "language_model_outputs"] @@ -1255,7 +1255,7 @@ class InstructBlipVideoModel(InstructBlipVideoPreTrainedModel): interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, InstructBlipVideoForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipVideoForConditionalGenerationModelOutput]: r""" qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided @@ -1490,7 +1490,7 @@ class InstructBlipVideoForConditionalGeneration(InstructBlipVideoPreTrainedModel interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, InstructBlipVideoForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipVideoForConditionalGenerationModelOutput]: r""" qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided @@ -1539,7 +1539,7 @@ class InstructBlipVideoForConditionalGeneration(InstructBlipVideoPreTrainedModel ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' diff --git a/src/transformers/models/instructblipvideo/modular_instructblipvideo.py b/src/transformers/models/instructblipvideo/modular_instructblipvideo.py index 079abe579ed..e941b34c9f6 100644 --- a/src/transformers/models/instructblipvideo/modular_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/modular_instructblipvideo.py @@ -14,7 +14,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -210,7 +210,7 @@ class InstructBlipVideoModel(InstructBlipModel): interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, InstructBlipVideoForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipVideoForConditionalGenerationModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict # step 1: forward the images through the vision encoder, @@ -381,7 +381,7 @@ class InstructBlipVideoForConditionalGeneration(InstructBlipForConditionalGenera interpolate_pos_encoding: bool = False, use_cache: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, InstructBlipVideoForConditionalGenerationModelOutput]: + ) -> Union[tuple, InstructBlipVideoForConditionalGenerationModelOutput]: r""" ```python >>> from transformers import InstructBlipVideoProcessor, InstructBlipVideoForConditionalGeneration @@ -395,7 +395,7 @@ class InstructBlipVideoForConditionalGeneration(InstructBlipForConditionalGenera ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' diff --git a/src/transformers/models/instructblipvideo/processing_instructblipvideo.py b/src/transformers/models/instructblipvideo/processing_instructblipvideo.py index fad69b72e2f..044b78996c8 100644 --- a/src/transformers/models/instructblipvideo/processing_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/processing_instructblipvideo.py @@ -17,7 +17,7 @@ Processor class for InstructBLIP. Largely copy of Blip2Processor with addition o """ import os -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...processing_utils import ProcessorMixin @@ -73,7 +73,7 @@ class InstructBlipVideoProcessor(ProcessorMixin): def __call__( self, images: VideoInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/instructblipvideo/video_processing_instructblipvideo.py b/src/transformers/models/instructblipvideo/video_processing_instructblipvideo.py index f120006d40d..ea08466568e 100644 --- a/src/transformers/models/instructblipvideo/video_processing_instructblipvideo.py +++ b/src/transformers/models/instructblipvideo/video_processing_instructblipvideo.py @@ -17,7 +17,7 @@ Video processor class for InstructBLIPVideo """ -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ( @@ -75,8 +75,8 @@ class InstructBlipVideoVideoProcessor(BaseVideoProcessor): def _preprocess( self, - videos: List["torch.Tensor"], - video_metadata: Union[List[VideoMetadata], List[dict]], + videos: list["torch.Tensor"], + video_metadata: Union[list[VideoMetadata], list[dict]], do_convert_rgb: bool, do_resize: bool, size: SizeDict, @@ -89,8 +89,8 @@ class InstructBlipVideoVideoProcessor(BaseVideoProcessor): rescale_factor: float, do_normalize: bool, do_sample_frames: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], fps: Optional[int] = None, num_frames: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, diff --git a/src/transformers/models/internvl/modeling_internvl.py b/src/transformers/models/internvl/modeling_internvl.py index 84a2d412ff4..3958c780182 100644 --- a/src/transformers/models/internvl/modeling_internvl.py +++ b/src/transformers/models/internvl/modeling_internvl.py @@ -22,7 +22,7 @@ import collections.abc from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -405,7 +405,7 @@ class InternVLVisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: attention_output, attention_weights = self.attention( self.layernorm_before(hidden_states), # in InternVLVision, layernorm is applied before self-attention output_attentions=output_attentions, @@ -630,7 +630,7 @@ class InternVLModel(InternVLPreTrainedModel): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -640,7 +640,7 @@ class InternVLModel(InternVLPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layer (`int` or `List[int]`): + vision_feature_layer (`int` or `list[int]`): Layer index or list of layer indices to extract features from. Returns: vision_features (`torch.Tensor`): Image feature tensor of shape `(num_images, image_length, embed_dim)`. @@ -688,9 +688,9 @@ class InternVLModel(InternVLPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -698,7 +698,7 @@ class InternVLModel(InternVLPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, InternVLModelOutputWithPast]: + ) -> Union[tuple, InternVLModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -836,9 +836,9 @@ class InternVLCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -886,7 +886,7 @@ class InternVLForConditionalGeneration(InternVLPreTrainedModel, GenerationMixin) def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -918,9 +918,9 @@ class InternVLForConditionalGeneration(InternVLPreTrainedModel, GenerationMixin) pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -931,7 +931,7 @@ class InternVLForConditionalGeneration(InternVLPreTrainedModel, GenerationMixin) logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, InternVLCausalLMOutputWithPast]: + ) -> Union[tuple, InternVLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/internvl/modular_internvl.py b/src/transformers/models/internvl/modular_internvl.py index 248f58f3b78..7ba30c92274 100644 --- a/src/transformers/models/internvl/modular_internvl.py +++ b/src/transformers/models/internvl/modular_internvl.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -356,7 +356,7 @@ class InternVLVisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor], Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[tuple[torch.Tensor], tuple[torch.Tensor, torch.Tensor]]: attention_output, attention_weights = self.attention( self.layernorm_before(hidden_states), # in InternVLVision, layernorm is applied before self-attention output_attentions=output_attentions, @@ -556,7 +556,7 @@ class InternVLModel(LlavaModel): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -566,7 +566,7 @@ class InternVLModel(LlavaModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layer (`int` or `List[int]`): + vision_feature_layer (`int` or `list[int]`): Layer index or list of layer indices to extract features from. Returns: vision_features (`torch.Tensor`): Image feature tensor of shape `(num_images, image_length, embed_dim)`. @@ -614,9 +614,9 @@ class InternVLModel(LlavaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -624,7 +624,7 @@ class InternVLModel(LlavaModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, InternVLModelOutputWithPast]: + ) -> Union[tuple, InternVLModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/internvl/processing_internvl.py b/src/transformers/models/internvl/processing_internvl.py index 5456788c7a2..0193af7080d 100644 --- a/src/transformers/models/internvl/processing_internvl.py +++ b/src/transformers/models/internvl/processing_internvl.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -156,7 +156,7 @@ class InternVLProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos: Optional[VideoInput] = None, **kwargs: Unpack[InternVLProcessorKwargs], @@ -169,14 +169,14 @@ class InternVLProcessor(ProcessorMixin): GotOcr2ImageProcessor's [`~GotOcr2ImageProcessor.__call__`] if `images` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch return_tensors (`str` or [`~utils.TensorType`], *optional*): If set, will return tensors of a particular framework. Acceptable values are: @@ -270,7 +270,7 @@ class InternVLProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/internvl/video_processing_internvl.py b/src/transformers/models/internvl/video_processing_internvl.py index 74f5981af95..149f780c8fd 100644 --- a/src/transformers/models/internvl/video_processing_internvl.py +++ b/src/transformers/models/internvl/video_processing_internvl.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Video processor class for InternVL.""" -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ( @@ -127,8 +127,8 @@ class InternVLVideoProcessor(BaseVideoProcessor): def _preprocess( self, - videos: List["torch.Tensor"], - video_metadata: Union[List[VideoMetadata], List[dict]], + videos: list["torch.Tensor"], + video_metadata: Union[list[VideoMetadata], list[dict]], do_convert_rgb: bool, do_resize: bool, size: SizeDict, @@ -140,8 +140,8 @@ class InternVLVideoProcessor(BaseVideoProcessor): do_pad: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_sample_frames: Optional[bool] = None, fps: Optional[int] = None, num_frames: Optional[int] = None, diff --git a/src/transformers/models/jamba/modeling_jamba.py b/src/transformers/models/jamba/modeling_jamba.py index 70e68559721..52570d8f7f8 100755 --- a/src/transformers/models/jamba/modeling_jamba.py +++ b/src/transformers/models/jamba/modeling_jamba.py @@ -20,7 +20,7 @@ """PyTorch Jamba model.""" import math -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn.functional as F @@ -65,7 +65,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func with gate->router def load_balancing_loss_func( - router_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + router_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -228,8 +228,8 @@ class HybridMambaAttentionDynamicCache(DynamicCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: # Update the cache if self.key_cache[layer_idx].shape[-1] == 0: self.key_cache[layer_idx] = key_states @@ -261,11 +261,11 @@ class HybridMambaAttentionDynamicCache(DynamicCache): return 0 return self.key_cache[layer_idx].shape[-2] - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor], tuple[torch.Tensor]]: raise NotImplementedError("HybridMambaAttentionDynamicCache does not have a legacy cache equivalent.") @classmethod - def from_legacy_cache(cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None) -> "DynamicCache": + def from_legacy_cache(cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None) -> "DynamicCache": raise NotImplementedError("HybridMambaAttentionDynamicCache does not have a legacy cache equivalent.") @@ -314,7 +314,7 @@ class JambaAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -474,7 +474,7 @@ class JambaSdpaAttention(JambaAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -854,7 +854,7 @@ class JambaSparseMoeBlock(nn.Module): self.router = nn.Linear(self.hidden_dim, self.num_experts, bias=False) self.experts = nn.ModuleList([JambaMLP(config) for _ in range(self.num_experts)]) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """ """ batch_size, sequence_length, hidden_dim = hidden_states.shape @@ -916,7 +916,7 @@ class JambaAttentionDecoderLayer(GradientCheckpointingLayer): output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -998,7 +998,7 @@ class JambaMambaDecoderLayer(GradientCheckpointingLayer): output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` diff --git a/src/transformers/models/janus/configuration_janus.py b/src/transformers/models/janus/configuration_janus.py index d90f64b3831..5063640bfc0 100644 --- a/src/transformers/models/janus/configuration_janus.py +++ b/src/transformers/models/janus/configuration_janus.py @@ -19,8 +19,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List - from ...configuration_utils import PretrainedConfig from ...utils import logging from ..auto import CONFIG_MAPPING, AutoConfig @@ -150,7 +148,7 @@ class JanusVQVAEConfig(PretrainedConfig): Number of out channels. base_channels (`int`, *optional*, defaults to 128): Base channel count. - channel_multiplier (`List[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): + channel_multiplier (`list[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): Channel multipliers for each resolution. num_res_blocks (`int`, *optional*, defaults to 2): Number of residual blocks. @@ -182,7 +180,7 @@ class JanusVQVAEConfig(PretrainedConfig): in_channels: int = 3, out_channels: int = 3, base_channels: int = 128, - channel_multiplier: List[int] = [1, 1, 2, 2, 4], + channel_multiplier: list[int] = [1, 1, 2, 2, 4], num_res_blocks: int = 2, dropout: float = 0.0, initializer_range=0.02, diff --git a/src/transformers/models/janus/image_processing_janus.py b/src/transformers/models/janus/image_processing_janus.py index d7d77e78ee1..3b123606130 100644 --- a/src/transformers/models/janus/image_processing_janus.py +++ b/src/transformers/models/janus/image_processing_janus.py @@ -20,7 +20,7 @@ # limitations under the License. from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -82,11 +82,11 @@ class JanusImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -99,14 +99,14 @@ class JanusImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, min_size: int = 14, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, **kwargs, ) -> None: @@ -133,7 +133,7 @@ class JanusImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Union[Dict[str, int], int], + size: Union[dict[str, int], int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -204,13 +204,13 @@ class JanusImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, do_convert_rgb: Optional[bool] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -225,7 +225,7 @@ class JanusImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -238,9 +238,9 @@ class JanusImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. @@ -338,7 +338,7 @@ class JanusImageProcessor(BaseImageProcessor): def pad_to_square( self, image: np.ndarray, - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.array: @@ -348,7 +348,7 @@ class JanusImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -415,8 +415,8 @@ class JanusImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[List[float]] = None, - image_std: Optional[List[float]] = None, + image_mean: Optional[list[float]] = None, + image_std: Optional[list[float]] = None, input_data_format: Optional[str] = None, return_tensors: Optional[str] = None, ): diff --git a/src/transformers/models/janus/modeling_janus.py b/src/transformers/models/janus/modeling_janus.py index 7084382988f..1017bc1c118 100644 --- a/src/transformers/models/janus/modeling_janus.py +++ b/src/transformers/models/janus/modeling_janus.py @@ -21,7 +21,7 @@ import copy from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -140,10 +140,10 @@ class JanusBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -182,10 +182,10 @@ class JanusCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None class JanusVisionEmbeddings(nn.Module): @@ -415,7 +415,7 @@ class JanusVisionEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -553,7 +553,7 @@ class JanusVisionModel(JanusPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1003,7 +1003,7 @@ class JanusVQVAE(JanusPreTrainedModel): def forward( self, pixel_values: torch.FloatTensor, - ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: + ) -> tuple[torch.FloatTensor, torch.FloatTensor]: batch_size = pixel_values.shape[0] quant, embedding_loss, indices = self.encode(pixel_values) decoded_pixel_values = self.decode(indices.view(batch_size, -1)) diff --git a/src/transformers/models/janus/modular_janus.py b/src/transformers/models/janus/modular_janus.py index 17b6565ddcb..5acbf9a0fbb 100644 --- a/src/transformers/models/janus/modular_janus.py +++ b/src/transformers/models/janus/modular_janus.py @@ -16,7 +16,7 @@ import copy from collections.abc import Iterable from dataclasses import dataclass -from typing import Callable, Dict, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -201,7 +201,7 @@ class JanusVQVAEConfig(ChameleonVQVAEConfig): Number of out channels. base_channels (`int`, *optional*, defaults to 128): Base channel count. - channel_multiplier (`List[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): + channel_multiplier (`list[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`): Channel multipliers for each resolution. num_res_blocks (`int`, *optional*, defaults to 2): Number of residual blocks. @@ -230,7 +230,7 @@ class JanusVQVAEConfig(ChameleonVQVAEConfig): in_channels: int = 3, out_channels: int = 3, base_channels: int = 128, - channel_multiplier: List[int] = [1, 1, 2, 2, 4], + channel_multiplier: list[int] = [1, 1, 2, 2, 4], num_res_blocks: int = 2, dropout: float = 0.0, initializer_range=0.02, @@ -840,7 +840,7 @@ class JanusVQVAE(ChameleonVQVAE): def forward( self, pixel_values: torch.FloatTensor, - ) -> Tuple[torch.FloatTensor, torch.FloatTensor]: + ) -> tuple[torch.FloatTensor, torch.FloatTensor]: batch_size = pixel_values.shape[0] quant, embedding_loss, indices = self.encode(pixel_values) decoded_pixel_values = self.decode(indices.view(batch_size, -1)) @@ -1353,11 +1353,11 @@ class JanusImageProcessor(BlipImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -1368,14 +1368,14 @@ class JanusImageProcessor(BlipImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, min_size: int = 14, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, **kwargs, ): @@ -1390,7 +1390,7 @@ class JanusImageProcessor(BlipImageProcessor): def pad_to_square( self, image: np.ndarray, - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.array: @@ -1400,7 +1400,7 @@ class JanusImageProcessor(BlipImageProcessor): Args: image (`np.ndarray`): The image to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -1464,7 +1464,7 @@ class JanusImageProcessor(BlipImageProcessor): def resize( self, image: np.ndarray, - size: Union[Dict[str, int], int], + size: Union[dict[str, int], int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1536,8 +1536,8 @@ class JanusImageProcessor(BlipImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[List[float]] = None, - image_std: Optional[List[float]] = None, + image_mean: Optional[list[float]] = None, + image_std: Optional[list[float]] = None, input_data_format: Optional[str] = None, return_tensors: Optional[str] = None, ): diff --git a/src/transformers/models/janus/processing_janus.py b/src/transformers/models/janus/processing_janus.py index d5f626a24c7..3db5637006e 100644 --- a/src/transformers/models/janus/processing_janus.py +++ b/src/transformers/models/janus/processing_janus.py @@ -16,7 +16,7 @@ Processor class for Janus. """ -from typing import List, Union +from typing import Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput @@ -79,7 +79,7 @@ class JanusProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, images: ImageInput = None, videos=None, audio=None, @@ -93,11 +93,11 @@ class JanusProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. return_tensors (`str` or [`~utils.TensorType`], *optional*): diff --git a/src/transformers/models/jetmoe/modeling_jetmoe.py b/src/transformers/models/jetmoe/modeling_jetmoe.py index 8afe851bd21..ab843099c54 100644 --- a/src/transformers/models/jetmoe/modeling_jetmoe.py +++ b/src/transformers/models/jetmoe/modeling_jetmoe.py @@ -15,7 +15,7 @@ """PyTorch JetMoe model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -48,7 +48,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -497,7 +497,7 @@ class JetMoeAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states, router_logits, topo_info = self.experts.map(hidden_states) @@ -565,7 +565,7 @@ class JetMoeSdpaAttention(JetMoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]], Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]], Optional[torch.Tensor]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -655,8 +655,8 @@ class JetMoeFlashAttention2(JetMoeAttention): output_attentions: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, ) -> Union[ - Tuple[torch.Tensor, Tuple[torch.Tensor]], - Optional[Tuple[torch.Tensor, Tuple[torch.Tensor], Tuple[torch.Tensor, ...]]], + tuple[torch.Tensor, tuple[torch.Tensor]], + Optional[tuple[torch.Tensor, tuple[torch.Tensor], tuple[torch.Tensor, ...]]], ]: """ Forward pass of the JetMoeAttention module. @@ -664,13 +664,13 @@ class JetMoeFlashAttention2(JetMoeAttention): Args: hidden_states (Optional[torch.FloatTensor]): Input hidden states. attention_mask (Optional[torch.FloatTensor]): Attention mask. - layer_past (Optional[Tuple[torch.Tensor]]): Past layer state. + layer_past (Optional[tuple[torch.Tensor]]): Past layer state. use_cache (Optional[bool]): Whether to use cached states. output_attentions (Optional[bool]): Whether to output attention weights. cache_position (Optional[torch.LongTensor]): Position of the cache. Returns: - Union[Tuple[torch.Tensor, Tuple[torch.Tensor]], Optional[Tuple[...]]]: Tuple containing outputs. + Union[tuple[torch.Tensor, tuple[torch.Tensor]], Optional[tuple[...]]]: Tuple containing outputs. """ output_attentions = False bsz, q_len, hidden_size = hidden_states.size() @@ -778,13 +778,13 @@ class JetMoeBlock(nn.Module): self, hidden_states: Optional[torch.FloatTensor], position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Optional[Tuple[torch.Tensor, Tuple[torch.FloatTensor, ...]]]]: + ) -> Union[tuple[torch.Tensor], Optional[tuple[torch.Tensor, tuple[torch.FloatTensor, ...]]]]: # Self Attention attn_output, self_attn_weights, present_key_value, attn_router_logits = self.self_attention( hidden_states=self.input_layernorm(hidden_states), @@ -886,7 +886,7 @@ class JetMoeModel(JetMoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1185,7 +1185,7 @@ class JetMoeForCausalLM(JetMoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/kosmos2/modeling_kosmos2.py b/src/transformers/models/kosmos2/modeling_kosmos2.py index 745563ad120..34dc0848b79 100644 --- a/src/transformers/models/kosmos2/modeling_kosmos2.py +++ b/src/transformers/models/kosmos2/modeling_kosmos2.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -129,14 +129,14 @@ class Kosmos2ModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_embeds: Optional[torch.FloatTensor] = None - projection_attentions: Optional[Tuple[torch.FloatTensor]] = None + projection_attentions: Optional[tuple[torch.FloatTensor]] = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -187,14 +187,14 @@ class Kosmos2ForConditionalGenerationModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_embeds: Optional[torch.FloatTensor] = None - projection_attentions: Optional[Tuple[torch.FloatTensor]] = None + projection_attentions: Optional[tuple[torch.FloatTensor]] = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -337,7 +337,7 @@ class Kosmos2VisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -419,7 +419,7 @@ class Kosmos2VisionEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -478,7 +478,7 @@ class Kosmos2VisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -572,7 +572,7 @@ class Kosmos2VisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -745,12 +745,12 @@ class KosmosTextAttention(nn.Module): self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -878,11 +878,11 @@ class Kosmos2TextBlock(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states # Self Attention @@ -1051,7 +1051,7 @@ class Kosmos2TextTransformer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1059,7 +1059,7 @@ class Kosmos2TextTransformer(nn.Module): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1309,7 +1309,7 @@ class Kosmos2VisionModel(Kosmos2PreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: return self.model( pixel_values=pixel_values, output_attentions=output_attentions, @@ -1346,7 +1346,7 @@ class Kosmos2TextModel(Kosmos2PreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1354,7 +1354,7 @@ class Kosmos2TextModel(Kosmos2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0, @@ -1436,7 +1436,7 @@ class Kosmos2TextForCausalLM(Kosmos2PreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1445,7 +1445,7 @@ class Kosmos2TextForCausalLM(Kosmos2PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0, @@ -1665,7 +1665,7 @@ class Kosmos2Model(Kosmos2PreTrainedModel): image_embeds_position_mask: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, image_embeds: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1675,7 +1675,7 @@ class Kosmos2Model(Kosmos2PreTrainedModel): interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, Kosmos2ModelOutput]: + ) -> Union[tuple, Kosmos2ModelOutput]: r""" image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0, @@ -1801,7 +1801,7 @@ class Kosmos2ForConditionalGeneration(Kosmos2PreTrainedModel, GenerationMixin): image_embeds_position_mask: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, image_embeds: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, @@ -1810,7 +1810,7 @@ class Kosmos2ForConditionalGeneration(Kosmos2PreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Kosmos2ForConditionalGenerationModelOutput]: + ) -> Union[tuple, Kosmos2ForConditionalGenerationModelOutput]: r""" image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0, diff --git a/src/transformers/models/kosmos2/processing_kosmos2.py b/src/transformers/models/kosmos2/processing_kosmos2.py index 3a1c9253824..377940fcadc 100644 --- a/src/transformers/models/kosmos2/processing_kosmos2.py +++ b/src/transformers/models/kosmos2/processing_kosmos2.py @@ -17,7 +17,7 @@ import copy import math import re -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput, is_batched @@ -27,15 +27,15 @@ from ...tokenization_utils_base import BatchEncoding, TextInput BboxInput = Union[ - List[Tuple[int, int]], - List[Tuple[float, float, float, float]], - List[List[Tuple[int, int]]], - List[List[Tuple[float, float, float]]], + list[tuple[int, int]], + list[tuple[float, float, float, float]], + list[list[tuple[int, int]]], + list[list[tuple[float, float, float]]], ] class Kosmos2ImagesKwargs(ImagesKwargs, total=False): - bboxes: Optional[List[float]] + bboxes: Optional[list[float]] num_image_tokens: Optional[int] first_image_token_id: Optional[int] @@ -135,7 +135,7 @@ class Kosmos2Processor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, List[TextInput]] = None, + text: Union[TextInput, list[TextInput]] = None, audio=None, videos=None, **kwargs: Unpack[Kosmos2ProcessorKwargs], @@ -149,7 +149,7 @@ class Kosmos2Processor(ProcessorMixin): The rest of this documentation shows the arguments specific to `Kosmos2Processor`. Args: - bboxes (`Union[List[Tuple[int]], List[Tuple[float]], List[List[Tuple[int]]], List[List[Tuple[float]]]]`, *optional*): + bboxes (`Union[list[tuple[int]], list[tuple[float]], list[list[tuple[int]]], list[list[tuple[float]]]]`, *optional*): The bounding bboxes associated to `texts`. num_image_tokens (`int`, *optional* defaults to 64): The number of (consecutive) places that are used to mark the placeholders to store image information. @@ -341,24 +341,24 @@ class Kosmos2Processor(ProcessorMixin): def preprocess_examples( self, - texts: Union[TextInput, List[TextInput]], + texts: Union[TextInput, list[TextInput]], images: ImageInput = None, bboxes: BboxInput = None, num_image_tokens: Optional[int] = 64, - ) -> Union[str, List[str]]: + ) -> Union[str, list[str]]: """Add image and bounding box information to `texts` as image and patch index tokens. Args: - texts (`Union[TextInput, List[TextInput]]`): The texts to be processed. + texts (`Union[TextInput, list[TextInput]]`): The texts to be processed. images (`ImageInput`, *optional*): The images associated to `texts`. - bboxes (`Union[List[Tuple[int]], List[Tuple[float]], List[List[Tuple[int]]], List[List[Tuple[float]]]]`, *optional*): + bboxes (`Union[list[tuple[int]], list[tuple[float]], list[list[tuple[int]]], list[list[tuple[float]]]]`, *optional*): The bounding bboxes associated to `texts`. num_image_tokens (`int`, *optional*, defaults to 64): The number of image tokens (used as latent queries). This should corresponds to the `latent_query_num` attribute in `Kosmos2Config`. Returns: - `Union[TextInput, List[TextInput]]`: The processed texts with image and patch index tokens. + `Union[TextInput, list[TextInput]]`: The processed texts with image and patch index tokens. """ # These are fake `` tokens enclosed between (the actual) `` token and ``. img_tokens = [self.boi_token] * num_image_tokens @@ -441,7 +441,7 @@ class Kosmos2Processor(ProcessorMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text. + `list[str]`: The decoded text. """ generated_texts = self.batch_decode(generated_outputs, skip_special_tokens=skip_special_tokens, **kwargs) return [self.post_process_generation(text, cleanup_and_extract=False) for text in generated_texts] @@ -453,7 +453,7 @@ class Kosmos2Processor(ProcessorMixin): image_processor_input_names = self.image_processor.model_input_names return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names)) - def _insert_patch_index_tokens(self, text: str, bboxes: Union[List[Tuple[int]], List[Tuple[float]]]) -> str: + def _insert_patch_index_tokens(self, text: str, bboxes: Union[list[tuple[int]], list[tuple[float]]]) -> str: if bboxes is None or len(bboxes) == 0: return text @@ -499,8 +499,8 @@ class Kosmos2Processor(ProcessorMixin): return text def _convert_bbox_to_patch_index_tokens( - self, bbox: Union[Tuple[int, int], Tuple[float, float, float, float]] - ) -> Tuple[str, str]: + self, bbox: Union[tuple[int, int], tuple[float, float, float, float]] + ) -> tuple[str, str]: # already computed patch indices if len(bbox) == 2: idx_1, idx_2 = bbox @@ -516,17 +516,17 @@ class Kosmos2Processor(ProcessorMixin): return token_1, token_2 -def coordinate_to_patch_index(bbox: Tuple[float, float, float, float], num_patches_per_side: int) -> Tuple[int, int]: +def coordinate_to_patch_index(bbox: tuple[float, float, float, float], num_patches_per_side: int) -> tuple[int, int]: """Convert a bounding box to a pair of patch indices. Args: - bbox (`Tuple[float, float, float, float]`): + bbox (`tuple[float, float, float, float]`): The 4 coordinates of the bounding box, with the format being (x1, y1, x2, y2) specifying the upper-left and lower-right corners of the box. It should have x2 > x1 and y2 > y1. num_patches_per_side (`int`): the number of patches along each side. Returns: - `Tuple[int, int]`: A pair of patch indices representing the upper-left patch and lower-right patch. + `tuple[int, int]`: A pair of patch indices representing the upper-left patch and lower-right patch. """ (x1, y1, x2, y2) = bbox @@ -558,7 +558,7 @@ def patch_index_to_coordinate(ul_idx: int, lr_idx: int, num_patches_per_side: in num_patches_per_side (`int`): the number of patches along each side. Returns: - `Tuple[float]`: the normalized coordinates of the bounding box, in the form (x1, y1, x2, y2). + `tuple[float]`: the normalized coordinates of the bounding box, in the form (x1, y1, x2, y2). """ # Compute the size of each cell in the grid cell_size = 1.0 / num_patches_per_side diff --git a/src/transformers/models/layoutlm/configuration_layoutlm.py b/src/transformers/models/layoutlm/configuration_layoutlm.py index d053e5e57c0..1a440cf55e8 100644 --- a/src/transformers/models/layoutlm/configuration_layoutlm.py +++ b/src/transformers/models/layoutlm/configuration_layoutlm.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import Any, List, Optional +from typing import Any, Optional from ... import PretrainedConfig, PreTrainedTokenizer from ...onnx import OnnxConfig, PatchingSpec @@ -140,7 +140,7 @@ class LayoutLMOnnxConfig(OnnxConfig): self, config: PretrainedConfig, task: str = "default", - patching_specs: Optional[List[PatchingSpec]] = None, + patching_specs: Optional[list[PatchingSpec]] = None, ): super().__init__(config, task=task, patching_specs=patching_specs) self.max_2d_positions = config.max_2d_position_embeddings - 1 diff --git a/src/transformers/models/layoutlm/modeling_layoutlm.py b/src/transformers/models/layoutlm/modeling_layoutlm.py index 68d05e509f9..6ce2e7e2dcf 100644 --- a/src/transformers/models/layoutlm/modeling_layoutlm.py +++ b/src/transformers/models/layoutlm/modeling_layoutlm.py @@ -15,7 +15,7 @@ """PyTorch LayoutLM model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -159,9 +159,9 @@ class LayoutLMSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -309,9 +309,9 @@ class LayoutLMAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -380,9 +380,9 @@ class LayoutLMLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -459,12 +459,12 @@ class LayoutLMEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -674,7 +674,7 @@ class LayoutLMModel(LayoutLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -823,7 +823,7 @@ class LayoutLMForMaskedLM(LayoutLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -946,7 +946,7 @@ class LayoutLMForSequenceClassification(LayoutLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1084,7 +1084,7 @@ class LayoutLMForTokenClassification(LayoutLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1202,7 +1202,7 @@ class LayoutLMForQuestionAnswering(LayoutLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, diff --git a/src/transformers/models/layoutlm/modeling_tf_layoutlm.py b/src/transformers/models/layoutlm/modeling_tf_layoutlm.py index c17d0f1ec57..5f35948771e 100644 --- a/src/transformers/models/layoutlm/modeling_tf_layoutlm.py +++ b/src/transformers/models/layoutlm/modeling_tf_layoutlm.py @@ -18,7 +18,7 @@ from __future__ import annotations import math import warnings -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -232,10 +232,10 @@ class TFLayoutLMSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -370,10 +370,10 @@ class TFLayoutLMAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -487,10 +487,10 @@ class TFLayoutLMLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -583,13 +583,13 @@ class TFLayoutLMEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -747,7 +747,7 @@ class TFLayoutLMLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -829,7 +829,7 @@ class TFLayoutLMMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -1074,7 +1074,7 @@ class TFLayoutLMModel(TFLayoutLMPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: r""" Returns: @@ -1180,7 +1180,7 @@ class TFLayoutLMForMaskedLM(TFLayoutLMPreTrainedModel, TFMaskedLanguageModelingL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1309,7 +1309,7 @@ class TFLayoutLMForSequenceClassification(TFLayoutLMPreTrainedModel, TFSequenceC return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1445,7 +1445,7 @@ class TFLayoutLMForTokenClassification(TFLayoutLMPreTrainedModel, TFTokenClassif return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1578,7 +1578,7 @@ class TFLayoutLMForQuestionAnswering(TFLayoutLMPreTrainedModel, TFQuestionAnswer start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/layoutlm/tokenization_layoutlm.py b/src/transformers/models/layoutlm/tokenization_layoutlm.py index f7ca50ec4d7..fbb6a92cd42 100644 --- a/src/transformers/models/layoutlm/tokenization_layoutlm.py +++ b/src/transformers/models/layoutlm/tokenization_layoutlm.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -187,8 +187,8 @@ class LayoutLMTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A LayoutLM sequence has the following format: @@ -212,8 +212,8 @@ class LayoutLMTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -239,7 +239,7 @@ class LayoutLMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/layoutlm/tokenization_layoutlm_fast.py b/src/transformers/models/layoutlm/tokenization_layoutlm_fast.py index 09bcc18e3b8..c7ade6e0b8c 100644 --- a/src/transformers/models/layoutlm/tokenization_layoutlm_fast.py +++ b/src/transformers/models/layoutlm/tokenization_layoutlm_fast.py @@ -15,7 +15,7 @@ """Tokenization class for model LayoutLM.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -139,7 +139,7 @@ class LayoutLMTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/layoutlmv2/configuration_layoutlmv2.py b/src/transformers/models/layoutlmv2/configuration_layoutlmv2.py index 6ffdec66385..b729ddbb1d4 100644 --- a/src/transformers/models/layoutlmv2/configuration_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/configuration_layoutlmv2.py @@ -78,7 +78,7 @@ class LayoutLMv2Config(PretrainedConfig): The maximum number of relative 2D positions in the self-attention mechanism. rel_2d_pos_bins (`int`, *optional*, defaults to 64): The number of 2D relative position bins in the self-attention mechanism. - image_feature_pool_shape (`List[int]`, *optional*, defaults to [7, 7, 256]): + image_feature_pool_shape (`list[int]`, *optional*, defaults to [7, 7, 256]): The shape of the average-pooled feature map. coordinate_size (`int`, *optional*, defaults to 128): Dimension of the coordinate embeddings. diff --git a/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py b/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py index 8a73e443de5..dfb61c352dc 100644 --- a/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/image_processing_layoutlmv2.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for LayoutLMv2.""" -from typing import Dict, Optional, Union +from typing import Optional, Union import numpy as np @@ -108,7 +108,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to `(size["height"], size["width"])`. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): Resampling filter to use if resizing the image. Can be overridden by the `resample` parameter in the @@ -129,7 +129,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, apply_ocr: bool = True, ocr_lang: Optional[str] = None, @@ -151,7 +151,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -163,7 +163,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -201,7 +201,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, apply_ocr: Optional[bool] = None, ocr_lang: Optional[str] = None, @@ -218,7 +218,7 @@ class LayoutLMv2ImageProcessor(BaseImageProcessor): Image to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Desired size of the output image after resizing. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PIL.Image` resampling diff --git a/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py b/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py index 4333e2c4c87..fa89c6c45b4 100755 --- a/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py @@ -15,7 +15,7 @@ """PyTorch LayoutLMv2 model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -739,7 +739,7 @@ class LayoutLMv2Model(LayoutLMv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bbox (`torch.LongTensor` of shape `((batch_size, sequence_length), 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -909,7 +909,7 @@ class LayoutLMv2ForSequenceClassification(LayoutLMv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `batch_size, sequence_length`): Indices of input sequence tokens in the vocabulary. @@ -1114,7 +1114,7 @@ class LayoutLMv2ForTokenClassification(LayoutLMv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `batch_size, sequence_length`): Indices of input sequence tokens in the vocabulary. @@ -1267,7 +1267,7 @@ class LayoutLMv2ForQuestionAnswering(LayoutLMv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `batch_size, sequence_length`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/layoutlmv2/processing_layoutlmv2.py b/src/transformers/models/layoutlmv2/processing_layoutlmv2.py index a5ac6681c28..9f8453b492b 100644 --- a/src/transformers/models/layoutlmv2/processing_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/processing_layoutlmv2.py @@ -17,7 +17,7 @@ Processor class for LayoutLMv2. """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy @@ -69,10 +69,10 @@ class LayoutLMv2Processor(ProcessorMixin): def __call__( self, images, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = False, diff --git a/src/transformers/models/layoutlmv2/tokenization_layoutlmv2.py b/src/transformers/models/layoutlmv2/tokenization_layoutlmv2.py index a607777d494..e4a546fc05b 100644 --- a/src/transformers/models/layoutlmv2/tokenization_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/tokenization_layoutlmv2.py @@ -18,7 +18,7 @@ import collections import os import sys import unicodedata -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from ...tokenization_utils import AddedToken, PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...tokenization_utils_base import ( @@ -215,7 +215,7 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): tokenize_chinese_chars=True, strip_accents=None, model_max_length: int = 512, - additional_special_tokens: Optional[List[str]] = None, + additional_special_tokens: Optional[list[str]] = None, **kwargs, ): sep_token = AddedToken(sep_token, special=True) if isinstance(sep_token, str) else sep_token @@ -306,8 +306,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -331,8 +331,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -358,7 +358,7 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( @@ -381,10 +381,10 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): @add_end_docstrings(LAYOUTLMV2_ENCODE_KWARGS_DOCSTRING, LAYOUTLMV2_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -534,13 +534,13 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -594,13 +594,13 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -653,8 +653,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): self, batch_text_or_text_pairs, is_pair: Optional[bool] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -727,8 +727,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -745,7 +745,7 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): return_length: bool = False, verbose: bool = True, **kwargs, - ) -> List[int]: + ) -> list[int]: encoded_inputs = self.encode_plus( text=text, text_pair=text_pair, @@ -776,8 +776,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -844,8 +844,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -899,8 +899,8 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1109,15 +1109,15 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): def truncate_sequences( self, - ids: List[int], - token_boxes: List[List[int]], - pair_ids: Optional[List[int]] = None, - pair_token_boxes: Optional[List[List[int]]] = None, - labels: Optional[List[int]] = None, + ids: list[int], + token_boxes: list[list[int]], + pair_ids: Optional[list[int]] = None, + pair_token_boxes: Optional[list[list[int]]] = None, + labels: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Truncates a sequence pair in-place following the strategy. @@ -1234,7 +1234,7 @@ class LayoutLMv2Tokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, diff --git a/src/transformers/models/layoutlmv2/tokenization_layoutlmv2_fast.py b/src/transformers/models/layoutlmv2/tokenization_layoutlmv2_fast.py index f6c9d3a1d26..a7c15020587 100644 --- a/src/transformers/models/layoutlmv2/tokenization_layoutlmv2_fast.py +++ b/src/transformers/models/layoutlmv2/tokenization_layoutlmv2_fast.py @@ -18,7 +18,7 @@ and _encode_plus, in which the Rust tokenizer is used. """ import json -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from tokenizers import normalizers @@ -155,10 +155,10 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): @add_end_docstrings(LAYOUTLMV2_ENCODE_KWARGS_DOCSTRING, LAYOUTLMV2_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -308,13 +308,13 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -365,7 +365,7 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): **kwargs, ) - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: batched_input = [(text, pair)] if pair else [text] encodings = self._tokenizer.encode_batch( batched_input, add_special_tokens=add_special_tokens, is_pretokenized=False, **kwargs @@ -378,8 +378,8 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -445,13 +445,13 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -604,8 +604,8 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -670,7 +670,7 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -781,7 +781,7 @@ class LayoutLMv2TokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py b/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py index 1a93d76e921..5d6b690bb61 100644 --- a/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/image_processing_layoutlmv3.py @@ -15,7 +15,7 @@ """Image processor class for LayoutLMv3.""" from collections.abc import Iterable -from typing import Dict, Optional, Union +from typing import Optional, Union import numpy as np @@ -111,7 +111,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to `(size["height"], size["width"])`. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): Resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. @@ -147,7 +147,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_value: float = 1 / 255, @@ -179,7 +179,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -191,7 +191,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -229,7 +229,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, @@ -252,7 +252,7 @@ class LayoutLMv3ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Desired size of the output image after applying `resize`. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the `PILImageResampling` filters. diff --git a/src/transformers/models/layoutlmv3/modeling_layoutlmv3.py b/src/transformers/models/layoutlmv3/modeling_layoutlmv3.py index 9e1569d11f5..83f87ec5281 100644 --- a/src/transformers/models/layoutlmv3/modeling_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/modeling_layoutlmv3.py @@ -16,7 +16,7 @@ import collections import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -693,7 +693,7 @@ class LayoutLMv3Model(LayoutLMv3PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, token_sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -942,7 +942,7 @@ class LayoutLMv3ForTokenClassification(LayoutLMv3PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, pixel_values: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1044,7 +1044,7 @@ class LayoutLMv3ForQuestionAnswering(LayoutLMv3PreTrainedModel): return_dict: Optional[bool] = None, bbox: Optional[torch.LongTensor] = None, pixel_values: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1164,7 +1164,7 @@ class LayoutLMv3ForSequenceClassification(LayoutLMv3PreTrainedModel): return_dict: Optional[bool] = None, bbox: Optional[torch.LongTensor] = None, pixel_values: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, diff --git a/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py b/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py index d145b51b3c1..d6703420e4a 100644 --- a/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/modeling_tf_layoutlmv3.py @@ -18,7 +18,7 @@ from __future__ import annotations import collections import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -363,7 +363,7 @@ class TFLayoutLMv3SelfAttention(keras.layers.Layer): rel_pos: tf.Tensor | None = None, rel_2d_pos: tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], Tuple[tf.Tensor, tf.Tensor]]: + ) -> Union[tuple[tf.Tensor], tuple[tf.Tensor, tf.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -468,7 +468,7 @@ class TFLayoutLMv3Attention(keras.layers.Layer): rel_pos: tf.Tensor | None = None, rel_2d_pos: tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], Tuple[tf.Tensor, tf.Tensor]]: + ) -> Union[tuple[tf.Tensor], tuple[tf.Tensor, tf.Tensor]]: self_outputs = self.self_attention( hidden_states, attention_mask, @@ -571,7 +571,7 @@ class TFLayoutLMv3Layer(keras.layers.Layer): rel_pos: tf.Tensor | None = None, rel_2d_pos: tf.Tensor | None = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], Tuple[tf.Tensor, tf.Tensor]]: + ) -> Union[tuple[tf.Tensor], tuple[tf.Tensor, tf.Tensor]]: self_attention_outputs = self.attention( hidden_states, attention_mask, @@ -713,9 +713,9 @@ class TFLayoutLMv3Encoder(keras.layers.Layer): training: bool = False, ) -> Union[ TFBaseModelOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], ]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -855,7 +855,7 @@ class TFLayoutLMv3MainLayer(keras.layers.Layer): """ raise NotImplementedError - def init_visual_bbox(self, image_size: Tuple[int, int], max_len: int = 1000): + def init_visual_bbox(self, image_size: tuple[int, int], max_len: int = 1000): # We should not hardcode max_len to 1000, but it is done by the reference implementation, # so we keep it for compatibility with the pretrained weights. The more correct approach # would have been to pass on max_len=config.max_2d_position_embeddings - 1. @@ -926,7 +926,7 @@ class TFLayoutLMv3MainLayer(keras.layers.Layer): return extended_attention_mask - def get_head_mask(self, head_mask: tf.Tensor | None) -> Union[tf.Tensor, List[tf.Tensor | None]]: + def get_head_mask(self, head_mask: tf.Tensor | None) -> Union[tf.Tensor, list[tf.Tensor | None]]: if head_mask is None: return [None] * self.config.num_hidden_layers @@ -968,9 +968,9 @@ class TFLayoutLMv3MainLayer(keras.layers.Layer): training: bool = False, ) -> Union[ TFBaseModelOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], ]: # This method can be called with a variety of modalities: # 1. text + layout @@ -1280,9 +1280,9 @@ class TFLayoutLMv3Model(TFLayoutLMv3PreTrainedModel): training: bool = False, ) -> Union[ TFBaseModelOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], ]: r""" Returns: @@ -1421,10 +1421,10 @@ class TFLayoutLMv3ForSequenceClassification(TFLayoutLMv3PreTrainedModel, TFSeque training: Optional[bool] = False, ) -> Union[ TFSequenceClassifierOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], ]: """ Returns: @@ -1546,10 +1546,10 @@ class TFLayoutLMv3ForTokenClassification(TFLayoutLMv3PreTrainedModel, TFTokenCla training: Optional[bool] = False, ) -> Union[ TFTokenClassifierOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], ]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1676,10 +1676,10 @@ class TFLayoutLMv3ForQuestionAnswering(TFLayoutLMv3PreTrainedModel, TFQuestionAn training: bool = False, ) -> Union[ TFQuestionAnsweringModelOutput, - Tuple[tf.Tensor], - Tuple[tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor], - Tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor], + tuple[tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor], + tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor], ]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/layoutlmv3/processing_layoutlmv3.py b/src/transformers/models/layoutlmv3/processing_layoutlmv3.py index 209272ca354..d38090af3f2 100644 --- a/src/transformers/models/layoutlmv3/processing_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/processing_layoutlmv3.py @@ -17,7 +17,7 @@ Processor class for LayoutLMv3. """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy @@ -69,10 +69,10 @@ class LayoutLMv3Processor(ProcessorMixin): def __call__( self, images, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/layoutlmv3/tokenization_layoutlmv3.py b/src/transformers/models/layoutlmv3/tokenization_layoutlmv3.py index 7758ba0acc6..6e2ef36612f 100644 --- a/src/transformers/models/layoutlmv3/tokenization_layoutlmv3.py +++ b/src/transformers/models/layoutlmv3/tokenization_layoutlmv3.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import regex as re @@ -240,11 +240,11 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): add_prefix_space (`bool`, *optional*, defaults to `True`): Whether or not to add an initial space to the input. This allows to treat the leading word just as any other word. (RoBERTa tokenizer detect beginning of words by the preceding space). - cls_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + cls_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [CLS] token. - sep_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + sep_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's @@ -410,7 +410,7 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): return text # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -441,8 +441,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RoBERTa sequence has the following format: @@ -451,13 +451,13 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -467,22 +467,22 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -495,20 +495,20 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -533,10 +533,10 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.__call__ def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -687,13 +687,13 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -748,13 +748,13 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -808,8 +808,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): self, batch_text_or_text_pairs, is_pair: Optional[bool] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -883,8 +883,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -901,7 +901,7 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): return_length: bool = False, verbose: bool = True, **kwargs, - ) -> List[int]: + ) -> list[int]: encoded_inputs = self.encode_plus( text=text, text_pair=text_pair, @@ -933,8 +933,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1002,8 +1002,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1057,8 +1057,8 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1089,9 +1089,9 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): labeled with -100, such that they will be ignored by the loss function. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples). """ @@ -1270,15 +1270,15 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer.truncate_sequences def truncate_sequences( self, - ids: List[int], - token_boxes: List[List[int]], - pair_ids: Optional[List[int]] = None, - pair_token_boxes: Optional[List[List[int]]] = None, - labels: Optional[List[int]] = None, + ids: list[int], + token_boxes: list[list[int]], + pair_ids: Optional[list[int]] = None, + pair_token_boxes: Optional[list[list[int]]] = None, + labels: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Truncates a sequence pair in-place following the strategy. @@ -1396,7 +1396,7 @@ class LayoutLMv3Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2.LayoutLMv2Tokenizer._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, diff --git a/src/transformers/models/layoutlmv3/tokenization_layoutlmv3_fast.py b/src/transformers/models/layoutlmv3/tokenization_layoutlmv3_fast.py index 3d0cd26d80c..aa2fb67b136 100644 --- a/src/transformers/models/layoutlmv3/tokenization_layoutlmv3_fast.py +++ b/src/transformers/models/layoutlmv3/tokenization_layoutlmv3_fast.py @@ -18,7 +18,7 @@ and _encode_plus, in which the Rust tokenizer is used. """ import json -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from tokenizers import processors @@ -101,11 +101,11 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): other word. (RoBERTa tokenizer detect beginning of words by the preceding space). trim_offsets (`bool`, *optional*, defaults to `True`): Whether the post processing step should trim offsets to avoid including whitespaces. - cls_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + cls_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [CLS] token. - sep_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + sep_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's @@ -199,10 +199,10 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.__call__ def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -353,13 +353,13 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -411,7 +411,7 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): ) # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.tokenize - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: batched_input = [(text, pair)] if pair else [text] encodings = self._tokenizer.encode_batch( batched_input, add_special_tokens=add_special_tokens, is_pretokenized=False, **kwargs @@ -425,8 +425,8 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -492,13 +492,13 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -538,9 +538,9 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): ) # Convert encoding to dict - # `Tokens` has type: Tuple[ - # List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]], - # List[EncodingFast] + # `Tokens` has type: tuple[ + # list[dict[str, list[list[int]]]] or list[dict[str, 2D-Tensor]], + # list[EncodingFast] # ] # with nested dimensions corresponding to batch, overflows, sequence length tokens_and_encodings = [ @@ -657,8 +657,8 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -724,7 +724,7 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -812,7 +812,7 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): return encoded_inputs # Copied from transformers.models.layoutlmv2.tokenization_layoutlmv2_fast.LayoutLMv2TokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -824,18 +824,18 @@ class LayoutLMv3TokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Args: Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not: make use of token type ids, therefore a list of zeros is returned. - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/layoutxlm/processing_layoutxlm.py b/src/transformers/models/layoutxlm/processing_layoutxlm.py index b325221d9f4..c50b43576c1 100644 --- a/src/transformers/models/layoutxlm/processing_layoutxlm.py +++ b/src/transformers/models/layoutxlm/processing_layoutxlm.py @@ -17,7 +17,7 @@ Processor class for LayoutXLM. """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy @@ -68,10 +68,10 @@ class LayoutXLMProcessor(ProcessorMixin): def __call__( self, images, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/layoutxlm/tokenization_layoutxlm.py b/src/transformers/models/layoutxlm/tokenization_layoutxlm.py index 8aa85b4f318..3dbe3c21a63 100644 --- a/src/transformers/models/layoutxlm/tokenization_layoutxlm.py +++ b/src/transformers/models/layoutxlm/tokenization_layoutxlm.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece as spm @@ -191,11 +191,11 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - cls_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + cls_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [CLS] token. - sep_token_box (`List[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): + sep_token_box (`list[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's @@ -241,7 +241,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): pad_token_box=[0, 0, 0, 0], pad_token_label=-100, only_label_first_subword=True, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -309,8 +309,8 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): self.sp_model.LoadFromSerializedProto(self.sp_model_proto) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -319,13 +319,13 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -335,22 +335,22 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -363,20 +363,20 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -396,7 +396,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -419,7 +419,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -439,10 +439,10 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): @add_end_docstrings(LAYOUTXLM_ENCODE_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -465,16 +465,16 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): sequences with word-level normalized bounding boxes and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - boxes (`List[List[int]]`, `List[List[List[int]]]`): + boxes (`list[list[int]]`, `list[list[list[int]]]`): Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale. - word_labels (`List[int]`, `List[List[int]]`, *optional*): + word_labels (`list[int]`, `list[list[int]]`, *optional*): Word-level integer labels (for token classification tasks such as FUNSD, CORD). """ @@ -502,18 +502,18 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = words if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be words if not isinstance(text, (list, tuple)): raise ValueError( - "Words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -591,13 +591,13 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -650,8 +650,8 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): self, batch_text_or_text_pairs, is_pair: Optional[bool] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -723,8 +723,8 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -778,8 +778,8 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -808,9 +808,9 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): labeled with -100, such that they will be ignored by the loss function. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples). """ @@ -975,30 +975,30 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): def truncate_sequences( self, - ids: List[int], - token_boxes: List[List[int]], - pair_ids: Optional[List[int]] = None, - pair_token_boxes: Optional[List[List[int]]] = None, - labels: Optional[List[int]] = None, + ids: list[int], + token_boxes: list[list[int]], + pair_ids: Optional[list[int]] = None, + pair_token_boxes: Optional[list[list[int]]] = None, + labels: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Truncates a sequence pair in-place following the strategy. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - token_boxes (`List[List[int]]`): + token_boxes (`list[list[int]]`): Bounding boxes of the first sequence. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - pair_token_boxes (`List[List[int]]`, *optional*): + pair_token_boxes (`list[list[int]]`, *optional*): Bounding boxes of the second sequence. - labels (`List[int]`, *optional*): + labels (`list[int]`, *optional*): Labels of the first sequence (for token classification tasks). num_tokens_to_remove (`int`, *optional*, defaults to 0): Number of tokens to remove using the truncation strategy. @@ -1022,7 +1022,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): sequence returned. The value of this argument defines the number of additional tokens. Returns: - `Tuple[List[int], List[int], List[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of + `tuple[list[int], list[int], list[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of overflowing tokens. """ if num_tokens_to_remove <= 0: @@ -1100,7 +1100,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -1112,7 +1112,7 @@ class LayoutXLMTokenizer(PreTrainedTokenizer): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. diff --git a/src/transformers/models/layoutxlm/tokenization_layoutxlm_fast.py b/src/transformers/models/layoutxlm/tokenization_layoutxlm_fast.py index 7cf8655cc67..cca7a805fed 100644 --- a/src/transformers/models/layoutxlm/tokenization_layoutxlm_fast.py +++ b/src/transformers/models/layoutxlm/tokenization_layoutxlm_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from ...tokenization_utils import AddedToken from ...tokenization_utils_base import ( @@ -193,18 +193,18 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - cls_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + cls_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [CLS] token. - sep_token_box (`List[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): + sep_token_box (`list[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's CrossEntropyLoss. only_label_first_subword (`bool`, *optional*, defaults to `True`): Whether or not to only label the first subword, in case word labels are provided. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -263,10 +263,10 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): @add_end_docstrings(LAYOUTXLM_ENCODE_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -289,16 +289,16 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): sequences with word-level normalized bounding boxes and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - boxes (`List[List[int]]`, `List[List[List[int]]]`): + boxes (`list[list[int]]`, `list[list[list[int]]]`): Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale. - word_labels (`List[int]`, `List[List[int]]`, *optional*): + word_labels (`list[int]`, `list[list[int]]`, *optional*): Word-level integer labels (for token classification tasks such as FUNSD, CORD). """ @@ -326,18 +326,18 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = words if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be words if not isinstance(text, (list, tuple)): raise ValueError( - "Words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -412,7 +412,7 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): **kwargs, ) - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: batched_input = [(text, pair)] if pair else [text] self._tokenizer.encode_special_tokens = kwargs.pop( @@ -428,13 +428,13 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -475,9 +475,9 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): ) # Convert encoding to dict - # `Tokens` has type: Tuple[ - # List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]], - # List[EncodingFast] + # `Tokens` has type: tuple[ + # list[dict[str, list[list[int]]]] or list[dict[str, 2D-Tensor]], + # list[EncodingFast] # ] # with nested dimensions corresponding to batch, overflows, sequence length tokens_and_encodings = [ @@ -588,8 +588,8 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -654,7 +654,7 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -666,7 +666,7 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. @@ -742,8 +742,8 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): return encoded_inputs def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -752,13 +752,13 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -768,20 +768,20 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -792,7 +792,7 @@ class LayoutXLMTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/led/configuration_led.py b/src/transformers/models/led/configuration_led.py index 0dcc3ecce42..57809df4aa8 100644 --- a/src/transformers/models/led/configuration_led.py +++ b/src/transformers/models/led/configuration_led.py @@ -14,7 +14,7 @@ # limitations under the License. """LED model configuration""" -from typing import List, Union +from typing import Union from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -127,7 +127,7 @@ class LEDConfig(PretrainedConfig): pad_token_id=1, bos_token_id=0, eos_token_id=2, - attention_window: Union[List[int], int] = 512, + attention_window: Union[list[int], int] = 512, **kwargs, ): self.vocab_size = vocab_size diff --git a/src/transformers/models/led/modeling_led.py b/src/transformers/models/led/modeling_led.py index 1783fe06ecd..bc573861321 100755 --- a/src/transformers/models/led/modeling_led.py +++ b/src/transformers/models/led/modeling_led.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -738,7 +738,7 @@ class LEDEncoderAttention(nn.Module): is_index_global_attn: Optional[torch.Tensor] = None, is_global_attn: Optional[bool] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" self_outputs = self.longformer_self_attn( @@ -793,11 +793,11 @@ class LEDDecoderAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -997,7 +997,7 @@ class LEDDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ): @@ -1169,9 +1169,9 @@ class LEDEncoderBaseModelOutput(ModelOutput): """ last_hidden_state: torch.FloatTensor - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1186,7 +1186,7 @@ class LEDSeq2SeqModelOutput(ModelOutput): If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1, hidden_size)` is output. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1232,14 +1232,14 @@ class LEDSeq2SeqModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1252,7 +1252,7 @@ class LEDSeq2SeqLMOutput(ModelOutput): Language modeling loss. logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1299,14 +1299,14 @@ class LEDSeq2SeqLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1319,7 +1319,7 @@ class LEDSeq2SeqSequenceClassifierOutput(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1366,14 +1366,14 @@ class LEDSeq2SeqSequenceClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1388,7 +1388,7 @@ class LEDSeq2SeqQuestionAnsweringModelOutput(ModelOutput): Span-start scores (before SoftMax). end_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Span-end scores (before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1436,14 +1436,14 @@ class LEDSeq2SeqQuestionAnsweringModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - encoder_global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + encoder_global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None class LEDEncoder(LEDPreTrainedModel): @@ -2040,16 +2040,16 @@ class LEDModel(LEDPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, global_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], LEDSeq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], LEDSeq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -2209,9 +2209,9 @@ class LEDForConditionalGeneration(LEDPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, global_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2219,7 +2219,7 @@ class LEDForConditionalGeneration(LEDPreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], LEDSeq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], LEDSeq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -2416,7 +2416,7 @@ class LEDForSequenceClassification(LEDPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, global_attention_mask: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, @@ -2425,7 +2425,7 @@ class LEDForSequenceClassification(LEDPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], LEDSeq2SeqSequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], LEDSeq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -2567,7 +2567,7 @@ class LEDForQuestionAnswering(LEDPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, global_attention_mask: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, @@ -2577,7 +2577,7 @@ class LEDForQuestionAnswering(LEDPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], LEDSeq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], LEDSeq2SeqQuestionAnsweringModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/led/modeling_tf_led.py b/src/transformers/models/led/modeling_tf_led.py index c867cebe363..c964c78784a 100644 --- a/src/transformers/models/led/modeling_tf_led.py +++ b/src/transformers/models/led/modeling_tf_led.py @@ -18,7 +18,7 @@ from __future__ import annotations import random from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -1071,11 +1071,11 @@ class TFLEDDecoderAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training=False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1322,9 +1322,9 @@ class TFLEDDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, encoder_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training=False, - ) -> Tuple[tf.Tensor, tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)* @@ -1471,9 +1471,9 @@ class TFLEDEncoderBaseModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -1488,7 +1488,7 @@ class TFLEDSeq2SeqModelOutput(ModelOutput): If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1, hidden_size)` is output. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1534,14 +1534,14 @@ class TFLEDSeq2SeqModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor, ...] | None = None - decoder_attentions: Tuple[tf.Tensor, ...] | None = None - cross_attentions: Tuple[tf.Tensor, ...] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor, ...] | None = None + decoder_attentions: tuple[tf.Tensor, ...] | None = None + cross_attentions: tuple[tf.Tensor, ...] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor, ...] | None = None - encoder_attentions: Tuple[tf.Tensor, ...] | None = None - encoder_global_attentions: Tuple[tf.Tensor, ...] | None = None + encoder_hidden_states: tuple[tf.Tensor, ...] | None = None + encoder_attentions: tuple[tf.Tensor, ...] | None = None + encoder_global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -1554,7 +1554,7 @@ class TFLEDSeq2SeqLMOutput(ModelOutput): Language modeling loss. logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -1601,14 +1601,14 @@ class TFLEDSeq2SeqLMOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None - decoder_hidden_states: Tuple[tf.Tensor, ...] | None = None - decoder_attentions: Tuple[tf.Tensor, ...] | None = None - cross_attentions: Tuple[tf.Tensor, ...] | None = None + past_key_values: list[tf.Tensor] | None = None + decoder_hidden_states: tuple[tf.Tensor, ...] | None = None + decoder_attentions: tuple[tf.Tensor, ...] | None = None + cross_attentions: tuple[tf.Tensor, ...] | None = None encoder_last_hidden_state: tf.Tensor | None = None - encoder_hidden_states: Tuple[tf.Tensor, ...] | None = None - encoder_attentions: Tuple[tf.Tensor, ...] | None = None - encoder_global_attentions: Tuple[tf.Tensor, ...] | None = None + encoder_hidden_states: tuple[tf.Tensor, ...] | None = None + encoder_attentions: tuple[tf.Tensor, ...] | None = None + encoder_global_attentions: tuple[tf.Tensor, ...] | None = None LED_START_DOCSTRING = r""" @@ -1695,7 +1695,7 @@ LED_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.Tensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -2074,7 +2074,7 @@ class TFLEDDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape @@ -2253,7 +2253,7 @@ class TFLEDMainLayer(keras.layers.Layer): decoder_attention_mask=None, head_mask=None, decoder_head_mask=None, - encoder_outputs: Optional[Union[Tuple, TFLEDEncoderBaseModelOutput]] = None, + encoder_outputs: Optional[Union[tuple, TFLEDEncoderBaseModelOutput]] = None, global_attention_mask=None, past_key_values=None, inputs_embeds=None, @@ -2372,7 +2372,7 @@ class TFLEDModel(TFLEDPreTrainedModel): decoder_head_mask: tf.Tensor | None = None, encoder_outputs: tf.Tensor | None = None, global_attention_mask: tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: bool | None = None, @@ -2381,7 +2381,7 @@ class TFLEDModel(TFLEDPreTrainedModel): return_dict: bool | None = None, training: bool = False, **kwargs, - ) -> Tuple[tf.Tensor] | TFLEDSeq2SeqModelOutput: + ) -> tuple[tf.Tensor] | TFLEDSeq2SeqModelOutput: outputs = self.led( input_ids=input_ids, attention_mask=attention_mask, @@ -2509,7 +2509,7 @@ class TFLEDForConditionalGeneration(TFLEDPreTrainedModel): decoder_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: TFLEDEncoderBaseModelOutput | None = None, global_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Tuple[Tuple[Union[np.ndarray, tf.Tensor]]] | None = None, + past_key_values: tuple[tuple[Union[np.ndarray, tf.Tensor]]] | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: bool | None = None, @@ -2518,7 +2518,7 @@ class TFLEDForConditionalGeneration(TFLEDPreTrainedModel): return_dict: bool | None = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Tuple[tf.Tensor] | TFLEDSeq2SeqLMOutput: + ) -> tuple[tf.Tensor] | TFLEDSeq2SeqLMOutput: """ Returns: diff --git a/src/transformers/models/led/tokenization_led.py b/src/transformers/models/led/tokenization_led.py index dce0e7dc7c0..ee01b3aec3b 100644 --- a/src/transformers/models/led/tokenization_led.py +++ b/src/transformers/models/led/tokenization_led.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import regex as re @@ -292,7 +292,7 @@ class LEDTokenizer(PreTrainedTokenizer): return text # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -323,8 +323,8 @@ class LEDTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.build_inputs_with_special_tokens with BART->LED def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A LED sequence has the following format: @@ -333,13 +333,13 @@ class LEDTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -349,22 +349,22 @@ class LEDTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -377,20 +377,20 @@ class LEDTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bart.tokenization_bart.BartTokenizer.create_token_type_ids_from_sequences with BART->LED def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. LED does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -408,7 +408,7 @@ class LEDTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, diff --git a/src/transformers/models/led/tokenization_led_fast.py b/src/transformers/models/led/tokenization_led_fast.py index 0cc29622987..baea10f2351 100644 --- a/src/transformers/models/led/tokenization_led_fast.py +++ b/src/transformers/models/led/tokenization_led_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for LED.""" import json -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from tokenizers import processors @@ -237,7 +237,7 @@ class LEDTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -251,20 +251,20 @@ class LEDTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.bart.tokenization_bart_fast.BartTokenizerFast.create_token_type_ids_from_sequences with BART->LED def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. LED does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -276,7 +276,7 @@ class LEDTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.led.tokenization_led.LEDTokenizer._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, diff --git a/src/transformers/models/levit/configuration_levit.py b/src/transformers/models/levit/configuration_levit.py index f8befdb78de..b0d63ed8e37 100644 --- a/src/transformers/models/levit/configuration_levit.py +++ b/src/transformers/models/levit/configuration_levit.py @@ -50,20 +50,20 @@ class LevitConfig(PretrainedConfig): The padding size for the initial convolution layers of patch embedding. patch_size (`int`, *optional*, defaults to 16): The patch size for embeddings. - hidden_sizes (`List[int]`, *optional*, defaults to `[128, 256, 384]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[128, 256, 384]`): Dimension of each of the encoder blocks. - num_attention_heads (`List[int]`, *optional*, defaults to `[4, 8, 12]`): + num_attention_heads (`list[int]`, *optional*, defaults to `[4, 8, 12]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - depths (`List[int]`, *optional*, defaults to `[4, 4, 4]`): + depths (`list[int]`, *optional*, defaults to `[4, 4, 4]`): The number of layers in each encoder block. - key_dim (`List[int]`, *optional*, defaults to `[16, 16, 16]`): + key_dim (`list[int]`, *optional*, defaults to `[16, 16, 16]`): The size of key in each of the encoder blocks. drop_path_rate (`int`, *optional*, defaults to 0): The dropout probability for stochastic depths, used in the blocks of the Transformer encoder. - mlp_ratios (`List[int]`, *optional*, defaults to `[2, 2, 2]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[2, 2, 2]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. - attention_ratios (`List[int]`, *optional*, defaults to `[2, 2, 2]`): + attention_ratios (`list[int]`, *optional*, defaults to `[2, 2, 2]`): Ratio of the size of the output dimension compared to input dimension of attention layers. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. diff --git a/src/transformers/models/levit/image_processing_levit.py b/src/transformers/models/levit/image_processing_levit.py index fc1e9678e7b..7832dbe5f43 100644 --- a/src/transformers/models/levit/image_processing_levit.py +++ b/src/transformers/models/levit/image_processing_levit.py @@ -15,7 +15,7 @@ """Image processor class for LeViT.""" from collections.abc import Iterable -from typing import Dict, Optional, Union +from typing import Optional, Union import numpy as np @@ -54,7 +54,7 @@ class LevitImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Wwhether to resize the shortest edge of the input to int(256/224 *`size`). Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]`, *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]`, *optional*, defaults to `{"shortest_edge": 224}`): Size of the output image after resizing. If size is a dict with keys "width" and "height", the image will be resized to `(size["height"], size["width"])`. If size is a dict with key "shortest_edge", the shortest edge value `c` is rescaled to `int(c * (256/224))`. The smaller edge of the image will be matched to this @@ -78,10 +78,10 @@ class LevitImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`List[int]`, *optional*, defaults to `[0.485, 0.456, 0.406]`): + image_mean (`list[int]`, *optional*, defaults to `[0.485, 0.456, 0.406]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`List[int]`, *optional*, defaults to `[0.229, 0.224, 0.225]`): + image_std (`list[int]`, *optional*, defaults to `[0.229, 0.224, 0.225]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -91,10 +91,10 @@ class LevitImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, @@ -122,7 +122,7 @@ class LevitImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -141,7 +141,7 @@ class LevitImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image after resizing. If size is a dict with keys "width" and "height", the image will be resized to (height, width). If size is a dict with key "shortest_edge", the shortest edge value `c` is rescaled to int(`c` * (256/224)). The smaller edge of the image will be matched to this value @@ -179,10 +179,10 @@ class LevitImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, @@ -201,7 +201,7 @@ class LevitImageProcessor(BaseImageProcessor): from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the output image after resizing. If size is a dict with keys "width" and "height", the image will be resized to (height, width). If size is a dict with key "shortest_edge", the shortest edge value `c` is rescaled to int(`c` * (256/224)). The smaller edge of the image will be matched to this value @@ -210,7 +210,7 @@ class LevitImageProcessor(BaseImageProcessor): Resampling filter to use when resiizing the image. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the output image after center cropping. Crops images to (crop_size["height"], crop_size["width"]). do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -219,9 +219,9 @@ class LevitImageProcessor(BaseImageProcessor): Factor to rescale the image pixel values by. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image pixel values by `image_mean` and `image_std`. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Mean to normalize the image pixel values by. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Standard deviation to normalize the image pixel values by. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/levit/modeling_levit.py b/src/transformers/models/levit/modeling_levit.py index 837d59979ac..0e51a4ff1d6 100644 --- a/src/transformers/models/levit/modeling_levit.py +++ b/src/transformers/models/levit/modeling_levit.py @@ -16,7 +16,7 @@ import itertools from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -60,7 +60,7 @@ class LevitForImageClassificationWithTeacherOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None cls_logits: Optional[torch.FloatTensor] = None distillation_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class LevitConvEmbeddings(nn.Module): @@ -504,7 +504,7 @@ class LevitModel(LevitPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -565,7 +565,7 @@ class LevitForImageClassification(LevitPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., @@ -649,7 +649,7 @@ class LevitForImageClassificationWithTeacher(LevitPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LevitForImageClassificationWithTeacherOutput]: + ) -> Union[tuple, LevitForImageClassificationWithTeacherOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.levit(pixel_values, output_hidden_states=output_hidden_states, return_dict=return_dict) diff --git a/src/transformers/models/lilt/modeling_lilt.py b/src/transformers/models/lilt/modeling_lilt.py index 9839e13f366..0c76a25a6e3 100644 --- a/src/transformers/models/lilt/modeling_lilt.py +++ b/src/transformers/models/lilt/modeling_lilt.py @@ -15,7 +15,7 @@ """PyTorch LiLT model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -374,7 +374,7 @@ class LiltAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, layout_inputs, @@ -444,7 +444,7 @@ class LiltLayer(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_attention_outputs = self.attention( hidden_states, layout_inputs, @@ -496,7 +496,7 @@ class LiltEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutput]: + ) -> Union[tuple[torch.Tensor], BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -636,7 +636,7 @@ class LiltModel(LiltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPooling]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -772,7 +772,7 @@ class LiltForSequenceClassification(LiltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -889,7 +889,7 @@ class LiltForTokenClassification(LiltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1008,7 +1008,7 @@ class LiltForQuestionAnswering(LiltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" bbox (`torch.LongTensor` of shape `(batch_size, sequence_length, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, diff --git a/src/transformers/models/llama/configuration_llama.py b/src/transformers/models/llama/configuration_llama.py index 3399ab64893..0065cf56b3b 100644 --- a/src/transformers/models/llama/configuration_llama.py +++ b/src/transformers/models/llama/configuration_llama.py @@ -106,11 +106,11 @@ class LlamaConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/llama/convert_llama_weights_to_hf.py b/src/transformers/models/llama/convert_llama_weights_to_hf.py index 5ba1418a113..2a1b0df8fd3 100644 --- a/src/transformers/models/llama/convert_llama_weights_to_hf.py +++ b/src/transformers/models/llama/convert_llama_weights_to_hf.py @@ -17,7 +17,6 @@ import json import os import tempfile import warnings -from typing import List import torch from tokenizers import AddedToken, processors @@ -560,7 +559,7 @@ def main(): parser.add_argument( "--special_tokens", default=None, - type=List[str], + type=list[str], help="The list of special tokens that should be added to the model.", ) parser.add_argument( diff --git a/src/transformers/models/llama/modeling_flax_llama.py b/src/transformers/models/llama/modeling_flax_llama.py index c3cf79e705a..63e34e996ad 100644 --- a/src/transformers/models/llama/modeling_flax_llama.py +++ b/src/transformers/models/llama/modeling_flax_llama.py @@ -20,7 +20,7 @@ """Flax LLaMA model.""" from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -114,7 +114,7 @@ LLAMA_INPUTS_DOCSTRING = r""" config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -413,7 +413,7 @@ class FlaxLlamaPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: LlamaConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -422,7 +422,7 @@ class FlaxLlamaPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/llama/modeling_llama.py b/src/transformers/models/llama/modeling_llama.py index 4502cee6e57..314b5c323b4 100644 --- a/src/transformers/models/llama/modeling_llama.py +++ b/src/transformers/models/llama/modeling_llama.py @@ -17,7 +17,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -223,12 +223,12 @@ class LlamaAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -284,9 +284,9 @@ class LlamaDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/llama/tokenization_llama.py b/src/transformers/models/llama/tokenization_llama.py index 4869ba04ea1..971c7c49dda 100644 --- a/src/transformers/models/llama/tokenization_llama.py +++ b/src/transformers/models/llama/tokenization_llama.py @@ -22,7 +22,7 @@ import os from shutil import copyfile -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Optional import sentencepiece as spm @@ -71,7 +71,7 @@ class LlamaTokenizer(PreTrainedTokenizer): pad_token (`str` or `tokenizers.AddedToken`, *optional*): A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by attention mechanisms or loss computation. - sp_model_kwargs (`Dict[str, Any]`, `Optional`, *optional*): + sp_model_kwargs (`dict[str, Any]`, `Optional`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things, to set: @@ -136,7 +136,7 @@ class LlamaTokenizer(PreTrainedTokenizer): bos_token="", eos_token="", pad_token=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, @@ -232,7 +232,7 @@ class LlamaTokenizer(PreTrainedTokenizer): return vocab # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.tokenize - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Converts a string to a list of tokens. If `self.legacy` is set to `False`, a prefix token is added unless the first token is special. @@ -303,7 +303,7 @@ class LlamaTokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string - def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> tuple[str]: """ Save the vocabulary and special tokens file to a directory. @@ -342,22 +342,22 @@ class LlamaTokenizer(PreTrainedTokenizer): return output def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -379,8 +379,8 @@ class LlamaTokenizer(PreTrainedTokenizer): ) def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format: @@ -393,13 +393,13 @@ class LlamaTokenizer(PreTrainedTokenizer): if token_ids_1 is None, only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ bos_token_id = [self.bos_token_id] if self.add_bos_token else [] eos_token_id = [self.eos_token_id] if self.add_eos_token else [] diff --git a/src/transformers/models/llama/tokenization_llama_fast.py b/src/transformers/models/llama/tokenization_llama_fast.py index a0fff2589ca..212e65404e8 100644 --- a/src/transformers/models/llama/tokenization_llama_fast.py +++ b/src/transformers/models/llama/tokenization_llama_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import os from shutil import copyfile -from typing import Optional, Tuple +from typing import Optional from tokenizers import processors @@ -215,7 +215,7 @@ class LlamaTokenizerFast(PreTrainedTokenizerFast): self._add_bos_token = value self.update_post_processor() - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/llama4/configuration_llama4.py b/src/transformers/models/llama4/configuration_llama4.py index 42f9442d678..cff2ecb6ed1 100644 --- a/src/transformers/models/llama4/configuration_llama4.py +++ b/src/transformers/models/llama4/configuration_llama4.py @@ -210,11 +210,11 @@ class Llama4TextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -224,7 +224,7 @@ class Llama4TextConfig(PretrainedConfig): Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE - no_rope_layers (`List[int]`, *optional*): + no_rope_layers (`list[int]`, *optional*): List with at least the same length as the number of layers in the model. A `1` at an index position indicates that the corresponding layer will use RoPE, while a `0` indicates that it's a NoPE layer. diff --git a/src/transformers/models/llama4/convert_llama4_weights_to_hf.py b/src/transformers/models/llama4/convert_llama4_weights_to_hf.py index 5e68e6ade65..2a81af6056c 100644 --- a/src/transformers/models/llama4/convert_llama4_weights_to_hf.py +++ b/src/transformers/models/llama4/convert_llama4_weights_to_hf.py @@ -4,7 +4,7 @@ import io import json import os import re -from typing import List, Optional +from typing import Optional import torch from tokenizers import AddedToken, processors @@ -621,7 +621,7 @@ class Llama4Converter(TikTokenConverter): def __init__( self, vocab_file, - special_tokens: List[str], + special_tokens: list[str], pattern: str, model_max_length: int = 0, chat_template: Optional[str] = None, @@ -710,7 +710,7 @@ if __name__ == "__main__": parser.add_argument( "--special_tokens", default=None, - type=List[str], + type=list[str], help="The list of special tokens that should be added to the model.", ) parser.add_argument( diff --git a/src/transformers/models/llama4/image_processing_llama4_fast.py b/src/transformers/models/llama4/image_processing_llama4_fast.py index e004400de93..d8a8db606ef 100644 --- a/src/transformers/models/llama4/image_processing_llama4_fast.py +++ b/src/transformers/models/llama4/image_processing_llama4_fast.py @@ -17,7 +17,7 @@ import math from collections import defaultdict from functools import lru_cache -from typing import List, Optional, Set, Tuple, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -47,7 +47,7 @@ if is_torchvision_available(): from torchvision.transforms import functional as F -def get_factors(dividend: int) -> Set[int]: +def get_factors(dividend: int) -> set[int]: """ Calculate all factors of a given number, i.e. a divisor that leaves no remainder. For example, if dividend=12, it will return {1, 2, 3, 4, 6, 12}. @@ -68,18 +68,18 @@ def get_factors(dividend: int) -> Set[int]: def get_max_res_without_distortion( - image_size: Tuple[int, int], - target_size: Tuple[int, int], -) -> Tuple[int, int]: + image_size: tuple[int, int], + target_size: tuple[int, int], +) -> tuple[int, int]: """ Determines the maximum resolution to which an image can be resized to without distorting its aspect ratio, based on the target resolution. Args: - image_size (Tuple[int, int]): The original resolution of the image (height, width). - target_resolution (Tuple[int, int]): The desired resolution to fit the image into (height, width). + image_size (tuple[int, int]): The original resolution of the image (height, width). + target_resolution (tuple[int, int]): The desired resolution to fit the image into (height, width). Returns: - Tuple[int, int]: The optimal dimensions (height, width) to which the image should be resized. + tuple[int, int]: The optimal dimensions (height, width) to which the image should be resized. Example: >>> _get_max_res_without_distortion([200, 300], target_size = [450, 200]) (134, 200) @@ -186,8 +186,8 @@ def find_supported_resolutions(max_num_chunks: int, patch_size: SizeDict) -> tor def pad_to_best_fit( images: "torch.Tensor", - target_size: Tuple[int, int], - background_color: Union[int, Tuple[int, int, int]] = 0, + target_size: tuple[int, int], + background_color: Union[int, tuple[int, int, int]] = 0, ) -> "torch.Tensor": """ Pads an image to fit the target size. @@ -195,7 +195,7 @@ def pad_to_best_fit( Args: images (`np.ndarray`): The images to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -221,10 +221,10 @@ def pad_to_best_fit( def get_best_fit( - image_size: Tuple[int, int], + image_size: tuple[int, int], possible_resolutions: torch.Tensor, resize_to_max_canvas: bool = False, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Determines the best canvas possible from a list of possible resolutions to, without distortion, resize an image to. @@ -245,13 +245,13 @@ def get_best_fit( has more padding. Args: - image_size (Tuple[int, int]): A tuple containing the height and width of the image. + image_size (tuple[int, int]): A tuple containing the height and width of the image. possible_resolutions (torch.Tensor): A tensor of shape (N, 2) where each row represents a possible resolution (height, width). resize_to_max_canvas (bool): If True, will return the largest upscaling resolution. Returns: - List[int]: The best resolution [height, width] for the given image. + list[int]: The best resolution [height, width] for the given image. Example: >>> image_size = (200, 300) @@ -360,8 +360,8 @@ class Llama4ImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Union[float, List[float]], - image_std: Union[float, List[float]], + image_mean: Union[float, list[float]], + image_std: Union[float, list[float]], ) -> "torch.Tensor": """ Rescale and normalize images. @@ -383,7 +383,7 @@ class Llama4ImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], size: SizeDict, max_patches: int, resize_to_max_canvas: bool, @@ -391,8 +391,8 @@ class Llama4ImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], **kwargs, ) -> BatchFeature: diff --git a/src/transformers/models/llama4/modeling_llama4.py b/src/transformers/models/llama4/modeling_llama4.py index e6308d37c47..38b6fde1037 100644 --- a/src/transformers/models/llama4/modeling_llama4.py +++ b/src/transformers/models/llama4/modeling_llama4.py @@ -15,7 +15,7 @@ # limitations under the License. import math from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -193,7 +193,7 @@ def apply_rotary_emb( xq: torch.Tensor, xk: torch.Tensor, freqs_cis: torch.Tensor, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2)) xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2)) xq_out = torch.view_as_real(xq_ * freqs_cis[:, :, None, :]).flatten(3) @@ -303,12 +303,12 @@ class Llama4TextAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -381,14 +381,14 @@ class Llama4TextDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -510,7 +510,7 @@ class Llama4TextModel(Llama4PreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -660,7 +660,7 @@ class Llama4ForCausalLM(Llama4PreTrainedModel, GenerationMixin): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -670,7 +670,7 @@ class Llama4ForCausalLM(Llama4PreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -765,9 +765,9 @@ class Llama4CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -846,7 +846,7 @@ def vision_apply_rotary_emb( query: torch.Tensor, key: torch.Tensor, freqs_ci: torch.Tensor, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: query_ = torch.view_as_complex(query.float().reshape(*query.shape[:-1], -1, 2)) key_ = torch.view_as_complex(key.float().reshape(*key.shape[:-1], -1, 2)) freqs_ci = reshape_for_broadcast(freqs_ci=freqs_ci, query=query_) # freqs_ci[:,:,None,:] @@ -879,7 +879,7 @@ class Llama4VisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Cache] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -998,7 +998,7 @@ class Llama4VisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -1149,7 +1149,7 @@ class Llama4VisionModel(Llama4PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[BaseModelOutput, tuple[torch.Tensor, ...]]: r""" Example: @@ -1278,7 +1278,7 @@ class Llama4ForConditionalGeneration(Llama4PreTrainedModel, GenerationMixin): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Union[int, List[int]], + vision_feature_layer: Union[int, list[int]], vision_feature_select_strategy: str, **kwargs, ): @@ -1288,7 +1288,7 @@ class Llama4ForConditionalGeneration(Llama4PreTrainedModel, GenerationMixin): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`): + vision_feature_layer (`Union[int, list[int]]`): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -1312,9 +1312,9 @@ class Llama4ForConditionalGeneration(Llama4PreTrainedModel, GenerationMixin): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1325,7 +1325,7 @@ class Llama4ForConditionalGeneration(Llama4PreTrainedModel, GenerationMixin): logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: torch.Tensor = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Llama4CausalLMOutputWithPast]: + ) -> Union[tuple, Llama4CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/llama4/processing_llama4.py b/src/transformers/models/llama4/processing_llama4.py index a020826aade..0ff76403f43 100644 --- a/src/transformers/models/llama4/processing_llama4.py +++ b/src/transformers/models/llama4/processing_llama4.py @@ -14,7 +14,7 @@ # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union from transformers.processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack from transformers.tokenization_utils_base import PreTokenizedInput, TextInput @@ -138,7 +138,7 @@ class Llama4Processor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[Llama4ProcessorKwargs], @@ -150,10 +150,10 @@ class Llama4Processor(ProcessorMixin): Llama4ImageProcessor's [`~Llama4ImageProcessor.__call__`] if `images` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/llava/configuration_llava.py b/src/transformers/models/llava/configuration_llava.py index 7b6ad8d0e0e..869841e967a 100644 --- a/src/transformers/models/llava/configuration_llava.py +++ b/src/transformers/models/llava/configuration_llava.py @@ -44,7 +44,7 @@ class LlavaConfig(PretrainedConfig): vision_feature_select_strategy (`str`, *optional*, defaults to `"default"`): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -2): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -2): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/llava/image_processing_llava.py b/src/transformers/models/llava/image_processing_llava.py index 2940ed5c801..fa737bd9b9c 100644 --- a/src/transformers/models/llava/image_processing_llava.py +++ b/src/transformers/models/llava/image_processing_llava.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for LLaVa.""" -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -62,7 +62,7 @@ class LlavaImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -71,7 +71,7 @@ class LlavaImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -82,10 +82,10 @@ class LlavaImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -99,15 +99,15 @@ class LlavaImageProcessor(BaseImageProcessor): self, do_pad: bool = False, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -151,7 +151,7 @@ class LlavaImageProcessor(BaseImageProcessor): def pad_to_square( self, image: np.ndarray, - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.array: @@ -161,7 +161,7 @@ class LlavaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -230,7 +230,7 @@ class LlavaImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -243,7 +243,7 @@ class LlavaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -281,15 +281,15 @@ class LlavaImageProcessor(BaseImageProcessor): images: ImageInput, do_pad: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: Optional[PILImageResampling] = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -308,7 +308,7 @@ class LlavaImageProcessor(BaseImageProcessor): The padding value is determined by the `image_mean` parameter. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -316,7 +316,7 @@ class LlavaImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -324,9 +324,9 @@ class LlavaImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/llava/image_processing_llava_fast.py b/src/transformers/models/llava/image_processing_llava_fast.py index e149bf1c625..171d1580027 100644 --- a/src/transformers/models/llava/image_processing_llava_fast.py +++ b/src/transformers/models/llava/image_processing_llava_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for LLaVa.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from ...image_processing_utils import ( BatchFeature, @@ -94,7 +94,7 @@ class LlavaImageProcessorFast(BaseImageProcessorFast): def pad_to_square( self, images: "torch.Tensor", - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, ) -> "torch.Tensor": """ Pads an image to a square based on the longest edge. @@ -102,7 +102,7 @@ class LlavaImageProcessorFast(BaseImageProcessorFast): Args: images (`np.ndarray`): The images to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -135,7 +135,7 @@ class LlavaImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, interpolation: Optional["F.InterpolationMode"], @@ -145,8 +145,8 @@ class LlavaImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: # Group images by size for batched resizing diff --git a/src/transformers/models/llava/modeling_llava.py b/src/transformers/models/llava/modeling_llava.py index aa83b57678b..fdbaa3d29a9 100644 --- a/src/transformers/models/llava/modeling_llava.py +++ b/src/transformers/models/llava/modeling_llava.py @@ -15,7 +15,7 @@ """PyTorch Llava model.""" from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -102,9 +102,9 @@ class LlavaCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -184,7 +184,7 @@ class LlavaModel(LlavaPreTrainedModel): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -194,7 +194,7 @@ class LlavaModel(LlavaPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`): The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -253,9 +253,9 @@ class LlavaModel(LlavaPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -264,7 +264,7 @@ class LlavaModel(LlavaPreTrainedModel): cache_position: Optional[torch.LongTensor] = None, image_sizes: torch.Tensor = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaModelOutputWithPast]: + ) -> Union[tuple, LlavaModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -379,7 +379,7 @@ class LlavaForConditionalGeneration(LlavaPreTrainedModel, GenerationMixin): def get_image_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, **kwargs, ): @@ -411,9 +411,9 @@ class LlavaForConditionalGeneration(LlavaPreTrainedModel, GenerationMixin): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -424,7 +424,7 @@ class LlavaForConditionalGeneration(LlavaPreTrainedModel, GenerationMixin): logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/llava/processing_llava.py b/src/transformers/models/llava/processing_llava.py index b345df4d23b..50d67d9cbc4 100644 --- a/src/transformers/models/llava/processing_llava.py +++ b/src/transformers/models/llava/processing_llava.py @@ -16,7 +16,7 @@ Processor class for Llava. """ -from typing import List, Union +from typing import Union import numpy as np @@ -94,7 +94,7 @@ class LlavaProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[LlavaProcessorKwargs], @@ -107,10 +107,10 @@ class LlavaProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -186,7 +186,7 @@ class LlavaProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/llava_next/configuration_llava_next.py b/src/transformers/models/llava_next/configuration_llava_next.py index 8e2ba1db75d..4cda90aff16 100644 --- a/src/transformers/models/llava_next/configuration_llava_next.py +++ b/src/transformers/models/llava_next/configuration_llava_next.py @@ -44,7 +44,7 @@ class LlavaNextConfig(PretrainedConfig): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`. If `"default"`, the CLS token is removed from the vision features. If `"full"`, the full vision features are used. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -2): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -2): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/llava_next/image_processing_llava_next.py b/src/transformers/models/llava_next/image_processing_llava_next.py index 1e8ef6cf3bc..21c47701405 100644 --- a/src/transformers/models/llava_next/image_processing_llava_next.py +++ b/src/transformers/models/llava_next/image_processing_llava_next.py @@ -15,7 +15,7 @@ """Image processor class for LLaVa-NeXT.""" from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -59,7 +59,7 @@ if is_vision_available(): from PIL import Image -def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> List[np.array]: +def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> list[np.array]: """ Divides an image into patches of a specified size. @@ -114,7 +114,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -127,7 +127,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -138,10 +138,10 @@ class LlavaNextImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -157,16 +157,16 @@ class LlavaNextImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - image_grid_pinpoints: Optional[List] = None, + size: Optional[dict[str, int]] = None, + image_grid_pinpoints: Optional[list] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = True, do_convert_rgb: bool = True, **kwargs, @@ -200,7 +200,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -213,7 +213,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -250,7 +250,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): def pad( self, image: np.ndarray, - padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], + padding: Union[int, tuple[int, int], Iterable[tuple[int, int]]], mode: PaddingMode = PaddingMode.CONSTANT, constant_values: Union[float, Iterable[float]] = 0.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -264,7 +264,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. @@ -319,15 +319,15 @@ class LlavaNextImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> Image.Image: @@ -340,7 +340,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -348,7 +348,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -356,9 +356,9 @@ class LlavaNextImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`): @@ -454,7 +454,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): resample: PILImageResampling, data_format: ChannelDimension, input_data_format: ChannelDimension, - ) -> List[np.array]: + ) -> list[np.array]: """ Process an image with variable resolutions by dividing it into patches. @@ -475,7 +475,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): The channel dimension format of the input image. Returns: - List[np.array]: A list of NumPy arrays containing the processed image patches. + list[np.array]: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") @@ -511,7 +511,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): def _pad_for_batching( self, - pixel_values: List[np.ndarray], + pixel_values: list[np.ndarray], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): @@ -519,7 +519,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[np.ndarray]`): + pixel_values (`list[np.ndarray]`): An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`) data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. Can be one of: @@ -533,7 +533,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): If unset, will use the inferred format of the input image. Returns: - List[`np.ndarray`]: The padded images. + list[`np.ndarray`]: The padded images. """ max_patch = max(len(x) for x in pixel_values) pixel_values = [ @@ -552,16 +552,16 @@ class LlavaNextImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, - image_grid_pinpoints: Optional[List] = None, + size: Optional[dict[str, int]] = None, + image_grid_pinpoints: Optional[list] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -575,7 +575,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. image_grid_pinpoints (`List` *optional*, defaults to `self.image_grid_pinpoints`): @@ -586,7 +586,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -594,9 +594,9 @@ class LlavaNextImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): diff --git a/src/transformers/models/llava_next/image_processing_llava_next_fast.py b/src/transformers/models/llava_next/image_processing_llava_next_fast.py index ac90290cef4..2217c73f8c3 100644 --- a/src/transformers/models/llava_next/image_processing_llava_next_fast.py +++ b/src/transformers/models/llava_next/image_processing_llava_next_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for LLaVa-NeXT.""" -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature, get_patch_output_size, select_best_resolution from ...image_processing_utils_fast import ( @@ -56,7 +56,7 @@ if is_torchvision_available(): class LlavaNextFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): """ - image_grid_pinpoints (`List[List[int]]`, *optional*): + image_grid_pinpoints (`list[list[int]]`, *optional*): A list of possible resolutions to use for processing high resolution images. The best resolution is selected based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` method. @@ -65,7 +65,7 @@ class LlavaNextFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): number of patches in the batch. Padding will be applied to the bottom and right with zeros. """ - image_grid_pinpoints: Optional[List[List[int]]] + image_grid_pinpoints: Optional[list[list[int]]] do_pad: Optional[bool] @@ -168,7 +168,7 @@ class LlavaNextImageProcessorFast(BaseImageProcessorFast): size: tuple, patch_size: int, interpolation: "F.InterpolationMode", - ) -> List["torch.Tensor"]: + ) -> list["torch.Tensor"]: """ Process an image with variable resolutions by dividing it into patches. @@ -185,7 +185,7 @@ class LlavaNextImageProcessorFast(BaseImageProcessorFast): Resampling filter to use if resizing the image. Returns: - List["torch.Tensor"]: A list of NumPy arrays containing the processed image patches. + list["torch.Tensor"]: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") @@ -207,17 +207,17 @@ class LlavaNextImageProcessorFast(BaseImageProcessorFast): def _pad_for_batching( self, - pixel_values: List["torch.Tensor"], - ) -> List["torch.Tensor"]: + pixel_values: list["torch.Tensor"], + ) -> list["torch.Tensor"]: """ Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[torch.Tensor]`): + pixel_values (`list[torch.Tensor]`): An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`) Returns: - List[`torch.Tensor`]: The padded images. + list[`torch.Tensor`]: The padded images. """ max_patch = max(len(x) for x in pixel_values) pixel_values = [ @@ -229,18 +229,18 @@ class LlavaNextImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - image_grid_pinpoints: List[List[int]], + image_grid_pinpoints: list[list[int]], interpolation: Optional["F.InterpolationMode"], do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: diff --git a/src/transformers/models/llava_next/modeling_llava_next.py b/src/transformers/models/llava_next/modeling_llava_next.py index 010de9060a1..4ce50206fff 100644 --- a/src/transformers/models/llava_next/modeling_llava_next.py +++ b/src/transformers/models/llava_next/modeling_llava_next.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -73,7 +73,7 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int): Calculate the number of patches after the preprocessing for images of any resolution. Args: - image_size (`torch.LongTensor` or `np.ndarray` or `Tuple[int, int]`): + image_size (`torch.LongTensor` or `np.ndarray` or `tuple[int, int]`): The size of the input image in the format (height, width). ? grid_pinpoints (`List`): A list containing possible resolutions. Each item in the list should be a tuple or list @@ -211,9 +211,9 @@ class LlavaNextCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -299,7 +299,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): Reshape, unpad and then pack each image_feature into a single image_features tensor containing all visual vectors. Args: - image_features (`List[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) + image_features (`list[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) List of image feature tensor, each contains all the visual feature of all patches. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). @@ -309,7 +309,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): New line embedding vector. Returns: image_features (`torch.Tensor` of shape `(all_feat_len, embed_dim)`) - feature_lens (`List[int]`) + feature_lens (`list[int]`) token length of each image in image_features """ new_image_features = [] @@ -365,7 +365,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -376,7 +376,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): The tensors corresponding to the input images. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -384,7 +384,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - image_features (List[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches + image_features (list[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches and are of shape `(num_patches, image_length, embed_dim)`). """ vision_feature_layer = ( @@ -448,9 +448,9 @@ class LlavaNextModel(LlavaNextPreTrainedModel): image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -458,7 +458,7 @@ class LlavaNextModel(LlavaNextPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaNextModelOutputWithPast]: + ) -> Union[tuple, LlavaNextModelOutputWithPast]: r""" vision_feature_select_strategy (`str`, *optional*, defaults to `"default"`): The feature selection strategy used to select the vision feature from the vision backbone. @@ -586,7 +586,7 @@ class LlavaNextForConditionalGeneration(LlavaNextPreTrainedModel, GenerationMixi self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_image_features( @@ -618,9 +618,9 @@ class LlavaNextForConditionalGeneration(LlavaNextPreTrainedModel, GenerationMixi image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -629,7 +629,7 @@ class LlavaNextForConditionalGeneration(LlavaNextPreTrainedModel, GenerationMixi cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaNextCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaNextCausalLMOutputWithPast]: r""" vision_feature_select_strategy (`str`, *optional*, defaults to `"default"`): The feature selection strategy used to select the vision feature from the vision backbone. diff --git a/src/transformers/models/llava_next/processing_llava_next.py b/src/transformers/models/llava_next/processing_llava_next.py index 5a25f4072c2..8de438cb8a0 100644 --- a/src/transformers/models/llava_next/processing_llava_next.py +++ b/src/transformers/models/llava_next/processing_llava_next.py @@ -16,7 +16,7 @@ Processor class for LLaVa-NeXT. """ -from typing import List, Union +from typing import Union import numpy as np @@ -104,7 +104,7 @@ class LlavaNextProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[LlavaNextProcessorKwargs], @@ -117,10 +117,10 @@ class LlavaNextProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -232,14 +232,14 @@ class LlavaNextProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (List[List[str]], *optional*): + image_sizes (list[list[str]], *optional*): The input sizes formatted as (height, width) per each image. - video_sizes (List[List[str]], *optional*): + video_sizes (list[list[str]], *optional*): The input sizes formatted as (num_frames, height, width) per each video. - audio_lengths (List[int], *optional*): + audio_lengths (list[int], *optional*): The input length formatted as per each audio. Returns: - Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio") + dict[str, list[int]]: A dictionary mapping each modality ("image", "video", "audio") to a list containing the number of placeholder tokens required. If the model doesn't accept a certain modality or no input sizes are provided, the dict value is set to an empty list. """ diff --git a/src/transformers/models/llava_next_video/configuration_llava_next_video.py b/src/transformers/models/llava_next_video/configuration_llava_next_video.py index 8a5f7fecee3..94d2cac4c89 100644 --- a/src/transformers/models/llava_next_video/configuration_llava_next_video.py +++ b/src/transformers/models/llava_next_video/configuration_llava_next_video.py @@ -48,7 +48,7 @@ class LlavaNextVideoConfig(PretrainedConfig): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`. If `"default"`, the CLS token is removed from the vision features. If `"full"`, the full vision features are used. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -2): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -2): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/llava_next_video/image_processing_llava_next_video.py b/src/transformers/models/llava_next_video/image_processing_llava_next_video.py index b272c8a7167..2bb7b43e677 100644 --- a/src/transformers/models/llava_next_video/image_processing_llava_next_video.py +++ b/src/transformers/models/llava_next_video/image_processing_llava_next_video.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for LLaVa-NeXT-Video.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -52,7 +52,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -65,7 +65,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -76,10 +76,10 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -92,16 +92,16 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - image_grid_pinpoints: Optional[List] = None, + size: Optional[dict[str, int]] = None, + image_grid_pinpoints: Optional[list] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -128,7 +128,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -141,7 +141,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -179,15 +179,15 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -201,7 +201,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -209,7 +209,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -217,9 +217,9 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`): @@ -279,15 +279,15 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): self, images: VideoInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -300,7 +300,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the video. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the video after resizing. Shortest edge of the video is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -308,7 +308,7 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the video. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the video. @@ -316,9 +316,9 @@ class LlavaNextVideoImageProcessor(BaseImageProcessor): Rescale factor to rescale the video by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the video. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Frame mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Frame standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/llava_next_video/modeling_llava_next_video.py b/src/transformers/models/llava_next_video/modeling_llava_next_video.py index 9b663bd3903..19d47b98495 100644 --- a/src/transformers/models/llava_next_video/modeling_llava_next_video.py +++ b/src/transformers/models/llava_next_video/modeling_llava_next_video.py @@ -21,7 +21,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -119,9 +119,9 @@ class LlavaNextVideoCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None video_hidden_states: Optional[torch.FloatTensor] = None @@ -246,7 +246,7 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int): Calculate the number of patches after the preprocessing for images of any resolution. Args: - image_size (`torch.LongTensor` or `np.ndarray` or `Tuple[int, int]`): + image_size (`torch.LongTensor` or `np.ndarray` or `tuple[int, int]`): The size of the input image in the format (height, width). ? grid_pinpoints (`List`): A list containing possible resolutions. Each item in the list should be a tuple or list @@ -353,7 +353,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): Reshape, unpad and then pack each image_feature into a single image_features tensor containing all visual vectors. Args: - image_features (`List[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) + image_features (`list[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) List of image feature tensor, each contains all the visual feature of all patches. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). @@ -363,7 +363,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): New line embedding vector. Returns: image_features (`torch.Tensor` of shape `(all_feat_len, embed_dim)`) - feature_lens (`List[int]`) + feature_lens (`list[int]`) token length of each image in image_features """ new_image_features = [] @@ -419,7 +419,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -430,7 +430,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): The tensors corresponding to the input images. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -438,7 +438,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - image_features (List[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches + image_features (list[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches and are of shape `(num_patches, image_length, embed_dim)`). """ vision_feature_layer = ( @@ -501,9 +501,9 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -511,7 +511,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaNextVideoModelOutputWithPast]: + ) -> Union[tuple, LlavaNextVideoModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -611,7 +611,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -620,7 +620,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`) The tensors corresponding to the input video. - vision_feature_layer (`Union[int, List[int]]`, *optiona;*): + vision_feature_layer (`Union[int, list[int]]`, *optiona;*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -628,7 +628,7 @@ class LlavaNextVideoModel(LlavaNextVideoPreTrainedModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - video_features (List[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches + video_features (list[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches and are of shape `(num_videos, video_length, embed_dim)`). """ vision_feature_layer = ( @@ -718,7 +718,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextVideoPreTrainedModel, Gene self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_image_features( @@ -751,9 +751,9 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextVideoPreTrainedModel, Gene image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -763,7 +763,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextVideoPreTrainedModel, Gene cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaNextVideoCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaNextVideoCausalLMOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -787,7 +787,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextVideoPreTrainedModel, Gene ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -977,7 +977,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextVideoPreTrainedModel, Gene def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_video_features( diff --git a/src/transformers/models/llava_next_video/modular_llava_next_video.py b/src/transformers/models/llava_next_video/modular_llava_next_video.py index 97ea22ae2e6..75aaa7ed7bb 100644 --- a/src/transformers/models/llava_next_video/modular_llava_next_video.py +++ b/src/transformers/models/llava_next_video/modular_llava_next_video.py @@ -15,7 +15,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -64,7 +64,7 @@ class LlavaNextVideoConfig(PretrainedConfig): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`. If `"default"`, the CLS token is removed from the vision features. If `"full"`, the full vision features are used. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -2): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -2): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -252,7 +252,7 @@ class LlavaNextVideoModel(LlavaNextModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -263,7 +263,7 @@ class LlavaNextVideoModel(LlavaNextModel): The tensors corresponding to the input images. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -271,7 +271,7 @@ class LlavaNextVideoModel(LlavaNextModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - image_features (List[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches + image_features (list[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches and are of shape `(num_patches, image_length, embed_dim)`). """ vision_feature_layer = ( @@ -327,7 +327,7 @@ class LlavaNextVideoModel(LlavaNextModel): def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -336,7 +336,7 @@ class LlavaNextVideoModel(LlavaNextModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`) The tensors corresponding to the input video. - vision_feature_layer (`Union[int, List[int]]`, *optiona;*): + vision_feature_layer (`Union[int, list[int]]`, *optiona;*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -344,7 +344,7 @@ class LlavaNextVideoModel(LlavaNextModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - video_features (List[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches + video_features (list[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches and are of shape `(num_videos, video_length, embed_dim)`). """ vision_feature_layer = ( @@ -387,9 +387,9 @@ class LlavaNextVideoModel(LlavaNextModel): image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -397,7 +397,7 @@ class LlavaNextVideoModel(LlavaNextModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaNextVideoModelOutputWithPast]: + ) -> Union[tuple, LlavaNextVideoModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -499,7 +499,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextForConditionalGeneration): def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_video_features( @@ -516,9 +516,9 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextForConditionalGeneration): image_sizes: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -528,7 +528,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextForConditionalGeneration): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaNextVideoCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaNextVideoCausalLMOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -552,7 +552,7 @@ class LlavaNextVideoForConditionalGeneration(LlavaNextForConditionalGeneration): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' diff --git a/src/transformers/models/llava_next_video/processing_llava_next_video.py b/src/transformers/models/llava_next_video/processing_llava_next_video.py index b4cd7f7c221..c19936d00f4 100644 --- a/src/transformers/models/llava_next_video/processing_llava_next_video.py +++ b/src/transformers/models/llava_next_video/processing_llava_next_video.py @@ -16,7 +16,7 @@ Processor class for LLaVa-NeXT-Video. """ -from typing import List, Union +from typing import Union import numpy as np @@ -115,7 +115,7 @@ class LlavaNextVideoProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos: VideoInput = None, **kwargs: Unpack[LlavaNextVideoProcessorKwargs], @@ -130,14 +130,14 @@ class LlavaNextVideoProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch tensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported. return_tensors (`str` or [`~utils.TensorType`], *optional*): diff --git a/src/transformers/models/llava_onevision/configuration_llava_onevision.py b/src/transformers/models/llava_onevision/configuration_llava_onevision.py index 25f9c30d933..6e618b1ce59 100644 --- a/src/transformers/models/llava_onevision/configuration_llava_onevision.py +++ b/src/transformers/models/llava_onevision/configuration_llava_onevision.py @@ -49,7 +49,7 @@ class LlavaOnevisionConfig(PretrainedConfig): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`. If `"default"`, the CLS token is removed from the vision features. If `"full"`, the full vision features are used. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -1): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -1): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/llava_onevision/image_processing_llava_onevision.py b/src/transformers/models/llava_onevision/image_processing_llava_onevision.py index e7ed7d4b0d6..f7f108729ea 100644 --- a/src/transformers/models/llava_onevision/image_processing_llava_onevision.py +++ b/src/transformers/models/llava_onevision/image_processing_llava_onevision.py @@ -15,7 +15,7 @@ """Image processor class for LLaVa-Onevision.""" from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -58,7 +58,7 @@ if is_vision_available(): # Copied from transformers.models.llava_next.image_processing_llava_next.divide_to_patches -def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> List[np.array]: +def divide_to_patches(image: np.array, patch_size: int, input_data_format) -> list[np.array]: """ Divides an image into patches of a specified size. @@ -113,7 +113,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -131,10 +131,10 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -150,14 +150,14 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - image_grid_pinpoints: Optional[List] = None, + size: Optional[dict[str, int]] = None, + image_grid_pinpoints: Optional[list] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = True, do_convert_rgb: bool = True, **kwargs, @@ -224,7 +224,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): def pad( self, image: np.ndarray, - padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], + padding: Union[int, tuple[int, int], Iterable[tuple[int, int]]], mode: PaddingMode = PaddingMode.CONSTANT, constant_values: Union[float, Iterable[float]] = 0.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -238,7 +238,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. @@ -348,7 +348,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): resample: PILImageResampling, data_format: ChannelDimension, input_data_format: ChannelDimension, - ) -> List[np.array]: + ) -> list[np.array]: """ Process an image with variable resolutions by dividing it into patches. @@ -369,7 +369,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): The channel dimension format of the input image. Returns: - List[np.array]: A list of NumPy arrays containing the processed image patches. + list[np.array]: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") @@ -406,7 +406,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): # Copied from transformers.models.llava_next.image_processing_llava_next.LlavaNextImageProcessor._pad_for_batching def _pad_for_batching( self, - pixel_values: List[np.ndarray], + pixel_values: list[np.ndarray], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): @@ -414,7 +414,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[np.ndarray]`): + pixel_values (`list[np.ndarray]`): An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`) data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. Can be one of: @@ -428,7 +428,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): If unset, will use the inferred format of the input image. Returns: - List[`np.ndarray`]: The padded images. + list[`np.ndarray`]: The padded images. """ max_patch = max(len(x) for x in pixel_values) pixel_values = [ @@ -447,7 +447,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): def pad_to_square( self, image: np.ndarray, - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.array: @@ -457,7 +457,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -526,13 +526,13 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -544,7 +544,7 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -556,9 +556,9 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`): @@ -601,14 +601,14 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, - image_grid_pinpoints: Optional[List] = None, + size: Optional[dict[str, int]] = None, + image_grid_pinpoints: Optional[list] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -617,12 +617,12 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): ): """ Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. image_grid_pinpoints (`List` *optional*, defaults to `self.image_grid_pinpoints`): @@ -637,9 +637,9 @@ class LlavaOnevisionImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): diff --git a/src/transformers/models/llava_onevision/image_processing_llava_onevision_fast.py b/src/transformers/models/llava_onevision/image_processing_llava_onevision_fast.py index a29631fcb6a..38e281e092c 100644 --- a/src/transformers/models/llava_onevision/image_processing_llava_onevision_fast.py +++ b/src/transformers/models/llava_onevision/image_processing_llava_onevision_fast.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch @@ -53,7 +53,7 @@ else: class LlavaOnevisionFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): """ - image_grid_pinpoints (`List[List[int]]`, *optional*): + image_grid_pinpoints (`list[list[int]]`, *optional*): A list of possible resolutions to use for processing high resolution images. The best resolution is selected based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` method. @@ -62,7 +62,7 @@ class LlavaOnevisionFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): number of patches in the batch. Padding will be applied to the bottom and right with zeros. """ - image_grid_pinpoints: Optional[List[List[int]]] + image_grid_pinpoints: Optional[list[list[int]]] do_pad: Optional[bool] @@ -173,7 +173,7 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): size: tuple, patch_size: int, interpolation: "F.InterpolationMode", - ) -> List["torch.Tensor"]: + ) -> list["torch.Tensor"]: """ Process an image with variable resolutions by dividing it into patches. @@ -190,7 +190,7 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): Resampling filter to use if resizing the image. Returns: - List["torch.Tensor"]: A list of NumPy arrays containing the processed image patches. + list["torch.Tensor"]: A list of NumPy arrays containing the processed image patches. """ if not isinstance(grid_pinpoints, list): raise TypeError("grid_pinpoints must be a list of possible resolutions.") @@ -212,17 +212,17 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): def _pad_for_batching( self, - pixel_values: List["torch.Tensor"], - ) -> List["torch.Tensor"]: + pixel_values: list["torch.Tensor"], + ) -> list["torch.Tensor"]: """ Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[torch.Tensor]`): + pixel_values (`list[torch.Tensor]`): An array of pixel values of each images of shape (`batch_size`, `num_patches`, `image_in_3D`) Returns: - List[`torch.Tensor`]: The padded images. + list[`torch.Tensor`]: The padded images. """ max_patch = max(len(x) for x in pixel_values) pixel_values = [ @@ -234,20 +234,20 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - image_grid_pinpoints: List[List[int]], + image_grid_pinpoints: list[list[int]], interpolation: Optional["F.InterpolationMode"], do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - batch_num_images: List[int], + batch_num_images: list[int], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: processed_images = [] @@ -321,7 +321,7 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): def pad_to_square( self, images: "torch.Tensor", - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, ) -> "torch.Tensor": """ Pads an image to a square based on the longest edge. @@ -329,7 +329,7 @@ class LlavaOnevisionImageProcessorFast(BaseImageProcessorFast): Args: images (`np.ndarray`): The images to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. diff --git a/src/transformers/models/llava_onevision/modeling_llava_onevision.py b/src/transformers/models/llava_onevision/modeling_llava_onevision.py index 4205abf8571..0e051c0b68d 100644 --- a/src/transformers/models/llava_onevision/modeling_llava_onevision.py +++ b/src/transformers/models/llava_onevision/modeling_llava_onevision.py @@ -21,7 +21,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -125,9 +125,9 @@ class LlavaOnevisionCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None video_hidden_states: Optional[torch.FloatTensor] = None @@ -225,7 +225,7 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int): Calculate the number of patches after the preprocessing for images of any resolution. Args: - image_size (`torch.LongTensor` or `np.ndarray` or `Tuple[int, int]`): + image_size (`torch.LongTensor` or `np.ndarray` or `tuple[int, int]`): The size of the input image in the format (height, width). ? grid_pinpoints (`List`): A list containing possible resolutions. Each item in the list should be a tuple or list @@ -355,7 +355,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): Reshape, unpad and then pack each image_feature into a single image_features tensor containing all visual vectors. Args: - image_features (`List[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) + image_features (`list[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) List of image feature tensor, each contains all the visual feature of all patches. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). @@ -365,7 +365,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): Aspect ratio used when processong image features. The default value is "anyres_max_9". Returns: image_features (`torch.Tensor` of shape `(all_feat_len, embed_dim)`) - feature_lens (`List[int]`) + feature_lens (`list[int]`) token length of each image in image_features """ new_image_features = [] @@ -420,7 +420,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -433,7 +433,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): The tensors corresponding to the input images. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). - vision_feature_layer (`Union[int, List[int]]`): + vision_feature_layer (`Union[int, list[int]]`): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -443,7 +443,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): batch_num_images (`torch.LongTensor`, *optional*): Number of images in each sample. Returns: - image_features (List[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches + image_features (list[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches and are of shape `(num_patches, image_length, embed_dim)`). """ vision_feature_layer = ( @@ -517,9 +517,9 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): image_sizes_videos: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -529,7 +529,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaOnevisionModelOutputWithPast]: + ) -> Union[tuple, LlavaOnevisionModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -647,7 +647,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Union[int, List[int]], + vision_feature_layer: Union[int, list[int]], vision_feature_select_strategy: str, ): """ @@ -656,7 +656,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`) The tensors corresponding to the input video. - vision_feature_layer (`Union[int, List[int]], *optional*, defaults to -2`): + vision_feature_layer (`Union[int, list[int]], *optional*, defaults to -2`): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -664,7 +664,7 @@ class LlavaOnevisionModel(LlavaOnevisionPreTrainedModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - video_features (List[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches + video_features (list[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches and are of shape `(num_videos, video_length, embed_dim)`). """ batch_size, frames, channels, height, width = pixel_values.shape @@ -759,7 +759,7 @@ class LlavaOnevisionForConditionalGeneration(LlavaOnevisionPreTrainedModel, Gene self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_image_features( @@ -793,9 +793,9 @@ class LlavaOnevisionForConditionalGeneration(LlavaOnevisionPreTrainedModel, Gene image_sizes_videos: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -807,7 +807,7 @@ class LlavaOnevisionForConditionalGeneration(LlavaOnevisionPreTrainedModel, Gene cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaOnevisionCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaOnevisionCausalLMOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -1013,7 +1013,7 @@ class LlavaOnevisionForConditionalGeneration(LlavaOnevisionPreTrainedModel, Gene def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_video_features( diff --git a/src/transformers/models/llava_onevision/modular_llava_onevision.py b/src/transformers/models/llava_onevision/modular_llava_onevision.py index e61eca445e9..c4ba5c0ed32 100644 --- a/src/transformers/models/llava_onevision/modular_llava_onevision.py +++ b/src/transformers/models/llava_onevision/modular_llava_onevision.py @@ -14,7 +14,7 @@ # limitations under the License. import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -68,7 +68,7 @@ logger = logging.get_logger(__name__) class LlavaOnevisionFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): """ - image_grid_pinpoints (`List[List[int]]`, *optional*): + image_grid_pinpoints (`list[list[int]]`, *optional*): A list of possible resolutions to use for processing high resolution images. The best resolution is selected based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` method. @@ -77,7 +77,7 @@ class LlavaOnevisionFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): number of patches in the batch. Padding will be applied to the bottom and right with zeros. """ - image_grid_pinpoints: Optional[List[List[int]]] + image_grid_pinpoints: Optional[list[list[int]]] do_pad: Optional[bool] @@ -101,7 +101,7 @@ class LlavaOnevisionImageProcessorFast(LlavaNextImageProcessorFast): def pad_to_square( self, images: "torch.Tensor", - background_color: Union[int, Tuple[int, int, int]] = 0, + background_color: Union[int, tuple[int, int, int]] = 0, ) -> "torch.Tensor": """ Pads an image to a square based on the longest edge. @@ -109,7 +109,7 @@ class LlavaOnevisionImageProcessorFast(LlavaNextImageProcessorFast): Args: images (`np.ndarray`): The images to pad. - background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0): + background_color (`int` or `tuple[int, int, int]`, *optional*, defaults to 0): The color to use for the padding. Can be an integer for single channel or a tuple of integers representing for multi-channel images. If passed as integer in mutli-channel mode, it will default to `0` in subsequent channels. @@ -155,20 +155,20 @@ class LlavaOnevisionImageProcessorFast(LlavaNextImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - image_grid_pinpoints: List[List[int]], + image_grid_pinpoints: list[list[int]], interpolation: Optional["F.InterpolationMode"], do_center_crop: bool, crop_size: SizeDict, do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - batch_num_images: List[int], + batch_num_images: list[int], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: processed_images = [] @@ -261,7 +261,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): Reshape, unpad and then pack each image_feature into a single image_features tensor containing all visual vectors. Args: - image_features (`List[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) + image_features (`list[torch.Tensor]` of length num_images, each of shape `(num_patches, image_length, embed_dim)`) List of image feature tensor, each contains all the visual feature of all patches. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). @@ -271,7 +271,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): Aspect ratio used when processong image features. The default value is "anyres_max_9". Returns: image_features (`torch.Tensor` of shape `(all_feat_len, embed_dim)`) - feature_lens (`List[int]`) + feature_lens (`list[int]`) token length of each image in image_features """ new_image_features = [] @@ -340,7 +340,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -353,7 +353,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): The tensors corresponding to the input images. image_sizes (`torch.Tensor` of shape `(num_images, 2)`) Actual image size of each images (H, W). - vision_feature_layer (`Union[int, List[int]]`): + vision_feature_layer (`Union[int, list[int]]`): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -363,7 +363,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): batch_num_images (`torch.LongTensor`, *optional*): Number of images in each sample. Returns: - image_features (List[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches + image_features (list[`torch.Tensor`]): List of image feature tensor, each contains all the visual feature of all patches and are of shape `(num_patches, image_length, embed_dim)`). """ vision_feature_layer = ( @@ -429,7 +429,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): def get_video_features( self, pixel_values: torch.FloatTensor, - vision_feature_layer: Union[int, List[int]], + vision_feature_layer: Union[int, list[int]], vision_feature_select_strategy: str, ): """ @@ -438,7 +438,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`) The tensors corresponding to the input video. - vision_feature_layer (`Union[int, List[int]], *optional*, defaults to -2`): + vision_feature_layer (`Union[int, list[int]], *optional*, defaults to -2`): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -446,7 +446,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"` Returns: - video_features (List[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches + video_features (list[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches and are of shape `(num_videos, video_length, embed_dim)`). """ batch_size, frames, channels, height, width = pixel_values.shape @@ -481,9 +481,9 @@ class LlavaOnevisionModel(LlavaNextVideoModel): image_sizes_videos: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -493,7 +493,7 @@ class LlavaOnevisionModel(LlavaNextVideoModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, LlavaOnevisionModelOutputWithPast]: + ) -> Union[tuple, LlavaOnevisionModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -621,9 +621,9 @@ class LlavaOnevisionForConditionalGeneration(LlavaNextVideoForConditionalGenerat image_sizes_videos: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, vision_aspect_ratio: Optional[str] = None, batch_num_images: Optional[torch.LongTensor] = None, @@ -635,7 +635,7 @@ class LlavaOnevisionForConditionalGeneration(LlavaNextVideoForConditionalGenerat cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, LlavaOnevisionCausalLMOutputWithPast]: + ) -> Union[tuple, LlavaOnevisionCausalLMOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(batch_size, frames, num_channels, image_size, image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using diff --git a/src/transformers/models/llava_onevision/processing_llava_onevision.py b/src/transformers/models/llava_onevision/processing_llava_onevision.py index cae50bd9bf6..a3f2e283591 100644 --- a/src/transformers/models/llava_onevision/processing_llava_onevision.py +++ b/src/transformers/models/llava_onevision/processing_llava_onevision.py @@ -18,7 +18,7 @@ Processor class for LLaVa-Onevision. import math from collections.abc import Iterable -from typing import List, Union +from typing import Union import numpy as np @@ -113,7 +113,7 @@ class LlavaOnevisionProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos: VideoInput = None, **kwargs: Unpack[LlavaOnevisionProcessorKwargs], @@ -126,14 +126,14 @@ class LlavaOnevisionProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch Returns: @@ -204,8 +204,8 @@ class LlavaOnevisionProcessor(ProcessorMixin): def _expand_image_tokens( self, - text: List[TextInput], - image_sizes: Iterable[Union[List[int], int]], + text: list[TextInput], + image_sizes: Iterable[Union[list[int], int]], height: int, width: int, special_token: str, @@ -290,14 +290,14 @@ class LlavaOnevisionProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (List[List[str]], *optional*): + image_sizes (list[list[str]], *optional*): The input sizes formatted as (height, width) per each image. - video_sizes (List[List[str]], *optional*): + video_sizes (list[list[str]], *optional*): The input sizes formatted as (num_frames, height, width) per each video. - audio_lengths (List[int], *optional*): + audio_lengths (list[int], *optional*): The input length formatted as per each audio. Returns: - Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio") + dict[str, list[int]]: A dictionary mapping each modality ("image", "video", "audio") to a list containing the number of placeholder tokens required. If the model doesn't accept a certain modality or no input sizes are provided, the dict value is set to an empty list. """ diff --git a/src/transformers/models/longformer/configuration_longformer.py b/src/transformers/models/longformer/configuration_longformer.py index 178d1a07f27..207cc183947 100644 --- a/src/transformers/models/longformer/configuration_longformer.py +++ b/src/transformers/models/longformer/configuration_longformer.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import TYPE_CHECKING, Any, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from ...configuration_utils import PretrainedConfig from ...onnx import OnnxConfig @@ -74,9 +74,9 @@ class LongformerConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-12): The epsilon used by the layer normalization layers. - attention_window (`int` or `List[int]`, *optional*, defaults to 512): + attention_window (`int` or `list[int]`, *optional*, defaults to 512): Size of an attention window around each token. If an `int`, use the same size for all layers. To specify a - different window size for each layer, use a `List[int]` where `len(attention_window) == num_hidden_layers`. + different window size for each layer, use a `list[int]` where `len(attention_window) == num_hidden_layers`. Example: @@ -97,7 +97,7 @@ class LongformerConfig(PretrainedConfig): def __init__( self, - attention_window: Union[List[int], int] = 512, + attention_window: Union[list[int], int] = 512, sep_token_id: int = 2, pad_token_id: int = 1, bos_token_id: int = 0, @@ -141,7 +141,7 @@ class LongformerConfig(PretrainedConfig): class LongformerOnnxConfig(OnnxConfig): def __init__( - self, config: "PretrainedConfig", task: str = "default", patching_specs: "Optional[List[PatchingSpec]]" = None + self, config: "PretrainedConfig", task: str = "default", patching_specs: "Optional[list[PatchingSpec]]" = None ): super().__init__(config, task, patching_specs) config.onnx_export = True diff --git a/src/transformers/models/longformer/modeling_longformer.py b/src/transformers/models/longformer/modeling_longformer.py index dc0f1615114..a40d5bb0e2c 100755 --- a/src/transformers/models/longformer/modeling_longformer.py +++ b/src/transformers/models/longformer/modeling_longformer.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -71,9 +71,9 @@ class LongformerBaseModelOutput(ModelOutput): """ last_hidden_state: torch.FloatTensor - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -119,9 +119,9 @@ class LongformerBaseModelOutputWithPooling(ModelOutput): last_hidden_state: torch.FloatTensor pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -165,9 +165,9 @@ class LongformerMaskedLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -214,9 +214,9 @@ class LongformerQuestionAnsweringModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -260,9 +260,9 @@ class LongformerSequenceClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -308,9 +308,9 @@ class LongformerMultipleChoiceModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -354,9 +354,9 @@ class LongformerTokenClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - global_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + global_attentions: Optional[tuple[torch.FloatTensor, ...]] = None def _get_question_end_index(input_ids, sep_token_id): @@ -1542,7 +1542,7 @@ class LongformerModel(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerBaseModelOutputWithPooling]: + ) -> Union[tuple, LongformerBaseModelOutputWithPooling]: r""" global_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to decide the attention given on each token, local attention or global attention. Tokens with global @@ -1694,7 +1694,7 @@ class LongformerForMaskedLM(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerMaskedLMOutput]: + ) -> Union[tuple, LongformerMaskedLMOutput]: r""" global_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to decide the attention given on each token, local attention or global attention. Tokens with global @@ -1807,7 +1807,7 @@ class LongformerForSequenceClassification(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerSequenceClassifierOutput]: + ) -> Union[tuple, LongformerSequenceClassifierOutput]: r""" global_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to decide the attention given on each token, local attention or global attention. Tokens with global @@ -1931,7 +1931,7 @@ class LongformerForQuestionAnswering(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerQuestionAnsweringModelOutput]: + ) -> Union[tuple, LongformerQuestionAnsweringModelOutput]: r""" global_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to decide the attention given on each token, local attention or global attention. Tokens with global @@ -2061,7 +2061,7 @@ class LongformerForTokenClassification(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerTokenClassifierOutput]: + ) -> Union[tuple, LongformerTokenClassifierOutput]: r""" global_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to decide the attention given on each token, local attention or global attention. Tokens with global @@ -2142,7 +2142,7 @@ class LongformerForMultipleChoice(LongformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LongformerMultipleChoiceModelOutput]: + ) -> Union[tuple, LongformerMultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/longformer/modeling_tf_longformer.py b/src/transformers/models/longformer/modeling_tf_longformer.py index bb0f5279bd0..2163cbbdf49 100644 --- a/src/transformers/models/longformer/modeling_tf_longformer.py +++ b/src/transformers/models/longformer/modeling_tf_longformer.py @@ -18,7 +18,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -94,9 +94,9 @@ class TFLongformerBaseModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -142,9 +142,9 @@ class TFLongformerBaseModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None pooler_output: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -188,9 +188,9 @@ class TFLongformerMaskedLMOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -237,9 +237,9 @@ class TFLongformerQuestionAnsweringModelOutput(ModelOutput): loss: tf.Tensor | None = None start_logits: Optional[tf.Tensor] = None end_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -283,9 +283,9 @@ class TFLongformerSequenceClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -331,9 +331,9 @@ class TFLongformerMultipleChoiceModelOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -377,9 +377,9 @@ class TFLongformerTokenClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - global_attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + global_attentions: tuple[tf.Tensor, ...] | None = None def _compute_global_attention_mask(input_ids_shape, sep_token_indices, before_sep_token=True): @@ -2142,7 +2142,7 @@ class TFLongformerModel(TFLongformerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFLongformerBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerBaseModelOutputWithPooling, tuple[tf.Tensor]]: outputs = self.longformer( input_ids=input_ids, attention_mask=attention_mask, @@ -2213,7 +2213,7 @@ class TFLongformerForMaskedLM(TFLongformerPreTrainedModel, TFMaskedLanguageModel return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFLongformerMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2310,7 +2310,7 @@ class TFLongformerForQuestionAnswering(TFLongformerPreTrainedModel, TFQuestionAn start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFLongformerQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -2482,7 +2482,7 @@ class TFLongformerForSequenceClassification(TFLongformerPreTrainedModel, TFSeque return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFLongformerSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerSequenceClassifierOutput, tuple[tf.Tensor]]: if input_ids is not None and not isinstance(input_ids, tf.Tensor): input_ids = tf.convert_to_tensor(input_ids, dtype=tf.int64) elif input_ids is not None: @@ -2608,7 +2608,7 @@ class TFLongformerForMultipleChoice(TFLongformerPreTrainedModel, TFMultipleChoic return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFLongformerMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -2726,7 +2726,7 @@ class TFLongformerForTokenClassification(TFLongformerPreTrainedModel, TFTokenCla return_dict: Optional[bool] = None, labels: Optional[Union[np.array, tf.Tensor]] = None, training: Optional[bool] = False, - ) -> Union[TFLongformerTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFLongformerTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. diff --git a/src/transformers/models/longformer/tokenization_longformer.py b/src/transformers/models/longformer/tokenization_longformer.py index afecf750135..fb033493c35 100644 --- a/src/transformers/models/longformer/tokenization_longformer.py +++ b/src/transformers/models/longformer/tokenization_longformer.py @@ -16,7 +16,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -288,7 +288,7 @@ class LongformerTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -318,8 +318,8 @@ class LongformerTokenizer(PreTrainedTokenizer): return vocab_file, merge_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Longformer sequence has the following format: @@ -328,13 +328,13 @@ class LongformerTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -343,22 +343,22 @@ class LongformerTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -370,20 +370,20 @@ class LongformerTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Longformer does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/longformer/tokenization_longformer_fast.py b/src/transformers/models/longformer/tokenization_longformer_fast.py index b8111b3d8a2..bde6bb55fec 100644 --- a/src/transformers/models/longformer/tokenization_longformer_fast.py +++ b/src/transformers/models/longformer/tokenization_longformer_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization classes for Longformer.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -227,7 +227,7 @@ class LongformerTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -239,20 +239,20 @@ class LongformerTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. Longformer does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/longt5/modeling_flax_longt5.py b/src/transformers/models/longt5/modeling_flax_longt5.py index 2674c225e58..dee4afeadf7 100644 --- a/src/transformers/models/longt5/modeling_flax_longt5.py +++ b/src/transformers/models/longt5/modeling_flax_longt5.py @@ -15,7 +15,7 @@ """Flax LongT5 model.""" import copy -from typing import Any, Callable, List, Optional, Tuple +from typing import Any, Callable, Optional import flax.linen as nn import jax @@ -99,7 +99,7 @@ def _concatenate_3_blocks(x: jnp.ndarray, block_axis: int, sequence_axis: int, p # [batch_size, num_blocks, block_len] -> [batch_size, num_blocks + 2, block_len] x = jnp.pad(x, pad_width=pad, mode="constant", constant_values=pad_value) - blocks_list: List[np.array] = [] + blocks_list: list[np.array] = [] for i in range(3): # We use indexing approach here: # https://numpy.org/doc/stable/user/basics.indexing.html#dealing-with-variable-numbers-of-indices-within-programs @@ -142,7 +142,7 @@ def _get_local_attention_mask(attention_mask: np.ndarray, block_len: int) -> jnp return local_attention_mask[:, None, ...] -def _make_global_fixed_block_ids(attention_mask: np.ndarray, global_block_size: int) -> Tuple[jnp.ndarray, np.ndarray]: +def _make_global_fixed_block_ids(attention_mask: np.ndarray, global_block_size: int) -> tuple[jnp.ndarray, np.ndarray]: """Obtain the "fixed block" global id corresponding to each input token. This implementation is a simplified version of the original Flaxformr implementation adopted from: @@ -1675,7 +1675,7 @@ class FlaxLongT5PreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: LongT5Config, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -1691,7 +1691,7 @@ class FlaxLongT5PreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") diff --git a/src/transformers/models/longt5/modeling_longt5.py b/src/transformers/models/longt5/modeling_longt5.py index 35d9cb30a95..2ddcc4f3d4d 100644 --- a/src/transformers/models/longt5/modeling_longt5.py +++ b/src/transformers/models/longt5/modeling_longt5.py @@ -17,7 +17,7 @@ import copy import math import warnings -from typing import Any, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch from torch import nn @@ -103,7 +103,7 @@ def _concatenate_3_blocks(x: torch.Tensor, block_dim: int, sequence_dim: int, pa # [batch_size, num_blocks, block_len] -> [batch_size, num_blocks + 2, block_len] x = nn.functional.pad(x, pad=pad, mode="constant", value=pad_value) - blocks_list: List[torch.Tensor] = [] + blocks_list: list[torch.Tensor] = [] for i in range(3): # We use indexing approach here: # https://numpy.org/doc/stable/user/basics.indexing.html#dealing-with-variable-numbers-of-indices-within-programs @@ -151,7 +151,7 @@ def _get_local_attention_mask(attention_mask: torch.Tensor, block_len: int, devi def _make_global_fixed_block_ids( attention_mask: torch.Tensor, global_block_size: int -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: """Obtain the "fixed block" global id corresponding to each input token. This implementation is a simplified version of the original Flaxformr implementation adopted from: @@ -1787,8 +1787,8 @@ class LongT5Model(LongT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1796,7 +1796,7 @@ class LongT5Model(LongT5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. LongT5 is a model with relative position embeddings so @@ -1988,8 +1988,8 @@ class LongT5ForConditionalGeneration(LongT5PreTrainedModel, GenerationMixin): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1998,7 +1998,7 @@ class LongT5ForConditionalGeneration(LongT5PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. LongT5 is a model with relative position embeddings so @@ -2224,7 +2224,7 @@ class LongT5EncoderModel(LongT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. LongT5 is a model with relative position embeddings so diff --git a/src/transformers/models/luke/modeling_luke.py b/src/transformers/models/luke/modeling_luke.py index b921dbda67e..c01bb7453f9 100644 --- a/src/transformers/models/luke/modeling_luke.py +++ b/src/transformers/models/luke/modeling_luke.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -62,7 +62,7 @@ class BaseLukeModelOutputWithPooling(BaseModelOutputWithPooling): """ entity_last_hidden_state: Optional[torch.FloatTensor] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -93,7 +93,7 @@ class BaseLukeModelOutput(BaseModelOutput): """ entity_last_hidden_state: Optional[torch.FloatTensor] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -134,9 +134,9 @@ class LukeMaskedLMOutput(ModelOutput): mep_loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None entity_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -165,9 +165,9 @@ class EntityClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -196,9 +196,9 @@ class EntityPairClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -227,9 +227,9 @@ class EntitySpanClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -261,9 +261,9 @@ class LukeSequenceClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -295,9 +295,9 @@ class LukeTokenClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -332,9 +332,9 @@ class LukeQuestionAnsweringModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -368,9 +368,9 @@ class LukeMultipleChoiceModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - entity_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + entity_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None class LukeEmbeddings(nn.Module): @@ -952,7 +952,7 @@ class LukeModel(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseLukeModelOutputWithPooling]: + ) -> Union[tuple, BaseLukeModelOutputWithPooling]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1221,7 +1221,7 @@ class LukeForMaskedLM(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LukeMaskedLMOutput]: + ) -> Union[tuple, LukeMaskedLMOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1356,7 +1356,7 @@ class LukeForEntityClassification(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, EntityClassificationOutput]: + ) -> Union[tuple, EntityClassificationOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1486,7 +1486,7 @@ class LukeForEntityPairClassification(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, EntityPairClassificationOutput]: + ) -> Union[tuple, EntityPairClassificationOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1623,7 +1623,7 @@ class LukeForEntitySpanClassification(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, EntitySpanClassificationOutput]: + ) -> Union[tuple, EntitySpanClassificationOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1780,7 +1780,7 @@ class LukeForSequenceClassification(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LukeSequenceClassifierOutput]: + ) -> Union[tuple, LukeSequenceClassifierOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -1908,7 +1908,7 @@ class LukeForTokenClassification(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LukeTokenClassifierOutput]: + ) -> Union[tuple, LukeTokenClassifierOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -2011,7 +2011,7 @@ class LukeForQuestionAnswering(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LukeQuestionAnsweringModelOutput]: + ) -> Union[tuple, LukeQuestionAnsweringModelOutput]: r""" entity_ids (`torch.LongTensor` of shape `(batch_size, entity_length)`): Indices of entity tokens in the entity vocabulary. @@ -2130,7 +2130,7 @@ class LukeForMultipleChoice(LukePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, LukeMultipleChoiceModelOutput]: + ) -> Union[tuple, LukeMultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/luke/tokenization_luke.py b/src/transformers/models/luke/tokenization_luke.py index beb06aed6e4..b91f4e8c2ed 100644 --- a/src/transformers/models/luke/tokenization_luke.py +++ b/src/transformers/models/luke/tokenization_luke.py @@ -19,7 +19,7 @@ import json import os from collections.abc import Mapping from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import regex as re @@ -42,10 +42,10 @@ from ...utils import add_end_docstrings, is_tf_tensor, is_torch_tensor, logging logger = logging.get_logger(__name__) -EntitySpan = Tuple[int, int] -EntitySpanInput = List[EntitySpan] +EntitySpan = tuple[int, int] +EntitySpanInput = list[EntitySpan] Entity = str -EntityInput = List[Entity] +EntityInput = list[Entity] VOCAB_FILES_NAMES = { "vocab_file": "vocab.json", @@ -470,8 +470,8 @@ class LukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.build_inputs_with_special_tokens with Roberta->Luke, RoBERTa->LUKE def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A LUKE sequence has the following format: @@ -480,13 +480,13 @@ class LukeTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -496,22 +496,22 @@ class LukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.get_special_tokens_mask with Roberta->Luke, RoBERTa->LUKE def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -524,20 +524,20 @@ class LukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.roberta.tokenization_roberta.RobertaTokenizer.create_token_type_ids_from_sequences with Roberta->Luke, RoBERTa->LUKE def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. LUKE does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -556,12 +556,12 @@ class LukeTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, List[TextInput]], - text_pair: Optional[Union[TextInput, List[TextInput]]] = None, - entity_spans: Optional[Union[EntitySpanInput, List[EntitySpanInput]]] = None, - entity_spans_pair: Optional[Union[EntitySpanInput, List[EntitySpanInput]]] = None, - entities: Optional[Union[EntityInput, List[EntityInput]]] = None, - entities_pair: Optional[Union[EntityInput, List[EntityInput]]] = None, + text: Union[TextInput, list[TextInput]], + text_pair: Optional[Union[TextInput, list[TextInput]]] = None, + entity_spans: Optional[Union[EntitySpanInput, list[EntitySpanInput]]] = None, + entity_spans_pair: Optional[Union[EntitySpanInput, list[EntitySpanInput]]] = None, + entities: Optional[Union[EntityInput, list[EntityInput]]] = None, + entities_pair: Optional[Union[EntityInput, list[EntityInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -586,31 +586,31 @@ class LukeTokenizer(PreTrainedTokenizer): sequences, depending on the task you want to prepare them for. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this tokenizer does not support tokenization based on pretokenized strings. - text_pair (`str`, `List[str]`, `List[List[str]]`): + text_pair (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this tokenizer does not support tokenization based on pretokenized strings. - entity_spans (`List[Tuple[int, int]]`, `List[List[Tuple[int, int]]]`, *optional*): + entity_spans (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*): The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each with two integers denoting character-based start and end positions of entities. If you specify `"entity_classification"` or `"entity_pair_classification"` as the `task` argument in the constructor, the length of each sequence must be 1 or 2, respectively. If you specify `entities`, the length of each sequence must be equal to the length of each sequence of `entities`. - entity_spans_pair (`List[Tuple[int, int]]`, `List[List[Tuple[int, int]]]`, *optional*): + entity_spans_pair (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*): The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each with two integers denoting character-based start and end positions of entities. If you specify the `task` argument in the constructor, this argument is ignored. If you specify `entities_pair`, the length of each sequence must be equal to the length of each sequence of `entities_pair`. - entities (`List[str]`, `List[List[str]]`, *optional*): + entities (`list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of each sequence must be equal to the length of each sequence of `entity_spans`. If you specify `entity_spans` without specifying this argument, the entity sequence or the batch of entity sequences is automatically constructed by filling it with the [MASK] entity. - entities_pair (`List[str]`, `List[List[str]]`, *optional*): + entities_pair (`list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of @@ -624,14 +624,14 @@ class LukeTokenizer(PreTrainedTokenizer): is_valid_single_text = isinstance(text, str) is_valid_batch_text = isinstance(text, (list, tuple)) and (len(text) == 0 or (isinstance(text[0], str))) if not (is_valid_single_text or is_valid_batch_text): - raise ValueError("text input must be of type `str` (single example) or `List[str]` (batch).") + raise ValueError("text input must be of type `str` (single example) or `list[str]` (batch).") is_valid_single_text_pair = isinstance(text_pair, str) is_valid_batch_text_pair = isinstance(text_pair, (list, tuple)) and ( len(text_pair) == 0 or isinstance(text_pair[0], str) ) if not (text_pair is None or is_valid_single_text_pair or is_valid_batch_text_pair): - raise ValueError("text_pair input must be of type `str` (single example) or `List[str]` (batch).") + raise ValueError("text_pair input must be of type `str` (single example) or `list[str]` (batch).") is_batched = bool(isinstance(text, (list, tuple))) @@ -786,12 +786,12 @@ class LukeTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, - batch_text_or_text_pairs: Union[List[TextInput], List[TextInputPair]], + batch_text_or_text_pairs: Union[list[TextInput], list[TextInputPair]], batch_entity_spans_or_entity_spans_pairs: Optional[ - Union[List[EntitySpanInput], List[Tuple[EntitySpanInput, EntitySpanInput]]] + Union[list[EntitySpanInput], list[tuple[EntitySpanInput, EntitySpanInput]]] ] = None, batch_entities_or_entities_pairs: Optional[ - Union[List[EntityInput], List[Tuple[EntityInput, EntityInput]]] + Union[list[EntityInput], list[tuple[EntityInput, EntityInput]]] ] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -921,7 +921,7 @@ class LukeTokenizer(PreTrainedTokenizer): entity_spans: Optional[EntitySpanInput] = None, entity_spans_pair: Optional[EntitySpanInput] = None, **kwargs, - ) -> Tuple[list, list, list, list, list, list]: + ) -> tuple[list, list, list, list, list, list]: def get_input_ids(text): tokens = self.tokenize(text, **kwargs) return self.convert_tokens_to_ids(tokens) @@ -1067,9 +1067,9 @@ class LukeTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def _batch_prepare_for_model( self, - batch_ids_pairs: List[Tuple[List[int], None]], - batch_entity_ids_pairs: List[Tuple[Optional[List[int]], Optional[List[int]]]], - batch_entity_token_spans_pairs: List[Tuple[Optional[List[Tuple[int, int]]], Optional[List[Tuple[int, int]]]]], + batch_ids_pairs: list[tuple[list[int], None]], + batch_entity_ids_pairs: list[tuple[Optional[list[int]], Optional[list[int]]]], + batch_entity_token_spans_pairs: list[tuple[Optional[list[tuple[int, int]]], Optional[list[tuple[int, int]]]]], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1152,12 +1152,12 @@ class LukeTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def prepare_for_model( self, - ids: List[int], - pair_ids: Optional[List[int]] = None, - entity_ids: Optional[List[int]] = None, - pair_entity_ids: Optional[List[int]] = None, - entity_token_spans: Optional[List[Tuple[int, int]]] = None, - pair_entity_token_spans: Optional[List[Tuple[int, int]]] = None, + ids: list[int], + pair_ids: Optional[list[int]] = None, + entity_ids: Optional[list[int]] = None, + pair_entity_ids: Optional[list[int]] = None, + entity_token_spans: Optional[list[tuple[int, int]]] = None, + pair_entity_token_spans: Optional[list[tuple[int, int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1186,17 +1186,17 @@ class LukeTokenizer(PreTrainedTokenizer): error. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. - entity_ids (`List[int]`, *optional*): + entity_ids (`list[int]`, *optional*): Entity ids of the first sequence. - pair_entity_ids (`List[int]`, *optional*): + pair_entity_ids (`list[int]`, *optional*): Entity ids of the second sequence. - entity_token_spans (`List[Tuple[int, int]]`, *optional*): + entity_token_spans (`list[tuple[int, int]]`, *optional*): Entity spans of the first sequence. - pair_entity_token_spans (`List[Tuple[int, int]]`, *optional*): + pair_entity_token_spans (`list[tuple[int, int]]`, *optional*): Entity spans of the second sequence. max_entity_length (`int`, *optional*): The maximum length of the entity sequence. @@ -1385,10 +1385,10 @@ class LukeTokenizer(PreTrainedTokenizer): self, encoded_inputs: Union[ BatchEncoding, - List[BatchEncoding], - Dict[str, EncodedInput], - Dict[str, List[EncodedInput]], - List[Dict[str, EncodedInput]], + list[BatchEncoding], + dict[str, EncodedInput], + dict[str, list[EncodedInput]], + list[dict[str, EncodedInput]], ], padding: Union[bool, str, PaddingStrategy] = True, max_length: Optional[int] = None, @@ -1408,11 +1408,11 @@ class LukeTokenizer(PreTrainedTokenizer): specific device of your tensors however. Args: - encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `Dict[str, List[int]]`, `Dict[str, List[List[int]]` or `List[Dict[str, List[int]]]`): - Tokenized inputs. Can represent one input ([`BatchEncoding`] or `Dict[str, List[int]]`) or a batch of - tokenized inputs (list of [`BatchEncoding`], *Dict[str, List[List[int]]]* or *List[Dict[str, - List[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader - collate function. Instead of `List[int]` you can have tensors (numpy arrays, PyTorch tensors or + encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `dict[str, list[int]]`, `dict[str, list[list[int]]` or `list[dict[str, list[int]]]`): + Tokenized inputs. Can represent one input ([`BatchEncoding`] or `dict[str, list[int]]`) or a batch of + tokenized inputs (list of [`BatchEncoding`], *dict[str, list[list[int]]]* or *list[dict[str, + list[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader + collate function. Instead of `list[int]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see the note above for the return type. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`): Select a strategy to pad the returned sequences (according to the model's padding side and padding @@ -1549,7 +1549,7 @@ class LukeTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, max_entity_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -1563,7 +1563,7 @@ class LukeTokenizer(PreTrainedTokenizer): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. max_entity_length: The maximum length of the entity sequence. @@ -1691,7 +1691,7 @@ class LukeTokenizer(PreTrainedTokenizer): return encoded_inputs - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/lxmert/modeling_lxmert.py b/src/transformers/models/lxmert/modeling_lxmert.py index 8e73378df56..252fe2f40d0 100644 --- a/src/transformers/models/lxmert/modeling_lxmert.py +++ b/src/transformers/models/lxmert/modeling_lxmert.py @@ -18,7 +18,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -80,11 +80,11 @@ class LxmertModelOutput(ModelOutput): language_output: Optional[torch.FloatTensor] = None vision_output: Optional[torch.FloatTensor] = None pooled_output: Optional[torch.FloatTensor] = None - language_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - language_attentions: Optional[Tuple[torch.FloatTensor]] = None - vision_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + language_hidden_states: Optional[tuple[torch.FloatTensor]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + language_attentions: Optional[tuple[torch.FloatTensor]] = None + vision_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -120,11 +120,11 @@ class LxmertForQuestionAnsweringOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None question_answering_score: Optional[torch.FloatTensor] = None - language_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - language_attentions: Optional[Tuple[torch.FloatTensor]] = None - vision_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + language_hidden_states: Optional[tuple[torch.FloatTensor]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + language_attentions: Optional[tuple[torch.FloatTensor]] = None + vision_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -168,11 +168,11 @@ class LxmertForPreTrainingOutput(ModelOutput): prediction_logits: Optional[torch.FloatTensor] = None cross_relationship_score: Optional[torch.FloatTensor] = None question_answering_score: Optional[torch.FloatTensor] = None - language_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - language_attentions: Optional[Tuple[torch.FloatTensor]] = None - vision_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + language_hidden_states: Optional[tuple[torch.FloatTensor]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor]] = None + language_attentions: Optional[tuple[torch.FloatTensor]] = None + vision_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_encoder_attentions: Optional[tuple[torch.FloatTensor]] = None def load_tf_weights_in_lxmert(model, config, tf_checkpoint_path): @@ -809,7 +809,7 @@ class LxmertModel(LxmertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[LxmertModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[LxmertModelOutput, tuple[torch.FloatTensor]]: r""" visual_feats (`torch.FloatTensor` of shape `(batch_size, num_visual_features, visual_feat_dim)`): This input represents visual features. They ROI pooled object features from bounding boxes using a @@ -1092,14 +1092,14 @@ class LxmertForPreTraining(LxmertPreTrainedModel): token_type_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - obj_labels: Optional[Dict[str, Tuple[torch.FloatTensor, torch.FloatTensor]]] = None, + obj_labels: Optional[dict[str, tuple[torch.FloatTensor, torch.FloatTensor]]] = None, matched_label: Optional[torch.LongTensor] = None, ans: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[LxmertForPreTrainingOutput, Tuple[torch.FloatTensor]]: + ) -> Union[LxmertForPreTrainingOutput, tuple[torch.FloatTensor]]: r""" visual_feats (`torch.FloatTensor` of shape `(batch_size, num_visual_features, visual_feat_dim)`): This input represents visual features. They ROI pooled object features from bounding boxes using a @@ -1123,7 +1123,7 @@ class LxmertForPreTraining(LxmertPreTrainedModel): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` - obj_labels (`Dict[Str: Tuple[Torch.FloatTensor, Torch.FloatTensor]]`, *optional*): + obj_labels (`dict[Str: tuple[Torch.FloatTensor, Torch.FloatTensor]]`, *optional*): each key is named after each one of the visual losses and each element of the tuple is of the shape `(batch_size, num_features)` and `(batch_size, num_features, visual_feature_dim)` for each the label id and the label score respectively @@ -1342,7 +1342,7 @@ class LxmertForQuestionAnswering(LxmertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[LxmertForQuestionAnsweringOutput, Tuple[torch.FloatTensor]]: + ) -> Union[LxmertForQuestionAnsweringOutput, tuple[torch.FloatTensor]]: r""" visual_feats (`torch.FloatTensor` of shape `(batch_size, num_visual_features, visual_feat_dim)`): This input represents visual features. They ROI pooled object features from bounding boxes using a diff --git a/src/transformers/models/lxmert/modeling_tf_lxmert.py b/src/transformers/models/lxmert/modeling_tf_lxmert.py index 637646ae2f4..1c4bc74db55 100644 --- a/src/transformers/models/lxmert/modeling_tf_lxmert.py +++ b/src/transformers/models/lxmert/modeling_tf_lxmert.py @@ -20,7 +20,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -92,11 +92,11 @@ class TFLxmertModelOutput(ModelOutput): language_output: tf.Tensor | None = None vision_output: tf.Tensor | None = None pooled_output: tf.Tensor | None = None - language_hidden_states: Tuple[tf.Tensor] | None = None - vision_hidden_states: Tuple[tf.Tensor] | None = None - language_attentions: Tuple[tf.Tensor] | None = None - vision_attentions: Tuple[tf.Tensor] | None = None - cross_encoder_attentions: Tuple[tf.Tensor] | None = None + language_hidden_states: tuple[tf.Tensor] | None = None + vision_hidden_states: tuple[tf.Tensor] | None = None + language_attentions: tuple[tf.Tensor] | None = None + vision_attentions: tuple[tf.Tensor] | None = None + cross_encoder_attentions: tuple[tf.Tensor] | None = None @dataclass @@ -140,11 +140,11 @@ class TFLxmertForPreTrainingOutput(ModelOutput): prediction_logits: tf.Tensor | None = None cross_relationship_score: tf.Tensor | None = None question_answering_score: tf.Tensor | None = None - language_hidden_states: Tuple[tf.Tensor] | None = None - vision_hidden_states: Tuple[tf.Tensor] | None = None - language_attentions: Tuple[tf.Tensor] | None = None - vision_attentions: Tuple[tf.Tensor] | None = None - cross_encoder_attentions: Tuple[tf.Tensor] | None = None + language_hidden_states: tuple[tf.Tensor] | None = None + vision_hidden_states: tuple[tf.Tensor] | None = None + language_attentions: tuple[tf.Tensor] | None = None + vision_attentions: tuple[tf.Tensor] | None = None + cross_encoder_attentions: tuple[tf.Tensor] | None = None class TFLxmertVisualFeatureEncoder(keras.layers.Layer): @@ -1113,7 +1113,7 @@ class TFLxmertModel(TFLxmertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFLxmertModelOutput]: + ) -> Union[tuple, TFLxmertModelOutput]: outputs = self.lxmert( input_ids, visual_feats, @@ -1235,7 +1235,7 @@ class TFLxmertLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -1505,20 +1505,20 @@ class TFLxmertForPreTraining(TFLxmertPreTrainedModel): token_type_ids: tf.Tensor | None = None, inputs_embeds: tf.Tensor | None = None, masked_lm_labels: tf.Tensor | None = None, - obj_labels: Dict[str, Tuple[tf.Tensor, tf.Tensor]] | None = None, + obj_labels: dict[str, tuple[tf.Tensor, tf.Tensor]] | None = None, matched_label: tf.Tensor | None = None, ans: tf.Tensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> Tuple[tf.Tensor] | TFLxmertForPreTrainingOutput: + ) -> tuple[tf.Tensor] | TFLxmertForPreTrainingOutput: r""" masked_lm_labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` - obj_labels (`Dict[Str: Tuple[tf.Tensor, tf.Tensor]]`, *optional*, defaults to `None`): + obj_labels (`dict[Str: tuple[tf.Tensor, tf.Tensor]]`, *optional*, defaults to `None`): each key is named after each one of the visual losses and each element of the tuple is of the shape `(batch_size, num_features)` and `(batch_size, num_features, visual_feature_dim)` for each the label id and the label score respectively diff --git a/src/transformers/models/lxmert/tokenization_lxmert.py b/src/transformers/models/lxmert/tokenization_lxmert.py index 5f6d39a0ca1..5f69ab5e856 100644 --- a/src/transformers/models/lxmert/tokenization_lxmert.py +++ b/src/transformers/models/lxmert/tokenization_lxmert.py @@ -16,7 +16,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -186,8 +186,8 @@ class LxmertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A Lxmert sequence has the following format: @@ -211,8 +211,8 @@ class LxmertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -238,7 +238,7 @@ class LxmertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/lxmert/tokenization_lxmert_fast.py b/src/transformers/models/lxmert/tokenization_lxmert_fast.py index eadcff6d418..fcfa3263acd 100644 --- a/src/transformers/models/lxmert/tokenization_lxmert_fast.py +++ b/src/transformers/models/lxmert/tokenization_lxmert_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -135,7 +135,7 @@ class LxmertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/m2m_100/modeling_m2m_100.py b/src/transformers/models/m2m_100/modeling_m2m_100.py index 0b19afb8f07..25ed975e4d5 100755 --- a/src/transformers/models/m2m_100/modeling_m2m_100.py +++ b/src/transformers/models/m2m_100/modeling_m2m_100.py @@ -15,7 +15,7 @@ """PyTorch M2M100 model.""" import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -262,7 +262,7 @@ class M2M100Attention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -963,7 +963,7 @@ class M2M100Decoder(M2M100PreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1255,8 +1255,8 @@ class M2M100Model(M2M100PreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1264,7 +1264,7 @@ class M2M100Model(M2M100PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1383,8 +1383,8 @@ class M2M100ForConditionalGeneration(M2M100PreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1393,7 +1393,7 @@ class M2M100ForConditionalGeneration(M2M100PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/m2m_100/tokenization_m2m_100.py b/src/transformers/models/m2m_100/tokenization_m2m_100.py index f47f9687317..ccced10f2ba 100644 --- a/src/transformers/models/m2m_100/tokenization_m2m_100.py +++ b/src/transformers/models/m2m_100/tokenization_m2m_100.py @@ -17,7 +17,7 @@ import json import os from pathlib import Path from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece @@ -108,8 +108,8 @@ class M2M100Tokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -123,7 +123,7 @@ class M2M100Tokenizer(PreTrainedTokenizer): pad_token="", unk_token="", language_codes="m2m100", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, num_madeup_words=8, **kwargs, ) -> None: @@ -179,7 +179,7 @@ class M2M100Tokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.encoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab @@ -193,7 +193,7 @@ class M2M100Tokenizer(PreTrainedTokenizer): self._src_lang = new_src_lang self.set_src_lang_special_tokens(self._src_lang) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -222,22 +222,22 @@ class M2M100Tokenizer(PreTrainedTokenizer): return out_string.strip() def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -252,8 +252,8 @@ class M2M100Tokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An MBART sequence has the following format, where `X` represents the sequence: @@ -265,25 +265,25 @@ class M2M100Tokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens # We don't expect to process pairs, but leave the pair logic for API consistency return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens - def __getstate__(self) -> Dict: + def __getstate__(self) -> dict: state = self.__dict__.copy() state["sp_model"] = None return state - def __setstate__(self, d: Dict) -> None: + def __setstate__(self, d: dict) -> None: self.__dict__ = d # for backward compatibility @@ -292,7 +292,7 @@ class M2M100Tokenizer(PreTrainedTokenizer): self.sp_model = load_spm(self.spm_file, self.sp_model_kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: save_dir = Path(save_directory) if not save_dir.is_dir(): raise OSError(f"{save_directory} should be a directory") @@ -316,9 +316,9 @@ class M2M100Tokenizer(PreTrainedTokenizer): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "ro", **kwargs, ) -> BatchEncoding: @@ -365,13 +365,13 @@ class M2M100Tokenizer(PreTrainedTokenizer): return self.lang_token_to_id[lang_token] -def load_spm(path: str, sp_model_kwargs: Dict[str, Any]) -> sentencepiece.SentencePieceProcessor: +def load_spm(path: str, sp_model_kwargs: dict[str, Any]) -> sentencepiece.SentencePieceProcessor: spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs) spm.Load(str(path)) return spm -def load_json(path: str) -> Union[Dict, List]: +def load_json(path: str) -> Union[dict, list]: with open(path, "r") as f: return json.load(f) diff --git a/src/transformers/models/mamba/convert_mamba_ssm_checkpoint_to_pytorch.py b/src/transformers/models/mamba/convert_mamba_ssm_checkpoint_to_pytorch.py index f55b032207c..eaedafa13fe 100644 --- a/src/transformers/models/mamba/convert_mamba_ssm_checkpoint_to_pytorch.py +++ b/src/transformers/models/mamba/convert_mamba_ssm_checkpoint_to_pytorch.py @@ -17,7 +17,6 @@ import argparse import json import math -from typing import Tuple import torch @@ -53,7 +52,7 @@ logger = logging.get_logger(__name__) def convert_mamba_ssm_checkpoint_to_huggingface_model( original_state_dict: dict, original_ssm_config_dict: dict -) -> Tuple[MambaForCausalLM, AutoTokenizer]: +) -> tuple[MambaForCausalLM, AutoTokenizer]: if not is_mamba_ssm_available(): raise ImportError( "Calling convert_mamba_ssm_checkpoint_to_huggingface_model requires the mamba_ssm library to be installed. Please install it with `pip install mamba_ssm`." diff --git a/src/transformers/models/mamba/modeling_mamba.py b/src/transformers/models/mamba/modeling_mamba.py index 7f2497849f1..0f6dfab8112 100644 --- a/src/transformers/models/mamba/modeling_mamba.py +++ b/src/transformers/models/mamba/modeling_mamba.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -449,7 +449,7 @@ class MambaOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None cache_params: Optional[MambaCache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -477,7 +477,7 @@ class MambaCausalLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None cache_params: Optional[MambaCache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -517,7 +517,7 @@ class MambaModel(MambaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, MambaOutput]: + ) -> Union[tuple, MambaOutput]: r""" cache_params (`MambaCache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the @@ -620,8 +620,8 @@ class MambaForCausalLM(MambaPreTrainedModel, GenerationMixin): return self.backbone.set_input_embeddings(new_embeddings) def _update_model_kwargs_for_generation( - self, outputs: ModelOutput, model_kwargs: Dict[str, Any], num_new_tokens: int = 1, **kwargs - ) -> Dict[str, Any]: + self, outputs: ModelOutput, model_kwargs: dict[str, Any], num_new_tokens: int = 1, **kwargs + ) -> dict[str, Any]: model_kwargs["cache_params"] = outputs.get("cache_params", None) if ( model_kwargs.get("use_cache", True) @@ -699,7 +699,7 @@ class MambaForCausalLM(MambaPreTrainedModel, GenerationMixin): use_cache: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, **kwargs, # for now we need this for generation - ) -> Union[Tuple, MambaCausalLMOutput]: + ) -> Union[tuple, MambaCausalLMOutput]: r""" cache_params (`MambaCache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the diff --git a/src/transformers/models/mamba2/convert_mamba2_ssm_checkpoint_to_pytorch.py b/src/transformers/models/mamba2/convert_mamba2_ssm_checkpoint_to_pytorch.py index bd1d413f40a..482dd539b82 100644 --- a/src/transformers/models/mamba2/convert_mamba2_ssm_checkpoint_to_pytorch.py +++ b/src/transformers/models/mamba2/convert_mamba2_ssm_checkpoint_to_pytorch.py @@ -18,7 +18,7 @@ import argparse import json from functools import partial from os import path -from typing import Dict, Optional +from typing import Optional import torch from safetensors import safe_open @@ -27,7 +27,7 @@ from safetensors.torch import save_model from transformers import GPTNeoXTokenizerFast, LlamaTokenizerFast, Mamba2Config, Mamba2ForCausalLM -def load_state_dict_from_safetensors(mamba2_checkpoint_path: str, ckpt_name: str) -> Dict[str, torch.Tensor]: +def load_state_dict_from_safetensors(mamba2_checkpoint_path: str, ckpt_name: str) -> dict[str, torch.Tensor]: # Load weights and config from paths original_state_dict = {} with safe_open(path.join(mamba2_checkpoint_path, ckpt_name), framework="pt") as f: @@ -37,11 +37,11 @@ def load_state_dict_from_safetensors(mamba2_checkpoint_path: str, ckpt_name: str return original_state_dict -def load_state_dict_from_torch(mamba2_checkpoint_path: str, ckpt_name: str) -> Dict[str, torch.Tensor]: +def load_state_dict_from_torch(mamba2_checkpoint_path: str, ckpt_name: str) -> dict[str, torch.Tensor]: return torch.load(path.join(mamba2_checkpoint_path, ckpt_name), map_location="cpu", weights_only=True) -def convert_ssm_config_to_hf_config(config_ssm: Dict, mamba2_model_dict: Dict) -> Mamba2Config: +def convert_ssm_config_to_hf_config(config_ssm: dict, mamba2_model_dict: dict) -> Mamba2Config: """Convert a Mamba2Config from mamba_ssm to a Mamba2Config from here.""" hf_config = Mamba2Config() diff --git a/src/transformers/models/mamba2/modeling_mamba2.py b/src/transformers/models/mamba2/modeling_mamba2.py index 28e785b8604..638ae9ccbb4 100644 --- a/src/transformers/models/mamba2/modeling_mamba2.py +++ b/src/transformers/models/mamba2/modeling_mamba2.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -784,7 +784,7 @@ class Mamba2Output(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None cache_params: Optional[Mamba2Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -813,7 +813,7 @@ class Mamba2CausalLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None cache_params: Optional[Mamba2Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -854,7 +854,7 @@ class Mamba2Model(Mamba2PreTrainedModel): cache_position: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, **kwargs, - ) -> Union[Tuple, Mamba2Output]: + ) -> Union[tuple, Mamba2Output]: r""" cache_params (`Mamba2Cache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the @@ -1019,7 +1019,7 @@ class Mamba2ForCausalLM(Mamba2PreTrainedModel, GenerationMixin): cache_position: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, **kwargs, # for now we need this for generation and loss_function - ) -> Union[Tuple, Mamba2CausalLMOutput]: + ) -> Union[tuple, Mamba2CausalLMOutput]: r""" cache_params (`Mamba2Cache`, *optional*): If passed along, the model uses the previous state in all the blocks (which will give the output for the diff --git a/src/transformers/models/marian/convert_marian_tatoeba_to_pytorch.py b/src/transformers/models/marian/convert_marian_tatoeba_to_pytorch.py index 6181e94c60a..abd1c4768d1 100644 --- a/src/transformers/models/marian/convert_marian_tatoeba_to_pytorch.py +++ b/src/transformers/models/marian/convert_marian_tatoeba_to_pytorch.py @@ -18,7 +18,6 @@ import json import os import re from pathlib import Path -from typing import Tuple import yaml from tqdm import tqdm @@ -107,7 +106,7 @@ class TatoebaConverter: print(f"Three letter monolingual code: {code}") return [code] - def resolve_lang_code(self, src, tgt) -> Tuple[str, str]: + def resolve_lang_code(self, src, tgt) -> tuple[str, str]: src_tags = self.get_tags(src, self.tag2name[src]) tgt_tags = self.get_tags(tgt, self.tag2name[tgt]) return src_tags, tgt_tags diff --git a/src/transformers/models/marian/convert_marian_to_pytorch.py b/src/transformers/models/marian/convert_marian_to_pytorch.py index fd38b2e17cb..6c432ebcdf6 100644 --- a/src/transformers/models/marian/convert_marian_to_pytorch.py +++ b/src/transformers/models/marian/convert_marian_to_pytorch.py @@ -19,7 +19,7 @@ import socket import time import warnings from pathlib import Path -from typing import Dict, List, Union +from typing import Union from zipfile import ZipFile import numpy as np @@ -61,7 +61,7 @@ def load_layers_(layer_lst: nn.ModuleList, opus_state: dict, converter, is_decod layer.load_state_dict(sd, strict=False) -def find_pretrained_model(src_lang: str, tgt_lang: str) -> List[str]: +def find_pretrained_model(src_lang: str, tgt_lang: str) -> list[str]: """Find models that can accept src_lang as input and return tgt_lang as output.""" prefix = "Helsinki-NLP/opus-mt-" model_list = list_models() @@ -94,7 +94,7 @@ def _cast_yaml_str(v): return v -def cast_marian_config(raw_cfg: Dict[str, str]) -> Dict: +def cast_marian_config(raw_cfg: dict[str, str]) -> dict: return {k: _cast_yaml_str(v) for k, v in raw_cfg.items()} @@ -315,7 +315,7 @@ def convert_all_sentencepiece_models(model_list=None, repo_path=None, dest_dir=P return save_paths -def lmap(f, x) -> List: +def lmap(f, x) -> list: return list(map(f, x)) @@ -370,7 +370,7 @@ def save_tokenizer_config(dest_dir: Path, separate_vocabs=False): save_json(dct, dest_dir / "tokenizer_config.json") -def add_to_vocab_(vocab: Dict[str, int], special_tokens: List[str]): +def add_to_vocab_(vocab: dict[str, int], special_tokens: list[str]): start = max(vocab.values()) + 1 added = 0 for tok in special_tokens: @@ -685,7 +685,7 @@ def load_yaml(path): return yaml.load(f, Loader=yaml.BaseLoader) -def save_json(content: Union[Dict, List], path: str) -> None: +def save_json(content: Union[dict, list], path: str) -> None: with open(path, "w") as f: json.dump(content, f) diff --git a/src/transformers/models/marian/modeling_flax_marian.py b/src/transformers/models/marian/modeling_flax_marian.py index bcd2ab5f32f..e1d9bea4fcd 100644 --- a/src/transformers/models/marian/modeling_flax_marian.py +++ b/src/transformers/models/marian/modeling_flax_marian.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -202,7 +202,7 @@ MARIAN_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -320,7 +320,7 @@ class FlaxMarianAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -441,7 +441,7 @@ class FlaxMarianEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -567,7 +567,7 @@ class FlaxMarianDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states # Self Attention @@ -878,7 +878,7 @@ class FlaxMarianPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: MarianConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -887,7 +887,7 @@ class FlaxMarianPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") # make sure initialization pass will work for FlaxMarianForSequenceClassificationModule diff --git a/src/transformers/models/marian/modeling_marian.py b/src/transformers/models/marian/modeling_marian.py index 18c435a393d..5630f916ee3 100755 --- a/src/transformers/models/marian/modeling_marian.py +++ b/src/transformers/models/marian/modeling_marian.py @@ -16,7 +16,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -197,7 +197,7 @@ class MarianAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -296,7 +296,7 @@ class MarianEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -386,7 +386,7 @@ class MarianDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -742,7 +742,7 @@ class MarianEncoder(MarianPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutput]: + ) -> Union[tuple[torch.Tensor], BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -907,14 +907,14 @@ class MarianDecoder(MarianPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1245,8 +1245,8 @@ class MarianModel(MarianPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple[torch.Tensor], BaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[Union[tuple[torch.Tensor], BaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1502,8 +1502,8 @@ class MarianMTModel(MarianPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Union[Tuple[torch.Tensor], BaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[Union[tuple[torch.Tensor], BaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1684,7 +1684,7 @@ class MarianForCausalLM(MarianPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1692,7 +1692,7 @@ class MarianForCausalLM(MarianPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/marian/modeling_tf_marian.py b/src/transformers/models/marian/modeling_tf_marian.py index 3a92dea6df0..9b03424444f 100644 --- a/src/transformers/models/marian/modeling_tf_marian.py +++ b/src/transformers/models/marian/modeling_tf_marian.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -213,11 +213,11 @@ class TFMarianAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -461,9 +461,9 @@ class TFMarianDecoderLayer(keras.layers.Layer): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_value: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -683,7 +683,7 @@ MARIAN_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -923,7 +923,7 @@ class TFMarianDecoder(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -973,7 +973,7 @@ class TFMarianDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1154,8 +1154,8 @@ class TFMarianMainLayer(keras.layers.Layer): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[tf.Tensor]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[tf.Tensor]]] = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1276,7 +1276,7 @@ class TFMarianModel(TFMarianPreTrainedModel): decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, encoder_outputs: tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: bool | None = None, @@ -1285,7 +1285,7 @@ class TFMarianModel(TFMarianPreTrainedModel): return_dict: bool | None = None, training: bool = False, **kwargs, - ) -> Tuple[tf.Tensor] | TFSeq2SeqModelOutput: + ) -> tuple[tf.Tensor] | TFSeq2SeqModelOutput: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1412,7 +1412,7 @@ class TFMarianMTModel(TFMarianPreTrainedModel, TFCausalLanguageModelingLoss): decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, encoder_outputs: TFBaseModelOutput | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: bool | None = None, @@ -1421,7 +1421,7 @@ class TFMarianMTModel(TFMarianPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: bool | None = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Tuple[tf.Tensor] | TFSeq2SeqLMOutput: + ) -> tuple[tf.Tensor] | TFSeq2SeqLMOutput: r""" labels (`tf.tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/marian/tokenization_marian.py b/src/transformers/models/marian/tokenization_marian.py index bf9e0a8a2a1..f7a70205be9 100644 --- a/src/transformers/models/marian/tokenization_marian.py +++ b/src/transformers/models/marian/tokenization_marian.py @@ -17,7 +17,7 @@ import re import warnings from pathlib import Path from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece @@ -70,7 +70,7 @@ class MarianTokenizer(PreTrainedTokenizer): The token used for padding, for example when batching sequences of different lengths. model_max_length (`int`, *optional*, defaults to 512): The maximum sentence length the model accepts. - additional_special_tokens (`List[str]`, *optional*, defaults to `["", ""]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["", ""]`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -118,7 +118,7 @@ class MarianTokenizer(PreTrainedTokenizer): eos_token="", pad_token="", model_max_length=512, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, separate_vocabs=False, **kwargs, ) -> None: @@ -190,7 +190,7 @@ class MarianTokenizer(PreTrainedTokenizer): code: list = [match.group(0)] if match else [] return code, self.language_code_re.sub("", text) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: code, text = self.remove_language_code(text) pieces = self.current_spm.encode(text, out_type=str) return code + pieces @@ -204,7 +204,7 @@ class MarianTokenizer(PreTrainedTokenizer): Convert a list of lists of token ids into a list of strings by calling decode. Args: - sequences (`Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]`): + sequences (`Union[list[int], list[list[int]], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -218,7 +218,7 @@ class MarianTokenizer(PreTrainedTokenizer): Will be passed to the underlying model specific decode method. Returns: - `List[str]`: The list of decoded sentences. + `list[str]`: The list of decoded sentences. """ return super().batch_decode(sequences, **kwargs) @@ -230,7 +230,7 @@ class MarianTokenizer(PreTrainedTokenizer): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -248,7 +248,7 @@ class MarianTokenizer(PreTrainedTokenizer): """ return super().decode(token_ids, **kwargs) - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """Uses source spm if _decode_use_source_tokenizer is True, and target spm otherwise""" sp_model = self.spm_source if self._decode_use_source_tokenizer else self.spm_target current_sub_tokens = [] @@ -264,7 +264,7 @@ class MarianTokenizer(PreTrainedTokenizer): out_string = out_string.replace(SPIECE_UNDERLINE, " ") return out_string.strip() - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """Build model inputs from a sequence by appending eos_token_id.""" if token_ids_1 is None: return token_ids_0 + [self.eos_token_id] @@ -284,7 +284,7 @@ class MarianTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.encoder) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -329,7 +329,7 @@ class MarianTokenizer(PreTrainedTokenizer): return tuple(saved_files) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: return self.get_src_vocab() def get_src_vocab(self): @@ -338,14 +338,14 @@ class MarianTokenizer(PreTrainedTokenizer): def get_tgt_vocab(self): return dict(self.target_encoder, **self.added_tokens_decoder) - def __getstate__(self) -> Dict: + def __getstate__(self) -> dict: state = self.__dict__.copy() state.update( dict.fromkeys(["spm_source", "spm_target", "current_spm", "punc_normalizer", "target_vocab_file"]) ) return state - def __setstate__(self, d: Dict) -> None: + def __setstate__(self, d: dict) -> None: self.__dict__ = d # for backward compatibility @@ -366,8 +366,8 @@ class MarianTokenizer(PreTrainedTokenizer): return [1 if x in all_special_ids else 0 for x in seq] def get_special_tokens_mask( - self, token_ids_0: List, token_ids_1: Optional[List] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list, token_ids_1: Optional[list] = None, already_has_special_tokens: bool = False + ) -> list[int]: """Get list where entries are [1] if a token is [eos] or [pad] else 0.""" if already_has_special_tokens: return self._special_token_mask(token_ids_0) @@ -377,7 +377,7 @@ class MarianTokenizer(PreTrainedTokenizer): return self._special_token_mask(token_ids_0 + token_ids_1) + [1] -def load_spm(path: str, sp_model_kwargs: Dict[str, Any]) -> sentencepiece.SentencePieceProcessor: +def load_spm(path: str, sp_model_kwargs: dict[str, Any]) -> sentencepiece.SentencePieceProcessor: spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs) spm.Load(path) return spm @@ -388,7 +388,7 @@ def save_json(data, path: str) -> None: json.dump(data, f, indent=2) -def load_json(path: str) -> Union[Dict, List]: +def load_json(path: str) -> Union[dict, list]: with open(path, "r") as f: return json.load(f) diff --git a/src/transformers/models/markuplm/feature_extraction_markuplm.py b/src/transformers/models/markuplm/feature_extraction_markuplm.py index 9b1bd29060b..43c6ed93953 100644 --- a/src/transformers/models/markuplm/feature_extraction_markuplm.py +++ b/src/transformers/models/markuplm/feature_extraction_markuplm.py @@ -101,7 +101,7 @@ class MarkupLMFeatureExtractor(FeatureExtractionMixin): Main method to prepare for the model one or several HTML strings. Args: - html_strings (`str`, `List[str]`): + html_strings (`str`, `list[str]`): The HTML string or batch of HTML strings from which to extract nodes and corresponding xpaths. Returns: @@ -155,7 +155,7 @@ class MarkupLMFeatureExtractor(FeatureExtractionMixin): if not valid_strings: raise ValueError( - "HTML strings must of type `str`, `List[str]` (batch of examples), " + "HTML strings must of type `str`, `list[str]` (batch of examples), " f"but is of type {type(html_strings)}." ) diff --git a/src/transformers/models/markuplm/modeling_markuplm.py b/src/transformers/models/markuplm/modeling_markuplm.py index f2ebe388de3..8ce6b5ed5ec 100755 --- a/src/transformers/models/markuplm/modeling_markuplm.py +++ b/src/transformers/models/markuplm/modeling_markuplm.py @@ -16,7 +16,7 @@ import math import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -365,9 +365,9 @@ class MarkupLMSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -500,9 +500,9 @@ class MarkupLMAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -540,9 +540,9 @@ class MarkupLMLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -619,12 +619,12 @@ class MarkupLMEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -776,7 +776,7 @@ class MarkupLMModel(MarkupLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: r""" xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*): Tag IDs for each token in the input sequence, padded up to config.max_depth. @@ -908,7 +908,7 @@ class MarkupLMForQuestionAnswering(MarkupLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*): Tag IDs for each token in the input sequence, padded up to config.max_depth. @@ -1028,7 +1028,7 @@ class MarkupLMForTokenClassification(MarkupLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*): Tag IDs for each token in the input sequence, padded up to config.max_depth. @@ -1135,7 +1135,7 @@ class MarkupLMForSequenceClassification(MarkupLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" xpath_tags_seq (`torch.LongTensor` of shape `(batch_size, sequence_length, config.max_depth)`, *optional*): Tag IDs for each token in the input sequence, padded up to config.max_depth. diff --git a/src/transformers/models/markuplm/tokenization_markuplm.py b/src/transformers/models/markuplm/tokenization_markuplm.py index b45f6916bce..9ad5bf688c4 100644 --- a/src/transformers/models/markuplm/tokenization_markuplm.py +++ b/src/transformers/models/markuplm/tokenization_markuplm.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import regex as re @@ -367,7 +367,7 @@ class MarkupLMTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -405,8 +405,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): return (text, kwargs) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RoBERTa sequence has the following format: @@ -414,12 +414,12 @@ class MarkupLMTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -428,36 +428,36 @@ class MarkupLMTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def build_xpath_tags_with_special_tokens( - self, xpath_tags_0: List[int], xpath_tags_1: Optional[List[int]] = None - ) -> List[int]: + self, xpath_tags_0: list[int], xpath_tags_1: Optional[list[int]] = None + ) -> list[int]: pad = [self.pad_xpath_tags_seq] if len(xpath_tags_1) == 0: return pad + xpath_tags_0 + pad return pad + xpath_tags_0 + pad + xpath_tags_1 + pad def build_xpath_subs_with_special_tokens( - self, xpath_subs_0: List[int], xpath_subs_1: Optional[List[int]] = None - ) -> List[int]: + self, xpath_subs_0: list[int], xpath_subs_1: Optional[list[int]] = None + ) -> list[int]: pad = [self.pad_xpath_subs_seq] if len(xpath_subs_1) == 0: return pad + xpath_subs_0 + pad return pad + xpath_subs_0 + pad + xpath_subs_1 + pad def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Args: Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -469,18 +469,18 @@ class MarkupLMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -492,10 +492,10 @@ class MarkupLMTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - xpaths: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - node_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + xpaths: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + node_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -518,16 +518,16 @@ class MarkupLMTokenizer(PreTrainedTokenizer): sequences with node-level xpaths and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (nodes of a single example or questions of a batch of examples) or a list of list of strings (batch of nodes). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - xpaths (`List[List[int]]`, `List[List[List[int]]]`): + xpaths (`list[list[int]]`, `list[list[list[int]]]`): Node-level xpaths. - node_labels (`List[int]`, `List[List[int]]`, *optional*): + node_labels (`list[int]`, `list[list[int]]`, *optional*): Node-level integer labels (for token classification tasks). """ @@ -555,18 +555,18 @@ class MarkupLMTokenizer(PreTrainedTokenizer): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = nodes if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "Nodes must be of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Nodes must be of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be nodes if not isinstance(text, (list, tuple)): raise ValueError( - "Nodes must be of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Nodes must be of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -641,13 +641,13 @@ class MarkupLMTokenizer(PreTrainedTokenizer): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - xpaths: Optional[List[List[List[int]]]] = None, - node_labels: Optional[Union[List[int], List[List[int]]]] = None, + xpaths: Optional[list[list[list[int]]]] = None, + node_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -701,13 +701,13 @@ class MarkupLMTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - xpaths: Optional[List[List[List[int]]]] = None, - node_labels: Optional[List[List[int]]] = None, + xpaths: Optional[list[list[list[int]]]] = None, + node_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -760,8 +760,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): self, batch_text_or_text_pairs, is_pair: Optional[bool] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[List[int]]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -834,8 +834,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -852,7 +852,7 @@ class MarkupLMTokenizer(PreTrainedTokenizer): return_length: bool = False, verbose: bool = True, **kwargs, - ) -> List[int]: + ) -> list[int]: encoded_inputs = self.encode_plus( text=text, text_pair=text_pair, @@ -883,8 +883,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -907,9 +907,9 @@ class MarkupLMTokenizer(PreTrainedTokenizer): `__call__` should be used instead. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (nodes of a single example) or a list of list of strings (nodes of a batch of examples). """ @@ -951,8 +951,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1006,8 +1006,8 @@ class MarkupLMTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1038,9 +1038,9 @@ class MarkupLMTokenizer(PreTrainedTokenizer): while remaining tokens are labeled with -100, such that they will be ignored by the loss function. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (nodes of a single example) or a list of list of strings (nodes of a batch of examples). """ @@ -1234,33 +1234,33 @@ class MarkupLMTokenizer(PreTrainedTokenizer): def truncate_sequences( self, - ids: List[int], - xpath_tags_seq: List[List[int]], - xpath_subs_seq: List[List[int]], - pair_ids: Optional[List[int]] = None, - pair_xpath_tags_seq: Optional[List[List[int]]] = None, - pair_xpath_subs_seq: Optional[List[List[int]]] = None, - labels: Optional[List[int]] = None, + ids: list[int], + xpath_tags_seq: list[list[int]], + xpath_subs_seq: list[list[int]], + pair_ids: Optional[list[int]] = None, + pair_xpath_tags_seq: Optional[list[list[int]]] = None, + pair_xpath_subs_seq: Optional[list[list[int]]] = None, + labels: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Args: Truncates a sequence pair in-place following the strategy. - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - xpath_tags_seq (`List[List[int]]`): + xpath_tags_seq (`list[list[int]]`): XPath tag IDs of the first sequence. - xpath_subs_seq (`List[List[int]]`): + xpath_subs_seq (`list[list[int]]`): XPath sub IDs of the first sequence. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - pair_xpath_tags_seq (`List[List[int]]`, *optional*): + pair_xpath_tags_seq (`list[list[int]]`, *optional*): XPath tag IDs of the second sequence. - pair_xpath_subs_seq (`List[List[int]]`, *optional*): + pair_xpath_subs_seq (`list[list[int]]`, *optional*): XPath sub IDs of the second sequence. num_tokens_to_remove (`int`, *optional*, defaults to 0): Number of tokens to remove using the truncation strategy. @@ -1283,7 +1283,7 @@ class MarkupLMTokenizer(PreTrainedTokenizer): If set to a positive number, the overflowing tokens returned will contain some tokens from the main sequence returned. The value of this argument defines the number of additional tokens. Returns: - `Tuple[List[int], List[int], List[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of + `tuple[list[int], list[int], list[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of overflowing tokens. Note: The *longest_first* strategy returns empty list of overflowing tokens if a pair of sequences (or a batch of pairs) is provided. """ @@ -1370,7 +1370,7 @@ class MarkupLMTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -1381,7 +1381,7 @@ class MarkupLMTokenizer(PreTrainedTokenizer): Args: Pad encoded inputs (on left/right and up to predefined length or max length in the batch) encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. diff --git a/src/transformers/models/markuplm/tokenization_markuplm_fast.py b/src/transformers/models/markuplm/tokenization_markuplm_fast.py index e9e9a11953f..d66ac53fd26 100644 --- a/src/transformers/models/markuplm/tokenization_markuplm_fast.py +++ b/src/transformers/models/markuplm/tokenization_markuplm_fast.py @@ -19,7 +19,7 @@ and _encode_plus, in which the Rust tokenizer is used. import json from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from tokenizers import processors @@ -268,10 +268,10 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, MARKUPLM_ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - xpaths: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - node_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + xpaths: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + node_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -294,16 +294,16 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): sequences with nodes, xpaths and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - xpaths (`List[List[int]]`, `List[List[List[int]]]`): + xpaths (`list[list[int]]`, `list[list[list[int]]]`): Node-level xpaths. Each bounding box should be normalized to be on a 0-1000 scale. - node_labels (`List[int]`, `List[List[int]]`, *optional*): + node_labels (`list[int]`, `list[list[int]]`, *optional*): Node-level integer labels (for token classification tasks). """ @@ -331,18 +331,18 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = nodes if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "Nodes must be of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Nodes must be of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be nodes if not isinstance(text, (list, tuple)): raise ValueError( - "Nodes must be of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Nodes must be of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -417,13 +417,13 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - xpaths: Optional[List[List[List[int]]]] = None, - node_labels: Optional[Union[List[int], List[List[int]]]] = None, + xpaths: Optional[list[list[list[int]]]] = None, + node_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -474,7 +474,7 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): **kwargs, ) - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: batched_input = [(text, pair)] if pair else [text] encodings = self._tokenizer.encode_batch( batched_input, add_special_tokens=add_special_tokens, is_pretokenized=False, **kwargs @@ -487,8 +487,8 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -511,9 +511,9 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): `__call__` should be used instead. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples). """ @@ -554,13 +554,13 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - xpaths: Optional[List[List[List[int]]]] = None, - node_labels: Optional[List[List[int]]] = None, + xpaths: Optional[list[list[list[int]]]] = None, + node_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -600,8 +600,8 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): ) # Convert encoding to dict - # `Tokens` is a tuple of (List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]], - # List[EncodingFast]) with nested dimensions corresponding to batch, overflows, sequence length + # `Tokens` is a tuple of (list[dict[str, list[list[int]]]] or list[dict[str, 2D-Tensor]], + # list[EncodingFast]) with nested dimensions corresponding to batch, overflows, sequence length tokens_and_encodings = [ self._convert_encoding( encoding=encoding, @@ -714,8 +714,8 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - xpaths: Optional[List[List[int]]] = None, - node_labels: Optional[List[int]] = None, + xpaths: Optional[list[list[int]]] = None, + node_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -780,7 +780,7 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -791,7 +791,7 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): Args: Pad encoded inputs (on left/right and up to predefined length or max length in the batch) encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. @@ -877,8 +877,8 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): return encoded_inputs def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RoBERTa sequence has the following format: @@ -886,12 +886,12 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -900,19 +900,19 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -921,7 +921,7 @@ class MarkupLMTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/mask2former/configuration_mask2former.py b/src/transformers/models/mask2former/configuration_mask2former.py index 37281afeca1..5f9ab6f5e60 100644 --- a/src/transformers/models/mask2former/configuration_mask2former.py +++ b/src/transformers/models/mask2former/configuration_mask2former.py @@ -14,7 +14,7 @@ # limitations under the License. """Mask2Former model configuration""" -from typing import Dict, List, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -105,7 +105,7 @@ class Mask2FormerConfig(PretrainedConfig): use_auxiliary_loss (`boolean``, *optional*, defaults to `True`): If `True` [`Mask2FormerForUniversalSegmentationOutput`] will contain the auxiliary losses computed using the logits from each decoder's stage. - feature_strides (`List[int]`, *optional*, defaults to `[4, 8, 16, 32]`): + feature_strides (`list[int]`, *optional*, defaults to `[4, 8, 16, 32]`): Feature strides corresponding to features generated from backbone network. output_auxiliary_logits (`bool`, *optional*): Should the model output its `auxiliary_logits` or not. @@ -133,7 +133,7 @@ class Mask2FormerConfig(PretrainedConfig): def __init__( self, - backbone_config: Optional[Dict] = None, + backbone_config: Optional[dict] = None, feature_size: int = 256, mask_feature_size: int = 256, hidden_dim: int = 256, @@ -159,12 +159,12 @@ class Mask2FormerConfig(PretrainedConfig): init_std: float = 0.02, init_xavier_std: float = 1.0, use_auxiliary_loss: bool = True, - feature_strides: List[int] = [4, 8, 16, 32], + feature_strides: list[int] = [4, 8, 16, 32], output_auxiliary_logits: Optional[bool] = None, backbone: Optional[str] = None, use_pretrained_backbone: bool = False, use_timm_backbone: bool = False, - backbone_kwargs: Optional[Dict] = None, + backbone_kwargs: Optional[dict] = None, **kwargs, ): if backbone_config is None and backbone is None: diff --git a/src/transformers/models/mask2former/convert_mask2former_original_pytorch_checkpoint_to_pytorch.py b/src/transformers/models/mask2former/convert_mask2former_original_pytorch_checkpoint_to_pytorch.py index b61cf5e52d9..09acc3b97a9 100644 --- a/src/transformers/models/mask2former/convert_mask2former_original_pytorch_checkpoint_to_pytorch.py +++ b/src/transformers/models/mask2former/convert_mask2former_original_pytorch_checkpoint_to_pytorch.py @@ -19,7 +19,7 @@ from collections.abc import Iterator from dataclasses import dataclass from pathlib import Path from pprint import pformat -from typing import Any, Dict, List, Set, Tuple +from typing import Any import requests import torch @@ -45,7 +45,7 @@ from transformers.models.mask2former.modeling_mask2former import ( from transformers.utils import logging -StateDict = Dict[str, Tensor] +StateDict = dict[str, Tensor] logging.set_verbosity_info() logger = logging.get_logger() @@ -54,14 +54,14 @@ torch.manual_seed(0) class TrackedStateDict: - def __init__(self, to_track: Dict): + def __init__(self, to_track: dict): """This class "tracks" a python dictionary by keeping track of which item is accessed. Args: to_track (Dict): The dictionary we wish to track """ self.to_track = to_track - self._seen: Set[str] = set() + self._seen: set[str] = set() def __getitem__(self, key: str) -> Any: return self.to_track[key] @@ -70,16 +70,16 @@ class TrackedStateDict: self._seen.add(key) self.to_track[key] = item - def diff(self) -> List[str]: + def diff(self) -> list[str]: """This method returns a set difference between the keys in the tracked state dict and the one we have access so far. This is an effective method to check if we have update all the keys Returns: - List[str]: List of keys not yet updated + list[str]: List of keys not yet updated """ return set(self.to_track.keys()) - self._seen - def copy(self) -> Dict: + def copy(self) -> dict: # proxy the call to the internal dictionary return self.to_track.copy() @@ -214,7 +214,7 @@ class OriginalMask2FormerCheckpointToOursConverter: self.original_model = original_model self.config = config - def pop_all(self, renamed_keys: List[Tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): + def pop_all(self, renamed_keys: list[tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): for src_key, dst_key in renamed_keys: dst_state_dict[dst_key] = src_state_dict.pop(src_key) @@ -822,8 +822,8 @@ class OriginalMask2FormerCheckpointToOursConverter: return mask2former @staticmethod - def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[Tuple[object, Path, Path]]: - checkpoints: List[Path] = checkpoints_dir.glob("**/*.pkl") + def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[tuple[object, Path, Path]]: + checkpoints: list[Path] = checkpoints_dir.glob("**/*.pkl") for checkpoint in checkpoints: logger.info(f"💪 Converting {checkpoint.stem}") diff --git a/src/transformers/models/mask2former/image_processing_mask2former.py b/src/transformers/models/mask2former/image_processing_mask2former.py index f6977132503..ef4f4701e7e 100644 --- a/src/transformers/models/mask2former/image_processing_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_mask2former.py @@ -16,7 +16,7 @@ import math from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -62,7 +62,7 @@ if is_torch_available(): # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -71,8 +71,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -90,7 +90,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -98,7 +98,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -139,7 +139,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -170,7 +170,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -207,14 +207,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -228,7 +228,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -265,7 +265,7 @@ def compute_segments( # Copied from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_map_to_binary_masks def convert_segmentation_map_to_binary_masks( segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -307,19 +307,19 @@ def convert_segmentation_map_to_binary_masks( # Copied from transformers.models.maskformer.image_processing_maskformer.get_maskformer_resize_output_image_size with maskformer->mask2former def get_mask2former_resize_output_image_size( image: np.ndarray, - size: Union[int, Tuple[int, int], List[int], Tuple[int]], + size: Union[int, tuple[int, int], list[int], tuple[int]], max_size: Optional[int] = None, size_divisor: int = 0, default_to_square: bool = True, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output size given the desired size. Args: image (`np.ndarray`): The input image. - size (`int` or `Tuple[int, int]` or `List[int]` or `Tuple[int]`): + size (`int` or `tuple[int, int]` or `list[int]` or `tuple[int]`): The size of the output image. max_size (`int`, *optional*): The maximum size of the output image. @@ -331,7 +331,7 @@ def get_mask2former_resize_output_image_size( The channel dimension format of the input image. If unset, will use the inferred format from the input. Returns: - `Tuple[int, int]`: The output size. + `tuple[int, int]`: The output size. """ output_size = get_resize_output_image_size( input_image=image, @@ -405,14 +405,14 @@ class Mask2FormerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, num_labels: Optional[int] = None, @@ -441,7 +441,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): self.num_labels = num_labels @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `Mask2FormerImageProcessor.from_pretrained(checkpoint, max_size=800)` @@ -456,7 +456,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): return super().from_dict(image_processor_dict, **kwargs) # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.to_dict - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. This method calls the superclass method and then removes the `_max_size` attribute from the dictionary. @@ -470,7 +470,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], size_divisor: int = 0, resample: PILImageResampling = PILImageResampling.BILINEAR, data_format=None, @@ -484,7 +484,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size of the output image. size_divisor (`int`, *optional*, defaults to 0): If `size_divisor` is given, the output image size will be divisible by the number. @@ -557,7 +557,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): def convert_segmentation_map_to_binary_masks( self, segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -577,14 +577,14 @@ class Mask2FormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_resize: @@ -601,14 +601,14 @@ class Mask2FormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -643,7 +643,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 0, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -682,16 +682,16 @@ class Mask2FormerImageProcessor(BaseImageProcessor): self, images: ImageInput, segmentation_maps: Optional[ImageInput] = None, - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -781,7 +781,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -808,7 +808,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): # Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessor.pad def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -863,9 +863,9 @@ class Mask2FormerImageProcessor(BaseImageProcessor): def encode_inputs( self, - pixel_values_list: List[ImageInput], + pixel_values_list: list[ImageInput], segmentation_maps: ImageInput = None, - instance_id_to_semantic_id: Optional[Union[List[Dict[int, int]], Dict[int, int]]] = None, + instance_id_to_semantic_id: Optional[Union[list[dict[int, int]], dict[int, int]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, return_tensors: Optional[Union[str, TensorType]] = None, @@ -881,7 +881,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): each mask. Args: - pixel_values_list (`List[ImageInput]`): + pixel_values_list (`list[ImageInput]`): List of images (pixel values) to be padded. Each image should be a tensor of shape `(channels, height, width)`. @@ -896,7 +896,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): - 1 for pixels that are real (i.e. **not masked**), - 0 for pixels that are padding (i.e. **masked**). - instance_id_to_semantic_id (`List[Dict[int, int]]` or `Dict[int, int]`, *optional*): + instance_id_to_semantic_id (`list[dict[int, int]]` or `dict[int, int]`, *optional*): A mapping between object instance ids and class ids. If passed, `segmentation_maps` is treated as an instance segmentation map where each pixel represents an instance id. Can be provided as a single dictionary with a global/dataset-level mapping or as a list of dictionaries (one per image), to map @@ -969,7 +969,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): return encoded_inputs def post_process_semantic_segmentation( - self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None + self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None ) -> "torch.Tensor": """ Converts the output of [`Mask2FormerForUniversalSegmentation`] into semantic segmentation maps. Only supports @@ -978,11 +978,11 @@ class Mask2FormerImageProcessor(BaseImageProcessor): Args: outputs ([`Mask2FormerForUniversalSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[tuple[int, int]]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1029,10 +1029,10 @@ class Mask2FormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, return_binary_maps: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`Mask2FormerForUniversalSegmentationOutput`] into instance segmentation predictions. Only supports PyTorch. If instances could overlap, set either return_coco_annotation or return_binary_maps @@ -1048,8 +1048,8 @@ class Mask2FormerImageProcessor(BaseImageProcessor): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. return_coco_annotation (`bool`, *optional*, defaults to `False`): If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. @@ -1057,9 +1057,9 @@ class Mask2FormerImageProcessor(BaseImageProcessor): If set to `True`, segmentation maps are returned as a concatenated tensor of binary segmentation maps (one per detected instance). Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id`, or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`, or a tensor of shape `(num_instances, height, width)` if return_binary_maps is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. @@ -1085,7 +1085,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): num_queries = class_queries_logits.shape[-2] # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(class_queries_logits.shape[0]): mask_pred = masks_queries_logits[i] @@ -1150,9 +1150,9 @@ class Mask2FormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`Mask2FormerForUniversalSegmentationOutput`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1171,13 +1171,13 @@ class Mask2FormerImageProcessor(BaseImageProcessor): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If left to None, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id`, set to `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1210,7 +1210,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/mask2former/modeling_mask2former.py b/src/transformers/models/mask2former/modeling_mask2former.py index 0ff091059ac..b1573c1ced2 100644 --- a/src/transformers/models/mask2former/modeling_mask2former.py +++ b/src/transformers/models/mask2former/modeling_mask2former.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -61,9 +61,9 @@ class Mask2FormerPixelDecoderOutput(ModelOutput): or when `config.output_attentions=True` """ - multi_scale_features: Tuple[torch.FloatTensor] = None + multi_scale_features: tuple[torch.FloatTensor] = None mask_features: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -92,10 +92,10 @@ class Mask2FormerMaskedAttentionDecoderOutput(BaseModelOutputWithCrossAttentions """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None attentions: Optional[torch.FloatTensor] = None - masks_queries_logits: Tuple[torch.FloatTensor] = None - intermediate_hidden_states: Tuple[torch.FloatTensor] = None + masks_queries_logits: tuple[torch.FloatTensor] = None + intermediate_hidden_states: tuple[torch.FloatTensor] = None @dataclass @@ -124,9 +124,9 @@ class Mask2FormerPixelLevelModuleOutput(ModelOutput): """ encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None decoder_last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Tuple[torch.FloatTensor] = None + decoder_hidden_states: tuple[torch.FloatTensor] = None @dataclass @@ -167,12 +167,12 @@ class Mask2FormerModelOutput(ModelOutput): encoder_last_hidden_state: Optional[torch.FloatTensor] = None pixel_decoder_last_hidden_state: Optional[torch.FloatTensor] = None transformer_decoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - transformer_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - transformer_decoder_intermediate_states: Tuple[torch.FloatTensor] = None - masks_queries_logits: Tuple[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + transformer_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + transformer_decoder_intermediate_states: tuple[torch.FloatTensor] = None + masks_queries_logits: tuple[torch.FloatTensor] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -194,7 +194,7 @@ class Mask2FormerForUniversalSegmentationOutput(ModelOutput): masks_queries_logits (`torch.FloatTensor`): A tensor of shape `(batch_size, num_queries, height, width)` representing the proposed masks for each query. - auxiliary_logits (`List[Dict(str, torch.FloatTensor)]`, *optional*): + auxiliary_logits (`list[Dict(str, torch.FloatTensor)]`, *optional*): List of class and mask predictions from each layer of the transformer decoder. encoder_last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Last hidden states (final feature map) of the last stage of the encoder model (backbone). @@ -222,14 +222,14 @@ class Mask2FormerForUniversalSegmentationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None class_queries_logits: Optional[torch.FloatTensor] = None masks_queries_logits: Optional[torch.FloatTensor] = None - auxiliary_logits: Optional[List[Dict[str, torch.FloatTensor]]] = None + auxiliary_logits: Optional[list[dict[str, torch.FloatTensor]]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None pixel_decoder_last_hidden_state: Optional[torch.FloatTensor] = None transformer_decoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None transformer_decoder_hidden_states: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # Adapted from https://github.com/facebookresearch/detectron2/blob/main/projects/PointRend/point_rend/point_features.py @@ -407,7 +407,7 @@ class Mask2FormerHungarianMatcher(nn.Module): class_queries_logits: torch.Tensor, mask_labels: torch.Tensor, class_labels: torch.Tensor, - ) -> List[Tuple[Tensor]]: + ) -> list[tuple[Tensor]]: """ Params: masks_queries_logits (`torch.Tensor`): @@ -421,14 +421,14 @@ class Mask2FormerHungarianMatcher(nn.Module): A tensor of dim `num_target_boxes, height, width` containing the target masks. Returns: - matched_indices (`List[Tuple[Tensor]]`): A list of size batch_size, containing tuples of (index_i, index_j) + matched_indices (`list[tuple[Tensor]]`): A list of size batch_size, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected labels (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes). """ - indices: List[Tuple[np.array]] = [] + indices: list[tuple[np.array]] = [] # iterate through batch size batch_size = masks_queries_logits.shape[0] @@ -462,7 +462,7 @@ class Mask2FormerHungarianMatcher(nn.Module): cost_matrix = torch.maximum(cost_matrix, torch.tensor(-1e10)) cost_matrix = torch.nan_to_num(cost_matrix, 0) # do the assignment using the hungarian algorithm in scipy - assigned_indices: Tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) + assigned_indices: tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) indices.append(assigned_indices) # It could be stacked in one tensor @@ -474,7 +474,7 @@ class Mask2FormerHungarianMatcher(nn.Module): # Adapted from https://github.com/facebookresearch/Mask2Former/blob/main/mask2former/modeling/criterion.py class Mask2FormerLoss(nn.Module): - def __init__(self, config: Mask2FormerConfig, weight_dict: Dict[str, float]): + def __init__(self, config: Mask2FormerConfig, weight_dict: dict[str, float]): """ The Mask2Former Loss. The loss is computed very similar to DETR. The process happens in two steps: 1) we compute hungarian assignment between ground truth masks and the outputs of the model 2) we supervise each pair @@ -483,7 +483,7 @@ class Mask2FormerLoss(nn.Module): Args: config (`Mask2FormerConfig`): The configuration for Mask2Former model also containing loss calculation specific parameters. - weight_dict (`Dict[str, float]`): + weight_dict (`dict[str, float]`): A dictionary of weights to be applied to the different losses. """ super().__init__() @@ -509,7 +509,7 @@ class Mask2FormerLoss(nn.Module): num_points=self.num_points, ) - def _max_by_axis(self, sizes: List[List[int]]) -> List[int]: + def _max_by_axis(self, sizes: list[list[int]]) -> list[int]: maxes = sizes[0] for sublist in sizes[1:]: for index, item in enumerate(sublist): @@ -517,7 +517,7 @@ class Mask2FormerLoss(nn.Module): return maxes # Adapted from nested_tensor_from_tensor_list() in original implementation - def _pad_images_to_max_in_batch(self, tensors: List[Tensor]) -> Tuple[Tensor, Tensor]: + def _pad_images_to_max_in_batch(self, tensors: list[Tensor]) -> tuple[Tensor, Tensor]: # get the maximum size in the batch max_size = self._max_by_axis([list(tensor.shape) for tensor in tensors]) # compute final size @@ -535,20 +535,20 @@ class Mask2FormerLoss(nn.Module): return padded_tensors, padding_masks def loss_labels( - self, class_queries_logits: Tensor, class_labels: List[Tensor], indices: Tuple[np.array] - ) -> Dict[str, Tensor]: + self, class_queries_logits: Tensor, class_labels: list[Tensor], indices: tuple[np.array] + ) -> dict[str, Tensor]: """Compute the losses related to the labels using cross entropy. Args: class_queries_logits (`torch.Tensor`): A tensor of shape `batch_size, num_queries, num_labels` - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. """ pred_logits = class_queries_logits @@ -571,10 +571,10 @@ class Mask2FormerLoss(nn.Module): def loss_masks( self, masks_queries_logits: torch.Tensor, - mask_labels: List[torch.Tensor], - indices: Tuple[np.array], + mask_labels: list[torch.Tensor], + indices: tuple[np.array], num_masks: int, - ) -> Dict[str, torch.Tensor]: + ) -> dict[str, torch.Tensor]: """Compute the losses related to the masks using sigmoid_cross_entropy_loss and dice loss. Args: @@ -582,13 +582,13 @@ class Mask2FormerLoss(nn.Module): A tensor of shape `(batch_size, num_queries, height, width)`. mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. num_masks (`int)`: The number of masks, used for normalization. Returns: - losses (`Dict[str, Tensor]`): A dict of `torch.Tensor` containing two keys: + losses (`dict[str, Tensor]`): A dict of `torch.Tensor` containing two keys: - **loss_mask** -- The loss computed using sigmoid cross entropy loss on the predicted and ground truth. masks. - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth, @@ -718,10 +718,10 @@ class Mask2FormerLoss(nn.Module): self, masks_queries_logits: torch.Tensor, class_queries_logits: torch.Tensor, - mask_labels: List[torch.Tensor], - class_labels: List[torch.Tensor], - auxiliary_predictions: Optional[Dict[str, torch.Tensor]] = None, - ) -> Dict[str, torch.Tensor]: + mask_labels: list[torch.Tensor], + class_labels: list[torch.Tensor], + auxiliary_predictions: Optional[dict[str, torch.Tensor]] = None, + ) -> dict[str, torch.Tensor]: """ This performs the loss computation. @@ -732,14 +732,14 @@ class Mask2FormerLoss(nn.Module): A tensor of shape `(batch_size, num_queries, num_labels)`. mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. - auxiliary_predictions (`Dict[str, torch.Tensor]`, *optional*): + auxiliary_predictions (`dict[str, torch.Tensor]`, *optional*): if `use_auxiliary_loss` was set to `true` in [`Mask2FormerConfig`], then it contains the logits from the inner layers of the Mask2FormerMaskedAttentionDecoder. Returns: - losses (`Dict[str, Tensor]`): A dict of `torch.Tensor` containing three keys: + losses (`dict[str, Tensor]`): A dict of `torch.Tensor` containing three keys: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. - **loss_mask** -- The loss computed using sigmoid cross_entropy loss on the predicted and ground truth masks. @@ -754,7 +754,7 @@ class Mask2FormerLoss(nn.Module): # compute the average number of target masks for normalization purposes num_masks = self.get_num_masks(class_labels, device=class_labels[0].device) # get all the losses - losses: Dict[str, Tensor] = { + losses: dict[str, Tensor] = { **self.loss_masks(masks_queries_logits, mask_labels, indices, num_masks), **self.loss_labels(class_queries_logits, class_labels, indices), } @@ -788,7 +788,7 @@ class Mask2FormerLoss(nn.Module): # Copied from transformers.models.oneformer.modeling_oneformer.multi_scale_deformable_attention def multi_scale_deformable_attention( value: Tensor, - value_spatial_shapes: Union[Tensor, List[Tuple]], + value_spatial_shapes: Union[Tensor, list[tuple]], sampling_locations: Tensor, attention_weights: Tensor, ) -> Tensor: @@ -1444,7 +1444,7 @@ class Mask2FormerAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, key_value_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" hidden_states = hidden_states.permute(1, 0, 2) if hidden_states is not None else None @@ -1792,7 +1792,7 @@ class Mask2FormerMaskedAttentionDecoder(nn.Module): pixel_embeddings: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, query_position_embeddings: Optional[torch.Tensor] = None, - feature_size_list: Optional[List] = None, + feature_size_list: Optional[list] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, @@ -1811,7 +1811,7 @@ class Mask2FormerMaskedAttentionDecoder(nn.Module): encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross(masked)-attention of the decoder. - feature_size_list (`List[torch.Size]`): + feature_size_list (`list[torch.Size]`): This is a list containing shapes (height & width) of multi-scale features from the Pixel Decoder. output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. See `attentions` under @@ -2043,7 +2043,7 @@ class Mask2FormerTransformerModule(nn.Module): def forward( self, - multi_scale_features: List[Tensor], + multi_scale_features: list[Tensor], mask_features: Tensor, output_hidden_states: bool = False, output_attentions: bool = False, @@ -2244,7 +2244,7 @@ class Mask2FormerForUniversalSegmentation(Mask2FormerPreTrainedModel): super().__init__(config) self.model = Mask2FormerModel(config) - self.weight_dict: Dict[str, float] = { + self.weight_dict: dict[str, float] = { "loss_cross_entropy": config.class_weight, "loss_mask": config.mask_weight, "loss_dice": config.dice_weight, @@ -2261,9 +2261,9 @@ class Mask2FormerForUniversalSegmentation(Mask2FormerPreTrainedModel): class_queries_logits: Tensor, mask_labels: Tensor, class_labels: Tensor, - auxiliary_predictions: Dict[str, Tensor], - ) -> Dict[str, Tensor]: - loss_dict: Dict[str, Tensor] = self.criterion( + auxiliary_predictions: dict[str, Tensor], + ) -> dict[str, Tensor]: + loss_dict: dict[str, Tensor] = self.criterion( masks_queries_logits=masks_queries_logits, class_queries_logits=class_queries_logits, mask_labels=mask_labels, @@ -2279,11 +2279,11 @@ class Mask2FormerForUniversalSegmentation(Mask2FormerPreTrainedModel): return loss_dict - def get_loss(self, loss_dict: Dict[str, Tensor]) -> Tensor: + def get_loss(self, loss_dict: dict[str, Tensor]) -> Tensor: return sum(loss_dict.values()) def get_auxiliary_logits(self, classes: torch.Tensor, output_masks: torch.Tensor): - auxiliary_logits: List[Dict[str, Tensor]] = [] + auxiliary_logits: list[dict[str, Tensor]] = [] for aux_binary_masks, aux_classes in zip(output_masks[:-1], classes[:-1]): auxiliary_logits.append({"masks_queries_logits": aux_binary_masks, "class_queries_logits": aux_classes}) @@ -2294,8 +2294,8 @@ class Mask2FormerForUniversalSegmentation(Mask2FormerPreTrainedModel): def forward( self, pixel_values: Tensor, - mask_labels: Optional[List[Tensor]] = None, - class_labels: Optional[List[Tensor]] = None, + mask_labels: Optional[list[Tensor]] = None, + class_labels: Optional[list[Tensor]] = None, pixel_mask: Optional[Tensor] = None, output_hidden_states: Optional[bool] = None, output_auxiliary_logits: Optional[bool] = None, @@ -2303,9 +2303,9 @@ class Mask2FormerForUniversalSegmentation(Mask2FormerPreTrainedModel): return_dict: Optional[bool] = None, ) -> Mask2FormerForUniversalSegmentationOutput: r""" - mask_labels (`List[torch.Tensor]`, *optional*): + mask_labels (`list[torch.Tensor]`, *optional*): List of mask labels of shape `(num_labels, height, width)` to be fed to a model - class_labels (`List[torch.LongTensor]`, *optional*): + class_labels (`list[torch.LongTensor]`, *optional*): list of target class labels of shape `(num_labels, height, width)` to be fed to a model. They identify the labels of `mask_labels`, e.g. the label of `mask_labels[i][j]` if `class_labels[i][j]`. output_auxiliary_logits (`bool`, *optional*): diff --git a/src/transformers/models/maskformer/configuration_maskformer.py b/src/transformers/models/maskformer/configuration_maskformer.py index 8149e028b91..60acff610ff 100644 --- a/src/transformers/models/maskformer/configuration_maskformer.py +++ b/src/transformers/models/maskformer/configuration_maskformer.py @@ -14,7 +14,7 @@ # limitations under the License. """MaskFormer model configuration""" -from typing import Dict, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -113,8 +113,8 @@ class MaskFormerConfig(PretrainedConfig): mask_feature_size: int = 256, no_object_weight: float = 0.1, use_auxiliary_loss: bool = False, - backbone_config: Optional[Dict] = None, - decoder_config: Optional[Dict] = None, + backbone_config: Optional[dict] = None, + decoder_config: Optional[dict] = None, init_std: float = 0.02, init_xavier_std: float = 1.0, dice_weight: float = 1.0, @@ -124,7 +124,7 @@ class MaskFormerConfig(PretrainedConfig): backbone: Optional[str] = None, use_pretrained_backbone: bool = False, use_timm_backbone: bool = False, - backbone_kwargs: Optional[Dict] = None, + backbone_kwargs: Optional[dict] = None, **kwargs, ): if backbone_config is None and backbone is None: diff --git a/src/transformers/models/maskformer/configuration_maskformer_swin.py b/src/transformers/models/maskformer/configuration_maskformer_swin.py index f3010e71d01..84157117bbf 100644 --- a/src/transformers/models/maskformer/configuration_maskformer_swin.py +++ b/src/transformers/models/maskformer/configuration_maskformer_swin.py @@ -42,9 +42,9 @@ class MaskFormerSwinConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embed_dim (`int`, *optional*, defaults to 96): Dimensionality of patch embedding. - depths (`List[int]`, *optional*, defaults to `[2, 2, 6, 2]`): + depths (`list[int]`, *optional*, defaults to `[2, 2, 6, 2]`): Depth of each layer in the Transformer encoder. - num_heads (`List[int]`, *optional*, defaults to `[3, 6, 12, 24]`): + num_heads (`list[int]`, *optional*, defaults to `[3, 6, 12, 24]`): Number of attention heads in each layer of the Transformer encoder. window_size (`int`, *optional*, defaults to 7): Size of windows. @@ -67,12 +67,12 @@ class MaskFormerSwinConfig(BackboneConfigMixin, PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-12): The epsilon used by the layer normalization layers. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/maskformer/convert_maskformer_original_pytorch_checkpoint_to_pytorch.py b/src/transformers/models/maskformer/convert_maskformer_original_pytorch_checkpoint_to_pytorch.py index 8c0a8bd7853..fac17d02203 100644 --- a/src/transformers/models/maskformer/convert_maskformer_original_pytorch_checkpoint_to_pytorch.py +++ b/src/transformers/models/maskformer/convert_maskformer_original_pytorch_checkpoint_to_pytorch.py @@ -18,7 +18,7 @@ from collections.abc import Iterator from dataclasses import dataclass from pathlib import Path from pprint import pformat -from typing import Any, Dict, List, Set, Tuple +from typing import Any import requests import torch @@ -41,7 +41,7 @@ from transformers.models.maskformer.modeling_maskformer import ( from transformers.utils import logging -StateDict = Dict[str, Tensor] +StateDict = dict[str, Tensor] logging.set_verbosity_info() logger = logging.get_logger() @@ -50,14 +50,14 @@ torch.manual_seed(0) class TrackedStateDict: - def __init__(self, to_track: Dict): + def __init__(self, to_track: dict): """This class "tracks" a python dictionary by keeping track of which item is accessed. Args: to_track (Dict): The dictionary we wish to track """ self.to_track = to_track - self._seen: Set[str] = set() + self._seen: set[str] = set() def __getitem__(self, key: str) -> Any: return self.to_track[key] @@ -66,16 +66,16 @@ class TrackedStateDict: self._seen.add(key) self.to_track[key] = item - def diff(self) -> List[str]: + def diff(self) -> list[str]: """This method returns a set difference between the keys in the tracked state dict and the one we have access so far. This is an effective method to check if we have update all the keys Returns: - List[str]: List of keys not yet updated + list[str]: List of keys not yet updated """ return set(self.to_track.keys()) - self._seen - def copy(self) -> Dict: + def copy(self) -> dict: # proxy the call to the internal dictionary return self.to_track.copy() @@ -187,7 +187,7 @@ class OriginalMaskFormerCheckpointToOursConverter: self.original_model = original_model self.config = config - def pop_all(self, renamed_keys: List[Tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): + def pop_all(self, renamed_keys: list[tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): for src_key, dst_key in renamed_keys: dst_state_dict[dst_key] = src_state_dict.pop(src_key) @@ -544,8 +544,8 @@ class OriginalMaskFormerCheckpointToOursConverter: return mask_former @staticmethod - def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[Tuple[object, Path, Path]]: - checkpoints: List[Path] = checkpoints_dir.glob("**/*.pkl") + def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[tuple[object, Path, Path]]: + checkpoints: list[Path] = checkpoints_dir.glob("**/*.pkl") for checkpoint in checkpoints: logger.info(f"💪 Converting {checkpoint.stem}") diff --git a/src/transformers/models/maskformer/image_processing_maskformer.py b/src/transformers/models/maskformer/image_processing_maskformer.py index 63b7d4e4aea..91bdf020f21 100644 --- a/src/transformers/models/maskformer/image_processing_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_maskformer.py @@ -17,7 +17,7 @@ import math import warnings from collections.abc import Iterable -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union +from typing import TYPE_CHECKING, Any, Optional, Union import numpy as np @@ -68,7 +68,7 @@ if is_torch_available(): # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -77,8 +77,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -96,7 +96,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -104,7 +104,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -145,7 +145,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -176,7 +176,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -213,14 +213,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -234,7 +234,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -270,7 +270,7 @@ def compute_segments( # TODO: (Amy) Move to image_transforms def convert_segmentation_map_to_binary_masks( segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -311,19 +311,19 @@ def convert_segmentation_map_to_binary_masks( def get_maskformer_resize_output_image_size( image: np.ndarray, - size: Union[int, Tuple[int, int], List[int], Tuple[int]], + size: Union[int, tuple[int, int], list[int], tuple[int]], max_size: Optional[int] = None, size_divisor: int = 0, default_to_square: bool = True, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output size given the desired size. Args: image (`np.ndarray`): The input image. - size (`int` or `Tuple[int, int]` or `List[int]` or `Tuple[int]`): + size (`int` or `tuple[int, int]` or `list[int]` or `tuple[int]`): The size of the output image. max_size (`int`, *optional*): The maximum size of the output image. @@ -335,7 +335,7 @@ def get_maskformer_resize_output_image_size( The channel dimension format of the input image. If unset, will use the inferred format from the input. Returns: - `Tuple[int, int]`: The output size. + `tuple[int, int]`: The output size. """ output_size = get_resize_output_image_size( input_image=image, @@ -411,14 +411,14 @@ class MaskFormerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, num_labels: Optional[int] = None, @@ -447,7 +447,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): self.num_labels = num_labels @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `MaskFormerImageProcessor.from_pretrained(checkpoint, max_size=800)` @@ -461,7 +461,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): image_processor_dict["do_reduce_labels"] = image_processor_dict.pop("reduce_labels") return super().from_dict(image_processor_dict, **kwargs) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. This method calls the superclass method and then removes the `_max_size` attribute from the dictionary. @@ -474,7 +474,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], size_divisor: int = 0, resample: PILImageResampling = PILImageResampling.BILINEAR, data_format=None, @@ -488,7 +488,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size of the output image. size_divisor (`int`, *optional*, defaults to 0): If `size_divisor` is given, the output image size will be divisible by the number. @@ -560,7 +560,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): def convert_segmentation_map_to_binary_masks( self, segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -580,14 +580,14 @@ class MaskFormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_resize: @@ -604,14 +604,14 @@ class MaskFormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -646,7 +646,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 0, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -685,16 +685,16 @@ class MaskFormerImageProcessor(BaseImageProcessor): self, images: ImageInput, segmentation_maps: Optional[ImageInput] = None, - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -784,7 +784,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -811,7 +811,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): # Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessor.pad def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -866,9 +866,9 @@ class MaskFormerImageProcessor(BaseImageProcessor): def encode_inputs( self, - pixel_values_list: List[ImageInput], + pixel_values_list: list[ImageInput], segmentation_maps: ImageInput = None, - instance_id_to_semantic_id: Optional[Union[List[Dict[int, int]], Dict[int, int]]] = None, + instance_id_to_semantic_id: Optional[Union[list[dict[int, int]], dict[int, int]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, return_tensors: Optional[Union[str, TensorType]] = None, @@ -884,7 +884,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): each mask. Args: - pixel_values_list (`List[ImageInput]`): + pixel_values_list (`list[ImageInput]`): List of images (pixel values) to be padded. Each image should be a tensor of shape `(channels, height, width)`. @@ -899,7 +899,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): - 1 for pixels that are real (i.e. **not masked**), - 0 for pixels that are padding (i.e. **masked**). - instance_id_to_semantic_id (`List[Dict[int, int]]` or `Dict[int, int]`, *optional*): + instance_id_to_semantic_id (`list[dict[int, int]]` or `dict[int, int]`, *optional*): A mapping between object instance ids and class ids. If passed, `segmentation_maps` is treated as an instance segmentation map where each pixel represents an instance id. Can be provided as a single dictionary with a global/dataset-level mapping or as a list of dictionaries (one per image), to map @@ -974,7 +974,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): return encoded_inputs def post_process_segmentation( - self, outputs: "MaskFormerForInstanceSegmentationOutput", target_size: Optional[Tuple[int, int]] = None + self, outputs: "MaskFormerForInstanceSegmentationOutput", target_size: Optional[tuple[int, int]] = None ) -> "torch.Tensor": """ Converts the output of [`MaskFormerForInstanceSegmentationOutput`] into image segmentation predictions. Only @@ -984,7 +984,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): outputs ([`MaskFormerForInstanceSegmentationOutput`]): The outputs from [`MaskFormerForInstanceSegmentation`]. - target_size (`Tuple[int, int]`, *optional*): + target_size (`tuple[int, int]`, *optional*): If set, the `masks_queries_logits` will be resized to `target_size`. Returns: @@ -1022,7 +1022,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): return segmentation def post_process_semantic_segmentation( - self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None + self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None ) -> "torch.Tensor": """ Converts the output of [`MaskFormerForInstanceSegmentation`] into semantic segmentation maps. Only supports @@ -1031,11 +1031,11 @@ class MaskFormerImageProcessor(BaseImageProcessor): Args: outputs ([`MaskFormerForInstanceSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[tuple[int, int]]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1077,10 +1077,10 @@ class MaskFormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, return_binary_maps: Optional[bool] = False, - ) -> List[Dict]: + ) -> list[dict]: """ Converts the output of [`MaskFormerForInstanceSegmentationOutput`] into instance segmentation predictions. Only supports PyTorch. If instances could overlap, set either return_coco_annotation or return_binary_maps @@ -1096,8 +1096,8 @@ class MaskFormerImageProcessor(BaseImageProcessor): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. return_coco_annotation (`bool`, *optional*, defaults to `False`): If set to `True`, segmentation maps are returned in COCO run-length encoding (RLE) format. @@ -1105,9 +1105,9 @@ class MaskFormerImageProcessor(BaseImageProcessor): If set to `True`, segmentation maps are returned as a concatenated tensor of binary segmentation maps (one per detected instance). Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- A tensor of shape `(height, width)` where each pixel represents a `segment_id`, or - `List[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to + `list[List]` run-length encoding (RLE) of the segmentation map if return_coco_annotation is set to `True`, or a tensor of shape `(num_instances, height, width)` if return_binary_maps is set to `True`. Set to `None` if no mask if found above `threshold`. - **segments_info** -- A dictionary that contains additional information on each segment. @@ -1128,7 +1128,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): num_queries = class_queries_logits.shape[-2] # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(class_queries_logits.shape[0]): mask_pred = masks_queries_logits[i] @@ -1193,9 +1193,9 @@ class MaskFormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`MaskFormerForInstanceSegmentationOutput`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1214,13 +1214,13 @@ class MaskFormerImageProcessor(BaseImageProcessor): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If left to None, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id`, set to `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1248,7 +1248,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/maskformer/modeling_maskformer.py b/src/transformers/models/maskformer/modeling_maskformer.py index 6d3742a253f..02f9848a8fb 100644 --- a/src/transformers/models/maskformer/modeling_maskformer.py +++ b/src/transformers/models/maskformer/modeling_maskformer.py @@ -17,7 +17,7 @@ import math from dataclasses import dataclass from numbers import Number -from typing import Dict, List, Optional, Tuple +from typing import Optional import numpy as np import torch @@ -109,8 +109,8 @@ class MaskFormerPixelLevelModuleOutput(ModelOutput): encoder_last_hidden_state: Optional[torch.FloatTensor] = None decoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -133,8 +133,8 @@ class MaskFormerPixelDecoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -173,11 +173,11 @@ class MaskFormerModelOutput(ModelOutput): encoder_last_hidden_state: Optional[torch.FloatTensor] = None pixel_decoder_last_hidden_state: Optional[torch.FloatTensor] = None transformer_decoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - transformer_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + transformer_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -233,11 +233,11 @@ class MaskFormerForInstanceSegmentationOutput(ModelOutput): encoder_last_hidden_state: Optional[torch.FloatTensor] = None pixel_decoder_last_hidden_state: Optional[torch.FloatTensor] = None transformer_decoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - transformer_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + transformer_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None def upsample_like(pixel_values: Tensor, like: Tensor, mode: str = "bilinear") -> Tensor: @@ -444,7 +444,7 @@ class DetrAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, spatial_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder @@ -829,7 +829,7 @@ class MaskFormerHungarianMatcher(nn.Module): self.cost_dice = cost_dice @torch.no_grad() - def forward(self, masks_queries_logits, class_queries_logits, mask_labels, class_labels) -> List[Tuple[Tensor]]: + def forward(self, masks_queries_logits, class_queries_logits, mask_labels, class_labels) -> list[tuple[Tensor]]: """Performs the matching Params: @@ -848,13 +848,13 @@ class MaskFormerHungarianMatcher(nn.Module): masks. Returns: - `List[Tuple[Tensor]]`: A list of size batch_size, containing tuples of (index_i, index_j) where: + `list[tuple[Tensor]]`: A list of size batch_size, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected labels (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_target_boxes). """ - indices: List[Tuple[np.array]] = [] + indices: list[tuple[np.array]] = [] preds_masks = masks_queries_logits preds_probs = class_queries_logits @@ -878,7 +878,7 @@ class MaskFormerHungarianMatcher(nn.Module): # final cost matrix cost_matrix = self.cost_mask * cost_mask + self.cost_class * cost_class + self.cost_dice * cost_dice # do the assignment using the hungarian algorithm in scipy - assigned_indices: Tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) + assigned_indices: tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) indices.append(assigned_indices) # It could be stacked in one tensor @@ -905,7 +905,7 @@ class MaskFormerLoss(nn.Module): self, num_labels: int, matcher: MaskFormerHungarianMatcher, - weight_dict: Dict[str, float], + weight_dict: dict[str, float], eos_coef: float, ): """ @@ -918,7 +918,7 @@ class MaskFormerLoss(nn.Module): The number of classes. matcher (`MaskFormerHungarianMatcher`): A torch module that computes the assignments between the predictions and labels. - weight_dict (`Dict[str, float]`): + weight_dict (`dict[str, float]`): A dictionary of weights to be applied to the different losses. eos_coef (`float`): Weight to apply to the null class. @@ -934,14 +934,14 @@ class MaskFormerLoss(nn.Module): empty_weight[-1] = self.eos_coef self.register_buffer("empty_weight", empty_weight) - def _max_by_axis(self, the_list: List[List[int]]) -> List[int]: + def _max_by_axis(self, the_list: list[list[int]]) -> list[int]: maxes = the_list[0] for sublist in the_list[1:]: for index, item in enumerate(sublist): maxes[index] = max(maxes[index], item) return maxes - def _pad_images_to_max_in_batch(self, tensors: List[Tensor]) -> Tuple[Tensor, Tensor]: + def _pad_images_to_max_in_batch(self, tensors: list[Tensor]) -> tuple[Tensor, Tensor]: # get the maximum size in the batch max_size = self._max_by_axis([list(tensor.shape) for tensor in tensors]) batch_size = len(tensors) @@ -961,20 +961,20 @@ class MaskFormerLoss(nn.Module): return padded_tensors, padding_masks def loss_labels( - self, class_queries_logits: Tensor, class_labels: List[Tensor], indices: Tuple[np.array] - ) -> Dict[str, Tensor]: + self, class_queries_logits: Tensor, class_labels: list[Tensor], indices: tuple[np.array] + ) -> dict[str, Tensor]: """Compute the losses related to the labels using cross entropy. Args: class_queries_logits (`torch.Tensor`): A tensor of shape `batch_size, num_queries, num_labels` - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. """ @@ -996,8 +996,8 @@ class MaskFormerLoss(nn.Module): return losses def loss_masks( - self, masks_queries_logits: Tensor, mask_labels: List[Tensor], indices: Tuple[np.array], num_masks: int - ) -> Dict[str, Tensor]: + self, masks_queries_logits: Tensor, mask_labels: list[Tensor], indices: tuple[np.array], num_masks: int + ) -> dict[str, Tensor]: """Compute the losses related to the masks using focal and dice loss. Args: @@ -1005,13 +1005,13 @@ class MaskFormerLoss(nn.Module): A tensor of shape `batch_size, num_queries, height, width` mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. num_masks (`int)`: The number of masks, used for normalization. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: - **loss_mask** -- The loss computed using sigmoid focal loss on the predicted and ground truth masks. - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth masks. @@ -1053,10 +1053,10 @@ class MaskFormerLoss(nn.Module): self, masks_queries_logits: Tensor, class_queries_logits: Tensor, - mask_labels: List[Tensor], - class_labels: List[Tensor], - auxiliary_predictions: Optional[Dict[str, Tensor]] = None, - ) -> Dict[str, Tensor]: + mask_labels: list[Tensor], + class_labels: list[Tensor], + auxiliary_predictions: Optional[dict[str, Tensor]] = None, + ) -> dict[str, Tensor]: """ This performs the loss computation. @@ -1067,14 +1067,14 @@ class MaskFormerLoss(nn.Module): A tensor of shape `batch_size, num_queries, num_labels` mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. - auxiliary_predictions (`Dict[str, torch.Tensor]`, *optional*): + auxiliary_predictions (`dict[str, torch.Tensor]`, *optional*): if `use_auxiliary_loss` was set to `true` in [`MaskFormerConfig`], then it contains the logits from the inner layers of the Detr's Decoder. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. - **loss_mask** -- The loss computed using sigmoid focal loss on the predicted and ground truth masks. - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth @@ -1088,7 +1088,7 @@ class MaskFormerLoss(nn.Module): # compute the average number of target masks for normalization purposes num_masks: Number = self.get_num_masks(class_labels, device=class_labels[0].device) # get all the losses - losses: Dict[str, Tensor] = { + losses: dict[str, Tensor] = { **self.loss_masks(masks_queries_logits, mask_labels, indices, num_masks), **self.loss_labels(class_queries_logits, class_labels, indices), } @@ -1181,7 +1181,7 @@ class MaskFormerFPNLayer(nn.Module): class MaskFormerFPNModel(nn.Module): - def __init__(self, in_features: int, lateral_widths: List[int], feature_size: int = 256): + def __init__(self, in_features: int, lateral_widths: list[int], feature_size: int = 256): """ Feature Pyramid Network, given an input tensor and a set of feature map of different feature/spatial size, it creates a list of feature maps with the same feature size. @@ -1189,7 +1189,7 @@ class MaskFormerFPNModel(nn.Module): Args: in_features (`int`): The number of input features (channels). - lateral_widths (`List[int]`): + lateral_widths (`list[int]`): A list with the features (channels) size of each lateral connection. feature_size (int, *optional*, defaults to 256): The features (channels) of the resulting feature maps. @@ -1200,7 +1200,7 @@ class MaskFormerFPNModel(nn.Module): *[MaskFormerFPNLayer(feature_size, lateral_width) for lateral_width in lateral_widths[::-1]] ) - def forward(self, features: List[Tensor]) -> List[Tensor]: + def forward(self, features: list[Tensor]) -> list[Tensor]: fpn_features = [] last_feature = features[-1] other_features = features[:-1] @@ -1230,7 +1230,7 @@ class MaskFormerPixelDecoder(nn.Module): self.mask_projection = nn.Conv2d(feature_size, mask_feature_size, kernel_size=3, padding=1) def forward( - self, features: List[Tensor], output_hidden_states: bool = False, return_dict: bool = True + self, features: list[Tensor], output_hidden_states: bool = False, return_dict: bool = True ) -> MaskFormerPixelDecoderOutput: fpn_features = self.fpn(features) # we use the last feature map @@ -1597,7 +1597,7 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): cost_class=1.0, cost_dice=config.dice_weight, cost_mask=config.mask_weight ) - self.weight_dict: Dict[str, float] = { + self.weight_dict: dict[str, float] = { "loss_cross_entropy": config.cross_entropy_weight, "loss_mask": config.mask_weight, "loss_dice": config.dice_weight, @@ -1618,9 +1618,9 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): class_queries_logits: Tensor, mask_labels: Tensor, class_labels: Tensor, - auxiliary_logits: Dict[str, Tensor], - ) -> Dict[str, Tensor]: - loss_dict: Dict[str, Tensor] = self.criterion( + auxiliary_logits: dict[str, Tensor], + ) -> dict[str, Tensor]: + loss_dict: dict[str, Tensor] = self.criterion( masks_queries_logits, class_queries_logits, mask_labels, class_labels, auxiliary_logits ) # weight each loss by `self.weight_dict[]` including auxiliary losses @@ -1631,13 +1631,13 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): return loss_dict - def get_loss(self, loss_dict: Dict[str, Tensor]) -> Tensor: + def get_loss(self, loss_dict: dict[str, Tensor]) -> Tensor: return sum(loss_dict.values()) - def get_logits(self, outputs: MaskFormerModelOutput) -> Tuple[Tensor, Tensor, Dict[str, Tensor]]: + def get_logits(self, outputs: MaskFormerModelOutput) -> tuple[Tensor, Tensor, dict[str, Tensor]]: pixel_embeddings = outputs.pixel_decoder_last_hidden_state # get the auxiliary predictions (one for each decoder's layer) - auxiliary_logits: List[str, Tensor] = [] + auxiliary_logits: list[str, Tensor] = [] # This code is a little bit cumbersome, an improvement can be to return a list of predictions. If we have auxiliary loss then we are going to return more than one element in the list if self.config.use_auxiliary_loss: @@ -1670,8 +1670,8 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): def forward( self, pixel_values: Tensor, - mask_labels: Optional[List[Tensor]] = None, - class_labels: Optional[List[Tensor]] = None, + mask_labels: Optional[list[Tensor]] = None, + class_labels: Optional[list[Tensor]] = None, pixel_mask: Optional[Tensor] = None, output_auxiliary_logits: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1679,9 +1679,9 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): return_dict: Optional[bool] = None, ) -> MaskFormerForInstanceSegmentationOutput: r""" - mask_labels (`List[torch.Tensor]`, *optional*): + mask_labels (`list[torch.Tensor]`, *optional*): List of mask labels of shape `(num_labels, height, width)` to be fed to a model - class_labels (`List[torch.LongTensor]`, *optional*): + class_labels (`list[torch.LongTensor]`, *optional*): list of target class labels of shape `(num_labels, height, width)` to be fed to a model. They identify the labels of `mask_labels`, e.g. the label of `mask_labels[i][j]` if `class_labels[i][j]`. output_auxiliary_logits (`bool`, *optional*): @@ -1784,7 +1784,7 @@ class MaskFormerForInstanceSegmentation(MaskFormerPreTrainedModel): class_queries_logits, masks_queries_logits, auxiliary_logits = self.get_logits(outputs) if mask_labels is not None and class_labels is not None: - loss_dict: Dict[str, Tensor] = self.get_loss_dict( + loss_dict: dict[str, Tensor] = self.get_loss_dict( masks_queries_logits, class_queries_logits, mask_labels, class_labels, auxiliary_logits ) loss = self.get_loss(loss_dict) diff --git a/src/transformers/models/maskformer/modeling_maskformer_swin.py b/src/transformers/models/maskformer/modeling_maskformer_swin.py index 3cb72c6c846..f4845424850 100644 --- a/src/transformers/models/maskformer/modeling_maskformer_swin.py +++ b/src/transformers/models/maskformer/modeling_maskformer_swin.py @@ -19,7 +19,7 @@ states before downsampling, which is different from the default Swin Transformer import collections.abc import math from dataclasses import dataclass -from typing import Optional, Tuple +from typing import Optional import torch from torch import Tensor, nn @@ -63,9 +63,9 @@ class MaskFormerSwinModelOutputWithPooling(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_spatial_dimensions: Tuple[Tuple[int, int]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_spatial_dimensions: tuple[tuple[int, int]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -94,9 +94,9 @@ class MaskFormerSwinBaseModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_spatial_dimensions: Tuple[Tuple[int, int]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_spatial_dimensions: tuple[tuple[int, int]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.swin.modeling_swin.window_partition @@ -254,7 +254,7 @@ class MaskFormerSwinPatchEmbeddings(nn.Module): pixel_values = nn.functional.pad(pixel_values, pad_values) return pixel_values - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: _, num_channels, height, width = pixel_values.shape # pad the input to be divisible by self.patch_size, if needed pixel_values = self.maybe_pad(pixel_values, height, width) @@ -272,7 +272,7 @@ class MaskFormerSwinPatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -280,7 +280,7 @@ class MaskFormerSwinPatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -295,7 +295,7 @@ class MaskFormerSwinPatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -386,7 +386,7 @@ class MaskFormerSwinSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -483,7 +483,7 @@ class MaskFormerSwinAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -929,7 +929,7 @@ class MaskFormerSwinBackbone(MaskFormerSwinPreTrainedModel, BackboneMixin): # we need to reshape the hidden states to their original spatial dimensions # spatial dimensions contains all the heights and widths of each stage, including after the embeddings - spatial_dimensions: Tuple[Tuple[int, int]] = outputs.hidden_states_spatial_dimensions + spatial_dimensions: tuple[tuple[int, int]] = outputs.hidden_states_spatial_dimensions feature_maps = () for i, (hidden_state, stage, (height, width)) in enumerate( zip(hidden_states, self.stage_names[1:], spatial_dimensions) diff --git a/src/transformers/models/mbart/modeling_flax_mbart.py b/src/transformers/models/mbart/modeling_flax_mbart.py index f369e39e492..ad269860a95 100644 --- a/src/transformers/models/mbart/modeling_flax_mbart.py +++ b/src/transformers/models/mbart/modeling_flax_mbart.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -203,7 +203,7 @@ MBART_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -321,7 +321,7 @@ class FlaxMBartAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -441,7 +441,7 @@ class FlaxMBartEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -565,7 +565,7 @@ class FlaxMBartDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) @@ -947,7 +947,7 @@ class FlaxMBartPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: MBartConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -956,7 +956,7 @@ class FlaxMBartPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") # make sure initialization pass will work for FlaxMBartForSequenceClassificationModule diff --git a/src/transformers/models/mbart/modeling_mbart.py b/src/transformers/models/mbart/modeling_mbart.py index 785f7511425..18ad34026f4 100755 --- a/src/transformers/models/mbart/modeling_mbart.py +++ b/src/transformers/models/mbart/modeling_mbart.py @@ -16,7 +16,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -207,7 +207,7 @@ class MBartAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -774,7 +774,7 @@ class MBartEncoder(MBartPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -951,14 +951,14 @@ class MBartDecoder(MBartPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1241,8 +1241,8 @@ class MBartModel(MBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1250,7 +1250,7 @@ class MBartModel(MBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Seq2SeqModelOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqModelOutput, tuple[torch.FloatTensor]]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1397,8 +1397,8 @@ class MBartForConditionalGeneration(MBartPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1407,7 +1407,7 @@ class MBartForConditionalGeneration(MBartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1576,7 +1576,7 @@ class MBartForSequenceClassification(MBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1585,7 +1585,7 @@ class MBartForSequenceClassification(MBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1722,7 +1722,7 @@ class MBartForQuestionAnswering(MBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1732,7 +1732,7 @@ class MBartForQuestionAnswering(MBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple, Seq2SeqQuestionAnsweringModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1887,7 +1887,7 @@ class MBartForCausalLM(MBartPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1895,7 +1895,7 @@ class MBartForCausalLM(MBartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/mbart/modeling_tf_mbart.py b/src/transformers/models/mbart/modeling_tf_mbart.py index 54c90bf011b..44ac6562a86 100644 --- a/src/transformers/models/mbart/modeling_tf_mbart.py +++ b/src/transformers/models/mbart/modeling_tf_mbart.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -182,11 +182,11 @@ class TFMBartAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -428,9 +428,9 @@ class TFMBartDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)* @@ -631,7 +631,7 @@ MBART_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -750,7 +750,7 @@ class TFMBartEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: """ Args: input_ids (`tf.Tensor` of shape `(batch_size, sequence_length)`): @@ -927,14 +927,14 @@ class TFMBartDecoder(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, ) -> Union[ - TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor] + TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor, tf.Tensor] ]: r""" Args: @@ -979,7 +979,7 @@ class TFMBartDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1169,8 +1169,8 @@ class TFMBartMainLayer(keras.layers.Layer): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1293,8 +1293,8 @@ class TFMBartModel(TFMBartPreTrainedModel): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: tuple[tuple[tf.Tensor]] | None = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1303,7 +1303,7 @@ class TFMBartModel(TFMBartPreTrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFSeq2SeqModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqModelOutput, tuple[tf.Tensor]]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1430,7 +1430,7 @@ class TFMBartForConditionalGeneration(TFMBartPreTrainedModel, TFCausalLanguageMo decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, encoder_outputs: Optional[TFBaseModelOutput] = None, - past_key_values: Optional[Tuple[Tuple[tf.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[tf.Tensor]]] = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1439,7 +1439,7 @@ class TFMBartForConditionalGeneration(TFMBartPreTrainedModel, TFCausalLanguageMo return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSeq2SeqLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqLMOutput, tuple[tf.Tensor]]: """ labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/mbart/tokenization_mbart.py b/src/transformers/models/mbart/tokenization_mbart.py index b6c55b38331..852357c9a26 100644 --- a/src/transformers/models/mbart/tokenization_mbart.py +++ b/src/transformers/models/mbart/tokenization_mbart.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -59,8 +59,8 @@ class MBartTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -75,7 +75,7 @@ class MBartTokenizer(PreTrainedTokenizer): tokenizer_file=None, src_lang=None, tgt_lang=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, additional_special_tokens=None, **kwargs, ): @@ -170,22 +170,22 @@ class MBartTokenizer(PreTrainedTokenizer): self.set_src_lang_special_tokens(self._src_lang) def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -200,8 +200,8 @@ class MBartTokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An MBART sequence has the following format, where `X` represents the sequence: @@ -213,13 +213,13 @@ class MBartTokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -227,20 +227,20 @@ class MBartTokenizer(PreTrainedTokenizer): return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. mBART does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -268,7 +268,7 @@ class MBartTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -291,7 +291,7 @@ class MBartTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -310,9 +310,9 @@ class MBartTokenizer(PreTrainedTokenizer): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en_XX", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "ro_RO", **kwargs, ) -> BatchEncoding: diff --git a/src/transformers/models/mbart/tokenization_mbart_fast.py b/src/transformers/models/mbart/tokenization_mbart_fast.py index 1996a9ecd2e..7cf4d468c7d 100644 --- a/src/transformers/models/mbart/tokenization_mbart_fast.py +++ b/src/transformers/models/mbart/tokenization_mbart_fast.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -67,8 +67,8 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): model_input_names = ["input_ids", "attention_mask"] slow_tokenizer_class = MBartTokenizer - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -133,8 +133,8 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): self.set_src_lang_special_tokens(self._src_lang) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. The special tokens depend on calling set_lang. @@ -148,13 +148,13 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -162,20 +162,20 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. mBART does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -200,9 +200,9 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en_XX", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "ro_RO", **kwargs, ) -> BatchEncoding: @@ -246,7 +246,7 @@ class MBartTokenizerFast(PreTrainedTokenizerFast): special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str, self.prefix_tokens + self.suffix_tokens)), ) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/mbart50/tokenization_mbart50.py b/src/transformers/models/mbart50/tokenization_mbart50.py index a212012463b..413beaa03a8 100644 --- a/src/transformers/models/mbart50/tokenization_mbart50.py +++ b/src/transformers/models/mbart50/tokenization_mbart50.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -97,8 +97,8 @@ class MBart50Tokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -111,7 +111,7 @@ class MBart50Tokenizer(PreTrainedTokenizer): unk_token="", pad_token="", mask_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -181,12 +181,12 @@ class MBart50Tokenizer(PreTrainedTokenizer): self._src_lang = new_src_lang self.set_src_lang_special_tokens(self._src_lang) - def __getstate__(self) -> Dict: + def __getstate__(self) -> dict: state = self.__dict__.copy() state["sp_model"] = None return state - def __setstate__(self, d: Dict) -> None: + def __setstate__(self, d: dict) -> None: self.__dict__ = d # for backward compatibility @@ -196,12 +196,12 @@ class MBart50Tokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token: str) -> int: @@ -239,7 +239,7 @@ class MBart50Tokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string.strip() - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -257,22 +257,22 @@ class MBart50Tokenizer(PreTrainedTokenizer): return (out_vocab_file,) def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -287,8 +287,8 @@ class MBart50Tokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An MBART-50 sequence has the following format, where `X` represents the sequence: @@ -300,13 +300,13 @@ class MBart50Tokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -327,9 +327,9 @@ class MBart50Tokenizer(PreTrainedTokenizer): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en_XX", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "ro_RO", **kwargs, ) -> BatchEncoding: diff --git a/src/transformers/models/mbart50/tokenization_mbart50_fast.py b/src/transformers/models/mbart50/tokenization_mbart50_fast.py index 01980a2a822..985b0929f87 100644 --- a/src/transformers/models/mbart50/tokenization_mbart50_fast.py +++ b/src/transformers/models/mbart50/tokenization_mbart50_fast.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -87,8 +87,8 @@ class MBart50TokenizerFast(PreTrainedTokenizerFast): model_input_names = ["input_ids", "attention_mask"] slow_tokenizer_class = MBart50Tokenizer - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -147,8 +147,8 @@ class MBart50TokenizerFast(PreTrainedTokenizerFast): self.set_src_lang_special_tokens(self._src_lang) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. The special tokens depend on calling set_lang. @@ -162,13 +162,13 @@ class MBart50TokenizerFast(PreTrainedTokenizerFast): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -177,9 +177,9 @@ class MBart50TokenizerFast(PreTrainedTokenizerFast): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en_XX", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "ro_RO", **kwargs, ) -> BatchEncoding: @@ -235,7 +235,7 @@ class MBart50TokenizerFast(PreTrainedTokenizerFast): inputs["forced_bos_token_id"] = tgt_lang_id return inputs - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/megatron_bert/modeling_megatron_bert.py b/src/transformers/models/megatron_bert/modeling_megatron_bert.py index b195b2841c7..abbda408588 100755 --- a/src/transformers/models/megatron_bert/modeling_megatron_bert.py +++ b/src/transformers/models/megatron_bert/modeling_megatron_bert.py @@ -19,7 +19,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -215,9 +215,9 @@ class MegatronBertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -357,9 +357,9 @@ class MegatronBertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: ln_outputs = self.ln(hidden_states) self_outputs = self.self( ln_outputs, @@ -428,9 +428,9 @@ class MegatronBertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -511,12 +511,12 @@ class MegatronBertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( @@ -741,8 +741,8 @@ class MegatronBertForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -800,12 +800,12 @@ class MegatronBertModel(MegatronBertPreTrainedModel): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -938,7 +938,7 @@ class MegatronBertForPreTraining(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MegatronBertForPreTrainingOutput]: + ) -> Union[tuple, MegatronBertForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1042,13 +1042,13 @@ class MegatronBertForCausalLM(MegatronBertPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1164,7 +1164,7 @@ class MegatronBertForMaskedLM(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1252,7 +1252,7 @@ class MegatronBertForNextSentencePrediction(MegatronBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, NextSentencePredictorOutput]: + ) -> Union[tuple, NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -1353,7 +1353,7 @@ class MegatronBertForSequenceClassification(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1438,7 +1438,7 @@ class MegatronBertForMultipleChoice(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1543,7 +1543,7 @@ class MegatronBertForTokenClassification(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1610,7 +1610,7 @@ class MegatronBertForQuestionAnswering(MegatronBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.bert( diff --git a/src/transformers/models/mgp_str/configuration_mgp_str.py b/src/transformers/models/mgp_str/configuration_mgp_str.py index c98dd783751..1f6b355c771 100644 --- a/src/transformers/models/mgp_str/configuration_mgp_str.py +++ b/src/transformers/models/mgp_str/configuration_mgp_str.py @@ -32,7 +32,7 @@ class MgpstrConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - image_size (`List[int]`, *optional*, defaults to `[32, 128]`): + image_size (`list[int]`, *optional*, defaults to `[32, 128]`): The size (resolution) of each image. patch_size (`int`, *optional*, defaults to 4): The size (resolution) of each patch. diff --git a/src/transformers/models/mgp_str/modeling_mgp_str.py b/src/transformers/models/mgp_str/modeling_mgp_str.py index 5d19456c3c8..29e4d7981f8 100644 --- a/src/transformers/models/mgp_str/modeling_mgp_str.py +++ b/src/transformers/models/mgp_str/modeling_mgp_str.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -100,10 +100,10 @@ class MgpstrModelOutput(ModelOutput): heads. """ - logits: Tuple[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - a3_attentions: Optional[Tuple[torch.FloatTensor]] = None + logits: tuple[torch.FloatTensor] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + a3_attentions: Optional[tuple[torch.FloatTensor]] = None class MgpstrEmbeddings(nn.Module): @@ -331,7 +331,7 @@ class MgpstrModel(MgpstrPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -391,7 +391,7 @@ class MgpstrForSceneTextRecognition(MgpstrPreTrainedModel): output_a3_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], MgpstrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], MgpstrModelOutput]: r""" output_a3_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of a3 modules. See `a3_attentions` under returned tensors diff --git a/src/transformers/models/mgp_str/processing_mgp_str.py b/src/transformers/models/mgp_str/processing_mgp_str.py index 9e68db0c816..c224e0fecc4 100644 --- a/src/transformers/models/mgp_str/processing_mgp_str.py +++ b/src/transformers/models/mgp_str/processing_mgp_str.py @@ -110,11 +110,11 @@ class MgpstrProcessor(ProcessorMixin): List of tokenized input ids. Returns: - `Dict[str, any]`: Dictionary of all the outputs of the decoded results. - generated_text (`List[str]`): The final results after fusion of char, bpe, and wp. scores - (`List[float]`): The final scores after fusion of char, bpe, and wp. char_preds (`List[str]`): The list - of character decoded sentences. bpe_preds (`List[str]`): The list of bpe decoded sentences. wp_preds - (`List[str]`): The list of wp decoded sentences. + `dict[str, any]`: Dictionary of all the outputs of the decoded results. + generated_text (`list[str]`): The final results after fusion of char, bpe, and wp. scores + (`list[float]`): The final scores after fusion of char, bpe, and wp. char_preds (`list[str]`): The list + of character decoded sentences. bpe_preds (`list[str]`): The list of bpe decoded sentences. wp_preds + (`list[str]`): The list of wp decoded sentences. This method forwards all its arguments to PreTrainedTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please refer to the docstring of this method for more information. @@ -154,7 +154,7 @@ class MgpstrProcessor(ProcessorMixin): Type of model prediction. Must be one of ['char', 'bpe', 'wp']. Returns: `tuple`: - dec_strs(`str`): The decode strings of model prediction. conf_scores(`List[float]`): The confidence + dec_strs(`str`): The decode strings of model prediction. conf_scores(`list[float]`): The confidence score of model prediction. """ if format == DecodeType.CHARACTER: @@ -201,7 +201,7 @@ class MgpstrProcessor(ProcessorMixin): sequences (`torch.Tensor`): List of tokenized input ids. Returns: - `List[str]`: The list of char decoded sentences. + `list[str]`: The list of char decoded sentences. """ decode_strs = [seq.replace(" ", "") for seq in self.char_tokenizer.batch_decode(sequences)] return decode_strs @@ -214,7 +214,7 @@ class MgpstrProcessor(ProcessorMixin): sequences (`torch.Tensor`): List of tokenized input ids. Returns: - `List[str]`: The list of bpe decoded sentences. + `list[str]`: The list of bpe decoded sentences. """ return self.bpe_tokenizer.batch_decode(sequences) @@ -226,7 +226,7 @@ class MgpstrProcessor(ProcessorMixin): sequences (`torch.Tensor`): List of tokenized input ids. Returns: - `List[str]`: The list of wp decoded sentences. + `list[str]`: The list of wp decoded sentences. """ decode_strs = [seq.replace(" ", "") for seq in self.wp_tokenizer.batch_decode(sequences)] return decode_strs diff --git a/src/transformers/models/mgp_str/tokenization_mgp_str.py b/src/transformers/models/mgp_str/tokenization_mgp_str.py index 1c224d142f7..2df2dab7796 100644 --- a/src/transformers/models/mgp_str/tokenization_mgp_str.py +++ b/src/transformers/models/mgp_str/tokenization_mgp_str.py @@ -16,7 +16,7 @@ import json import os -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -87,7 +87,7 @@ class MgpstrTokenizer(PreTrainedTokenizer): """Converts an index (integer) in a token (str) using the vocab.""" return self.decoder.get(index) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) return diff --git a/src/transformers/models/mimi/modeling_mimi.py b/src/transformers/models/mimi/modeling_mimi.py index 451e9c790a9..9023c93433a 100644 --- a/src/transformers/models/mimi/modeling_mimi.py +++ b/src/transformers/models/mimi/modeling_mimi.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -73,8 +73,8 @@ class MimiOutput(ModelOutput): audio_codes: Optional[torch.LongTensor] = None audio_values: Optional[torch.FloatTensor] = None - encoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None - decoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None + encoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None + decoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None @dataclass @@ -94,7 +94,7 @@ class MimiEncoderOutput(ModelOutput): """ audio_codes: Optional[torch.LongTensor] = None - encoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None + encoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None @dataclass @@ -114,7 +114,7 @@ class MimiDecoderOutput(ModelOutput): """ audio_values: Optional[torch.FloatTensor] = None - decoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None + decoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None class MimiConv1d(nn.Module): @@ -187,7 +187,7 @@ class MimiConv1d(nn.Module): @staticmethod # Copied from transformers.models.encodec.modeling_encodec.EncodecConv1d._pad1d - def _pad1d(hidden_states: torch.Tensor, paddings: Tuple[int, int], mode: str = "zero", value: float = 0.0): + def _pad1d(hidden_states: torch.Tensor, paddings: tuple[int, int], mode: str = "zero", value: float = 0.0): """Tiny wrapper around torch.nn.functional.pad, just to allow for reflect padding on small input. If this is the case, we insert extra 0 padding to the right before the reflection happens. """ @@ -310,7 +310,7 @@ class MimiResnetBlock(nn.Module): Residual block from SEANet model as used by Mimi. """ - def __init__(self, config: MimiConfig, dim: int, dilations: List[int]): + def __init__(self, config: MimiConfig, dim: int, dilations: list[int]): super().__init__() kernel_sizes = (config.residual_kernel_size, 1) if len(kernel_sizes) != len(dilations): @@ -540,7 +540,7 @@ class MimiAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -616,7 +616,7 @@ class MimiFlashAttention2(MimiAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -722,7 +722,7 @@ class MimiSdpaAttention(MimiAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -822,7 +822,7 @@ class MimiTransformerLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -900,13 +900,13 @@ class MimiTransformerModel(nn.Module): hidden_states: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: """ Args: hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): @@ -1517,9 +1517,9 @@ class MimiModel(MimiPreTrainedModel): input_values: torch.Tensor, num_quantizers: int, padding_mask: int, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, return_dict: Optional[bool] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """ Encodes the given input using the underlying VQVAE. The padding mask is required to compute the correct scale. """ @@ -1575,9 +1575,9 @@ class MimiModel(MimiPreTrainedModel): input_values: torch.Tensor, padding_mask: Optional[torch.Tensor] = None, num_quantizers: Optional[float] = None, - encoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + encoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, Optional[torch.Tensor]], MimiEncoderOutput]: + ) -> Union[tuple[torch.Tensor, Optional[torch.Tensor]], MimiEncoderOutput]: """ Encodes the input audio waveform into discrete codes. @@ -1639,7 +1639,7 @@ class MimiModel(MimiPreTrainedModel): def _decode_frame( self, codes: torch.Tensor, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, return_dict: Optional[bool] = None, ) -> torch.Tensor: embeddings = self.quantizer.decode(codes) @@ -1660,9 +1660,9 @@ class MimiModel(MimiPreTrainedModel): self, audio_codes: torch.Tensor, padding_mask: Optional[torch.Tensor] = None, - decoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + decoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], MimiDecoderOutput]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], MimiDecoderOutput]: """ Decodes the given frames into an output audio waveform. @@ -1711,10 +1711,10 @@ class MimiModel(MimiPreTrainedModel): padding_mask: Optional[torch.Tensor] = None, num_quantizers: Optional[int] = None, audio_codes: Optional[torch.Tensor] = None, - encoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, - decoder_past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + encoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, + decoder_past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], MimiOutput]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], MimiOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, channels, sequence_length)`, *optional*): Raw audio input converted to Float. diff --git a/src/transformers/models/minimax/modeling_minimax.py b/src/transformers/models/minimax/modeling_minimax.py index 5ac3f98f1d2..1bf968e0361 100644 --- a/src/transformers/models/minimax/modeling_minimax.py +++ b/src/transformers/models/minimax/modeling_minimax.py @@ -20,7 +20,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -74,7 +74,7 @@ class MiniMaxRMSNorm(nn.Module): class MiniMaxCache(DynamicCache): def __init__(self): super().__init__() - self.linear_cache: List[torch.Tensor] = [] + self.linear_cache: list[torch.Tensor] = [] def set_linear_cache(self, layer_idx, linear_cache): # There may be skipped layers, fill them with empty lists @@ -170,12 +170,12 @@ class MiniMaxLightningAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: batch_size, seq_len, hidden_size = hidden_states.shape num_blocks = (seq_len + self.block_size - 1) // self.block_size @@ -358,12 +358,12 @@ class MiniMaxAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -513,20 +513,20 @@ class MiniMaxDecoderLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. attention_mask (`torch.Tensor`, *optional*): attention mask of size @@ -676,7 +676,7 @@ class MiniMaxModel(MiniMaxPreTrainedModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -791,7 +791,7 @@ class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ... def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -915,7 +915,7 @@ class MiniMaxForCausalLM(MiniMaxPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1200,7 +1200,7 @@ class MiniMaxForQuestionAnswering(MiniMaxPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/minimax/modular_minimax.py b/src/transformers/models/minimax/modular_minimax.py index e0d3ef892cc..9b6fc12ae3d 100644 --- a/src/transformers/models/minimax/modular_minimax.py +++ b/src/transformers/models/minimax/modular_minimax.py @@ -15,7 +15,7 @@ # limitations under the License. """PyTorch MiniMax model.""" -from typing import List, Optional, Tuple +from typing import Optional import torch import torch.nn.functional as F @@ -185,7 +185,7 @@ class MiniMaxRMSNorm(MixtralRMSNorm): class MiniMaxCache(DynamicCache): def __init__(self): super().__init__() - self.linear_cache: List[torch.Tensor] = [] + self.linear_cache: list[torch.Tensor] = [] def set_linear_cache(self, layer_idx, linear_cache): # There may be skipped layers, fill them with empty lists @@ -281,12 +281,12 @@ class MiniMaxLightningAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: batch_size, seq_len, hidden_size = hidden_states.shape num_blocks = (seq_len + self.block_size - 1) // self.block_size @@ -402,20 +402,20 @@ class MiniMaxDecoderLayer(MixtralDecoderLayer, GradientCheckpointingLayer): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. attention_mask (`torch.Tensor`, *optional*): attention mask of size @@ -483,7 +483,7 @@ class MiniMaxModel(MixtralModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, diff --git a/src/transformers/models/mistral/modeling_flax_mistral.py b/src/transformers/models/mistral/modeling_flax_mistral.py index 6785a7b46a0..5b34a514416 100644 --- a/src/transformers/models/mistral/modeling_flax_mistral.py +++ b/src/transformers/models/mistral/modeling_flax_mistral.py @@ -14,7 +14,7 @@ # limitations under the License. """Flax Mistral model.""" -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -113,7 +113,7 @@ MISTRAL_INPUTS_DOCSTRING = r""" config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids) - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -290,7 +290,7 @@ class FlaxMistralAttention(nn.Module): deterministic: bool = True, output_attentions: bool = False, init_cache: bool = False, - ) -> Tuple[jnp.ndarray, jnp.ndarray]: + ) -> tuple[jnp.ndarray, jnp.ndarray]: query_states = self.q_proj(hidden_states) key_states = self.k_proj(hidden_states) value_states = self.v_proj(hidden_states) @@ -407,7 +407,7 @@ class FlaxMistralPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: MistralConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -416,7 +416,7 @@ class FlaxMistralPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/mistral/modeling_mistral.py b/src/transformers/models/mistral/modeling_mistral.py index 90881cbcd2b..10b0019aeea 100644 --- a/src/transformers/models/mistral/modeling_mistral.py +++ b/src/transformers/models/mistral/modeling_mistral.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_mistral.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -141,12 +141,12 @@ class MistralAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -222,9 +222,9 @@ class MistralDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -752,7 +752,7 @@ class MistralForQuestionAnswering(MistralPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/mistral/modeling_tf_mistral.py b/src/transformers/models/mistral/modeling_tf_mistral.py index f6d2289ff2d..d141d222659 100644 --- a/src/transformers/models/mistral/modeling_tf_mistral.py +++ b/src/transformers/models/mistral/modeling_tf_mistral.py @@ -21,7 +21,7 @@ import math import warnings -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -284,12 +284,12 @@ class TFMistralAttention(keras.layers.Layer): hidden_states: tf.Tensor, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_value: Optional[Tuple[tf.Tensor]] = None, + past_key_value: Optional[tuple[tf.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, training=None, **kwargs, - ) -> Tuple[tf.Tensor, Optional[tf.Tensor], Optional[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, Optional[tf.Tensor], Optional[tuple[tf.Tensor]]]: if "padding_mask" in kwargs: warnings.warn( "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`" @@ -396,11 +396,11 @@ class TFMistralDecoderLayer(keras.layers.Layer): hidden_states: tf.Tensor, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_value: Optional[Tuple[tf.Tensor]] = None, + past_key_value: Optional[tuple[tf.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, **kwargs, - ) -> Tuple[tf.Tensor, Optional[Tuple[tf.Tensor, tf.Tensor]]]: + ) -> tuple[tf.Tensor, Optional[tuple[tf.Tensor, tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -531,13 +531,13 @@ class TFMistralMainLayer(keras.layers.Layer): input_ids: Optional[tf.Tensor] = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutputWithPast]: + ) -> Union[tuple, TFBaseModelOutputWithPast]: # retrieve input_ids and inputs_embeds if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time") @@ -773,13 +773,13 @@ class TFMistralModel(TFMistralPreTrainedModel): input_ids: Optional[tf.Tensor] = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutputWithPast]: + ) -> Union[tuple, TFBaseModelOutputWithPast]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -840,14 +840,14 @@ class TFMistralForCausalLM(TFMistralPreTrainedModel, TFCausalLanguageModelingLos input_ids: Optional[tf.Tensor] = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, labels: Optional[tf.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFCausalLMOutputWithPast]: + ) -> Union[tuple, TFCausalLMOutputWithPast]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` @@ -965,14 +965,14 @@ class TFMistralForSequenceClassification(TFMistralPreTrainedModel, TFSequenceCla input_ids: Optional[tf.Tensor] = None, attention_mask: Optional[tf.Tensor] = None, position_ids: Optional[tf.Tensor] = None, - past_key_values: Optional[List[tf.Tensor]] = None, + past_key_values: Optional[list[tf.Tensor]] = None, inputs_embeds: Optional[tf.Tensor] = None, labels: Optional[tf.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFSequenceClassifierOutputWithPast]: + ) -> Union[tuple, TFSequenceClassifierOutputWithPast]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/mistral/modular_mistral.py b/src/transformers/models/mistral/modular_mistral.py index e943150f01b..b28ba006e92 100644 --- a/src/transformers/models/mistral/modular_mistral.py +++ b/src/transformers/models/mistral/modular_mistral.py @@ -1,4 +1,4 @@ -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -52,12 +52,12 @@ class MistralAttention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -239,7 +239,7 @@ class MistralForQuestionAnswering(LlamaForQuestionAnswering): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/mistral3/configuration_mistral3.py b/src/transformers/models/mistral3/configuration_mistral3.py index 357ba2cddd4..b6f9caa3340 100644 --- a/src/transformers/models/mistral3/configuration_mistral3.py +++ b/src/transformers/models/mistral3/configuration_mistral3.py @@ -37,7 +37,7 @@ class Mistral3Config(PretrainedConfig): The image token index to encode the image prompt. projector_hidden_act (`str`, *optional*, defaults to `"gelu"`): The activation function used by the multimodal projector. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -1): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -1): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/mistral3/modeling_mistral3.py b/src/transformers/models/mistral3/modeling_mistral3.py index e034392b740..a5d12a115ba 100644 --- a/src/transformers/models/mistral3/modeling_mistral3.py +++ b/src/transformers/models/mistral3/modeling_mistral3.py @@ -20,7 +20,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -156,9 +156,9 @@ class Mistral3CausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -252,7 +252,7 @@ class Mistral3Model(Mistral3PreTrainedModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, **kwargs, ): """ @@ -261,7 +261,7 @@ class Mistral3Model(Mistral3PreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`): The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -299,9 +299,9 @@ class Mistral3Model(Mistral3PreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -309,7 +309,7 @@ class Mistral3Model(Mistral3PreTrainedModel): cache_position: Optional[torch.LongTensor] = None, image_sizes: torch.Tensor = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, Mistral3ModelOutputWithPast]: + ) -> Union[tuple, Mistral3ModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -416,7 +416,7 @@ class Mistral3ForConditionalGeneration(Mistral3PreTrainedModel, GenerationMixin) self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, **kwargs, ): return self.model.get_image_features( @@ -447,7 +447,7 @@ class Mistral3ForConditionalGeneration(Mistral3PreTrainedModel, GenerationMixin) pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -458,7 +458,7 @@ class Mistral3ForConditionalGeneration(Mistral3PreTrainedModel, GenerationMixin) logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Mistral3CausalLMOutputWithPast]: + ) -> Union[tuple, Mistral3CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/mistral3/modular_mistral3.py b/src/transformers/models/mistral3/modular_mistral3.py index 611973b64bd..391b02d42b8 100644 --- a/src/transformers/models/mistral3/modular_mistral3.py +++ b/src/transformers/models/mistral3/modular_mistral3.py @@ -13,7 +13,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -136,7 +136,7 @@ class Mistral3Model(LlavaModel): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, **kwargs, ): """ @@ -145,7 +145,7 @@ class Mistral3Model(LlavaModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`): The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -181,9 +181,9 @@ class Mistral3Model(LlavaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -191,7 +191,7 @@ class Mistral3Model(LlavaModel): cache_position: Optional[torch.LongTensor] = None, image_sizes: torch.Tensor = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, Mistral3ModelOutputWithPast]: + ) -> Union[tuple, Mistral3ModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -258,7 +258,7 @@ class Mistral3ForConditionalGeneration(LlavaForConditionalGeneration): self, pixel_values: torch.FloatTensor, image_sizes: torch.Tensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, **kwargs, ): return self.model.get_image_features( @@ -274,7 +274,7 @@ class Mistral3ForConditionalGeneration(LlavaForConditionalGeneration): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -285,7 +285,7 @@ class Mistral3ForConditionalGeneration(LlavaForConditionalGeneration): logits_to_keep: Union[int, torch.Tensor] = 0, image_sizes: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Mistral3CausalLMOutputWithPast]: + ) -> Union[tuple, Mistral3CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/mixtral/modeling_mixtral.py b/src/transformers/models/mixtral/modeling_mixtral.py index 5e3e39f621d..26007b7b18a 100644 --- a/src/transformers/models/mixtral/modeling_mixtral.py +++ b/src/transformers/models/mixtral/modeling_mixtral.py @@ -25,7 +25,7 @@ # limitations under the License. from functools import partial -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -253,12 +253,12 @@ class MixtralAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -311,14 +311,14 @@ class MixtralDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -470,7 +470,7 @@ class MixtralModel(MixtralPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -589,7 +589,7 @@ class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ... def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -713,7 +713,7 @@ class MixtralForCausalLM(MixtralPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -998,7 +998,7 @@ class MixtralForQuestionAnswering(MixtralPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/mixtral/modular_mixtral.py b/src/transformers/models/mixtral/modular_mixtral.py index 288bc21247d..bfc78597cfe 100644 --- a/src/transformers/models/mixtral/modular_mixtral.py +++ b/src/transformers/models/mixtral/modular_mixtral.py @@ -20,7 +20,7 @@ """PyTorch Mixtral model.""" from functools import partial -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -52,7 +52,7 @@ logger = logging.get_logger(__name__) def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -242,14 +242,14 @@ class MixtralDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -321,7 +321,7 @@ class MixtralModel(MistralModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -454,7 +454,7 @@ class MixtralForCausalLM(MistralForCausalLM): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/mlcd/modeling_mlcd.py b/src/transformers/models/mlcd/modeling_mlcd.py index 84ee78433b0..b20e9f107e0 100644 --- a/src/transformers/models/mlcd/modeling_mlcd.py +++ b/src/transformers/models/mlcd/modeling_mlcd.py @@ -18,7 +18,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -209,7 +209,7 @@ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: def apply_rotary_pos_emb_vision( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: orig_q_dtype = q.dtype orig_k_dtype = k.dtype q, k = q.float(), k.float() @@ -254,10 +254,10 @@ class MLCDAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length = hidden_states.shape[:-1] @@ -311,16 +311,16 @@ class MLCDEncoderLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): Input to the layer of shape `(batch, seq_len, embed_dim)`. Represents the hidden states from the previous layer or the input embeddings. - position_embeddings (`Tuple[torch.Tensor, torch.Tensor]`): + position_embeddings (`tuple[torch.Tensor, torch.Tensor]`): A tuple of two tensors, each of shape `(batch, seq_len, embed_dim)`. Represents absolute positional embeddings for the query and key in the attention mechanism. attention_mask (`torch.FloatTensor`): @@ -373,19 +373,19 @@ class MLCDEncoder(nn.Module): def forward( self, inputs_embeds: torch.FloatTensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model's internal embedding lookup matrix. - position_embeddings (`Tuple[torch.Tensor, torch.Tensor]`): + position_embeddings (`tuple[torch.Tensor, torch.Tensor]`): A tuple of two tensors, each of shape `(batch, seq_len, embed_dim)`. Represents absolute positional embeddings for the query and key in the attention mechanism. attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -469,7 +469,7 @@ class MLCDVisionTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -579,7 +579,7 @@ class MLCDVisionModel(MLCDPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Example: diff --git a/src/transformers/models/mlcd/modular_mlcd.py b/src/transformers/models/mlcd/modular_mlcd.py index 1dad06b3c87..d18b2346224 100644 --- a/src/transformers/models/mlcd/modular_mlcd.py +++ b/src/transformers/models/mlcd/modular_mlcd.py @@ -12,7 +12,7 @@ # 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. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -203,10 +203,10 @@ class MLCDAttention(CLIPAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: batch_size, seq_length = hidden_states.shape[:-1] # Each of shape: [batch_size, seq_length, num_heads, head_dim] @@ -255,16 +255,16 @@ class MLCDEncoderLayer(CLIPEncoderLayer): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): Input to the layer of shape `(batch, seq_len, embed_dim)`. Represents the hidden states from the previous layer or the input embeddings. - position_embeddings (`Tuple[torch.Tensor, torch.Tensor]`): + position_embeddings (`tuple[torch.Tensor, torch.Tensor]`): A tuple of two tensors, each of shape `(batch, seq_len, embed_dim)`. Represents absolute positional embeddings for the query and key in the attention mechanism. attention_mask (`torch.FloatTensor`): @@ -313,19 +313,19 @@ class MLCDEncoder(CLIPEncoder): def forward( self, inputs_embeds: torch.FloatTensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model's internal embedding lookup matrix. - position_embeddings (`Tuple[torch.Tensor, torch.Tensor]`): + position_embeddings (`tuple[torch.Tensor, torch.Tensor]`): A tuple of two tensors, each of shape `(batch, seq_len, embed_dim)`. Represents absolute positional embeddings for the query and key in the attention mechanism. attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -402,7 +402,7 @@ class MLCDVisionTransformer(CLIPVisionTransformer): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -494,7 +494,7 @@ class MLCDVisionModel(CLIPVisionModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Example: diff --git a/src/transformers/models/mllama/configuration_mllama.py b/src/transformers/models/mllama/configuration_mllama.py index b501e9efd35..6d0518ab9b8 100644 --- a/src/transformers/models/mllama/configuration_mllama.py +++ b/src/transformers/models/mllama/configuration_mllama.py @@ -13,7 +13,7 @@ # limitations under the License. """Mllama model configuration""" -from typing import Dict, List, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...modeling_rope_utils import rope_config_validation @@ -62,10 +62,10 @@ class MllamaVisionConfig(PretrainedConfig): The epsilon used by the layer normalization layers. max_num_tiles (`int`, *optional*, defaults to 4): Maximum number of tiles for image splitting. - intermediate_layers_indices (`List[int]`, *optional*, defaults to [3, 7, 15, 23, 30]): + intermediate_layers_indices (`list[int]`, *optional*, defaults to [3, 7, 15, 23, 30]): Indices of intermediate layers of transformer encoder from which to extract and output features. These output features are concatenated with final hidden state of transformer encoder. - supported_aspect_ratios (`List[List[int]]`, *optional*): + supported_aspect_ratios (`list[list[int]]`, *optional*): List of supported aspect ratios for image splitting. If not specified, the default supported aspect ratios are [[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [3, 1], [4, 1]] for `max_num_tiles=4`. initializer_range (`float`, *optional*, defaults to 0.02): @@ -103,8 +103,8 @@ class MllamaVisionConfig(PretrainedConfig): patch_size: int = 14, norm_eps: float = 1e-5, max_num_tiles: int = 4, - intermediate_layers_indices: Optional[List[int]] = None, - supported_aspect_ratios: Optional[List[List[int]]] = None, + intermediate_layers_indices: Optional[list[int]] = None, + supported_aspect_ratios: Optional[list[list[int]]] = None, initializer_range: float = 0.02, **kwargs, ): @@ -193,11 +193,11 @@ class MllamaTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -215,7 +215,7 @@ class MllamaTextConfig(PretrainedConfig): Whether or not the model should return the last key/values attentions. tie_word_embeddings (`bool`, *optional*, defaults to `False`): Whether to tie weight embeddings - cross_attention_layers (`List[int]`, *optional*): + cross_attention_layers (`list[int]`, *optional*): Indices of the cross attention layers. If not specified, will default to [3, 8, 13, 18, 23, 28, 33, 38]. dropout (`float`, *optional*, defaults to 0): The dropout probability for self- and cross-attention layers. @@ -254,13 +254,13 @@ class MllamaTextConfig(PretrainedConfig): num_key_value_heads: int = 8, intermediate_size: int = 14_336, rope_theta: float = 500_000, - rope_scaling: Optional[Dict] = None, + rope_scaling: Optional[dict] = None, rms_norm_eps: float = 1e-5, max_position_embeddings: int = 131_072, initializer_range: float = 0.02, use_cache: bool = True, tie_word_embeddings: bool = False, - cross_attention_layers: Optional[List[int]] = None, + cross_attention_layers: Optional[list[int]] = None, dropout: float = 0, bos_token_id: int = 128000, eos_token_id: int = 128001, diff --git a/src/transformers/models/mllama/convert_mllama_weights_to_hf.py b/src/transformers/models/mllama/convert_mllama_weights_to_hf.py index 8cb9e78daa5..1a6060eba41 100644 --- a/src/transformers/models/mllama/convert_mllama_weights_to_hf.py +++ b/src/transformers/models/mllama/convert_mllama_weights_to_hf.py @@ -17,7 +17,7 @@ import gc import json import math import os -from typing import List, Optional +from typing import Optional import regex as re import torch @@ -475,7 +475,7 @@ class MllamaConverter(TikTokenConverter): def __init__( self, vocab_file, - special_tokens: List[str], + special_tokens: list[str], pattern: str, model_max_length: int, chat_template: Optional[str] = None, @@ -605,7 +605,7 @@ def main(): parser.add_argument( "--special_tokens", default=None, - type=List[str], + type=list[str], help="The list of special tokens that should be added to the model.", ) parser.add_argument( diff --git a/src/transformers/models/mllama/image_processing_mllama.py b/src/transformers/models/mllama/image_processing_mllama.py index bcb97bbcd99..47a0fcad3ee 100644 --- a/src/transformers/models/mllama/image_processing_mllama.py +++ b/src/transformers/models/mllama/image_processing_mllama.py @@ -15,7 +15,7 @@ import math from functools import lru_cache -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -50,7 +50,7 @@ logger = logging.get_logger(__name__) @lru_cache(maxsize=10) -def get_all_supported_aspect_ratios(max_image_tiles: int) -> List[Tuple[int, int]]: +def get_all_supported_aspect_ratios(max_image_tiles: int) -> list[tuple[int, int]]: """ Computes all allowed aspect ratios for a given maximum number of input tiles. @@ -63,7 +63,7 @@ def get_all_supported_aspect_ratios(max_image_tiles: int) -> List[Tuple[int, int The maximum number of tiles allowed. Returns: - `List[Tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) + `list[tuple[int, int]]`: A list of tuples, each tuple representing a valid (width, height) configuration in terms of number of tiles. Example: @@ -85,7 +85,7 @@ def get_image_size_fit_to_canvas( canvas_height: int, canvas_width: int, tile_size: int, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Calculates the new size of an image to fit within a canvas while maintaining aspect ratio. @@ -108,7 +108,7 @@ def get_image_size_fit_to_canvas( The tile size. Returns: - `Tuple[int, int]`: A tuple containing the new height and width of the image. + `tuple[int, int]`: A tuple containing the new height and width of the image. """ # Set target image size in between `tile_size` and canvas_size @@ -136,7 +136,7 @@ def get_optimal_tiled_canvas( image_width: int, max_image_tiles: int, tile_size: int, -) -> Tuple[int, int]: +) -> tuple[int, int]: r""" Determines the best canvas based on image and tile size and maximum number of tiles. @@ -239,7 +239,7 @@ def get_optimal_tiled_canvas( The tile size. Returns: - `Tuple[int, int]`: The best canvas resolution [height, width] for the given image. + `tuple[int, int]`: The best canvas resolution [height, width] for the given image. """ possible_tile_arrangements = get_all_supported_aspect_ratios(max_image_tiles) possible_canvas_sizes = np.array(possible_tile_arrangements) * tile_size @@ -311,12 +311,12 @@ def split_to_tiles(image: np.ndarray, num_tiles_height: int, num_tiles_width: in return np.ascontiguousarray(image) -def build_aspect_ratio_mask(aspect_ratios: List[List[Tuple[int, int]]], max_image_tiles: int) -> np.ndarray: +def build_aspect_ratio_mask(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Builds a mask for the aspect ratios of the images. Args: - aspect_ratios (`List[List[Tuple[int, int]]]`): + aspect_ratios (`list[list[tuple[int, int]]]`): A list of lists containing aspect ratios for each image in the batch. Each aspect ratio is represented as a tuple of (width, height) in terms of number of tiles. max_image_tiles (`int`): @@ -345,9 +345,9 @@ def build_aspect_ratio_mask(aspect_ratios: List[List[Tuple[int, int]]], max_imag def pack_images( - batch_images: List[List[np.ndarray]], + batch_images: list[list[np.ndarray]], max_image_tiles: int, -) -> Tuple[np.ndarray, List[List[int]]]: +) -> tuple[np.ndarray, list[list[int]]]: """ Stack a list of lists of images with variable lengths into a numpy array, applying zero padding as needed. Each list in the input represents a batch sample, and each image within a list is expected to be @@ -355,7 +355,7 @@ def pack_images( (batch_size, max_num_images, max_image_tiles, channels, tile_height, tile_width). Args: - batch_images (`List[List[np.ndarray]]`): + batch_images (`list[list[np.ndarray]]`): A list of lists of image tiles. Each inner list represents a batch sample containing multiple images, where each image is pre-split into tiles. The shape of each tile array is (num_tiles, channels, tile_height, tile_width). @@ -363,11 +363,11 @@ def pack_images( The maximum number of tiles any image was potantially split. Returns: - `Tuple[np.ndarray, List[List[int]]]`: A tuple containing: + `tuple[np.ndarray, list[list[int]]]`: A tuple containing: - stacked_images (`np.ndarray`): A numpy array of stacked images with shape (batch_size, max_num_images, max_image_tiles, channels, tile_height, tile_width). - - all_num_tiles (`List[List[int]]`): + - all_num_tiles (`list[list[int]]`): A list of lists containing the number of tiles for each image in each batch sample. """ @@ -397,12 +397,12 @@ def pack_images( return stacked_images, all_num_tiles -def pack_aspect_ratios(aspect_ratios: List[List[Tuple[int, int]]], pad_value: int = 1) -> np.ndarray: +def pack_aspect_ratios(aspect_ratios: list[list[tuple[int, int]]], pad_value: int = 1) -> np.ndarray: """ Stack a list of aspect ratios into a numpy array. Args: - aspect_ratios (`List[List[Tuple[int, int]]]`): + aspect_ratios (`list[list[tuple[int, int]]]`): A list of aspect ratios. pad_value (`int`, *optional*, defaults to 1): The value to pad the aspect ratios with. @@ -421,7 +421,7 @@ def pack_aspect_ratios(aspect_ratios: List[List[Tuple[int, int]]], pad_value: in return aspect_ratios_stacked -def convert_aspect_ratios_to_ids(aspect_ratios: List[List[Tuple[int, int]]], max_image_tiles: int) -> np.ndarray: +def convert_aspect_ratios_to_ids(aspect_ratios: list[list[tuple[int, int]]], max_image_tiles: int) -> np.ndarray: """ Convert aspect ratio tuples to unique ids. @@ -429,7 +429,7 @@ def convert_aspect_ratios_to_ids(aspect_ratios: List[List[Tuple[int, int]]], max The aspect ratio ids start from 1, with 1 corresponding to the first supported aspect ratio. Args: - aspect_ratios (`List[List[Tuple[int, int]]]`): + aspect_ratios (`list[list[tuple[int, int]]]`): A list of aspect ratios for each image in the batch. max_image_tiles (`int`): The maximum number of tiles any image can be split into. @@ -516,7 +516,7 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: return alpha_composite -def _validate_size(size: Dict[str, int]) -> None: +def _validate_size(size: dict[str, int]) -> None: if not ("height" in size and "width" in size): raise ValueError(f"Argument `size` must be a dictionary with keys 'height' and 'width'. Got: {size}") if size["height"] != size["width"]: @@ -543,7 +543,7 @@ class MllamaImageProcessor(BaseImageProcessor): Only has an effect if the input image is in the PIL format. do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image tile. Should be a dictionary containing 'height' and 'width' keys, both with integer values. The height and width values should be equal. resample (`int`, *optional*, defaults to `Resampling.BILINEAR`): @@ -555,9 +555,9 @@ class MllamaImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `True`): @@ -572,13 +572,13 @@ class MllamaImageProcessor(BaseImageProcessor): self, do_convert_rgb: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, max_image_tiles: int = 4, **kwargs, @@ -603,13 +603,13 @@ class MllamaImageProcessor(BaseImageProcessor): images: ImageInput, do_convert_rgb: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: Optional[PILImageResampling] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, max_image_tiles: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -625,7 +625,7 @@ class MllamaImageProcessor(BaseImageProcessor): Whether to convert the image to RGB. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image tile. Should be a dictionary containing 'height' and 'width' keys, both with integer values. The height and width values should be equal. resample (`int`, *optional*, defaults to `self.resample`): @@ -637,9 +637,9 @@ class MllamaImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): @@ -664,7 +664,7 @@ class MllamaImageProcessor(BaseImageProcessor): `BatchFeature` of the following structure: - **pixel_values** (`TensorType`): The preprocessed pixel values. - **aspect_ratio_ids** (`TensorType`): The aspect ratio ids of the images. - - **num_tiles** (`List[List[int]]`): The number of tiles for each image in the batch. + - **num_tiles** (`list[list[int]]`): The number of tiles for each image in the batch. """ do_convert_rgb = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb do_resize = do_resize if do_resize is not None else self.do_resize @@ -771,7 +771,7 @@ class MllamaImageProcessor(BaseImageProcessor): # images (np.ndarray) with shape (batch_size, max_num_images, max_image_tiles, channels, tile_height, tile_width) # aspect_ratio_ids (np.ndarray) with shape (batch_size, max_num_images) - aspect ratio ids for each image, padded to max_num_images with 0 - # num_tiles (List[List[int]]) with (batch_size, num_images_in_batch) - real number of tiles for each image, not padded + # num_tiles (list[list[int]]) with (batch_size, num_images_in_batch) - real number of tiles for each image, not padded # aspect_ratio_mask (np.ndarray) with shape (batch_size, max_num_images, max_image_tiles) - number of tiles for each image, padded to max_num_images with 0 encoded_inputs = BatchFeature( data={ @@ -788,8 +788,8 @@ class MllamaImageProcessor(BaseImageProcessor): def pad( self, image: np.ndarray, - size: Dict[str, int], - aspect_ratio: Tuple[int, int], + size: dict[str, int], + aspect_ratio: tuple[int, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -800,9 +800,9 @@ class MllamaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. - aspect_ratio (`Tuple[int, int]`): + aspect_ratio (`tuple[int, int]`): The aspect ratio of the image. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format of the image. If not provided, it will be the same as the input image. @@ -835,12 +835,12 @@ class MllamaImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], max_image_tiles: int, resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Union[np.ndarray, Tuple[int, int]]: + ) -> Union[np.ndarray, tuple[int, int]]: """ Resizes an image to fit within a tiled canvas while maintaining its aspect ratio. The optimal canvas size is calculated based on the maximum number of tiles and the tile size. @@ -852,7 +852,7 @@ class MllamaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. max_image_tiles (`int`): The maximum number of tiles to split the image into. @@ -864,7 +864,7 @@ class MllamaImageProcessor(BaseImageProcessor): The channel dimension format of the input image. If not provided, it will be inferred. Returns: - `Union[np.ndarray, Tuple[int, int]]`: The resized image and a tuple containing the number of tiles + `Union[np.ndarray, tuple[int, int]]`: The resized image and a tuple containing the number of tiles along the height and width dimensions. """ diff --git a/src/transformers/models/mllama/modeling_mllama.py b/src/transformers/models/mllama/modeling_mllama.py index 4c73295cab6..841674f0c7f 100644 --- a/src/transformers/models/mllama/modeling_mllama.py +++ b/src/transformers/models/mllama/modeling_mllama.py @@ -15,7 +15,7 @@ """PyTorch Mllama model.""" import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -48,7 +48,7 @@ def _prepare_cross_attention_mask( cross_attention_mask: torch.Tensor, num_vision_tokens: int, dtype: str, -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: # reshape so it can be used by attn module batch_size, text_total_length, *_ = cross_attention_mask.shape cross_attention_mask = cross_attention_mask.repeat_interleave(num_vision_tokens, dim=3) @@ -237,7 +237,7 @@ class MllamaVisionAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: query = self.q_proj(hidden_state) key = self.k_proj(hidden_state) value = self.v_proj(hidden_state) @@ -352,7 +352,7 @@ class MllamaVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -474,7 +474,7 @@ class MllamaTextCrossAttention(nn.Module): use_cache: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -688,15 +688,15 @@ class MllamaSelfAttentionDecoderLayer(nn.Module): cross_attention_states: Optional[torch.Tensor] = None, cross_attention_mask: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, - full_text_row_masked_out_mask: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + full_text_row_masked_out_mask: Optional[tuple[torch.Tensor, torch.Tensor]] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -712,7 +712,7 @@ class MllamaSelfAttentionDecoderLayer(nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -775,7 +775,7 @@ class MllamaCrossAttentionDecoderLayer(torch.nn.Module): cross_attention_states: torch.Tensor, cross_attention_mask: torch.Tensor, attention_mask: torch.Tensor, - full_text_row_masked_out_mask: Tuple[torch.Tensor, torch.Tensor], + full_text_row_masked_out_mask: tuple[torch.Tensor, torch.Tensor], position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: Optional[bool] = False, @@ -783,7 +783,7 @@ class MllamaCrossAttentionDecoderLayer(torch.nn.Module): cache_position: Optional[torch.LongTensor] = None, position_embeddings: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -1087,7 +1087,7 @@ class MllamaVisionModel(MllamaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutput, Tuple[torch.Tensor, ...]]: + ) -> Union[BaseModelOutput, tuple[torch.Tensor, ...]]: r""" aspect_ratio_ids (`torch.Tensor` of shape `(batch_size, max_num_images)`, *optional*): Aspect ratio ids used to select the appropriate precomputed tile embeddings based on the aspect ratio of each input image. @@ -1294,8 +1294,8 @@ class MllamaTextModel(MllamaPreTrainedModel): position_ids: Optional[torch.LongTensor] = None, cross_attention_states: Optional[torch.FloatTensor] = None, cross_attention_mask: Optional[torch.Tensor] = None, - full_text_row_masked_out_mask: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + full_text_row_masked_out_mask: Optional[tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1303,7 +1303,7 @@ class MllamaTextModel(MllamaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" cross_attention_states (`torch.FloatTensor`, *optional*): Output of the vision model, used for cross-attention. This tensor contains the processed image features that @@ -1315,7 +1315,7 @@ class MllamaTextModel(MllamaPreTrainedModel): For each text token (in seq_length): - 1 indicates the token **should attend** to the corresponding image tile - 0 indicates the token **should not attend** to the corresponding image tile - full_text_row_masked_out_mask (`Tuple[torch.Tensor, torch.Tensor]`, *optional*): + full_text_row_masked_out_mask (`tuple[torch.Tensor, torch.Tensor]`, *optional*): A tuple containing two tensors that mask out rows in the cross-attention mechanism: - The first tensor has shape `(batch_size, 1, seq_length, 1)` and contains values of 0 or 1. A value of 0 indicates that the corresponding text token's entire row in the cross-attention @@ -1508,8 +1508,8 @@ class MllamaForCausalLM(MllamaPreTrainedModel, GenerationMixin): position_ids: Optional[torch.LongTensor] = None, cross_attention_states: Optional[torch.LongTensor] = None, cross_attention_mask: Optional[torch.LongTensor] = None, - full_text_row_masked_out_mask: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + full_text_row_masked_out_mask: Optional[tuple[torch.Tensor, torch.Tensor]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1519,7 +1519,7 @@ class MllamaForCausalLM(MllamaPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" cross_attention_states (`torch.FloatTensor`, *optional*): Output of the vision model, used for cross-attention. This tensor contains the processed image features that @@ -1535,7 +1535,7 @@ class MllamaForCausalLM(MllamaPreTrainedModel, GenerationMixin): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. - full_text_row_masked_out_mask (`Tuple[torch.Tensor, torch.Tensor]`, *optional*): + full_text_row_masked_out_mask (`tuple[torch.Tensor, torch.Tensor]`, *optional*): A tuple containing two tensors that mask out rows in the cross-attention mechanism: - The first tensor has shape `(batch_size, 1, seq_length, 1)` and contains values of 0 or 1. A value of 0 indicates that the corresponding text token's entire row in the cross-attention @@ -1653,7 +1653,7 @@ class MllamaModel(MllamaPreTrainedModel): cross_attention_mask: Optional[torch.Tensor] = None, cross_attention_states: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1661,7 +1661,7 @@ class MllamaModel(MllamaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" aspect_ratio_mask (`torch.Tensor` of shape `(batch_size, max_num_images, max_num_tiles)`, *optional*): Mask to avoid performing attention on padding tiles. Mask values selected in `[0, 1]`: @@ -1818,7 +1818,7 @@ class MllamaForConditionalGeneration(MllamaPreTrainedModel, GenerationMixin): cross_attention_mask: Optional[torch.Tensor] = None, cross_attention_states: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1828,7 +1828,7 @@ class MllamaForConditionalGeneration(MllamaPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" aspect_ratio_mask (`torch.Tensor` of shape `(batch_size, max_num_images, max_num_tiles)`, *optional*): Mask to avoid performing attention on padding tiles. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/mllama/processing_mllama.py b/src/transformers/models/mllama/processing_mllama.py index ffe76709482..063f0e8abc3 100644 --- a/src/transformers/models/mllama/processing_mllama.py +++ b/src/transformers/models/mllama/processing_mllama.py @@ -15,7 +15,7 @@ """Processor class for Mllama.""" -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -39,7 +39,7 @@ class MllamaProcessorKwargs(ProcessingKwargs, total=False): } -def get_cross_attention_token_mask(input_ids: List[int], image_token_id: int) -> List[List[int]]: +def get_cross_attention_token_mask(input_ids: list[int], image_token_id: int) -> list[list[int]]: """ Generate a cross-attention token mask for image tokens in the input sequence. @@ -47,11 +47,11 @@ def get_cross_attention_token_mask(input_ids: List[int], image_token_id: int) -> a mask that defines which subsequent tokens each image token should attend to. Args: - input_ids (List[int]): A list of token ids representing the input sequence. + input_ids (list[int]): A list of token ids representing the input sequence. image_token_id (int): The id of the token used to represent images in the sequence. Returns: - List[List[int]]: A list of [start, end] pairs, where each pair represents the range + list[list[int]]: A list of [start, end] pairs, where each pair represents the range of tokens an image token should attend to. Notes: @@ -88,8 +88,8 @@ def get_cross_attention_token_mask(input_ids: List[int], image_token_id: int) -> def convert_sparse_cross_attention_mask_to_dense( - cross_attention_token_mask: List[List[List[int]]], - num_tiles: List[List[int]], + cross_attention_token_mask: list[list[list[int]]], + num_tiles: list[list[int]], max_num_tiles: int, length: int, ) -> np.ndarray: @@ -100,11 +100,11 @@ def convert_sparse_cross_attention_mask_to_dense( The sparse representation is a nested list structure that defines attention ranges for each image in each batch item. Args: - cross_attention_token_mask (List[List[List[int]]]): A nested list structure where: + cross_attention_token_mask (list[list[list[int]]]): A nested list structure where: - The outer list represents the batch dimension. - The middle list represents different images within each batch item. - The inner list contains pairs of integers [start, end] representing token ranges for each image. - num_tiles (List[List[int]]): A nested list structure specifying the number of tiles for each image in each batch item. + num_tiles (list[list[int]]): A nested list structure specifying the number of tiles for each image in each batch item. max_num_tiles (int): The maximum possible number of tiles. length (int): The total sequence length of the input. @@ -224,7 +224,7 @@ class MllamaProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio=None, videos=None, **kwargs: Unpack[MllamaProcessorKwargs], @@ -237,10 +237,10 @@ class MllamaProcessor(ProcessorMixin): to the docstring of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -372,7 +372,7 @@ class MllamaProcessor(ProcessorMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text. + `list[str]`: The decoded text. """ return self.tokenizer.batch_decode( generated_outputs, diff --git a/src/transformers/models/mluke/tokenization_mluke.py b/src/transformers/models/mluke/tokenization_mluke.py index 90619befc22..f858eca12d5 100644 --- a/src/transformers/models/mluke/tokenization_mluke.py +++ b/src/transformers/models/mluke/tokenization_mluke.py @@ -19,7 +19,7 @@ import json import os from collections.abc import Mapping from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import sentencepiece as spm @@ -43,10 +43,10 @@ from ...utils.import_utils import requires logger = logging.get_logger(__name__) -EntitySpan = Tuple[int, int] -EntitySpanInput = List[EntitySpan] +EntitySpan = tuple[int, int] +EntitySpanInput = list[EntitySpan] Entity = str -EntityInput = List[Entity] +EntityInput = list[Entity] SPIECE_UNDERLINE = "▁" @@ -192,7 +192,7 @@ class MLukeTokenizer(PreTrainedTokenizer): entity_token_2 (`str`, *optional*, defaults to ``): The special token used to represent an entity span in a word token sequence. This token is only used when `task` is set to `"entity_pair_classification"`. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -238,7 +238,7 @@ class MLukeTokenizer(PreTrainedTokenizer): entity_pad_token="[PAD]", entity_mask_token="[MASK]", entity_mask2_token="[MASK2]", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -342,7 +342,7 @@ class MLukeTokenizer(PreTrainedTokenizer): return vocab # Copied from transformers.models.xlm_roberta.tokenization_xlm_roberta.XLMRobertaTokenizer._tokenize - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: # TODO check if the t5/llama PR also applies here return self.sp_model.encode(text, out_type=str) @@ -387,12 +387,12 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer.__call__ def __call__( self, - text: Union[TextInput, List[TextInput]], - text_pair: Optional[Union[TextInput, List[TextInput]]] = None, - entity_spans: Optional[Union[EntitySpanInput, List[EntitySpanInput]]] = None, - entity_spans_pair: Optional[Union[EntitySpanInput, List[EntitySpanInput]]] = None, - entities: Optional[Union[EntityInput, List[EntityInput]]] = None, - entities_pair: Optional[Union[EntityInput, List[EntityInput]]] = None, + text: Union[TextInput, list[TextInput]], + text_pair: Optional[Union[TextInput, list[TextInput]]] = None, + entity_spans: Optional[Union[EntitySpanInput, list[EntitySpanInput]]] = None, + entity_spans_pair: Optional[Union[EntitySpanInput, list[EntitySpanInput]]] = None, + entities: Optional[Union[EntityInput, list[EntityInput]]] = None, + entities_pair: Optional[Union[EntityInput, list[EntityInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -417,31 +417,31 @@ class MLukeTokenizer(PreTrainedTokenizer): sequences, depending on the task you want to prepare them for. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this tokenizer does not support tokenization based on pretokenized strings. - text_pair (`str`, `List[str]`, `List[List[str]]`): + text_pair (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence must be a string. Note that this tokenizer does not support tokenization based on pretokenized strings. - entity_spans (`List[Tuple[int, int]]`, `List[List[Tuple[int, int]]]`, *optional*): + entity_spans (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*): The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each with two integers denoting character-based start and end positions of entities. If you specify `"entity_classification"` or `"entity_pair_classification"` as the `task` argument in the constructor, the length of each sequence must be 1 or 2, respectively. If you specify `entities`, the length of each sequence must be equal to the length of each sequence of `entities`. - entity_spans_pair (`List[Tuple[int, int]]`, `List[List[Tuple[int, int]]]`, *optional*): + entity_spans_pair (`list[tuple[int, int]]`, `list[list[tuple[int, int]]]`, *optional*): The sequence or batch of sequences of entity spans to be encoded. Each sequence consists of tuples each with two integers denoting character-based start and end positions of entities. If you specify the `task` argument in the constructor, this argument is ignored. If you specify `entities_pair`, the length of each sequence must be equal to the length of each sequence of `entities_pair`. - entities (`List[str]`, `List[List[str]]`, *optional*): + entities (`list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of each sequence must be equal to the length of each sequence of `entity_spans`. If you specify `entity_spans` without specifying this argument, the entity sequence or the batch of entity sequences is automatically constructed by filling it with the [MASK] entity. - entities_pair (`List[str]`, `List[List[str]]`, *optional*): + entities_pair (`list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences of entities to be encoded. Each sequence consists of strings representing entities, i.e., special entities (e.g., [MASK]) or entity titles of Wikipedia (e.g., Los Angeles). This argument is ignored if you specify the `task` argument in the constructor. The length of @@ -455,14 +455,14 @@ class MLukeTokenizer(PreTrainedTokenizer): is_valid_single_text = isinstance(text, str) is_valid_batch_text = isinstance(text, (list, tuple)) and (len(text) == 0 or (isinstance(text[0], str))) if not (is_valid_single_text or is_valid_batch_text): - raise ValueError("text input must be of type `str` (single example) or `List[str]` (batch).") + raise ValueError("text input must be of type `str` (single example) or `list[str]` (batch).") is_valid_single_text_pair = isinstance(text_pair, str) is_valid_batch_text_pair = isinstance(text_pair, (list, tuple)) and ( len(text_pair) == 0 or isinstance(text_pair[0], str) ) if not (text_pair is None or is_valid_single_text_pair or is_valid_batch_text_pair): - raise ValueError("text_pair input must be of type `str` (single example) or `List[str]` (batch).") + raise ValueError("text_pair input must be of type `str` (single example) or `list[str]` (batch).") is_batched = bool(isinstance(text, (list, tuple))) @@ -619,12 +619,12 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer._batch_encode_plus def _batch_encode_plus( self, - batch_text_or_text_pairs: Union[List[TextInput], List[TextInputPair]], + batch_text_or_text_pairs: Union[list[TextInput], list[TextInputPair]], batch_entity_spans_or_entity_spans_pairs: Optional[ - Union[List[EntitySpanInput], List[Tuple[EntitySpanInput, EntitySpanInput]]] + Union[list[EntitySpanInput], list[tuple[EntitySpanInput, EntitySpanInput]]] ] = None, batch_entities_or_entities_pairs: Optional[ - Union[List[EntityInput], List[Tuple[EntityInput, EntityInput]]] + Union[list[EntityInput], list[tuple[EntityInput, EntityInput]]] ] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -756,7 +756,7 @@ class MLukeTokenizer(PreTrainedTokenizer): entity_spans: Optional[EntitySpanInput] = None, entity_spans_pair: Optional[EntitySpanInput] = None, **kwargs, - ) -> Tuple[list, list, list, list, list, list]: + ) -> tuple[list, list, list, list, list, list]: def get_input_ids(text): tokens = self.tokenize(text, **kwargs) return self.convert_tokens_to_ids(tokens) @@ -903,9 +903,9 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer._batch_prepare_for_model def _batch_prepare_for_model( self, - batch_ids_pairs: List[Tuple[List[int], None]], - batch_entity_ids_pairs: List[Tuple[Optional[List[int]], Optional[List[int]]]], - batch_entity_token_spans_pairs: List[Tuple[Optional[List[Tuple[int, int]]], Optional[List[Tuple[int, int]]]]], + batch_ids_pairs: list[tuple[list[int], None]], + batch_entity_ids_pairs: list[tuple[Optional[list[int]], Optional[list[int]]]], + batch_entity_token_spans_pairs: list[tuple[Optional[list[tuple[int, int]]], Optional[list[tuple[int, int]]]]], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -989,12 +989,12 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer.prepare_for_model def prepare_for_model( self, - ids: List[int], - pair_ids: Optional[List[int]] = None, - entity_ids: Optional[List[int]] = None, - pair_entity_ids: Optional[List[int]] = None, - entity_token_spans: Optional[List[Tuple[int, int]]] = None, - pair_entity_token_spans: Optional[List[Tuple[int, int]]] = None, + ids: list[int], + pair_ids: Optional[list[int]] = None, + entity_ids: Optional[list[int]] = None, + pair_entity_ids: Optional[list[int]] = None, + entity_token_spans: Optional[list[tuple[int, int]]] = None, + pair_entity_token_spans: Optional[list[tuple[int, int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1023,17 +1023,17 @@ class MLukeTokenizer(PreTrainedTokenizer): error. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. - entity_ids (`List[int]`, *optional*): + entity_ids (`list[int]`, *optional*): Entity ids of the first sequence. - pair_entity_ids (`List[int]`, *optional*): + pair_entity_ids (`list[int]`, *optional*): Entity ids of the second sequence. - entity_token_spans (`List[Tuple[int, int]]`, *optional*): + entity_token_spans (`list[tuple[int, int]]`, *optional*): Entity spans of the first sequence. - pair_entity_token_spans (`List[Tuple[int, int]]`, *optional*): + pair_entity_token_spans (`list[tuple[int, int]]`, *optional*): Entity spans of the second sequence. max_entity_length (`int`, *optional*): The maximum length of the entity sequence. @@ -1223,10 +1223,10 @@ class MLukeTokenizer(PreTrainedTokenizer): self, encoded_inputs: Union[ BatchEncoding, - List[BatchEncoding], - Dict[str, EncodedInput], - Dict[str, List[EncodedInput]], - List[Dict[str, EncodedInput]], + list[BatchEncoding], + dict[str, EncodedInput], + dict[str, list[EncodedInput]], + list[dict[str, EncodedInput]], ], padding: Union[bool, str, PaddingStrategy] = True, max_length: Optional[int] = None, @@ -1246,11 +1246,11 @@ class MLukeTokenizer(PreTrainedTokenizer): specific device of your tensors however. Args: - encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `Dict[str, List[int]]`, `Dict[str, List[List[int]]` or `List[Dict[str, List[int]]]`): - Tokenized inputs. Can represent one input ([`BatchEncoding`] or `Dict[str, List[int]]`) or a batch of - tokenized inputs (list of [`BatchEncoding`], *Dict[str, List[List[int]]]* or *List[Dict[str, - List[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader - collate function. Instead of `List[int]` you can have tensors (numpy arrays, PyTorch tensors or + encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `dict[str, list[int]]`, `dict[str, list[list[int]]` or `list[dict[str, list[int]]]`): + Tokenized inputs. Can represent one input ([`BatchEncoding`] or `dict[str, list[int]]`) or a batch of + tokenized inputs (list of [`BatchEncoding`], *dict[str, list[list[int]]]* or *list[dict[str, + list[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader + collate function. Instead of `list[int]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see the note above for the return type. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`): Select a strategy to pad the returned sequences (according to the model's padding side and padding @@ -1388,7 +1388,7 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, max_entity_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -1402,7 +1402,7 @@ class MLukeTokenizer(PreTrainedTokenizer): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. max_entity_length: The maximum length of the entity sequence. @@ -1530,7 +1530,7 @@ class MLukeTokenizer(PreTrainedTokenizer): return encoded_inputs - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str, str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str, str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -1557,8 +1557,8 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm_roberta.tokenization_xlm_roberta.XLMRobertaTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -1567,13 +1567,13 @@ class MLukeTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -1584,22 +1584,22 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm_roberta.tokenization_xlm_roberta.XLMRobertaTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -1613,20 +1613,20 @@ class MLukeTokenizer(PreTrainedTokenizer): # Copied from transformers.models.xlm_roberta.tokenization_xlm_roberta.XLMRobertaTokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ diff --git a/src/transformers/models/mobilebert/modeling_mobilebert.py b/src/transformers/models/mobilebert/modeling_mobilebert.py index 6cfd569f002..7c34fd3d0b7 100644 --- a/src/transformers/models/mobilebert/modeling_mobilebert.py +++ b/src/transformers/models/mobilebert/modeling_mobilebert.py @@ -24,7 +24,7 @@ import math import os import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -244,7 +244,7 @@ class MobileBertSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(query_tensor) mixed_key_layer = self.key(key_tensor) mixed_value_layer = self.value(value_tensor) @@ -326,7 +326,7 @@ class MobileBertAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( query_tensor, key_tensor, @@ -416,7 +416,7 @@ class Bottleneck(nn.Module): if self.key_query_shared_bottleneck: self.attention = BottleneckLayer(config) - def forward(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor]: # This method can return three different tuples of values. These different values make use of bottlenecks, # which are linear layers used to project the hidden states to a lower-dimensional vector, reducing memory # usage. These linear layer have weights that are learned during training. @@ -487,7 +487,7 @@ class MobileBertLayer(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.use_bottleneck: query_tensor, key_tensor, value_tensor, layer_input = self.bottleneck(hidden_states) else: @@ -543,7 +543,7 @@ class MobileBertEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None for i, layer_module in enumerate(self.layer): @@ -646,7 +646,7 @@ class MobileBertPreTrainingHeads(nn.Module): self.predictions = MobileBertLMPredictionHead(config) self.seq_relationship = nn.Linear(config.hidden_size, 2) - def forward(self, sequence_output: torch.Tensor, pooled_output: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, sequence_output: torch.Tensor, pooled_output: torch.Tensor) -> tuple[torch.Tensor]: prediction_scores = self.predictions(sequence_output) seq_relationship_score = self.seq_relationship(pooled_output) return prediction_scores, seq_relationship_score @@ -707,8 +707,8 @@ class MobileBertForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -758,7 +758,7 @@ class MobileBertModel(MobileBertPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -864,7 +864,7 @@ class MobileBertForPreTraining(MobileBertPreTrainedModel): output_attentions: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[torch.FloatTensor] = None, return_dict: Optional[torch.FloatTensor] = None, - ) -> Union[Tuple, MobileBertForPreTrainingOutput]: + ) -> Union[tuple, MobileBertForPreTrainingOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -969,7 +969,7 @@ class MobileBertForMaskedLM(MobileBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1049,7 +1049,7 @@ class MobileBertForNextSentencePrediction(MobileBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, NextSentencePredictorOutput]: + ) -> Union[tuple, NextSentencePredictorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the next sequence prediction (classification) loss. Input should be a sequence pair @@ -1154,7 +1154,7 @@ class MobileBertForSequenceClassification(MobileBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1241,7 +1241,7 @@ class MobileBertForQuestionAnswering(MobileBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.mobilebert( @@ -1322,7 +1322,7 @@ class MobileBertForMultipleChoice(MobileBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1431,7 +1431,7 @@ class MobileBertForTokenClassification(MobileBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. diff --git a/src/transformers/models/mobilebert/modeling_tf_mobilebert.py b/src/transformers/models/mobilebert/modeling_tf_mobilebert.py index fff35745bf4..10b8d0c49c4 100644 --- a/src/transformers/models/mobilebert/modeling_tf_mobilebert.py +++ b/src/transformers/models/mobilebert/modeling_tf_mobilebert.py @@ -19,7 +19,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -1065,8 +1065,8 @@ class TFMobileBertForPreTrainingOutput(ModelOutput): loss: tf.Tensor | None = None prediction_logits: Optional[tf.Tensor] = None seq_relationship_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None MOBILEBERT_START_DOCSTRING = r""" @@ -1195,7 +1195,7 @@ class TFMobileBertModel(TFMobileBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPooling]: + ) -> Union[tuple, TFBaseModelOutputWithPooling]: outputs = self.mobilebert( input_ids=input_ids, attention_mask=attention_mask, @@ -1258,7 +1258,7 @@ class TFMobileBertForPreTraining(TFMobileBertPreTrainedModel, TFMobileBertPreTra labels: np.ndarray | tf.Tensor | None = None, next_sentence_label: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFMobileBertForPreTrainingOutput]: + ) -> Union[tuple, TFMobileBertForPreTrainingOutput]: r""" Return: @@ -1374,7 +1374,7 @@ class TFMobileBertForMaskedLM(TFMobileBertPreTrainedModel, TFMaskedLanguageModel return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFMaskedLMOutput]: + ) -> Union[tuple, TFMaskedLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1476,7 +1476,7 @@ class TFMobileBertForNextSentencePrediction(TFMobileBertPreTrainedModel, TFNextS return_dict: Optional[bool] = None, next_sentence_label: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFNextSentencePredictorOutput]: + ) -> Union[tuple, TFNextSentencePredictorOutput]: r""" Return: @@ -1592,7 +1592,7 @@ class TFMobileBertForSequenceClassification(TFMobileBertPreTrainedModel, TFSeque return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSequenceClassifierOutput]: + ) -> Union[tuple, TFSequenceClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1693,7 +1693,7 @@ class TFMobileBertForQuestionAnswering(TFMobileBertPreTrainedModel, TFQuestionAn start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFQuestionAnsweringModelOutput]: + ) -> Union[tuple, TFQuestionAnsweringModelOutput]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. @@ -1801,7 +1801,7 @@ class TFMobileBertForMultipleChoice(TFMobileBertPreTrainedModel, TFMultipleChoic return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFMultipleChoiceModelOutput]: + ) -> Union[tuple, TFMultipleChoiceModelOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1919,7 +1919,7 @@ class TFMobileBertForTokenClassification(TFMobileBertPreTrainedModel, TFTokenCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFTokenClassifierOutput]: + ) -> Union[tuple, TFTokenClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. diff --git a/src/transformers/models/mobilebert/tokenization_mobilebert.py b/src/transformers/models/mobilebert/tokenization_mobilebert.py index 6b1db74ffba..5845c8e95b3 100644 --- a/src/transformers/models/mobilebert/tokenization_mobilebert.py +++ b/src/transformers/models/mobilebert/tokenization_mobilebert.py @@ -18,7 +18,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -188,8 +188,8 @@ class MobileBertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A MobileBERT sequence has the following format: @@ -213,8 +213,8 @@ class MobileBertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -240,7 +240,7 @@ class MobileBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/mobilebert/tokenization_mobilebert_fast.py b/src/transformers/models/mobilebert/tokenization_mobilebert_fast.py index d745868c972..0f97ddbbb70 100644 --- a/src/transformers/models/mobilebert/tokenization_mobilebert_fast.py +++ b/src/transformers/models/mobilebert/tokenization_mobilebert_fast.py @@ -16,7 +16,7 @@ """Tokenization classes for MobileBERT.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -140,7 +140,7 @@ class MobileBertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py b/src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py index 2342979f0de..449b0b6f7bc 100644 --- a/src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py +++ b/src/transformers/models/mobilenet_v1/image_processing_mobilenet_v1.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for MobileNetV1.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -53,7 +53,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -64,7 +64,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size when applying center-cropping. Only has an effect if `do_center_crop` is set to `True`. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -76,10 +76,10 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): do_normalize: Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -89,15 +89,15 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -120,7 +120,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -133,7 +133,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -171,15 +171,15 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -193,7 +193,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -201,7 +201,7 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -209,9 +209,9 @@ class MobileNetV1ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py index e6b909a8e49..c8887ab836b 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py +++ b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for MobileNetV2.""" -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -59,7 +59,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -70,7 +70,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Desired output size when applying center-cropping. Only has an effect if `do_center_crop` is set to `True`. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -82,10 +82,10 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): do_normalize: Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -95,15 +95,15 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -126,7 +126,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -139,7 +139,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -177,15 +177,15 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -199,7 +199,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -207,7 +207,7 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -215,9 +215,9 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: @@ -309,19 +309,19 @@ class MobileNetV2ImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) # Copied from transformers.models.beit.image_processing_beit.BeitImageProcessor.post_process_semantic_segmentation with Beit->MobileNetV2 - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`MobileNetV2ForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`MobileNetV2ForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2_fast.py b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2_fast.py index 3edcd2d7b15..59eb43917d4 100644 --- a/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2_fast.py +++ b/src/transformers/models/mobilenet_v2/image_processing_mobilenet_v2_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for MobileNetV2.""" -from typing import List, Optional, Tuple +from typing import Optional from ...image_processing_utils_fast import BaseImageProcessorFast from ...image_utils import IMAGENET_STANDARD_MEAN, IMAGENET_STANDARD_STD, PILImageResampling @@ -39,19 +39,19 @@ class MobileNetV2ImageProcessorFast(BaseImageProcessorFast): do_normalize = True do_convert_rgb = None - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`MobileNetV2ForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`MobileNetV2ForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/mobilevit/configuration_mobilevit.py b/src/transformers/models/mobilevit/configuration_mobilevit.py index 7f3cd6381b8..1bb22e1590b 100644 --- a/src/transformers/models/mobilevit/configuration_mobilevit.py +++ b/src/transformers/models/mobilevit/configuration_mobilevit.py @@ -44,9 +44,9 @@ class MobileViTConfig(PretrainedConfig): The size (resolution) of each image. patch_size (`int`, *optional*, defaults to 2): The size (resolution) of each patch. - hidden_sizes (`List[int]`, *optional*, defaults to `[144, 192, 240]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[144, 192, 240]`): Dimensionality (hidden size) of the Transformer encoders at each stage. - neck_hidden_sizes (`List[int]`, *optional*, defaults to `[16, 32, 64, 96, 128, 160, 640]`): + neck_hidden_sizes (`list[int]`, *optional*, defaults to `[16, 32, 64, 96, 128, 160, 640]`): The number of channels for the feature maps of the backbone. num_attention_heads (`int`, *optional*, defaults to 4): Number of attention heads for each attention layer in the Transformer encoder. @@ -74,7 +74,7 @@ class MobileViTConfig(PretrainedConfig): Whether to add a bias to the queries, keys and values. aspp_out_channels (`int`, *optional*, defaults to 256): Number of output channels used in the ASPP layer for semantic segmentation. - atrous_rates (`List[int]`, *optional*, defaults to `[6, 12, 18]`): + atrous_rates (`list[int]`, *optional*, defaults to `[6, 12, 18]`): Dilation (atrous) factors used in the ASPP layer for semantic segmentation. aspp_dropout_prob (`float`, *optional*, defaults to 0.1): The dropout ratio for the ASPP layer for semantic segmentation. diff --git a/src/transformers/models/mobilevit/image_processing_mobilevit.py b/src/transformers/models/mobilevit/image_processing_mobilevit.py index c23de20ee52..b9eb353a654 100644 --- a/src/transformers/models/mobilevit/image_processing_mobilevit.py +++ b/src/transformers/models/mobilevit/image_processing_mobilevit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for MobileViT.""" -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -61,7 +61,7 @@ class MobileViTImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Controls the size of the output image after resizing. Can be overridden by the `size` parameter in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): @@ -77,7 +77,7 @@ class MobileViTImageProcessor(BaseImageProcessor): Whether to crop the input at the center. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 256, "width": 256}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 256, "width": 256}`): Desired output size `(size["height"], size["width"])` when applying center-cropping. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_flip_channel_order (`bool`, *optional*, defaults to `True`): @@ -90,12 +90,12 @@ class MobileViTImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_flip_channel_order: bool = True, **kwargs, ) -> None: @@ -118,7 +118,7 @@ class MobileViTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -131,7 +131,7 @@ class MobileViTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): Resampling filter to use when resiizing the image. @@ -199,10 +199,10 @@ class MobileViTImageProcessor(BaseImageProcessor): do_rescale: bool, do_center_crop: bool, do_flip_channel_order: bool, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, rescale_factor: Optional[float] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_resize: @@ -223,12 +223,12 @@ class MobileViTImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_flip_channel_order: Optional[bool] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -265,9 +265,9 @@ class MobileViTImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: """Preprocesses a single mask.""" @@ -305,12 +305,12 @@ class MobileViTImageProcessor(BaseImageProcessor): images: ImageInput, segmentation_maps: Optional[ImageInput] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_flip_channel_order: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -327,7 +327,7 @@ class MobileViTImageProcessor(BaseImageProcessor): Segmentation map to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only @@ -338,7 +338,7 @@ class MobileViTImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop if `do_center_crop` is set to `True`. do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): Whether to flip the channel order of the image. @@ -440,19 +440,19 @@ class MobileViTImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) # Copied from transformers.models.beit.image_processing_beit.BeitImageProcessor.post_process_semantic_segmentation with Beit->MobileViT - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`MobileViTForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`MobileViTForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/mobilevit/modeling_mobilevit.py b/src/transformers/models/mobilevit/modeling_mobilevit.py index 459bb613917..149eb9261ef 100755 --- a/src/transformers/models/mobilevit/modeling_mobilevit.py +++ b/src/transformers/models/mobilevit/modeling_mobilevit.py @@ -17,7 +17,7 @@ """PyTorch MobileViT model.""" import math -from typing import Dict, Optional, Set, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -260,7 +260,7 @@ class MobileViTAttention(nn.Module): self.output = MobileViTSelfOutput(config, hidden_size) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -413,7 +413,7 @@ class MobileViTLayer(nn.Module): config, in_channels=2 * in_channels, out_channels=in_channels, kernel_size=config.conv_kernel_size ) - def unfolding(self, features: torch.Tensor) -> Tuple[torch.Tensor, Dict]: + def unfolding(self, features: torch.Tensor) -> tuple[torch.Tensor, dict]: patch_width, patch_height = self.patch_width, self.patch_height patch_area = int(patch_width * patch_height) @@ -464,7 +464,7 @@ class MobileViTLayer(nn.Module): } return patches, info_dict - def folding(self, patches: torch.Tensor, info_dict: Dict) -> torch.Tensor: + def folding(self, patches: torch.Tensor, info_dict: dict) -> torch.Tensor: patch_width, patch_height = self.patch_width, self.patch_height patch_area = int(patch_width * patch_height) diff --git a/src/transformers/models/mobilevit/modeling_tf_mobilevit.py b/src/transformers/models/mobilevit/modeling_tf_mobilevit.py index d19470c68f9..4acf1ff013a 100644 --- a/src/transformers/models/mobilevit/modeling_tf_mobilevit.py +++ b/src/transformers/models/mobilevit/modeling_tf_mobilevit.py @@ -18,7 +18,7 @@ from __future__ import annotations -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -555,7 +555,7 @@ class TFMobileViTLayer(keras.layers.Layer): ) self.hidden_size = hidden_size - def unfolding(self, features: tf.Tensor) -> Tuple[tf.Tensor, Dict]: + def unfolding(self, features: tf.Tensor) -> tuple[tf.Tensor, dict]: patch_width, patch_height = self.patch_width, self.patch_height patch_area = tf.cast(patch_width * patch_height, "int32") @@ -599,7 +599,7 @@ class TFMobileViTLayer(keras.layers.Layer): } return patches, info_dict - def folding(self, patches: tf.Tensor, info_dict: Dict) -> tf.Tensor: + def folding(self, patches: tf.Tensor, info_dict: dict) -> tf.Tensor: patch_width, patch_height = self.patch_width, self.patch_height patch_area = int(patch_width * patch_height) @@ -833,7 +833,7 @@ class TFMobileViTMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutputWithPooling]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutputWithPooling]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -960,7 +960,7 @@ MOBILEVIT_START_DOCSTRING = r""" MOBILEVIT_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]`, `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]`, `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`MobileViTImageProcessor.__call__`] for details. @@ -1001,7 +1001,7 @@ class TFMobileViTModel(TFMobileViTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutputWithPooling]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutputWithPooling]: output = self.mobilevit(pixel_values, output_hidden_states, return_dict, training=training) return output diff --git a/src/transformers/models/mobilevitv2/configuration_mobilevitv2.py b/src/transformers/models/mobilevitv2/configuration_mobilevitv2.py index 49c3e63c538..dc9db247602 100644 --- a/src/transformers/models/mobilevitv2/configuration_mobilevitv2.py +++ b/src/transformers/models/mobilevitv2/configuration_mobilevitv2.py @@ -60,15 +60,15 @@ class MobileViTV2Config(PretrainedConfig): The epsilon used by the layer normalization layers. aspp_out_channels (`int`, *optional*, defaults to 512): Number of output channels used in the ASPP layer for semantic segmentation. - atrous_rates (`List[int]`, *optional*, defaults to `[6, 12, 18]`): + atrous_rates (`list[int]`, *optional*, defaults to `[6, 12, 18]`): Dilation (atrous) factors used in the ASPP layer for semantic segmentation. aspp_dropout_prob (`float`, *optional*, defaults to 0.1): The dropout ratio for the ASPP layer for semantic segmentation. semantic_loss_ignore_index (`int`, *optional*, defaults to 255): The index that is ignored by the loss function of the semantic segmentation model. - n_attn_blocks (`List[int]`, *optional*, defaults to `[2, 4, 3]`): + n_attn_blocks (`list[int]`, *optional*, defaults to `[2, 4, 3]`): The number of attention blocks in each MobileViTV2Layer - base_attn_unit_dims (`List[int]`, *optional*, defaults to `[128, 192, 256]`): + base_attn_unit_dims (`list[int]`, *optional*, defaults to `[128, 192, 256]`): The base multiplier for dimensions of attention blocks in each MobileViTV2Layer width_multiplier (`float`, *optional*, defaults to 1.0): The width multiplier for MobileViTV2. diff --git a/src/transformers/models/mobilevitv2/modeling_mobilevitv2.py b/src/transformers/models/mobilevitv2/modeling_mobilevitv2.py index b60177abf31..868c595dbac 100644 --- a/src/transformers/models/mobilevitv2/modeling_mobilevitv2.py +++ b/src/transformers/models/mobilevitv2/modeling_mobilevitv2.py @@ -16,7 +16,7 @@ # Original license: https://github.com/apple/ml-cvnets/blob/main/LICENSE """PyTorch MobileViTV2 model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -417,7 +417,7 @@ class MobileViTV2Layer(nn.Module): use_activation=False, ) - def unfolding(self, feature_map: torch.Tensor) -> Tuple[torch.Tensor, Tuple[int, int]]: + def unfolding(self, feature_map: torch.Tensor) -> tuple[torch.Tensor, tuple[int, int]]: batch_size, in_channels, img_height, img_width = feature_map.shape patches = nn.functional.unfold( feature_map, @@ -428,7 +428,7 @@ class MobileViTV2Layer(nn.Module): return patches, (img_height, img_width) - def folding(self, patches: torch.Tensor, output_size: Tuple[int, int]) -> torch.Tensor: + def folding(self, patches: torch.Tensor, output_size: tuple[int, int]) -> torch.Tensor: batch_size, in_dim, patch_size, n_patches = patches.shape patches = patches.reshape(batch_size, in_dim * patch_size, n_patches) diff --git a/src/transformers/models/modernbert/modeling_modernbert.py b/src/transformers/models/modernbert/modeling_modernbert.py index 0b254bd73a2..d984c523d0c 100644 --- a/src/transformers/models/modernbert/modeling_modernbert.py +++ b/src/transformers/models/modernbert/modeling_modernbert.py @@ -21,7 +21,7 @@ import math from contextlib import nullcontext -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -165,7 +165,7 @@ class ModernBertUnpaddedRotaryEmbedding(RotaryEmbedding): qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: Optional[int] = None, - ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]: """ Apply rotary embedding *inplace* to qkv. qkv: (total_nnz, 3, nheads, headdim) @@ -312,12 +312,12 @@ def eager_attention_forward( attention_mask: torch.Tensor, sliding_window_mask: torch.Tensor, position_ids: Optional[torch.LongTensor], - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, output_attentions: Optional[bool] = False, **_kwargs, -) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: +) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: # qkv: [batch_size, seqlen, 3, nheads, headdim] cos, sin = module.rotary_emb(qkv, position_ids=position_ids) query, key, value = qkv.transpose(3, 1).unbind(dim=2) @@ -349,12 +349,12 @@ def flash_attention_forward( rotary_emb: ModernBertUnpaddedRotaryEmbedding, cu_seqlens: torch.Tensor, max_seqlen: int, - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, target_dtype: torch.dtype = torch.bfloat16, **_kwargs, -) -> Tuple[torch.Tensor]: +) -> tuple[torch.Tensor]: # (total_seqlen, 3, nheads, headdim) qkv = rotary_emb(qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen) @@ -392,11 +392,11 @@ def sdpa_attention_forward( attention_mask: torch.Tensor, sliding_window_mask: torch.Tensor, position_ids: Optional[torch.LongTensor], - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, **_kwargs, -) -> Tuple[torch.Tensor]: +) -> tuple[torch.Tensor]: # qkv: [batch_size, seqlen, 3, nheads, headdim] cos, sin = module.rotary_emb(qkv, position_ids=position_ids) query, key, value = qkv.transpose(3, 1).unbind(dim=2) @@ -616,7 +616,7 @@ class ModernBertPreTrainedModel(PreTrainedModel): cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, check_device_map: bool = True, ): # If the user didn't specify anything, try to use flash_attention_2 if available. @@ -691,7 +691,7 @@ def _unpad_modernbert_input( attention_mask: torch.Tensor, position_ids: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, int, Optional[torch.Tensor], Optional[torch.Tensor]]: +) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, int, Optional[torch.Tensor], Optional[torch.Tensor]]: """ Remove padding from input sequences. @@ -793,7 +793,7 @@ class ModernBertModel(ModernBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutput]: + ) -> Union[tuple[torch.Tensor, ...], BaseModelOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1005,7 +1005,7 @@ class ModernBertForMaskedLM(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1137,7 +1137,7 @@ class ModernBertForSequenceClassification(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1259,7 +1259,7 @@ class ModernBertForTokenClassification(ModernBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1350,7 +1350,7 @@ class ModernBertForQuestionAnswering(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers diff --git a/src/transformers/models/modernbert/modular_modernbert.py b/src/transformers/models/modernbert/modular_modernbert.py index 18f2bb8beb7..ff46a523a6c 100644 --- a/src/transformers/models/modernbert/modular_modernbert.py +++ b/src/transformers/models/modernbert/modular_modernbert.py @@ -15,7 +15,7 @@ import math from contextlib import nullcontext -from typing import Dict, Literal, Optional, Tuple, Union +from typing import Literal, Optional, Union import torch import torch.nn.functional as F @@ -251,7 +251,7 @@ def _unpad_modernbert_input( attention_mask: torch.Tensor, position_ids: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, -) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, int, Optional[torch.Tensor], Optional[torch.Tensor]]: +) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, int, Optional[torch.Tensor], Optional[torch.Tensor]]: """ Remove padding from input sequences. @@ -428,7 +428,7 @@ class ModernBertUnpaddedRotaryEmbedding(RotaryEmbedding): qkv: torch.Tensor, cu_seqlens: torch.Tensor, max_seqlen: Optional[int] = None, - ) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]: + ) -> Union[torch.Tensor, tuple[torch.Tensor, torch.Tensor]]: """ Apply rotary embedding *inplace* to qkv. qkv: (total_nnz, 3, nheads, headdim) @@ -514,12 +514,12 @@ def eager_attention_forward( attention_mask: torch.Tensor, sliding_window_mask: torch.Tensor, position_ids: Optional[torch.LongTensor], - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, output_attentions: Optional[bool] = False, **_kwargs, -) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: +) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: # qkv: [batch_size, seqlen, 3, nheads, headdim] cos, sin = module.rotary_emb(qkv, position_ids=position_ids) query, key, value = qkv.transpose(3, 1).unbind(dim=2) @@ -551,12 +551,12 @@ def flash_attention_forward( rotary_emb: ModernBertUnpaddedRotaryEmbedding, cu_seqlens: torch.Tensor, max_seqlen: int, - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, target_dtype: torch.dtype = torch.bfloat16, **_kwargs, -) -> Tuple[torch.Tensor]: +) -> tuple[torch.Tensor]: # (total_seqlen, 3, nheads, headdim) qkv = rotary_emb(qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen) @@ -594,11 +594,11 @@ def sdpa_attention_forward( attention_mask: torch.Tensor, sliding_window_mask: torch.Tensor, position_ids: Optional[torch.LongTensor], - local_attention: Tuple[int, int], + local_attention: tuple[int, int], bs: int, dim: int, **_kwargs, -) -> Tuple[torch.Tensor]: +) -> tuple[torch.Tensor]: # qkv: [batch_size, seqlen, 3, nheads, headdim] cos, sin = module.rotary_emb(qkv, position_ids=position_ids) query, key, value = qkv.transpose(3, 1).unbind(dim=2) @@ -818,7 +818,7 @@ class ModernBertPreTrainedModel(PreTrainedModel): cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, check_device_map: bool = True, ): # If the user didn't specify anything, try to use flash_attention_2 if available. @@ -923,7 +923,7 @@ class ModernBertModel(ModernBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutput]: + ) -> Union[tuple[torch.Tensor, ...], BaseModelOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1135,7 +1135,7 @@ class ModernBertForMaskedLM(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1267,7 +1267,7 @@ class ModernBertForSequenceClassification(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1389,7 +1389,7 @@ class ModernBertForTokenClassification(ModernBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers @@ -1480,7 +1480,7 @@ class ModernBertForQuestionAnswering(ModernBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" sliding_window_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Mask to avoid performing attention on padding or far-away tokens. In ModernBert, only every few layers diff --git a/src/transformers/models/moonshine/configuration_moonshine.py b/src/transformers/models/moonshine/configuration_moonshine.py index ef771269175..270a2e3e484 100644 --- a/src/transformers/models/moonshine/configuration_moonshine.py +++ b/src/transformers/models/moonshine/configuration_moonshine.py @@ -108,11 +108,11 @@ class MoonshineConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/moonshine/modeling_moonshine.py b/src/transformers/models/moonshine/modeling_moonshine.py index 0987bf2d0f2..d8f61fbf502 100644 --- a/src/transformers/models/moonshine/modeling_moonshine.py +++ b/src/transformers/models/moonshine/modeling_moonshine.py @@ -18,7 +18,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -209,13 +209,13 @@ class MoonshineAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, key_value_states: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len = hidden_states.shape[:-1] query_states = ( @@ -354,9 +354,9 @@ class MoonshineEncoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -424,10 +424,10 @@ class MoonshineDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - encoder_position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + encoder_position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -562,7 +562,7 @@ class MoonshineEncoder(MoonshinePreTrainedModel): Args: input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -694,7 +694,7 @@ class MoonshineDecoder(MoonshinePreTrainedModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention @@ -817,7 +817,7 @@ class MoonshineDecoder(MoonshinePreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1015,10 +1015,10 @@ class MoonshineModel(MoonshinePreTrainedModel): attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1027,7 +1027,7 @@ class MoonshineModel(MoonshinePreTrainedModel): r""" input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -1190,10 +1190,10 @@ class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixi attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1203,7 +1203,7 @@ class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixi r""" input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. diff --git a/src/transformers/models/moonshine/modular_moonshine.py b/src/transformers/models/moonshine/modular_moonshine.py index 7b400ab5b9c..4ee7cd81f77 100644 --- a/src/transformers/models/moonshine/modular_moonshine.py +++ b/src/transformers/models/moonshine/modular_moonshine.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -130,11 +130,11 @@ class MoonshineConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -305,13 +305,13 @@ class MoonshineAttention(GlmAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, key_value_states: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len = hidden_states.shape[:-1] query_states = ( @@ -448,10 +448,10 @@ class MoonshineDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 - encoder_position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # will become mandatory in v4.46 + encoder_position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -586,7 +586,7 @@ class MoonshineEncoder(MoonshinePreTrainedModel): Args: input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -700,7 +700,7 @@ class MoonshineDecoder(LlamaModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, **flash_attn_kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention @@ -831,10 +831,10 @@ class MoonshineModel(WhisperModel): attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -843,7 +843,7 @@ class MoonshineModel(WhisperModel): r""" input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -990,10 +990,10 @@ class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixi attention_mask: Optional[torch.LongTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1003,7 +1003,7 @@ class MoonshineForConditionalGeneration(MoonshinePreTrainedModel, GenerationMixi r""" input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`): Float values of the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoFeatureExtractor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. diff --git a/src/transformers/models/moshi/modeling_moshi.py b/src/transformers/models/moshi/modeling_moshi.py index 50608d609cc..c0ef9c00147 100644 --- a/src/transformers/models/moshi/modeling_moshi.py +++ b/src/transformers/models/moshi/modeling_moshi.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn as nn @@ -90,12 +90,12 @@ class MoshiConditionalGenerationGenerateOutput(ModelOutput): audio_sequences: Optional[torch.Tensor] = None sequences: Optional[torch.LongTensor] = None sequences_scores: Optional[torch.FloatTensor] = None - scores: Optional[Tuple[torch.FloatTensor]] = None - logits: Optional[Tuple[torch.FloatTensor]] = None + scores: Optional[tuple[torch.FloatTensor]] = None + logits: Optional[tuple[torch.FloatTensor]] = None beam_indices: Optional[torch.LongTensor] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - past_key_values: Optional[Tuple[Tuple[Tuple[torch.FloatTensor]]]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[tuple[torch.FloatTensor]]] = None + past_key_values: Optional[tuple[tuple[tuple[torch.FloatTensor]]]] = None audio_codes: Optional[torch.LongTensor] = None @@ -133,9 +133,9 @@ class MoshiCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -183,14 +183,14 @@ class MoshiConditionalGenerationOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None depth_loss: Optional[torch.FloatTensor] = None audio_logits: Optional[torch.FloatTensor] = None - depth_past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - depth_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - depth_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + depth_past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + depth_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + depth_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -463,7 +463,7 @@ class MoshiAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states, cache_position) # Ignore copy @@ -544,7 +544,7 @@ class MoshiFlashAttention2(MoshiAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -655,7 +655,7 @@ class MoshiSdpaAttention(MoshiAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -764,7 +764,7 @@ class MoshiDecoderLayer(nn.Module): use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -887,7 +887,7 @@ class MoshiDepthDecoder(MoshiPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, last_hidden_state: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.BoolTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -896,7 +896,7 @@ class MoshiDepthDecoder(MoshiPreTrainedModel, GenerationMixin): position_ids: Optional[torch.LongTensor] = None, labels: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: """ Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1274,14 +1274,14 @@ class MoshiModel(MoshiPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1591,7 +1591,7 @@ class MoshiForCausalLM(MoshiPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1601,7 +1601,7 @@ class MoshiForCausalLM(MoshiPreTrainedModel, GenerationMixin): labels: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs, - ) -> Union[Tuple, MoshiCausalLMOutputWithPast]: + ) -> Union[tuple, MoshiCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1734,7 +1734,7 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): user_audio_codes: Optional[torch.Tensor] = None, moshi_input_values: Optional[torch.FloatTensor] = None, moshi_audio_codes: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, text_labels: Optional[torch.LongTensor] = None, audio_labels: Optional[torch.LongTensor] = None, @@ -1743,7 +1743,7 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" user_input_values (`torch.Tensor `of shape `(batch_size, 1, audio_sequence_length), *optional*): The audio waveforms used as audio user prompt for the generation. @@ -1906,7 +1906,7 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): self, input_ids: torch.LongTensor, generation_config: GenerationConfig, - kwargs: Dict[str, Any], + kwargs: dict[str, Any], ) -> torch.LongTensor: pad_token_id = generation_config.pad_token_id eos_token_id = generation_config.eos_token_id @@ -2068,7 +2068,7 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): If `True`, will also returns the generated audio codes, i.e the intermediate audio "tokens" which transforms to `audio_sequences` once passed through the audio decoder. concat_unconditional_inputs (`bool`, *optional*, defaults to `True`): If `False`, won't concatenate initial audio and text tokens. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Remaining dictionary of keyword arguments that are passed to the `generate` method. Refers to the original [`generate` docstrings](https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.GenerationMixin.generate) for more information on how to use them. @@ -2394,10 +2394,10 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): def _update_model_kwargs_for_generation( self, outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], is_encoder_decoder: bool = False, num_new_tokens: int = 1, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: model_kwargs = super()._update_model_kwargs_for_generation( outputs, model_kwargs, is_encoder_decoder, num_new_tokens ) @@ -2596,8 +2596,8 @@ class MoshiForConditionalGeneration(MoshiPreTrainedModel, GenerationMixin): @staticmethod def _reorder_cache( - past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor - ) -> Tuple[Tuple[torch.Tensor]]: + past_key_values: tuple[tuple[torch.Tensor]], beam_idx: torch.Tensor + ) -> tuple[tuple[torch.Tensor]]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct diff --git a/src/transformers/models/mpnet/modeling_mpnet.py b/src/transformers/models/mpnet/modeling_mpnet.py index 1e1a18c5596..efa74f191f1 100644 --- a/src/transformers/models/mpnet/modeling_mpnet.py +++ b/src/transformers/models/mpnet/modeling_mpnet.py @@ -16,7 +16,7 @@ """PyTorch MPNet model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -454,7 +454,7 @@ class MPNetModel(MPNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -532,7 +532,7 @@ class MPNetForMaskedLM(MPNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -629,7 +629,7 @@ class MPNetForSequenceClassification(MPNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -710,7 +710,7 @@ class MPNetForMultipleChoice(MPNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -804,7 +804,7 @@ class MPNetForTokenClassification(MPNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -889,7 +889,7 @@ class MPNetForQuestionAnswering(MPNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.mpnet( diff --git a/src/transformers/models/mpnet/modeling_tf_mpnet.py b/src/transformers/models/mpnet/modeling_tf_mpnet.py index 6c9dd5bbd05..2ce85a5424f 100644 --- a/src/transformers/models/mpnet/modeling_tf_mpnet.py +++ b/src/transformers/models/mpnet/modeling_tf_mpnet.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -773,7 +773,7 @@ class TFMPNetModel(TFMPNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: outputs = self.mpnet( input_ids=input_ids, attention_mask=attention_mask, @@ -892,7 +892,7 @@ class TFMPNetForMaskedLM(TFMPNetPreTrainedModel, TFMaskedLanguageModelingLoss): return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1011,7 +1011,7 @@ class TFMPNetForSequenceClassification(TFMPNetPreTrainedModel, TFSequenceClassif return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1095,7 +1095,7 @@ class TFMPNetForMultipleChoice(TFMPNetPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1196,7 +1196,7 @@ class TFMPNetForTokenClassification(TFMPNetPreTrainedModel, TFTokenClassificatio return_dict: Optional[bool] = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1283,7 +1283,7 @@ class TFMPNetForQuestionAnswering(TFMPNetPreTrainedModel, TFQuestionAnsweringLos end_positions: tf.Tensor | None = None, training: bool = False, **kwargs, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/mpnet/tokenization_mpnet.py b/src/transformers/models/mpnet/tokenization_mpnet.py index 0aca1cf2614..42eb7d68e16 100644 --- a/src/transformers/models/mpnet/tokenization_mpnet.py +++ b/src/transformers/models/mpnet/tokenization_mpnet.py @@ -18,7 +18,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -219,8 +219,8 @@ class MPNetTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A MPNet sequence has the following format: @@ -229,13 +229,13 @@ class MPNetTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -244,22 +244,22 @@ class MPNetTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Set to True if the token list is already formatted with special tokens for the model Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -271,20 +271,20 @@ class MPNetTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. MPNet does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -293,7 +293,7 @@ class MPNetTokenizer(PreTrainedTokenizer): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/mpnet/tokenization_mpnet_fast.py b/src/transformers/models/mpnet/tokenization_mpnet_fast.py index 4a0c59d1e4d..1a470565a84 100644 --- a/src/transformers/models/mpnet/tokenization_mpnet_fast.py +++ b/src/transformers/models/mpnet/tokenization_mpnet_fast.py @@ -16,7 +16,7 @@ """Fast Tokenization classes for MPNet.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -179,20 +179,20 @@ class MPNetTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. MPNet does not make use of token type ids, therefore a list of zeros is returned Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -201,7 +201,7 @@ class MPNetTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/mpt/modeling_mpt.py b/src/transformers/models/mpt/modeling_mpt.py index 5f7fd05cf8e..79ec42e2b8d 100644 --- a/src/transformers/models/mpt/modeling_mpt.py +++ b/src/transformers/models/mpt/modeling_mpt.py @@ -15,7 +15,7 @@ """PyTorch MPT model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -87,7 +87,7 @@ class MptAttention(nn.Module): self, hidden_states: torch.Tensor, position_bias: torch.Tensor, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, ): batch_size, seq_length = hidden_states.shape[:2] @@ -186,7 +186,7 @@ class MptBlock(nn.Module): hidden_states: torch.Tensor, position_bias: torch.Tensor, attention_mask: torch.Tensor, - layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + layer_past: Optional[tuple[torch.Tensor, torch.Tensor]] = None, use_cache: bool = False, output_attentions: bool = False, ): @@ -254,8 +254,8 @@ class MptPreTrainedModel(PreTrainedModel): @staticmethod def _convert_to_mpt_cache( - past_key_value: Tuple[Tuple[torch.Tensor, torch.Tensor]], - ) -> Tuple[Tuple[torch.Tensor, torch.Tensor]]: + past_key_value: tuple[tuple[torch.Tensor, torch.Tensor]], + ) -> tuple[tuple[torch.Tensor, torch.Tensor]]: """ Converts the cache to the format expected by Mpt, i.e. to tuple(tuple([batch_size * num_heads, ...])) """ @@ -309,7 +309,7 @@ class MptModel(MptPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -317,7 +317,7 @@ class MptModel(MptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # NOOP kwargs, for now - ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor, ...], BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -459,7 +459,7 @@ class MptForCausalLM(MptPreTrainedModel, GenerationMixin): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -468,7 +468,7 @@ class MptForCausalLM(MptPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -527,8 +527,8 @@ class MptForCausalLM(MptPreTrainedModel, GenerationMixin): ) def _reorder_cache( - self, past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor - ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]: + self, past: tuple[tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor + ) -> tuple[tuple[torch.Tensor, torch.Tensor], ...]: """ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct @@ -578,7 +578,7 @@ class MptForSequenceClassification(MptPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -586,7 +586,7 @@ class MptForSequenceClassification(MptPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutputWithPast]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -701,7 +701,7 @@ class MptForTokenClassification(MptPreTrainedModel): def forward( self, input_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor, torch.Tensor], ...]] = None, attention_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -710,7 +710,7 @@ class MptForTokenClassification(MptPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **deprecated_arguments, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` @@ -788,7 +788,7 @@ class MptForQuestionAnswering(MptPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else `past_key_values[0][0].shape[2]` diff --git a/src/transformers/models/mra/modeling_mra.py b/src/transformers/models/mra/modeling_mra.py index ec793d62c16..7501c4d8306 100644 --- a/src/transformers/models/mra/modeling_mra.py +++ b/src/transformers/models/mra/modeling_mra.py @@ -16,7 +16,7 @@ import math from pathlib import Path -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -874,7 +874,7 @@ class MraModel(MraPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithCrossAttentions]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -973,7 +973,7 @@ class MraForMaskedLM(MraPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1063,7 +1063,7 @@ class MraForSequenceClassification(MraPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1144,7 +1144,7 @@ class MraForMultipleChoice(MraPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1249,7 +1249,7 @@ class MraForTokenClassification(MraPreTrainedModel): labels: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1325,7 +1325,7 @@ class MraForQuestionAnswering(MraPreTrainedModel): end_positions: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.mra( diff --git a/src/transformers/models/mt5/modeling_mt5.py b/src/transformers/models/mt5/modeling_mt5.py index 1d31d9ebb89..8596fbeb4f9 100644 --- a/src/transformers/models/mt5/modeling_mt5.py +++ b/src/transformers/models/mt5/modeling_mt5.py @@ -18,7 +18,7 @@ import copy import math import os import warnings -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -73,7 +73,7 @@ PARALLELIZE_DOCSTRING = r""" it will evenly distribute blocks across all devices. Args: - device_map (`Dict[int, list]`, *optional*): + device_map (`dict[int, list]`, *optional*): A dictionary that maps attention modules to devices. Note that the embedding module and LMHead are always automatically mapped to the first device (for esoteric reasons). That means that the first device should have fewer attention modules mapped to it than other devices. For reference, the mt5 models have the @@ -1436,8 +1436,8 @@ class MT5Model(MT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1445,7 +1445,7 @@ class MT5Model(MT5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. MT5 is a model with relative position embeddings so you @@ -1708,8 +1708,8 @@ class MT5ForConditionalGeneration(MT5PreTrainedModel, GenerationMixin): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1718,7 +1718,7 @@ class MT5ForConditionalGeneration(MT5PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. MT5 is a model with relative position embeddings so you @@ -2028,7 +2028,7 @@ class MT5EncoderModel(MT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. MT5 is a model with relative position embeddings so you @@ -2099,7 +2099,7 @@ class MT5ForSequenceClassification(MT5PreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2107,7 +2107,7 @@ class MT5ForSequenceClassification(MT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. MT5 is a model with relative position embeddings so you @@ -2265,7 +2265,7 @@ class MT5ForTokenClassification(MT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. MT5 is a model with relative position embeddings so you @@ -2374,7 +2374,7 @@ class MT5ForQuestionAnswering(MT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -2383,7 +2383,7 @@ class MT5ForQuestionAnswering(MT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you diff --git a/src/transformers/models/musicgen/convert_musicgen_transformers.py b/src/transformers/models/musicgen/convert_musicgen_transformers.py index 4eba8012295..b561a673c24 100644 --- a/src/transformers/models/musicgen/convert_musicgen_transformers.py +++ b/src/transformers/models/musicgen/convert_musicgen_transformers.py @@ -17,7 +17,6 @@ import argparse from collections import OrderedDict from pathlib import Path -from typing import Dict, Tuple import torch from audiocraft.models import MusicGen @@ -68,7 +67,7 @@ def rename_keys(name): return name -def rename_state_dict(state_dict: OrderedDict, hidden_size: int) -> Tuple[Dict, Dict]: +def rename_state_dict(state_dict: OrderedDict, hidden_size: int) -> tuple[dict, dict]: """Function that takes the fairseq Musicgen state dict and renames it according to the HF module names. It further partitions the state dict into the decoder (LM) state dict, and that for the encoder-decoder projection.""" diff --git a/src/transformers/models/musicgen/modeling_musicgen.py b/src/transformers/models/musicgen/modeling_musicgen.py index 9f0ddc6c9db..a7ead0a5146 100644 --- a/src/transformers/models/musicgen/modeling_musicgen.py +++ b/src/transformers/models/musicgen/modeling_musicgen.py @@ -19,7 +19,7 @@ import inspect import math import random from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import torch import torch.nn as nn @@ -72,7 +72,7 @@ logger = logging.get_logger(__name__) class MusicgenUnconditionalInput(ModelOutput): """ Args: - encoder_outputs (`Tuple[torch.FloatTensor]` of length 1, with tensor shape `(batch_size, sequence_length, hidden_size)`): + encoder_outputs (`tuple[torch.FloatTensor]` of length 1, with tensor shape `(batch_size, sequence_length, hidden_size)`): Sequence of hidden-states at the output of the last layer of the text encoder model. attention_mask (`torch.LongTensor`) of shape `(batch_size, sequence_length)`, *optional*): Encoder attention mask to avoid performing attention on padding token indices. Mask values selected in `[0, @@ -82,7 +82,7 @@ class MusicgenUnconditionalInput(ModelOutput): from the prompts) and the unconditional logits (predicted without prompts). """ - encoder_outputs: Tuple[torch.FloatTensor] = None + encoder_outputs: tuple[torch.FloatTensor] = None attention_mask: Optional[torch.LongTensor] = None guidance_scale: Optional[float] = None @@ -220,14 +220,14 @@ class MusicgenAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -346,7 +346,7 @@ class MusicgenDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -497,13 +497,13 @@ class MusicgenDecoder(MusicgenPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -779,13 +779,13 @@ class MusicgenModel(MusicgenPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -904,7 +904,7 @@ class MusicgenForCausalLM(MusicgenPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -912,7 +912,7 @@ class MusicgenForCausalLM(MusicgenPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -1200,7 +1200,7 @@ class MusicgenForCausalLM(MusicgenPreTrainedModel, GenerationMixin): streamer (`BaseStreamer`, *optional*): Streamer object that will be used to stream the generated sequences. Generated tokens are passed through `streamer.put(token_ids)` and the streamer is responsible for any further processing. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. @@ -1707,8 +1707,8 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): padding_mask: Optional[torch.BoolTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1717,7 +1717,7 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -1937,11 +1937,11 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): self, batch_size: int, model_input_name: str, - model_kwargs: Dict[str, torch.Tensor], + model_kwargs: dict[str, torch.Tensor], decoder_start_token_id: Optional[int] = None, bos_token_id: Optional[int] = None, device: torch.device = None, - ) -> Tuple[torch.LongTensor, Dict[str, torch.Tensor]]: + ) -> tuple[torch.LongTensor, dict[str, torch.Tensor]]: """Prepares `decoder_input_ids` for generation with encoder-decoder models""" # 1. Check whether the user has defined `decoder_input_ids` manually. To facilitate in terms of input naming, @@ -1986,7 +1986,7 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): model_kwargs, model_input_name: Optional[str], generation_config: GenerationConfig, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: # 1. get text encoder encoder = self.get_text_encoder() # Compatibility with Accelerate big model inference: we need the encoder to outputs stuff on the same device @@ -2135,7 +2135,7 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, torch.Tensor]] = None, + model_kwargs: Optional[dict[str, torch.Tensor]] = None, ) -> torch.LongTensor: """Initializes input ids for generation, if necessary.""" if inputs is not None: @@ -2160,7 +2160,7 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): return torch.ones((batch_size, 1), dtype=torch.long, device=self.device) * bos_token_id def _get_decoder_start_token_id( - self, decoder_start_token_id: Optional[Union[int, List[int]]] = None, bos_token_id: Optional[int] = None + self, decoder_start_token_id: Optional[Union[int, list[int]]] = None, bos_token_id: Optional[int] = None ) -> int: decoder_start_token_id = ( decoder_start_token_id @@ -2230,7 +2230,7 @@ class MusicgenForConditionalGeneration(PreTrainedModel, GenerationMixin): streamer (`BaseStreamer`, *optional*): Streamer object that will be used to stream the generated sequences. Generated tokens are passed through `streamer.put(token_ids)` and the streamer is responsible for any further processing. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. diff --git a/src/transformers/models/musicgen/processing_musicgen.py b/src/transformers/models/musicgen/processing_musicgen.py index 82671e4bf4c..167c27f47bd 100644 --- a/src/transformers/models/musicgen/processing_musicgen.py +++ b/src/transformers/models/musicgen/processing_musicgen.py @@ -16,7 +16,7 @@ Text/audio processor class for MusicGen """ -from typing import List, Optional +from typing import Optional import numpy as np @@ -113,7 +113,7 @@ class MusicgenProcessor(ProcessorMixin): """ return self.tokenizer.decode(*args, **kwargs) - def _decode_audio(self, audio_values, padding_mask: Optional = None) -> List[np.ndarray]: + def _decode_audio(self, audio_values, padding_mask: Optional = None) -> list[np.ndarray]: """ This method strips any padding from the audio values to return a list of numpy audio arrays. """ diff --git a/src/transformers/models/musicgen_melody/convert_musicgen_melody_transformers.py b/src/transformers/models/musicgen_melody/convert_musicgen_melody_transformers.py index ec22afd82f3..3db5c2c8e33 100644 --- a/src/transformers/models/musicgen_melody/convert_musicgen_melody_transformers.py +++ b/src/transformers/models/musicgen_melody/convert_musicgen_melody_transformers.py @@ -17,7 +17,6 @@ import argparse from collections import OrderedDict from pathlib import Path -from typing import Dict, Tuple import torch from audiocraft.models import MusicGen @@ -73,7 +72,7 @@ def rename_keys(name): return name -def rename_state_dict(state_dict: OrderedDict, hidden_size: int) -> Tuple[Dict, Dict]: +def rename_state_dict(state_dict: OrderedDict, hidden_size: int) -> tuple[dict, dict]: """Function that takes the fairseq MusicgenMelody state dict and renames it according to the HF module names. It further partitions the state dict into the decoder (LM) state dict, and that for the text encoder projection and for the audio encoder projection.""" diff --git a/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py b/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py index f0a7e4dee03..ec23899e91e 100644 --- a/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py +++ b/src/transformers/models/musicgen_melody/feature_extraction_musicgen_melody.py @@ -17,7 +17,7 @@ Feature extractor class for Musicgen Melody """ import copy -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import numpy as np @@ -75,7 +75,7 @@ class MusicgenMelodyFeatureExtractor(SequenceFeatureExtractor): bugs. - stem_indices (`List[int]`, *optional*, defaults to `[3, 2]`): + stem_indices (`list[int]`, *optional*, defaults to `[3, 2]`): Stem channels to extract if demucs outputs are passed. """ @@ -182,7 +182,7 @@ class MusicgenMelodyFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - audio: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + audio: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], truncation: bool = True, pad_to_multiple_of: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -196,7 +196,7 @@ class MusicgenMelodyFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - audio (`torch.Tensor`, `np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[torch.Tensor]`, `List[List[float]]`): + audio (`torch.Tensor`, `np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[torch.Tensor]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a torch tensor, a numpy array, a list of float values, a list of numpy arrays, a list of torch tensors, or a list of list of float values. If `audio` is the output of Demucs, it has to be a torch tensor of shape `(batch_size, num_stems, channel_size, audio_length)`. @@ -315,10 +315,10 @@ class MusicgenMelodyFeatureExtractor(SequenceFeatureExtractor): return padded_inputs - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) output["feature_extractor_type"] = self.__class__.__name__ diff --git a/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py b/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py index 3343cf2333a..a57955a7a70 100644 --- a/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py +++ b/src/transformers/models/musicgen_melody/modeling_musicgen_melody.py @@ -19,7 +19,7 @@ import inspect import math import random from dataclasses import dataclass -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import torch import torch.nn as nn @@ -94,9 +94,9 @@ class MusicgenMelodyOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None encoder_hidden_states: Optional[torch.FloatTensor] = None @@ -236,14 +236,14 @@ class MusicgenMelodyAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -349,7 +349,7 @@ class MusicgenMelodyDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -472,13 +472,13 @@ class MusicgenMelodyDecoder(MusicgenMelodyPreTrainedModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -718,13 +718,13 @@ class MusicgenMelodyModel(MusicgenMelodyPreTrainedModel): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -836,14 +836,14 @@ class MusicgenMelodyForCausalLM(MusicgenMelodyPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, MusicgenMelodyOutputWithPast]: + ) -> Union[tuple, MusicgenMelodyOutputWithPast]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_codebooks, sequence_length)`): Indices of input sequence tokens in the vocabulary, corresponding to the sequence of audio codes. @@ -1136,7 +1136,7 @@ class MusicgenMelodyForCausalLM(MusicgenMelodyPreTrainedModel, GenerationMixin): streamer (`BaseStreamer`, *optional*): Streamer object that will be used to stream the generated sequences. Generated tokens are passed through `streamer.put(token_ids)` and the streamer is responsible for any further processing. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. @@ -1613,7 +1613,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): input_features: Optional[torch.FloatTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1623,7 +1623,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, MusicgenMelodyOutputWithPast]: + ) -> Union[tuple, MusicgenMelodyOutputWithPast]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, audio_sequence_length, num_chroma)`): Input audio features. @@ -1844,11 +1844,11 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): self, batch_size: int, model_input_name: str, - model_kwargs: Dict[str, torch.Tensor], + model_kwargs: dict[str, torch.Tensor], decoder_start_token_id: Optional[int] = None, bos_token_id: Optional[int] = None, device: torch.device = None, - ) -> Tuple[torch.LongTensor, Dict[str, torch.Tensor]]: + ) -> tuple[torch.LongTensor, dict[str, torch.Tensor]]: """Prepares `decoder_input_ids` for generation with encoder-decoder models""" # 1. Check whether the user has defined `decoder_input_ids` manually. To facilitate in terms of input naming, @@ -1893,7 +1893,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): model_kwargs, model_input_name: Optional[str], generation_config: GenerationConfig, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: encoder_hidden_states = None # attention mask is consumed once to produce text conditional hidden states through the text encoder encoder_attention_mask = model_kwargs.pop("attention_mask") @@ -2001,7 +2001,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): self, inputs: Optional[torch.Tensor] = None, bos_token_id: Optional[int] = None, - model_kwargs: Optional[Dict[str, torch.Tensor]] = None, + model_kwargs: Optional[dict[str, torch.Tensor]] = None, ) -> torch.LongTensor: """Initializes input ids for generation, if necessary.""" if inputs is not None: @@ -2037,7 +2037,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): # Copied from transformers.models.musicgen.modeling_musicgen.MusicgenForConditionalGeneration._get_decoder_start_token_id def _get_decoder_start_token_id( - self, decoder_start_token_id: Optional[Union[int, List[int]]] = None, bos_token_id: Optional[int] = None + self, decoder_start_token_id: Optional[Union[int, list[int]]] = None, bos_token_id: Optional[int] = None ) -> int: decoder_start_token_id = ( decoder_start_token_id @@ -2107,7 +2107,7 @@ class MusicgenMelodyForConditionalGeneration(PreTrainedModel, GenerationMixin): streamer (`BaseStreamer`, *optional*): Streamer object that will be used to stream the generated sequences. Generated tokens are passed through `streamer.put(token_ids)` and the streamer is responsible for any further processing. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. diff --git a/src/transformers/models/musicgen_melody/processing_musicgen_melody.py b/src/transformers/models/musicgen_melody/processing_musicgen_melody.py index f42d6af6016..19e99947f07 100644 --- a/src/transformers/models/musicgen_melody/processing_musicgen_melody.py +++ b/src/transformers/models/musicgen_melody/processing_musicgen_melody.py @@ -16,7 +16,7 @@ Text/audio processor class for MusicGen Melody """ -from typing import List, Optional +from typing import Optional import numpy as np @@ -59,10 +59,10 @@ class MusicgenMelodyProcessor(ProcessorMixin): PreTrainedTokenizer's [`~PreTrainedTokenizer.__call__`] if `text` is not `None`. Please refer to the docstring of the above two methods for more information. Args: - audio (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + audio (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The audio or batch of audios to be prepared. Each audio can be NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each audio should be a mono-stereo signal of shape (T), where T is the sample length of the audio. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -123,7 +123,7 @@ class MusicgenMelodyProcessor(ProcessorMixin): return self.tokenizer.decode(*args, **kwargs) # Copied from transformers.models.musicgen.processing_musicgen.MusicgenProcessor._decode_audio with padding_mask->attention_mask - def _decode_audio(self, audio_values, attention_mask: Optional = None) -> List[np.ndarray]: + def _decode_audio(self, audio_values, attention_mask: Optional = None) -> list[np.ndarray]: """ This method strips any padding from the audio values to return a list of numpy audio arrays. """ diff --git a/src/transformers/models/mvp/modeling_mvp.py b/src/transformers/models/mvp/modeling_mvp.py index 6f823b28879..739ecd8f015 100644 --- a/src/transformers/models/mvp/modeling_mvp.py +++ b/src/transformers/models/mvp/modeling_mvp.py @@ -16,7 +16,7 @@ import copy import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -126,12 +126,12 @@ class MvpAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, attn_prompt: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -268,7 +268,7 @@ class MvpEncoderLayer(nn.Module): layer_head_mask: torch.FloatTensor, self_attn_prompt: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -353,10 +353,10 @@ class MvpDecoderLayer(nn.Module): cross_attn_layer_head_mask: Optional[torch.Tensor] = None, self_attn_prompt: Optional[torch.Tensor] = None, cross_attn_prompt: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -483,7 +483,7 @@ class MvpPrompt(nn.Module): nn.Linear(config.prompt_mid_dim, num_layers * 2 * config.d_model), ) - def forward(self, prompt_ids: torch.Tensor) -> Tuple[torch.Tensor]: + def forward(self, prompt_ids: torch.Tensor) -> tuple[torch.Tensor]: prompt = self.prompt_trans(self.prompt_embedding(prompt_ids)) prompt = prompt.view(self.prompt_length, self.num_layers * 2, self.num_heads, self.head_dim) prompt = self.dropout(prompt) @@ -583,7 +583,7 @@ class MvpEncoder(MvpPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -779,13 +779,13 @@ class MvpDecoder(MvpPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1047,15 +1047,15 @@ class MvpModel(MvpPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqModelOutput]: + ) -> Union[tuple, Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1214,8 +1214,8 @@ class MvpForConditionalGeneration(MvpPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1223,7 +1223,7 @@ class MvpForConditionalGeneration(MvpPreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1387,7 +1387,7 @@ class MvpForSequenceClassification(MvpPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1395,7 +1395,7 @@ class MvpForSequenceClassification(MvpPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1559,7 +1559,7 @@ class MvpForQuestionAnswering(MvpPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1568,7 +1568,7 @@ class MvpForQuestionAnswering(MvpPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple, Seq2SeqQuestionAnsweringModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1759,14 +1759,14 @@ class MvpForCausalLM(MvpPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/mvp/tokenization_mvp.py b/src/transformers/models/mvp/tokenization_mvp.py index e3a32082cce..d8fe24d5408 100644 --- a/src/transformers/models/mvp/tokenization_mvp.py +++ b/src/transformers/models/mvp/tokenization_mvp.py @@ -16,7 +16,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -280,7 +280,7 @@ class MvpTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -310,8 +310,8 @@ class MvpTokenizer(PreTrainedTokenizer): return vocab_file, merge_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A MVP sequence has the following format: @@ -320,13 +320,13 @@ class MvpTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -335,22 +335,22 @@ class MvpTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -362,20 +362,20 @@ class MvpTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. MVP does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/mvp/tokenization_mvp_fast.py b/src/transformers/models/mvp/tokenization_mvp_fast.py index ae226812c83..ca0bc6b165f 100644 --- a/src/transformers/models/mvp/tokenization_mvp_fast.py +++ b/src/transformers/models/mvp/tokenization_mvp_fast.py @@ -14,7 +14,7 @@ # limitations under the License. import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -236,7 +236,7 @@ class MvpTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -248,20 +248,20 @@ class MvpTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. MVP does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/myt5/tokenization_myt5.py b/src/transformers/models/myt5/tokenization_myt5.py index f86e5e44c2a..e262a70fcaa 100644 --- a/src/transformers/models/myt5/tokenization_myt5.py +++ b/src/transformers/models/myt5/tokenization_myt5.py @@ -18,7 +18,7 @@ import json import os import warnings from collections import defaultdict -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging @@ -36,14 +36,14 @@ class ByteRewriter: This class is used to rewrite bytes using a hash tree. The hash tree is constructed from a set of rewriting rules. Args: - rewriting_rules (`str` or `Dict[str, str]`): + rewriting_rules (`str` or `dict[str, str]`): A path to a json file containing the rewriting rules or a dictionary containing the rewriting rules. """ LEAF = "[LEAF]" - def __init__(self, rewriting_rules: Union[str, Dict[str, str]]): + def __init__(self, rewriting_rules: Union[str, dict[str, str]]): if isinstance(rewriting_rules, str): with open(rewriting_rules, "r") as f: rewriting_rules = json.load(f) @@ -56,7 +56,7 @@ class ByteRewriter: reverse_rewriting_rules = {v: k for k, v in rewriting_rules.items()} self.reverse_hash_tree = self.construct_hash_tree(reverse_rewriting_rules) - def add_leaf(self, hash_tree: Dict[str, Union[dict, List[str]]], byte_in_sequence: str, byte_out_sequence: str): + def add_leaf(self, hash_tree: dict[str, Union[dict, list[str]]], byte_in_sequence: str, byte_out_sequence: str): """ Add a leaf with the output byte sequence to the hash tree. """ @@ -71,7 +71,7 @@ class ByteRewriter: tree_pointer[self.LEAF] = byte_out_list - def construct_hash_tree(self, rewriting_rules: Dict[str, str]) -> Dict[str, Union[dict, List[str]]]: + def construct_hash_tree(self, rewriting_rules: dict[str, str]) -> dict[str, Union[dict, list[str]]]: """ Construct a hash tree for rewritten byte sequences. """ @@ -84,7 +84,7 @@ class ByteRewriter: return hash_tree - def search_hash_tree(self, byte_sequence: List[str]) -> Union[None, List[str]]: + def search_hash_tree(self, byte_sequence: list[str]) -> Union[None, list[str]]: """ Search the hash tree and return the rewritten byte sequence if found. """ @@ -97,15 +97,15 @@ class ByteRewriter: return tree_pointer[self.LEAF] - def rewrite_bytes(self, in_bytes: List[str], reverse=False) -> List[str]: + def rewrite_bytes(self, in_bytes: list[str], reverse=False) -> list[str]: """ Rewrite a sequence of bytes using the hash tree. Args: - in_bytes (`List[str]`): A list of bytes to be rewritten. + in_bytes (`list[str]`): A list of bytes to be rewritten. reverse (`bool`): If True, decoding is performed with the reverse hash tree. Returns: - `List[str]`: The rewritten byte sequence. + `list[str]`: The rewritten byte sequence. """ out_bytes = [] b_start = 0 @@ -155,7 +155,7 @@ class MyT5Tokenizer(PreTrainedTokenizer): indexed from the end of the vocabulary up to beginning ("" is the last token in the vocabulary like in ByT5 preprocessing see [here](https://github.com/google-research/text-to-text-transfer-transformer/blob/9fd7b14a769417be33bc6c850f9598764913c833/t5/data/preprocessors.py#L2117)). - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. """ @@ -220,22 +220,22 @@ class MyT5Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.byt5.tokenization_byt5.ByT5Tokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -247,7 +247,7 @@ class MyT5Tokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] - def _add_eos_if_not_present(self, token_ids: List[int]) -> List[int]: + def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]: """Do not add eos again if user already added it.""" if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( @@ -259,20 +259,20 @@ class MyT5Tokenizer(PreTrainedTokenizer): return token_ids + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. MyT5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] @@ -282,8 +282,8 @@ class MyT5Tokenizer(PreTrainedTokenizer): # Copied from transformers.models.byt5.tokenization_byt5.ByT5Tokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -292,13 +292,13 @@ class MyT5Tokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = self._add_eos_if_not_present(token_ids_0) if token_ids_1 is None: @@ -307,7 +307,7 @@ class MyT5Tokenizer(PreTrainedTokenizer): token_ids_1 = self._add_eos_if_not_present(token_ids_1) return token_ids_0 + token_ids_1 - def _tokenize(self, text: str, **kwargs) -> List[str]: + def _tokenize(self, text: str, **kwargs) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words. Represents tokens in two character hex format""" @@ -330,13 +330,13 @@ class MyT5Tokenizer(PreTrainedTokenizer): token = f"{index - self.offset:02x}" return token - def morphological_encode(self, indices: List[str]) -> List[str]: + def morphological_encode(self, indices: list[str]) -> list[str]: # Decompose and merge morphological sequences indices = self.decompose_rewriter.rewrite_bytes(indices, reverse=False) indices = self.merge_rewriter.rewrite_bytes(indices, reverse=False) return indices - def morphological_decode(self, indices: List[str]) -> List[str]: + def morphological_decode(self, indices: list[str]) -> list[str]: # Demerge and compose morphological sequences indices = self.merge_rewriter.rewrite_bytes(indices, reverse=True) indices = self.decompose_rewriter.rewrite_bytes(indices, reverse=True) @@ -365,7 +365,7 @@ class MyT5Tokenizer(PreTrainedTokenizer): string = bstring.decode("utf-8", errors="ignore") return string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if os.path.isdir(save_directory): vocab_file = os.path.join( save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"] diff --git a/src/transformers/models/nemotron/modeling_nemotron.py b/src/transformers/models/nemotron/modeling_nemotron.py index ea0f0de0456..6b5cf370c9d 100644 --- a/src/transformers/models/nemotron/modeling_nemotron.py +++ b/src/transformers/models/nemotron/modeling_nemotron.py @@ -16,7 +16,7 @@ """PyTorch Nemotron model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -61,7 +61,7 @@ def _cast_if_autocast_enabled(*args): class NemotronLayerNorm1P(nn.LayerNorm): def __init__( self, - normalized_shape: Union[int, List[int], Size], + normalized_shape: Union[int, list[int], Size], eps: float = 1e-5, elementwise_affine: bool = True, bias: bool = True, @@ -217,14 +217,14 @@ class NemotronAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -291,14 +291,14 @@ class NemotronFlashAttention2(NemotronAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if isinstance(past_key_value, StaticCache): raise ValueError( "`static` cache implementation is not compatible with `attn_implementation==flash_attention_2` " @@ -398,7 +398,7 @@ class NemotronSdpaAttention(NemotronAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, past_key_value: Optional[Cache] = None, @@ -406,7 +406,7 @@ class NemotronSdpaAttention(NemotronAttention): use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -508,9 +508,9 @@ class NemotronDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -526,7 +526,7 @@ class NemotronDecoderLayer(nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -634,7 +634,7 @@ class NemotronModel(NemotronPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -903,7 +903,7 @@ class NemotronForCausalLM(NemotronPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/nllb/tokenization_nllb.py b/src/transformers/models/nllb/tokenization_nllb.py index 1ac0059ddd2..4962a642bb3 100644 --- a/src/transformers/models/nllb/tokenization_nllb.py +++ b/src/transformers/models/nllb/tokenization_nllb.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -102,15 +102,15 @@ class NllbTokenizer(PreTrainedTokenizer): The language to use as source language for translation. tgt_lang (`str`, *optional*): The language to use as target language for translation. - sp_model_kwargs (`Dict[str, str]`): + sp_model_kwargs (`dict[str, str]`): Additional keyword arguments to pass to the model initialization. """ vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -125,7 +125,7 @@ class NllbTokenizer(PreTrainedTokenizer): tokenizer_file=None, src_lang=None, tgt_lang=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, additional_special_tokens=None, legacy_behaviour=False, **kwargs, @@ -213,22 +213,22 @@ class NllbTokenizer(PreTrainedTokenizer): self.set_src_lang_special_tokens(self._src_lang) def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -243,8 +243,8 @@ class NllbTokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An NLLB sequence has the following format, where `X` represents the sequence: @@ -256,13 +256,13 @@ class NllbTokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -270,20 +270,20 @@ class NllbTokenizer(PreTrainedTokenizer): return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. nllb does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -311,7 +311,7 @@ class NllbTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -329,7 +329,7 @@ class NllbTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -348,9 +348,9 @@ class NllbTokenizer(PreTrainedTokenizer): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "eng_Latn", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "fra_Latn", **kwargs, ) -> BatchEncoding: diff --git a/src/transformers/models/nllb/tokenization_nllb_fast.py b/src/transformers/models/nllb/tokenization_nllb_fast.py index a5592dc96ed..5300b3942b5 100644 --- a/src/transformers/models/nllb/tokenization_nllb_fast.py +++ b/src/transformers/models/nllb/tokenization_nllb_fast.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -113,8 +113,8 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): model_input_names = ["input_ids", "attention_mask"] slow_tokenizer_class = NllbTokenizer - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -176,8 +176,8 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): self.set_src_lang_special_tokens(self._src_lang) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. The special tokens depend on calling set_lang. @@ -191,13 +191,13 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -205,20 +205,20 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. nllb does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -243,9 +243,9 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "eng_Latn", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "fra_Latn", **kwargs, ) -> BatchEncoding: @@ -304,7 +304,7 @@ class NllbTokenizerFast(PreTrainedTokenizerFast): special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str, self.prefix_tokens + self.suffix_tokens)), ) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/nllb_moe/modeling_nllb_moe.py b/src/transformers/models/nllb_moe/modeling_nllb_moe.py index a9642e97370..15c87871649 100644 --- a/src/transformers/models/nllb_moe/modeling_nllb_moe.py +++ b/src/transformers/models/nllb_moe/modeling_nllb_moe.py @@ -15,7 +15,7 @@ """PyTorch NLLB-MoE model.""" import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -275,7 +275,7 @@ class NllbMoeTop2Router(nn.Module): router_logits: torch.Tensor, input_dtype: torch.dtype = torch.float32, padding_mask: Optional[torch.LongTensor] = None, - ) -> Tuple: + ) -> tuple: """ Computes the `dispatch_mask` and the `dispatch_weights` for each experts. The masks are adapted to the expert capacity. @@ -355,7 +355,7 @@ class NllbMoeTop2Router(nn.Module): return top_1_mask, router_probs - def forward(self, hidden_states: torch.Tensor, padding_mask: Optional[torch.LongTensor] = None) -> Tuple: + def forward(self, hidden_states: torch.Tensor, padding_mask: Optional[torch.LongTensor] = None) -> tuple: r""" The hidden states are reshaped to simplify the computation of the router probabilities (combining weights for each experts.) @@ -541,14 +541,14 @@ class NllbMoeAttention(nn.Module): self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if encoder_hidden_states are provided this layer is used as a cross-attention layer @@ -747,7 +747,7 @@ class NllbMoeDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = True, @@ -1136,7 +1136,7 @@ class NllbMoeDecoder(NllbMoePreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1494,8 +1494,8 @@ class NllbMoeModel(NllbMoePreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1503,7 +1503,7 @@ class NllbMoeModel(NllbMoePreTrainedModel): output_hidden_states: Optional[bool] = None, output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqMoEModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqMoEModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1642,8 +1642,8 @@ class NllbMoeForConditionalGeneration(NllbMoePreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1652,7 +1652,7 @@ class NllbMoeForConditionalGeneration(NllbMoePreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqMoEOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqMoEOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/nougat/image_processing_nougat.py b/src/transformers/models/nougat/image_processing_nougat.py index 9d38a0afafd..ad898ccfb20 100644 --- a/src/transformers/models/nougat/image_processing_nougat.py +++ b/src/transformers/models/nougat/image_processing_nougat.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Nougat.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -65,7 +65,7 @@ class NougatImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 896, "width": 672}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 896, "width": 672}`): Size of the image after resizing. Can be overridden by `size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. @@ -83,10 +83,10 @@ class NougatImageProcessor(BaseImageProcessor): `preprocess` method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Image standard deviation. """ @@ -96,7 +96,7 @@ class NougatImageProcessor(BaseImageProcessor): self, do_crop_margin: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_thumbnail: bool = True, do_align_long_axis: bool = False, @@ -104,8 +104,8 @@ class NougatImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -197,7 +197,7 @@ class NougatImageProcessor(BaseImageProcessor): def align_long_axis( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -207,7 +207,7 @@ class NougatImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be aligned. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to align the long axis to. data_format (`str` or `ChannelDimension`, *optional*): The data format of the output image. If unset, the same format as the input image is used. @@ -244,7 +244,7 @@ class NougatImageProcessor(BaseImageProcessor): def pad_image( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -254,7 +254,7 @@ class NougatImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be padded. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to pad the image to. data_format (`str` or `ChannelDimension`, *optional*): The data format of the output image. If unset, the same format as the input image is used. @@ -280,7 +280,7 @@ class NougatImageProcessor(BaseImageProcessor): def thumbnail( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -293,7 +293,7 @@ class NougatImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): The image to be resized. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size `{"height": h, "width": w}` to resize the image to. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): The resampling filter to use. @@ -331,7 +331,7 @@ class NougatImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -343,7 +343,7 @@ class NougatImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -373,7 +373,7 @@ class NougatImageProcessor(BaseImageProcessor): images: ImageInput, do_crop_margin: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_thumbnail: Optional[bool] = None, do_align_long_axis: Optional[bool] = None, @@ -381,8 +381,8 @@ class NougatImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -397,7 +397,7 @@ class NougatImageProcessor(BaseImageProcessor): Whether to crop the image margins. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to min(size["height"], size["width"]) with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -415,9 +415,9 @@ class NougatImageProcessor(BaseImageProcessor): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/nougat/processing_nougat.py b/src/transformers/models/nougat/processing_nougat.py index 6f48f23d581..2213c9cdbcd 100644 --- a/src/transformers/models/nougat/processing_nougat.py +++ b/src/transformers/models/nougat/processing_nougat.py @@ -16,7 +16,7 @@ Processor class for Nougat. """ -from typing import Dict, List, Optional, Union +from typing import Optional, Union from transformers.tokenization_utils_base import PreTokenizedInput, TextInput, TruncationStrategy @@ -52,7 +52,7 @@ class NougatProcessor(ProcessorMixin): text=None, do_crop_margin: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: "PILImageResampling" = None, # noqa: F821 do_thumbnail: Optional[bool] = None, do_align_long_axis: Optional[bool] = None, @@ -60,14 +60,14 @@ class NougatProcessor(ProcessorMixin): do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional["ChannelDimension"] = "channels_first", # noqa: F821 input_data_format: Optional[Union[str, "ChannelDimension"]] = None, # noqa: F821 - text_pair: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, diff --git a/src/transformers/models/nougat/tokenization_nougat_fast.py b/src/transformers/models/nougat/tokenization_nougat_fast.py index a4e94ab3f02..48291815897 100644 --- a/src/transformers/models/nougat/tokenization_nougat_fast.py +++ b/src/transformers/models/nougat/tokenization_nougat_fast.py @@ -19,7 +19,7 @@ Fast tokenizer class for Nougat. import re from functools import partial from multiprocessing import Pool -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -258,13 +258,13 @@ def get_slices(lines, clean_lines): greater than 0.9. Args: - lines (`List[str]`): + lines (`list[str]`): The list of lines containing the text. - clean_lines (`List[str]`): + clean_lines (`list[str]`): A cleaned version of the text (without numbers). Returns: - `List[tuple]`: A list of tuples representing the start and end indices of text slices. + `list[tuple]`: A list of tuples representing the start and end indices of text slices. """ indices = np.zeros(len(lines)) for i in range(len(lines) - 1): @@ -582,10 +582,10 @@ class NougatTokenizerFast(PreTrainedTokenizerFast): def post_process_generation( self, - generation: Union[str, List[str]], + generation: Union[str, list[str]], fix_markdown: bool = True, num_workers: Optional[int] = None, - ) -> Union[str, List[str]]: + ) -> Union[str, list[str]]: """ Postprocess a generated text or a list of generated texts. @@ -594,7 +594,7 @@ class NougatTokenizerFast(PreTrainedTokenizerFast): Postprocessing is quite slow so it is recommended to use multiprocessing to speed up the process. Args: - generation (Union[str, List[str]]): + generation (Union[str, list[str]]): The generated text or a list of generated texts. fix_markdown (`bool`, *optional*, defaults to `True`): Whether to perform Markdown formatting fixes. @@ -603,7 +603,7 @@ class NougatTokenizerFast(PreTrainedTokenizerFast): parallel). Returns: - Union[str, List[str]]: The postprocessed text or list of postprocessed texts. + Union[str, list[str]]: The postprocessed text or list of postprocessed texts. """ requires_backends(self, ["nltk", "levenshtein"]) diff --git a/src/transformers/models/nystromformer/modeling_nystromformer.py b/src/transformers/models/nystromformer/modeling_nystromformer.py index 1f40b40c5d4..17a3319de3a 100755 --- a/src/transformers/models/nystromformer/modeling_nystromformer.py +++ b/src/transformers/models/nystromformer/modeling_nystromformer.py @@ -15,7 +15,7 @@ """PyTorch Nystromformer model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -503,7 +503,7 @@ class NystromformerModel(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -605,7 +605,7 @@ class NystromformerForMaskedLM(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -696,7 +696,7 @@ class NystromformerForSequenceClassification(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -779,7 +779,7 @@ class NystromformerForMultipleChoice(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -886,7 +886,7 @@ class NystromformerForTokenClassification(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -955,7 +955,7 @@ class NystromformerForQuestionAnswering(NystromformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.nystromformer( diff --git a/src/transformers/models/olmo/modeling_olmo.py b/src/transformers/models/olmo/modeling_olmo.py index 36999733b3a..c35988e2b8d 100644 --- a/src/transformers/models/olmo/modeling_olmo.py +++ b/src/transformers/models/olmo/modeling_olmo.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_olmo.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -159,12 +159,12 @@ class OlmoAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -228,9 +228,9 @@ class OlmoDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/olmo/modular_olmo.py b/src/transformers/models/olmo/modular_olmo.py index 18a17533a3c..44678f09399 100644 --- a/src/transformers/models/olmo/modular_olmo.py +++ b/src/transformers/models/olmo/modular_olmo.py @@ -1,4 +1,4 @@ -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.nn as nn @@ -79,12 +79,12 @@ class OlmoAttention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) diff --git a/src/transformers/models/olmo2/convert_olmo2_weights_to_hf.py b/src/transformers/models/olmo2/convert_olmo2_weights_to_hf.py index 86d403916a3..4b515aae69b 100644 --- a/src/transformers/models/olmo2/convert_olmo2_weights_to_hf.py +++ b/src/transformers/models/olmo2/convert_olmo2_weights_to_hf.py @@ -19,7 +19,7 @@ import json import os import shutil from pathlib import Path -from typing import Any, Dict +from typing import Any import torch import yaml @@ -110,7 +110,7 @@ def write_model( loaded = torch.load(os.path.join(input_base_path, "model.pt"), map_location="cpu", weights_only=True) param_count = 0 - index_dict: Dict[str, Any] = {"weight_map": {}} + index_dict: dict[str, Any] = {"weight_map": {}} for layer_i in range(n_layers): filename = f"pytorch_model-{layer_i + 1}-of-{n_layers + 1}.bin" # Unsharded diff --git a/src/transformers/models/olmo2/modeling_olmo2.py b/src/transformers/models/olmo2/modeling_olmo2.py index 661a9341d67..8e69f43d3eb 100644 --- a/src/transformers/models/olmo2/modeling_olmo2.py +++ b/src/transformers/models/olmo2/modeling_olmo2.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_olmo2.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -152,12 +152,12 @@ class Olmo2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -232,9 +232,9 @@ class Olmo2DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states # Self Attention diff --git a/src/transformers/models/olmo2/modular_olmo2.py b/src/transformers/models/olmo2/modular_olmo2.py index 9772a2b7275..6464d5f3ec0 100644 --- a/src/transformers/models/olmo2/modular_olmo2.py +++ b/src/transformers/models/olmo2/modular_olmo2.py @@ -1,4 +1,4 @@ -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.nn as nn @@ -198,12 +198,12 @@ class Olmo2Attention(OlmoAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -263,9 +263,9 @@ class Olmo2DecoderLayer(OlmoDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states # Self Attention diff --git a/src/transformers/models/olmoe/modeling_olmoe.py b/src/transformers/models/olmoe/modeling_olmoe.py index 374e941c023..6dc3c12c1ff 100644 --- a/src/transformers/models/olmoe/modeling_olmoe.py +++ b/src/transformers/models/olmoe/modeling_olmoe.py @@ -12,7 +12,7 @@ """PyTorch OLMoE model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -41,7 +41,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -294,9 +294,9 @@ class OlmoeAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_norm(self.q_proj(hidden_states)) @@ -376,9 +376,9 @@ class OlmoeFlashAttention2(OlmoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: output_attentions = False bsz, q_len, _ = hidden_states.size() @@ -477,8 +477,8 @@ class OlmoeSdpaAttention(OlmoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -631,9 +631,9 @@ class OlmoeDecoderLayer(nn.Module): output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -652,7 +652,7 @@ class OlmoeDecoderLayer(nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -754,7 +754,7 @@ class OlmoeModel(OlmoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -762,7 +762,7 @@ class OlmoeModel(OlmoePreTrainedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, MoeModelOutputWithPast]: + ) -> Union[tuple, MoeModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_router_logits = ( output_router_logits if output_router_logits is not None else self.config.output_router_logits @@ -1045,7 +1045,7 @@ class OlmoeForCausalLM(OlmoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1056,7 +1056,7 @@ class OlmoeForCausalLM(OlmoePreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **loss_kwargs, - ) -> Union[Tuple, MoeCausalLMOutputWithPast]: + ) -> Union[tuple, MoeCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/omdet_turbo/configuration_omdet_turbo.py b/src/transformers/models/omdet_turbo/configuration_omdet_turbo.py index f26f01dc243..477880de3cd 100644 --- a/src/transformers/models/omdet_turbo/configuration_omdet_turbo.py +++ b/src/transformers/models/omdet_turbo/configuration_omdet_turbo.py @@ -116,7 +116,7 @@ class OmDetTurboConfig(PretrainedConfig): The number of points sampled in the decoder multi-scale deformable attention module. decoder_dropout (`float`, *optional*, defaults to 0.0): The dropout rate for the decoder. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to computes the effective height and width of the position embeddings after taking into account the stride (see RTDetr). learn_initial_query (`bool`, *optional*, defaults to `False`): @@ -125,7 +125,7 @@ class OmDetTurboConfig(PretrainedConfig): The cache size for the classes and prompts caches. is_encoder_decoder (`bool`, *optional*, defaults to `True`): Whether the model is used as an encoder-decoder model or not. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from the architecture. The values in kwargs will be saved as part of the configuration and can be used to control the model outputs. diff --git a/src/transformers/models/omdet_turbo/modeling_omdet_turbo.py b/src/transformers/models/omdet_turbo/modeling_omdet_turbo.py index 59e6cec1425..77af9993033 100644 --- a/src/transformers/models/omdet_turbo/modeling_omdet_turbo.py +++ b/src/transformers/models/omdet_turbo/modeling_omdet_turbo.py @@ -19,7 +19,7 @@ import warnings from collections import OrderedDict from dataclasses import dataclass from functools import lru_cache -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -60,14 +60,14 @@ class OmDetTurboEncoderOutput(ModelOutput): Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. - extracted_states (`Tuple[torch.FloatTensor]`): + extracted_states (`tuple[torch.FloatTensor]`): The extracted states from the Feature Pyramid Network (FPN) and Path Aggregation Network (PAN) of the encoder. """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - extracted_states: Tuple[torch.FloatTensor] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + extracted_states: tuple[torch.FloatTensor] = None @dataclass @@ -84,31 +84,31 @@ class OmDetTurboDecoderOutput(ModelOutput): The predicted classes of the objects. encoder_coord_logits (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`): The predicted coordinates of the objects from the encoder. - encoder_class_logits (`Tuple[torch.FloatTensor]`) of shape `(batch_size, num_queries, num_classes)`: + encoder_class_logits (`tuple[torch.FloatTensor]`) of shape `(batch_size, num_queries, num_classes)`: The predicted class of the objects from the encoder. init_reference_points (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`): The initial reference points. - intermediate_reference_points (`Tuple[Tuple[torch.FloatTensor]]`): + intermediate_reference_points (`tuple[tuple[torch.FloatTensor]]`): The intermediate reference points. - hidden_states (`Optional[Tuple[torch.FloatTensor]]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): + hidden_states (`Optional[tuple[torch.FloatTensor]]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus the initial embedding outputs. - attentions (`Optional[Tuple[Tuple[torch.FloatTensor]]]`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`): + attentions (`Optional[tuple[tuple[torch.FloatTensor]]]`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`): Tuple of tuples of `torch.FloatTensor` (one for attention for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in the self-attention, cross-attention and multi-scale deformable attention heads. """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None decoder_coords: Optional[torch.FloatTensor] = None decoder_classes: Optional[torch.FloatTensor] = None encoder_coord_logits: Optional[torch.FloatTensor] = None - encoder_class_logits: Tuple[torch.FloatTensor] = None + encoder_class_logits: tuple[torch.FloatTensor] = None init_reference_points: Optional[torch.FloatTensor] = None - intermediate_reference_points: Tuple[Tuple[torch.FloatTensor]] = None + intermediate_reference_points: tuple[tuple[torch.FloatTensor]] = None @dataclass @@ -125,27 +125,27 @@ class OmDetTurboObjectDetectionOutput(ModelOutput): The predicted class of the objects. init_reference_points (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`): The initial reference points. - intermediate_reference_points (`Tuple[Tuple[torch.FloatTensor]]`): + intermediate_reference_points (`tuple[tuple[torch.FloatTensor]]`): The intermediate reference points. encoder_coord_logits (`torch.FloatTensor` of shape `(batch_size, num_queries, 4)`): The predicted coordinates of the objects from the encoder. - encoder_class_logits (`Tuple[torch.FloatTensor]`): + encoder_class_logits (`tuple[torch.FloatTensor]`): The predicted class of the objects from the encoder. encoder_extracted_states (`torch.FloatTensor`): The extracted states from the Feature Pyramid Network (FPN) and Path Aggregation Network (PAN) of the encoder. - decoder_hidden_states (`Tuple[torch.FloatTensor]`, *optional*): + decoder_hidden_states (`tuple[torch.FloatTensor]`, *optional*): Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus the initial embedding outputs. - decoder_attentions (`Tuple[Tuple[torch.FloatTensor]]`, *optional*): + decoder_attentions (`tuple[tuple[torch.FloatTensor]]`, *optional*): Tuple of tuples of `torch.FloatTensor` (one for attention for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in the self-attention, cross-attention and multi-scale deformable attention heads. - encoder_hidden_states (`Tuple[torch.FloatTensor]`, *optional*): + encoder_hidden_states (`tuple[torch.FloatTensor]`, *optional*): Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus the initial embedding outputs. - encoder_attentions (`Tuple[Tuple[torch.FloatTensor]]`, *optional*): + encoder_attentions (`tuple[tuple[torch.FloatTensor]]`, *optional*): Tuple of tuples of `torch.FloatTensor` (one for attention for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in the self-attention, cross-attention and multi-scale deformable attention heads. @@ -157,14 +157,14 @@ class OmDetTurboObjectDetectionOutput(ModelOutput): decoder_coord_logits: Optional[torch.FloatTensor] = None decoder_class_logits: Optional[torch.FloatTensor] = None init_reference_points: Optional[torch.FloatTensor] = None - intermediate_reference_points: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + intermediate_reference_points: Optional[tuple[tuple[torch.FloatTensor]]] = None encoder_coord_logits: Optional[torch.FloatTensor] = None - encoder_class_logits: Tuple[torch.FloatTensor] = None + encoder_class_logits: tuple[torch.FloatTensor] = None encoder_extracted_states: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None classes_structure: Optional[torch.LongTensor] = None @@ -175,7 +175,7 @@ class MultiScaleDeformableAttention(nn.Module): self, value: Tensor, value_spatial_shapes: Tensor, - value_spatial_shapes_list: List[Tuple], + value_spatial_shapes_list: list[tuple], level_start_index: Tensor, sampling_locations: Tensor, attention_weights: Tensor, @@ -518,7 +518,7 @@ class OmDetTurboMultiheadAttention(nn.Module): values: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(queries)) key_layer = self.transpose_for_scores(self.key(keys)) value_layer = self.transpose_for_scores(self.value(values)) @@ -630,7 +630,7 @@ class OmDetTurboEncoder(nn.Module): def forward( self, src, src_mask=None, pos_embed=None, output_attentions: bool = False - ) -> Tuple[Union[torch.Tensor, Tuple[torch.Tensor]]]: + ) -> tuple[Union[torch.Tensor, tuple[torch.Tensor]]]: hidden_states = src attention = () if output_attentions else None for layer in self.layers: @@ -1523,7 +1523,7 @@ class OmDetTurboForObjectDetection(OmDetTurboPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], OmDetTurboObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], OmDetTurboObjectDetectionOutput]: r""" classes_input_ids (`torch.LongTensor` of shape `(total_classes (>= batch_size), sequence_length)`): Indices of input classes sequence tokens in the vocabulary of the language model. diff --git a/src/transformers/models/omdet_turbo/processing_omdet_turbo.py b/src/transformers/models/omdet_turbo/processing_omdet_turbo.py index 618c67c783d..4e526b4f209 100644 --- a/src/transformers/models/omdet_turbo/processing_omdet_turbo.py +++ b/src/transformers/models/omdet_turbo/processing_omdet_turbo.py @@ -17,7 +17,7 @@ Processor class for OmDet-Turbo. """ import warnings -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_transforms import center_to_corners_format @@ -38,7 +38,7 @@ if TYPE_CHECKING: class OmDetTurboTextKwargs(TextKwargs, total=False): - task: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] + task: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] if is_torch_available(): @@ -89,7 +89,7 @@ class DictWithDeprecationWarning(dict): return super().get(key, *args, **kwargs) -def clip_boxes(box, box_size: Tuple[int, int]): +def clip_boxes(box, box_size: tuple[int, int]): """ Clip the boxes by limiting x coordinates to the range [0, width] and y coordinates to the range [0, height]. @@ -128,11 +128,11 @@ def _post_process_boxes_for_image( scores: "torch.Tensor", labels: "torch.Tensor", image_num_classes: int, - image_size: Tuple[int, int], + image_size: tuple[int, int], threshold: float, nms_threshold: float, max_num_det: Optional[int] = None, -) -> Tuple["torch.Tensor", "torch.Tensor", "torch.Tensor"]: +) -> tuple["torch.Tensor", "torch.Tensor", "torch.Tensor"]: """ Filter predicted results using given thresholds and NMS. @@ -147,7 +147,7 @@ def _post_process_boxes_for_image( A Tensor of predicted labels for the image. image_num_classes (`int`): The number of classes queried for detection on the image. - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): A tuple of (height, width) for the image. threshold (`float`): Only return detections with a confidence score exceeding this threshold. @@ -227,7 +227,7 @@ class OmDetTurboProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Optional[Union[List[str], List[List[str]]]] = None, + text: Optional[Union[list[str], list[list[str]]]] = None, audio=None, videos=None, **kwargs: Unpack[OmDetTurboProcessorKwargs], @@ -241,12 +241,12 @@ class OmDetTurboProcessor(ProcessorMixin): Args: images (`ImageInput`): Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. - text (`Union[str, List[str], List[List[str]]]`): + text (`Union[str, list[str], list[list[str]]]`): The classes used to limit the scope of the open vocabulary detection. Expects a list of strings or a list of list of strings. Batched classes can be of different lengths. Examples: ["cat", "dog", "bird"], [["cat", "dog", "bird"], ["hat", "person"], ["car"]] Kwargs: - task (`Union[str, List[str], TextInput, PreTokenizedInput]`): + task (`Union[str, list[str], TextInput, PreTokenizedInput]`): The grounded text used to guide open vocabulary detection. Expects a single string or a list of strings. Examples: "Detect a cat, a dog, and a bird.",[ "Detect everything.", "Detect trees and flowers."] When not provided, the default task is "Detect [class1], [class2], [class3]" etc. @@ -306,7 +306,7 @@ class OmDetTurboProcessor(ProcessorMixin): """ return self.tokenizer.decode(*args, **kwargs) - def _get_default_image_size(self) -> Tuple[int, int]: + def _get_default_image_size(self) -> tuple[int, int]: height = ( self.image_processor.size["height"] if "height" in self.image_processor.size @@ -324,10 +324,10 @@ class OmDetTurboProcessor(ProcessorMixin): def post_process_grounded_object_detection( self, outputs: "OmDetTurboObjectDetectionOutput", - text_labels: Optional[Union[List[str], List[List[str]]]] = None, + text_labels: Optional[Union[list[str], list[list[str]]]] = None, threshold: float = 0.3, nms_threshold: float = 0.5, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, max_num_det: Optional[int] = None, ): """ @@ -337,19 +337,19 @@ class OmDetTurboProcessor(ProcessorMixin): Args: outputs ([`OmDetTurboObjectDetectionOutput`]): Raw outputs of the model. - text_labels (Union[List[str], List[List[str]]], *optional*): + text_labels (Union[list[str], list[list[str]]], *optional*): The input classes names. If not provided, `text_labels` will be set to `None` in `outputs`. threshold (float, defaults to 0.3): Only return detections with a confidence score exceeding this threshold. nms_threshold (float, defaults to 0.5): The threshold to use for box non-maximum suppression. Value in [0, 1]. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. max_num_det (`int`, *optional*): The maximum number of detections to return. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, classes and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, classes and boxes for an image in the batch as predicted by the model. """ diff --git a/src/transformers/models/oneformer/configuration_oneformer.py b/src/transformers/models/oneformer/configuration_oneformer.py index 47438cde419..580995d6556 100644 --- a/src/transformers/models/oneformer/configuration_oneformer.py +++ b/src/transformers/models/oneformer/configuration_oneformer.py @@ -14,7 +14,7 @@ # limitations under the License. """OneFormer model configuration""" -from typing import Dict, Optional +from typing import Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -150,11 +150,11 @@ class OneFormerConfig(PretrainedConfig): def __init__( self, - backbone_config: Optional[Dict] = None, + backbone_config: Optional[dict] = None, backbone: Optional[str] = None, use_pretrained_backbone: bool = False, use_timm_backbone: bool = False, - backbone_kwargs: Optional[Dict] = None, + backbone_kwargs: Optional[dict] = None, ignore_value: int = 255, num_queries: int = 150, no_object_weight: int = 0.1, diff --git a/src/transformers/models/oneformer/convert_to_hf_oneformer.py b/src/transformers/models/oneformer/convert_to_hf_oneformer.py index 94aca98c136..2bc7edfb74a 100644 --- a/src/transformers/models/oneformer/convert_to_hf_oneformer.py +++ b/src/transformers/models/oneformer/convert_to_hf_oneformer.py @@ -22,7 +22,7 @@ from collections.abc import Iterator from dataclasses import dataclass from pathlib import Path from pprint import pformat -from typing import Any, Dict, List, Set, Tuple +from typing import Any import requests import torch @@ -51,7 +51,7 @@ from transformers.models.oneformer.processing_oneformer import OneFormerProcesso from transformers.utils import logging -StateDict = Dict[str, Tensor] +StateDict = dict[str, Tensor] logging.set_verbosity_info() logger = logging.get_logger() @@ -60,14 +60,14 @@ torch.manual_seed(0) class TrackedStateDict: - def __init__(self, to_track: Dict): + def __init__(self, to_track: dict): """This class "tracks" a python dictionary by keeping track of which item is accessed. Args: to_track (Dict): The dictionary we wish to track """ self.to_track = to_track - self._seen: Set[str] = set() + self._seen: set[str] = set() def __getitem__(self, key: str) -> Any: return self.to_track[key] @@ -76,16 +76,16 @@ class TrackedStateDict: self._seen.add(key) self.to_track[key] = item - def diff(self) -> List[str]: + def diff(self) -> list[str]: """This method returns a set difference between the keys in the tracked state dict and the one we have access so far. This is an effective method to check if we have update all the keys Returns: - List[str]: List of keys not yet updated + list[str]: List of keys not yet updated """ return set(self.to_track.keys()) - self._seen - def copy(self) -> Dict: + def copy(self) -> dict: # proxy the call to the internal dictionary return self.to_track.copy() @@ -243,7 +243,7 @@ class OriginalOneFormerCheckpointToOursConverter: self.original_model = original_model self.config = config - def pop_all(self, renamed_keys: List[Tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): + def pop_all(self, renamed_keys: list[tuple[str, str]], dst_state_dict: StateDict, src_state_dict: StateDict): for src_key, dst_key in renamed_keys: dst_state_dict[dst_key] = src_state_dict.pop(src_key) @@ -922,8 +922,8 @@ class OriginalOneFormerCheckpointToOursConverter: return oneformer @staticmethod - def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[Tuple[object, Path, Path]]: - checkpoints: List[Path] = checkpoints_dir.glob("**/*.pth") + def using_dirs(checkpoints_dir: Path, config_dir: Path) -> Iterator[tuple[object, Path, Path]]: + checkpoints: list[Path] = checkpoints_dir.glob("**/*.pth") for checkpoint in checkpoints: logger.info(f"💪 Converting {checkpoint.stem}") @@ -933,7 +933,7 @@ class OriginalOneFormerCheckpointToOursConverter: yield config, checkpoint -def post_process_sem_seg_output(outputs: OneFormerForUniversalSegmentationOutput, target_size: Tuple[int, int]): +def post_process_sem_seg_output(outputs: OneFormerForUniversalSegmentationOutput, target_size: tuple[int, int]): # class_queries_logits has shape [BATCH, QUERIES, CLASSES + 1] class_queries_logits = outputs.class_queries_logits # masks_queries_logits has shape [BATCH, QUERIES, HEIGHT, WIDTH] diff --git a/src/transformers/models/oneformer/image_processing_oneformer.py b/src/transformers/models/oneformer/image_processing_oneformer.py index e13d65be79b..4268edb13ce 100644 --- a/src/transformers/models/oneformer/image_processing_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_oneformer.py @@ -17,7 +17,7 @@ import json import os from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union import numpy as np from huggingface_hub import hf_hub_download @@ -65,7 +65,7 @@ if is_torch_available(): # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -74,8 +74,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -93,7 +93,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -101,7 +101,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -142,7 +142,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -173,7 +173,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -210,14 +210,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -231,7 +231,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -267,7 +267,7 @@ def compute_segments( # Copied from transformers.models.maskformer.image_processing_maskformer.convert_segmentation_map_to_binary_masks def convert_segmentation_map_to_binary_masks( segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -308,7 +308,7 @@ def convert_segmentation_map_to_binary_masks( def get_oneformer_resize_output_image_size( image: np.ndarray, - size: Union[int, Tuple[int, int], List[int], Tuple[int]], + size: Union[int, tuple[int, int], list[int], tuple[int]], max_size: Optional[int] = None, default_to_square: bool = True, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -319,7 +319,7 @@ def get_oneformer_resize_output_image_size( Args: image (`np.ndarray`): The input image. - size (`int` or `Tuple[int, int]` or `List[int]` or `Tuple[int]`): + size (`int` or `tuple[int, int]` or `list[int]` or `tuple[int]`): The size of the output image. max_size (`int`, *optional*): The maximum size of the output image. @@ -329,7 +329,7 @@ def get_oneformer_resize_output_image_size( The channel dimension format of the input image. If unset, will use the inferred format from the input. Returns: - `Tuple[int, int]`: The output size. + `tuple[int, int]`: The output size. """ output_size = get_resize_output_image_size( input_image=image, @@ -431,13 +431,13 @@ class OneFormerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, repo_path: Optional[str] = "shi-labs/oneformer_demo", @@ -474,7 +474,7 @@ class OneFormerImageProcessor(BaseImageProcessor): self.num_labels = num_labels @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to save support of deprecated `reduce_labels` in old configs """ @@ -484,7 +484,7 @@ class OneFormerImageProcessor(BaseImageProcessor): return super().from_dict(image_processor_dict, **kwargs) # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.to_dict - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. This method calls the superclass method and then removes the `_max_size` attribute from the dictionary. @@ -498,7 +498,7 @@ class OneFormerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format=None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -564,7 +564,7 @@ class OneFormerImageProcessor(BaseImageProcessor): def convert_segmentation_map_to_binary_masks( self, segmentation_map: "np.ndarray", - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, ): @@ -584,13 +584,13 @@ class OneFormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_resize: @@ -605,13 +605,13 @@ class OneFormerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -645,7 +645,7 @@ class OneFormerImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: """Preprocesses a single mask.""" @@ -680,17 +680,17 @@ class OneFormerImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - task_inputs: Optional[List[str]] = None, + task_inputs: Optional[list[str]] = None, segmentation_maps: Optional[ImageInput] = None, - instance_id_to_semantic_id: Optional[Dict[int, int]] = None, + instance_id_to_semantic_id: Optional[dict[int, int]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -781,7 +781,7 @@ class OneFormerImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -808,7 +808,7 @@ class OneFormerImageProcessor(BaseImageProcessor): # Copied from transformers.models.vilt.image_processing_vilt.ViltImageProcessor.pad def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -960,10 +960,10 @@ class OneFormerImageProcessor(BaseImageProcessor): def encode_inputs( self, - pixel_values_list: List[ImageInput], - task_inputs: List[str], + pixel_values_list: list[ImageInput], + task_inputs: list[str], segmentation_maps: ImageInput = None, - instance_id_to_semantic_id: Optional[Union[List[Dict[int, int]], Dict[int, int]]] = None, + instance_id_to_semantic_id: Optional[Union[list[dict[int, int]], dict[int, int]]] = None, ignore_index: Optional[int] = None, do_reduce_labels: bool = False, return_tensors: Optional[Union[str, TensorType]] = None, @@ -979,11 +979,11 @@ class OneFormerImageProcessor(BaseImageProcessor): each mask. Args: - pixel_values_list (`List[ImageInput]`): + pixel_values_list (`list[ImageInput]`): List of images (pixel values) to be padded. Each image should be a tensor of shape `(channels, height, width)`. - task_inputs (`List[str]`): + task_inputs (`list[str]`): List of task values. segmentation_maps (`ImageInput`, *optional*): @@ -997,7 +997,7 @@ class OneFormerImageProcessor(BaseImageProcessor): - 1 for pixels that are real (i.e. **not masked**), - 0 for pixels that are padding (i.e. **masked**). - instance_id_to_semantic_id (`List[Dict[int, int]]` or `Dict[int, int]`, *optional*): + instance_id_to_semantic_id (`list[dict[int, int]]` or `dict[int, int]`, *optional*): A mapping between object instance ids and class ids. If passed, `segmentation_maps` is treated as an instance segmentation map where each pixel represents an instance id. Can be provided as a single dictionary with a global/dataset-level mapping or as a list of dictionaries (one per image), to map @@ -1094,7 +1094,7 @@ class OneFormerImageProcessor(BaseImageProcessor): # Copied from transformers.models.maskformer.image_processing_maskformer.MaskFormerImageProcessor.post_process_semantic_segmentation def post_process_semantic_segmentation( - self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None + self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None ) -> "torch.Tensor": """ Converts the output of [`MaskFormerForInstanceSegmentation`] into semantic segmentation maps. Only supports @@ -1103,11 +1103,11 @@ class OneFormerImageProcessor(BaseImageProcessor): Args: outputs ([`MaskFormerForInstanceSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[tuple[int, int]]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. Returns: - `List[torch.Tensor]`: + `list[torch.Tensor]`: A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. @@ -1151,7 +1151,7 @@ class OneFormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - target_sizes: Optional[List[Tuple[int, int]]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, return_coco_annotation: Optional[bool] = False, ): """ @@ -1173,15 +1173,15 @@ class OneFormerImageProcessor(BaseImageProcessor): overlap_mask_area_threshold (`float`, *optional*, defaults to 0.8): The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If left to None, predictions will not be resized. return_coco_annotation (`bool`, *optional)*, defaults to `False`): Whether to return predictions in COCO format. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id`, set to `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1201,7 +1201,7 @@ class OneFormerImageProcessor(BaseImageProcessor): num_classes = class_queries_logits.shape[-1] - 1 # Loop over items in batch size - results: List[Dict[str, torch.Tensor]] = [] + results: list[dict[str, torch.Tensor]] = [] for i in range(batch_size): # [Q, K] @@ -1269,9 +1269,9 @@ class OneFormerImageProcessor(BaseImageProcessor): threshold: float = 0.5, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_sizes: Optional[List[Tuple[int, int]]] = None, - ) -> List[Dict]: + label_ids_to_fuse: Optional[set[int]] = None, + target_sizes: Optional[list[tuple[int, int]]] = None, + ) -> list[dict]: """ Converts the output of [`MaskFormerForInstanceSegmentationOutput`] into image panoptic segmentation predictions. Only supports PyTorch. @@ -1290,13 +1290,13 @@ class OneFormerImageProcessor(BaseImageProcessor): The labels in this state will have all their instances be fused together. For instance we could say there can only be one sky in an image, but several persons, so the label ID for sky would be in that set, but not the one for person. - target_sizes (`List[Tuple]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested + target_sizes (`list[Tuple]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]]`) corresponds to the requested final size (height, width) of each prediction in batch. If left to None, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: + `list[Dict]`: A list of dictionaries, one per image, each dictionary containing two keys: - **segmentation** -- a tensor of shape `(height, width)` where each pixel represents a `segment_id`, set to `None` if no mask if found above `threshold`. If `target_sizes` is specified, segmentation is resized to the corresponding `target_sizes` entry. @@ -1324,7 +1324,7 @@ class OneFormerImageProcessor(BaseImageProcessor): pred_scores, pred_labels = nn.functional.softmax(class_queries_logits, dim=-1).max(-1) # Loop over items in batch size - results: List[Dict[str, TensorType]] = [] + results: list[dict[str, TensorType]] = [] for i in range(batch_size): mask_probs_item, pred_scores_item, pred_labels_item = remove_low_and_no_objects( diff --git a/src/transformers/models/oneformer/modeling_oneformer.py b/src/transformers/models/oneformer/modeling_oneformer.py index 219b1a972b1..bd740204b1b 100644 --- a/src/transformers/models/oneformer/modeling_oneformer.py +++ b/src/transformers/models/oneformer/modeling_oneformer.py @@ -18,7 +18,7 @@ import copy import math import warnings from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -57,7 +57,7 @@ def _get_clones(module, N): def multi_scale_deformable_attention( value: Tensor, - value_spatial_shapes: Union[Tensor, List[Tuple]], + value_spatial_shapes: Union[Tensor, list[tuple]], sampling_locations: Tensor, attention_weights: Tensor, ) -> Tensor: @@ -262,7 +262,7 @@ class OneFormerHungarianMatcher(nn.Module): self.num_points = num_points @torch.no_grad() - def forward(self, masks_queries_logits, class_queries_logits, mask_labels, class_labels) -> List[Tuple[Tensor]]: + def forward(self, masks_queries_logits, class_queries_logits, mask_labels, class_labels) -> list[tuple[Tensor]]: """Performs the matching Params: @@ -281,13 +281,13 @@ class OneFormerHungarianMatcher(nn.Module): masks. Returns: - `List[Tuple[Tensor]]`: A list of size batch_size, containing tuples of (index_i, index_j) where: + `list[tuple[Tensor]]`: A list of size batch_size, containing tuples of (index_i, index_j) where: - index_i is the indices of the selected predictions (in order) - index_j is the indices of the corresponding selected labels (in order) For each batch element, it holds: len(index_i) = len(index_j) = min(num_queries, num_targets). """ - indices: List[Tuple[np.array]] = [] + indices: list[tuple[np.array]] = [] num_queries = class_queries_logits.shape[1] @@ -332,7 +332,7 @@ class OneFormerHungarianMatcher(nn.Module): cost_matrix = self.cost_mask * cost_mask + self.cost_class * cost_class + self.cost_dice * cost_dice cost_matrix = cost_matrix.reshape(num_queries, -1).cpu() # do the assigmented using the hungarian algorithm in scipy - assigned_indices: Tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) + assigned_indices: tuple[np.array] = linear_sum_assignment(cost_matrix.cpu()) indices.append(assigned_indices) # It could be stacked in one tensor @@ -347,7 +347,7 @@ class OneFormerLoss(nn.Module): self, num_classes: int, matcher: OneFormerHungarianMatcher, - weight_dict: Dict[str, float], + weight_dict: dict[str, float], eos_coef: float, num_points: int, oversample_ratio: float, @@ -366,7 +366,7 @@ class OneFormerLoss(nn.Module): The number of classes. matcher (`OneFormerHungarianMatcher`): A torch module that computes the assignments between the predictions and labels. - weight_dict (`Dict[str, float]`): + weight_dict (`dict[str, float]`): A dictionary of weights to be applied to the different losses. eos_coef (`float`): Weight to apply to the null class. @@ -397,14 +397,14 @@ class OneFormerLoss(nn.Module): if self.contrastive_temperature is not None: self.logit_scale = nn.Parameter(torch.tensor(np.log(1 / contrastive_temperature))) - def _max_by_axis(self, the_list: List[List[int]]) -> List[int]: + def _max_by_axis(self, the_list: list[list[int]]) -> list[int]: maxes = the_list[0] for sublist in the_list[1:]: for index, item in enumerate(sublist): maxes[index] = max(maxes[index], item) return maxes - def _pad_images_to_max_in_batch(self, tensors: List[Tensor]) -> Tuple[Tensor, Tensor]: + def _pad_images_to_max_in_batch(self, tensors: list[Tensor]) -> tuple[Tensor, Tensor]: # get the maximum size in the batch max_size = self._max_by_axis([list(tensor.shape) for tensor in tensors]) batch_size = len(tensors) @@ -432,7 +432,7 @@ class OneFormerLoss(nn.Module): text_queries (`torch.Tensor`): A tensor of shape `batch_size, num_queries, hidden_dim` Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: - **loss_contrastive** -- The query-text contrastive loss computed using task-guided queries and text queries derived from input text list. """ @@ -461,20 +461,20 @@ class OneFormerLoss(nn.Module): return losses def loss_labels( - self, class_queries_logits: Tensor, class_labels: List[Tensor], indices: Tuple[np.array] - ) -> Dict[str, Tensor]: + self, class_queries_logits: Tensor, class_labels: list[Tensor], indices: tuple[np.array] + ) -> dict[str, Tensor]: """Compute the losses related to the labels using cross entropy. Args: class_queries_logits (`torch.Tensor`): A tensor of shape `batch_size, num_queries, num_labels` - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing the following key: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. """ pred_logits = class_queries_logits @@ -496,8 +496,8 @@ class OneFormerLoss(nn.Module): return losses def loss_masks( - self, masks_queries_logits: Tensor, mask_labels: List[Tensor], indices: Tuple[np.array], num_masks: int - ) -> Dict[str, Tensor]: + self, masks_queries_logits: Tensor, mask_labels: list[Tensor], indices: tuple[np.array], num_masks: int + ) -> dict[str, Tensor]: """Compute the losses related to the masks using focal and dice loss. Args: @@ -505,13 +505,13 @@ class OneFormerLoss(nn.Module): A tensor of shape `batch_size, num_queries, height, width` mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - indices (`Tuple[np.array])`: + indices (`tuple[np.array])`: The indices computed by the Hungarian matcher. num_masks (`int)`: The number of masks, used for normalization. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: - **loss_mask** -- The loss computed using sigmoid ce loss on the predicted and ground truth masks. - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth masks. @@ -643,12 +643,12 @@ class OneFormerLoss(nn.Module): masks_queries_logits: Tensor, class_queries_logits: Tensor, contrastive_queries_logits: Tensor, - mask_labels: List[Tensor], - class_labels: List[Tensor], + mask_labels: list[Tensor], + class_labels: list[Tensor], text_queries: Tensor, - auxiliary_predictions: Optional[Dict[str, Tensor]] = None, + auxiliary_predictions: Optional[dict[str, Tensor]] = None, calculate_contrastive_loss: bool = True, - ) -> Dict[str, Tensor]: + ) -> dict[str, Tensor]: """ This performs the loss computation. @@ -661,18 +661,18 @@ class OneFormerLoss(nn.Module): A tensor of shape `batch_size, num_queries, hidden_dim` mask_labels (`torch.Tensor`): List of mask labels of shape `(labels, height, width)`. - class_labels (`List[torch.Tensor]`): + class_labels (`list[torch.Tensor]`): List of class labels of shape `(labels)`. text_queries (`torch.Tensor`): A tensor of shape `batch_size, num_queries, hidden_dim` - auxiliary_predictions (`Dict[str, torch.Tensor]`, *optional*): + auxiliary_predictions (`dict[str, torch.Tensor]`, *optional*): if `use_auxiliary_loss` was set to `true` in [`OneFormerConfig`], then it contains the logits from the inner layers of the Detr's Decoder. calculate_contrastive_loss (`bool`, *optional*, defaults to `True`): Whether or not to calculate the contrastive loss. Returns: - `Dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: + `dict[str, Tensor]`: A dict of `torch.Tensor` containing two keys: - **loss_cross_entropy** -- The loss computed using cross entropy on the predicted and ground truth labels. - **loss_mask** -- The loss computed using sigmoid ce loss on the predicted and ground truth masks. - **loss_dice** -- The loss computed using dice loss on the predicted on the predicted and ground truth @@ -687,7 +687,7 @@ class OneFormerLoss(nn.Module): # compute the average number of target masks for normalization purposes num_masks = self.get_num_masks(class_labels, device=class_labels[0].device) # get all the losses - losses: Dict[str, Tensor] = { + losses: dict[str, Tensor] = { **self.loss_masks(masks_queries_logits, mask_labels, indices, num_masks), **self.loss_labels(class_queries_logits, class_labels, indices), } @@ -752,7 +752,7 @@ class OneFormerTransformerDecoderOutput(BaseModelOutput): contrastive_logits: Optional[torch.FloatTensor] = None prediction_masks: Optional[torch.FloatTensor] = None prediction_class: Optional[torch.FloatTensor] = None - auxiliary_predictions: Optional[Tuple[Dict[str, torch.FloatTensor]]] = None + auxiliary_predictions: Optional[tuple[dict[str, torch.FloatTensor]]] = None @dataclass @@ -775,9 +775,9 @@ class OneFormerPixelDecoderOutput(ModelOutput): or when `config.output_attentions=True` """ - multi_scale_features: Tuple[torch.FloatTensor] = None + multi_scale_features: tuple[torch.FloatTensor] = None mask_features: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -798,8 +798,8 @@ class OneFormerPixelLevelModuleOutput(ModelOutput): 1/4 scale features from the last Pixel Decoder Layer. """ - encoder_features: List[torch.FloatTensor] = None - decoder_features: List[torch.FloatTensor] = None + encoder_features: list[torch.FloatTensor] = None + decoder_features: list[torch.FloatTensor] = None decoder_last_feature: Optional[torch.FloatTensor] = None @@ -840,17 +840,17 @@ class OneFormerModelOutput(ModelOutput): sequence_length)`. Self and Cross Attentions weights from transformer decoder. """ - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None transformer_decoder_hidden_states: Optional[torch.FloatTensor] = None transformer_decoder_object_queries: Optional[torch.FloatTensor] = None transformer_decoder_contrastive_queries: Optional[torch.FloatTensor] = None transformer_decoder_mask_predictions: Optional[torch.FloatTensor] = None transformer_decoder_class_predictions: Optional[torch.FloatTensor] = None - transformer_decoder_auxiliary_predictions: Optional[Tuple[Dict[str, torch.FloatTensor]]] = None + transformer_decoder_auxiliary_predictions: Optional[tuple[dict[str, torch.FloatTensor]]] = None text_queries: Optional[torch.FloatTensor] = None task_token: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -908,18 +908,18 @@ class OneFormerForUniversalSegmentationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None class_queries_logits: Optional[torch.FloatTensor] = None masks_queries_logits: Optional[torch.FloatTensor] = None - auxiliary_predictions: List[Dict[str, torch.FloatTensor]] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - pixel_decoder_hidden_states: Optional[List[torch.FloatTensor]] = None + auxiliary_predictions: list[dict[str, torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + pixel_decoder_hidden_states: Optional[list[torch.FloatTensor]] = None transformer_decoder_hidden_states: Optional[torch.FloatTensor] = None transformer_decoder_object_queries: Optional[torch.FloatTensor] = None transformer_decoder_contrastive_queries: Optional[torch.FloatTensor] = None transformer_decoder_mask_predictions: Optional[torch.FloatTensor] = None transformer_decoder_class_predictions: Optional[torch.FloatTensor] = None - transformer_decoder_auxiliary_predictions: Optional[List[Dict[str, torch.FloatTensor]]] = None + transformer_decoder_auxiliary_predictions: Optional[list[dict[str, torch.FloatTensor]]] = None text_queries: Optional[torch.FloatTensor] = None task_token: Optional[torch.FloatTensor] = None - attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None + attentions: Optional[tuple[tuple[torch.FloatTensor]]] = None # Modified from transformers.models.deformable_detr.modeling_deformable_detr.DeformableDetrFrozenBatchNorm2d with DeformableDetr->OneFormerPixelDecoder @@ -1485,7 +1485,7 @@ class OneFormerPixelLevelModule(nn.Module): self.decoder = OneFormerPixelDecoder(config, feature_channels=self.encoder.channels) def forward(self, pixel_values: Tensor, output_hidden_states: bool = False) -> OneFormerPixelLevelModuleOutput: - features: List[Tensor] = self.encoder(pixel_values).feature_maps + features: list[Tensor] = self.encoder(pixel_values).feature_maps decoder_output: OneFormerPixelDecoderOutput = self.decoder(features, output_hidden_states=output_hidden_states) return OneFormerPixelLevelModuleOutput( encoder_features=tuple(features), @@ -1540,7 +1540,7 @@ class OneFormerAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, key_value_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" hidden_states = hidden_states.permute(1, 0, 2) if hidden_states is not None else None @@ -1869,8 +1869,8 @@ class OneFormerTransformerDecoderLayer(nn.Module): self, index: int, output: torch.Tensor, - multi_stage_features: List[torch.Tensor], - multi_stage_positional_embeddings: List[torch.Tensor], + multi_stage_features: list[torch.Tensor], + multi_stage_positional_embeddings: list[torch.Tensor], attention_mask: Optional[torch.Tensor] = None, query_embeddings: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, @@ -1879,8 +1879,8 @@ class OneFormerTransformerDecoderLayer(nn.Module): Args: index (`int`): index of the layer in the Transformer decoder. output (`torch.FloatTensor`): the object queries of shape `(N, batch, hidden_dim)` - multi_stage_features (`List[torch.Tensor]`): the multi-scale features from the pixel decoder. - multi_stage_positional_embeddings (`List[torch.Tensor]`): + multi_stage_features (`list[torch.Tensor]`): the multi-scale features from the pixel decoder. + multi_stage_positional_embeddings (`list[torch.Tensor]`): positional embeddings for the multi_stage_features attention_mask (`torch.FloatTensor`): attention mask for the masked cross attention layer query_embeddings (`torch.FloatTensor`, *optional*): @@ -2327,7 +2327,7 @@ class OneFormerTransformerModule(nn.Module): def forward( self, - multi_scale_features: List[Tensor], + multi_scale_features: list[Tensor], mask_features: Tensor, task_token: Tensor, output_attentions: bool = False, @@ -2896,7 +2896,7 @@ class OneFormerModel(OneFormerPreTrainedModel): task_inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Task inputs. Task inputs can be obtained using [`AutoImageProcessor`]. See [`OneFormerProcessor.__call__`] for details. - text_inputs (`List[torch.Tensor]`, *optional*): + text_inputs (`list[torch.Tensor]`, *optional*): Tensor fof shape `(num_queries, sequence_length)` to be fed to a model Example: @@ -3011,7 +3011,7 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): num_points=config.train_num_points, ) - self.weight_dict: Dict[str, float] = { + self.weight_dict: dict[str, float] = { "loss_cross_entropy": config.class_weight, "loss_mask": config.mask_weight, "loss_dice": config.dice_weight, @@ -3039,10 +3039,10 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): mask_labels: Tensor, class_labels: Tensor, text_queries: Tensor, - auxiliary_predictions: Dict[str, Tensor], + auxiliary_predictions: dict[str, Tensor], calculate_contrastive_loss: bool, - ) -> Dict[str, Tensor]: - loss_dict: Dict[str, Tensor] = self.criterion( + ) -> dict[str, Tensor]: + loss_dict: dict[str, Tensor] = self.criterion( masks_queries_logits=masks_queries_logits, class_queries_logits=class_queries_logits, contrastive_queries_logits=contrastive_queries_logits, @@ -3061,7 +3061,7 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): return loss_dict - def get_loss(self, loss_dict: Dict[str, Tensor]) -> Tensor: + def get_loss(self, loss_dict: dict[str, Tensor]) -> Tensor: return sum(loss_dict.values()) @auto_docstring @@ -3070,8 +3070,8 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): pixel_values: Tensor, task_inputs: Tensor, text_inputs: Optional[Tensor] = None, - mask_labels: Optional[List[Tensor]] = None, - class_labels: Optional[List[Tensor]] = None, + mask_labels: Optional[list[Tensor]] = None, + class_labels: Optional[list[Tensor]] = None, pixel_mask: Optional[Tensor] = None, output_auxiliary_logits: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -3082,11 +3082,11 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): task_inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Task inputs. Task inputs can be obtained using [`AutoImageProcessor`]. See [`OneFormerProcessor.__call__`] for details. - text_inputs (`List[torch.Tensor]`, *optional*): + text_inputs (`list[torch.Tensor]`, *optional*): Tensor fof shape `(num_queries, sequence_length)` to be fed to a model - mask_labels (`List[torch.Tensor]`, *optional*): + mask_labels (`list[torch.Tensor]`, *optional*): List of mask labels of shape `(num_labels, height, width)` to be fed to a model - class_labels (`List[torch.LongTensor]`, *optional*): + class_labels (`list[torch.LongTensor]`, *optional*): list of target class labels of shape `(num_labels, height, width)` to be fed to a model. They identify the labels of `mask_labels`, e.g. the label of `mask_labels[i][j]` if `class_labels[i][j]`. output_auxiliary_logits (`bool`, *optional*): @@ -3189,7 +3189,7 @@ class OneFormerForUniversalSegmentation(OneFormerPreTrainedModel): text_queries = outputs.text_queries if mask_labels is not None and class_labels is not None: - loss_dict: Dict[str, Tensor] = self.get_loss_dict( + loss_dict: dict[str, Tensor] = self.get_loss_dict( masks_queries_logits=masks_queries_logits, class_queries_logits=class_queries_logits, contrastive_queries_logits=contrastive_queries_logits, diff --git a/src/transformers/models/oneformer/processing_oneformer.py b/src/transformers/models/oneformer/processing_oneformer.py index d3e02f50d81..bcc0f9cbb61 100644 --- a/src/transformers/models/oneformer/processing_oneformer.py +++ b/src/transformers/models/oneformer/processing_oneformer.py @@ -16,8 +16,6 @@ Image/Text processor class for OneFormer """ -from typing import List - from ...processing_utils import ProcessorMixin from ...utils import is_torch_available @@ -85,11 +83,11 @@ class OneFormerProcessor(ProcessorMixin): docstring of the above two methods for more information. Args: - task_inputs (`str`, `List[str]`): + task_inputs (`str`, `list[str]`): The sequence or batch of task_inputs sequences to be encoded. Each sequence can be a string or a list of strings of the template "the task is {task}". - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, - `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, + `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. segmentation_maps (`ImageInput`, *optional*): @@ -121,7 +119,7 @@ class OneFormerProcessor(ProcessorMixin): if isinstance(task_inputs, str): task_inputs = [task_inputs] - if isinstance(task_inputs, List) and all(isinstance(task_input, str) for task_input in task_inputs): + if isinstance(task_inputs, list) and all(isinstance(task_input, str) for task_input in task_inputs): task_token_inputs = [] for task in task_inputs: task_input = f"the task is {task}" @@ -161,7 +159,7 @@ class OneFormerProcessor(ProcessorMixin): if isinstance(task_inputs, str): task_inputs = [task_inputs] - if isinstance(task_inputs, List) and all(isinstance(task_input, str) for task_input in task_inputs): + if isinstance(task_inputs, list) and all(isinstance(task_input, str) for task_input in task_inputs): task_token_inputs = [] for task in task_inputs: task_input = f"the task is {task}" diff --git a/src/transformers/models/openai/modeling_openai.py b/src/transformers/models/openai/modeling_openai.py index 55aa53c40ff..73aba214371 100644 --- a/src/transformers/models/openai/modeling_openai.py +++ b/src/transformers/models/openai/modeling_openai.py @@ -19,7 +19,7 @@ import json import math import os from dataclasses import dataclass -from typing import Any, Callable, Dict, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch from torch import nn @@ -410,8 +410,8 @@ class OpenAIGPTDoubleHeadsModelOutput(ModelOutput): mc_loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None mc_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -453,7 +453,7 @@ class OpenAIGPTModel(OpenAIGPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutput]: + ) -> Union[tuple[torch.Tensor], BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -571,7 +571,7 @@ class OpenAIGPTLMHeadModel(OpenAIGPTPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutput]: + ) -> Union[tuple[torch.Tensor], CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set @@ -615,7 +615,7 @@ class OpenAIGPTLMHeadModel(OpenAIGPTPreTrainedModel, GenerationMixin): attentions=transformer_outputs.attentions, ) - def prepare_inputs_for_generation(self, input_ids: torch.LongTensor, **kwargs) -> Dict[str, Any]: + def prepare_inputs_for_generation(self, input_ids: torch.LongTensor, **kwargs) -> dict[str, Any]: # Overwritten -- old model with reduced inputs return {"input_ids": input_ids} @@ -663,7 +663,7 @@ class OpenAIGPTDoubleHeadsModel(OpenAIGPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], OpenAIGPTDoubleHeadsModelOutput]: + ) -> Union[tuple[torch.Tensor], OpenAIGPTDoubleHeadsModelOutput]: r""" mc_token_ids (`torch.LongTensor` of shape `(batch_size, num_choices)`, *optional*, default to index of the last token of the input): Index of the classification token in each input sequence. Selected in the range `[0, input_ids.size(-1) - @@ -775,7 +775,7 @@ class OpenAIGPTForSequenceClassification(OpenAIGPTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/openai/modeling_tf_openai.py b/src/transformers/models/openai/modeling_tf_openai.py index 3856711d106..e78523eb78f 100644 --- a/src/transformers/models/openai/modeling_tf_openai.py +++ b/src/transformers/models/openai/modeling_tf_openai.py @@ -18,7 +18,7 @@ from __future__ import annotations from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -297,7 +297,7 @@ class TFOpenAIGPTMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -431,8 +431,8 @@ class TFOpenAIGPTDoubleHeadsModelOutput(ModelOutput): logits: Optional[tf.Tensor] = None mc_logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None OPENAI_GPT_START_DOCSTRING = r""" @@ -561,7 +561,7 @@ class TFOpenAIGPTModel(TFOpenAIGPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -625,7 +625,7 @@ class TFOpenAIGPTLMHeadModel(TFOpenAIGPTPreTrainedModel, TFCausalLanguageModelin return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFCausalLMOutput]: + ) -> Union[tuple, TFCausalLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., @@ -712,7 +712,7 @@ class TFOpenAIGPTDoubleHeadsModel(TFOpenAIGPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFOpenAIGPTDoubleHeadsModelOutput]: + ) -> Union[tuple, TFOpenAIGPTDoubleHeadsModelOutput]: r""" mc_token_ids (`tf.Tensor` or `Numpy array` of shape `(batch_size, num_choices)`, *optional*, default to index of the last token of the input): Index of the classification token in each input sequence. Selected in the range `[0, input_ids.size(-1) - @@ -858,7 +858,7 @@ class TFOpenAIGPTForSequenceClassification(TFOpenAIGPTPreTrainedModel, TFSequenc return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSequenceClassifierOutput]: + ) -> Union[tuple, TFSequenceClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/openai/tokenization_openai.py b/src/transformers/models/openai/tokenization_openai.py index cbfb41fc888..0617121d13b 100644 --- a/src/transformers/models/openai/tokenization_openai.py +++ b/src/transformers/models/openai/tokenization_openai.py @@ -18,7 +18,7 @@ import json import os import re import unicodedata -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -363,7 +363,7 @@ class OpenAIGPTTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace("", " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/openai/tokenization_openai_fast.py b/src/transformers/models/openai/tokenization_openai_fast.py index c17d7d29b7d..83edf5eafa9 100644 --- a/src/transformers/models/openai/tokenization_openai_fast.py +++ b/src/transformers/models/openai/tokenization_openai_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Tokenization classes for OpenAI GPT.""" -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import logging @@ -58,7 +58,7 @@ class OpenAIGPTTokenizerFast(PreTrainedTokenizerFast): def do_lower_case(self): return True - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/opt/modeling_flax_opt.py b/src/transformers/models/opt/modeling_flax_opt.py index 97637a83d1a..d2f77ecbee2 100644 --- a/src/transformers/models/opt/modeling_flax_opt.py +++ b/src/transformers/models/opt/modeling_flax_opt.py @@ -15,7 +15,7 @@ """Flax OPT model.""" from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -185,7 +185,7 @@ class FlaxOPTAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -308,7 +308,7 @@ class FlaxOPTDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states # 125m, 1.7B, ..., 175B applies layer norm BEFORE attention @@ -522,7 +522,7 @@ class FlaxOPTPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: OPTConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -531,7 +531,7 @@ class FlaxOPTPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/opt/modeling_opt.py b/src/transformers/models/opt/modeling_opt.py index b03e9bb452b..fd22722b69c 100644 --- a/src/transformers/models/opt/modeling_opt.py +++ b/src/transformers/models/opt/modeling_opt.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch OPT model.""" -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -144,13 +144,13 @@ class OPTAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, cache_position: Optional[torch.Tensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[Cache]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, _ = hidden_states.size() @@ -227,13 +227,13 @@ class OPTDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -519,7 +519,7 @@ class OPTDecoder(OPTPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -528,7 +528,7 @@ class OPTDecoder(OPTPreTrainedModel): position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -751,7 +751,7 @@ class OPTModel(OPTPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -760,7 +760,7 @@ class OPTModel(OPTPreTrainedModel): position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -833,7 +833,7 @@ class OPTForCausalLM(OPTPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -843,7 +843,7 @@ class OPTForCausalLM(OPTPreTrainedModel, GenerationMixin): position_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -950,7 +950,7 @@ class OPTForSequenceClassification(OPTPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -958,7 +958,7 @@ class OPTForSequenceClassification(OPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1062,7 +1062,7 @@ class OPTForQuestionAnswering(OPTPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, @@ -1071,7 +1071,7 @@ class OPTForQuestionAnswering(OPTPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, position_ids: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" Example: diff --git a/src/transformers/models/opt/modeling_tf_opt.py b/src/transformers/models/opt/modeling_tf_opt.py index ec5ef7616a3..f403d1cc5cc 100644 --- a/src/transformers/models/opt/modeling_tf_opt.py +++ b/src/transformers/models/opt/modeling_tf_opt.py @@ -16,7 +16,7 @@ from __future__ import annotations -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -154,11 +154,11 @@ class TFOPTAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -312,11 +312,11 @@ class TFOPTDecoderLayer(keras.layers.Layer): hidden_states: tf.Tensor, attention_mask: np.ndarray | tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, - past_key_value: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_value: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, training: Optional[bool] = False, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -473,7 +473,7 @@ OPT_INPUTS_DOCSTRING = r""" - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -578,13 +578,13 @@ class TFOPTDecoder(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPast, tuple[tf.Tensor]]: r""" Args: input_ids (`tf.Tensor` of shape `(batch_size, sequence_length)`): @@ -609,7 +609,7 @@ class TFOPTDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -780,7 +780,7 @@ class TFOPTMainLayer(keras.layers.Layer): input_ids: TFModelInputType | None = None, attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -788,7 +788,7 @@ class TFOPTMainLayer(keras.layers.Layer): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPast, tuple[tf.Tensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -860,7 +860,7 @@ class TFOPTModel(TFOPTPreTrainedModel): input_ids: TFModelInputType | None = None, attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -868,7 +868,7 @@ class TFOPTModel(TFOPTPreTrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFBaseModelOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPast, tuple[tf.Tensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -963,7 +963,7 @@ class TFOPTForCausalLM(TFOPTPreTrainedModel, TFCausalLanguageModelingLoss): def call( self, input_ids: TFModelInputType | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, attention_mask: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, @@ -975,7 +975,7 @@ class TFOPTForCausalLM(TFOPTPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[TFCausalLMOutputWithPast, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithPast, tuple[tf.Tensor]]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): diff --git a/src/transformers/models/owlv2/configuration_owlv2.py b/src/transformers/models/owlv2/configuration_owlv2.py index bf70afc7ccf..48e094b8f04 100644 --- a/src/transformers/models/owlv2/configuration_owlv2.py +++ b/src/transformers/models/owlv2/configuration_owlv2.py @@ -14,7 +14,7 @@ # limitations under the License. """OWLv2 model configuration""" -from typing import TYPE_CHECKING, Dict +from typing import TYPE_CHECKING if TYPE_CHECKING: @@ -271,7 +271,7 @@ class Owlv2Config(PretrainedConfig): self.initializer_factor = 1.0 @classmethod - def from_text_vision_configs(cls, text_config: Dict, vision_config: Dict, **kwargs): + def from_text_vision_configs(cls, text_config: dict, vision_config: dict, **kwargs): r""" Instantiate a [`Owlv2Config`] (or a derived class) from owlv2 text model configuration and owlv2 vision model configuration. diff --git a/src/transformers/models/owlv2/image_processing_owlv2.py b/src/transformers/models/owlv2/image_processing_owlv2.py index b7c7785bc68..33a14adfb0b 100644 --- a/src/transformers/models/owlv2/image_processing_owlv2.py +++ b/src/transformers/models/owlv2/image_processing_owlv2.py @@ -15,7 +15,7 @@ """Image processor class for OWLv2.""" import warnings -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -73,7 +73,7 @@ def _scale_boxes(boxes, target_sizes): Args: boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. - target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. Returns: @@ -225,17 +225,17 @@ class Owlv2ImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 960, "width": 960}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 960, "width": 960}`): Size to resize the image to. Can be overridden by `size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): Resampling method to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `OPENAI_CLIP_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -248,11 +248,11 @@ class Owlv2ImageProcessor(BaseImageProcessor): rescale_factor: Union[int, float] = 1 / 255, do_pad: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -301,7 +301,7 @@ class Owlv2ImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], anti_aliasing: bool = True, anti_aliasing_sigma=None, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -314,7 +314,7 @@ class Owlv2ImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary containing the height and width to resize the image to. anti_aliasing (`bool`, *optional*, defaults to `True`): Whether to apply anti-aliasing when downsampling the image. @@ -371,12 +371,12 @@ class Owlv2ImageProcessor(BaseImageProcessor): images: ImageInput, do_pad: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -392,7 +392,7 @@ class Owlv2ImageProcessor(BaseImageProcessor): Whether to pad the image to a square with gray pixels on the bottom and the right. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size to resize the image to. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -400,9 +400,9 @@ class Owlv2ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: @@ -504,7 +504,7 @@ class Owlv2ImageProcessor(BaseImageProcessor): self, outputs: "Owlv2ObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`Owlv2ForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -515,12 +515,12 @@ class Owlv2ImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. @@ -572,7 +572,7 @@ class Owlv2ImageProcessor(BaseImageProcessor): None, predictions will not be unnormalized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. All labels are set to None as `OwlViTForObjectDetection.image_guided_detection` perform one-shot object detection. """ diff --git a/src/transformers/models/owlv2/modeling_owlv2.py b/src/transformers/models/owlv2/modeling_owlv2.py index 5affe9ca8df..4e838c90a9f 100644 --- a/src/transformers/models/owlv2/modeling_owlv2.py +++ b/src/transformers/models/owlv2/modeling_owlv2.py @@ -16,7 +16,7 @@ from dataclasses import dataclass from functools import lru_cache -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -69,7 +69,7 @@ class Owlv2Output(ModelOutput): image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`): The image embeddings obtained by applying the projection layer to the pooled output of [`Owlv2VisionModel`]. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`Owlv2TextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`Owlv2VisionModel`]. @@ -83,7 +83,7 @@ class Owlv2Output(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -188,14 +188,14 @@ class Owlv2ObjectDetectionOutput(ModelOutput): class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`): Class embeddings of all image patches. OWLv2 represents images as a set of image patches where the total number of patches is (image_size / patch_size)**2. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`Owlv2TextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`Owlv2VisionModel`]. """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None objectness_logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None @@ -205,7 +205,7 @@ class Owlv2ObjectDetectionOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -240,7 +240,7 @@ class Owlv2ImageGuidedObjectDetectionOutput(ModelOutput): class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`): Class embeddings of all image patches. OWLv2 represents images as a set of image patches where the total number of patches is (image_size / patch_size)**2. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`Owlv2TextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`Owlv2VisionModel`]. @@ -255,7 +255,7 @@ class Owlv2ImageGuidedObjectDetectionOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -404,7 +404,7 @@ class Owlv2Attention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -512,7 +512,7 @@ class Owlv2EncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -619,7 +619,7 @@ class Owlv2Encoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`). @@ -705,7 +705,7 @@ class Owlv2TextTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -787,7 +787,7 @@ class Owlv2TextModel(Owlv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -837,7 +837,7 @@ class Owlv2VisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -896,7 +896,7 @@ class Owlv2VisionModel(Owlv2PreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: ```python @@ -1060,7 +1060,7 @@ class Owlv2Model(Owlv2PreTrainedModel): interpolate_pos_encoding: bool = False, return_base_image_embeds: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Owlv2Output]: + ) -> Union[tuple, Owlv2Output]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. @@ -1180,7 +1180,7 @@ class Owlv2ClassPredictionHead(nn.Module): image_embeds: torch.FloatTensor, query_embeds: Optional[torch.FloatTensor], query_mask: Optional[torch.Tensor], - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: image_class_embeds = self.dense0(image_embeds) if query_embeds is None: device = image_class_embeds.device @@ -1325,7 +1325,7 @@ class Owlv2ForObjectDetection(Owlv2PreTrainedModel): image_feats: torch.FloatTensor, query_embeds: Optional[torch.FloatTensor] = None, query_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: image_feats: @@ -1348,7 +1348,7 @@ class Owlv2ForObjectDetection(Owlv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: # Encode text and image outputs = self.owlv2( pixel_values=pixel_values, @@ -1398,7 +1398,7 @@ class Owlv2ForObjectDetection(Owlv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: # Get Owlv2Model vision embeddings (same as CLIP) vision_outputs = self.owlv2.vision_model( pixel_values=pixel_values, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=True diff --git a/src/transformers/models/owlv2/processing_owlv2.py b/src/transformers/models/owlv2/processing_owlv2.py index ce4a3ed49d2..eddfcb3f7de 100644 --- a/src/transformers/models/owlv2/processing_owlv2.py +++ b/src/transformers/models/owlv2/processing_owlv2.py @@ -17,7 +17,7 @@ Image/Text processor class for OWLv2 """ import warnings -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -81,7 +81,7 @@ class Owlv2Processor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, # The following is to capture `query_images` argument that may be passed as a positional argument. # See transformers.processing_utils.ProcessorMixin.prepare_and_validate_optional_call_args for more details, # or this conversation for more context: https://github.com/huggingface/transformers/pull/32544#discussion_r1720208116 @@ -100,15 +100,15 @@ class Owlv2Processor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, - `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, + `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The query image to be prepared, one query image is expected per target image to be queried. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. @@ -146,10 +146,10 @@ class Owlv2Processor(ProcessorMixin): data = {} if text is not None: - if isinstance(text, str) or (isinstance(text, List) and not isinstance(text[0], List)): + if isinstance(text, str) or (isinstance(text, list) and not isinstance(text[0], list)): encodings = [self.tokenizer(text, **output_kwargs["text_kwargs"])] - elif isinstance(text, List) and isinstance(text[0], List): + elif isinstance(text, list) and isinstance(text[0], list): encodings = [] # Maximum number of queries across batch @@ -222,8 +222,8 @@ class Owlv2Processor(ProcessorMixin): self, outputs: "Owlv2ObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, - text_labels: Optional[List[List[str]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, + text_labels: Optional[list[list[str]]] = None, ): """ Converts the raw output of [`Owlv2ForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -234,15 +234,15 @@ class Owlv2Processor(ProcessorMixin): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. - text_labels (`List[List[str]]`, *optional*): + text_labels (`list[list[str]]`, *optional*): List of lists of text labels for each image in the batch. If unset, "text_labels" in output will be set to `None`. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. @@ -272,7 +272,7 @@ class Owlv2Processor(ProcessorMixin): outputs: "Owlv2ImageGuidedObjectDetectionOutput", threshold: float = 0.0, nms_threshold: float = 0.3, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the output of [`Owlv2ForObjectDetection.image_guided_detection`] into the format expected by the COCO @@ -291,7 +291,7 @@ class Owlv2Processor(ProcessorMixin): None, predictions will not be unnormalized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. - "labels": Set to `None`. diff --git a/src/transformers/models/owlvit/configuration_owlvit.py b/src/transformers/models/owlvit/configuration_owlvit.py index 458f7fe70dc..d4873ff4a08 100644 --- a/src/transformers/models/owlvit/configuration_owlvit.py +++ b/src/transformers/models/owlvit/configuration_owlvit.py @@ -16,7 +16,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import TYPE_CHECKING, Any, Dict, Optional +from typing import TYPE_CHECKING, Any, Optional if TYPE_CHECKING: @@ -272,7 +272,7 @@ class OwlViTConfig(PretrainedConfig): self.initializer_factor = 1.0 @classmethod - def from_text_vision_configs(cls, text_config: Dict, vision_config: Dict, **kwargs): + def from_text_vision_configs(cls, text_config: dict, vision_config: dict, **kwargs): r""" Instantiate a [`OwlViTConfig`] (or a derived class) from owlvit text model configuration and owlvit vision model configuration. diff --git a/src/transformers/models/owlvit/image_processing_owlvit.py b/src/transformers/models/owlvit/image_processing_owlvit.py index dfd5007f990..8d3448e6885 100644 --- a/src/transformers/models/owlvit/image_processing_owlvit.py +++ b/src/transformers/models/owlvit/image_processing_owlvit.py @@ -15,7 +15,7 @@ """Image processor class for OwlViT""" import warnings -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -69,7 +69,7 @@ def _scale_boxes(boxes, target_sizes): Args: boxes (`torch.Tensor` of shape `(batch_size, num_boxes, 4)`): Bounding boxes to scale. Each box is expected to be in (x1, y1, x2, y2) format. - target_sizes (`List[Tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): + target_sizes (`list[tuple[int, int]]` or `torch.Tensor` of shape `(batch_size, 2)`): Target sizes to scale the boxes to. Each target size is expected to be in (height, width) format. Returns: @@ -132,7 +132,7 @@ class OwlViTImageProcessor(BaseImageProcessor): Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the shorter edge of the input to a certain `size`. - size (`Dict[str, int]`, *optional*, defaults to {"height": 768, "width": 768}): + size (`dict[str, int]`, *optional*, defaults to {"height": 768, "width": 768}): The size to use for resizing the image. Only has an effect if `do_resize` is set to `True`. If `size` is a sequence like (h, w), output size will be matched to this. If `size` is an int, then image will be resized to (size, size). @@ -153,9 +153,9 @@ class OwlViTImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether or not to normalize the input with `image_mean` and `image_std`. Desired output size when applying center-cropping. Only has an effect if `do_center_crop` is set to `True`. - image_mean (`List[int]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`list[int]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): The sequence of means for each channel, to be used when normalizing images. - image_std (`List[int]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`list[int]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): The sequence of standard deviations for each channel, to be used when normalizing images. """ @@ -203,7 +203,7 @@ class OwlViTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -215,7 +215,7 @@ class OwlViTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): The size to resize the image to. Must contain height and width keys. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): The resampling filter to use when resizing the input. @@ -241,7 +241,7 @@ class OwlViTImageProcessor(BaseImageProcessor): def center_crop( self, image: np.ndarray, - crop_size: Dict[str, int], + crop_size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -252,7 +252,7 @@ class OwlViTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to center crop. - crop_size (`Dict[str, int]`): + crop_size (`dict[str, int]`): The size to center crop the image to. Must contain height and width keys. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. If unset, the channel dimension format of the input @@ -306,15 +306,15 @@ class OwlViTImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -328,7 +328,7 @@ class OwlViTImageProcessor(BaseImageProcessor): ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether or not to resize the input. If `True`, will resize the input to the size specified by `size`. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): The size to resize the input to. Only has an effect if `do_resize` is set to `True`. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): The resampling filter to use when resizing the input. Only has an effect if `do_resize` is set to @@ -336,7 +336,7 @@ class OwlViTImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether or not to center crop the input. If `True`, will center crop the input to the size specified by `crop_size`. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): The size to center crop the input to. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether or not to rescale the input. If `True`, will rescale the input by dividing it by @@ -346,10 +346,10 @@ class OwlViTImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether or not to normalize the input. If `True`, will normalize the input by subtracting `image_mean` and dividing by `image_std`. - image_mean (`Union[float, List[float]]`, *optional*, defaults to `self.image_mean`): + image_mean (`Union[float, list[float]]`, *optional*, defaults to `self.image_mean`): The mean to subtract from the input when normalizing. Only has an effect if `do_normalize` is set to `True`. - image_std (`Union[float, List[float]]`, *optional*, defaults to `self.image_std`): + image_std (`Union[float, list[float]]`, *optional*, defaults to `self.image_std`): The standard deviation to divide the input by when normalizing. Only has an effect if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): @@ -458,7 +458,7 @@ class OwlViTImageProcessor(BaseImageProcessor): image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ # TODO: (amy) add support for other frameworks @@ -495,7 +495,7 @@ class OwlViTImageProcessor(BaseImageProcessor): self, outputs: "OwlViTObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`OwlViTForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -506,12 +506,12 @@ class OwlViTImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. @@ -562,7 +562,7 @@ class OwlViTImageProcessor(BaseImageProcessor): None, predictions will not be unnormalized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. All labels are set to None as `OwlViTForObjectDetection.image_guided_detection` perform one-shot object detection. """ diff --git a/src/transformers/models/owlvit/image_processing_owlvit_fast.py b/src/transformers/models/owlvit/image_processing_owlvit_fast.py index 8e8114451fb..8689ac72dc4 100644 --- a/src/transformers/models/owlvit/image_processing_owlvit_fast.py +++ b/src/transformers/models/owlvit/image_processing_owlvit_fast.py @@ -15,7 +15,7 @@ """Fast Image processor class for OwlViT""" import warnings -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union from ...image_processing_utils_fast import BaseImageProcessorFast from ...image_transforms import center_to_corners_format @@ -65,7 +65,7 @@ class OwlViTImageProcessorFast(BaseImageProcessorFast): image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ # TODO: (amy) add support for other frameworks @@ -103,7 +103,7 @@ class OwlViTImageProcessorFast(BaseImageProcessorFast): self, outputs: "OwlViTObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the raw output of [`OwlViTForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -114,12 +114,12 @@ class OwlViTImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. @@ -171,7 +171,7 @@ class OwlViTImageProcessorFast(BaseImageProcessorFast): None, predictions will not be unnormalized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. All labels are set to None as `OwlViTForObjectDetection.image_guided_detection` perform one-shot object detection. """ diff --git a/src/transformers/models/owlvit/modeling_owlvit.py b/src/transformers/models/owlvit/modeling_owlvit.py index eee13334628..d487706611d 100644 --- a/src/transformers/models/owlvit/modeling_owlvit.py +++ b/src/transformers/models/owlvit/modeling_owlvit.py @@ -16,7 +16,7 @@ from dataclasses import dataclass from functools import lru_cache -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.utils.checkpoint @@ -69,7 +69,7 @@ class OwlViTOutput(ModelOutput): image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`): The image embeddings obtained by applying the projection layer to the pooled output of [`OwlViTVisionModel`]. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`OwlViTTextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`OwlViTVisionModel`]. @@ -83,7 +83,7 @@ class OwlViTOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -185,14 +185,14 @@ class OwlViTObjectDetectionOutput(ModelOutput): class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`): Class embeddings of all image patches. OWL-ViT represents images as a set of image patches where the total number of patches is (image_size / patch_size)**2. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`OwlViTTextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`OwlViTVisionModel`]. """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None text_embeds: Optional[torch.FloatTensor] = None @@ -201,7 +201,7 @@ class OwlViTObjectDetectionOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -235,7 +235,7 @@ class OwlViTImageGuidedObjectDetectionOutput(ModelOutput): class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`): Class embeddings of all image patches. OWL-ViT represents images as a set of image patches where the total number of patches is (image_size / patch_size)**2. - text_model_output (Tuple[`BaseModelOutputWithPooling`]): + text_model_output (tuple[`BaseModelOutputWithPooling`]): The output of the [`OwlViTTextModel`]. vision_model_output (`BaseModelOutputWithPooling`): The output of the [`OwlViTVisionModel`]. @@ -250,7 +250,7 @@ class OwlViTImageGuidedObjectDetectionOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -392,7 +392,7 @@ class OwlViTAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, embed_dim = hidden_states.size() @@ -500,7 +500,7 @@ class OwlViTEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -605,7 +605,7 @@ class OwlViTEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`). @@ -690,7 +690,7 @@ class OwlViTTextTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -771,7 +771,7 @@ class OwlViTTextModel(OwlViTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -820,7 +820,7 @@ class OwlViTVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -878,7 +878,7 @@ class OwlViTVisionModel(OwlViTPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: ```python @@ -1041,7 +1041,7 @@ class OwlViTModel(OwlViTPreTrainedModel): interpolate_pos_encoding: bool = False, return_base_image_embeds: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, OwlViTOutput]: + ) -> Union[tuple, OwlViTOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. @@ -1159,7 +1159,7 @@ class OwlViTClassPredictionHead(nn.Module): image_embeds: torch.FloatTensor, query_embeds: Optional[torch.FloatTensor], query_mask: Optional[torch.Tensor], - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: image_class_embeds = self.dense0(image_embeds) if query_embeds is None: device = image_class_embeds.device @@ -1285,7 +1285,7 @@ class OwlViTForObjectDetection(OwlViTPreTrainedModel): image_feats: torch.FloatTensor, query_embeds: Optional[torch.FloatTensor] = None, query_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: image_feats: @@ -1307,7 +1307,7 @@ class OwlViTForObjectDetection(OwlViTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: # Encode text and image outputs = self.owlvit( pixel_values=pixel_values, @@ -1356,7 +1356,7 @@ class OwlViTForObjectDetection(OwlViTPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: # Get OwlViTModel vision embeddings (same as CLIP) vision_outputs = self.owlvit.vision_model( pixel_values=pixel_values, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=True diff --git a/src/transformers/models/owlvit/processing_owlvit.py b/src/transformers/models/owlvit/processing_owlvit.py index 564b198ac14..089f1eb26eb 100644 --- a/src/transformers/models/owlvit/processing_owlvit.py +++ b/src/transformers/models/owlvit/processing_owlvit.py @@ -17,7 +17,7 @@ Image/Text processor class for OWL-ViT """ import warnings -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -95,7 +95,7 @@ class OwlViTProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, # The following is to capture `query_images` argument that may be passed as a positional argument. # See transformers.processing_utils.ProcessorMixin.prepare_and_validate_optional_call_args for more details, # or this conversation for more context: https://github.com/huggingface/transformers/pull/32544#discussion_r1720208116 @@ -114,15 +114,15 @@ class OwlViTProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, - `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, + `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The query image to be prepared, one query image is expected per target image to be queried. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. @@ -160,10 +160,10 @@ class OwlViTProcessor(ProcessorMixin): data = {} if text is not None: - if isinstance(text, str) or (isinstance(text, List) and not isinstance(text[0], List)): + if isinstance(text, str) or (isinstance(text, list) and not isinstance(text[0], list)): encodings = [self.tokenizer(text, **output_kwargs["text_kwargs"])] - elif isinstance(text, List) and isinstance(text[0], List): + elif isinstance(text, list) and isinstance(text[0], list): encodings = [] # Maximum number of queries across batch @@ -241,8 +241,8 @@ class OwlViTProcessor(ProcessorMixin): self, outputs: "OwlViTObjectDetectionOutput", threshold: float = 0.1, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, - text_labels: Optional[List[List[str]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, + text_labels: Optional[list[list[str]]] = None, ): """ Converts the raw output of [`OwlViTForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -253,15 +253,15 @@ class OwlViTProcessor(ProcessorMixin): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.1): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. - text_labels (`List[List[str]]`, *optional*): + text_labels (`list[list[str]]`, *optional*): List of lists of text labels for each image in the batch. If unset, "text_labels" in output will be set to `None`. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "labels": Indexes of the classes predicted by the model on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. @@ -290,7 +290,7 @@ class OwlViTProcessor(ProcessorMixin): outputs: "OwlViTImageGuidedObjectDetectionOutput", threshold: float = 0.0, nms_threshold: float = 0.3, - target_sizes: Optional[Union[TensorType, List[Tuple]]] = None, + target_sizes: Optional[Union[TensorType, list[tuple]]] = None, ): """ Converts the output of [`OwlViTForObjectDetection.image_guided_detection`] into the format expected by the COCO @@ -309,7 +309,7 @@ class OwlViTProcessor(ProcessorMixin): None, predictions will not be unnormalized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the following keys: + `list[Dict]`: A list of dictionaries, each dictionary containing the following keys: - "scores": The confidence scores for each predicted box on the image. - "boxes": Image bounding boxes in (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format. - "labels": Set to `None`. diff --git a/src/transformers/models/paligemma/modeling_paligemma.py b/src/transformers/models/paligemma/modeling_paligemma.py index 4782da29ce4..64055cbe1c7 100644 --- a/src/transformers/models/paligemma/modeling_paligemma.py +++ b/src/transformers/models/paligemma/modeling_paligemma.py @@ -15,7 +15,7 @@ """PyTorch PaliGemmamodel.""" from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -102,9 +102,9 @@ class PaliGemmaCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -265,7 +265,7 @@ class PaliGemmaModel(PaliGemmaPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -275,7 +275,7 @@ class PaliGemmaModel(PaliGemmaPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, PaligemmaModelOutputWithPast]: + ) -> Union[tuple, PaligemmaModelOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -447,7 +447,7 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel, GenerationMixi pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -458,7 +458,7 @@ class PaliGemmaForConditionalGeneration(PaliGemmaPreTrainedModel, GenerationMixi return_dict: Optional[bool] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, PaliGemmaCausalLMOutputWithPast]: + ) -> Union[tuple, PaliGemmaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/paligemma/processing_paligemma.py b/src/transformers/models/paligemma/processing_paligemma.py index 1440ea7b66f..d724a8e0f14 100644 --- a/src/transformers/models/paligemma/processing_paligemma.py +++ b/src/transformers/models/paligemma/processing_paligemma.py @@ -16,7 +16,7 @@ Processor class for PaliGemma. """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -42,7 +42,7 @@ EXTRA_TOKENS = [f"4}>" for i in range(1024)] + [f"3}>" for i i class PaliGemmaTextKwargs(TextKwargs): - suffix: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] + suffix: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] class PaliGemmaImagesKwargs(ImagesKwargs): @@ -90,7 +90,7 @@ def build_string_from_input(prompt, bos_token, image_seq_len, image_token, num_i The output will be: "Initial str" Args: - prompt (`List[Union[str, ImageInput]]`): The input prompt. + prompt (`list[Union[str, ImageInput]]`): The input prompt. bos_token (`str`): The beginning of sentence token. image_seq_len (`int`): The length of the image sequence. image_token (`str`): The image token. @@ -154,7 +154,7 @@ class PaliGemmaProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[PaliGemmaProcessorKwargs], @@ -186,11 +186,11 @@ class PaliGemmaProcessor(ProcessorMixin): Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -201,7 +201,7 @@ class PaliGemmaProcessor(ProcessorMixin): - `'pt'`: Return PyTorch `torch.Tensor` objects. - `'np'`: Return NumPy `np.ndarray` objects. - `'jax'`: Return JAX `jnp.ndarray` objects. - suffix (`str`, `List[str]`, `List[List[str]]`): + suffix (`str`, `list[str]`, `list[list[str]]`): The suffixes or batch of suffixes to be encoded. Only necessary for finetuning. See https://github.com/google-research/big_vision/blob/main/big_vision/configs/proj/paligemma/README.md for more information. If your prompt is " What is on the image", the suffix corresponds to the expected prediction "a cow sitting on a bench". @@ -250,7 +250,7 @@ class PaliGemmaProcessor(ProcessorMixin): "each text has and add special tokens." ) - if isinstance(text, List) and isinstance(images, List): + if isinstance(text, list) and isinstance(images, list): if len(images) != len(text): raise ValueError( f"Received {len(images)} images for {len(text)} prompts. Each prompt should be associated with an image or list of images." @@ -327,10 +327,10 @@ class PaliGemmaProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (List[List[str]], *optional*): + image_sizes (list[list[str]], *optional*): The input sizes formatted as (height, width) per each image. Returns: - Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio") + dict[str, list[int]]: A dictionary mapping each modality ("image", "video", "audio") to a list containing the number of placeholder tokens required. If the model doesn't accept a certain modality or no input sizes are provided, the dict value is set to an empty list. """ diff --git a/src/transformers/models/patchtsmixer/configuration_patchtsmixer.py b/src/transformers/models/patchtsmixer/configuration_patchtsmixer.py index 856553336f3..9725bd75463 100644 --- a/src/transformers/models/patchtsmixer/configuration_patchtsmixer.py +++ b/src/transformers/models/patchtsmixer/configuration_patchtsmixer.py @@ -14,7 +14,7 @@ # limitations under the License. """PatchTSMixer model configuration""" -from typing import List, Optional, Union +from typing import Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -175,11 +175,11 @@ class PatchTSMixerConfig(PretrainedConfig): # Pretrain model configuration mask_type: str = "random", random_mask_ratio: float = 0.5, - num_forecast_mask_patches: Optional[Union[List[int], int]] = [2], + num_forecast_mask_patches: Optional[Union[list[int], int]] = [2], mask_value: int = 0, masked_loss: bool = True, channel_consistent_masking: bool = True, - unmasked_channel_indices: Optional[List[int]] = None, + unmasked_channel_indices: Optional[list[int]] = None, # General head configuration head_dropout: float = 0.2, distribution_output: str = "student_t", diff --git a/src/transformers/models/patchtsmixer/modeling_patchtsmixer.py b/src/transformers/models/patchtsmixer/modeling_patchtsmixer.py index 8f00e890092..7f7906ba3d4 100644 --- a/src/transformers/models/patchtsmixer/modeling_patchtsmixer.py +++ b/src/transformers/models/patchtsmixer/modeling_patchtsmixer.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -307,14 +307,14 @@ class PatchTSMixerAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1021,7 +1021,7 @@ class PatchTSMixerStdScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1058,7 +1058,7 @@ class PatchTSMixerMeanScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1110,7 +1110,7 @@ class PatchTSMixerNOPScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: Optional[torch.Tensor] = None - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1138,7 +1138,7 @@ class PatchTSMixerEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class PatchTSMixerEncoder(PatchTSMixerPreTrainedModel): @@ -1172,7 +1172,7 @@ class PatchTSMixerEncoder(PatchTSMixerPreTrainedModel): past_values: torch.Tensor, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PatchTSMixerEncoderOutput]: + ) -> Union[tuple, PatchTSMixerEncoderOutput]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, seq_length, num_input_channels)`): Context values of the time series. For a pretraining task, this denotes the input time series to @@ -1233,7 +1233,7 @@ class PatchTSMixerModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None patch_input: Optional[torch.FloatTensor] = None mask: Optional[torch.FloatTensor] = None loc: Optional[torch.FloatTensor] = None @@ -1361,7 +1361,7 @@ class PatchTSMixerForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_outputs: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class PatchTSMixerForPretraining(PatchTSMixerPreTrainedModel): @@ -1483,7 +1483,7 @@ class PatchTSMixerForPredictionOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_outputs: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None @@ -1782,7 +1782,7 @@ class PatchTSMixerForTimeSeriesClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_outputs: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class PatchTSMixerForTimeSeriesClassification(PatchTSMixerPreTrainedModel): @@ -1914,7 +1914,7 @@ class PatchTSMixerForRegressionOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None regression_outputs: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class InjectScalerStatistics4D(nn.Module): diff --git a/src/transformers/models/patchtst/configuration_patchtst.py b/src/transformers/models/patchtst/configuration_patchtst.py index be19f2383ac..17cabe491c0 100644 --- a/src/transformers/models/patchtst/configuration_patchtst.py +++ b/src/transformers/models/patchtst/configuration_patchtst.py @@ -14,7 +14,7 @@ # limitations under the License. """PatchTST model configuration""" -from typing import List, Optional, Union +from typing import Optional, Union from transformers.configuration_utils import PretrainedConfig from transformers.utils import logging @@ -180,16 +180,16 @@ class PatchTSTConfig(PretrainedConfig): do_mask_input: Optional[bool] = None, mask_type: str = "random", random_mask_ratio: float = 0.5, - num_forecast_mask_patches: Optional[Union[List[int], int]] = [2], + num_forecast_mask_patches: Optional[Union[list[int], int]] = [2], channel_consistent_masking: Optional[bool] = False, - unmasked_channel_indices: Optional[List[int]] = None, + unmasked_channel_indices: Optional[list[int]] = None, mask_value: int = 0, # head pooling_type: str = "mean", head_dropout: float = 0.0, prediction_length: int = 24, num_targets: int = 1, - output_range: Optional[List] = None, + output_range: Optional[list] = None, # distribution head num_parallel_samples: int = 100, **kwargs, diff --git a/src/transformers/models/patchtst/modeling_patchtst.py b/src/transformers/models/patchtst/modeling_patchtst.py index b85e8a66b25..9817d080208 100755 --- a/src/transformers/models/patchtst/modeling_patchtst.py +++ b/src/transformers/models/patchtst/modeling_patchtst.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -104,14 +104,14 @@ class PatchTSTAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -794,8 +794,8 @@ class PatchTSTModelOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None mask: Optional[torch.FloatTensor] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None @@ -827,8 +827,8 @@ class PatchTSTForPretrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -856,8 +856,8 @@ class PatchTSTForRegressionOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None regression_outputs: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -889,8 +889,8 @@ class PatchTSTForPredictionOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_outputs: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None loc: Optional[torch.FloatTensor] = None scale: Optional[torch.FloatTensor] = None @@ -921,8 +921,8 @@ class PatchTSTForClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -987,7 +987,7 @@ class PatchTSTStdScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1024,7 +1024,7 @@ class PatchTSTMeanScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1076,7 +1076,7 @@ class PatchTSTNOPScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: Optional[torch.Tensor] = None - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1103,7 +1103,7 @@ class PatchTSTScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -1147,7 +1147,7 @@ class PatchTSTModel(PatchTSTPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PatchTSTModelOutput]: + ) -> Union[tuple, PatchTSTModelOutput]: r""" Parameters: past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*): @@ -1283,7 +1283,7 @@ class PatchTSTForPretraining(PatchTSTPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PatchTSTForPretrainingOutput]: + ) -> Union[tuple, PatchTSTForPretrainingOutput]: r""" Parameters: past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*): @@ -1649,7 +1649,7 @@ class PatchTSTForPrediction(PatchTSTPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PatchTSTForPredictionOutput]: + ) -> Union[tuple, PatchTSTForPredictionOutput]: r""" Parameters: past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*): diff --git a/src/transformers/models/pegasus/convert_pegasus_tf_to_pytorch.py b/src/transformers/models/pegasus/convert_pegasus_tf_to_pytorch.py index cf183b590c1..9251c9a92ac 100644 --- a/src/transformers/models/pegasus/convert_pegasus_tf_to_pytorch.py +++ b/src/transformers/models/pegasus/convert_pegasus_tf_to_pytorch.py @@ -16,7 +16,6 @@ import argparse import os from pathlib import Path -from typing import Dict import tensorflow as tf import torch @@ -85,7 +84,7 @@ def convert_pegasus(tf_weights: dict, cfg_updates: dict) -> PegasusForConditiona return torch_model -def get_tf_weights_as_numpy(path="./ckpt/aeslc/model.ckpt-32000") -> Dict: +def get_tf_weights_as_numpy(path="./ckpt/aeslc/model.ckpt-32000") -> dict: init_vars = tf.train.list_variables(path) tf_weights = {} ignore_name = ["Adafactor", "global_step"] diff --git a/src/transformers/models/pegasus/modeling_flax_pegasus.py b/src/transformers/models/pegasus/modeling_flax_pegasus.py index 5bbe62c8569..ddf0ae49240 100644 --- a/src/transformers/models/pegasus/modeling_flax_pegasus.py +++ b/src/transformers/models/pegasus/modeling_flax_pegasus.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -194,7 +194,7 @@ PEGASUS_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -313,7 +313,7 @@ class FlaxPegasusAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -434,7 +434,7 @@ class FlaxPegasusEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -559,7 +559,7 @@ class FlaxPegasusDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) @@ -893,7 +893,7 @@ class FlaxPegasusPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: PegasusConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -902,7 +902,7 @@ class FlaxPegasusPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/pegasus/modeling_pegasus.py b/src/transformers/models/pegasus/modeling_pegasus.py index d723e4c6440..6922d3c815b 100755 --- a/src/transformers/models/pegasus/modeling_pegasus.py +++ b/src/transformers/models/pegasus/modeling_pegasus.py @@ -16,7 +16,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -196,7 +196,7 @@ class PegasusAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1247,7 +1247,7 @@ class PegasusModel(PegasusPreTrainedModel): self.encoder.resize_position_embeddings(new_num_position_embeddings) self.decoder.resize_position_embeddings(new_num_position_embeddings) - def get_position_embeddings(self) -> Tuple[nn.Embedding]: + def get_position_embeddings(self) -> tuple[nn.Embedding]: """ Returns the position embeddings matrix """ @@ -1263,8 +1263,8 @@ class PegasusModel(PegasusPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1272,7 +1272,7 @@ class PegasusModel(PegasusPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqModelOutput]: + ) -> Union[tuple, Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1433,7 +1433,7 @@ class PegasusForConditionalGeneration(PegasusPreTrainedModel, GenerationMixin): self.model.encoder.resize_position_embeddings(new_num_position_embeddings) self.model.decoder.resize_position_embeddings(new_num_position_embeddings) - def get_position_embeddings(self) -> Tuple[nn.Embedding]: + def get_position_embeddings(self) -> tuple[nn.Embedding]: """ Returns the position embeddings matrix """ @@ -1449,8 +1449,8 @@ class PegasusForConditionalGeneration(PegasusPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -1459,7 +1459,7 @@ class PegasusForConditionalGeneration(PegasusPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1654,7 +1654,7 @@ class PegasusForCausalLM(PegasusPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1662,7 +1662,7 @@ class PegasusForCausalLM(PegasusPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/pegasus/modeling_tf_pegasus.py b/src/transformers/models/pegasus/modeling_tf_pegasus.py index c584e6da948..48f9e0b6953 100644 --- a/src/transformers/models/pegasus/modeling_tf_pegasus.py +++ b/src/transformers/models/pegasus/modeling_tf_pegasus.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -215,11 +215,11 @@ class TFPegasusAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -463,9 +463,9 @@ class TFPegasusDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)* @@ -683,7 +683,7 @@ PEGASUS_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -933,7 +933,7 @@ class TFPegasusDecoder(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[tf.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[tf.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -983,7 +983,7 @@ class TFPegasusDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1169,8 +1169,8 @@ class TFPegasusMainLayer(keras.layers.Layer): head_mask: tf.Tensor | None = None, decoder_head_mask: tf.Tensor | None = None, cross_attn_head_mask: tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[tf.Tensor]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[tf.Tensor]]] = None, inputs_embeds: tf.Tensor | None = None, decoder_inputs_embeds: tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1290,8 +1290,8 @@ class TFPegasusModel(TFPegasusPreTrainedModel): head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1300,7 +1300,7 @@ class TFPegasusModel(TFPegasusPreTrainedModel): return_dict: Optional[bool] = None, training: bool = False, **kwargs, - ) -> Union[TFSeq2SeqModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqModelOutput, tuple[tf.Tensor]]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -1427,7 +1427,7 @@ class TFPegasusForConditionalGeneration(TFPegasusPreTrainedModel, TFCausalLangua decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: Optional[TFBaseModelOutput] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1436,7 +1436,7 @@ class TFPegasusForConditionalGeneration(TFPegasusPreTrainedModel, TFCausalLangua return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFSeq2SeqLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqLMOutput, tuple[tf.Tensor]]: """ labels (`tf.tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/pegasus/tokenization_pegasus.py b/src/transformers/models/pegasus/tokenization_pegasus.py index 19b5f5aaa5b..b8a4a1c737d 100644 --- a/src/transformers/models/pegasus/tokenization_pegasus.py +++ b/src/transformers/models/pegasus/tokenization_pegasus.py @@ -14,7 +14,7 @@ # limitations under the License. import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -106,7 +106,7 @@ class PegasusTokenizer(PreTrainedTokenizer): mask_token_sent="", additional_special_tokens=None, offset=103, # entries 2 - 104 are only used for pretraining - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.offset = offset @@ -175,7 +175,7 @@ class PegasusTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.sp_model) + self.offset - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab @@ -195,7 +195,7 @@ class PegasusTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" return self.sp_model.encode(text, out_type=str) @@ -236,8 +236,8 @@ class PegasusTokenizer(PreTrainedTokenizer): return [1 if x in all_special_ids else 0 for x in seq] def get_special_tokens_mask( - self, token_ids_0: List, token_ids_1: Optional[List] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list, token_ids_1: Optional[list] = None, already_has_special_tokens: bool = False + ) -> list[int]: """Get list where entries are [1] if a token is [eos] or [pad] else 0.""" if already_has_special_tokens: return self._special_token_mask(token_ids_0) @@ -246,7 +246,7 @@ class PegasusTokenizer(PreTrainedTokenizer): else: return self._special_token_mask(token_ids_0 + token_ids_1) + [1] - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """ Build model inputs from a sequence or a pair of sequences for sequence classification tasks by concatenating and adding special tokens. A PEGASUS sequence has the following format, where `X` represents the sequence: @@ -271,7 +271,7 @@ class PegasusTokenizer(PreTrainedTokenizer): # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_0 + token_ids_1 + [self.eos_token_id] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/pegasus/tokenization_pegasus_fast.py b/src/transformers/models/pegasus/tokenization_pegasus_fast.py index 3105d6f1090..92a37c44ff2 100644 --- a/src/transformers/models/pegasus/tokenization_pegasus_fast.py +++ b/src/transformers/models/pegasus/tokenization_pegasus_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging @@ -161,8 +161,8 @@ class PegasusTokenizerFast(PreTrainedTokenizerFast): return [1 if x in all_special_ids else 0 for x in seq] def get_special_tokens_mask( - self, token_ids_0: List, token_ids_1: Optional[List] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list, token_ids_1: Optional[list] = None, already_has_special_tokens: bool = False + ) -> list[int]: """Get list where entries are [1] if a token is [eos] or [pad] else 0.""" if already_has_special_tokens: return self._special_token_mask(token_ids_0) @@ -171,7 +171,7 @@ class PegasusTokenizerFast(PreTrainedTokenizerFast): else: return self._special_token_mask(token_ids_0 + token_ids_1) + [1] - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """ Build model inputs from a sequence by adding eos to the end. no bos token is added to the front. @@ -192,7 +192,7 @@ class PegasusTokenizerFast(PreTrainedTokenizerFast): # We don't expect to process pairs, but leave the pair logic for API consistency return token_ids_0 + token_ids_1 + [self.eos_token_id] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/pegasus_x/modeling_pegasus_x.py b/src/transformers/models/pegasus_x/modeling_pegasus_x.py index 40d7d1db733..0c48aa61412 100755 --- a/src/transformers/models/pegasus_x/modeling_pegasus_x.py +++ b/src/transformers/models/pegasus_x/modeling_pegasus_x.py @@ -16,7 +16,7 @@ import dataclasses import math -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -222,7 +222,7 @@ class PegasusXAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -334,7 +334,7 @@ class PegasusXGlobalLocalAttention(nn.Module): global_hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" dim = DimensionInfo( batch_size=token_hidden_states.shape[0], @@ -1498,7 +1498,7 @@ class PegasusXModel(PegasusXPreTrainedModel): self.encoder.resize_position_embeddings(new_num_position_embeddings) self.decoder.resize_position_embeddings(new_num_position_embeddings) - def get_position_embeddings(self) -> Tuple[nn.Embedding]: + def get_position_embeddings(self) -> tuple[nn.Embedding]: """ Returns the position embeddings matrix """ @@ -1511,8 +1511,8 @@ class PegasusXModel(PegasusXPreTrainedModel): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.Tensor] = None, decoder_attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1520,7 +1520,7 @@ class PegasusXModel(PegasusXPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqModelOutput]: + ) -> Union[tuple, Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1654,7 +1654,7 @@ class PegasusXForConditionalGeneration(PegasusXPreTrainedModel, GenerationMixin) self.model.encoder.resize_position_embeddings(new_num_position_embeddings) self.model.decoder.resize_position_embeddings(new_num_position_embeddings) - def get_position_embeddings(self) -> Tuple[nn.Embedding]: + def get_position_embeddings(self) -> tuple[nn.Embedding]: """ Returns the position embeddings matrix """ @@ -1667,8 +1667,8 @@ class PegasusXForConditionalGeneration(PegasusXPreTrainedModel, GenerationMixin) attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.Tensor] = None, decoder_attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -1677,7 +1677,7 @@ class PegasusXForConditionalGeneration(PegasusXPreTrainedModel, GenerationMixin) output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/perceiver/configuration_perceiver.py b/src/transformers/models/perceiver/configuration_perceiver.py index 4605f1fb529..91e7bcd58fd 100644 --- a/src/transformers/models/perceiver/configuration_perceiver.py +++ b/src/transformers/models/perceiver/configuration_perceiver.py @@ -85,7 +85,7 @@ class PerceiverConfig(PretrainedConfig): this to something large just in case (e.g., 512 or 1024 or 2048). image_size (`int`, *optional*, defaults to 56): Size of the images after preprocessing, for [`PerceiverForImageClassificationLearned`]. - train_size (`List[int]`, *optional*, defaults to `[368, 496]`): + train_size (`list[int]`, *optional*, defaults to `[368, 496]`): Training size of the images for the optical flow model. num_frames (`int`, *optional*, defaults to 16): Number of video frames used for the multimodal autoencoding model. @@ -93,7 +93,7 @@ class PerceiverConfig(PretrainedConfig): Number of audio samples per frame for the multimodal autoencoding model. samples_per_patch (`int`, *optional*, defaults to 16): Number of audio samples per patch when preprocessing the audio for the multimodal autoencoding model. - output_shape (`List[int]`, *optional*, defaults to `[1, 16, 224, 224]`): + output_shape (`list[int]`, *optional*, defaults to `[1, 16, 224, 224]`): Shape of the output (batch_size, num_frames, height, width) for the video decoder queries of the multimodal autoencoding model. This excludes the channel dimension. output_num_channels (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/perceiver/image_processing_perceiver.py b/src/transformers/models/perceiver/image_processing_perceiver.py index 2edd4bef93e..7550bc9471d 100644 --- a/src/transformers/models/perceiver/image_processing_perceiver.py +++ b/src/transformers/models/perceiver/image_processing_perceiver.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Perceiver.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -55,13 +55,13 @@ class PerceiverImageProcessor(BaseImageProcessor): Whether or not to center crop the image. If the input size if smaller than `crop_size` along any edge, the image will be padded with zeros and then center cropped. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 256, "width": 256}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 256, "width": 256}`): Desired output size when applying center-cropping. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image to `(size["height"], size["width"])`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by the `size` parameter in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Defines the resampling filter to use if resizing the image. Can be overridden by the `resample` parameter @@ -75,10 +75,10 @@ class PerceiverImageProcessor(BaseImageProcessor): do_normalize: Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -88,15 +88,15 @@ class PerceiverImageProcessor(BaseImageProcessor): def __init__( self, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -119,7 +119,7 @@ class PerceiverImageProcessor(BaseImageProcessor): def center_crop( self, image: np.ndarray, - crop_size: Dict[str, int], + crop_size: dict[str, int], size: Optional[int] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -135,9 +135,9 @@ class PerceiverImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to center crop. - crop_size (`Dict[str, int]`): + crop_size (`dict[str, int]`): Desired output size after applying the center crop. - size (`Dict[str, int]`, *optional*): + size (`dict[str, int]`, *optional*): Size of the image after resizing. If not provided, the self.size attribute will be used. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format of the image. If not provided, it will be the same as the input image. @@ -164,7 +164,7 @@ class PerceiverImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -176,7 +176,7 @@ class PerceiverImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -214,15 +214,15 @@ class PerceiverImageProcessor(BaseImageProcessor): self, images: ImageInput, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -236,11 +236,11 @@ class PerceiverImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image to `crop_size`. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Desired output size after applying the center crop. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only @@ -251,9 +251,9 @@ class PerceiverImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/perceiver/image_processing_perceiver_fast.py b/src/transformers/models/perceiver/image_processing_perceiver_fast.py index 3dcdd1ffcb7..16a90bcf880 100644 --- a/src/transformers/models/perceiver/image_processing_perceiver_fast.py +++ b/src/transformers/models/perceiver/image_processing_perceiver_fast.py @@ -67,9 +67,9 @@ class PerceiverImageProcessorFast(BaseImageProcessorFast): Args: image (`"torch.Tensor"`): Image to center crop. - crop_size (`Dict[str, int]`): + crop_size (`dict[str, int]`): Desired output size after applying the center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. Returns: diff --git a/src/transformers/models/perceiver/modeling_perceiver.py b/src/transformers/models/perceiver/modeling_perceiver.py index e6903cf205e..1a6f65cf6c4 100755 --- a/src/transformers/models/perceiver/modeling_perceiver.py +++ b/src/transformers/models/perceiver/modeling_perceiver.py @@ -20,7 +20,7 @@ from collections.abc import Mapping from dataclasses import dataclass from functools import reduce from operator import __add__ -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np import torch @@ -37,7 +37,7 @@ from .configuration_perceiver import PerceiverConfig ModalitySizeType = Mapping[str, int] -PreprocessorOutputType = Tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor] +PreprocessorOutputType = tuple[torch.Tensor, Optional[torch.Tensor], torch.Tensor] PreprocessorType = Callable[..., PreprocessorOutputType] PostprocessorType = Callable[..., Any] @@ -70,9 +70,9 @@ class PerceiverModelOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -90,7 +90,7 @@ class PerceiverDecoderOutput(ModelOutput): """ logits: Optional[torch.FloatTensor] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -119,9 +119,9 @@ class PerceiverMaskedLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -151,9 +151,9 @@ class PerceiverClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None class PerceiverEmbeddings(nn.Module): @@ -224,7 +224,7 @@ class PerceiverSelfAttention(nn.Module): inputs: Optional[torch.FloatTensor] = None, inputs_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: hidden_states = self.layernorm1(hidden_states) inputs = self.layernorm2(inputs) @@ -369,7 +369,7 @@ class PerceiverAttention(nn.Module): inputs: Optional[torch.FloatTensor] = None, inputs_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -448,7 +448,7 @@ class PerceiverLayer(nn.Module): inputs: Optional[torch.FloatTensor] = None, inputs_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: attention_outputs = self.attention( hidden_states, attention_mask, @@ -537,7 +537,7 @@ class PerceiverEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions else None @@ -684,17 +684,17 @@ class PerceiverModel(PerceiverPreTrainedModel): self, inputs: torch.FloatTensor, attention_mask: Optional[torch.FloatTensor] = None, - subsampled_output_points: Optional[Dict[str, torch.Tensor]] = None, + subsampled_output_points: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PerceiverModelOutput]: + ) -> Union[tuple, PerceiverModelOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. - subsampled_output_points (`Dict[str, torch.Tensor]`, *optional*): + subsampled_output_points (`dict[str, torch.Tensor]`, *optional*): Dictionary of tensors used as queries for the decoder. The decoder maps these queries to the latent representation of the model. Used for subsampled decoding, e.g. when only decoding certain image patches. @@ -934,7 +934,7 @@ class PerceiverForMaskedLM(PerceiverPreTrainedModel): labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, input_ids: Optional[torch.Tensor] = None, - ) -> Union[Tuple, PerceiverMaskedLMOutput]: + ) -> Union[tuple, PerceiverMaskedLMOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1061,7 +1061,7 @@ class PerceiverForSequenceClassification(PerceiverPreTrainedModel): labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, input_ids: Optional[torch.Tensor] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1195,7 +1195,7 @@ class PerceiverForImageClassificationLearned(PerceiverPreTrainedModel): interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, pixel_values: Optional[torch.Tensor] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1338,7 +1338,7 @@ class PerceiverForImageClassificationFourier(PerceiverPreTrainedModel): labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, pixel_values: Optional[torch.Tensor] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1480,7 +1480,7 @@ class PerceiverForImageClassificationConvProcessing(PerceiverPreTrainedModel): labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, pixel_values: Optional[torch.Tensor] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1638,7 +1638,7 @@ class PerceiverForOpticalFlow(PerceiverPreTrainedModel): output_hidden_states: Optional[bool] = None, labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1842,13 +1842,13 @@ class PerceiverForMultimodalAutoencoding(PerceiverPreTrainedModel): self, inputs: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, - subsampled_output_points: Optional[Dict[str, torch.Tensor]] = None, + subsampled_output_points: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, labels: Optional[torch.Tensor] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, PerceiverClassifierOutput]: + ) -> Union[tuple, PerceiverClassifierOutput]: r""" inputs (`torch.FloatTensor`): Inputs to the perceiver. Can be anything: images, text, audio, video, etc. @@ -1856,7 +1856,7 @@ class PerceiverForMultimodalAutoencoding(PerceiverPreTrainedModel): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If `config.num_labels > 1` a classification loss is computed (Cross-Entropy). - subsampled_output_points (`Dict[str, torch.Tensor]`, *optional*): + subsampled_output_points (`dict[str, torch.Tensor]`, *optional*): Dictionary of tensors used as queries for the decoder. The decoder maps these queries to the latent representation of the model. Used for subsampled decoding, e.g. when only decoding certain image patches. @@ -2286,14 +2286,14 @@ class PerceiverBasicVideoAutoencodingDecoder(PerceiverAbstractDecoder): Args: config ([*PerceiverConfig*]): Model configuration. - output_shape (`List[int]`): + output_shape (`list[int]`): Shape of the output as (batch_size, num_frames, height, width), excluding the channel dimension. position_encoding_type (`str`): The type of position encoding to use. Can be either "trainable", "fourier", or "none". """ def __init__( - self, config: PerceiverConfig, output_shape: List[int], position_encoding_type: str, **decoder_kwargs + self, config: PerceiverConfig, output_shape: list[int], position_encoding_type: str, **decoder_kwargs ) -> None: super().__init__() if len(output_shape) != 4: # B, T, H, W @@ -2367,7 +2367,7 @@ class PerceiverMultimodalDecoder(PerceiverAbstractDecoder): Args: config ([*PerceiverConfig*]): Model configuration. - modalities (`Dict[str, PerceiverAbstractDecoder]`): + modalities (`dict[str, PerceiverAbstractDecoder]`): Dictionary mapping modality name to the decoder of that modality. num_outputs (`int`): The number of outputs of the decoder. @@ -2376,7 +2376,7 @@ class PerceiverMultimodalDecoder(PerceiverAbstractDecoder): min_padding_size (`int`, *optional*, defaults to 2): The minimum padding size for all modalities. The final output will have num_channels equal to the maximum channels across all modalities plus min_padding_size. - subsampled_index_dims (`Dict[str, PerceiverAbstractDecoder]`, *optional*): + subsampled_index_dims (`dict[str, PerceiverAbstractDecoder]`, *optional*): Dictionary mapping modality name to the subsampled index dimensions to use for the decoder query of that modality. """ @@ -2384,11 +2384,11 @@ class PerceiverMultimodalDecoder(PerceiverAbstractDecoder): def __init__( self, config: PerceiverConfig, - modalities: Dict[str, PerceiverAbstractDecoder], + modalities: dict[str, PerceiverAbstractDecoder], num_outputs: int, output_num_channels: int, min_padding_size: Optional[int] = 2, - subsampled_index_dims: Optional[Dict[str, PerceiverAbstractDecoder]] = None, + subsampled_index_dims: Optional[dict[str, PerceiverAbstractDecoder]] = None, **decoder_kwargs, ) -> None: super().__init__() @@ -2589,7 +2589,7 @@ def generate_fourier_features(pos, num_bands, max_resolution=(224, 224), concat_ The Tensor containing the position of n points in d dimensional space. num_bands (`int`): The number of frequency bands (K) to use. - max_resolution (`Tuple[int]`, *optional*, defaults to (224, 224)): + max_resolution (`tuple[int]`, *optional*, defaults to (224, 224)): The maximum resolution (i.e. the number of pixels per dim). A tuple representing resolution for each dimension. concat_pos (`bool`, *optional*, defaults to `True`): Whether to concatenate the input position encoding to the Fourier features. @@ -2637,9 +2637,9 @@ def build_linear_positions(index_dims, output_range=(-1.0, 1.0)): Generate an array of position indices for an N-D input array. Args: - index_dims (`List[int]`): + index_dims (`list[int]`): The shape of the index dimensions of the input array. - output_range (`Tuple[float]`, *optional*, defaults to `(-1.0, 1.0)`): + output_range (`tuple[float]`, *optional*, defaults to `(-1.0, 1.0)`): The min and max values taken by each input index dimension. Returns: @@ -2733,7 +2733,7 @@ def _check_or_build_spatial_positions(pos, index_dims, batch_size): Args: pos (`torch.FloatTensor`): None, or an array of position features. If None, position features are built. Otherwise, their size is checked. - index_dims (`List[int]`): + index_dims (`list[int]`): An iterable giving the spatial/index size of the data to be featurized. batch_size (`int`): The batch size of the data to be featurized. @@ -2783,7 +2783,7 @@ class PerceiverFourierPositionEncoding(PerceiverAbstractPositionEncoding): def forward( self, - index_dims: List[int], + index_dims: list[int], batch_size: int, device: torch.device, dtype: torch.dtype, @@ -3347,7 +3347,7 @@ class PerceiverMultimodalPreprocessor(AbstractPreprocessor): Args: modalities (`Mapping[str, PreprocessorType]`): Dict mapping modality name to preprocessor. - mask_probs (`Dict[str, float]`): + mask_probs (`dict[str, float]`): Dict mapping modality name to masking probability of that modality. min_padding_size (`int`, *optional*, defaults to 2): The minimum padding size for all modalities. The final output will have num_channels equal to the maximum diff --git a/src/transformers/models/perceiver/tokenization_perceiver.py b/src/transformers/models/perceiver/tokenization_perceiver.py index 2a7fe6f43c6..f17e7e99ac9 100644 --- a/src/transformers/models/perceiver/tokenization_perceiver.py +++ b/src/transformers/models/perceiver/tokenization_perceiver.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization class for Perceiver.""" -from typing import Dict, List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken, PreTrainedTokenizer from ...utils import logging @@ -77,7 +77,7 @@ class PerceiverTokenizer(PreTrainedTokenizer): self._utf_vocab_size = 2**8 # utf is 8 bits # Since these tokens are not part of the vocabulary, we manually add them - self._added_tokens_decoder: Dict[str, int] = { + self._added_tokens_decoder: dict[str, int] = { 0: pad_token, 1: bos_token, 2: eos_token, @@ -97,7 +97,7 @@ class PerceiverTokenizer(PreTrainedTokenizer): **kwargs, ) - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: vocab = {} for i in range(self._utf_vocab_size): token = chr(i) @@ -110,22 +110,22 @@ class PerceiverTokenizer(PreTrainedTokenizer): return self._utf_vocab_size def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -138,8 +138,8 @@ class PerceiverTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks. A sequence has the following format: @@ -148,20 +148,20 @@ class PerceiverTokenizer(PreTrainedTokenizer): - pair of sequences: `[CLS] A [SEP] B [SEP]` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] else: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] + token_ids_1 + [self.sep_token_id] - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" tokens = [chr(i) for i in text.encode("utf-8")] return tokens @@ -193,7 +193,7 @@ class PerceiverTokenizer(PreTrainedTokenizer): return string # PerceiverTokenizer has no vocab file - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: return () diff --git a/src/transformers/models/persimmon/configuration_persimmon.py b/src/transformers/models/persimmon/configuration_persimmon.py index 80ca823f28e..3773ad4174d 100644 --- a/src/transformers/models/persimmon/configuration_persimmon.py +++ b/src/transformers/models/persimmon/configuration_persimmon.py @@ -85,11 +85,11 @@ class PersimmonConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/persimmon/modeling_persimmon.py b/src/transformers/models/persimmon/modeling_persimmon.py index a5376d651ce..ce142c8d6d2 100644 --- a/src/transformers/models/persimmon/modeling_persimmon.py +++ b/src/transformers/models/persimmon/modeling_persimmon.py @@ -19,7 +19,7 @@ # limitations under the License. """PyTorch Persimmon model.""" -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -202,7 +202,7 @@ class PersimmonAttention(nn.Module): self.attention_dropout = nn.Dropout(config.attention_dropout) self.rotary_emb = PersimmonRotaryEmbedding(config=self.config) - def _split_heads(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + def _split_heads(self, fused_qkv: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Split the last dimension into (num_heads, head_dim) without making any copies, results share same memory storage as `fused_qkv` @@ -227,9 +227,9 @@ class PersimmonAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() # [batch_size, seq_length, 3 x hidden_size] @@ -314,13 +314,13 @@ class PersimmonDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -340,7 +340,7 @@ class PersimmonDecoderLayer(nn.Module): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -450,7 +450,7 @@ class PersimmonModel(PersimmonPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -737,7 +737,7 @@ class PersimmonForCausalLM(PersimmonPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/phi/configuration_phi.py b/src/transformers/models/phi/configuration_phi.py index 222722e19b0..0a3e6a6787e 100644 --- a/src/transformers/models/phi/configuration_phi.py +++ b/src/transformers/models/phi/configuration_phi.py @@ -100,11 +100,11 @@ class PhiConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/phi/modeling_phi.py b/src/transformers/models/phi/modeling_phi.py index 3f2deffd9e0..5edceb27c0d 100644 --- a/src/transformers/models/phi/modeling_phi.py +++ b/src/transformers/models/phi/modeling_phi.py @@ -5,7 +5,7 @@ # modular_phi.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 from functools import partial -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn as nn @@ -132,12 +132,12 @@ class PhiAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -219,13 +219,13 @@ class PhiDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/phi/modular_phi.py b/src/transformers/models/phi/modular_phi.py index c1d40774bcd..c515e13e723 100644 --- a/src/transformers/models/phi/modular_phi.py +++ b/src/transformers/models/phi/modular_phi.py @@ -1,5 +1,5 @@ from functools import partial -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.nn as nn @@ -55,12 +55,12 @@ class PhiAttention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -131,13 +131,13 @@ class PhiDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/phi3/modeling_phi3.py b/src/transformers/models/phi3/modeling_phi3.py index 08f93a468b5..79703927021 100644 --- a/src/transformers/models/phi3/modeling_phi3.py +++ b/src/transformers/models/phi3/modeling_phi3.py @@ -20,7 +20,7 @@ # limitations under the License. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -164,12 +164,12 @@ class Phi3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -254,9 +254,9 @@ class Phi3DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): diff --git a/src/transformers/models/phi3/modular_phi3.py b/src/transformers/models/phi3/modular_phi3.py index 9c341388807..4469c31769b 100644 --- a/src/transformers/models/phi3/modular_phi3.py +++ b/src/transformers/models/phi3/modular_phi3.py @@ -15,7 +15,7 @@ """PyTorch Phi-3 model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.utils.checkpoint @@ -116,12 +116,12 @@ class Phi3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -182,9 +182,9 @@ class Phi3DecoderLayer(MistralDecoderLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): diff --git a/src/transformers/models/phi4_multimodal/feature_extraction_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/feature_extraction_phi4_multimodal.py index df2559e531a..b7111fe0cac 100644 --- a/src/transformers/models/phi4_multimodal/feature_extraction_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/feature_extraction_phi4_multimodal.py @@ -155,7 +155,7 @@ class Phi4MultimodalFeatureExtractor(SequenceFeatureExtractor): the STFT computation if available, otherwise a slower NumPy based one. Args: - raw_speech (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + raw_speech (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The sequence or batch of sequences to be processed. Each sequence can be a numpy array or PyTorch tensor. For batched inputs, sequences can be a list of numpy arrays or PyTorch tensors, or a single numpy array or PyTorch tensor with first dimension being the batch size. diff --git a/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal_fast.py b/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal_fast.py index 2273293b4c4..1f079005b01 100644 --- a/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal_fast.py +++ b/src/transformers/models/phi4_multimodal/image_processing_phi4_multimodal_fast.py @@ -13,7 +13,7 @@ # limitations under the License. import math -from typing import List, Optional, Union +from typing import Optional, Union import torch @@ -174,7 +174,7 @@ class Phi4MultimodalImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], size: SizeDict, interpolation: Optional["F.InterpolationMode"], patch_size: int, @@ -182,8 +182,8 @@ class Phi4MultimodalImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: Optional[float], do_normalize: bool, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, ): diff --git a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py index d484f9255a8..a9a902598c1 100644 --- a/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/modeling_phi4_multimodal.py @@ -20,7 +20,7 @@ import math import warnings -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -111,7 +111,7 @@ class Phi4MultimodalVisionAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -154,7 +154,7 @@ class Phi4MultimodalVisionEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -1399,12 +1399,12 @@ class Phi4MultimodalAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -1468,9 +1468,9 @@ class Phi4MultimodalDecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): @@ -1684,7 +1684,7 @@ class Phi4MultimodalModel(Phi4MultimodalPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, image_pixel_values: Optional[torch.FloatTensor] = None, image_sizes: Optional[torch.LongTensor] = None, @@ -1847,7 +1847,7 @@ class Phi4MultimodalForCausalLM(Phi4MultimodalPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, image_pixel_values: Optional[torch.FloatTensor] = None, image_sizes: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/phi4_multimodal/modular_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/modular_phi4_multimodal.py index 62fb4e1d509..28cd2de9fbc 100644 --- a/src/transformers/models/phi4_multimodal/modular_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/modular_phi4_multimodal.py @@ -13,7 +13,7 @@ # limitations under the License. import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -489,7 +489,7 @@ class Phi4MultimodalVisionAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -1500,7 +1500,7 @@ class Phi4MultimodalModel(Phi3Model, nn.Module): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, image_pixel_values: Optional[torch.FloatTensor] = None, image_sizes: Optional[torch.LongTensor] = None, @@ -1640,7 +1640,7 @@ class Phi4MultimodalForCausalLM(Phi3ForCausalLM, nn.Module): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, image_pixel_values: Optional[torch.FloatTensor] = None, image_sizes: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/phi4_multimodal/processing_phi4_multimodal.py b/src/transformers/models/phi4_multimodal/processing_phi4_multimodal.py index ebccdc04608..b9a97391e99 100644 --- a/src/transformers/models/phi4_multimodal/processing_phi4_multimodal.py +++ b/src/transformers/models/phi4_multimodal/processing_phi4_multimodal.py @@ -17,7 +17,7 @@ Processor class for Phi4Multimodal """ import re -from typing import List, Optional, Union +from typing import Optional, Union from ...audio_utils import AudioInput from ...image_processing_utils import BatchFeature @@ -78,7 +78,7 @@ class Phi4MultimodalProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, List[TextInput]], + text: Union[TextInput, list[TextInput]], images: Optional[ImageInput] = None, audio: Optional[AudioInput] = None, **kwargs: Unpack[ProcessingKwargs], @@ -91,14 +91,14 @@ class Phi4MultimodalProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - audio (`List[Union[np.ndarray, torch.Tensor]]`): + audio (`list[Union[np.ndarray, torch.Tensor]]`): List of the audios to be prepared. Returns: diff --git a/src/transformers/models/phimoe/modeling_phimoe.py b/src/transformers/models/phimoe/modeling_phimoe.py index bcb8b7f0f70..2a924078339 100644 --- a/src/transformers/models/phimoe/modeling_phimoe.py +++ b/src/transformers/models/phimoe/modeling_phimoe.py @@ -16,7 +16,7 @@ """PyTorch Phimoe model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -53,7 +53,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -269,8 +269,8 @@ class PhimoeAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -337,7 +337,7 @@ class PhimoeFlashAttention2(PhimoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ): bsz, q_len, _ = hidden_states.size() @@ -431,8 +431,8 @@ class PhimoeSdpaAttention(PhimoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -568,7 +568,7 @@ class MultiplierProcessor(torch.autograd.Function): grad_at_output (torch.Tensor): Gradient at the output. Returns: - Tuple[torch.Tensor, None, None, None, None]: Gradients for the inputs. + tuple[torch.Tensor, None, None, None, None]: Gradients for the inputs. """ multiplier, selected_experts, masked_gates = ctx.saved_tensors @@ -606,7 +606,7 @@ def sparsemixer(scores, jitter_eps, training, top_k=2): top_k (int): Number of top experts to select. Returns: - Tuple[torch.Tensor, torch.Tensor]: Multiplier and selected experts tensors. + tuple[torch.Tensor, torch.Tensor]: Multiplier and selected experts tensors. """ if top_k != 2: raise ValueError("top_k must be equal to 2") @@ -808,14 +808,14 @@ class PhimoeDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -941,7 +941,7 @@ class PhimoeModel(PhimoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1269,7 +1269,7 @@ class PhimoeForCausalLM(PhimoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/phobert/tokenization_phobert.py b/src/transformers/models/phobert/tokenization_phobert.py index 471b3a89f85..61ac8194b45 100644 --- a/src/transformers/models/phobert/tokenization_phobert.py +++ b/src/transformers/models/phobert/tokenization_phobert.py @@ -18,7 +18,7 @@ import os import re from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -144,8 +144,8 @@ class PhobertTokenizer(PreTrainedTokenizer): ) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A PhoBERT sequence has the following format: @@ -154,13 +154,13 @@ class PhobertTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -170,22 +170,22 @@ class PhobertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -198,20 +198,20 @@ class PhobertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. PhoBERT does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] @@ -295,7 +295,7 @@ class PhobertTokenizer(PreTrainedTokenizer): out_string = " ".join(tokens).replace("@@ ", "").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/pix2struct/image_processing_pix2struct.py b/src/transformers/models/pix2struct/image_processing_pix2struct.py index ff303bf0692..e434c01fe1f 100644 --- a/src/transformers/models/pix2struct/image_processing_pix2struct.py +++ b/src/transformers/models/pix2struct/image_processing_pix2struct.py @@ -16,7 +16,7 @@ import io import math -from typing import Dict, Optional, Union +from typing import Optional, Union import numpy as np from huggingface_hub import hf_hub_download @@ -197,7 +197,7 @@ class Pix2StructImageProcessor(BaseImageProcessor): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. According to Pix2Struct paper and code, the image is normalized with its own mean and standard deviation. - patch_size (`Dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`): + patch_size (`dict[str, int]`, *optional*, defaults to `{"height": 16, "width": 16}`): The patch size to use for the image. According to Pix2Struct paper and code, the patch size is 16x16. max_patches (`int`, *optional*, defaults to 2048): The maximum number of patches to extract from the image as per the [Pix2Struct @@ -213,7 +213,7 @@ class Pix2StructImageProcessor(BaseImageProcessor): self, do_convert_rgb: bool = True, do_normalize: bool = True, - patch_size: Optional[Dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, max_patches: int = 2048, is_vqa: bool = False, **kwargs, @@ -352,7 +352,7 @@ class Pix2StructImageProcessor(BaseImageProcessor): do_convert_rgb: Optional[bool] = None, do_normalize: Optional[bool] = None, max_patches: Optional[int] = None, - patch_size: Optional[Dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -369,7 +369,7 @@ class Pix2StructImageProcessor(BaseImageProcessor): Args: images (`ImageInput`): Image to preprocess. Expects a single or batch of images. - header_text (`Union[List[str], str]`, *optional*): + header_text (`Union[list[str], str]`, *optional*): Text to render as a header. Only has an effect if `image_processor.is_vqa` is `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the image to RGB. diff --git a/src/transformers/models/pix2struct/modeling_pix2struct.py b/src/transformers/models/pix2struct/modeling_pix2struct.py index 65088ec424d..54c956601a5 100644 --- a/src/transformers/models/pix2struct/modeling_pix2struct.py +++ b/src/transformers/models/pix2struct/modeling_pix2struct.py @@ -15,7 +15,7 @@ """Pix2Struct modeling file""" import math -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -278,7 +278,7 @@ class Pix2StructVisionLayer(nn.Module): attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: residual = hidden_states # in Pix2StructVision, layernorm is applied before self-attention @@ -505,7 +505,7 @@ class Pix2StructVisionModel(Pix2StructPreTrainedModel): def get_input_embeddings(self): return self.embeddings.patch_projection - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -522,7 +522,7 @@ class Pix2StructVisionModel(Pix2StructPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" flattened_patches (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_channels x patch_height x patch_width)`): Flattened and padded pixel values. These values can be obtained using [`AutoImageProcessor`]. See @@ -1103,7 +1103,7 @@ class Pix2StructTextModel(Pix2StructPreTrainedModel): inputs_embeds: Optional[torch.LongTensor] = None, head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1111,7 +1111,7 @@ class Pix2StructTextModel(Pix2StructPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs, - ) -> Union[Tuple[torch.FloatTensor, ...], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.FloatTensor, ...], CausalLMOutputWithCrossAttentions]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Pix2StructText is a model with relative position @@ -1520,8 +1520,8 @@ class Pix2StructForConditionalGeneration(Pix2StructPreTrainedModel, GenerationMi head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, labels: Optional[torch.LongTensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1529,7 +1529,7 @@ class Pix2StructForConditionalGeneration(Pix2StructPreTrainedModel, GenerationMi output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" flattened_patches (`torch.FloatTensor` of shape `(batch_size, seq_length, hidden_size)`): Flattened pixel patches. the `hidden_size` is obtained by the following formula: `hidden_size` = diff --git a/src/transformers/models/pix2struct/processing_pix2struct.py b/src/transformers/models/pix2struct/processing_pix2struct.py index f9b1fcc4408..a386c38ec2b 100644 --- a/src/transformers/models/pix2struct/processing_pix2struct.py +++ b/src/transformers/models/pix2struct/processing_pix2struct.py @@ -16,7 +16,7 @@ Processor class for Pix2Struct. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...processing_utils import ImagesKwargs, ProcessingKwargs, ProcessorMixin, Unpack @@ -26,7 +26,7 @@ from ...utils import logging class Pix2StructImagesKwargs(ImagesKwargs, total=False): max_patches: Optional[int] - header_text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] + header_text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] class Pix2StructProcessorKwargs(ProcessingKwargs, total=False): @@ -78,7 +78,7 @@ class Pix2StructProcessor(ProcessorMixin): def __call__( self, images=None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[Pix2StructProcessorKwargs], diff --git a/src/transformers/models/pixtral/image_processing_pixtral.py b/src/transformers/models/pixtral/image_processing_pixtral.py index 7cd9149e385..b82e803a92e 100644 --- a/src/transformers/models/pixtral/image_processing_pixtral.py +++ b/src/transformers/models/pixtral/image_processing_pixtral.py @@ -15,7 +15,7 @@ """Image processor class for Pixtral.""" import math -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -76,14 +76,14 @@ def convert_to_rgb(image: ImageInput) -> ImageInput: return new_image -def _num_image_tokens(image_size: Tuple[int, int], patch_size: Tuple[int, int]) -> int: +def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: """ Calculate the number of image tokens given the image size and patch size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The size of the image as `(height, width)`. - patch_size (`Tuple[int, int]`): + patch_size (`tuple[int, int]`): The patch size as `(height, width)`. Returns: @@ -98,8 +98,8 @@ def _num_image_tokens(image_size: Tuple[int, int], patch_size: Tuple[int, int]) def get_resize_output_image_size( input_image: ImageInput, - size: Union[int, Tuple[int, int], List[int], Tuple[int]], - patch_size: Union[int, Tuple[int, int], List[int], Tuple[int]], + size: Union[int, tuple[int, int], list[int], tuple[int]], + patch_size: Union[int, tuple[int, int], list[int], tuple[int]], input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> tuple: """ @@ -109,9 +109,9 @@ def get_resize_output_image_size( Args: input_image (`ImageInput`): The image to resize. - size (`int` or `Tuple[int, int]`): + size (`int` or `tuple[int, int]`): Max image size an input image can be. Must be a dictionary with the key "longest_edge". - patch_size (`int` or `Tuple[int, int]`): + patch_size (`int` or `tuple[int, int]`): The patch_size as `(height, width)` to use for resizing the image. If patch_size is an integer, `(patch_size, patch_size)` will be used input_data_format (`ChannelDimension`, *optional*): @@ -144,10 +144,10 @@ class PixtralImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"longest_edge": 1024}`): + size (`dict[str, int]` *optional*, defaults to `{"longest_edge": 1024}`): Size of the maximum dimension of either the height or width dimension of the image. Used to control how images are resized. If either the height or width are greater than `size["longest_edge"]` then both the height and width are rescaled by `height / ratio`, `width /ratio` where `ratio = max(height / longest_edge, width / longest_edge)` - patch_size (`Dict[str, int]` *optional*, defaults to `{"height": 16, "width": 16}`): + patch_size (`dict[str, int]` *optional*, defaults to `{"height": 16, "width": 16}`): Size of the patches in the model, used to calculate the output image size. Can be overridden by `patch_size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. @@ -159,10 +159,10 @@ class PixtralImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -175,14 +175,14 @@ class PixtralImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - patch_size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -221,8 +221,8 @@ class PixtralImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], - patch_size: Dict[str, int], + size: dict[str, int], + patch_size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -235,9 +235,9 @@ class PixtralImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dict containing the longest possible edge of the image. - patch_size (`Dict[str, int]`): + patch_size (`dict[str, int]`): Patch size used to calculate the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -275,17 +275,17 @@ class PixtralImageProcessor(BaseImageProcessor): def _pad_for_batching( self, - pixel_values: List[np.ndarray], - image_sizes: List[List[int]], + pixel_values: list[np.ndarray], + image_sizes: list[list[int]], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): """ Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[np.ndarray]`): + pixel_values (`list[np.ndarray]`): An array of pixel values of each images of shape (`batch_size`, `height`, `width`, `channels`) - image_sizes (`List[List[int]]`): + image_sizes (`list[list[int]]`): A list of sizes for each image in `pixel_values` in (height, width) format. data_format (`str` or `ChannelDimension`, *optional*): The channel dimension format for the output image. Can be one of: @@ -298,7 +298,7 @@ class PixtralImageProcessor(BaseImageProcessor): - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. If unset, will use the inferred format of the input image. Returns: - List[`np.ndarray`]: The padded images. + list[`np.ndarray`]: The padded images. """ max_shape = ( @@ -320,14 +320,14 @@ class PixtralImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, - patch_size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, + patch_size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -343,9 +343,9 @@ class PixtralImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Describes the maximum input dimensions to the model. - patch_size (`Dict[str, int]`, *optional*, defaults to `self.patch_size`): + patch_size (`dict[str, int]`, *optional*, defaults to `self.patch_size`): Patch size in the model. Used to calculate the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -356,9 +356,9 @@ class PixtralImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/pixtral/image_processing_pixtral_fast.py b/src/transformers/models/pixtral/image_processing_pixtral_fast.py index 0b5bd193c08..97cb652bb89 100644 --- a/src/transformers/models/pixtral/image_processing_pixtral_fast.py +++ b/src/transformers/models/pixtral/image_processing_pixtral_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Pixtral.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -60,11 +60,11 @@ if is_torchvision_available(): class PixtralFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): """ - patch_size (`Dict[str, int]` *optional*, defaults to `{"height": 16, "width": 16}`): + patch_size (`dict[str, int]` *optional*, defaults to `{"height": 16, "width": 16}`): Size of the patches in the model, used to calculate the output image size. Can be overridden by `patch_size` in the `preprocess` method. """ - patch_size: Optional[Dict[str, int]] + patch_size: Optional[dict[str, int]] @auto_docstring @@ -129,18 +129,18 @@ class PixtralImageProcessorFast(BaseImageProcessorFast): # Adapted from transformers.models.pixtral.image_processing_pixtral.PixtralImageProcessor._pad_for_batching def _pad_for_batching( self, - pixel_values: List[torch.Tensor], - image_sizes: List[List[int]], + pixel_values: list[torch.Tensor], + image_sizes: list[list[int]], ): """ Pads images on the `num_of_patches` dimension with zeros to form a batch of same number of patches. Args: - pixel_values (`List[torch.Tensor]`): + pixel_values (`list[torch.Tensor]`): An array of pixel values of each images of shape (`batch_size`, `channels`, `height`, `width`) - image_sizes (`List[List[int]]`): + image_sizes (`list[list[int]]`): A list of sizes for each image in `pixel_values` in (height, width) format. Returns: - List[`torch.Tensor`]: The padded images. + list[`torch.Tensor`]: The padded images. """ max_shape = (max([size[0] for size in image_sizes]), max([size[1] for size in image_sizes])) @@ -152,18 +152,18 @@ class PixtralImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, - patch_size: Dict[str, int], + patch_size: dict[str, int], interpolation: Optional["F.InterpolationMode"], do_center_crop: bool, - crop_size: Dict[str, int], + crop_size: dict[str, int], do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], ) -> BatchFeature: patch_size = get_size_dict(patch_size, default_to_square=True) diff --git a/src/transformers/models/pixtral/modeling_pixtral.py b/src/transformers/models/pixtral/modeling_pixtral.py index c416179131c..998124a8da6 100644 --- a/src/transformers/models/pixtral/modeling_pixtral.py +++ b/src/transformers/models/pixtral/modeling_pixtral.py @@ -15,7 +15,7 @@ """PyTorch Pixtral model.""" from collections.abc import Callable -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -182,10 +182,10 @@ class PixtralAttention(nn.Module): self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, output_attentions: Optional[bool] = False, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, patches, _ = hidden_states.size() @@ -284,10 +284,10 @@ class PixtralAttentionLayer(nn.Module): self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, output_attentions: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -335,12 +335,12 @@ class PixtralTransformer(nn.Module): self, inputs_embeds, attention_mask: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -483,7 +483,7 @@ class PixtralVisionModel(PixtralPreTrainedModel): return_dict: Optional[bool] = None, *args, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: if image_sizes is None: batch_size, _, height, width = pixel_values.shape image_sizes = [(height, width)] * batch_size diff --git a/src/transformers/models/pixtral/processing_pixtral.py b/src/transformers/models/pixtral/processing_pixtral.py index 9225d4df6b2..849fa3e011e 100644 --- a/src/transformers/models/pixtral/processing_pixtral.py +++ b/src/transformers/models/pixtral/processing_pixtral.py @@ -16,7 +16,7 @@ Processor class for Pixtral. """ -from typing import List, Union +from typing import Union import numpy as np @@ -120,7 +120,7 @@ class PixtralProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[PixtralProcessorKwargs], @@ -133,10 +133,10 @@ class PixtralProcessor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -239,7 +239,7 @@ class PixtralProcessor(ProcessorMixin): Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: diff --git a/src/transformers/models/plbart/modeling_plbart.py b/src/transformers/models/plbart/modeling_plbart.py index 6399653cd07..a192fe70e23 100644 --- a/src/transformers/models/plbart/modeling_plbart.py +++ b/src/transformers/models/plbart/modeling_plbart.py @@ -21,7 +21,7 @@ import copy import math -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -393,7 +393,7 @@ class PLBartAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -491,7 +491,7 @@ class PLBartEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -590,7 +590,7 @@ class PLBartEncoder(PLBartPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -758,7 +758,7 @@ class PLBartDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -883,14 +883,14 @@ class PLBartDecoder(PLBartPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1192,8 +1192,8 @@ class PLBartModel(PLBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.LongTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -1201,7 +1201,7 @@ class PLBartModel(PLBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1349,8 +1349,8 @@ class PLBartForConditionalGeneration(PLBartPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.LongTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.Tensor] = None, @@ -1359,7 +1359,7 @@ class PLBartForConditionalGeneration(PLBartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1533,7 +1533,7 @@ class PLBartForSequenceClassification(PLBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1542,7 +1542,7 @@ class PLBartForSequenceClassification(PLBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1713,7 +1713,7 @@ class PLBartForCausalLM(PLBartPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1721,7 +1721,7 @@ class PLBartForCausalLM(PLBartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/plbart/modular_plbart.py b/src/transformers/models/plbart/modular_plbart.py index 1394e87f560..e5ec69efcf0 100644 --- a/src/transformers/models/plbart/modular_plbart.py +++ b/src/transformers/models/plbart/modular_plbart.py @@ -15,7 +15,7 @@ """PyTorch PLBART model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -323,8 +323,8 @@ class PLBartModel(PLBartPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.LongTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, @@ -332,7 +332,7 @@ class PLBartModel(PLBartPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -480,8 +480,8 @@ class PLBartForConditionalGeneration(PLBartPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.LongTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.Tensor] = None, @@ -490,7 +490,7 @@ class PLBartForConditionalGeneration(PLBartPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/plbart/tokenization_plbart.py b/src/transformers/models/plbart/tokenization_plbart.py index b9b73b6f474..7adeb11d6ab 100644 --- a/src/transformers/models/plbart/tokenization_plbart.py +++ b/src/transformers/models/plbart/tokenization_plbart.py @@ -15,7 +15,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -113,8 +113,8 @@ class PLBartTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -130,7 +130,7 @@ class PLBartTokenizer(PreTrainedTokenizer): tokenizer_file=None, src_lang=None, tgt_lang=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, additional_special_tokens=None, clean_up_tokenization_spaces=True, **kwargs, @@ -246,22 +246,22 @@ class PLBartTokenizer(PreTrainedTokenizer): self.set_src_lang_special_tokens(self._src_lang) def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -276,8 +276,8 @@ class PLBartTokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An PLBART sequence has the following format, where `X` represents the sequence: @@ -289,13 +289,13 @@ class PLBartTokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -303,20 +303,20 @@ class PLBartTokenizer(PreTrainedTokenizer): return self.prefix_tokens + token_ids_0 + token_ids_1 + self.suffix_tokens def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. PLBart does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] @@ -344,7 +344,7 @@ class PLBartTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -367,7 +367,7 @@ class PLBartTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -386,9 +386,9 @@ class PLBartTokenizer(PreTrainedTokenizer): def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "en_XX", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "python", **kwargs, ) -> BatchEncoding: diff --git a/src/transformers/models/poolformer/image_processing_poolformer.py b/src/transformers/models/poolformer/image_processing_poolformer.py index 25c00cc5fef..94e3c68ba42 100644 --- a/src/transformers/models/poolformer/image_processing_poolformer.py +++ b/src/transformers/models/poolformer/image_processing_poolformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for PoolFormer.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -55,7 +55,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. Can be overridden by `size` in the `preprocess` method. If crop_pct is unset: - size is `{"height": h, "width": w}`: the image is resized to `(h, w)`. @@ -78,7 +78,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): Whether to center crop the image. If the input size is smaller than `crop_size` along any edge, the image is padded with 0's and then center cropped. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after applying center crop. Only has an effect if `do_center_crop` is set to `True`. Can be overridden by the `crop_size` parameter in the `preprocess` method. rescale_factor (`int` or `float`, *optional*, defaults to `1/255`): @@ -90,10 +90,10 @@ class PoolFormerImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -103,16 +103,16 @@ class PoolFormerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_pct: int = 0.9, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, rescale_factor: Union[int, float] = 1 / 255, do_rescale: bool = True, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -136,7 +136,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], crop_pct: Optional[float] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -162,7 +162,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. crop_pct (`float`, *optional*): Percentage of the image that will be cropped from the center. If set, the image is resized @@ -214,16 +214,16 @@ class PoolFormerImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, crop_pct: Optional[int] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -237,7 +237,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after applying resize. crop_pct (`float`, *optional*, defaults to `self.crop_pct`): Percentage of the image to crop. Only has an effect if `do_resize` is set to `True`. @@ -246,7 +246,7 @@ class PoolFormerImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after applying center crop. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -254,9 +254,9 @@ class PoolFormerImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/poolformer/image_processing_poolformer_fast.py b/src/transformers/models/poolformer/image_processing_poolformer_fast.py index 0dcbfd15c55..00b5107f956 100644 --- a/src/transformers/models/poolformer/image_processing_poolformer_fast.py +++ b/src/transformers/models/poolformer/image_processing_poolformer_fast.py @@ -184,7 +184,7 @@ class PoolFormerImageProcessorFast(BaseImageProcessorFast): Args: image (`"torch.Tensor"`): Image to center crop. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. Returns: diff --git a/src/transformers/models/poolformer/modeling_poolformer.py b/src/transformers/models/poolformer/modeling_poolformer.py index 9fa2ff37431..ac6a27b3dd6 100755 --- a/src/transformers/models/poolformer/modeling_poolformer.py +++ b/src/transformers/models/poolformer/modeling_poolformer.py @@ -15,7 +15,7 @@ """PyTorch PoolFormer model.""" import collections.abc -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -286,7 +286,7 @@ class PoolFormerModel(PoolFormerPreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -349,7 +349,7 @@ class PoolFormerForImageClassification(PoolFormerPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutputWithNoAttention]: + ) -> Union[tuple, ImageClassifierOutputWithNoAttention]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/pop2piano/feature_extraction_pop2piano.py b/src/transformers/models/pop2piano/feature_extraction_pop2piano.py index e4a29de6a33..d7721942742 100644 --- a/src/transformers/models/pop2piano/feature_extraction_pop2piano.py +++ b/src/transformers/models/pop2piano/feature_extraction_pop2piano.py @@ -15,7 +15,7 @@ """Feature extractor class for Pop2Piano""" import warnings -from typing import List, Optional, Union +from typing import Optional, Union import numpy import numpy as np @@ -343,8 +343,8 @@ class Pop2PianoFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - audio: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], - sampling_rate: Union[int, List[int]], + audio: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], + sampling_rate: Union[int, list[int]], steps_per_beat: int = 2, resample: Optional[bool] = True, return_attention_mask: Optional[bool] = False, diff --git a/src/transformers/models/pop2piano/modeling_pop2piano.py b/src/transformers/models/pop2piano/modeling_pop2piano.py index f48ada2be86..b5e52e7c397 100644 --- a/src/transformers/models/pop2piano/modeling_pop2piano.py +++ b/src/transformers/models/pop2piano/modeling_pop2piano.py @@ -16,7 +16,7 @@ import copy import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -1154,8 +1154,8 @@ class Pop2PianoForConditionalGeneration(Pop2PianoPreTrainedModel, GenerationMixi head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, input_features: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1165,7 +1165,7 @@ class Pop2PianoForConditionalGeneration(Pop2PianoPreTrainedModel, GenerationMixi output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. Pop2Piano is a model with relative position embeddings diff --git a/src/transformers/models/pop2piano/processing_pop2piano.py b/src/transformers/models/pop2piano/processing_pop2piano.py index 437d4efaef2..0a96b1ecad0 100644 --- a/src/transformers/models/pop2piano/processing_pop2piano.py +++ b/src/transformers/models/pop2piano/processing_pop2piano.py @@ -15,7 +15,7 @@ """Processor class for Pop2Piano.""" import os -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -51,11 +51,11 @@ class Pop2PianoProcessor(ProcessorMixin): def __call__( self, - audio: Union[np.ndarray, List[float], List[np.ndarray]] = None, - sampling_rate: Optional[Union[int, List[int]]] = None, + audio: Union[np.ndarray, list[float], list[np.ndarray]] = None, + sampling_rate: Optional[Union[int, list[int]]] = None, steps_per_beat: int = 2, resample: Optional[bool] = True, - notes: Union[List, TensorType] = None, + notes: Union[list, TensorType] = None, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, max_length: Optional[int] = None, diff --git a/src/transformers/models/pop2piano/tokenization_pop2piano.py b/src/transformers/models/pop2piano/tokenization_pop2piano.py index 22bf21e2f4b..433edc9186b 100644 --- a/src/transformers/models/pop2piano/tokenization_pop2piano.py +++ b/src/transformers/models/pop2piano/tokenization_pop2piano.py @@ -16,7 +16,7 @@ import json import os -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -341,7 +341,7 @@ class Pop2PianoTokenizer(PreTrainedTokenizer): new_pm.remove_invalid_notes() return new_pm - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: """ Saves the tokenizer's vocabulary dictionary to the provided save_directory. @@ -366,7 +366,7 @@ class Pop2PianoTokenizer(PreTrainedTokenizer): def encode_plus( self, - notes: Union[np.ndarray, List[pretty_midi.Note]], + notes: Union[np.ndarray, list[pretty_midi.Note]], truncation_strategy: Optional[TruncationStrategy] = None, max_length: Optional[int] = None, **kwargs, @@ -437,7 +437,7 @@ class Pop2PianoTokenizer(PreTrainedTokenizer): def batch_encode_plus( self, - notes: Union[np.ndarray, List[pretty_midi.Note]], + notes: Union[np.ndarray, list[pretty_midi.Note]], truncation_strategy: Optional[TruncationStrategy] = None, max_length: Optional[int] = None, **kwargs, @@ -478,8 +478,8 @@ class Pop2PianoTokenizer(PreTrainedTokenizer): self, notes: Union[ np.ndarray, - List[pretty_midi.Note], - List[List[pretty_midi.Note]], + list[pretty_midi.Note], + list[list[pretty_midi.Note]], ], padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py index cf213133c14..5405d443fe3 100644 --- a/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/configuration_prompt_depth_anything.py @@ -39,7 +39,7 @@ class PromptDepthAnythingConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - backbone_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*): + backbone_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*): The configuration of the backbone model. Only used in case `is_hybrid` is `True` or in case you want to leverage the [`AutoBackbone`] API. backbone (`str`, *optional*): @@ -60,9 +60,9 @@ class PromptDepthAnythingConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. reassemble_hidden_size (`int`, *optional*, defaults to 384): The number of input channels of the reassemble layers. - reassemble_factors (`List[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): + reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): The up/downsampling factors of the reassemble layers. - neck_hidden_sizes (`List[str]`, *optional*, defaults to `[48, 96, 192, 384]`): + neck_hidden_sizes (`list[str]`, *optional*, defaults to `[48, 96, 192, 384]`): The hidden sizes to project to for the feature maps of the backbone. fusion_hidden_size (`int`, *optional*, defaults to 64): The number of channels before fusion. @@ -157,7 +157,7 @@ class PromptDepthAnythingConfig(PretrainedConfig): def to_dict(self): """ Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) diff --git a/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py index e0fc90dee2c..e283411b4e9 100644 --- a/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/image_processing_prompt_depth_anything.py @@ -14,7 +14,7 @@ import math from collections.abc import Iterable -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union if TYPE_CHECKING: @@ -72,7 +72,7 @@ def _get_resize_output_image_size( keep_aspect_ratio: bool, multiple: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: output_size = (output_size, output_size) if isinstance(output_size, int) else output_size input_height, input_width = get_image_size(input_image, input_data_format) @@ -104,7 +104,7 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 384, "width": 384}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 384, "width": 384}`): Size of the image after resizing. Can be overridden by `size` in `preprocess`. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): Defines the resampling filter to use if resizing the image. Can be overridden by `resample` in `preprocess`. @@ -122,10 +122,10 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_pad (`bool`, *optional*, defaults to `False`): @@ -143,15 +143,15 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = False, size_divisor: Optional[int] = None, prompt_scale_to_meter: float = 0.001, # default unit is mm @@ -177,7 +177,7 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, resample: PILImageResampling = PILImageResampling.BICUBIC, @@ -193,7 +193,7 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Target size of the output image. keep_aspect_ratio (`bool`, *optional*, defaults to `False`): If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. @@ -287,8 +287,8 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, size_divisor: Optional[int] = None, prompt_scale_to_meter: Optional[float] = None, @@ -312,7 +312,7 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): to meters. This is useful when the prompt depth is not in meters. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. If `keep_aspect_ratio` is `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. If `ensure_multiple_of` is set, the image is resized to a size that is a multiple of this value. @@ -330,9 +330,9 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. prompt_scale_to_meter (`float`, *optional*, defaults to `self.prompt_scale_to_meter`): Scale factor to convert the prompt depth to meters. @@ -463,8 +463,8 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): def post_process_depth_estimation( self, outputs: "DepthEstimatorOutput", - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - ) -> List[Dict[str, TensorType]]: + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + ) -> list[dict[str, TensorType]]: """ Converts the raw output of [`DepthEstimatorOutput`] into final depth predictions and depth PIL images. Only supports PyTorch. @@ -472,12 +472,12 @@ class PromptDepthAnythingImageProcessor(BaseImageProcessor): Args: outputs ([`DepthEstimatorOutput`]): Raw outputs of the model. - target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ requires_backends(self, "torch") diff --git a/src/transformers/models/prompt_depth_anything/modeling_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/modeling_prompt_depth_anything.py index f4336cc1e30..2a463be89ff 100644 --- a/src/transformers/models/prompt_depth_anything/modeling_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/modeling_prompt_depth_anything.py @@ -16,7 +16,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -215,7 +215,7 @@ class PromptDepthAnythingDepthEstimationHead(nn.Module): raise ValueError(f"Unknown depth estimation type: {config.depth_estimation_type}") self.max_depth = config.max_depth - def forward(self, hidden_states: List[torch.Tensor], patch_height: int, patch_width: int) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor], patch_height: int, patch_width: int) -> torch.Tensor: hidden_states = hidden_states[-1] predicted_depth = self.conv1(hidden_states) @@ -298,10 +298,10 @@ class PromptDepthAnythingReassembleStage(nn.Module): for channels, factor in zip(config.neck_hidden_sizes, config.reassemble_factors): self.layers.append(PromptDepthAnythingReassembleLayer(config, channels=channels, factor=factor)) - def forward(self, hidden_states: List[torch.Tensor], patch_height=None, patch_width=None) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height=None, patch_width=None) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): List of hidden states from the backbone. """ out = [] @@ -345,14 +345,14 @@ class PromptDepthAnythingNeck(nn.Module): def forward( self, - hidden_states: List[torch.Tensor], + hidden_states: list[torch.Tensor], patch_height: Optional[int] = None, patch_width: Optional[int] = None, prompt_depth: Optional[torch.Tensor] = None, - ) -> List[torch.Tensor]: + ) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): List of hidden states from the backbone. """ if not isinstance(hidden_states, (tuple, list)): @@ -399,7 +399,7 @@ class PromptDepthAnythingForDepthEstimation(PromptDepthAnythingPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" prompt_depth (`torch.FloatTensor` of shape `(batch_size, 1, height, width)`, *optional*): Prompt depth is the sparse or low-resolution depth obtained from multi-view geometry or a diff --git a/src/transformers/models/prompt_depth_anything/modular_prompt_depth_anything.py b/src/transformers/models/prompt_depth_anything/modular_prompt_depth_anything.py index ae7275a4695..9563fa98846 100644 --- a/src/transformers/models/prompt_depth_anything/modular_prompt_depth_anything.py +++ b/src/transformers/models/prompt_depth_anything/modular_prompt_depth_anything.py @@ -10,7 +10,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -134,7 +134,7 @@ class PromptDepthAnythingFeatureFusionStage(DepthAnythingFeatureFusionStage): class PromptDepthAnythingDepthEstimationHead(DepthAnythingDepthEstimationHead): - def forward(self, hidden_states: List[torch.Tensor], patch_height: int, patch_width: int) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor], patch_height: int, patch_width: int) -> torch.Tensor: hidden_states = hidden_states[-1] predicted_depth = self.conv1(hidden_states) @@ -201,14 +201,14 @@ class PromptDepthAnythingReassembleStage(DepthAnythingReassembleStage): class PromptDepthAnythingNeck(DepthAnythingNeck): def forward( self, - hidden_states: List[torch.Tensor], + hidden_states: list[torch.Tensor], patch_height: Optional[int] = None, patch_width: Optional[int] = None, prompt_depth: Optional[torch.Tensor] = None, - ) -> List[torch.Tensor]: + ) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): List of hidden states from the backbone. """ if not isinstance(hidden_states, (tuple, list)): @@ -242,7 +242,7 @@ class PromptDepthAnythingForDepthEstimation(DepthAnythingForDepthEstimation): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" prompt_depth (`torch.FloatTensor` of shape `(batch_size, 1, height, width)`, *optional*): Prompt depth is the sparse or low-resolution depth obtained from multi-view geometry or a diff --git a/src/transformers/models/prophetnet/modeling_prophetnet.py b/src/transformers/models/prophetnet/modeling_prophetnet.py index 0f7cfcd224d..d7783c48e0a 100644 --- a/src/transformers/models/prophetnet/modeling_prophetnet.py +++ b/src/transformers/models/prophetnet/modeling_prophetnet.py @@ -18,7 +18,7 @@ import copy import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -124,7 +124,7 @@ class ProphetNetSeq2SeqLMOutput(ModelOutput): logits_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Prediction scores of the predict stream language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -175,15 +175,15 @@ class ProphetNetSeq2SeqLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None logits_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @property def decoder_cross_attentions(self): @@ -209,7 +209,7 @@ class ProphetNetSeq2SeqModelOutput(ModelOutput): hidden_size)` is output. last_hidden_state_ngram (`torch.FloatTensor` of shape `(batch_size,ngram * decoder_sequence_length, config.vocab_size)`, *optional*): Sequence of predict stream hidden-states at the output of the last layer of the decoder of the model. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -261,15 +261,15 @@ class ProphetNetSeq2SeqModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor last_hidden_state_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - decoder_ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + decoder_ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None @property def decoder_cross_attentions(self): @@ -294,7 +294,7 @@ class ProphetNetDecoderModelOutput(ModelOutput): hidden_size)` is output. last_hidden_state_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Sequence of predict stream hidden-states at the output of the last layer of the decoder of the model. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -333,12 +333,12 @@ class ProphetNetDecoderModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor last_hidden_state_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_ngram: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_ngram: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -355,7 +355,7 @@ class ProphetNetDecoderLMOutput(ModelOutput): logits_ngram (`torch.FloatTensor` of shape `(batch_size, ngram * decoder_sequence_length, config.vocab_size)`): Prediction scores of the predict stream language modeling head (scores for each vocabulary token before SoftMax). - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_attn_heads, decoder_sequence_length, embed_size_per_head)`). @@ -395,12 +395,12 @@ class ProphetNetDecoderLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None logits_ngram: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - hidden_states_ngram: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - ngram_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + hidden_states_ngram: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + ngram_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -521,9 +521,9 @@ class ProphetNetAttention(nn.Module): key_value_states: Optional[Tensor] = None, attention_mask: Optional[Tensor] = None, layer_head_mask: Optional[Tensor] = None, - past_key_value: Optional[Tuple[Tensor]] = None, + past_key_value: Optional[tuple[Tensor]] = None, output_attentions: bool = False, - ) -> Tuple[Tensor, Optional[Tensor]]: + ) -> tuple[Tensor, Optional[Tensor]]: batch_size, tgt_len, hidden_size = hidden_states.size() # if key_value_states are provided this layer is used as a cross-attention layer @@ -676,7 +676,7 @@ class ProphetNetNgramSelfAttention(nn.Module): def forward( self, hidden_states, - past_key_value: Optional[Tuple[Tensor]] = None, + past_key_value: Optional[tuple[Tensor]] = None, attention_mask=None, layer_head_mask=None, extended_predict_attention_mask=None, @@ -1127,7 +1127,7 @@ class ProphetNetEncoder(ProphetNetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Example: @@ -1264,13 +1264,13 @@ class ProphetNetDecoder(ProphetNetPreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ProphetNetDecoderModelOutput]: + ) -> Union[tuple, ProphetNetDecoderModelOutput]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: @@ -1610,15 +1610,15 @@ class ProphetNetModel(ProphetNetPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ProphetNetSeq2SeqModelOutput]: + ) -> Union[tuple, ProphetNetSeq2SeqModelOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1751,7 +1751,7 @@ class ProphetNetForConditionalGeneration(ProphetNetPreTrainedModel, GenerationMi decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, encoder_outputs: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -1759,7 +1759,7 @@ class ProphetNetForConditionalGeneration(ProphetNetPreTrainedModel, GenerationMi output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ProphetNetSeq2SeqLMOutput]: + ) -> Union[tuple, ProphetNetSeq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. @@ -1971,14 +1971,14 @@ class ProphetNetForCausalLM(ProphetNetPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ProphetNetDecoderLMOutput]: + ) -> Union[tuple, ProphetNetDecoderLMOutput]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/prophetnet/tokenization_prophetnet.py b/src/transformers/models/prophetnet/tokenization_prophetnet.py index 97e22b6fa2f..4724f3a3b70 100644 --- a/src/transformers/models/prophetnet/tokenization_prophetnet.py +++ b/src/transformers/models/prophetnet/tokenization_prophetnet.py @@ -17,7 +17,7 @@ import collections import os import unicodedata from collections.abc import Iterable -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -319,7 +319,7 @@ class ProphetNetTokenizer(PreTrainedTokenizer): # first name has to correspond to main model input name # to make sure `tokenizer.pad(...)` works correctly # `ProphetNet` doesn't have `token_type_ids` as argument. - model_input_names: List[str] = ["input_ids", "attention_mask"] + model_input_names: list[str] = ["input_ids", "attention_mask"] def __init__( self, @@ -404,10 +404,10 @@ class ProphetNetTokenizer(PreTrainedTokenizer): def get_special_tokens_mask( self, - token_ids_0: List[int], - token_ids_1: Optional[List[int]] = None, + token_ids_0: list[int], + token_ids_1: Optional[list[int]] = None, already_has_special_tokens: Optional[bool] = False, - ) -> List[int]: + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -432,7 +432,7 @@ class ProphetNetTokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( @@ -453,8 +453,8 @@ class ProphetNetTokenizer(PreTrainedTokenizer): return (vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: diff --git a/src/transformers/models/pvt/configuration_pvt.py b/src/transformers/models/pvt/configuration_pvt.py index ff7a375fa67..208295db71f 100644 --- a/src/transformers/models/pvt/configuration_pvt.py +++ b/src/transformers/models/pvt/configuration_pvt.py @@ -18,7 +18,7 @@ from collections import OrderedDict from collections.abc import Mapping -from typing import Callable, List +from typing import Callable from packaging import version @@ -47,19 +47,19 @@ class PvtConfig(PretrainedConfig): The number of input channels. num_encoder_blocks (`int`, *optional*, defaults to 4): The number of encoder blocks (i.e. stages in the Mix Transformer encoder). - depths (`List[int]`, *optional*, defaults to `[2, 2, 2, 2]`): + depths (`list[int]`, *optional*, defaults to `[2, 2, 2, 2]`): The number of layers in each encoder block. - sequence_reduction_ratios (`List[int]`, *optional*, defaults to `[8, 4, 2, 1]`): + sequence_reduction_ratios (`list[int]`, *optional*, defaults to `[8, 4, 2, 1]`): Sequence reduction ratios in each encoder block. - hidden_sizes (`List[int]`, *optional*, defaults to `[64, 128, 320, 512]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[64, 128, 320, 512]`): Dimension of each of the encoder blocks. - patch_sizes (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + patch_sizes (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Patch size before each encoder block. - strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + strides (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Stride before each encoder block. - num_attention_heads (`List[int]`, *optional*, defaults to `[1, 2, 5, 8]`): + num_attention_heads (`list[int]`, *optional*, defaults to `[1, 2, 5, 8]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - mlp_ratios (`List[int]`, *optional*, defaults to `[8, 8, 4, 4]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[8, 8, 4, 4]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): @@ -101,13 +101,13 @@ class PvtConfig(PretrainedConfig): image_size: int = 224, num_channels: int = 3, num_encoder_blocks: int = 4, - depths: List[int] = [2, 2, 2, 2], - sequence_reduction_ratios: List[int] = [8, 4, 2, 1], - hidden_sizes: List[int] = [64, 128, 320, 512], - patch_sizes: List[int] = [4, 2, 2, 2], - strides: List[int] = [4, 2, 2, 2], - num_attention_heads: List[int] = [1, 2, 5, 8], - mlp_ratios: List[int] = [8, 8, 4, 4], + depths: list[int] = [2, 2, 2, 2], + sequence_reduction_ratios: list[int] = [8, 4, 2, 1], + hidden_sizes: list[int] = [64, 128, 320, 512], + patch_sizes: list[int] = [4, 2, 2, 2], + strides: list[int] = [4, 2, 2, 2], + num_attention_heads: list[int] = [1, 2, 5, 8], + mlp_ratios: list[int] = [8, 8, 4, 4], hidden_act: Mapping[str, Callable] = "gelu", hidden_dropout_prob: float = 0.0, attention_probs_dropout_prob: float = 0.0, diff --git a/src/transformers/models/pvt/image_processing_pvt.py b/src/transformers/models/pvt/image_processing_pvt.py index 6915fff6f9d..e9ebecd503a 100644 --- a/src/transformers/models/pvt/image_processing_pvt.py +++ b/src/transformers/models/pvt/image_processing_pvt.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Pvt.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -62,10 +62,10 @@ class PvtImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -75,13 +75,13 @@ class PvtImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -100,7 +100,7 @@ class PvtImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -112,7 +112,7 @@ class PvtImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -150,13 +150,13 @@ class PvtImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -170,7 +170,7 @@ class PvtImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -182,9 +182,9 @@ class PvtImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/pvt/modeling_pvt.py b/src/transformers/models/pvt/modeling_pvt.py index ea81df51ebd..2b776e83b87 100755 --- a/src/transformers/models/pvt/modeling_pvt.py +++ b/src/transformers/models/pvt/modeling_pvt.py @@ -19,7 +19,7 @@ import collections import math from collections.abc import Iterable -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -120,7 +120,7 @@ class PvtPatchEmbeddings(nn.Module): interpolated_embeddings = interpolated_embeddings.reshape(1, -1, height * width).permute(0, 2, 1) return interpolated_embeddings - def forward(self, pixel_values: torch.Tensor) -> Tuple[torch.Tensor, int, int]: + def forward(self, pixel_values: torch.Tensor) -> tuple[torch.Tensor, int, int]: batch_size, num_channels, height, width = pixel_values.shape if num_channels != self.num_channels: raise ValueError( @@ -197,7 +197,7 @@ class PvtEfficientSelfAttention(nn.Module): height: int, width: int, output_attentions: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(hidden_states)) if self.sequences_reduction_ratio > 1: @@ -270,7 +270,7 @@ class PvtAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, height: int, width: int, output_attentions: bool = False - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, height, width, output_attentions) attention_output = self.output(self_outputs[0]) @@ -408,7 +408,7 @@ class PvtEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -499,7 +499,7 @@ class PvtModel(PvtPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/pvt_v2/configuration_pvt_v2.py b/src/transformers/models/pvt_v2/configuration_pvt_v2.py index 9aef0e760f3..167c5674ac2 100644 --- a/src/transformers/models/pvt_v2/configuration_pvt_v2.py +++ b/src/transformers/models/pvt_v2/configuration_pvt_v2.py @@ -16,7 +16,7 @@ # limitations under the License. """Pvt V2 model configuration""" -from typing import Callable, List, Tuple, Union +from typing import Callable, Union from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -37,25 +37,25 @@ class PvtV2Config(BackboneConfigMixin, PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - image_size (`Union[int, Tuple[int, int]]`, *optional*, defaults to 224): + image_size (`Union[int, tuple[int, int]]`, *optional*, defaults to 224): The input image size. Pass int value for square image, or tuple of (height, width). num_channels (`int`, *optional*, defaults to 3): The number of input channels. num_encoder_blocks (`[int]`, *optional*, defaults to 4): The number of encoder blocks (i.e. stages in the Mix Transformer encoder). - depths (`List[int]`, *optional*, defaults to `[2, 2, 2, 2]`): + depths (`list[int]`, *optional*, defaults to `[2, 2, 2, 2]`): The number of layers in each encoder block. - sr_ratios (`List[int]`, *optional*, defaults to `[8, 4, 2, 1]`): + sr_ratios (`list[int]`, *optional*, defaults to `[8, 4, 2, 1]`): Spatial reduction ratios in each encoder block. - hidden_sizes (`List[int]`, *optional*, defaults to `[32, 64, 160, 256]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[32, 64, 160, 256]`): Dimension of each of the encoder blocks. - patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3, 3]`): + patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3, 3]`): Patch size for overlapping patch embedding before each encoder block. - strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + strides (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Stride for overlapping patch embedding before each encoder block. - num_attention_heads (`List[int]`, *optional*, defaults to `[1, 2, 5, 8]`): + num_attention_heads (`list[int]`, *optional*, defaults to `[1, 2, 5, 8]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - mlp_ratios (`List[int]`, *optional*, defaults to `[8, 8, 4, 4]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[8, 8, 4, 4]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`): @@ -76,11 +76,11 @@ class PvtV2Config(BackboneConfigMixin, PretrainedConfig): linear_attention (`bool`, *optional*, defaults to `False`): Use linear attention complexity. If set to True, `sr_ratio` is ignored and average pooling is used for dimensionality reduction in the attention layers rather than strided convolution. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. @@ -103,16 +103,16 @@ class PvtV2Config(BackboneConfigMixin, PretrainedConfig): def __init__( self, - image_size: Union[int, Tuple[int, int]] = 224, + image_size: Union[int, tuple[int, int]] = 224, num_channels: int = 3, num_encoder_blocks: int = 4, - depths: List[int] = [2, 2, 2, 2], - sr_ratios: List[int] = [8, 4, 2, 1], - hidden_sizes: List[int] = [32, 64, 160, 256], - patch_sizes: List[int] = [7, 3, 3, 3], - strides: List[int] = [4, 2, 2, 2], - num_attention_heads: List[int] = [1, 2, 5, 8], - mlp_ratios: List[int] = [8, 8, 4, 4], + depths: list[int] = [2, 2, 2, 2], + sr_ratios: list[int] = [8, 4, 2, 1], + hidden_sizes: list[int] = [32, 64, 160, 256], + patch_sizes: list[int] = [7, 3, 3, 3], + strides: list[int] = [4, 2, 2, 2], + num_attention_heads: list[int] = [1, 2, 5, 8], + mlp_ratios: list[int] = [8, 8, 4, 4], hidden_act: Union[str, Callable] = "gelu", hidden_dropout_prob: float = 0.0, attention_probs_dropout_prob: float = 0.0, diff --git a/src/transformers/models/pvt_v2/modeling_pvt_v2.py b/src/transformers/models/pvt_v2/modeling_pvt_v2.py index 7c0fe075b69..e378ce390ab 100644 --- a/src/transformers/models/pvt_v2/modeling_pvt_v2.py +++ b/src/transformers/models/pvt_v2/modeling_pvt_v2.py @@ -17,7 +17,7 @@ """PyTorch PVTv2 model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -169,7 +169,7 @@ class PvtV2SelfAttention(nn.Module): height: int, width: int, output_attentions: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, seq_len, num_channels = hidden_states.shape query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -360,7 +360,7 @@ class PvtV2Encoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -441,7 +441,7 @@ class PvtV2Model(PvtV2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/qwen2/configuration_qwen2.py b/src/transformers/models/qwen2/configuration_qwen2.py index e4c99a4fbca..3d5b7d4edcf 100644 --- a/src/transformers/models/qwen2/configuration_qwen2.py +++ b/src/transformers/models/qwen2/configuration_qwen2.py @@ -92,11 +92,11 @@ class Qwen2Config(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/qwen2/modeling_qwen2.py b/src/transformers/models/qwen2/modeling_qwen2.py index 03df9df94f6..aaebc3c82bd 100644 --- a/src/transformers/models/qwen2/modeling_qwen2.py +++ b/src/transformers/models/qwen2/modeling_qwen2.py @@ -4,7 +4,7 @@ # the file from the modular. If any change should be done, please apply the change to the # modular_qwen2.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -142,12 +142,12 @@ class Qwen2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -226,9 +226,9 @@ class Qwen2DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/qwen2/modular_qwen2.py b/src/transformers/models/qwen2/modular_qwen2.py index 10f3c3acca3..f73aedc2c07 100644 --- a/src/transformers/models/qwen2/modular_qwen2.py +++ b/src/transformers/models/qwen2/modular_qwen2.py @@ -1,4 +1,4 @@ -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.utils.checkpoint @@ -52,12 +52,12 @@ class Qwen2Attention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) diff --git a/src/transformers/models/qwen2/tokenization_qwen2.py b/src/transformers/models/qwen2/tokenization_qwen2.py index c388789b728..1b0725ee642 100644 --- a/src/transformers/models/qwen2/tokenization_qwen2.py +++ b/src/transformers/models/qwen2/tokenization_qwen2.py @@ -18,7 +18,7 @@ import json import os import unicodedata from functools import lru_cache -from typing import Optional, Tuple +from typing import Optional import regex as re @@ -305,7 +305,7 @@ class Qwen2Tokenizer(PreTrainedTokenizer): ) # Copied from transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/qwen2/tokenization_qwen2_fast.py b/src/transformers/models/qwen2/tokenization_qwen2_fast.py index b7312755ef5..dda8123c7d6 100644 --- a/src/transformers/models/qwen2/tokenization_qwen2_fast.py +++ b/src/transformers/models/qwen2/tokenization_qwen2_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Tokenization classes for Qwen2.""" -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -129,7 +129,7 @@ class Qwen2TokenizerFast(PreTrainedTokenizerFast): ) # Copied from transformers.models.gpt2.tokenization_gpt2_fast.GPT2TokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/qwen2_5_omni/configuration_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/configuration_qwen2_5_omni.py index a0ad1b0fb79..0aa4490e60b 100644 --- a/src/transformers/models/qwen2_5_omni/configuration_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/configuration_qwen2_5_omni.py @@ -287,11 +287,11 @@ class Qwen2_5OmniTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -636,11 +636,11 @@ class Qwen2_5OmniTalkerConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -824,11 +824,11 @@ class Qwen2_5OmniDiTConfig(PretrainedConfig): The dimension of the pre-trained speaker embedding. enc_dim (`int`, *optional*, defaults to 128): The dimension of the encoder output. - enc_channels (`List[int]`, *optional*, defaults to `[256, 256, 256, 256, 768]`): + enc_channels (`list[int]`, *optional*, defaults to `[256, 256, 256, 256, 768]`): A list of output channels for each TDNN/SERes2Net layer in the encoder. - enc_kernel_sizes (`List[int]`, *optional*, defaults to `[5, 3, 3, 3, 1]`): + enc_kernel_sizes (`list[int]`, *optional*, defaults to `[5, 3, 3, 3, 1]`): A list of kernel sizes for each layer in the encoder. - enc_dilations (`List[int]`, *optional*, defaults to `[1, 2, 3, 4, 1]`): + enc_dilations (`list[int]`, *optional*, defaults to `[1, 2, 3, 4, 1]`): A list of dilations for each layer in the encoder. enc_attention_channels (`int`, *optional*, defaults to 64): The number of attention channels in the SqueezeExcitationBlock. @@ -903,13 +903,13 @@ class Qwen2_5OmniBigVGANConfig(PretrainedConfig): The dimension of the mel-spectrogram. upsample_initial_channel (`int`, *optional*, defaults to 1536): The number of channels in the initial upsampling layer. - resblock_kernel_sizes (`List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`list[int]`, *optional*, defaults to `[3, 7, 11]`): A list of kernel sizes for each residual block. - resblock_dilation_sizes (`List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A list of dilation sizes for each residual block. - upsample_rates (`List[int]`, *optional*, defaults to `[5, 3, 2, 2, 2, 2]`): + upsample_rates (`list[int]`, *optional*, defaults to `[5, 3, 2, 2, 2, 2]`): A list of upsampling rates for each upsampling layer. - upsample_kernel_sizes (`List[int]`, *optional*, defaults to `[11, 7, 4, 4, 4, 4]`): + upsample_kernel_sizes (`list[int]`, *optional*, defaults to `[11, 7, 4, 4, 4, 4]`): A list of kernel sizes for each upsampling layer. """ diff --git a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py index 896b408116a..d6f2a0e4b5f 100644 --- a/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/modeling_qwen2_5_omni.py @@ -22,7 +22,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np import torch @@ -185,9 +185,9 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo start_idx: int, vision_idx: int, spatial_merge_size: int, - t_index: List[int], - grid_hs: List[int], - grid_ws: List[int], + t_index: list[int], + grid_hs: list[int], + grid_ws: list[int], ): llm_pos_ids_list = [] llm_grid_h = grid_hs[vision_idx] // spatial_merge_size @@ -220,7 +220,7 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo remove_index (`int`) An index id to subtract from `token_indices` before chunking Returns: - `List[Tuple[int, int]]`: A list of tuples, each representing the start (inclusive) + `list[tuple[int, int]]`: A list of tuples, each representing the start (inclusive) and end (exclusive) indices of a chunk in `token_indices`. """ @@ -246,7 +246,7 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo use_audio_in_video: bool = False, audio_seqlens: Optional[torch.LongTensor] = None, second_per_grids: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Calculate the 3D rope index based on image and video's temporal, height and width in LLM. @@ -562,9 +562,9 @@ class Qwen2_5OmniThinkerCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -600,7 +600,7 @@ class Qwen2_5OmniAudioAttention(nn.Module): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" seq_length, _ = hidden_states.size() @@ -656,7 +656,7 @@ class Qwen2_5OmniAudioFlashAttention2(Qwen2_5OmniAudioAttention): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: seq_length, all_dim = hidden_states.size() query_states = self.q_proj(hidden_states) query_states = query_states.reshape(seq_length, self.num_heads, -1) @@ -681,7 +681,7 @@ class Qwen2_5OmniAudioSdpaAttention(Qwen2_5OmniAudioAttention): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" seq_length, _ = hidden_states.size() @@ -851,7 +851,7 @@ class Qwen2_5OmniAudioEncoder(Qwen2_5OmniPreTrainedModel): r""" input_features (`torch.LongTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of mel features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -1504,9 +1504,9 @@ class Qwen2_5OmniAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -1583,13 +1583,13 @@ class Qwen2_5OmniDecoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1604,7 +1604,7 @@ class Qwen2_5OmniDecoderLayer(GradientCheckpointingLayer): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -1682,7 +1682,7 @@ class Qwen2_5OmniThinkerTextModel(Qwen2_5OmniPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1690,7 +1690,7 @@ class Qwen2_5OmniThinkerTextModel(Qwen2_5OmniPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1929,7 +1929,7 @@ class Qwen2_5OmniThinkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCo feature_attention_mask: Optional[torch.Tensor] = None, audio_feature_lengths: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, rope_deltas: Optional[torch.LongTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1940,11 +1940,11 @@ class Qwen2_5OmniThinkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCo use_audio_in_video: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, video_second_per_grid: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Qwen2_5OmniThinkerCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5OmniThinkerCausalLMOutputWithPast]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, feature_sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -2206,9 +2206,9 @@ class Qwen2_5OmniTalkerCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None thinker_reply_part: torch.FloatTensor = None @@ -2247,7 +2247,7 @@ class Qwen2_5OmniTalkerModel(Qwen2_5OmniPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -2255,7 +2255,7 @@ class Qwen2_5OmniTalkerModel(Qwen2_5OmniPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -2415,7 +2415,7 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, thinker_reply_part: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, rope_deltas: Optional[torch.LongTensor] = None, @@ -2430,7 +2430,7 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Qwen2_5OmniTalkerCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5OmniTalkerCausalLMOutputWithPast]: r""" rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): The rope index difference between sequence length and multimodal rope. @@ -2607,10 +2607,10 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon def _update_model_kwargs_for_generation( self, outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], is_encoder_decoder: bool = False, num_new_tokens: int = 1, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: model_kwargs = super()._update_model_kwargs_for_generation( outputs, model_kwargs, is_encoder_decoder, num_new_tokens ) diff --git a/src/transformers/models/qwen2_5_omni/modular_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/modular_qwen2_5_omni.py index 5af995b1374..58b3b87ad38 100644 --- a/src/transformers/models/qwen2_5_omni/modular_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/modular_qwen2_5_omni.py @@ -17,7 +17,7 @@ import math from dataclasses import dataclass -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import torch @@ -326,11 +326,11 @@ class Qwen2_5OmniTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -675,11 +675,11 @@ class Qwen2_5OmniTalkerConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -863,11 +863,11 @@ class Qwen2_5OmniDiTConfig(PretrainedConfig): The dimension of the pre-trained speaker embedding. enc_dim (`int`, *optional*, defaults to 128): The dimension of the encoder output. - enc_channels (`List[int]`, *optional*, defaults to `[256, 256, 256, 256, 768]`): + enc_channels (`list[int]`, *optional*, defaults to `[256, 256, 256, 256, 768]`): A list of output channels for each TDNN/SERes2Net layer in the encoder. - enc_kernel_sizes (`List[int]`, *optional*, defaults to `[5, 3, 3, 3, 1]`): + enc_kernel_sizes (`list[int]`, *optional*, defaults to `[5, 3, 3, 3, 1]`): A list of kernel sizes for each layer in the encoder. - enc_dilations (`List[int]`, *optional*, defaults to `[1, 2, 3, 4, 1]`): + enc_dilations (`list[int]`, *optional*, defaults to `[1, 2, 3, 4, 1]`): A list of dilations for each layer in the encoder. enc_attention_channels (`int`, *optional*, defaults to 64): The number of attention channels in the SqueezeExcitationBlock. @@ -942,13 +942,13 @@ class Qwen2_5OmniBigVGANConfig(PretrainedConfig): The dimension of the mel-spectrogram. upsample_initial_channel (`int`, *optional*, defaults to 1536): The number of channels in the initial upsampling layer. - resblock_kernel_sizes (`List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`list[int]`, *optional*, defaults to `[3, 7, 11]`): A list of kernel sizes for each residual block. - resblock_dilation_sizes (`List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A list of dilation sizes for each residual block. - upsample_rates (`List[int]`, *optional*, defaults to `[5, 3, 2, 2, 2, 2]`): + upsample_rates (`list[int]`, *optional*, defaults to `[5, 3, 2, 2, 2, 2]`): A list of upsampling rates for each upsampling layer. - upsample_kernel_sizes (`List[int]`, *optional*, defaults to `[11, 7, 4, 4, 4, 4]`): + upsample_kernel_sizes (`list[int]`, *optional*, defaults to `[11, 7, 4, 4, 4, 4]`): A list of kernel sizes for each upsampling layer. """ @@ -1215,9 +1215,9 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo start_idx: int, vision_idx: int, spatial_merge_size: int, - t_index: List[int], - grid_hs: List[int], - grid_ws: List[int], + t_index: list[int], + grid_hs: list[int], + grid_ws: list[int], ): llm_pos_ids_list = [] llm_grid_h = grid_hs[vision_idx] // spatial_merge_size @@ -1250,7 +1250,7 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo remove_index (`int`) An index id to subtract from `token_indices` before chunking Returns: - `List[Tuple[int, int]]`: A list of tuples, each representing the start (inclusive) + `list[tuple[int, int]]`: A list of tuples, each representing the start (inclusive) and end (exclusive) indices of a chunk in `token_indices`. """ @@ -1276,7 +1276,7 @@ class Qwen2_5OmniPreTrainedModelForConditionalGeneration(Qwen2_5OmniPreTrainedMo use_audio_in_video: bool = False, audio_seqlens: Optional[torch.LongTensor] = None, second_per_grids: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Calculate the 3D rope index based on image and video's temporal, height and width in LLM. @@ -1592,9 +1592,9 @@ class Qwen2_5OmniThinkerCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -1630,7 +1630,7 @@ class Qwen2_5OmniAudioAttention(nn.Module): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" seq_length, _ = hidden_states.size() @@ -1686,7 +1686,7 @@ class Qwen2_5OmniAudioFlashAttention2(Qwen2_5OmniAudioAttention): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: seq_length, all_dim = hidden_states.size() query_states = self.q_proj(hidden_states) query_states = query_states.reshape(seq_length, self.num_heads, -1) @@ -1711,7 +1711,7 @@ class Qwen2_5OmniAudioSdpaAttention(Qwen2_5OmniAudioAttention): self, hidden_states: torch.Tensor, cu_seqlens: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" seq_length, _ = hidden_states.size() @@ -1862,7 +1862,7 @@ class Qwen2_5OmniAudioEncoder(Qwen2_5OmniPreTrainedModel): r""" input_features (`torch.LongTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of mel features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -2351,7 +2351,7 @@ class Qwen2_5OmniThinkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCo feature_attention_mask: Optional[torch.Tensor] = None, audio_feature_lengths: Optional[torch.LongTensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, rope_deltas: Optional[torch.LongTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2362,11 +2362,11 @@ class Qwen2_5OmniThinkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCo use_audio_in_video: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, video_second_per_grid: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Qwen2_5OmniThinkerCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5OmniThinkerCausalLMOutputWithPast]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, feature_sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -2628,9 +2628,9 @@ class Qwen2_5OmniTalkerCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None thinker_reply_part: torch.FloatTensor = None @@ -2683,7 +2683,7 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, thinker_reply_part: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, rope_deltas: Optional[torch.LongTensor] = None, @@ -2698,7 +2698,7 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Qwen2_5OmniTalkerCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5OmniTalkerCausalLMOutputWithPast]: r""" rope_deltas (`torch.LongTensor` of shape `(batch_size, )`, *optional*): The rope index difference between sequence length and multimodal rope. @@ -2875,10 +2875,10 @@ class Qwen2_5OmniTalkerForConditionalGeneration(Qwen2_5OmniPreTrainedModelForCon def _update_model_kwargs_for_generation( self, outputs: ModelOutput, - model_kwargs: Dict[str, Any], + model_kwargs: dict[str, Any], is_encoder_decoder: bool = False, num_new_tokens: int = 1, - ) -> Dict[str, Any]: + ) -> dict[str, Any]: model_kwargs = super()._update_model_kwargs_for_generation( outputs, model_kwargs, is_encoder_decoder, num_new_tokens ) diff --git a/src/transformers/models/qwen2_5_omni/processing_qwen2_5_omni.py b/src/transformers/models/qwen2_5_omni/processing_qwen2_5_omni.py index e211b8d911f..8218ab5a3f0 100644 --- a/src/transformers/models/qwen2_5_omni/processing_qwen2_5_omni.py +++ b/src/transformers/models/qwen2_5_omni/processing_qwen2_5_omni.py @@ -19,7 +19,7 @@ Processor class for Qwen2.5Omni. import logging import re -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -31,7 +31,7 @@ from ...video_utils import VideoInput, make_batched_videos class Qwen2_5_OmniVideosKwargs(VideosKwargs): - fps: Optional[List[int]] = None + fps: Optional[list[int]] = None use_audio_in_video: Optional[bool] = None seconds_per_chunk: Optional[float] = None position_id_per_seconds: Optional[int] = None @@ -112,7 +112,7 @@ class Qwen2_5OmniProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, images: ImageInput = None, videos: VideoInput = None, audio: AudioInput = None, @@ -128,17 +128,17 @@ class Qwen2_5OmniProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch tensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported. - audio (`np.ndarray`, `List[np.ndarray]`): + audio (`np.ndarray`, `list[np.ndarray]`): The audio or batch of audio to be prepared. Each audio can be a NumPy array. """ @@ -300,7 +300,7 @@ class Qwen2_5OmniProcessor(ProcessorMixin): t_ntoken_per_chunk (`int`): Number of tokens per chunk (used as the chunk size threshold). Returns: - `List[Tuple[int, int]]`: A list of tuples, each representing the start (inclusive) + `list[tuple[int, int]]`: A list of tuples, each representing the start (inclusive) and end (exclusive) indices of a chunk in `token_indices`. """ diff --git a/src/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py index 37ad9238aa4..9312fdf2b44 100644 --- a/src/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/configuration_qwen2_5_vl.py @@ -147,11 +147,11 @@ class Qwen2_5_VLTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py index 53c8695ed0c..0001236c343 100644 --- a/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/modeling_qwen2_5_vl.py @@ -26,7 +26,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn as nn @@ -143,7 +143,7 @@ class Qwen2_5_VLPatchMerger(nn.Module): def apply_rotary_pos_emb_flashatt( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: cos = cos.chunk(2, dim=-1)[0].contiguous() sin = sin.chunk(2, dim=-1)[0].contiguous() q_embed = apply_rotary_emb(q.float(), cos.float(), sin.float()).type_as(q) @@ -163,7 +163,7 @@ class Qwen2_5_VLVisionFlashAttention2(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -200,7 +200,7 @@ def rotate_half(x): def apply_rotary_pos_emb_vision( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: orig_q_dtype = q.dtype orig_k_dtype = k.dtype q, k = q.float(), k.float() @@ -225,7 +225,7 @@ class Qwen2_5_VLVisionAttention(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -274,7 +274,7 @@ class Qwen2_5_VLVisionSdpaAttention(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -329,7 +329,7 @@ class Qwen2_5_VLVisionBlock(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: hidden_states = hidden_states + self.attn( self.norm1(hidden_states), @@ -560,9 +560,9 @@ class Qwen2_5_VLModelOutputWithPast(ModelOutput): """ last_hidden_state: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -750,9 +750,9 @@ class Qwen2_5_VLAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -815,13 +815,13 @@ class Qwen2_5_VLDecoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -836,7 +836,7 @@ class Qwen2_5_VLDecoderLayer(GradientCheckpointingLayer): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -913,7 +913,7 @@ class Qwen2_5_VLTextModel(Qwen2_5_VLPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -921,7 +921,7 @@ class Qwen2_5_VLTextModel(Qwen2_5_VLPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1074,7 +1074,7 @@ class Qwen2_5_VLModel(Qwen2_5_VLPreTrainedModel): video_grid_thw: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Calculate the 3D rope index based on image and video's temporal, height and width in LLM. @@ -1289,7 +1289,7 @@ class Qwen2_5_VLModel(Qwen2_5_VLPreTrainedModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1303,7 +1303,7 @@ class Qwen2_5_VLModel(Qwen2_5_VLPreTrainedModel): cache_position: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2_5_VLModelOutputWithPast]: + ) -> Union[tuple, Qwen2_5_VLModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(seq_length, num_channels * temporal_size * image_size * image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -1465,9 +1465,9 @@ class Qwen2_5_VLCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -1527,7 +1527,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2_5_VLPreTrainedModel, GenerationMi input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1541,7 +1541,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2_5_VLPreTrainedModel, GenerationMi cache_position: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2_5_VLCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5_VLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1677,7 +1677,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2_5_VLPreTrainedModel, GenerationMi def _get_image_nums_and_video_nums( self, input_ids: Optional[torch.LongTensor], - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Get the number of images and videos for each sample to calculate the separation length of the sample tensor. These parameters are not passed through the processor to avoid unpredictable impacts from interface modifications. @@ -1709,7 +1709,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2_5_VLPreTrainedModel, GenerationMi is_encoder_decoder: bool = False, input_ids: Optional[torch.LongTensor] = None, **model_kwargs, - ) -> Tuple[torch.LongTensor, Dict[str, Any]]: + ) -> tuple[torch.LongTensor, dict[str, Any]]: # Overwritten -- Support for expanding tensors without a batch size dimension # e.g., pixel_values, image_grid_thw, pixel_values_videos, video_grid_thw, second_per_grid_t # pixel_values.shape[0] is sum(seqlen_images for samples) diff --git a/src/transformers/models/qwen2_5_vl/modular_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/modular_qwen2_5_vl.py index 7f161ec43a0..71764b24563 100644 --- a/src/transformers/models/qwen2_5_vl/modular_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/modular_qwen2_5_vl.py @@ -20,7 +20,7 @@ """PyTorch Qwen2.5-VL model.""" from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -65,7 +65,7 @@ logger = logging.get_logger(__name__) def apply_rotary_pos_emb_flashatt( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: cos = cos.chunk(2, dim=-1)[0].contiguous() sin = sin.chunk(2, dim=-1)[0].contiguous() q_embed = apply_rotary_emb(q.float(), cos.float(), sin.float()).type_as(q) @@ -162,7 +162,7 @@ class Qwen2_5_VLVisionFlashAttention2(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -220,7 +220,7 @@ class Qwen2_5_VLVisionBlock(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: hidden_states = hidden_states + self.attn( self.norm1(hidden_states), @@ -430,7 +430,7 @@ class Qwen2_5_VLModel(Qwen2VLModel): video_grid_thw: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, attention_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Calculate the 3D rope index based on image and video's temporal, height and width in LLM. @@ -610,7 +610,7 @@ class Qwen2_5_VLModel(Qwen2VLModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -624,7 +624,7 @@ class Qwen2_5_VLModel(Qwen2VLModel): cache_position: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2_5_VLModelOutputWithPast]: + ) -> Union[tuple, Qwen2_5_VLModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(seq_length, num_channels * temporal_size * image_size * image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -764,7 +764,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2VLForConditionalGeneration): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -778,7 +778,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2VLForConditionalGeneration): cache_position: Optional[torch.LongTensor] = None, second_per_grid_ts: Optional[torch.Tensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2_5_VLCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2_5_VLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -913,7 +913,7 @@ class Qwen2_5_VLForConditionalGeneration(Qwen2VLForConditionalGeneration): class Qwen2_5_VLVideosProcessorKwargs(VideosKwargs, total=False): - fps: Union[List[float], float] + fps: Union[list[float], float] class Qwen2_5_VLImagesKwargs(Qwen2VLImagesKwargs): @@ -959,7 +959,7 @@ class Qwen2_5_VLProcessor(Qwen2VLProcessor): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, videos: VideoInput = None, **kwargs: Unpack[Qwen2_5_VLProcessorKwargs], ) -> BatchFeature: @@ -970,14 +970,14 @@ class Qwen2_5_VLProcessor(Qwen2VLProcessor): Qwen2VLImageProcessor's [`~Qwen2VLImageProcessor.__call__`] if `vision_infos` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch tensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported. return_tensors (`str` or [`~utils.TensorType`], *optional*): @@ -1067,9 +1067,9 @@ class Qwen2_5_VLProcessor(Qwen2VLProcessor): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. - video_sizes (`List[List[int]]`, *optional*): + video_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (num_frames, height, width) per each video. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided diff --git a/src/transformers/models/qwen2_5_vl/processing_qwen2_5_vl.py b/src/transformers/models/qwen2_5_vl/processing_qwen2_5_vl.py index e145791eea9..c3a0ef93733 100644 --- a/src/transformers/models/qwen2_5_vl/processing_qwen2_5_vl.py +++ b/src/transformers/models/qwen2_5_vl/processing_qwen2_5_vl.py @@ -23,7 +23,7 @@ # 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. -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -35,7 +35,7 @@ from ...video_utils import VideoInput class Qwen2_5_VLVideosProcessorKwargs(VideosKwargs, total=False): - fps: Union[List[float], float] + fps: Union[list[float], float] class Qwen2_5_VLImagesKwargs(ImagesKwargs): @@ -97,7 +97,7 @@ class Qwen2_5_VLProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, videos: VideoInput = None, **kwargs: Unpack[Qwen2_5_VLProcessorKwargs], ) -> BatchFeature: @@ -108,14 +108,14 @@ class Qwen2_5_VLProcessor(ProcessorMixin): Qwen2VLImageProcessor's [`~Qwen2VLImageProcessor.__call__`] if `vision_infos` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch tensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported. return_tensors (`str` or [`~utils.TensorType`], *optional*): @@ -205,9 +205,9 @@ class Qwen2_5_VLProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. - video_sizes (`List[List[int]]`, *optional*): + video_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (num_frames, height, width) per each video. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided @@ -271,7 +271,7 @@ class Qwen2_5_VLProcessor(ProcessorMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text. + `list[str]`: The decoded text. """ return self.tokenizer.batch_decode( generated_outputs, diff --git a/src/transformers/models/qwen2_audio/modeling_qwen2_audio.py b/src/transformers/models/qwen2_audio/modeling_qwen2_audio.py index 99be5b7f5ca..6569d78674e 100644 --- a/src/transformers/models/qwen2_audio/modeling_qwen2_audio.py +++ b/src/transformers/models/qwen2_audio/modeling_qwen2_audio.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -72,8 +72,8 @@ class Qwen2AudioCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None past_key_values: Optional[Cache] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None attention_mask: Optional[torch.FloatTensor] = None @@ -162,7 +162,7 @@ class Qwen2AudioAttention(nn.Module): layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" bsz, tgt_len, _ = hidden_states.size() @@ -367,7 +367,7 @@ class Qwen2AudioEncoder(Qwen2AudioPreTrainedModel): Args: input_features (`torch.LongTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of mel features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -752,11 +752,11 @@ class Qwen2AudioForConditionalGeneration(Qwen2AudioPreTrainedModel, GenerationMi output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Qwen2AudioCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2AudioCausalLMOutputWithPast]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, feature_sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] diff --git a/src/transformers/models/qwen2_audio/processing_qwen2_audio.py b/src/transformers/models/qwen2_audio/processing_qwen2_audio.py index ac9e8aff77a..cbe7482c74b 100644 --- a/src/transformers/models/qwen2_audio/processing_qwen2_audio.py +++ b/src/transformers/models/qwen2_audio/processing_qwen2_audio.py @@ -17,7 +17,7 @@ Processor class for Qwen2Audio. """ import warnings -from typing import List, Union +from typing import Union import numpy as np @@ -83,8 +83,8 @@ class Qwen2AudioProcessor(ProcessorMixin): @deprecate_kwarg("audios", version="4.54.0", new_name="audio") def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - audio: Union[np.ndarray, List[np.ndarray]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + audio: Union[np.ndarray, list[np.ndarray]] = None, audios=None, # kept for BC **kwargs: Unpack[Qwen2AudioProcessorKwargs], ) -> BatchFeature: @@ -96,11 +96,11 @@ class Qwen2AudioProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - audio (`np.ndarray`, `List[np.ndarray]`): + audio (`np.ndarray`, `list[np.ndarray]`): The audio or batch of audios to be prepared. Each audio can be a NumPy array. """ diff --git a/src/transformers/models/qwen2_moe/configuration_qwen2_moe.py b/src/transformers/models/qwen2_moe/configuration_qwen2_moe.py index b03f692dd9c..19881fd1df6 100644 --- a/src/transformers/models/qwen2_moe/configuration_qwen2_moe.py +++ b/src/transformers/models/qwen2_moe/configuration_qwen2_moe.py @@ -91,11 +91,11 @@ class Qwen2MoeConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -129,7 +129,7 @@ class Qwen2MoeConfig(PretrainedConfig): allow the model to output the auxiliary loss, including load balancing loss and router z-loss. router_aux_loss_coef (`float`, *optional*, defaults to 0.001): The aux loss factor for the total loss. - mlp_only_layers (`List[int]`, *optional*, defaults to `[]`): + mlp_only_layers (`list[int]`, *optional*, defaults to `[]`): Indicate which layers use Qwen2MoeMLP rather than Qwen2MoeSparseMoeBlock The list contains layer index, from 0 to num_layers-1 if we have num_layers layers If `mlp_only_layers` is empty, `decoder_sparse_step` is used to determine the sparsity. diff --git a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py index 86091759f31..9882ca447d7 100644 --- a/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py +++ b/src/transformers/models/qwen2_moe/modeling_qwen2_moe.py @@ -20,7 +20,7 @@ """PyTorch Qwen2MoE model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -58,7 +58,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -311,8 +311,8 @@ class Qwen2MoeAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -390,7 +390,7 @@ class Qwen2MoeFlashAttention2(Qwen2MoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC ): bsz, q_len, _ = hidden_states.size() @@ -492,8 +492,8 @@ class Qwen2MoeSdpaAttention(Qwen2MoeAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -656,14 +656,14 @@ class Qwen2MoeDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -681,7 +681,7 @@ class Qwen2MoeDecoderLayer(nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -789,7 +789,7 @@ class Qwen2MoeModel(Qwen2MoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1115,7 +1115,7 @@ class Qwen2MoeForCausalLM(Qwen2MoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1401,7 +1401,7 @@ class Qwen2MoeForQuestionAnswering(Qwen2MoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, diff --git a/src/transformers/models/qwen2_vl/configuration_qwen2_vl.py b/src/transformers/models/qwen2_vl/configuration_qwen2_vl.py index e3f8ea7e2f8..d60c2eb62e3 100644 --- a/src/transformers/models/qwen2_vl/configuration_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/configuration_qwen2_vl.py @@ -136,11 +136,11 @@ class Qwen2VLTextConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py index a4826428aca..95b303346c0 100644 --- a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl.py @@ -20,7 +20,7 @@ """Image processor class for Qwen2-VL.""" import math -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -88,7 +88,7 @@ class Qwen2VLImageProcessor(BaseImageProcessor): Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions. - size (`Dict[str, int]`, *optional*, defaults to `{"shortest_edge": 56 * 56, "longest_edge": 28 * 28 * 1280}`): + size (`dict[str, int]`, *optional*, defaults to `{"shortest_edge": 56 * 56, "longest_edge": 28 * 28 * 1280}`): Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): Resampling filter to use when resizing the image. @@ -98,9 +98,9 @@ class Qwen2VLImageProcessor(BaseImageProcessor): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats for each channel in the image. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats for each channel in the image. do_convert_rgb (`bool`, *optional*, defaults to `True`): Whether to convert the image to RGB. @@ -121,13 +121,13 @@ class Qwen2VLImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, min_pixels: Optional[int] = None, max_pixels: Optional[int] = None, @@ -167,13 +167,13 @@ class Qwen2VLImageProcessor(BaseImageProcessor): self, images: Union[ImageInput, VideoInput], do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, patch_size: Optional[int] = None, temporal_patch_size: Optional[int] = None, merge_size: Optional[int] = None, @@ -187,11 +187,11 @@ class Qwen2VLImageProcessor(BaseImageProcessor): Args: images (`ImageInput`): Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`. - vision_info (`List[Dict]`, *optional*): + vision_info (`list[Dict]`, *optional*): Optional list of dictionaries containing additional information about vision inputs. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the `PILImageResampling` enums. @@ -201,9 +201,9 @@ class Qwen2VLImageProcessor(BaseImageProcessor): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. patch_size (`int`, *optional*, defaults to `self.patch_size`): The spatial patch size of the vision encoder. @@ -302,15 +302,15 @@ class Qwen2VLImageProcessor(BaseImageProcessor): images: ImageInput, videos: VideoInput = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, min_pixels: Optional[int] = None, max_pixels: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, patch_size: Optional[int] = None, temporal_patch_size: Optional[int] = None, merge_size: Optional[int] = None, @@ -329,7 +329,7 @@ class Qwen2VLImageProcessor(BaseImageProcessor): passing in videos with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -341,9 +341,9 @@ class Qwen2VLImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. min_pixels (`int`, *optional*, defaults to `self.min_pixels`): diff --git a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl_fast.py b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl_fast.py index 7c27694c759..84f427ad606 100644 --- a/src/transformers/models/qwen2_vl/image_processing_qwen2_vl_fast.py +++ b/src/transformers/models/qwen2_vl/image_processing_qwen2_vl_fast.py @@ -19,7 +19,7 @@ # limitations under the License. """Fast Image processor class for Qwen2-VL.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -124,15 +124,15 @@ class Qwen2VLImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, size: SizeDict, interpolation: Optional["F.InterpolationMode"], do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], patch_size: int, temporal_patch_size: int, merge_size: int, @@ -146,11 +146,11 @@ class Qwen2VLImageProcessorFast(BaseImageProcessorFast): Args: images (`ImageInput`): Image or batch of images to preprocess. Expects pixel values ranging from 0 to 255. If pixel values range from 0 to 1, set `do_rescale=False`. - vision_info (`List[Dict]`, *optional*): + vision_info (`list[Dict]`, *optional*): Optional list of dictionaries containing additional information about vision inputs. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. `shortest_edge` and `longest_edge` keys must be present. interpolation (`InterpolationMode`): Resampling filter to use if resizing the image. @@ -160,9 +160,9 @@ class Qwen2VLImageProcessorFast(BaseImageProcessorFast): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Mean to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Standard deviation to use if normalizing the image. Can be a float or a list of floats corresponding to the number of channels in the image. patch_size (`int`, *optional*, defaults to `self.patch_size`): The spatial patch size of the vision encoder. @@ -253,13 +253,13 @@ class Qwen2VLImageProcessorFast(BaseImageProcessorFast): images: ImageInput, videos: VideoInput = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: Optional[Union["PILImageResampling", "F.InterpolationMode"]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, min_pixels: Optional[int] = None, max_pixels: Optional[int] = None, patch_size: Optional[int] = None, diff --git a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py index 67981d78cb9..95687334eb0 100644 --- a/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/modeling_qwen2_vl.py @@ -21,7 +21,7 @@ import math from dataclasses import dataclass -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.nn as nn @@ -86,9 +86,9 @@ class Qwen2VLModelOutputWithPast(ModelOutput): """ last_hidden_state: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -125,9 +125,9 @@ class Qwen2VLCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None rope_deltas: Optional[torch.LongTensor] = None @@ -222,7 +222,7 @@ def apply_multimodal_rotary_pos_emb(q, k, cos, sin, mrope_section, unsqueeze_dim def apply_rotary_pos_emb_vision( q: torch.Tensor, k: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: orig_q_dtype = q.dtype orig_k_dtype = k.dtype q, k = q.float(), k.float() @@ -312,7 +312,7 @@ class VisionAttention(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -361,7 +361,7 @@ class VisionFlashAttention2(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -399,7 +399,7 @@ class VisionSdpaAttention(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: seq_length = hidden_states.shape[0] q, k, v = self.qkv(hidden_states).reshape(seq_length, 3, self.num_heads, -1).permute(1, 0, 2, 3).unbind(0) @@ -456,7 +456,7 @@ class Qwen2VLVisionBlock(nn.Module): hidden_states: torch.Tensor, cu_seqlens: torch.Tensor, rotary_pos_emb: Optional[torch.Tensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, ) -> torch.Tensor: hidden_states = hidden_states + self.attn( self.norm1(hidden_states), @@ -594,9 +594,9 @@ class Qwen2VLAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -659,13 +659,13 @@ class Qwen2VLDecoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -680,7 +680,7 @@ class Qwen2VLDecoderLayer(GradientCheckpointingLayer): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -881,7 +881,7 @@ class Qwen2VLTextModel(Qwen2VLPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -889,7 +889,7 @@ class Qwen2VLTextModel(Qwen2VLPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1039,7 +1039,7 @@ class Qwen2VLModel(Qwen2VLPreTrainedModel): image_grid_thw: Optional[torch.LongTensor] = None, video_grid_thw: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Calculate the 3D rope index based on image and video's temporal, height and width in LLM. @@ -1222,7 +1222,7 @@ class Qwen2VLModel(Qwen2VLPreTrainedModel): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1235,7 +1235,7 @@ class Qwen2VLModel(Qwen2VLPreTrainedModel): rope_deltas: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2VLModelOutputWithPast]: + ) -> Union[tuple, Qwen2VLModelOutputWithPast]: r""" pixel_values_videos (`torch.FloatTensor` of shape `(seq_length, num_channels * temporal_size * image_size * image_size)): The tensors corresponding to the input videos. Pixel values can be obtained using @@ -1411,7 +1411,7 @@ class Qwen2VLForConditionalGeneration(Qwen2VLPreTrainedModel, GenerationMixin): input_ids: torch.LongTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1424,7 +1424,7 @@ class Qwen2VLForConditionalGeneration(Qwen2VLPreTrainedModel, GenerationMixin): rope_deltas: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, Qwen2VLCausalLMOutputWithPast]: + ) -> Union[tuple, Qwen2VLCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1555,7 +1555,7 @@ class Qwen2VLForConditionalGeneration(Qwen2VLPreTrainedModel, GenerationMixin): def _get_image_nums_and_video_nums( self, input_ids: Optional[torch.LongTensor], - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Get the number of images and videos for each sample to calculate the separation length of the sample tensor. These parameters are not passed through the processor to avoid unpredictable impacts from interface modifications. @@ -1587,7 +1587,7 @@ class Qwen2VLForConditionalGeneration(Qwen2VLPreTrainedModel, GenerationMixin): is_encoder_decoder: bool = False, input_ids: Optional[torch.LongTensor] = None, **model_kwargs, - ) -> Tuple[torch.LongTensor, Dict[str, Any]]: + ) -> tuple[torch.LongTensor, dict[str, Any]]: # Overwritten -- Support for expanding tensors without a batch size dimension # e.g., pixel_values, image_grid_thw, pixel_values_videos, video_grid_thw, second_per_grid_t # pixel_values.shape[0] is sum(seqlen_images for samples) diff --git a/src/transformers/models/qwen2_vl/processing_qwen2_vl.py b/src/transformers/models/qwen2_vl/processing_qwen2_vl.py index 6cd056aa1d5..0716b6b53aa 100644 --- a/src/transformers/models/qwen2_vl/processing_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/processing_qwen2_vl.py @@ -21,7 +21,7 @@ Processor class for Qwen2-VL. """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -93,7 +93,7 @@ class Qwen2VLProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, videos: VideoInput = None, **kwargs: Unpack[Qwen2VLProcessorKwargs], ) -> BatchFeature: @@ -104,14 +104,14 @@ class Qwen2VLProcessor(ProcessorMixin): Qwen2VLImageProcessor's [`~Qwen2VLImageProcessor.__call__`] if `vision_infos` is not `None`. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of videos to be prepared. Each video can be a 4D NumPy array or PyTorch tensor, or a nested list of 3D frames. Both channels-first and channels-last formats are supported. return_tensors (`str` or [`~utils.TensorType`], *optional*): @@ -190,9 +190,9 @@ class Qwen2VLProcessor(ProcessorMixin): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: - image_sizes (`List[List[int]]`, *optional*): + image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. - video_sizes (`List[List[int]]`, *optional*): + video_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (num_frames, height, width) per each video. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided @@ -256,7 +256,7 @@ class Qwen2VLProcessor(ProcessorMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text. + `list[str]`: The decoded text. """ return self.tokenizer.batch_decode( generated_outputs, diff --git a/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py b/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py index 49a4e9d2efc..9782964ea13 100644 --- a/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py +++ b/src/transformers/models/qwen2_vl/video_processing_qwen2_vl.py @@ -20,7 +20,7 @@ """video processor class for Qwen2-VL.""" import math -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import ( BatchFeature, @@ -191,8 +191,8 @@ class Qwen2VLVideoProcessor(BaseVideoProcessor): def _preprocess( self, - videos: List["torch.Tensor"], - video_metadata: Union[List[VideoMetadata], List[dict]], + videos: list["torch.Tensor"], + video_metadata: Union[list[VideoMetadata], list[dict]], do_convert_rgb: bool, do_resize: bool, size: SizeDict, @@ -201,8 +201,8 @@ class Qwen2VLVideoProcessor(BaseVideoProcessor): rescale_factor: float, do_normalize: bool, do_sample_frames: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], min_pixels: Optional[int] = None, max_pixels: Optional[int] = None, patch_size: Optional[int] = None, diff --git a/src/transformers/models/qwen3/configuration_qwen3.py b/src/transformers/models/qwen3/configuration_qwen3.py index 77fbdb4e099..2a52938cc11 100644 --- a/src/transformers/models/qwen3/configuration_qwen3.py +++ b/src/transformers/models/qwen3/configuration_qwen3.py @@ -94,11 +94,11 @@ class Qwen3Config(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/qwen3/modeling_qwen3.py b/src/transformers/models/qwen3/modeling_qwen3.py index ef1cd22e0c0..6da04485704 100644 --- a/src/transformers/models/qwen3/modeling_qwen3.py +++ b/src/transformers/models/qwen3/modeling_qwen3.py @@ -19,7 +19,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -189,12 +189,12 @@ class Qwen3Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -252,9 +252,9 @@ class Qwen3DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) diff --git a/src/transformers/models/qwen3/modular_qwen3.py b/src/transformers/models/qwen3/modular_qwen3.py index 466eb3d0298..2730da20c42 100644 --- a/src/transformers/models/qwen3/modular_qwen3.py +++ b/src/transformers/models/qwen3/modular_qwen3.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Qwen3 model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import torch import torch.utils.checkpoint @@ -66,12 +66,12 @@ class Qwen3Attention(LlamaAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) diff --git a/src/transformers/models/qwen3_moe/configuration_qwen3_moe.py b/src/transformers/models/qwen3_moe/configuration_qwen3_moe.py index 908e7ed9f93..a23d19c1154 100644 --- a/src/transformers/models/qwen3_moe/configuration_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/configuration_qwen3_moe.py @@ -92,11 +92,11 @@ class Qwen3MoeConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 @@ -127,7 +127,7 @@ class Qwen3MoeConfig(PretrainedConfig): allow the model to output the auxiliary loss, including load balancing loss and router z-loss. router_aux_loss_coef (`float`, *optional*, defaults to 0.001): The aux loss factor for the total loss. - mlp_only_layers (`List[int]`, *optional*, defaults to `[]`): + mlp_only_layers (`list[int]`, *optional*, defaults to `[]`): Indicate which layers use Qwen3MoeMLP rather than Qwen3MoeSparseMoeBlock The list contains layer index, from 0 to num_layers-1 if we have num_layers layers If `mlp_only_layers` is empty, `decoder_sparse_step` is used to determine the sparsity. diff --git a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py index 98b714c7262..67f21d1b836 100644 --- a/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/modeling_qwen3_moe.py @@ -20,7 +20,7 @@ # limitations under the License. from functools import partial -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.nn.functional as F @@ -154,12 +154,12 @@ class Qwen3MoeAttention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -308,14 +308,14 @@ class Qwen3MoeDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -333,7 +333,7 @@ class Qwen3MoeDecoderLayer(nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -476,7 +476,7 @@ class Qwen3MoeModel(Qwen3MoePreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -595,7 +595,7 @@ class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ... def load_balancing_loss_func( - gate_logits: Union[torch.Tensor, Tuple[torch.Tensor], None], + gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None], num_experts: Optional[int] = None, top_k=2, attention_mask: Optional[torch.Tensor] = None, @@ -719,7 +719,7 @@ class Qwen3MoeForCausalLM(Qwen3MoePreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/qwen3_moe/modular_qwen3_moe.py b/src/transformers/models/qwen3_moe/modular_qwen3_moe.py index 6f821d5b814..569b92cc6e8 100644 --- a/src/transformers/models/qwen3_moe/modular_qwen3_moe.py +++ b/src/transformers/models/qwen3_moe/modular_qwen3_moe.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch Qwen3 model.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -142,14 +142,14 @@ class Qwen3MoeDecoderLayer(Qwen2MoeDecoderLayer, nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, output_router_logits: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -167,7 +167,7 @@ class Qwen3MoeDecoderLayer(Qwen2MoeDecoderLayer, nn.Module): past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence. - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. kwargs (`dict`, *optional*): @@ -238,7 +238,7 @@ class Qwen3MoeForCausalLM(MixtralForCausalLM): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/rag/modeling_rag.py b/src/transformers/models/rag/modeling_rag.py index b246b50b128..5729e49d310 100644 --- a/src/transformers/models/rag/modeling_rag.py +++ b/src/transformers/models/rag/modeling_rag.py @@ -16,7 +16,7 @@ import copy from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -47,7 +47,7 @@ class RetrievAugLMMarginOutput(ModelOutput): doc_scores (`torch.FloatTensor` of shape `(batch_size, config.n_docs)`): Score between each retrieved document embeddings (see `retrieved_doc_embeds`) and `question_encoder_last_hidden_state`. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -112,20 +112,20 @@ class RetrievAugLMMarginOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None doc_scores: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None retrieved_doc_embeds: Optional[torch.FloatTensor] = None retrieved_doc_ids: Optional[torch.LongTensor] = None context_input_ids: Optional[torch.LongTensor] = None context_attention_mask: Optional[torch.LongTensor] = None question_encoder_last_hidden_state: Optional[torch.FloatTensor] = None - question_enc_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - question_enc_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + question_enc_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + question_enc_attentions: Optional[tuple[torch.FloatTensor, ...]] = None generator_enc_last_hidden_state: Optional[torch.FloatTensor] = None - generator_enc_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_enc_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_dec_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_dec_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + generator_enc_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + generator_enc_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + generator_dec_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + generator_dec_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + generator_cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -138,7 +138,7 @@ class RetrievAugLMOutput(ModelOutput): doc_scores (`torch.FloatTensor` of shape `(batch_size, config.n_docs)`): Score between each retrieved document embeddings (see `retrieved_doc_embeds`) and `question_encoder_last_hidden_state`. - past_key_values (`List[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[torch.FloatTensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `torch.FloatTensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -202,20 +202,20 @@ class RetrievAugLMOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None doc_scores: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None retrieved_doc_embeds: Optional[torch.FloatTensor] = None retrieved_doc_ids: Optional[torch.LongTensor] = None context_input_ids: Optional[torch.LongTensor] = None context_attention_mask: Optional[torch.LongTensor] = None question_encoder_last_hidden_state: Optional[torch.FloatTensor] = None - question_enc_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - question_enc_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + question_enc_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + question_enc_attentions: Optional[tuple[torch.FloatTensor, ...]] = None generator_enc_last_hidden_state: Optional[torch.FloatTensor] = None - generator_enc_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_enc_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_dec_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_dec_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - generator_cross_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + generator_enc_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + generator_enc_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + generator_dec_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + generator_dec_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + generator_cross_attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring( @@ -432,10 +432,10 @@ class RagModel(RagPreTrainedModel): self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, doc_scores: Optional[torch.FloatTensor] = None, context_input_ids: Optional[torch.LongTensor] = None, context_attention_mask: Optional[torch.LongTensor] = None, @@ -444,7 +444,7 @@ class RagModel(RagPreTrainedModel): output_hidden_states: Optional[bool] = None, output_retrieved: Optional[bool] = None, n_docs: Optional[int] = None, - ) -> Union[Tuple[torch.Tensor], RetrievAugLMOutput]: + ) -> Union[tuple[torch.Tensor], RetrievAugLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. [`RagConfig`], used to initialize the model, specifies @@ -706,10 +706,10 @@ class RagSequenceForGeneration(RagPreTrainedModel): self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, context_input_ids: Optional[torch.LongTensor] = None, context_attention_mask: Optional[torch.LongTensor] = None, doc_scores: Optional[torch.FloatTensor] = None, @@ -930,7 +930,7 @@ class RagSequenceForGeneration(RagPreTrainedModel): Number of beams for beam search. 1 means no beam search. n_docs (`int`, *optional*, defaults to `config.n_docs`) Number of documents to retrieve and/or number of documents for which to generate an answer. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional kwargs will be passed to [`~generation.GenerationMixin.generate`]. Return: @@ -1216,10 +1216,10 @@ class RagTokenForGeneration(RagPreTrainedModel, GenerationMixin): self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.FloatTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, context_input_ids: Optional[torch.LongTensor] = None, context_attention_mask: Optional[torch.LongTensor] = None, doc_scores: Optional[torch.FloatTensor] = None, @@ -1394,7 +1394,7 @@ class RagTokenForGeneration(RagPreTrainedModel, GenerationMixin): doc_scores: Optional[torch.FloatTensor] = None, n_docs: Optional[int] = None, generation_config: Optional[GenerationConfig] = None, - prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None, + prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], list[int]]] = None, logits_processor: Optional[LogitsProcessorList] = LogitsProcessorList(), stopping_criteria: Optional[StoppingCriteriaList] = StoppingCriteriaList(), **kwargs, @@ -1440,7 +1440,7 @@ class RagTokenForGeneration(RagPreTrainedModel, GenerationMixin): priority: 1) from the `generation_config.json` model file, if it exists; 2) from the model configuration. Please note that unspecified parameters will inherit [`~generation.GenerationConfig`]'s default values, whose documentation should be checked to parameterize generation. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the next generation step conditioned on @@ -1455,7 +1455,7 @@ class RagTokenForGeneration(RagPreTrainedModel, GenerationMixin): Custom stopping criteria that complement the default stopping criteria built from arguments and a model's config. If a stopping criteria is passed that is already created with the arguments or a model's config an error is thrown. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. diff --git a/src/transformers/models/rag/modeling_tf_rag.py b/src/transformers/models/rag/modeling_tf_rag.py index 650e591dc2e..74004dea6f6 100644 --- a/src/transformers/models/rag/modeling_tf_rag.py +++ b/src/transformers/models/rag/modeling_tf_rag.py @@ -19,7 +19,7 @@ from __future__ import annotations import copy from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -55,7 +55,7 @@ class TFRetrievAugLMMarginOutput(ModelOutput): logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head. The score is possibly marginalized over all documents for each vocabulary token. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -116,20 +116,20 @@ class TFRetrievAugLMMarginOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None doc_scores: tf.Tensor | None = None retrieved_doc_embeds: tf.Tensor | None = None retrieved_doc_ids: tf.Tensor | None = None context_input_ids: tf.Tensor | None = None context_attention_mask: tf.Tensor | None = None question_encoder_last_hidden_state: tf.Tensor | None = None - question_enc_hidden_states: Tuple[tf.Tensor, ...] | None = None - question_enc_attentions: Tuple[tf.Tensor, ...] | None = None + question_enc_hidden_states: tuple[tf.Tensor, ...] | None = None + question_enc_attentions: tuple[tf.Tensor, ...] | None = None generator_enc_last_hidden_state: tf.Tensor | None = None - generator_enc_hidden_states: Tuple[tf.Tensor, ...] | None = None - generator_enc_attentions: Tuple[tf.Tensor, ...] | None = None - generator_dec_hidden_states: Tuple[tf.Tensor, ...] | None = None - generator_dec_attentions: Tuple[tf.Tensor, ...] | None = None + generator_enc_hidden_states: tuple[tf.Tensor, ...] | None = None + generator_enc_attentions: tuple[tf.Tensor, ...] | None = None + generator_dec_hidden_states: tuple[tf.Tensor, ...] | None = None + generator_dec_attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -139,7 +139,7 @@ class TFRetrievAugLMOutput(ModelOutput): logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.vocab_size)`): Prediction scores of the language modeling head. The score is possibly marginalized over all documents for each vocabulary token. - past_key_values (`List[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_key_values (`list[tf.Tensor]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `tf.Tensor` of length `config.n_layers`, with each tensor of shape `(2, batch_size, num_heads, sequence_length, embed_size_per_head)`). @@ -199,20 +199,20 @@ class TFRetrievAugLMOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - past_key_values: List[tf.Tensor] | None = None + past_key_values: list[tf.Tensor] | None = None doc_scores: tf.Tensor | None = None retrieved_doc_embeds: tf.Tensor | None = None retrieved_doc_ids: tf.Tensor | None = None context_input_ids: tf.Tensor | None = None context_attention_mask: tf.Tensor | None = None question_encoder_last_hidden_state: tf.Tensor | None = None - question_enc_hidden_states: Tuple[tf.Tensor, ...] | None = None - question_enc_attentions: Tuple[tf.Tensor, ...] | None = None + question_enc_hidden_states: tuple[tf.Tensor, ...] | None = None + question_enc_attentions: tuple[tf.Tensor, ...] | None = None generator_enc_last_hidden_state: tf.Tensor | None = None - generator_enc_hidden_states: Tuple[tf.Tensor, ...] | None = None - generator_enc_attentions: Tuple[tf.Tensor, ...] | None = None - generator_dec_hidden_states: Tuple[tf.Tensor, ...] | None = None - generator_dec_attentions: Tuple[tf.Tensor, ...] | None = None + generator_enc_hidden_states: tuple[tf.Tensor, ...] | None = None + generator_enc_attentions: tuple[tf.Tensor, ...] | None = None + generator_dec_hidden_states: tuple[tf.Tensor, ...] | None = None + generator_dec_attentions: tuple[tf.Tensor, ...] | None = None class TFRagPreTrainedModel(TFPreTrainedModel): @@ -554,7 +554,7 @@ class TFRagModel(TFRagPreTrainedModel): encoder_outputs: np.ndarray | tf.Tensor | None = None, decoder_input_ids: np.ndarray | tf.Tensor | None = None, decoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Tuple[Tuple[Union[np.ndarray, tf.Tensor]]] | None = None, + past_key_values: tuple[tuple[Union[np.ndarray, tf.Tensor]]] | None = None, doc_scores: np.ndarray | tf.Tensor | None = None, context_input_ids: np.ndarray | tf.Tensor | None = None, context_attention_mask: np.ndarray | tf.Tensor | None = None, @@ -859,7 +859,7 @@ class TFRagTokenForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingLoss decoder_input_ids: np.ndarray | tf.Tensor | None = None, decoder_attention_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Tuple[Tuple[Union[np.ndarray, tf.Tensor]]] | None = None, + past_key_values: tuple[tuple[Union[np.ndarray, tf.Tensor]]] | None = None, doc_scores: np.ndarray | tf.Tensor | None = None, context_input_ids: np.ndarray | tf.Tensor | None = None, context_attention_mask: np.ndarray | tf.Tensor | None = None, @@ -886,7 +886,7 @@ class TFRagTokenForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingLoss reduce_loss (`bool`, *optional*): Only relevant if `labels` is passed. If `True`, the NLL loss is reduced using the `tf.Tensor.sum` operation. - kwargs (`Dict[str, any]`, *optional*, defaults to `{}`): + kwargs (`dict[str, any]`, *optional*, defaults to `{}`): Legacy dictionary, which is required so that model can use *generate()* function. Returns: @@ -1061,7 +1061,7 @@ class TFRagTokenForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingLoss Custom logits processors that complement the default logits processors built from arguments and a model's config. If a logit processor is passed that is already created with the arguments or a model's config an error is thrown. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. @@ -1373,7 +1373,7 @@ class TFRagSequenceForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingL decoder_input_ids: np.ndarray | tf.Tensor | None = None, decoder_attention_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, doc_scores: np.ndarray | tf.Tensor | None = None, context_input_ids: np.ndarray | tf.Tensor | None = None, context_attention_mask: np.ndarray | tf.Tensor | None = None, @@ -1388,7 +1388,7 @@ class TFRagSequenceForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingL return_dict: Optional[bool] = None, training: bool = False, **kwargs, # needs kwargs for generation - ) -> Union[Tuple[tf.Tensor], TFRetrievAugLMMarginOutput]: + ) -> Union[tuple[tf.Tensor], TFRetrievAugLMMarginOutput]: r""" exclude_bos_score (`bool`, *optional*): Only relevant if `labels` is passed. If `True`, the score of the BOS token is disregarded when computing @@ -1400,7 +1400,7 @@ class TFRagSequenceForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingL reduce_loss (`bool`, *optional*): Only relevant if `labels` is passed. If `True`, the NLL loss is reduced using the `tf.Tensor.sum` operation. - kwargs (`Dict[str, any]`, *optional*, defaults to `{}`): + kwargs (`dict[str, any]`, *optional*, defaults to `{}`): Legacy dictionary, which is required so that model can use *generate()* function. Returns: @@ -1647,7 +1647,7 @@ class TFRagSequenceForGeneration(TFRagPreTrainedModel, TFCausalLanguageModelingL Number of beams for beam search. 1 means no beam search. n_docs (`int`, *optional*, defaults to `config.n_docs`) Number of documents to retrieve and/or number of documents for which to generate an answer. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional kwargs will be passed to [`~generation.GenerationMixin.generate`] Return: diff --git a/src/transformers/models/rag/retrieval_rag.py b/src/transformers/models/rag/retrieval_rag.py index 355b131be50..4b6ab3c2d28 100644 --- a/src/transformers/models/rag/retrieval_rag.py +++ b/src/transformers/models/rag/retrieval_rag.py @@ -18,7 +18,7 @@ import os import pickle import time from collections.abc import Iterable -from typing import List, Optional, Tuple +from typing import Optional import numpy as np @@ -47,7 +47,7 @@ class Index: A base class for the Indices encapsulated by the [`RagRetriever`]. """ - def get_doc_dicts(self, doc_ids: np.ndarray) -> List[dict]: + def get_doc_dicts(self, doc_ids: np.ndarray) -> list[dict]: """ Returns a list of dictionaries, containing titles and text of the retrieved documents. @@ -57,7 +57,7 @@ class Index: """ raise NotImplementedError - def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> Tuple[np.ndarray, np.ndarray]: + def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> tuple[np.ndarray, np.ndarray]: """ For each query in the batch, retrieves `n_docs` documents. @@ -187,7 +187,7 @@ class LegacyIndex(Index): doc_dicts.append(doc_dict) return doc_dicts - def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> Tuple[np.ndarray, np.ndarray]: + def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> tuple[np.ndarray, np.ndarray]: aux_dim = np.zeros(len(question_hidden_states), dtype="float32").reshape(-1, 1) query_nhsw_vectors = np.hstack((question_hidden_states, aux_dim)) _, docs_ids = self.index.search(query_nhsw_vectors, n_docs) @@ -226,10 +226,10 @@ class HFIndexBase(Index): def is_initialized(self): return self._index_initialized - def get_doc_dicts(self, doc_ids: np.ndarray) -> List[dict]: + def get_doc_dicts(self, doc_ids: np.ndarray) -> list[dict]: return [self.dataset[doc_ids[i].tolist()] for i in range(doc_ids.shape[0])] - def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> Tuple[np.ndarray, np.ndarray]: + def get_top_docs(self, question_hidden_states: np.ndarray, n_docs=5) -> tuple[np.ndarray, np.ndarray]: _, ids = self.dataset.search_batch("embeddings", question_hidden_states, n_docs) docs = [self.dataset[[i for i in indices if i >= 0]] for indices in ids] vectors = [doc["embeddings"] for doc in docs] @@ -541,10 +541,10 @@ class RagRetriever: return contextualized_inputs["input_ids"], contextualized_inputs["attention_mask"] - def _chunk_tensor(self, t: Iterable, chunk_size: int) -> List[Iterable]: + def _chunk_tensor(self, t: Iterable, chunk_size: int) -> list[Iterable]: return [t[i : i + chunk_size] for i in range(0, len(t), chunk_size)] - def _main_retrieve(self, question_hidden_states: np.ndarray, n_docs: int) -> Tuple[np.ndarray, np.ndarray]: + def _main_retrieve(self, question_hidden_states: np.ndarray, n_docs: int) -> tuple[np.ndarray, np.ndarray]: question_hidden_states_batched = self._chunk_tensor(question_hidden_states, self.batch_size) ids_batched = [] vectors_batched = [] @@ -561,7 +561,7 @@ class RagRetriever: np.array(vectors_batched), ) # shapes (batch_size, n_docs) and (batch_size, n_docs, d) - def retrieve(self, question_hidden_states: np.ndarray, n_docs: int) -> Tuple[np.ndarray, np.ndarray, List[dict]]: + def retrieve(self, question_hidden_states: np.ndarray, n_docs: int) -> tuple[np.ndarray, np.ndarray, list[dict]]: """ Retrieves documents for specified `question_hidden_states`. @@ -572,12 +572,12 @@ class RagRetriever: The number of docs retrieved per query. Return: - `Tuple[np.ndarray, np.ndarray, List[dict]]`: A tuple with the following objects: + `tuple[np.ndarray, np.ndarray, list[dict]]`: A tuple with the following objects: - **retrieved_doc_embeds** (`np.ndarray` of shape `(batch_size, n_docs, dim)`) -- The retrieval embeddings of the retrieved docs per query. - **doc_ids** (`np.ndarray` of shape `(batch_size, n_docs)`) -- The ids of the documents in the index - - **doc_dicts** (`List[dict]`): The `retrieved_doc_embeds` examples per query. + - **doc_dicts** (`list[dict]`): The `retrieved_doc_embeds` examples per query. """ doc_ids, retrieved_doc_embeds = self._main_retrieve(question_hidden_states, n_docs) @@ -590,7 +590,7 @@ class RagRetriever: def __call__( self, - question_input_ids: List[List[int]], + question_input_ids: list[list[int]], question_hidden_states: np.ndarray, prefix=None, n_docs=None, @@ -600,7 +600,7 @@ class RagRetriever: Retrieves documents for specified `question_hidden_states`. Args: - question_input_ids (`List[List[int]]`) batch of input ids + question_input_ids (`list[list[int]]`) batch of input ids question_hidden_states (`np.ndarray` of shape `(batch_size, vector_size)`: A batch of query vectors to retrieve with. prefix (`str`, *optional*): diff --git a/src/transformers/models/rag/tokenization_rag.py b/src/transformers/models/rag/tokenization_rag.py index 428b5f66446..217dd2d82df 100644 --- a/src/transformers/models/rag/tokenization_rag.py +++ b/src/transformers/models/rag/tokenization_rag.py @@ -16,7 +16,7 @@ import os import warnings -from typing import List, Optional +from typing import Optional from ...tokenization_utils_base import BatchEncoding from ...utils import logging @@ -76,8 +76,8 @@ class RagTokenizer: def prepare_seq2seq_batch( self, - src_texts: List[str], - tgt_texts: Optional[List[str]] = None, + src_texts: list[str], + tgt_texts: Optional[list[str]] = None, max_length: Optional[int] = None, max_target_length: Optional[int] = None, padding: str = "longest", diff --git a/src/transformers/models/recurrent_gemma/configuration_recurrent_gemma.py b/src/transformers/models/recurrent_gemma/configuration_recurrent_gemma.py index 60a034f57d3..ef2a0869912 100644 --- a/src/transformers/models/recurrent_gemma/configuration_recurrent_gemma.py +++ b/src/transformers/models/recurrent_gemma/configuration_recurrent_gemma.py @@ -76,7 +76,7 @@ class RecurrentGemmaConfig(PretrainedConfig): The partial rotary factor used in the initialization of the rotary embeddings. rope_theta (`float`, *optional*, defaults to 10000.0): The base period of the RoPE embeddings. - block_types (`List[str]`, *optional*, defaults to `('recurrent', 'recurrent', 'attention')`): + block_types (`list[str]`, *optional*, defaults to `('recurrent', 'recurrent', 'attention')`): List of aleternating blocks that will be repeated to initialize the `temporal_block` layer. attention_dropout (`float`, *optional*, defaults to 0.0): dropout value to use after the attention softmax. num_key_value_heads (`16`, *optional*, defaults to 16): Number of key value heads to use GQA. diff --git a/src/transformers/models/recurrent_gemma/modeling_recurrent_gemma.py b/src/transformers/models/recurrent_gemma/modeling_recurrent_gemma.py index 315edd29864..47e79f34870 100644 --- a/src/transformers/models/recurrent_gemma/modeling_recurrent_gemma.py +++ b/src/transformers/models/recurrent_gemma/modeling_recurrent_gemma.py @@ -16,7 +16,7 @@ """PyTorch RecurrentGemma model.""" import math -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -166,7 +166,7 @@ class RecurrentGemmaSdpaAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, cache_position: Optional[torch.LongTensor] = None, use_cache: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -299,7 +299,7 @@ class RecurrentGemmaRglru(nn.Module): self, activations: torch.Tensor, position_ids: torch.Tensor, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: batch_size, seq_len, lru_width = activations.shape reset = position_ids[:, :, None] == 0 @@ -346,7 +346,7 @@ class RecurrentGemmaRglru(nn.Module): reset: torch.Tensor, recurrent_states: Union[torch.Tensor, None], acc_dtype: torch.dtype = torch.float32, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """Runs the recurrence of a linear RNN. Args: @@ -419,7 +419,7 @@ class RecurrentGemmaRecurrentBlock(nn.Module): attention_mask: torch.Tensor, cache_position: torch.Tensor, use_cache: bool = True, - ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]: + ) -> tuple[torch.Tensor, dict[str, torch.Tensor]]: _, seq_len, _ = input_states.shape y_branch = self.linear_y(input_states) @@ -488,7 +488,7 @@ class RecurrentGemmaDecoderLayer(nn.Module): attention_mask: torch.Tensor, cache_position: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, - ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]: + ) -> tuple[torch.Tensor, dict[str, torch.Tensor]]: raw_activations = activations inputs_normalized = self.temporal_pre_norm(raw_activations) # RMSNorm introduces slight slight differences @@ -611,7 +611,7 @@ class RecurrentGemmaModel(RecurrentGemmaPreTrainedModel): use_cache: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -747,7 +747,7 @@ class RecurrentGemmaForCausalLM(RecurrentGemmaPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, use_cache: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/reformer/configuration_reformer.py b/src/transformers/models/reformer/configuration_reformer.py index ab9b1c5f64c..d9f7734b9f3 100755 --- a/src/transformers/models/reformer/configuration_reformer.py +++ b/src/transformers/models/reformer/configuration_reformer.py @@ -35,7 +35,7 @@ class ReformerConfig(PretrainedConfig): Args: attention_head_size (`int`, *optional*, defaults to 64): Dimensionality of the projected key, query and value vectors - attn_layers (`List[str]`, *optional*, defaults to `["local", "lsh", "local", "lsh", "local", "lsh"]`): + attn_layers (`list[str]`, *optional*, defaults to `["local", "lsh", "local", "lsh", "local", "lsh"]`): List of attention layer types in ascending order. It can be chosen between a LSHSelfAttention layer (`"lsh"`) and a LocalSelfAttention layer (`"local"`). @@ -47,13 +47,13 @@ class ReformerConfig(PretrainedConfig): axial_norm_std (`float`, *optional*, defaults to 1.0): The standard deviation of the normal_initializer for initializing the weight matrices of the axial positional encodings. - axial_pos_shape (`List[int]`, *optional*, defaults to `[64, 64]`): + axial_pos_shape (`list[int]`, *optional*, defaults to `[64, 64]`): The position dims of the axial position encodings. During training, the product of the position dims has to be equal to the sequence length. For more information on how axial position embeddings work, see [Axial Position Encodings](reformer#axial-positional-encodings). - axial_pos_embds_dim (`List[int]`, *optional*, defaults to `[64, 192]`): + axial_pos_embds_dim (`list[int]`, *optional*, defaults to `[64, 192]`): The embedding dims of the axial position encodings. The sum of the embedding dims has to be equal to the hidden size. @@ -113,7 +113,7 @@ class ReformerConfig(PretrainedConfig): just in case (e.g., 512 or 1024 or 2048). num_attention_heads (`int`, *optional*, defaults to 12): Number of attention heads for each attention layer in the Transformer encoder. - num_buckets (`int` or `List[int]`, *optional*): + num_buckets (`int` or `list[int]`, *optional*): Number of buckets, the key query vectors can be "hashed into" using the locality sensitive hashing scheme. Each query key vector is hashed into a hash in `1, ..., num_buckets`. The number of buckets can also be factorized into a list for improved memory complexity. In this case, each query key vector is hashed into a diff --git a/src/transformers/models/reformer/modeling_reformer.py b/src/transformers/models/reformer/modeling_reformer.py index 3fd77f8fb46..ac9b71a64c2 100755 --- a/src/transformers/models/reformer/modeling_reformer.py +++ b/src/transformers/models/reformer/modeling_reformer.py @@ -20,7 +20,7 @@ from collections import namedtuple from dataclasses import dataclass from functools import reduce from operator import mul -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -1815,7 +1815,7 @@ class ReformerModelOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - past_buckets_states (`List[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_buckets_states (`list[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `Tuple(torch.LongTensor, torch.FloatTensor` of length `config.n_layers`, with the first element being the previous *buckets* of shape `(batch_size, num_heads, num_hashes, sequence_length)`) and the second being the previous *hidden_states* of shape `(batch_size, sequence_length, hidden_size)`). @@ -1836,9 +1836,9 @@ class ReformerModelOutput(ModelOutput): """ last_hidden_state: torch.FloatTensor - past_buckets_states: Optional[List[Tuple[torch.LongTensor, torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_buckets_states: Optional[list[tuple[torch.LongTensor, torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -1854,7 +1854,7 @@ class ReformerModelWithLMHeadOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - past_buckets_states (`List[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): + past_buckets_states (`list[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`): List of `Tuple(torch.LongTensor, torch.FloatTensor` of length `config.n_layers`, with the first element being the previous *buckets* of shape `(batch_size, num_heads, num_hashes, sequence_length)`) and the second being the previous *hidden_states* of shape `(batch_size, sequence_length, hidden_size)`). @@ -1876,9 +1876,9 @@ class ReformerModelWithLMHeadOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_buckets_states: Optional[List[Tuple[torch.LongTensor, torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_buckets_states: Optional[list[tuple[torch.LongTensor, torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring @@ -1919,12 +1919,12 @@ class ReformerModel(ReformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, num_hashes: Optional[int] = None, - past_buckets_states: Optional[List[Tuple[torch.Tensor]]] = None, + past_buckets_states: Optional[list[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ReformerModelOutput]: + ) -> Union[tuple, ReformerModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. During training the input_ids sequence_length has to be @@ -1940,7 +1940,7 @@ class ReformerModel(ReformerPreTrainedModel): the default defined in `config.num_hashes`. For more information, see `num_hashes` in [`ReformerConfig`]. - past_buckets_states (`List[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*): + past_buckets_states (`list[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*): List of `Tuple(torch.LongTensor, torch.FloatTensor` of length `config.n_layers`, with the first element being the previous *buckets* of shape `(batch_size, num_heads, num_hashes, sequence_length)`) and the second being the previous *hidden_states* of shape `(batch_size, sequence_length, hidden_size)`). @@ -2153,14 +2153,14 @@ class ReformerModelWithLMHead(ReformerPreTrainedModel, GenerationMixin): head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, num_hashes: Optional[int] = None, - past_buckets_states: Optional[List[Tuple[torch.Tensor]]] = None, + past_buckets_states: Optional[list[tuple[torch.Tensor]]] = None, use_cache: Optional[bool] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. During training the input_ids sequence_length has to be @@ -2176,7 +2176,7 @@ class ReformerModelWithLMHead(ReformerPreTrainedModel, GenerationMixin): the default defined in `config.num_hashes`. For more information, see `num_hashes` in [`ReformerConfig`]. - past_buckets_states (`List[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*): + past_buckets_states (`list[Tuple(torch.LongTensor, torch.FloatTensor)]`, *optional*): List of `Tuple(torch.LongTensor, torch.FloatTensor` of length `config.n_layers`, with the first element being the previous *buckets* of shape `(batch_size, num_heads, num_hashes, sequence_length)`) and the second being the previous *hidden_states* of shape `(batch_size, sequence_length, hidden_size)`). @@ -2297,7 +2297,7 @@ class ReformerForMaskedLM(ReformerPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. During training the input_ids sequence_length has to be @@ -2430,7 +2430,7 @@ class ReformerForSequenceClassification(ReformerPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. During training the input_ids sequence_length has to be @@ -2581,7 +2581,7 @@ class ReformerForQuestionAnswering(ReformerPreTrainedModel): output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. During training the input_ids sequence_length has to be diff --git a/src/transformers/models/reformer/tokenization_reformer.py b/src/transformers/models/reformer/tokenization_reformer.py index db2faf5dbc7..458b72df4ff 100644 --- a/src/transformers/models/reformer/tokenization_reformer.py +++ b/src/transformers/models/reformer/tokenization_reformer.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -58,7 +58,7 @@ class ReformerTokenizer(PreTrainedTokenizer): unk_token (`str`, *optional*, defaults to `""`): The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead. - additional_special_tokens (`List[str]`, *optional*, defaults to `[]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `[]`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -86,7 +86,7 @@ class ReformerTokenizer(PreTrainedTokenizer): eos_token="", unk_token="", additional_special_tokens=[], - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -107,7 +107,7 @@ class ReformerTokenizer(PreTrainedTokenizer): def vocab_size(self): return self.sp_model.get_piece_size() - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)} vocab.update(self.added_tokens_encoder) return vocab @@ -127,7 +127,7 @@ class ReformerTokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" return self.sp_model.encode(text, out_type=str) @@ -155,7 +155,7 @@ class ReformerTokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string.strip() - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/reformer/tokenization_reformer_fast.py b/src/transformers/models/reformer/tokenization_reformer_fast.py index f6d8c7c6005..d68528de587 100644 --- a/src/transformers/models/reformer/tokenization_reformer_fast.py +++ b/src/transformers/models/reformer/tokenization_reformer_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging @@ -63,7 +63,7 @@ class ReformerTokenizerFast(PreTrainedTokenizerFast): token instead. pad_token (`str`, *optional*, defaults to `""`): The token used for padding, for example when batching sequences of different lengths. - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. """ @@ -91,7 +91,7 @@ class ReformerTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/regnet/configuration_regnet.py b/src/transformers/models/regnet/configuration_regnet.py index c089f67297a..4858bd1e461 100644 --- a/src/transformers/models/regnet/configuration_regnet.py +++ b/src/transformers/models/regnet/configuration_regnet.py @@ -36,9 +36,9 @@ class RegNetConfig(PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"y"`): The layer to use, it can be either `"x" or `"y"`. An `x` layer is a ResNet's BottleNeck layer with diff --git a/src/transformers/models/regnet/convert_regnet_seer_10b_to_pytorch.py b/src/transformers/models/regnet/convert_regnet_seer_10b_to_pytorch.py index 95bd5b85428..9bf3a17e918 100644 --- a/src/transformers/models/regnet/convert_regnet_seer_10b_to_pytorch.py +++ b/src/transformers/models/regnet/convert_regnet_seer_10b_to_pytorch.py @@ -25,7 +25,7 @@ from dataclasses import dataclass, field from functools import partial from pathlib import Path from pprint import pprint -from typing import Dict, List, Optional, Tuple +from typing import Optional import torch import torch.nn as nn @@ -46,9 +46,9 @@ logger = logging.get_logger() @dataclass class Tracker: module: nn.Module - traced: List[nn.Module] = field(default_factory=list) + traced: list[nn.Module] = field(default_factory=list) handles: list = field(default_factory=list) - name2module: Dict[str, nn.Module] = field(default_factory=OrderedDict) + name2module: dict[str, nn.Module] = field(default_factory=OrderedDict) def _forward_hook(self, m, inputs: Tensor, outputs: Tensor, name: str): has_not_submodules = len(list(m.modules())) == 1 or isinstance(m, nn.Conv2d) or isinstance(m, nn.BatchNorm2d) @@ -77,7 +77,7 @@ class FakeRegNetVisslWrapper(nn.Module): def __init__(self, model: nn.Module): super().__init__() - feature_blocks: List[Tuple[str, nn.Module]] = [] + feature_blocks: list[tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("conv1", model.stem)) # - get all the feature blocks @@ -106,7 +106,7 @@ class FakeRegNetParams(RegNetParams): return [(8, 2, 2, 8, 1.0), (8, 2, 7, 8, 1.0), (8, 2, 17, 8, 1.0), (8, 2, 1, 8, 1.0)] -def get_from_to_our_keys(model_name: str) -> Dict[str, str]: +def get_from_to_our_keys(model_name: str) -> dict[str, str]: """ Returns a dictionary that maps from original model's key -> our implementation's keys """ @@ -184,7 +184,7 @@ def convert_weights_and_push(save_directory: Path, model_name: Optional[str] = N } # add seer weights logic - def load_using_classy_vision(checkpoint_url: str) -> Tuple[Dict, Dict]: + def load_using_classy_vision(checkpoint_url: str) -> tuple[dict, dict]: files = torch.hub.load_state_dict_from_url(checkpoint_url, model_dir=str(save_directory), map_location="cpu") # check if we have a head, if yes add it model_state_dict = files["classy_state_dict"]["base_model"]["model"] diff --git a/src/transformers/models/regnet/convert_regnet_to_pytorch.py b/src/transformers/models/regnet/convert_regnet_to_pytorch.py index 51c0e3a4a0a..b7b48568a18 100644 --- a/src/transformers/models/regnet/convert_regnet_to_pytorch.py +++ b/src/transformers/models/regnet/convert_regnet_to_pytorch.py @@ -19,7 +19,7 @@ import json from dataclasses import dataclass, field from functools import partial from pathlib import Path -from typing import Callable, Dict, List, Optional, Tuple +from typing import Callable, Optional import timm import torch @@ -40,7 +40,7 @@ logger = logging.get_logger() @dataclass class Tracker: module: nn.Module - traced: List[nn.Module] = field(default_factory=list) + traced: list[nn.Module] = field(default_factory=list) handles: list = field(default_factory=list) def _forward_hook(self, m, inputs: Tensor, outputs: Tensor): @@ -66,8 +66,8 @@ class ModuleTransfer: src: nn.Module dest: nn.Module verbose: int = 1 - src_skip: List = field(default_factory=list) - dest_skip: List = field(default_factory=list) + src_skip: list = field(default_factory=list) + dest_skip: list = field(default_factory=list) raise_if_mismatch: bool = True def __call__(self, x: Tensor): @@ -101,7 +101,7 @@ class FakeRegNetVisslWrapper(nn.Module): def __init__(self, model: nn.Module): super().__init__() - feature_blocks: List[Tuple[str, nn.Module]] = [] + feature_blocks: list[tuple[str, nn.Module]] = [] # - get the stem feature_blocks.append(("conv1", model.stem)) # - get all the feature blocks @@ -129,7 +129,7 @@ class NameToFromModelFuncMap(dict): x_split = x.split("-") return x_split[0] + x_split[1] + "_" + "".join(x_split[2:]) - def __getitem__(self, x: str) -> Callable[[], Tuple[nn.Module, Dict]]: + def __getitem__(self, x: str) -> Callable[[], tuple[nn.Module, dict]]: # default to timm! if x not in self: x = self.convert_name_to_timm(x) @@ -154,7 +154,7 @@ class NameToOurModelFuncMap(dict): return val -def manually_copy_vissl_head(from_state_dict, to_state_dict, keys: List[Tuple[str, str]]): +def manually_copy_vissl_head(from_state_dict, to_state_dict, keys: list[tuple[str, str]]): for from_key, to_key in keys: to_state_dict[to_key] = from_state_dict[from_key].clone() print(f"Copied key={from_key} to={to_key}") @@ -339,7 +339,7 @@ def convert_weights_and_push(save_directory: Path, model_name: Optional[str] = N names_to_from_model_map = NameToFromModelFuncMap() # add seer weights logic - def load_using_classy_vision(checkpoint_url: str, model_func: Callable[[], nn.Module]) -> Tuple[nn.Module, Dict]: + def load_using_classy_vision(checkpoint_url: str, model_func: Callable[[], nn.Module]) -> tuple[nn.Module, dict]: files = torch.hub.load_state_dict_from_url(checkpoint_url, model_dir=str(save_directory), map_location="cpu") model = model_func() # check if we have a head, if yes add it diff --git a/src/transformers/models/regnet/modeling_flax_regnet.py b/src/transformers/models/regnet/modeling_flax_regnet.py index 90f16560c79..2cc3707fa51 100644 --- a/src/transformers/models/regnet/modeling_flax_regnet.py +++ b/src/transformers/models/regnet/modeling_flax_regnet.py @@ -15,7 +15,7 @@ from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -576,7 +576,7 @@ class FlaxRegNetPreTrainedModel(FlaxPreTrainedModel): input_shape = (1, config.image_size, config.image_size, config.num_channels) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors pixel_values = jnp.zeros(input_shape, dtype=self.dtype) diff --git a/src/transformers/models/regnet/modeling_tf_regnet.py b/src/transformers/models/regnet/modeling_tf_regnet.py index 07f7058659e..b06e64b3b40 100644 --- a/src/transformers/models/regnet/modeling_tf_regnet.py +++ b/src/transformers/models/regnet/modeling_tf_regnet.py @@ -14,7 +14,7 @@ # limitations under the License. """TensorFlow RegNet model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -505,7 +505,7 @@ class TFRegNetModel(TFRegNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndNoAttention, tuple[tf.Tensor]]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -568,7 +568,7 @@ class TFRegNetForImageClassification(TFRegNetPreTrainedModel, TFSequenceClassifi output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/rembert/modeling_rembert.py b/src/transformers/models/rembert/modeling_rembert.py index ab1953659db..22a3ba7aeb9 100755 --- a/src/transformers/models/rembert/modeling_rembert.py +++ b/src/transformers/models/rembert/modeling_rembert.py @@ -16,7 +16,7 @@ import math import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -230,9 +230,9 @@ class RemBertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: bool = False, - ) -> Tuple: + ) -> tuple: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -351,9 +351,9 @@ class RemBertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -422,9 +422,9 @@ class RemBertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -503,12 +503,12 @@ class RemBertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( @@ -705,12 +705,12 @@ class RemBertModel(RemBertPreTrainedModel): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -840,7 +840,7 @@ class RemBertForMaskedLM(RemBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -942,14 +942,14 @@ class RemBertForCausalLM(RemBertPreTrainedModel, GenerationMixin): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1055,7 +1055,7 @@ class RemBertForSequenceClassification(RemBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1140,7 +1140,7 @@ class RemBertForMultipleChoice(RemBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1245,7 +1245,7 @@ class RemBertForTokenClassification(RemBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1313,7 +1313,7 @@ class RemBertForQuestionAnswering(RemBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.rembert( diff --git a/src/transformers/models/rembert/modeling_tf_rembert.py b/src/transformers/models/rembert/modeling_tf_rembert.py index 4ec2368ab2a..2ceba35cf19 100644 --- a/src/transformers/models/rembert/modeling_tf_rembert.py +++ b/src/transformers/models/rembert/modeling_tf_rembert.py @@ -17,7 +17,7 @@ from __future__ import annotations import math -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -188,10 +188,10 @@ class TFRemBertSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -326,10 +326,10 @@ class TFRemBertAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -443,10 +443,10 @@ class TFRemBertLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -544,13 +544,13 @@ class TFRemBertEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_values: Tuple[Tuple[tf.Tensor]], + past_key_values: tuple[tuple[tf.Tensor]], use_cache: bool, output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: hidden_states = self.embedding_hidden_mapping_in(inputs=hidden_states) all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -686,7 +686,7 @@ class TFRemBertLMPredictionHead(keras.layers.Layer): self.decoder = value self.decoder.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"decoder_bias": self.decoder_bias} def set_bias(self, value: tf.Variable): @@ -766,13 +766,13 @@ class TFRemBertMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -982,7 +982,7 @@ REMBERT_START_DOCSTRING = r""" REMBERT_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1063,13 +1063,13 @@ class TFRemBertModel(TFRemBertPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1081,7 +1081,7 @@ class TFRemBertModel(TFRemBertPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1155,7 +1155,7 @@ class TFRemBertForMaskedLM(TFRemBertPreTrainedModel, TFMaskedLanguageModelingLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1246,14 +1246,14 @@ class TFRemBertForCausalLM(TFRemBertPreTrainedModel, TFCausalLanguageModelingLos inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1265,7 +1265,7 @@ class TFRemBertForCausalLM(TFRemBertPreTrainedModel, TFCausalLanguageModelingLos - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1369,7 +1369,7 @@ class TFRemBertForSequenceClassification(TFRemBertPreTrainedModel, TFSequenceCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1454,7 +1454,7 @@ class TFRemBertForMultipleChoice(TFRemBertPreTrainedModel, TFMultipleChoiceLoss) return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1564,7 +1564,7 @@ class TFRemBertForTokenClassification(TFRemBertPreTrainedModel, TFTokenClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1649,7 +1649,7 @@ class TFRemBertForQuestionAnswering(TFRemBertPreTrainedModel, TFQuestionAnswerin start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/rembert/tokenization_rembert.py b/src/transformers/models/rembert/tokenization_rembert.py index a0ba7cf5ea6..7a424f1481f 100644 --- a/src/transformers/models/rembert/tokenization_rembert.py +++ b/src/transformers/models/rembert/tokenization_rembert.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional import sentencepiece as spm @@ -161,8 +161,8 @@ class RemBertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A REMBERT sequence has the following format: @@ -186,8 +186,8 @@ class RemBertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -216,7 +216,7 @@ class RemBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) return diff --git a/src/transformers/models/rembert/tokenization_rembert_fast.py b/src/transformers/models/rembert/tokenization_rembert_fast.py index 0d6dd1411a3..b4328543601 100644 --- a/src/transformers/models/rembert/tokenization_rembert_fast.py +++ b/src/transformers/models/rembert/tokenization_rembert_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -126,8 +126,8 @@ class RemBertTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RemBERT sequence has the following format: @@ -151,8 +151,8 @@ class RemBertTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -181,7 +181,7 @@ class RemBertTokenizerFast(PreTrainedTokenizerFast): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error("Vocabulary path ({}) should be a directory".format(save_directory)) return diff --git a/src/transformers/models/resnet/configuration_resnet.py b/src/transformers/models/resnet/configuration_resnet.py index fa7c64f8da9..f4dcc6e0c6b 100644 --- a/src/transformers/models/resnet/configuration_resnet.py +++ b/src/transformers/models/resnet/configuration_resnet.py @@ -43,9 +43,9 @@ class ResNetConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"bottleneck"`): The layer to use, it can be either `"basic"` (used for smaller models, like resnet-18 or resnet-34) or @@ -57,12 +57,12 @@ class ResNetConfig(BackboneConfigMixin, PretrainedConfig): If `True`, the first stage will downsample the inputs using a `stride` of 2. downsample_in_bottleneck (`bool`, *optional*, defaults to `False`): If `True`, the first conv 1x1 in ResNetBottleNeckLayer will downsample the inputs using a `stride` of 2. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/resnet/convert_resnet_to_pytorch.py b/src/transformers/models/resnet/convert_resnet_to_pytorch.py index 7552dbaefb9..7443b9cf502 100644 --- a/src/transformers/models/resnet/convert_resnet_to_pytorch.py +++ b/src/transformers/models/resnet/convert_resnet_to_pytorch.py @@ -19,7 +19,7 @@ import json from dataclasses import dataclass, field from functools import partial from pathlib import Path -from typing import List, Optional +from typing import Optional import timm import torch @@ -38,7 +38,7 @@ logger = logging.get_logger() @dataclass class Tracker: module: nn.Module - traced: List[nn.Module] = field(default_factory=list) + traced: list[nn.Module] = field(default_factory=list) handles: list = field(default_factory=list) def _forward_hook(self, m, inputs: Tensor, outputs: Tensor): @@ -64,8 +64,8 @@ class ModuleTransfer: src: nn.Module dest: nn.Module verbose: int = 0 - src_skip: List = field(default_factory=list) - dest_skip: List = field(default_factory=list) + src_skip: list = field(default_factory=list) + dest_skip: list = field(default_factory=list) def __call__(self, x: Tensor): """ diff --git a/src/transformers/models/resnet/modeling_flax_resnet.py b/src/transformers/models/resnet/modeling_flax_resnet.py index e6aba34cbee..a2a9418b7cf 100644 --- a/src/transformers/models/resnet/modeling_flax_resnet.py +++ b/src/transformers/models/resnet/modeling_flax_resnet.py @@ -14,7 +14,7 @@ # limitations under the License. from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -467,7 +467,7 @@ class FlaxResNetPreTrainedModel(FlaxPreTrainedModel): input_shape = (1, config.image_size, config.image_size, config.num_channels) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors pixel_values = jnp.zeros(input_shape, dtype=self.dtype) diff --git a/src/transformers/models/resnet/modeling_tf_resnet.py b/src/transformers/models/resnet/modeling_tf_resnet.py index 0f32c04f459..f7c415f97b0 100644 --- a/src/transformers/models/resnet/modeling_tf_resnet.py +++ b/src/transformers/models/resnet/modeling_tf_resnet.py @@ -14,7 +14,7 @@ # limitations under the License. """TensorFlow ResNet model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -419,7 +419,7 @@ class TFResNetMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -494,7 +494,7 @@ class TFResNetModel(TFResNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) @@ -557,7 +557,7 @@ class TFResNetForImageClassification(TFResNetPreTrainedModel, TFSequenceClassifi output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]: + ) -> Union[tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/roberta/modeling_flax_roberta.py b/src/transformers/models/roberta/modeling_flax_roberta.py index 3eb6d539291..3b46c0fa682 100644 --- a/src/transformers/models/roberta/modeling_flax_roberta.py +++ b/src/transformers/models/roberta/modeling_flax_roberta.py @@ -12,7 +12,7 @@ # 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. -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -734,7 +734,7 @@ class FlaxRobertaPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: RobertaConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -752,7 +752,7 @@ class FlaxRobertaPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.ones_like(input_ids) diff --git a/src/transformers/models/roberta/modeling_roberta.py b/src/transformers/models/roberta/modeling_roberta.py index 937931376d2..9381c8f9ab0 100644 --- a/src/transformers/models/roberta/modeling_roberta.py +++ b/src/transformers/models/roberta/modeling_roberta.py @@ -16,7 +16,7 @@ """PyTorch RoBERTa model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -175,9 +175,9 @@ class RobertaSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -285,9 +285,9 @@ class RobertaSdpaSelfAttention(RobertaSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -428,9 +428,9 @@ class RobertaAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -499,9 +499,9 @@ class RobertaLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -578,12 +578,12 @@ class RobertaEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -761,12 +761,12 @@ class RobertaModel(RobertaPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -932,13 +932,13 @@ class RobertaForCausalLM(RobertaPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1066,7 +1066,7 @@ class RobertaForMaskedLM(RobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1181,7 +1181,7 @@ class RobertaForSequenceClassification(RobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1275,7 +1275,7 @@ class RobertaForMultipleChoice(RobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1385,7 +1385,7 @@ class RobertaForTokenClassification(RobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1485,7 +1485,7 @@ class RobertaForQuestionAnswering(RobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: diff --git a/src/transformers/models/roberta/modeling_tf_roberta.py b/src/transformers/models/roberta/modeling_tf_roberta.py index 55361cdcc71..ef2800e2697 100644 --- a/src/transformers/models/roberta/modeling_tf_roberta.py +++ b/src/transformers/models/roberta/modeling_tf_roberta.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -244,10 +244,10 @@ class TFRobertaSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -382,10 +382,10 @@ class TFRobertaAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -499,10 +499,10 @@ class TFRobertaLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -595,13 +595,13 @@ class TFRobertaEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -709,13 +709,13 @@ class TFRobertaMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1005,13 +1005,13 @@ class TFRobertaModel(TFRobertaPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1023,7 +1023,7 @@ class TFRobertaModel(TFRobertaPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1161,7 +1161,7 @@ class TFRobertaForMaskedLM(TFRobertaPreTrainedModel, TFMaskedLanguageModelingLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1259,14 +1259,14 @@ class TFRobertaForCausalLM(TFRobertaPreTrainedModel, TFCausalLanguageModelingLos inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1278,7 +1278,7 @@ class TFRobertaForCausalLM(TFRobertaPreTrainedModel, TFCausalLanguageModelingLos - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1422,7 +1422,7 @@ class TFRobertaForSequenceClassification(TFRobertaPreTrainedModel, TFSequenceCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1511,7 +1511,7 @@ class TFRobertaForMultipleChoice(TFRobertaPreTrainedModel, TFMultipleChoiceLoss) return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1619,7 +1619,7 @@ class TFRobertaForTokenClassification(TFRobertaPreTrainedModel, TFTokenClassific return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1710,7 +1710,7 @@ class TFRobertaForQuestionAnswering(TFRobertaPreTrainedModel, TFQuestionAnswerin start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/roberta/tokenization_roberta.py b/src/transformers/models/roberta/tokenization_roberta.py index 6ec6cbbf986..c78d9831f77 100644 --- a/src/transformers/models/roberta/tokenization_roberta.py +++ b/src/transformers/models/roberta/tokenization_roberta.py @@ -17,7 +17,7 @@ import json import os from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import regex as re @@ -288,7 +288,7 @@ class RobertaTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -318,8 +318,8 @@ class RobertaTokenizer(PreTrainedTokenizer): return vocab_file, merge_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RoBERTa sequence has the following format: @@ -328,13 +328,13 @@ class RobertaTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -343,22 +343,22 @@ class RobertaTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -370,20 +370,20 @@ class RobertaTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/roberta/tokenization_roberta_fast.py b/src/transformers/models/roberta/tokenization_roberta_fast.py index cf288f4d8c7..d9ddcfc82d4 100644 --- a/src/transformers/models/roberta/tokenization_roberta_fast.py +++ b/src/transformers/models/roberta/tokenization_roberta_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization classes for RoBERTa.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import processors @@ -226,7 +226,7 @@ class RobertaTokenizerFast(PreTrainedTokenizerFast): return super()._encode_plus(*args, **kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) @@ -238,20 +238,20 @@ class RobertaTokenizerFast(PreTrainedTokenizerFast): return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ sep = [self.sep_token_id] cls = [self.cls_token_id] diff --git a/src/transformers/models/roberta_prelayernorm/modeling_flax_roberta_prelayernorm.py b/src/transformers/models/roberta_prelayernorm/modeling_flax_roberta_prelayernorm.py index ca90ce96fb4..f65dc07bb16 100644 --- a/src/transformers/models/roberta_prelayernorm/modeling_flax_roberta_prelayernorm.py +++ b/src/transformers/models/roberta_prelayernorm/modeling_flax_roberta_prelayernorm.py @@ -14,7 +14,7 @@ # limitations under the License. """Flax RoBERTa-PreLayerNorm model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -738,7 +738,7 @@ class FlaxRobertaPreLayerNormPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: RobertaPreLayerNormConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -756,7 +756,7 @@ class FlaxRobertaPreLayerNormPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.ones_like(input_ids) diff --git a/src/transformers/models/roberta_prelayernorm/modeling_roberta_prelayernorm.py b/src/transformers/models/roberta_prelayernorm/modeling_roberta_prelayernorm.py index a91491f2bd3..31d459e7d8b 100644 --- a/src/transformers/models/roberta_prelayernorm/modeling_roberta_prelayernorm.py +++ b/src/transformers/models/roberta_prelayernorm/modeling_roberta_prelayernorm.py @@ -16,7 +16,7 @@ """PyTorch RoBERTa-PreLayerNorm model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -174,9 +174,9 @@ class RobertaPreLayerNormSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -316,9 +316,9 @@ class RobertaPreLayerNormAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: hidden_states_pre_layer_norm = self.LayerNorm(hidden_states) self_outputs = self.self( hidden_states_pre_layer_norm, @@ -387,9 +387,9 @@ class RobertaPreLayerNormLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -466,12 +466,12 @@ class RobertaPreLayerNormEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -646,12 +646,12 @@ class RobertaPreLayerNormModel(RobertaPreLayerNormPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -800,13 +800,13 @@ class RobertaPreLayerNormForCausalLM(RobertaPreLayerNormPreTrainedModel, Generat encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -940,7 +940,7 @@ class RobertaPreLayerNormForMaskedLM(RobertaPreLayerNormPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1057,7 +1057,7 @@ class RobertaPreLayerNormForSequenceClassification(RobertaPreLayerNormPreTrained output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1152,7 +1152,7 @@ class RobertaPreLayerNormForMultipleChoice(RobertaPreLayerNormPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1263,7 +1263,7 @@ class RobertaPreLayerNormForTokenClassification(RobertaPreLayerNormPreTrainedMod output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1365,7 +1365,7 @@ class RobertaPreLayerNormForQuestionAnswering(RobertaPreLayerNormPreTrainedModel output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: diff --git a/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py b/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py index a11f6151c4d..4d218cf9c78 100644 --- a/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py +++ b/src/transformers/models/roberta_prelayernorm/modeling_tf_roberta_prelayernorm.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -245,10 +245,10 @@ class TFRobertaPreLayerNormSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -380,10 +380,10 @@ class TFRobertaPreLayerNormAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: hidden_states_pre_layer_norm = self.LayerNorm(inputs=input_tensor) self_outputs = self.self_attention( hidden_states=hidden_states_pre_layer_norm, @@ -500,10 +500,10 @@ class TFRobertaPreLayerNormLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -596,13 +596,13 @@ class TFRobertaPreLayerNormEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -707,13 +707,13 @@ class TFRobertaPreLayerNormMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1007,13 +1007,13 @@ class TFRobertaPreLayerNormModel(TFRobertaPreLayerNormPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1025,7 +1025,7 @@ class TFRobertaPreLayerNormModel(TFRobertaPreLayerNormPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1170,7 +1170,7 @@ class TFRobertaPreLayerNormForMaskedLM(TFRobertaPreLayerNormPreTrainedModel, TFM return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1275,14 +1275,14 @@ class TFRobertaPreLayerNormForCausalLM(TFRobertaPreLayerNormPreTrainedModel, TFC inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1294,7 +1294,7 @@ class TFRobertaPreLayerNormForCausalLM(TFRobertaPreLayerNormPreTrainedModel, TFC - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1442,7 +1442,7 @@ class TFRobertaPreLayerNormForSequenceClassification( return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1534,7 +1534,7 @@ class TFRobertaPreLayerNormForMultipleChoice(TFRobertaPreLayerNormPreTrainedMode return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1643,7 +1643,7 @@ class TFRobertaPreLayerNormForTokenClassification(TFRobertaPreLayerNormPreTraine return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1735,7 +1735,7 @@ class TFRobertaPreLayerNormForQuestionAnswering(TFRobertaPreLayerNormPreTrainedM start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/roc_bert/modeling_roc_bert.py b/src/transformers/models/roc_bert/modeling_roc_bert.py index f154de671d8..06721ae7719 100644 --- a/src/transformers/models/roc_bert/modeling_roc_bert.py +++ b/src/transformers/models/roc_bert/modeling_roc_bert.py @@ -16,7 +16,7 @@ import math import os -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -289,9 +289,9 @@ class RoCBertSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -439,9 +439,9 @@ class RoCBertAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -510,9 +510,9 @@ class RoCBertLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -589,12 +589,12 @@ class RoCBertEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -836,12 +836,12 @@ class RoCBertModel(RoCBertPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1006,7 +1006,7 @@ class RoCBertForPreTraining(RoCBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1211,7 +1211,7 @@ class RoCBertForMaskedLM(RoCBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1361,14 +1361,14 @@ class RoCBertForCausalLM(RoCBertPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.Tensor]] = None, + past_key_values: Optional[list[torch.Tensor]] = None, labels: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1541,7 +1541,7 @@ class RoCBertForSequenceClassification(RoCBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1648,7 +1648,7 @@ class RoCBertForMultipleChoice(RoCBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1781,7 +1781,7 @@ class RoCBertForTokenClassification(RoCBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. @@ -1867,7 +1867,7 @@ class RoCBertForQuestionAnswering(RoCBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" input_shape_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the shape vocabulary. diff --git a/src/transformers/models/roc_bert/tokenization_roc_bert.py b/src/transformers/models/roc_bert/tokenization_roc_bert.py index bea1769d0c6..2691e620a2e 100644 --- a/src/transformers/models/roc_bert/tokenization_roc_bert.py +++ b/src/transformers/models/roc_bert/tokenization_roc_bert.py @@ -19,7 +19,7 @@ import itertools import json import os import unicodedata -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...tokenization_utils_base import ( @@ -298,12 +298,12 @@ class RoCBertTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def prepare_for_model( self, - ids: List[int], - shape_ids: List[int], - pronunciation_ids: List[int], - pair_ids: Optional[List[int]] = None, - pair_shape_ids: Optional[List[int]] = None, - pair_pronunciation_ids: Optional[List[int]] = None, + ids: list[int], + shape_ids: list[int], + pronunciation_ids: list[int], + pair_ids: Optional[list[int]] = None, + pair_shape_ids: Optional[list[int]] = None, + pair_pronunciation_ids: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -480,7 +480,7 @@ class RoCBertTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -543,12 +543,12 @@ class RoCBertTokenizer(PreTrainedTokenizer): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], - List[PreTokenizedInputPair], - List[EncodedInput], - List[EncodedInputPair], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], + list[PreTokenizedInputPair], + list[EncodedInput], + list[EncodedInputPair], ], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -649,9 +649,9 @@ class RoCBertTokenizer(PreTrainedTokenizer): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def _batch_prepare_for_model( self, - batch_ids_pairs: List[Union[PreTokenizedInputPair, Tuple[List[int], None]]], - batch_shape_ids_pairs: List[Union[PreTokenizedInputPair, Tuple[List[int], None]]], - batch_pronunciation_ids_pairs: List[Union[PreTokenizedInputPair, Tuple[List[int], None]]], + batch_ids_pairs: list[Union[PreTokenizedInputPair, tuple[list[int], None]]], + batch_shape_ids_pairs: list[Union[PreTokenizedInputPair, tuple[list[int], None]]], + batch_pronunciation_ids_pairs: list[Union[PreTokenizedInputPair, tuple[list[int], None]]], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -733,7 +733,7 @@ class RoCBertTokenizer(PreTrainedTokenizer): """Converts a token (str) in an shape_id using the shape vocab.""" return self.word_shape.get(token, self.word_shape.get(self.unk_token)) - def convert_tokens_to_shape_ids(self, tokens: Union[str, List[str]]) -> Union[int, List[int]]: + def convert_tokens_to_shape_ids(self, tokens: Union[str, list[str]]) -> Union[int, list[int]]: if tokens is None: return None @@ -746,7 +746,7 @@ class RoCBertTokenizer(PreTrainedTokenizer): """Converts a token (str) in an shape_id using the shape vocab.""" return self.word_pronunciation.get(token, self.word_pronunciation.get(self.unk_token)) - def convert_tokens_to_pronunciation_ids(self, tokens: Union[str, List[str]]) -> Union[int, List[int]]: + def convert_tokens_to_pronunciation_ids(self, tokens: Union[str, list[str]]) -> Union[int, list[int]]: if tokens is None: return None @@ -768,11 +768,11 @@ class RoCBertTokenizer(PreTrainedTokenizer): def build_inputs_with_special_tokens( self, - token_ids_0: List[int], - token_ids_1: Optional[List[int]] = None, + token_ids_0: list[int], + token_ids_1: Optional[list[int]] = None, cls_token_id: Optional[int] = None, sep_token_id: Optional[int] = None, - ) -> List[int]: + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format: @@ -797,8 +797,8 @@ class RoCBertTokenizer(PreTrainedTokenizer): # Copied from transformers.models.bert.tokenization_bert.BertTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -824,7 +824,7 @@ class RoCBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str, str, str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str, str, str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/roformer/modeling_flax_roformer.py b/src/transformers/models/roformer/modeling_flax_roformer.py index c320407813e..de78eb4787c 100644 --- a/src/transformers/models/roformer/modeling_flax_roformer.py +++ b/src/transformers/models/roformer/modeling_flax_roformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Flax RoFormer model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -608,7 +608,7 @@ class FlaxRoFormerPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: RoFormerConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -617,7 +617,7 @@ class FlaxRoFormerPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.zeros_like(input_ids) diff --git a/src/transformers/models/roformer/modeling_roformer.py b/src/transformers/models/roformer/modeling_roformer.py index a675a397384..3f9b2875c20 100644 --- a/src/transformers/models/roformer/modeling_roformer.py +++ b/src/transformers/models/roformer/modeling_roformer.py @@ -16,7 +16,7 @@ import math import os -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -844,12 +844,12 @@ class RoFormerModel(RoFormerPreTrainedModel): inputs_embeds: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutputWithPastAndCrossAttentions, Tuple[torch.Tensor]]: + ) -> Union[BaseModelOutputWithPastAndCrossAttentions, tuple[torch.Tensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -976,7 +976,7 @@ class RoFormerForMaskedLM(RoFormerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.Tensor]]: + ) -> Union[MaskedLMOutput, tuple[torch.Tensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1070,14 +1070,14 @@ class RoFormerForCausalLM(RoFormerPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[CausalLMOutputWithCrossAttentions, Tuple[torch.Tensor]]: + ) -> Union[CausalLMOutputWithCrossAttentions, tuple[torch.Tensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1201,7 +1201,7 @@ class RoFormerForSequenceClassification(RoFormerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.Tensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.Tensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1282,7 +1282,7 @@ class RoFormerForMultipleChoice(RoFormerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MultipleChoiceModelOutput, Tuple[torch.Tensor]]: + ) -> Union[MultipleChoiceModelOutput, tuple[torch.Tensor]]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1380,7 +1380,7 @@ class RoFormerForTokenClassification(RoFormerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple[torch.Tensor]]: + ) -> Union[TokenClassifierOutput, tuple[torch.Tensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1447,7 +1447,7 @@ class RoFormerForQuestionAnswering(RoFormerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[QuestionAnsweringModelOutput, Tuple[torch.Tensor]]: + ) -> Union[QuestionAnsweringModelOutput, tuple[torch.Tensor]]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.roformer( diff --git a/src/transformers/models/roformer/modeling_tf_roformer.py b/src/transformers/models/roformer/modeling_tf_roformer.py index 6f697ad14e0..f7980145735 100644 --- a/src/transformers/models/roformer/modeling_tf_roformer.py +++ b/src/transformers/models/roformer/modeling_tf_roformer.py @@ -17,7 +17,7 @@ from __future__ import annotations import math -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -230,7 +230,7 @@ class TFRoFormerSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) mixed_key_layer = self.key(inputs=hidden_states) @@ -367,7 +367,7 @@ class TFRoFormerAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -472,7 +472,7 @@ class TFRoFormerLayer(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( input_tensor=hidden_states, attention_mask=attention_mask, @@ -524,7 +524,7 @@ class TFRoFormerEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -639,7 +639,7 @@ class TFRoFormerLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -719,7 +719,7 @@ class TFRoFormerMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -861,7 +861,7 @@ ROFORMER_START_DOCSTRING = r""" ROFORMER_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -938,7 +938,7 @@ class TFRoFormerModel(TFRoFormerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: outputs = self.roformer( input_ids=input_ids, attention_mask=attention_mask, @@ -998,7 +998,7 @@ class TFRoFormerForMaskedLM(TFRoFormerPreTrainedModel, TFMaskedLanguageModelingL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1077,7 +1077,7 @@ class TFRoFormerForCausalLM(TFRoFormerPreTrainedModel, TFCausalLanguageModelingL return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., @@ -1203,7 +1203,7 @@ class TFRoFormerForSequenceClassification(TFRoFormerPreTrainedModel, TFSequenceC return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1287,7 +1287,7 @@ class TFRoFormerForMultipleChoice(TFRoFormerPreTrainedModel, TFMultipleChoiceLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1394,7 +1394,7 @@ class TFRoFormerForTokenClassification(TFRoFormerPreTrainedModel, TFTokenClassif return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1477,7 +1477,7 @@ class TFRoFormerForQuestionAnswering(TFRoFormerPreTrainedModel, TFQuestionAnswer start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/roformer/tokenization_roformer.py b/src/transformers/models/roformer/tokenization_roformer.py index f1ee807c8d7..df79d02c7c0 100644 --- a/src/transformers/models/roformer/tokenization_roformer.py +++ b/src/transformers/models/roformer/tokenization_roformer.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -435,8 +435,8 @@ class RoFormerTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A RoFormer sequence has the following format: @@ -460,8 +460,8 @@ class RoFormerTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -487,7 +487,7 @@ class RoFormerTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/roformer/tokenization_roformer_fast.py b/src/transformers/models/roformer/tokenization_roformer_fast.py index f3ed8470f2d..c9704aa7190 100644 --- a/src/transformers/models/roformer/tokenization_roformer_fast.py +++ b/src/transformers/models/roformer/tokenization_roformer_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for RoFormer.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers from tokenizers.pre_tokenizers import BertPreTokenizer, PreTokenizer @@ -132,7 +132,7 @@ class RoFormerTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/roformer/tokenization_utils.py b/src/transformers/models/roformer/tokenization_utils.py index 4c9cf6cb0a0..1fe681500d1 100644 --- a/src/transformers/models/roformer/tokenization_utils.py +++ b/src/transformers/models/roformer/tokenization_utils.py @@ -14,8 +14,6 @@ # limitations under the License. """Tokenization utils for RoFormer.""" -from typing import List - from tokenizers import NormalizedString, PreTokenizedString, normalizers @@ -37,7 +35,7 @@ class JiebaPreTokenizer: ) self.jieba = rjieba - def jieba_split(self, i: int, normalized_string: NormalizedString) -> List[NormalizedString]: + def jieba_split(self, i: int, normalized_string: NormalizedString) -> list[NormalizedString]: splits = [] # this code slice normalized_string is too slow (6s) but test_alignment_methods can pass diff --git a/src/transformers/models/rt_detr/configuration_rt_detr.py b/src/transformers/models/rt_detr/configuration_rt_detr.py index 7b12f0028c8..1019c346500 100644 --- a/src/transformers/models/rt_detr/configuration_rt_detr.py +++ b/src/transformers/models/rt_detr/configuration_rt_detr.py @@ -64,7 +64,7 @@ class RTDetrConfig(PretrainedConfig): Dimension of the layers in hybrid encoder. encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`): Multi level features input for encoder. - feat_strides (`List[int]`, *optional*, defaults to `[8, 16, 32]`): + feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`): Strides used in each feature map. encoder_layers (`int`, *optional*, defaults to 1): Total of layers to be used by the encoder. @@ -76,7 +76,7 @@ class RTDetrConfig(PretrainedConfig): The ratio for all dropout layers. activation_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for activations inside the fully connected layer. - encode_proj_layers (`List[int]`, *optional*, defaults to `[2]`): + encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`): Indexes of the projected layers to be used in the encoder. positional_encoding_temperature (`int`, *optional*, defaults to 10000): The temperature parameter used to create the positional encodings. @@ -86,7 +86,7 @@ class RTDetrConfig(PretrainedConfig): activation_function (`str`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the general layer. If string, `"gelu"`, `"relu"`, `"silu"` and `"gelu_new"` are supported. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to computes the effective height and width of the position embeddings after taking into account the stride. normalize_before (`bool`, *optional*, defaults to `False`): @@ -123,7 +123,7 @@ class RTDetrConfig(PretrainedConfig): Scale or magnitude of noise to be added to the bounding boxes. learn_initial_query (`bool`, *optional*, defaults to `False`): Indicates whether the initial query embeddings for the decoder should be learned during training - anchor_image_size (`Tuple[int, int]`, *optional*): + anchor_image_size (`tuple[int, int]`, *optional*): Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied. disable_custom_kernels (`bool`, *optional*, defaults to `True`): Whether to disable custom kernels. diff --git a/src/transformers/models/rt_detr/configuration_rt_detr_resnet.py b/src/transformers/models/rt_detr/configuration_rt_detr_resnet.py index 35e74cd2f18..73b9517ab14 100644 --- a/src/transformers/models/rt_detr/configuration_rt_detr_resnet.py +++ b/src/transformers/models/rt_detr/configuration_rt_detr_resnet.py @@ -37,9 +37,9 @@ class RTDetrResNetConfig(BackboneConfigMixin, PretrainedConfig): The number of input channels. embedding_size (`int`, *optional*, defaults to 64): Dimensionality (hidden size) for the embedding layer. - hidden_sizes (`List[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[256, 512, 1024, 2048]`): Dimensionality (hidden size) at each stage. - depths (`List[int]`, *optional*, defaults to `[3, 4, 6, 3]`): + depths (`list[int]`, *optional*, defaults to `[3, 4, 6, 3]`): Depth (number of layers) for each stage. layer_type (`str`, *optional*, defaults to `"bottleneck"`): The layer to use, it can be either `"basic"` (used for smaller models, like resnet-18 or resnet-34) or @@ -51,12 +51,12 @@ class RTDetrResNetConfig(BackboneConfigMixin, PretrainedConfig): If `True`, the first stage will downsample the inputs using a `stride` of 2. downsample_in_bottleneck (`bool`, *optional*, defaults to `False`): If `True`, the first conv 1x1 in ResNetBottleNeckLayer will downsample the inputs using a `stride` of 2. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/rt_detr/image_processing_rt_detr.py b/src/transformers/models/rt_detr/image_processing_rt_detr.py index 714ae9467f0..9d9548e69e5 100644 --- a/src/transformers/models/rt_detr/image_processing_rt_detr.py +++ b/src/transformers/models/rt_detr/image_processing_rt_detr.py @@ -16,7 +16,7 @@ import pathlib from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -72,12 +72,12 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION,) # Copied from transformers.models.detr.image_processing_detr.get_size_with_aspect_ratio -def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]: +def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -114,10 +114,10 @@ def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, in # Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -126,7 +126,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -146,7 +146,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -214,7 +214,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.normalize_annotation -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -229,7 +229,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -238,8 +238,8 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -257,7 +257,7 @@ def get_max_height_width( # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -265,7 +265,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -331,9 +331,9 @@ def prepare_coco_detection_annotation( # Copied from transformers.models.detr.image_processing_detr.resize_annotation def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -341,11 +341,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -391,7 +391,7 @@ class RTDetrImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 640, "width": 640}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 640, "width": 640}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -414,10 +414,10 @@ class RTDetrImageProcessor(BaseImageProcessor): `preprocess` method. do_normalize (`bool`, *optional*, defaults to `False`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_annotations (`bool`, *optional*, defaults to `True`): @@ -429,7 +429,7 @@ class RTDetrImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -441,16 +441,16 @@ class RTDetrImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = False, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: bool = True, do_pad: bool = False, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: size = size if size is not None else {"height": 640, "width": 640} @@ -476,12 +476,12 @@ class RTDetrImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into RTDETR model. """ @@ -500,7 +500,7 @@ class RTDetrImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -513,7 +513,7 @@ class RTDetrImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -572,7 +572,7 @@ class RTDetrImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -609,7 +609,7 @@ class RTDetrImageProcessor(BaseImageProcessor): return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -619,12 +619,12 @@ class RTDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor._update_annotation_for_padded_image def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -664,8 +664,8 @@ class RTDetrImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -697,24 +697,24 @@ class RTDetrImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.pad def pad( self, - images: List[np.ndarray], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + images: list[np.ndarray], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width in the batch and optionally returns their corresponding pixel mask. Args: - images (List[`np.ndarray`]): + images (list[`np.ndarray`]): Images to pad. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): Annotations to transform according to the padding that is applied to the images. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -735,7 +735,7 @@ class RTDetrImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -784,24 +784,24 @@ class RTDetrImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, do_convert_annotations: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Preprocess an image or a batch of images so that it can be used by the model. @@ -810,15 +810,15 @@ class RTDetrImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -827,7 +827,7 @@ class RTDetrImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -849,9 +849,9 @@ class RTDetrImageProcessor(BaseImageProcessor): Whether to convert the annotations to the format expected by the model. Converts the bounding boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)` and in relative coordinates. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_pad (`bool`, *optional*, defaults to self.do_pad): Whether to pad the image. If `True`, padding will be applied to the bottom and right of @@ -872,7 +872,7 @@ class RTDetrImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1031,7 +1031,7 @@ class RTDetrImageProcessor(BaseImageProcessor): self, outputs, threshold: float = 0.5, - target_sizes: Union[TensorType, List[Tuple]] = None, + target_sizes: Union[TensorType, list[tuple]] = None, use_focal_loss: bool = True, ): """ @@ -1043,15 +1043,15 @@ class RTDetrImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.5): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. use_focal_loss (`bool` defaults to `True`): Variable informing if the focal loss was used to predict the outputs. If `True`, a sigmoid is applied to compute the scores of each detection, otherwise, a softmax function is used. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ requires_backends(self, ["torch"]) @@ -1063,7 +1063,7 @@ class RTDetrImageProcessor(BaseImageProcessor): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h, img_w = torch.as_tensor(target_sizes).unbind(1) else: img_h, img_w = target_sizes.unbind(1) diff --git a/src/transformers/models/rt_detr/image_processing_rt_detr_fast.py b/src/transformers/models/rt_detr/image_processing_rt_detr_fast.py index 2742e228bb1..b65fc3e4a05 100644 --- a/src/transformers/models/rt_detr/image_processing_rt_detr_fast.py +++ b/src/transformers/models/rt_detr/image_processing_rt_detr_fast.py @@ -5,7 +5,7 @@ # modular_rt_detr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import pathlib -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -64,7 +64,7 @@ class RTDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -75,7 +75,7 @@ class RTDetrFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -170,12 +170,12 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into RT_DETR model. """ @@ -246,9 +246,9 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -256,11 +256,11 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -297,7 +297,7 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -314,12 +314,12 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -350,8 +350,8 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -381,20 +381,20 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[RTDetrFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -404,8 +404,8 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -415,10 +415,10 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -512,7 +512,7 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): self, outputs, threshold: float = 0.5, - target_sizes: Union[TensorType, List[Tuple]] = None, + target_sizes: Union[TensorType, list[tuple]] = None, use_focal_loss: bool = True, ): """ @@ -524,15 +524,15 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.5): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. use_focal_loss (`bool` defaults to `True`): Variable informing if the focal loss was used to predict the outputs. If `True`, a sigmoid is applied to compute the scores of each detection, otherwise, a softmax function is used. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ requires_backends(self, ["torch"]) @@ -544,7 +544,7 @@ class RTDetrImageProcessorFast(BaseImageProcessorFast): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h, img_w = torch.as_tensor(target_sizes).unbind(1) else: img_h, img_w = target_sizes.unbind(1) diff --git a/src/transformers/models/rt_detr/modeling_rt_detr.py b/src/transformers/models/rt_detr/modeling_rt_detr.py index 5301187408a..41669d9cf80 100644 --- a/src/transformers/models/rt_detr/modeling_rt_detr.py +++ b/src/transformers/models/rt_detr/modeling_rt_detr.py @@ -18,7 +18,7 @@ import math import warnings from dataclasses import dataclass from functools import partial -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -53,7 +53,7 @@ class MultiScaleDeformableAttention(nn.Module): self, value: Tensor, value_spatial_shapes: Tensor, - value_spatial_shapes_list: List[Tuple], + value_spatial_shapes_list: list[tuple], level_start_index: Tensor, sampling_locations: Tensor, attention_weights: Tensor, @@ -143,9 +143,9 @@ class RTDetrDecoderOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -208,18 +208,18 @@ class RTDetrModelOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None init_reference_points: Optional[torch.FloatTensor] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None @dataclass @@ -296,28 +296,28 @@ class RTDetrObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_logits: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - init_reference_points: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + init_reference_points: Optional[tuple[torch.FloatTensor]] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None def _get_clones(partial_module, N): @@ -410,7 +410,7 @@ def get_contrastive_denoising_training_group( Creates a contrastive denoising training group using ground-truth samples. It adds noise to labels and boxes. Args: - targets (`List[dict]`): + targets (`list[dict]`): The target objects, each containing 'class_labels' and 'boxes' for objects in an image. num_classes (`int`): Total number of classes in the dataset. @@ -858,7 +858,7 @@ class RTDetrMultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -1632,11 +1632,11 @@ class RTDetrModel(RTDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], RTDetrModelOutput]: + ) -> Union[tuple[torch.FloatTensor], RTDetrModelOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1644,7 +1644,7 @@ class RTDetrModel(RTDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -1900,12 +1900,12 @@ class RTDetrForObjectDetection(RTDetrPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **loss_kwargs, - ) -> Union[Tuple[torch.FloatTensor], RTDetrObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], RTDetrObjectDetectionOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1913,7 +1913,7 @@ class RTDetrForObjectDetection(RTDetrPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/rt_detr/modular_rt_detr.py b/src/transformers/models/rt_detr/modular_rt_detr.py index b987603192c..641519fafea 100644 --- a/src/transformers/models/rt_detr/modular_rt_detr.py +++ b/src/transformers/models/rt_detr/modular_rt_detr.py @@ -1,5 +1,5 @@ import pathlib -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from transformers.models.detr.image_processing_detr_fast import DetrFastImageProcessorKwargs, DetrImageProcessorFast @@ -133,7 +133,7 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[RTDetrFastImageProcessorKwargs], ) -> BatchFeature: @@ -142,12 +142,12 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: format = format if format is not None else self.format if format == AnnotationFormat.COCO_DETECTION: @@ -161,8 +161,8 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -172,10 +172,10 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -269,7 +269,7 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): self, outputs, threshold: float = 0.5, - target_sizes: Union[TensorType, List[Tuple]] = None, + target_sizes: Union[TensorType, list[tuple]] = None, use_focal_loss: bool = True, ): """ @@ -281,15 +281,15 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*, defaults to 0.5): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. use_focal_loss (`bool` defaults to `True`): Variable informing if the focal loss was used to predict the outputs. If `True`, a sigmoid is applied to compute the scores of each detection, otherwise, a softmax function is used. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ requires_backends(self, ["torch"]) @@ -301,7 +301,7 @@ class RTDetrImageProcessorFast(DetrImageProcessorFast, BaseImageProcessorFast): raise ValueError( "Make sure that you pass in as many target sizes as the batch dimension of the logits" ) - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h, img_w = torch.as_tensor(target_sizes).unbind(1) else: img_h, img_w = target_sizes.unbind(1) diff --git a/src/transformers/models/rt_detr_v2/configuration_rt_detr_v2.py b/src/transformers/models/rt_detr_v2/configuration_rt_detr_v2.py index 1a16e226c89..602e63fabe8 100644 --- a/src/transformers/models/rt_detr_v2/configuration_rt_detr_v2.py +++ b/src/transformers/models/rt_detr_v2/configuration_rt_detr_v2.py @@ -69,7 +69,7 @@ class RTDetrV2Config(PretrainedConfig): Dimension of the layers in hybrid encoder. encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`): Multi level features input for encoder. - feat_strides (`List[int]`, *optional*, defaults to `[8, 16, 32]`): + feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`): Strides used in each feature map. encoder_layers (`int`, *optional*, defaults to 1): Total of layers to be used by the encoder. @@ -81,7 +81,7 @@ class RTDetrV2Config(PretrainedConfig): The ratio for all dropout layers. activation_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for activations inside the fully connected layer. - encode_proj_layers (`List[int]`, *optional*, defaults to `[2]`): + encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`): Indexes of the projected layers to be used in the encoder. positional_encoding_temperature (`int`, *optional*, defaults to 10000): The temperature parameter used to create the positional encodings. @@ -91,7 +91,7 @@ class RTDetrV2Config(PretrainedConfig): activation_function (`str`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the general layer. If string, `"gelu"`, `"relu"`, `"silu"` and `"gelu_new"` are supported. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to compute the effective height and width of the position embeddings after taking into account the stride. normalize_before (`bool`, *optional*, defaults to `False`): @@ -128,7 +128,7 @@ class RTDetrV2Config(PretrainedConfig): Scale or magnitude of noise to be added to the bounding boxes. learn_initial_query (`bool`, *optional*, defaults to `False`): Indicates whether the initial query embeddings for the decoder should be learned during training - anchor_image_size (`Tuple[int, int]`, *optional*): + anchor_image_size (`tuple[int, int]`, *optional*): Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied. with_box_refine (`bool`, *optional*, defaults to `True`): Whether to apply iterative bounding box refinement, where each decoder layer refines the bounding boxes diff --git a/src/transformers/models/rt_detr_v2/modeling_rt_detr_v2.py b/src/transformers/models/rt_detr_v2/modeling_rt_detr_v2.py index b076cfcc98d..b7ba9a11da5 100644 --- a/src/transformers/models/rt_detr_v2/modeling_rt_detr_v2.py +++ b/src/transformers/models/rt_detr_v2/modeling_rt_detr_v2.py @@ -22,7 +22,7 @@ import math import warnings from dataclasses import dataclass from functools import partial -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional as F @@ -43,7 +43,7 @@ def multi_scale_deformable_attention_v2( value_spatial_shapes: Tensor, sampling_locations: Tensor, attention_weights: Tensor, - num_points_list: List[int], + num_points_list: list[int], method="default", ) -> Tensor: batch_size, _, num_heads, hidden_dim = value.shape @@ -265,7 +265,7 @@ class RTDetrV2MultiheadAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" batch_size, target_len, embed_dim = hidden_states.size() @@ -486,9 +486,9 @@ class RTDetrV2DecoderOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -551,18 +551,18 @@ class RTDetrV2ModelOutput(ModelOutput): intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None init_reference_points: Optional[torch.FloatTensor] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None @dataclass @@ -639,28 +639,28 @@ class RTDetrV2ObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None intermediate_hidden_states: Optional[torch.FloatTensor] = None intermediate_logits: Optional[torch.FloatTensor] = None intermediate_reference_points: Optional[torch.FloatTensor] = None intermediate_predicted_corners: Optional[torch.FloatTensor] = None initial_reference_points: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - init_reference_points: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None + init_reference_points: Optional[tuple[torch.FloatTensor]] = None enc_topk_logits: Optional[torch.FloatTensor] = None enc_topk_bboxes: Optional[torch.FloatTensor] = None enc_outputs_class: Optional[torch.FloatTensor] = None enc_outputs_coord_logits: Optional[torch.FloatTensor] = None - denoising_meta_values: Optional[Dict] = None + denoising_meta_values: Optional[dict] = None class RTDetrV2FrozenBatchNorm2d(nn.Module): @@ -1142,7 +1142,7 @@ def get_contrastive_denoising_training_group( Creates a contrastive denoising training group using ground-truth samples. It adds noise to labels and boxes. Args: - targets (`List[dict]`): + targets (`list[dict]`): The target objects, each containing 'class_labels' and 'boxes' for objects in an image. num_classes (`int`): Total number of classes in the dataset. @@ -1610,11 +1610,11 @@ class RTDetrV2Model(RTDetrV2PreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], RTDetrV2ModelOutput]: + ) -> Union[tuple[torch.FloatTensor], RTDetrV2ModelOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1622,7 +1622,7 @@ class RTDetrV2Model(RTDetrV2PreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes @@ -1893,12 +1893,12 @@ class RTDetrV2ForObjectDetection(RTDetrV2PreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **loss_kwargs, - ) -> Union[Tuple[torch.FloatTensor], RTDetrV2ObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], RTDetrV2ObjectDetectionOutput]: r""" inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you @@ -1906,7 +1906,7 @@ class RTDetrV2ForObjectDetection(RTDetrV2PreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/rt_detr_v2/modular_rt_detr_v2.py b/src/transformers/models/rt_detr_v2/modular_rt_detr_v2.py index dfd2a461319..0bcff45cfa1 100644 --- a/src/transformers/models/rt_detr_v2/modular_rt_detr_v2.py +++ b/src/transformers/models/rt_detr_v2/modular_rt_detr_v2.py @@ -14,7 +14,7 @@ # limitations under the License. import warnings from functools import partial -from typing import List, Optional +from typing import Optional import torch import torch.nn.functional as F @@ -80,7 +80,7 @@ class RTDetrV2Config(PretrainedConfig): Dimension of the layers in hybrid encoder. encoder_in_channels (`list`, *optional*, defaults to `[512, 1024, 2048]`): Multi level features input for encoder. - feat_strides (`List[int]`, *optional*, defaults to `[8, 16, 32]`): + feat_strides (`list[int]`, *optional*, defaults to `[8, 16, 32]`): Strides used in each feature map. encoder_layers (`int`, *optional*, defaults to 1): Total of layers to be used by the encoder. @@ -92,7 +92,7 @@ class RTDetrV2Config(PretrainedConfig): The ratio for all dropout layers. activation_dropout (`float`, *optional*, defaults to 0.0): The dropout ratio for activations inside the fully connected layer. - encode_proj_layers (`List[int]`, *optional*, defaults to `[2]`): + encode_proj_layers (`list[int]`, *optional*, defaults to `[2]`): Indexes of the projected layers to be used in the encoder. positional_encoding_temperature (`int`, *optional*, defaults to 10000): The temperature parameter used to create the positional encodings. @@ -102,7 +102,7 @@ class RTDetrV2Config(PretrainedConfig): activation_function (`str`, *optional*, defaults to `"silu"`): The non-linear activation function (function or string) in the general layer. If string, `"gelu"`, `"relu"`, `"silu"` and `"gelu_new"` are supported. - eval_size (`Tuple[int, int]`, *optional*): + eval_size (`tuple[int, int]`, *optional*): Height and width used to compute the effective height and width of the position embeddings after taking into account the stride. normalize_before (`bool`, *optional*, defaults to `False`): @@ -139,7 +139,7 @@ class RTDetrV2Config(PretrainedConfig): Scale or magnitude of noise to be added to the bounding boxes. learn_initial_query (`bool`, *optional*, defaults to `False`): Indicates whether the initial query embeddings for the decoder should be learned during training - anchor_image_size (`Tuple[int, int]`, *optional*): + anchor_image_size (`tuple[int, int]`, *optional*): Height and width of the input image used during evaluation to generate the bounding box anchors. If None, automatic generate anchor is applied. with_box_refine (`bool`, *optional*, defaults to `True`): Whether to apply iterative bounding box refinement, where each decoder layer refines the bounding boxes @@ -392,7 +392,7 @@ def multi_scale_deformable_attention_v2( value_spatial_shapes: Tensor, sampling_locations: Tensor, attention_weights: Tensor, - num_points_list: List[int], + num_points_list: list[int], method="default", ) -> Tensor: batch_size, _, num_heads, hidden_dim = value.shape diff --git a/src/transformers/models/rwkv/modeling_rwkv.py b/src/transformers/models/rwkv/modeling_rwkv.py index 6e71308e17a..b7362000b57 100644 --- a/src/transformers/models/rwkv/modeling_rwkv.py +++ b/src/transformers/models/rwkv/modeling_rwkv.py @@ -18,7 +18,7 @@ import math from dataclasses import dataclass from pathlib import Path -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -470,9 +470,9 @@ class RwkvOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - state: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + state: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -503,9 +503,9 @@ class RwkvCausalLMOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - state: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + state: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring @@ -536,12 +536,12 @@ class RwkvModel(RwkvPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.LongTensor] = None, # noqa inputs_embeds: Optional[torch.FloatTensor] = None, - state: Optional[List[torch.FloatTensor]] = None, + state: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, RwkvOutput]: + ) -> Union[tuple, RwkvOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else @@ -733,14 +733,14 @@ class RwkvForCausalLM(RwkvPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.LongTensor] = None, # noqa inputs_embeds: Optional[torch.FloatTensor] = None, - state: Optional[List[torch.FloatTensor]] = None, + state: Optional[list[torch.FloatTensor]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, RwkvCausalLMOutput]: + ) -> Union[tuple, RwkvCausalLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`): `input_ids_length` = `sequence_length` if `past_key_values` is `None` else diff --git a/src/transformers/models/sam/configuration_sam.py b/src/transformers/models/sam/configuration_sam.py index 0f4a2b0893a..11a3e421d42 100644 --- a/src/transformers/models/sam/configuration_sam.py +++ b/src/transformers/models/sam/configuration_sam.py @@ -176,7 +176,7 @@ class SamVisionConfig(PretrainedConfig): Whether to use relative position embedding. window_size (`int`, *optional*, defaults to 14): Window size for relative position. - global_attn_indexes (`List[int]`, *optional*, defaults to `[2, 5, 8, 11]`): + global_attn_indexes (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`): The indexes of the global attention layers. num_pos_feats (`int`, *optional*, defaults to 128): The dimensionality of the position embedding. diff --git a/src/transformers/models/sam/image_processing_sam.py b/src/transformers/models/sam/image_processing_sam.py index 3142d9d1981..e1150c7f0b2 100644 --- a/src/transformers/models/sam/image_processing_sam.py +++ b/src/transformers/models/sam/image_processing_sam.py @@ -17,7 +17,7 @@ import math from copy import deepcopy from itertools import product -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -92,11 +92,11 @@ class SamImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -118,14 +118,14 @@ class SamImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, - mask_size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, + mask_size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, pad_size: Optional[int] = None, mask_pad_size: Optional[int] = None, @@ -166,7 +166,7 @@ class SamImageProcessor(BaseImageProcessor): def pad_image( self, image: np.ndarray, - pad_size: Dict[str, int], + pad_size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -177,7 +177,7 @@ class SamImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to pad. - pad_size (`Dict[str, int]`): + pad_size (`dict[str, int]`): Size of the output image after padding. data_format (`str` or `ChannelDimension`, *optional*): The data format of the image. Can be either "channels_first" or "channels_last". If `None`, the @@ -200,7 +200,7 @@ class SamImageProcessor(BaseImageProcessor): ) return padded_image - def _get_preprocess_shape(self, old_shape: Tuple[int, int], longest_edge: int): + def _get_preprocess_shape(self, old_shape: tuple[int, int], longest_edge: int): """ Compute the output size given input size and target long side length. """ @@ -214,7 +214,7 @@ class SamImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -226,7 +226,7 @@ class SamImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"longest_edge": int}` specifying the size of the output image. The longest edge of the image will be resized to the specified size, while the other edge will be resized to maintain the aspect ratio. @@ -266,13 +266,13 @@ class SamImageProcessor(BaseImageProcessor): do_resize: bool, do_rescale: bool, do_normalize: bool, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, rescale_factor: Optional[float] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_resize: @@ -294,19 +294,19 @@ class SamImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, do_convert_rgb: Optional[bool] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Tuple[np.ndarray, Tuple[int, int], Tuple[int, int]]: + ) -> tuple[np.ndarray, tuple[int, int], tuple[int, int]]: # PIL RGBA images are converted to RGB if do_convert_rgb: image = convert_to_rgb(image) @@ -349,9 +349,9 @@ class SamImageProcessor(BaseImageProcessor): self, segmentation_map: ImageInput, do_resize: Optional[bool] = None, - mask_size: Optional[Dict[str, int]] = None, + mask_size: Optional[dict[str, int]] = None, do_pad: Optional[bool] = None, - mask_pad_size: Optional[Dict[str, int]] = None, + mask_pad_size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: segmentation_map = to_numpy_array(segmentation_map) @@ -393,17 +393,17 @@ class SamImageProcessor(BaseImageProcessor): images: ImageInput, segmentation_maps: Optional[ImageInput] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, - mask_size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, + mask_size: Optional[dict[str, int]] = None, resample: Optional["PILImageResampling"] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, - pad_size: Optional[Dict[str, int]] = None, - mask_pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, + mask_pad_size: Optional[dict[str, int]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -420,10 +420,10 @@ class SamImageProcessor(BaseImageProcessor): Segmentation map to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The longest edge of the image is resized to `size["longest_edge"]` whilst preserving the aspect ratio. - mask_size (`Dict[str, int]`, *optional*, defaults to `self.mask_size`): + mask_size (`dict[str, int]`, *optional*, defaults to `self.mask_size`): Controls the size of the segmentation map after `resize`. The longest edge of the image is resized to `size["longest_edge"]` whilst preserving the aspect ratio. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): @@ -434,16 +434,16 @@ class SamImageProcessor(BaseImageProcessor): Rescale factor to apply to the image pixel values. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): Whether to pad the image. - pad_size (`Dict[str, int]`, *optional*, defaults to `self.pad_size`): + pad_size (`dict[str, int]`, *optional*, defaults to `self.pad_size`): Controls the size of the padding applied to the image. The image is padded to `pad_size["height"]` and `pad_size["width"]` if `do_pad` is set to `True`. - mask_pad_size (`Dict[str, int]`, *optional*, defaults to `self.mask_pad_size`): + mask_pad_size (`dict[str, int]`, *optional*, defaults to `self.mask_pad_size`): Controls the size of the padding applied to the segmentation map. The image is padded to `mask_pad_size["height"]` and `mask_pad_size["width"]` if `do_pad` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): @@ -585,12 +585,12 @@ class SamImageProcessor(BaseImageProcessor): Remove padding and upscale masks to the original image size. Args: - masks (`Union[List[torch.Tensor], List[np.ndarray], List[tf.Tensor]]`): + masks (`Union[list[torch.Tensor], list[np.ndarray], list[tf.Tensor]]`): Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. - original_sizes (`Union[torch.Tensor, tf.Tensor, List[Tuple[int,int]]]`): + original_sizes (`Union[torch.Tensor, tf.Tensor, list[tuple[int,int]]]`): The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. - reshaped_input_sizes (`Union[torch.Tensor, tf.Tensor, List[Tuple[int,int]]]`): + reshaped_input_sizes (`Union[torch.Tensor, tf.Tensor, list[tuple[int,int]]]`): The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. mask_threshold (`float`, *optional*, defaults to 0.0): The threshold to use for binarizing the masks. @@ -633,12 +633,12 @@ class SamImageProcessor(BaseImageProcessor): Remove padding and upscale masks to the original image size. Args: - masks (`Union[List[torch.Tensor], List[np.ndarray]]`): + masks (`Union[list[torch.Tensor], list[np.ndarray]]`): Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. - original_sizes (`Union[torch.Tensor, List[Tuple[int,int]]]`): + original_sizes (`Union[torch.Tensor, list[tuple[int,int]]]`): The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. - reshaped_input_sizes (`Union[torch.Tensor, List[Tuple[int,int]]]`): + reshaped_input_sizes (`Union[torch.Tensor, list[tuple[int,int]]]`): The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. mask_threshold (`float`, *optional*, defaults to 0.0): The threshold to use for binarizing the masks. @@ -722,11 +722,11 @@ class SamImageProcessor(BaseImageProcessor): Post processes mask that are generated by calling the Non Maximum Suppression algorithm on the predicted masks. Args: - all_masks (`Union[List[torch.Tensor], List[tf.Tensor]]`): + all_masks (`Union[list[torch.Tensor], list[tf.Tensor]]`): List of all predicted segmentation masks - all_scores (`Union[List[torch.Tensor], List[tf.Tensor]]`): + all_scores (`Union[list[torch.Tensor], list[tf.Tensor]]`): List of all predicted iou scores - all_boxes (`Union[List[torch.Tensor], List[tf.Tensor]]`): + all_boxes (`Union[list[torch.Tensor], list[tf.Tensor]]`): List of all bounding boxes of the predicted masks crops_nms_thresh (`float`): Threshold for NMS (Non Maximum Suppression) algorithm. @@ -745,7 +745,7 @@ class SamImageProcessor(BaseImageProcessor): crop_n_layers: int = 0, overlap_ratio: float = 512 / 1500, points_per_crop: Optional[int] = 32, - crop_n_points_downscale_factor: Optional[List[int]] = 1, + crop_n_points_downscale_factor: Optional[list[int]] = 1, device: Optional["torch.device"] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, return_tensors: str = "pt", @@ -766,7 +766,7 @@ class SamImageProcessor(BaseImageProcessor): the image length. Later layers with more crops scale down this overlap. points_per_crop (`int`, *optional*, defaults to 32): Number of points to sample from each crop. - crop_n_points_downscale_factor (`List[int]`, *optional*, defaults to 1): + crop_n_points_downscale_factor (`list[int]`, *optional*, defaults to 1): The number of points-per-side sampled in layer n is scaled down by crop_n_points_downscale_factor**n. device (`torch.device`, *optional*, defaults to None): Device to use for the computation. If None, cpu will be used. @@ -826,7 +826,7 @@ class SamImageProcessor(BaseImageProcessor): Input masks. iou_scores (`Union[torch.Tensor, tf.Tensor]`): List of IoU scores. - original_size (`Tuple[int,int]`): + original_size (`tuple[int,int]`): Size of the original image. cropped_box_image (`np.array`): The cropped image. @@ -886,7 +886,7 @@ class SamImageProcessor(BaseImageProcessor): Input masks. iou_scores (`torch.Tensor`): List of IoU scores. - original_size (`Tuple[int,int]`): + original_size (`tuple[int,int]`): Size of the original image. cropped_box_image (`np.array`): The cropped image. @@ -966,7 +966,7 @@ class SamImageProcessor(BaseImageProcessor): Input masks. iou_scores (`tf.Tensor`): List of IoU scores. - original_size (`Tuple[int,int]`): + original_size (`tuple[int,int]`): Size of the original image. cropped_box_image (`np.array`): The cropped image. @@ -1055,7 +1055,7 @@ def _build_point_grid(n_per_side: int) -> np.ndarray: def _normalize_coordinates( - target_size: int, coords: np.ndarray, original_size: Tuple[int, int], is_bounding_box=False + target_size: int, coords: np.ndarray, original_size: tuple[int, int], is_bounding_box=False ) -> np.ndarray: """ Expects a numpy array of length 2 in the final dimension. Requires the original image size in (height, width) @@ -1088,9 +1088,9 @@ def _generate_crop_boxes( crop_n_layers: int = 0, overlap_ratio: float = 512 / 1500, points_per_crop: Optional[int] = 32, - crop_n_points_downscale_factor: Optional[List[int]] = 1, + crop_n_points_downscale_factor: Optional[list[int]] = 1, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[List[List[int]], List[int]]: +) -> tuple[list[list[int]], list[int]]: """ Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. @@ -1202,7 +1202,7 @@ def _generate_crop_images( return cropped_images, total_points_per_crop -def _pad_masks(masks, crop_box: List[int], orig_height: int, orig_width: int): +def _pad_masks(masks, crop_box: list[int], orig_height: int, orig_width: int): left, top, right, bottom = crop_box if left == 0 and top == 0 and right == orig_width and bottom == orig_height: return masks @@ -1212,7 +1212,7 @@ def _pad_masks(masks, crop_box: List[int], orig_height: int, orig_width: int): return torch.nn.functional.pad(masks, pad, value=0) -def _pad_masks_tf(masks, crop_box: List[int], orig_height: int, orig_width: int): +def _pad_masks_tf(masks, crop_box: list[int], orig_height: int, orig_width: int): left, top, right, bottom = crop_box if left == 0 and top == 0 and right == orig_width and bottom == orig_height: return masks @@ -1419,7 +1419,7 @@ def _mask_to_rle_tf(input_mask: "tf.Tensor"): return out -def _rle_to_mask(rle: Dict[str, Any]) -> np.ndarray: +def _rle_to_mask(rle: dict[str, Any]) -> np.ndarray: """Compute a binary mask from an uncompressed RLE.""" height, width = rle["size"] mask = np.empty(height * width, dtype=bool) diff --git a/src/transformers/models/sam/modeling_sam.py b/src/transformers/models/sam/modeling_sam.py index b6af50ba88b..28e28f07d2d 100644 --- a/src/transformers/models/sam/modeling_sam.py +++ b/src/transformers/models/sam/modeling_sam.py @@ -16,7 +16,7 @@ import collections from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -65,8 +65,8 @@ class SamVisionEncoderOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -100,9 +100,9 @@ class SamImageSegmentationOutput(ModelOutput): iou_scores: Optional[torch.FloatTensor] = None pred_masks: Optional[torch.FloatTensor] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - vision_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - mask_decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + vision_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + mask_decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None class SamPatchEmbeddings(nn.Module): @@ -406,7 +406,7 @@ class SamTwoWayTransformer(nn.Module): target_embedding=None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -515,7 +515,7 @@ class SamMaskDecoder(nn.Module): output_attentions: Optional[bool] = None, attention_similarity: Optional[torch.Tensor] = None, target_embedding: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Predict masks given image and prompt embeddings. @@ -720,11 +720,11 @@ class SamPromptEncoder(nn.Module): def forward( self, - input_points: Optional[Tuple[torch.Tensor, torch.Tensor]], + input_points: Optional[tuple[torch.Tensor, torch.Tensor]], input_labels: Optional[torch.Tensor], input_boxes: Optional[torch.Tensor], input_masks: Optional[torch.Tensor], - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Embeds different types of prompts, returning both sparse and dense embeddings. @@ -830,8 +830,8 @@ class SamVisionAttention(nn.Module): query: torch.Tensor, rel_pos_h: torch.Tensor, rel_pos_w: torch.Tensor, - q_size: Tuple[int, int], - k_size: Tuple[int, int], + q_size: tuple[int, int], + k_size: tuple[int, int], ) -> torch.Tensor: """ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`. @@ -978,7 +978,7 @@ class SamVisionLayer(nn.Module): self.mlp = SamMLPBlock(config) self.window_size = window_size - def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> Tuple[torch.Tensor, Tuple[int, int]]: + def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> tuple[torch.Tensor, tuple[int, int]]: """ Args: Partition into non-overlapping windows with padding if needed. @@ -1003,7 +1003,7 @@ class SamVisionLayer(nn.Module): return windows, (pad_height, pad_width) def window_unpartition( - self, windows: torch.Tensor, window_size: int, padding_shape: Tuple[int, int], original_shape: Tuple[int, int] + self, windows: torch.Tensor, window_size: int, padding_shape: tuple[int, int], original_shape: tuple[int, int] ) -> torch.Tensor: """ Args: @@ -1036,7 +1036,7 @@ class SamVisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) @@ -1224,7 +1224,7 @@ class SamVisionModel(SamPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SamVisionEncoderOutput]: + ) -> Union[tuple, SamVisionEncoderOutput]: return self.vision_encoder( pixel_values, output_attentions=output_attentions, diff --git a/src/transformers/models/sam/modeling_tf_sam.py b/src/transformers/models/sam/modeling_tf_sam.py index fcfb7c21843..c44dfdf80ac 100644 --- a/src/transformers/models/sam/modeling_tf_sam.py +++ b/src/transformers/models/sam/modeling_tf_sam.py @@ -21,7 +21,7 @@ from __future__ import annotations import collections from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -72,8 +72,8 @@ class TFSamVisionEncoderOutput(ModelOutput): image_embeds: tf.Tensor | None = None last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -107,9 +107,9 @@ class TFSamImageSegmentationOutput(ModelOutput): iou_scores: Optional[tf.Tensor] = None pred_masks: Optional[tf.Tensor] = None - vision_hidden_states: Tuple[tf.Tensor, ...] | None = None - vision_attentions: Tuple[tf.Tensor, ...] | None = None - mask_decoder_attentions: Tuple[tf.Tensor, ...] | None = None + vision_hidden_states: tuple[tf.Tensor, ...] | None = None + vision_attentions: tuple[tf.Tensor, ...] | None = None + mask_decoder_attentions: tuple[tf.Tensor, ...] | None = None class TFSamPatchEmbeddings(keras.layers.Layer): @@ -434,7 +434,7 @@ class TFSamTwoWayTransformer(keras.layers.Layer): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -614,7 +614,7 @@ class TFSamMaskDecoder(keras.layers.Layer): dense_prompt_embeddings: tf.Tensor, multimask_output: bool, output_attentions: Optional[bool] = None, - ) -> Tuple[tf.Tensor, tf.Tensor]: + ) -> tuple[tf.Tensor, tf.Tensor]: batch_size, num_channels, height, width = shape_list(image_embeddings) point_batch_size = tf.math.maximum(1, tf.shape(sparse_prompt_embeddings)[1]) @@ -858,11 +858,11 @@ class TFSamPromptEncoder(keras.layers.Layer): def call( self, batch_size: Optional[int], - input_points: Optional[Tuple[tf.Tensor, tf.Tensor]], + input_points: Optional[tuple[tf.Tensor, tf.Tensor]], input_labels: tf.Tensor | None, input_boxes: tf.Tensor | None, input_masks: tf.Tensor | None, - ) -> Tuple[tf.Tensor, tf.Tensor]: + ) -> tuple[tf.Tensor, tf.Tensor]: """ Embeds different types of prompts, returning both sparse and dense embeddings. @@ -993,8 +993,8 @@ class TFSamVisionAttention(keras.layers.Layer): query: tf.Tensor, rel_pos_h: tf.Tensor, rel_pos_w: tf.Tensor, - q_size: Tuple[int, int], - k_size: Tuple[int, int], + q_size: tuple[int, int], + k_size: tuple[int, int], ) -> tf.Tensor: """ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`. @@ -1081,7 +1081,7 @@ class TFSamVisionLayer(keras.layers.Layer): self.window_size = window_size self.config = config - def window_partition(self, hidden_states: tf.Tensor, window_size: int) -> Tuple[tf.Tensor, Tuple[int, int]]: + def window_partition(self, hidden_states: tf.Tensor, window_size: int) -> tuple[tf.Tensor, tuple[int, int]]: batch_size, height, width, channel = shape_list(hidden_states) pad_h = (window_size - height % window_size) % window_size @@ -1100,7 +1100,7 @@ class TFSamVisionLayer(keras.layers.Layer): return windows, (pad_height, pad_width) def window_unpartition( - self, windows: tf.Tensor, window_size: int, padding_shape: Tuple[int, int], original_shape: Tuple[int, int] + self, windows: tf.Tensor, window_size: int, padding_shape: tuple[int, int], original_shape: tuple[int, int] ) -> tf.Tensor: pad_height, pad_width = padding_shape height, width = original_shape @@ -1121,7 +1121,7 @@ class TFSamVisionLayer(keras.layers.Layer): hidden_states: tf.Tensor, output_attentions: Optional[bool] = False, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) @@ -1272,7 +1272,7 @@ class TFSamVisionEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSamVisionEncoderOutput]: + ) -> Union[tuple, TFSamVisionEncoderOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1456,7 +1456,7 @@ class TFSamVisionModel(TFSamPreTrainedModel): return_dict: bool | None = None, training: bool = False, **kwargs, - ) -> TFSamVisionEncoderOutput | Tuple[tf.Tensor]: + ) -> TFSamVisionEncoderOutput | tuple[tf.Tensor]: r""" Returns: @@ -1581,7 +1581,7 @@ class TFSamModel(TFSamPreTrainedModel): return_dict: bool | None = None, training: bool = False, **kwargs, - ) -> TFSamImageSegmentationOutput | Tuple[tf.Tensor]: + ) -> TFSamImageSegmentationOutput | tuple[tf.Tensor]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/sam/processing_sam.py b/src/transformers/models/sam/processing_sam.py index 06e4095affe..379ccbe0e21 100644 --- a/src/transformers/models/sam/processing_sam.py +++ b/src/transformers/models/sam/processing_sam.py @@ -17,7 +17,7 @@ Processor class for SAM. """ from copy import deepcopy -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -37,9 +37,9 @@ if is_tf_available(): class SamImagesKwargs(ImagesKwargs): segmentation_maps: Optional[ImageInput] - input_points: Optional[List[List[float]]] - input_labels: Optional[List[List[int]]] - input_boxes: Optional[List[List[List[float]]]] + input_points: Optional[list[list[float]]] + input_labels: Optional[list[list[int]]] + input_boxes: Optional[list[list[list[float]]]] point_pad_value: Optional[int] @@ -89,7 +89,7 @@ class SamProcessor(ProcessorMixin): # https://github.com/huggingface/transformers/pull/32544#discussion_r1720208116 # This behavior is only needed for backward compatibility and will be removed in future versions. *args, # to be deprecated - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio: Optional[AudioInput] = None, video: Optional[VideoInput] = None, **kwargs, diff --git a/src/transformers/models/sam_hq/configuration_sam_hq.py b/src/transformers/models/sam_hq/configuration_sam_hq.py index 49062efc68d..1322bc560d9 100644 --- a/src/transformers/models/sam_hq/configuration_sam_hq.py +++ b/src/transformers/models/sam_hq/configuration_sam_hq.py @@ -115,7 +115,7 @@ class SamHQVisionConfig(PretrainedConfig): Whether to use relative position embedding. window_size (`int`, *optional*, defaults to 14): Window size for relative position. - global_attn_indexes (`List[int]`, *optional*, defaults to `[2, 5, 8, 11]`): + global_attn_indexes (`list[int]`, *optional*, defaults to `[2, 5, 8, 11]`): The indexes of the global attention layers. num_pos_feats (`int`, *optional*, defaults to 128): The dimensionality of the position embedding. diff --git a/src/transformers/models/sam_hq/modeling_sam_hq.py b/src/transformers/models/sam_hq/modeling_sam_hq.py index d0f78bb6ce3..14f30fb4b40 100644 --- a/src/transformers/models/sam_hq/modeling_sam_hq.py +++ b/src/transformers/models/sam_hq/modeling_sam_hq.py @@ -21,7 +21,7 @@ # limitations under the License. import collections from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -69,10 +69,10 @@ class SamHQVisionEncoderOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None - intermediate_embeddings: Optional[List[torch.FloatTensor]] = None + intermediate_embeddings: Optional[list[torch.FloatTensor]] = None @dataclass @@ -106,9 +106,9 @@ class SamHQImageSegmentationOutput(ModelOutput): iou_scores: Optional[torch.FloatTensor] = None pred_masks: Optional[torch.FloatTensor] = None - vision_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - vision_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - mask_decoder_attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + vision_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + vision_attentions: Optional[tuple[torch.FloatTensor, ...]] = None + mask_decoder_attentions: Optional[tuple[torch.FloatTensor, ...]] = None class SamHQPatchEmbeddings(nn.Module): @@ -225,8 +225,8 @@ class SamHQVisionAttention(nn.Module): query: torch.Tensor, rel_pos_h: torch.Tensor, rel_pos_w: torch.Tensor, - q_size: Tuple[int, int], - k_size: Tuple[int, int], + q_size: tuple[int, int], + k_size: tuple[int, int], ) -> torch.Tensor: """ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`. @@ -373,7 +373,7 @@ class SamHQVisionLayer(nn.Module): self.mlp = SamHQMLPBlock(config) self.window_size = window_size - def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> Tuple[torch.Tensor, Tuple[int, int]]: + def window_partition(self, hidden_states: torch.Tensor, window_size: int) -> tuple[torch.Tensor, tuple[int, int]]: """ Args: Partition into non-overlapping windows with padding if needed. @@ -398,7 +398,7 @@ class SamHQVisionLayer(nn.Module): return windows, (pad_height, pad_width) def window_unpartition( - self, windows: torch.Tensor, window_size: int, padding_shape: Tuple[int, int], original_shape: Tuple[int, int] + self, windows: torch.Tensor, window_size: int, padding_shape: tuple[int, int], original_shape: tuple[int, int] ) -> torch.Tensor: """ Args: @@ -431,7 +431,7 @@ class SamHQVisionLayer(nn.Module): self, hidden_states: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: residual = hidden_states hidden_states = self.layer_norm1(hidden_states) @@ -521,7 +521,7 @@ class SamHQVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SamHQVisionEncoderOutput]: + ) -> Union[tuple, SamHQVisionEncoderOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -832,7 +832,7 @@ class SamHQTwoWayTransformer(nn.Module): target_embedding=None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -956,11 +956,11 @@ class SamHQMaskDecoder(nn.Module): dense_prompt_embeddings: torch.Tensor, multimask_output: bool, hq_token_only: bool, - intermediate_embeddings: Optional[List[torch.Tensor]] = None, + intermediate_embeddings: Optional[list[torch.Tensor]] = None, output_attentions: Optional[bool] = None, attention_similarity: Optional[torch.Tensor] = None, target_embedding: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Predict high-quality masks given image and prompt embeddings. @@ -1171,7 +1171,7 @@ class SamHQVisionModel(SamHQPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SamHQVisionEncoderOutput]: + ) -> Union[tuple, SamHQVisionEncoderOutput]: return self.vision_encoder( pixel_values, output_attentions=output_attentions, @@ -1298,11 +1298,11 @@ class SamHQPromptEncoder(nn.Module): def forward( self, - input_points: Optional[Tuple[torch.Tensor, torch.Tensor]], + input_points: Optional[tuple[torch.Tensor, torch.Tensor]], input_labels: Optional[torch.Tensor], input_boxes: Optional[torch.Tensor], input_masks: Optional[torch.Tensor], - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Embeds different types of prompts, returning both sparse and dense embeddings. @@ -1467,9 +1467,9 @@ class SamHQModel(SamHQPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - intermediate_embeddings: Optional[List[torch.FloatTensor]] = None, + intermediate_embeddings: Optional[list[torch.FloatTensor]] = None, **kwargs, - ) -> List[Dict[str, torch.Tensor]]: + ) -> list[dict[str, torch.Tensor]]: r""" input_points (`torch.FloatTensor` of shape `(batch_size, num_points, 2)`): Input 2D spatial points, this is used by the prompt encoder to encode the prompt. Generally yields to much diff --git a/src/transformers/models/sam_hq/modular_sam_hq.py b/src/transformers/models/sam_hq/modular_sam_hq.py index 584c2824d98..45d8067d83a 100644 --- a/src/transformers/models/sam_hq/modular_sam_hq.py +++ b/src/transformers/models/sam_hq/modular_sam_hq.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -118,7 +118,7 @@ class SamHQVisionEncoderOutput(SamVisionEncoderOutput): This is specific to SAM-HQ and not present in base SAM. """ - intermediate_embeddings: Optional[List[torch.FloatTensor]] = None + intermediate_embeddings: Optional[list[torch.FloatTensor]] = None @dataclass @@ -133,7 +133,7 @@ class SamHQVisionEncoder(SamVisionEncoder): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SamHQVisionEncoderOutput]: + ) -> Union[tuple, SamHQVisionEncoderOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -259,11 +259,11 @@ class SamHQMaskDecoder(nn.Module): dense_prompt_embeddings: torch.Tensor, multimask_output: bool, hq_token_only: bool, - intermediate_embeddings: Optional[List[torch.Tensor]] = None, + intermediate_embeddings: Optional[list[torch.Tensor]] = None, output_attentions: Optional[bool] = None, attention_similarity: Optional[torch.Tensor] = None, target_embedding: Optional[torch.Tensor] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ Predict high-quality masks given image and prompt embeddings. @@ -495,9 +495,9 @@ class SamHQModel(SamModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - intermediate_embeddings: Optional[List[torch.FloatTensor]] = None, + intermediate_embeddings: Optional[list[torch.FloatTensor]] = None, **kwargs, - ) -> List[Dict[str, torch.Tensor]]: + ) -> list[dict[str, torch.Tensor]]: r""" input_points (`torch.FloatTensor` of shape `(batch_size, num_points, 2)`): Input 2D spatial points, this is used by the prompt encoder to encode the prompt. Generally yields to much diff --git a/src/transformers/models/sam_hq/processing_samhq.py b/src/transformers/models/sam_hq/processing_samhq.py index 44ba3247e8e..bd19784f5fb 100644 --- a/src/transformers/models/sam_hq/processing_samhq.py +++ b/src/transformers/models/sam_hq/processing_samhq.py @@ -17,7 +17,7 @@ Processor class for SAMHQ. """ from copy import deepcopy -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -34,9 +34,9 @@ if is_torch_available(): class SamHQImagesKwargs(ImagesKwargs): segmentation_maps: Optional[ImageInput] - input_points: Optional[List[List[float]]] - input_labels: Optional[List[List[int]]] - input_boxes: Optional[List[List[List[float]]]] + input_points: Optional[list[list[float]]] + input_labels: Optional[list[list[int]]] + input_boxes: Optional[list[list[list[float]]]] point_pad_value: Optional[int] @@ -91,7 +91,7 @@ class SamHQProcessor(ProcessorMixin): # https://github.com/huggingface/transformers/pull/32544#discussion_r1720208116 # This behavior is only needed for backward compatibility and will be removed in future versions. *args, # to be deprecated - text: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, audio: Optional[AudioInput] = None, video: Optional[VideoInput] = None, **kwargs: Unpack[SamHQProcessorKwargs], diff --git a/src/transformers/models/seamless_m4t/configuration_seamless_m4t.py b/src/transformers/models/seamless_m4t/configuration_seamless_m4t.py index 25715352dbd..fe4f911957f 100644 --- a/src/transformers/models/seamless_m4t/configuration_seamless_m4t.py +++ b/src/transformers/models/seamless_m4t/configuration_seamless_m4t.py @@ -184,18 +184,18 @@ class SeamlessM4TConfig(PretrainedConfig): The sampling rate at which the output audio will be generated, expressed in hertz (Hz). upsample_initial_channel (`int`, *optional*, defaults to 512): The number of input channels into the hifi-gan upsampling network. Applies to the vocoder only. - upsample_rates (`Tuple[int]` or `List[int]`, *optional*, defaults to `[5, 4, 4, 2, 2]`): + upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[5, 4, 4, 2, 2]`): A tuple of integers defining the stride of each 1D convolutional layer in the vocoder upsampling network. The length of *upsample_rates* defines the number of convolutional layers and has to match the length of *upsample_kernel_sizes*. Applies to the vocoder only. - upsample_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[11, 8, 8, 4, 4]`): + upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[11, 8, 8, 4, 4]`): A tuple of integers defining the kernel size of each 1D convolutional layer in the vocoder upsampling network. The length of *upsample_kernel_sizes* defines the number of convolutional layers and has to match the length of *upsample_rates*. Applies to the vocoder only. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`): A tuple of integers defining the kernel sizes of the vocoder 1D convolutional layers in the multi-receptive field fusion (MRF) module. Applies to the vocoder only. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A nested tuple of integers defining the dilation rates of the vocoder dilated 1D convolutional layers in the multi-receptive field fusion (MRF) module. Applies to the vocoder only. leaky_relu_slope (`float`, *optional*, defaults to 0.1): diff --git a/src/transformers/models/seamless_m4t/feature_extraction_seamless_m4t.py b/src/transformers/models/seamless_m4t/feature_extraction_seamless_m4t.py index b17dcf792e1..59a20059f86 100644 --- a/src/transformers/models/seamless_m4t/feature_extraction_seamless_m4t.py +++ b/src/transformers/models/seamless_m4t/feature_extraction_seamless_m4t.py @@ -16,7 +16,7 @@ Feature extractor class for SeamlessM4T """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -92,8 +92,8 @@ class SeamlessM4TFeatureExtractor(SequenceFeatureExtractor): @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def zero_mean_unit_var_norm( - input_values: List[np.ndarray], attention_mask: List[np.ndarray], padding_value: float = 0.0 - ) -> List[np.ndarray]: + input_values: list[np.ndarray], attention_mask: list[np.ndarray], padding_value: float = 0.0 + ) -> list[np.ndarray]: """ Every array in the list is normalized to have zero mean and unit variance """ @@ -143,7 +143,7 @@ class SeamlessM4TFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Union[bool, str, PaddingStrategy] = True, pad_to_multiple_of: Optional[int] = 2, max_length: Optional[int] = None, @@ -158,14 +158,14 @@ class SeamlessM4TFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `torch.Tensor`, `List[float]`, `List[np.ndarray]`, `List[torch.Tensor]`, - `List[List[float]]`, `List[List[List[float]]]`): + raw_speech (`np.ndarray`, `torch.Tensor`, `list[float]`, `list[np.ndarray]`, `list[torch.Tensor]`, + `list[list[float]]`, `list[list[list[float]]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a torch tensor, a list of float values, a list of numpy arrays, a list of torch tensors, a list of list of float values or a list of a list of list of float values. - If `raw_speech` is a one-dimensional `np.ndarray`, `torch.Tensor` or a `List[float]`, `raw_speech` is + If `raw_speech` is a one-dimensional `np.ndarray`, `torch.Tensor` or a `list[float]`, `raw_speech` is considered a single-channel, single-sample sound. In all other cases, the first dimension of - `raw_speech`, whether from an `np.ndarray`, a `torch.Tensor` or a `List[...]`, + `raw_speech`, whether from an `np.ndarray`, a `torch.Tensor` or a `list[...]`, corresponds to the number of samples in the batch, and the number of channels (i.e. mono or stereo character) is derived from the other dimensions (1D -> single-channel waveform batches; 2D-> stereo-channel waveform batches). diff --git a/src/transformers/models/seamless_m4t/modeling_seamless_m4t.py b/src/transformers/models/seamless_m4t/modeling_seamless_m4t.py index 64a8451ce5a..03136936e60 100755 --- a/src/transformers/models/seamless_m4t/modeling_seamless_m4t.py +++ b/src/transformers/models/seamless_m4t/modeling_seamless_m4t.py @@ -17,7 +17,7 @@ import copy import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -101,8 +101,8 @@ class SeamlessM4TGenerationOutput(ModelOutput): waveform: Optional[torch.FloatTensor] = None waveform_lengths: Optional[torch.IntTensor] = None - sequences: Optional[Tuple[torch.FloatTensor]] = None - unit_sequences: Optional[Tuple[torch.FloatTensor]] = None + sequences: Optional[tuple[torch.FloatTensor]] = None + unit_sequences: Optional[tuple[torch.FloatTensor]] = None ############ UTILS ################ @@ -493,7 +493,7 @@ class SeamlessM4TConformerSelfAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, relative_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() @@ -1043,10 +1043,10 @@ class SeamlessM4TAttention(nn.Module): self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if encoder_hidden_states are provided this layer is used as a cross-attention layer @@ -1272,7 +1272,7 @@ class SeamlessM4TDecoderLayer(nn.Module): attention_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -1408,14 +1408,14 @@ class SeamlessM4TPreTrainedModel(PreTrainedModel): def compute_last_hidden_states_per_sample( self, - hidden_states: Tuple[Tuple[torch.Tensor]], + hidden_states: tuple[tuple[torch.Tensor]], beam_indices: Optional[torch.Tensor] = None, ) -> torch.Tensor: """ Computes the last hidden states. Parameters: - hidden_states (`Tuple[Tuple[torch.Tensor]]`): + hidden_states (`tuple[tuple[torch.Tensor]]`): The generated hidden states. Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) of torch.FloatTensor of shape (batch_size*num_beams*num_return_sequences, generated_length, hidden_size). @@ -1489,7 +1489,7 @@ class SeamlessM4TSpeechEncoder(SeamlessM4TPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, Wav2Vec2BaseModelOutput]: + ) -> Union[tuple, Wav2Vec2BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1604,7 +1604,7 @@ class SeamlessM4TEncoder(SeamlessM4TPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1791,13 +1791,13 @@ class SeamlessM4TDecoder(SeamlessM4TPreTrainedModel): attention_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1949,15 +1949,15 @@ class SeamlessM4TTextToUnitModel(SeamlessM4TPreTrainedModel): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -2073,8 +2073,8 @@ class SeamlessM4TTextToUnitForConditionalGeneration(SeamlessM4TPreTrainedModel, attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2082,7 +2082,7 @@ class SeamlessM4TTextToUnitForConditionalGeneration(SeamlessM4TPreTrainedModel, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2416,7 +2416,7 @@ class SeamlessM4TCodeHifiGan(PreTrainedModel): def forward( self, input_ids: torch.LongTensor, spkr_id: torch.Tensor, lang_id: torch.Tensor - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: """ Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -2558,8 +2558,8 @@ class SeamlessM4TForTextToText(SeamlessM4TPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2568,7 +2568,7 @@ class SeamlessM4TForTextToText(SeamlessM4TPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2699,7 +2699,7 @@ class SeamlessM4TForTextToText(SeamlessM4TPreTrainedModel, GenerationMixin): Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -2709,7 +2709,7 @@ class SeamlessM4TForTextToText(SeamlessM4TPreTrainedModel, GenerationMixin): synced_gpus (`bool`, *optional*, defaults to `False`): Whether to continue running the while loop until max_length (needed to avoid deadlocking with `FullyShardedDataParallel` and DeepSpeed ZeRO Stage 3). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. @@ -2826,8 +2826,8 @@ class SeamlessM4TForSpeechToText(SeamlessM4TPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2836,7 +2836,7 @@ class SeamlessM4TForSpeechToText(SeamlessM4TPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2971,7 +2971,7 @@ class SeamlessM4TForSpeechToText(SeamlessM4TPreTrainedModel, GenerationMixin): Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -2981,7 +2981,7 @@ class SeamlessM4TForSpeechToText(SeamlessM4TPreTrainedModel, GenerationMixin): synced_gpus (`bool`, *optional*, defaults to `False`): Whether to continue running the while loop until max_length (needed to avoid deadlocking with `FullyShardedDataParallel` and DeepSpeed ZeRO Stage 3). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. @@ -3111,8 +3111,8 @@ class SeamlessM4TForTextToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3120,7 +3120,7 @@ class SeamlessM4TForTextToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -3262,7 +3262,7 @@ class SeamlessM4TForTextToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): Returns: - `Union[SeamlessM4TGenerationOutput, Tuple[Tensor]]`: + `Union[SeamlessM4TGenerationOutput, tuple[Tensor]]`: - If `return_intermediate_token_ids`, returns [`SeamlessM4TGenerationOutput`]. - If not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. @@ -3443,8 +3443,8 @@ class SeamlessM4TForSpeechToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3453,7 +3453,7 @@ class SeamlessM4TForSpeechToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -3599,7 +3599,7 @@ class SeamlessM4TForSpeechToSpeech(SeamlessM4TPreTrainedModel, GenerationMixin): Returns: - `Union[SeamlessM4TGenerationOutput, Tuple[Tensor]]`: + `Union[SeamlessM4TGenerationOutput, tuple[Tensor]]`: - If `return_intermediate_token_ids`, returns [`SeamlessM4TGenerationOutput`]. - If not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. @@ -3813,8 +3813,8 @@ class SeamlessM4TModel(SeamlessM4TPreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3823,7 +3823,7 @@ class SeamlessM4TModel(SeamlessM4TPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -4017,7 +4017,7 @@ class SeamlessM4TModel(SeamlessM4TPreTrainedModel, GenerationMixin): other. Returns: - `Union[SeamlessM4TGenerationOutput, Tuple[Tensor], ModelOutput]`: + `Union[SeamlessM4TGenerationOutput, tuple[Tensor], ModelOutput]`: - If `generate_speech` and `return_intermediate_token_ids`, returns [`SeamlessM4TGenerationOutput`]. - If `generate_speech` and not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. diff --git a/src/transformers/models/seamless_m4t/processing_seamless_m4t.py b/src/transformers/models/seamless_m4t/processing_seamless_m4t.py index 90d96b61c99..5d6766cd66e 100644 --- a/src/transformers/models/seamless_m4t/processing_seamless_m4t.py +++ b/src/transformers/models/seamless_m4t/processing_seamless_m4t.py @@ -50,11 +50,11 @@ class SeamlessM4TProcessor(ProcessorMixin): to the docstring of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - audios (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + audios (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The audio or batch of audios to be prepared. Each audio can be NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each audio should be of shape (C, T), where C is a number of channels, and T the sample length of the audio. diff --git a/src/transformers/models/seamless_m4t/tokenization_seamless_m4t.py b/src/transformers/models/seamless_m4t/tokenization_seamless_m4t.py index 0d73e6a099a..fd773316580 100644 --- a/src/transformers/models/seamless_m4t/tokenization_seamless_m4t.py +++ b/src/transformers/models/seamless_m4t/tokenization_seamless_m4t.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece as spm @@ -106,7 +106,7 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): The language to use as source language for translation. tgt_lang (`str`, *optional*, defaults to `"fra"`): The language to use as target language for translation. - sp_model_kwargs (`Dict[str, Any]`, *optional*): + sp_model_kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments to pass to the model initialization. additional_special_tokens (tuple or list of `str` or `tokenizers.AddedToken`, *optional*): A tuple or a list of additional special tokens. Can be used to specify the list of languages that will be @@ -119,8 +119,8 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -134,7 +134,7 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): tokenizer_file=None, src_lang="eng", tgt_lang="fra", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, additional_special_tokens=None, add_prefix_space=True, **kwargs, @@ -211,11 +211,11 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, padding: Union[bool, str, PaddingStrategy] = True, pad_to_multiple_of: Optional[int] = 2, @@ -225,19 +225,19 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): ): """ Args: - text (`str`, `List[str]`, `List[List[str]]`, *optional*): + text (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -309,22 +309,22 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): # Copied from transformers.models.nllb.tokenization_nllb.NllbTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -340,8 +340,8 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): # Copied from transformers.models.nllb.tokenization_nllb.NllbTokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An NLLB sequence has the following format, where `X` represents the sequence: @@ -353,13 +353,13 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -368,20 +368,20 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): # Copied from transformers.models.nllb.tokenization_nllb.NllbTokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. nllb does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -436,7 +436,7 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): return tokenizer # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.tokenize - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Converts a string to a list of tokens. If `self.legacy` is set to `False`, a prefix token is added unless the first token is special. @@ -494,7 +494,7 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): return out_string # Copied from transformers.models.nllb.tokenization_nllb.NllbTokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -514,9 +514,9 @@ class SeamlessM4TTokenizer(PreTrainedTokenizer): # Copied from transformers.models.nllb.tokenization_nllb.NllbTokenizer.prepare_seq2seq_batch with eng_Latn->eng, fra_Latn->fra def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "eng", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "fra", **kwargs, ) -> BatchEncoding: diff --git a/src/transformers/models/seamless_m4t/tokenization_seamless_m4t_fast.py b/src/transformers/models/seamless_m4t/tokenization_seamless_m4t_fast.py index a3ab631560a..0318336332c 100644 --- a/src/transformers/models/seamless_m4t/tokenization_seamless_m4t_fast.py +++ b/src/transformers/models/seamless_m4t/tokenization_seamless_m4t_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from tokenizers import processors @@ -112,8 +112,8 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): slow_tokenizer_class = SeamlessM4TTokenizer model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] - suffix_tokens: List[int] = [] + prefix_tokens: list[int] = [] + suffix_tokens: list[int] = [] def __init__( self, @@ -177,8 +177,8 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): self.set_tgt_lang_special_tokens(self._tgt_lang) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. The special tokens depend on calling set_lang. @@ -192,13 +192,13 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): separator. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: list of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + self.suffix_tokens @@ -207,20 +207,20 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.nllb.tokenization_nllb_fast.NllbTokenizerFast.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. nllb does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -248,9 +248,9 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.nllb.tokenization_nllb_fast.NllbTokenizerFast.prepare_seq2seq_batch with "fra_Latn"->"fra", "eng_Latn"->"eng" def prepare_seq2seq_batch( self, - src_texts: List[str], + src_texts: list[str], src_lang: str = "eng", - tgt_texts: Optional[List[str]] = None, + tgt_texts: Optional[list[str]] = None, tgt_lang: str = "fra", **kwargs, ) -> BatchEncoding: @@ -317,7 +317,7 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): ) # Copied from transformers.models.nllb.tokenization_nllb_fast.NllbTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " @@ -371,11 +371,11 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, padding: Union[bool, str, PaddingStrategy] = True, pad_to_multiple_of: Optional[int] = 2, @@ -385,19 +385,19 @@ class SeamlessM4TTokenizerFast(PreTrainedTokenizerFast): ): """ Args: - text (`str`, `List[str]`, `List[List[str]]`, *optional*): + text (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/seamless_m4t_v2/configuration_seamless_m4t_v2.py b/src/transformers/models/seamless_m4t_v2/configuration_seamless_m4t_v2.py index 5c5c34d6aa9..7ac6c4024a5 100644 --- a/src/transformers/models/seamless_m4t_v2/configuration_seamless_m4t_v2.py +++ b/src/transformers/models/seamless_m4t_v2/configuration_seamless_m4t_v2.py @@ -187,18 +187,18 @@ class SeamlessM4Tv2Config(PretrainedConfig): The sampling rate at which the output audio will be generated, expressed in hertz (Hz). upsample_initial_channel (`int`, *optional*, defaults to 512): The number of input channels into the hifi-gan upsampling network. Applies to the vocoder only. - upsample_rates (`Tuple[int]` or `List[int]`, *optional*, defaults to `[5, 4, 4, 2, 2]`): + upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[5, 4, 4, 2, 2]`): A tuple of integers defining the stride of each 1D convolutional layer in the vocoder upsampling network. The length of *upsample_rates* defines the number of convolutional layers and has to match the length of *upsample_kernel_sizes*. Applies to the vocoder only. - upsample_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[11, 8, 8, 4, 4]`): + upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[11, 8, 8, 4, 4]`): A tuple of integers defining the kernel size of each 1D convolutional layer in the vocoder upsampling network. The length of *upsample_kernel_sizes* defines the number of convolutional layers and has to match the length of *upsample_rates*. Applies to the vocoder only. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`): A tuple of integers defining the kernel sizes of the vocoder 1D convolutional layers in the multi-receptive field fusion (MRF) module. Applies to the vocoder only. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A nested tuple of integers defining the dilation rates of the vocoder dilated 1D convolutional layers in the multi-receptive field fusion (MRF) module. Applies to the vocoder only. leaky_relu_slope (`float`, *optional*, defaults to 0.1): diff --git a/src/transformers/models/seamless_m4t_v2/modeling_seamless_m4t_v2.py b/src/transformers/models/seamless_m4t_v2/modeling_seamless_m4t_v2.py index 6bf730003a4..b6047a10da6 100644 --- a/src/transformers/models/seamless_m4t_v2/modeling_seamless_m4t_v2.py +++ b/src/transformers/models/seamless_m4t_v2/modeling_seamless_m4t_v2.py @@ -17,7 +17,7 @@ import copy import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -99,8 +99,8 @@ class SeamlessM4Tv2GenerationOutput(ModelOutput): waveform: Optional[torch.FloatTensor] = None waveform_lengths: Optional[torch.IntTensor] = None - sequences: Optional[Tuple[torch.FloatTensor]] = None - unit_sequences: Optional[Tuple[torch.FloatTensor]] = None + sequences: Optional[tuple[torch.FloatTensor]] = None + unit_sequences: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -128,8 +128,8 @@ class SeamlessM4Tv2TextToUnitDecoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None padding_mask: Optional[torch.Tensor] = None @@ -178,11 +178,11 @@ class SeamlessM4Tv2TextToUnitOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None padding_mask: Optional[torch.Tensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None loss: Optional[torch.FloatTensor] = None @@ -434,7 +434,7 @@ class SeamlessM4Tv2ConformerSelfAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() @@ -950,10 +950,10 @@ class SeamlessM4Tv2Attention(nn.Module): self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" is_cross_attention = encoder_hidden_states is not None @@ -1131,7 +1131,7 @@ class SeamlessM4Tv2DecoderLayer(nn.Module): attention_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -1382,7 +1382,7 @@ class SeamlessM4Tv2PreTrainedModel(PreTrainedModel): Args: input_ids (`torch.Tensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. - subwords_batch (`List[List[str]]` of shape `(batch_size, sequence_length)`): + subwords_batch (`list[list[str]]` of shape `(batch_size, sequence_length)`): Corresponding text string for each input id. merge_space_with_prev_subword (`bool`, *optional*, defaults to `False`): Indicates if the space character is merged with the previous subword. If `False`, it will be merged @@ -1458,7 +1458,7 @@ class SeamlessM4Tv2PreTrainedModel(PreTrainedModel): Args: input_ids (`torch.Tensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. - subwords_batch (`List[List[str]]` of shape `(batch_size, sequence_length)`): + subwords_batch (`list[list[str]]` of shape `(batch_size, sequence_length)`): Corresponding text string for each input id. char_count_per_id (`torch.Tensor` of shape `(batch_size, sequence_length)`): Number of characters per input id. @@ -1557,7 +1557,7 @@ class SeamlessM4Tv2SpeechEncoder(SeamlessM4Tv2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, Wav2Vec2BaseModelOutput]: + ) -> Union[tuple, Wav2Vec2BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1673,7 +1673,7 @@ class SeamlessM4Tv2Encoder(SeamlessM4Tv2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -1861,13 +1861,13 @@ class SeamlessM4Tv2Decoder(SeamlessM4Tv2PreTrainedModel): attention_mask: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -2072,7 +2072,7 @@ class SeamlessM4Tv2TextToUnitDecoder(SeamlessM4Tv2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SeamlessM4Tv2TextToUnitDecoderOutput]: + ) -> Union[tuple, SeamlessM4Tv2TextToUnitDecoderOutput]: r""" Args: char_input_ids (`torch.LongTensor` of shape `(batch_size, char_sequence_length)`): @@ -2203,12 +2203,12 @@ class SeamlessM4Tv2TextToUnitModel(SeamlessM4Tv2PreTrainedModel): char_input_ids: Optional[torch.LongTensor] = None, char_count_per_id: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -2325,14 +2325,14 @@ class SeamlessM4Tv2TextToUnitForConditionalGeneration(SeamlessM4Tv2PreTrainedMod char_input_ids: Optional[torch.LongTensor] = None, char_count_per_id: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" char_input_ids (`torch.LongTensor` of shape `(batch_size, char_sequence_length)`): Character indices. The correspondence between characters and indices can be found in `char_to_id`, a @@ -2657,7 +2657,7 @@ class SeamlessM4Tv2CodeHifiGan(PreTrainedModel): # Copied from transformers.models.seamless_m4t.modeling_seamless_m4t.SeamlessM4TCodeHifiGan.forward with SeamlessM4T->SeamlessM4Tv2, spkr_id->speaker_id def forward( self, input_ids: torch.LongTensor, speaker_id: torch.Tensor, lang_id: torch.Tensor - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: """ Args: input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): @@ -2803,8 +2803,8 @@ class SeamlessM4Tv2ForTextToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2813,7 +2813,7 @@ class SeamlessM4Tv2ForTextToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -2944,7 +2944,7 @@ class SeamlessM4Tv2ForTextToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin): Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -2954,7 +2954,7 @@ class SeamlessM4Tv2ForTextToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin): synced_gpus (`bool`, *optional*, defaults to `False`): Whether to continue running the while loop until max_length (needed to avoid deadlocking with `FullyShardedDataParallel` and DeepSpeed ZeRO Stage 3). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. @@ -3080,8 +3080,8 @@ class SeamlessM4Tv2ForSpeechToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3090,7 +3090,7 @@ class SeamlessM4Tv2ForSpeechToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -3226,7 +3226,7 @@ class SeamlessM4Tv2ForSpeechToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -3236,7 +3236,7 @@ class SeamlessM4Tv2ForSpeechToText(SeamlessM4Tv2PreTrainedModel, GenerationMixin synced_gpus (`bool`, *optional*, defaults to `False`): Whether to continue running the while loop until max_length (needed to avoid deadlocking with `FullyShardedDataParallel` and DeepSpeed ZeRO Stage 3). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. @@ -3376,8 +3376,8 @@ class SeamlessM4Tv2ForTextToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMixin attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3385,7 +3385,7 @@ class SeamlessM4Tv2ForTextToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMixin output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -3527,7 +3527,7 @@ class SeamlessM4Tv2ForTextToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMixin Returns: - `Union[SeamlessM4Tv2GenerationOutput, Tuple[Tensor]]`: + `Union[SeamlessM4Tv2GenerationOutput, tuple[Tensor]]`: - If `return_intermediate_token_ids`, returns [`SeamlessM4Tv2GenerationOutput`]. - If not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. @@ -3749,8 +3749,8 @@ class SeamlessM4Tv2ForSpeechToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMix attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -3759,7 +3759,7 @@ class SeamlessM4Tv2ForSpeechToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMix output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -3905,7 +3905,7 @@ class SeamlessM4Tv2ForSpeechToSpeech(SeamlessM4Tv2PreTrainedModel, GenerationMix Returns: - `Union[SeamlessM4Tv2GenerationOutput, Tuple[Tensor]]`: + `Union[SeamlessM4Tv2GenerationOutput, tuple[Tensor]]`: - If `return_intermediate_token_ids`, returns [`SeamlessM4Tv2GenerationOutput`]. - If not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. @@ -4159,8 +4159,8 @@ class SeamlessM4Tv2Model(SeamlessM4Tv2PreTrainedModel, GenerationMixin): attention_mask: Optional[torch.Tensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.LongTensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -4169,7 +4169,7 @@ class SeamlessM4Tv2Model(SeamlessM4Tv2PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Seq2SeqLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[Seq2SeqLMOutput, tuple[torch.FloatTensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -4363,7 +4363,7 @@ class SeamlessM4Tv2Model(SeamlessM4Tv2PreTrainedModel, GenerationMixin): other. Returns: - `Union[SeamlessM4Tv2GenerationOutput, Tuple[Tensor], ModelOutput]`: + `Union[SeamlessM4Tv2GenerationOutput, tuple[Tensor], ModelOutput]`: - If `generate_speech` and `return_intermediate_token_ids`, returns [`SeamlessM4Tv2GenerationOutput`]. - If `generate_speech` and not `return_intermediate_token_ids`, returns a tuple composed of waveforms of shape `(batch_size, sequence_length)` and `waveform_lengths` which gives the length of each sample. diff --git a/src/transformers/models/segformer/configuration_segformer.py b/src/transformers/models/segformer/configuration_segformer.py index 4e5a76db16b..f42e6539985 100644 --- a/src/transformers/models/segformer/configuration_segformer.py +++ b/src/transformers/models/segformer/configuration_segformer.py @@ -44,19 +44,19 @@ class SegformerConfig(PretrainedConfig): The number of input channels. num_encoder_blocks (`int`, *optional*, defaults to 4): The number of encoder blocks (i.e. stages in the Mix Transformer encoder). - depths (`List[int]`, *optional*, defaults to `[2, 2, 2, 2]`): + depths (`list[int]`, *optional*, defaults to `[2, 2, 2, 2]`): The number of layers in each encoder block. - sr_ratios (`List[int]`, *optional*, defaults to `[8, 4, 2, 1]`): + sr_ratios (`list[int]`, *optional*, defaults to `[8, 4, 2, 1]`): Sequence reduction ratios in each encoder block. - hidden_sizes (`List[int]`, *optional*, defaults to `[32, 64, 160, 256]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[32, 64, 160, 256]`): Dimension of each of the encoder blocks. - patch_sizes (`List[int]`, *optional*, defaults to `[7, 3, 3, 3]`): + patch_sizes (`list[int]`, *optional*, defaults to `[7, 3, 3, 3]`): Patch size before each encoder block. - strides (`List[int]`, *optional*, defaults to `[4, 2, 2, 2]`): + strides (`list[int]`, *optional*, defaults to `[4, 2, 2, 2]`): Stride before each encoder block. - num_attention_heads (`List[int]`, *optional*, defaults to `[1, 2, 5, 8]`): + num_attention_heads (`list[int]`, *optional*, defaults to `[1, 2, 5, 8]`): Number of attention heads for each attention layer in each block of the Transformer encoder. - mlp_ratios (`List[int]`, *optional*, defaults to `[4, 4, 4, 4]`): + mlp_ratios (`list[int]`, *optional*, defaults to `[4, 4, 4, 4]`): Ratio of the size of the hidden layer compared to the size of the input layer of the Mix FFNs in the encoder blocks. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`): diff --git a/src/transformers/models/segformer/image_processing_segformer.py b/src/transformers/models/segformer/image_processing_segformer.py index 84fec78a2ad..aa62ce1bc19 100644 --- a/src/transformers/models/segformer/image_processing_segformer.py +++ b/src/transformers/models/segformer/image_processing_segformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Segformer.""" -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -64,7 +64,7 @@ class SegformerImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `(size["height"], size["width"])`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 512, "width": 512}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 512, "width": 512}`): Size of the output image after resizing. Can be overridden by the `size` parameter in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): @@ -79,10 +79,10 @@ class SegformerImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_reduce_labels (`bool`, *optional*, defaults to `False`): @@ -99,13 +99,13 @@ class SegformerImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_reduce_labels: bool = False, **kwargs, ) -> None: @@ -123,7 +123,7 @@ class SegformerImageProcessor(BaseImageProcessor): self.do_reduce_labels = do_reduce_labels @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to save support of deprecated `reduce_labels` in old configs """ @@ -136,7 +136,7 @@ class SegformerImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -148,7 +148,7 @@ class SegformerImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -197,11 +197,11 @@ class SegformerImageProcessor(BaseImageProcessor): do_resize: bool, do_rescale: bool, do_normalize: bool, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, rescale_factor: Optional[float] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ): if do_reduce_labels: @@ -222,13 +222,13 @@ class SegformerImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -264,7 +264,7 @@ class SegformerImageProcessor(BaseImageProcessor): segmentation_map: ImageInput, do_reduce_labels: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: """Preprocesses a single mask.""" @@ -311,13 +311,13 @@ class SegformerImageProcessor(BaseImageProcessor): images: ImageInput, segmentation_maps: Optional[ImageInput] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_reduce_labels: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -334,7 +334,7 @@ class SegformerImageProcessor(BaseImageProcessor): Segmentation map to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after `resize` is applied. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only @@ -345,9 +345,9 @@ class SegformerImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 @@ -437,19 +437,19 @@ class SegformerImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) # Copied from transformers.models.beit.image_processing_beit.BeitImageProcessor.post_process_semantic_segmentation with Beit->Segformer - def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple]] = None): + def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[list[tuple]] = None): """ Converts the output of [`SegformerForSemanticSegmentation`] into semantic segmentation maps. Only supports PyTorch. Args: outputs ([`SegformerForSemanticSegmentation`]): Raw outputs of the model. - target_sizes (`List[Tuple]` of length `batch_size`, *optional*): + target_sizes (`list[Tuple]` of length `batch_size`, *optional*): List of tuples corresponding to the requested final size (height, width) of each prediction. If unset, predictions will not be resized. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/segformer/modeling_segformer.py b/src/transformers/models/segformer/modeling_segformer.py index f5e08b9303f..959601aa57a 100755 --- a/src/transformers/models/segformer/modeling_segformer.py +++ b/src/transformers/models/segformer/modeling_segformer.py @@ -15,7 +15,7 @@ """PyTorch SegFormer model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -56,8 +56,8 @@ class SegFormerImageClassifierOutput(ImageClassifierOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.beit.modeling_beit.drop_path @@ -389,7 +389,7 @@ class SegformerEncoder(nn.Module): output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -475,7 +475,7 @@ class SegformerModel(SegformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -527,7 +527,7 @@ class SegformerForImageClassification(SegformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SegFormerImageClassifierOutput]: + ) -> Union[tuple, SegFormerImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., @@ -686,7 +686,7 @@ class SegformerForSemanticSegmentation(SegformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SemanticSegmenterOutput]: + ) -> Union[tuple, SemanticSegmenterOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth semantic segmentation maps for computing the loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/segformer/modeling_tf_segformer.py b/src/transformers/models/segformer/modeling_tf_segformer.py index 3c084f237ea..e3301b4dfc6 100644 --- a/src/transformers/models/segformer/modeling_tf_segformer.py +++ b/src/transformers/models/segformer/modeling_tf_segformer.py @@ -17,7 +17,7 @@ from __future__ import annotations import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -90,7 +90,7 @@ class TFSegformerOverlapPatchEmbeddings(keras.layers.Layer): self.num_channels = num_channels self.hidden_size = hidden_size - def call(self, pixel_values: tf.Tensor) -> Tuple[tf.Tensor, int, int]: + def call(self, pixel_values: tf.Tensor) -> tuple[tf.Tensor, int, int]: embeddings = self.proj(self.padding(pixel_values)) height = shape_list(embeddings)[1] width = shape_list(embeddings)[2] @@ -169,7 +169,7 @@ class TFSegformerEfficientSelfAttention(keras.layers.Layer): width: int, output_attentions: bool = False, training: bool = False, - ) -> Union[tf.Tensor, Tuple[tf.Tensor, tf.Tensor]]: + ) -> Union[tf.Tensor, tuple[tf.Tensor, tf.Tensor]]: batch_size = shape_list(hidden_states)[0] num_channels = shape_list(hidden_states)[2] @@ -272,7 +272,7 @@ class TFSegformerAttention(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, height: int, width: int, output_attentions: bool = False - ) -> Union[tf.Tensor, Tuple[tf.Tensor, tf.Tensor]]: + ) -> Union[tf.Tensor, tuple[tf.Tensor, tf.Tensor]]: self_outputs = self.self(hidden_states, height, width, output_attentions) attention_output = self.dense_output(self_outputs[0]) @@ -401,7 +401,7 @@ class TFSegformerLayer(keras.layers.Layer): width: int, output_attentions: bool = False, training: bool = False, - ) -> Tuple: + ) -> tuple: self_attention_outputs = self.attention( self.layer_norm_1(hidden_states), # in Segformer, layernorm is applied before self-attention height, @@ -503,7 +503,7 @@ class TFSegformerEncoder(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -584,7 +584,7 @@ class TFSegformerMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -666,7 +666,7 @@ SEGFORMER_START_DOCSTRING = r""" SEGFORMER_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details. @@ -718,7 +718,7 @@ class TFSegformerModel(TFSegformerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: outputs = self.segformer( pixel_values, output_attentions=output_attentions, @@ -770,7 +770,7 @@ class TFSegformerForImageClassification(TFSegformerPreTrainedModel, TFSequenceCl output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFSequenceClassifierOutput]: + ) -> Union[tuple, TFSequenceClassifierOutput]: outputs = self.segformer( pixel_values, output_attentions=output_attentions, @@ -954,7 +954,7 @@ class TFSegformerForSemanticSegmentation(TFSegformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TFSemanticSegmenterOutput]: + ) -> Union[tuple, TFSemanticSegmenterOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, height, width)`, *optional*): Ground truth semantic segmentation maps for computing the loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/seggpt/configuration_seggpt.py b/src/transformers/models/seggpt/configuration_seggpt.py index a735281facc..dc149adfa03 100644 --- a/src/transformers/models/seggpt/configuration_seggpt.py +++ b/src/transformers/models/seggpt/configuration_seggpt.py @@ -47,7 +47,7 @@ class SegGptConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-06): The epsilon used by the layer normalization layers. - image_size (`List[int]`, *optional*, defaults to `[896, 448]`): + image_size (`list[int]`, *optional*, defaults to `[896, 448]`): The size (resolution) of each image. patch_size (`int`, *optional*, defaults to 16): The size (resolution) of each patch. @@ -68,7 +68,7 @@ class SegGptConfig(PretrainedConfig): Whether to use relative position embeddings in the attention layers. merge_index (`int`, *optional*, defaults to 2): The index of the encoder layer to merge the embeddings. - intermediate_hidden_state_indices (`List[int]`, *optional*, defaults to `[5, 11, 17, 23]`): + intermediate_hidden_state_indices (`list[int]`, *optional*, defaults to `[5, 11, 17, 23]`): The indices of the encoder layers which we store as features for the decoder. beta (`float`, *optional*, defaults to 0.01): Regularization factor for SegGptLoss (smooth-l1 loss). diff --git a/src/transformers/models/seggpt/image_processing_seggpt.py b/src/transformers/models/seggpt/image_processing_seggpt.py index 3ccb42ee7ab..6ad18447445 100644 --- a/src/transformers/models/seggpt/image_processing_seggpt.py +++ b/src/transformers/models/seggpt/image_processing_seggpt.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for SegGPT.""" -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -47,7 +47,7 @@ logger = logging.get_logger(__name__) # See https://huggingface.co/papers/2212.02499 at 3.1 Redefining Output Spaces as "Images" - Semantic Segmentation from PAINTER paper # Taken from https://github.com/Abdullah-Meda/Painter/blob/main/Painter/data/coco_semseg/gen_color_coco_panoptic_segm.py#L31 -def build_palette(num_labels: int) -> List[Tuple[int, int]]: +def build_palette(num_labels: int) -> list[tuple[int, int]]: base = int(num_labels ** (1 / 3)) + 1 margin = 256 // base @@ -68,7 +68,7 @@ def build_palette(num_labels: int) -> List[Tuple[int, int]]: def mask_to_rgb( - mask: np.ndarray, palette: Optional[List[Tuple[int, int]]] = None, data_format: Optional[ChannelDimension] = None + mask: np.ndarray, palette: Optional[list[tuple[int, int]]] = None, data_format: Optional[ChannelDimension] = None ) -> np.ndarray: data_format = data_format if data_format is not None else ChannelDimension.FIRST @@ -118,10 +118,10 @@ class SegGptImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_rgb (`bool`, *optional*, defaults to `True`): @@ -134,13 +134,13 @@ class SegGptImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -157,7 +157,7 @@ class SegGptImageProcessor(BaseImageProcessor): self.image_std = image_std if image_std is not None else IMAGENET_DEFAULT_STD self.do_convert_rgb = do_convert_rgb - def get_palette(self, num_labels: int) -> List[Tuple[int, int]]: + def get_palette(self, num_labels: int) -> list[tuple[int, int]]: """Build a palette to map the prompt mask from a single channel to a 3 channel RGB. Args: @@ -165,14 +165,14 @@ class SegGptImageProcessor(BaseImageProcessor): Number of classes in the segmentation task (excluding the background). Returns: - `List[Tuple[int, int]]`: Palette to map the prompt mask from a single channel to a 3 channel RGB. + `list[tuple[int, int]]`: Palette to map the prompt mask from a single channel to a 3 channel RGB. """ return build_palette(num_labels) def mask_to_rgb( self, image: np.ndarray, - palette: Optional[List[Tuple[int, int]]] = None, + palette: Optional[list[tuple[int, int]]] = None, data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: """Converts a segmentation map to RGB format. @@ -180,7 +180,7 @@ class SegGptImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Segmentation map with dimensions (height, width) where pixel values represent the class index. - palette (`List[Tuple[int, int]]`, *optional*, defaults to `None`): + palette (`list[tuple[int, int]]`, *optional*, defaults to `None`): Palette to use to convert the mask to RGB format. If unset, the mask is duplicated across the channel dimension. data_format (`ChannelDimension` or `str`, *optional*): @@ -198,7 +198,7 @@ class SegGptImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -210,7 +210,7 @@ class SegGptImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`. @@ -247,13 +247,13 @@ class SegGptImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, do_convert_rgb: Optional[bool] = None, @@ -269,7 +269,7 @@ class SegGptImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -281,9 +281,9 @@ class SegGptImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: @@ -394,13 +394,13 @@ class SegGptImageProcessor(BaseImageProcessor): prompt_images: Optional[ImageInput] = None, prompt_masks: Optional[ImageInput] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, num_labels: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -427,7 +427,7 @@ class SegGptImageProcessor(BaseImageProcessor): dimension. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -439,9 +439,9 @@ class SegGptImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): Whether to convert the prompt mask to RGB format. If `num_labels` is specified, a palette will be built @@ -540,7 +540,7 @@ class SegGptImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) def post_process_semantic_segmentation( - self, outputs, target_sizes: Optional[List[Tuple[int, int]]] = None, num_labels: Optional[int] = None + self, outputs, target_sizes: Optional[list[tuple[int, int]]] = None, num_labels: Optional[int] = None ): """ Converts the output of [`SegGptImageSegmentationOutput`] into segmentation maps. Only supports @@ -549,15 +549,15 @@ class SegGptImageProcessor(BaseImageProcessor): Args: outputs ([`SegGptImageSegmentationOutput`]): Raw outputs of the model. - target_sizes (`List[Tuple[int, int]]`, *optional*): - List of length (batch_size), where each list item (`Tuple[int, int]`) corresponds to the requested + target_sizes (`list[tuple[int, int]]`, *optional*): + List of length (batch_size), where each list item (`tuple[int, int]`) corresponds to the requested final size (height, width) of each prediction. If left to None, predictions will not be resized. num_labels (`int`, *optional*): Number of classes in the segmentation task (excluding the background). If specified, a palette will be built, assuming that class_idx 0 is the background, to map prediction masks from RGB values to class indices. This value should be the same used when preprocessing inputs. Returns: - semantic_segmentation: `List[torch.Tensor]` of length `batch_size`, where each item is a semantic + semantic_segmentation: `list[torch.Tensor]` of length `batch_size`, where each item is a semantic segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each `torch.Tensor` correspond to a semantic class id. """ diff --git a/src/transformers/models/seggpt/modeling_seggpt.py b/src/transformers/models/seggpt/modeling_seggpt.py index ff4a9084529..9a7d830476b 100644 --- a/src/transformers/models/seggpt/modeling_seggpt.py +++ b/src/transformers/models/seggpt/modeling_seggpt.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -39,22 +39,22 @@ class SegGptEncoderOutput(ModelOutput): Args: last_hidden_state (`torch.FloatTensor` of shape `(batch_size, patch_height, patch_width, hidden_size)`): Sequence of hidden-states at the output of the last layer of the model. - hidden_states (`Tuple[torch.FloatTensor]`, `optional`, returned when `config.output_hidden_states=True`): + hidden_states (`tuple[torch.FloatTensor]`, `optional`, returned when `config.output_hidden_states=True`): Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, patch_height, patch_width, hidden_size)`. - attentions (`Tuple[torch.FloatTensor]`, `optional`, returned when `config.output_attentions=True`): + attentions (`tuple[torch.FloatTensor]`, `optional`, returned when `config.output_attentions=True`): Tuple of *torch.FloatTensor* (one for each layer) of shape `(batch_size, num_heads, seq_len, seq_len)`. - intermediate_hidden_states (`Tuple[torch.FloatTensor]`, *optional*, returned when `config.intermediate_hidden_state_indices` is set): + intermediate_hidden_states (`tuple[torch.FloatTensor]`, *optional*, returned when `config.intermediate_hidden_state_indices` is set): Tuple of `torch.FloatTensor` of shape `(batch_size, patch_height, patch_width, hidden_size)`. Each element in the Tuple corresponds to the output of the layer specified in `config.intermediate_hidden_state_indices`. Additionally, each feature passes through a LayerNorm. """ last_hidden_state: torch.FloatTensor - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - intermediate_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + intermediate_hidden_states: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -67,18 +67,18 @@ class SegGptImageSegmentationOutput(ModelOutput): The loss value. pred_masks (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): The predicted masks. - hidden_states (`Tuple[torch.FloatTensor]`, `optional`, returned when `config.output_hidden_states=True`): + hidden_states (`tuple[torch.FloatTensor]`, `optional`, returned when `config.output_hidden_states=True`): Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, patch_height, patch_width, hidden_size)`. - attentions (`Tuple[torch.FloatTensor]`, `optional`, returned when `config.output_attentions=True`): + attentions (`tuple[torch.FloatTensor]`, `optional`, returned when `config.output_attentions=True`): Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, seq_len, seq_len)`. """ loss: Optional[torch.FloatTensor] = None pred_masks: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.sam.modeling_sam.SamPatchEmbeddings with Sam->SegGpt @@ -267,8 +267,8 @@ class SegGptAttention(nn.Module): query: torch.Tensor, rel_pos_h: torch.Tensor, rel_pos_w: torch.Tensor, - q_size: Tuple[int, int], - k_size: Tuple[int, int], + q_size: tuple[int, int], + k_size: tuple[int, int], ) -> torch.Tensor: """ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`. @@ -410,7 +410,7 @@ class SegGptLayer(nn.Module): ensemble_cond: int, feature_ensemble: bool = False, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in SegGpt, layernorm is applied before self-attention output_attentions=output_attentions, @@ -663,7 +663,7 @@ class SegGptModel(SegGptPreTrainedModel): def get_input_embeddings(self) -> SegGptPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -684,7 +684,7 @@ class SegGptModel(SegGptPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SegGptEncoderOutput]: + ) -> Union[tuple, SegGptEncoderOutput]: r""" prompt_pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Prompt pixel values. Prompt pixel values can be obtained using [`AutoImageProcessor`]. See @@ -880,7 +880,7 @@ class SegGptForImageSegmentation(SegGptPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SegGptImageSegmentationOutput]: + ) -> Union[tuple, SegGptImageSegmentationOutput]: r""" prompt_pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`): Prompt pixel values. Prompt pixel values can be obtained using [`AutoImageProcessor`]. See diff --git a/src/transformers/models/sew/configuration_sew.py b/src/transformers/models/sew/configuration_sew.py index b8cb8040392..f6cf7b85a15 100644 --- a/src/transformers/models/sew/configuration_sew.py +++ b/src/transformers/models/sew/configuration_sew.py @@ -76,13 +76,13 @@ class SEWConfig(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. diff --git a/src/transformers/models/sew/modeling_sew.py b/src/transformers/models/sew/modeling_sew.py index f68e9a20782..30949092bd0 100644 --- a/src/transformers/models/sew/modeling_sew.py +++ b/src/transformers/models/sew/modeling_sew.py @@ -21,7 +21,7 @@ import math import warnings -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -302,14 +302,14 @@ class SEWAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -627,7 +627,7 @@ class SEWPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -822,7 +822,7 @@ class SEWModel(SEWPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -963,7 +963,7 @@ class SEWForCTC(SEWPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1086,11 +1086,11 @@ class SEWForSequenceClassification(SEWPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`SEWProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/sew/modular_sew.py b/src/transformers/models/sew/modular_sew.py index 1f7d9f1b43c..0b151b05a2c 100644 --- a/src/transformers/models/sew/modular_sew.py +++ b/src/transformers/models/sew/modular_sew.py @@ -16,7 +16,7 @@ import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -412,7 +412,7 @@ class SEWModel(SEWPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict diff --git a/src/transformers/models/sew_d/configuration_sew_d.py b/src/transformers/models/sew_d/configuration_sew_d.py index c23b12f6acd..2809fa0fb6b 100644 --- a/src/transformers/models/sew_d/configuration_sew_d.py +++ b/src/transformers/models/sew_d/configuration_sew_d.py @@ -58,7 +58,7 @@ class SEWDConfig(PretrainedConfig): Whether to share attention key with c2p and p2c. relative_attention (`bool`, *optional*, defaults to `True`): Whether to use relative position encoding. - pos_att_type (`Tuple[str]`, *optional*, defaults to `("p2c", "c2p")`): + pos_att_type (`tuple[str]`, *optional*, defaults to `("p2c", "c2p")`): The type of relative position attention, it can be a combination of `("p2c", "c2p")`, e.g. `("p2c")`, `("p2c", "c2p")`, `("p2c", "c2p")`. norm_rel_ebd (`str`, *optional*, defaults to `"layer_norm"`): @@ -89,13 +89,13 @@ class SEWDConfig(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. diff --git a/src/transformers/models/sew_d/modeling_sew_d.py b/src/transformers/models/sew_d/modeling_sew_d.py index 92da0e0935c..f2d682884c4 100644 --- a/src/transformers/models/sew_d/modeling_sew_d.py +++ b/src/transformers/models/sew_d/modeling_sew_d.py @@ -17,7 +17,7 @@ import math import warnings from collections.abc import Sequence -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -41,7 +41,7 @@ _HIDDEN_STATES_START_POSITION = 1 # Copied from transformers.models.wav2vec2.modeling_wav2vec2._compute_mask_indices def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1346,7 +1346,7 @@ class SEWDModel(SEWDPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1485,7 +1485,7 @@ class SEWDForCTC(SEWDPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1609,11 +1609,11 @@ class SEWDForSequenceClassification(SEWDPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`SEWDProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/shieldgemma2/modeling_shieldgemma2.py b/src/transformers/models/shieldgemma2/modeling_shieldgemma2.py index a9b897e2dfb..a164e61420a 100644 --- a/src/transformers/models/shieldgemma2/modeling_shieldgemma2.py +++ b/src/transformers/models/shieldgemma2/modeling_shieldgemma2.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -80,7 +80,7 @@ class ShieldGemma2ForImageClassification(PreTrainedModel): pixel_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[List[torch.FloatTensor], Cache]] = None, + past_key_values: Optional[Union[list[torch.FloatTensor], Cache]] = None, token_type_ids: Optional[torch.LongTensor] = None, cache_position: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, diff --git a/src/transformers/models/siglip/convert_siglip_to_hf.py b/src/transformers/models/siglip/convert_siglip_to_hf.py index b61bd7ffb70..910bf5c2213 100644 --- a/src/transformers/models/siglip/convert_siglip_to_hf.py +++ b/src/transformers/models/siglip/convert_siglip_to_hf.py @@ -20,7 +20,6 @@ URL: https://github.com/google-research/big_vision/tree/main import argparse import collections import os -from typing import Tuple import numpy as np import requests @@ -143,7 +142,7 @@ def get_vocab_file_from_model_name(model_name: str) -> str: return vocab_file -def get_text_and_vision_vit_variants(model_name: str) -> Tuple[str, str]: +def get_text_and_vision_vit_variants(model_name: str) -> tuple[str, str]: variant = model_name.split("-")[1] if "giant-opt" not in model_name else "giant-opt" return { "base": ("base", "base"), diff --git a/src/transformers/models/siglip/image_processing_siglip.py b/src/transformers/models/siglip/image_processing_siglip.py index ae9dabb3ed9..bde8913fc13 100644 --- a/src/transformers/models/siglip/image_processing_siglip.py +++ b/src/transformers/models/siglip/image_processing_siglip.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for SigLIP.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict from ...image_transforms import ( @@ -53,7 +53,7 @@ class SiglipImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after resizing. Can be overridden by `size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`): Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. @@ -66,10 +66,10 @@ class SiglipImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image by the specified mean and standard deviation. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -82,13 +82,13 @@ class SiglipImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, **kwargs, ) -> None: @@ -112,13 +112,13 @@ class SiglipImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -133,7 +133,7 @@ class SiglipImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -144,9 +144,9 @@ class SiglipImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/siglip/modeling_siglip.py b/src/transformers/models/siglip/modeling_siglip.py index fa5e3f6dc84..55a06643b72 100644 --- a/src/transformers/models/siglip/modeling_siglip.py +++ b/src/transformers/models/siglip/modeling_siglip.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np import torch @@ -159,8 +159,8 @@ class SiglipVisionModelOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -189,8 +189,8 @@ class SiglipTextModelOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -224,7 +224,7 @@ class SiglipOutput(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -395,7 +395,7 @@ class SiglipAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -468,7 +468,7 @@ class SiglipEncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): diff --git a/src/transformers/models/siglip/processing_siglip.py b/src/transformers/models/siglip/processing_siglip.py index 0d66fb9d5f9..21bdacc3a5f 100644 --- a/src/transformers/models/siglip/processing_siglip.py +++ b/src/transformers/models/siglip/processing_siglip.py @@ -16,7 +16,7 @@ Image/Text processor class for SigLIP. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput @@ -48,7 +48,7 @@ class SiglipProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, images: ImageInput = None, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -63,11 +63,11 @@ class SiglipProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`): diff --git a/src/transformers/models/siglip/tokenization_siglip.py b/src/transformers/models/siglip/tokenization_siglip.py index e1e62dcd049..7a82a7a411a 100644 --- a/src/transformers/models/siglip/tokenization_siglip.py +++ b/src/transformers/models/siglip/tokenization_siglip.py @@ -19,7 +19,7 @@ import re import string import warnings from shutil import copyfile -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Optional import sentencepiece as spm @@ -61,7 +61,7 @@ class SiglipTokenizer(PreTrainedTokenizer): token instead. pad_token (`str`, *optional*, defaults to `""`): The token used for padding, for example when batching sequences of different lengths. - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -94,7 +94,7 @@ class SiglipTokenizer(PreTrainedTokenizer): unk_token="", pad_token="", additional_special_tokens=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, model_max_length=64, do_lower_case=True, **kwargs, @@ -162,22 +162,22 @@ class SiglipTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -190,7 +190,7 @@ class SiglipTokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer._add_eos_if_not_present - def _add_eos_if_not_present(self, token_ids: List[int]) -> List[int]: + def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]: """Do not add eos again if user already added it.""" if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( @@ -203,20 +203,20 @@ class SiglipTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] @@ -226,8 +226,8 @@ class SiglipTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -236,13 +236,13 @@ class SiglipTokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = self._add_eos_if_not_present(token_ids_0) if token_ids_1 is None: @@ -293,7 +293,7 @@ class SiglipTokenizer(PreTrainedTokenizer): return text - def tokenize(self, text: "TextInput", add_special_tokens=False, **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", add_special_tokens=False, **kwargs) -> list[str]: """ Converts a string to a list of tokens. """ @@ -359,7 +359,7 @@ class SiglipTokenizer(PreTrainedTokenizer): return out_string.strip() # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/siglip2/image_processing_siglip2.py b/src/transformers/models/siglip2/image_processing_siglip2.py index 53d53797f2c..54dc1360226 100644 --- a/src/transformers/models/siglip2/image_processing_siglip2.py +++ b/src/transformers/models/siglip2/image_processing_siglip2.py @@ -16,7 +16,7 @@ import math from functools import lru_cache -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -50,7 +50,7 @@ if is_vision_available(): @lru_cache(maxsize=256) def get_image_size_for_max_num_patches( image_height: int, image_width: int, patch_size: int, max_num_patches: int, eps: float = 1e-5 -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Determine image size based on max number of patches, ensure dimensions are divisible by patch size and image is at least 1 patch. @@ -109,7 +109,7 @@ def convert_image_to_patches(image: np.ndarray, patch_size: int) -> np.ndarray: return patched_image -def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = 0) -> Tuple[np.ndarray, np.ndarray]: +def pad_along_first_dim(array: np.ndarray, target_length: int, pad_value: int = 0) -> tuple[np.ndarray, np.ndarray]: """ Pad the array along the first dimension. """ @@ -142,10 +142,10 @@ class Siglip2ImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image by the specified mean and standard deviation. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.5, 0.5, 0.5]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -167,8 +167,8 @@ class Siglip2ImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, patch_size: int = 16, max_num_patches: int = 256, @@ -199,8 +199,8 @@ class Siglip2ImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, do_convert_rgb: Optional[bool] = None, @@ -216,7 +216,7 @@ class Siglip2ImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -227,9 +227,9 @@ class Siglip2ImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): diff --git a/src/transformers/models/siglip2/image_processing_siglip2_fast.py b/src/transformers/models/siglip2/image_processing_siglip2_fast.py index 191de2f9611..bbab9196196 100644 --- a/src/transformers/models/siglip2/image_processing_siglip2_fast.py +++ b/src/transformers/models/siglip2/image_processing_siglip2_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for SigLIP2.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch @@ -69,7 +69,7 @@ def convert_image_to_patches(image: "torch.Tensor", patch_size: int) -> "torch.T def pad_along_first_dim( tensor: "torch.Tensor", target_length: int, pad_value: int = 0 -) -> Tuple["torch.Tensor", "torch.Tensor"]: +) -> tuple["torch.Tensor", "torch.Tensor"]: """ Pad the tensor along the first dimension. """ @@ -123,7 +123,7 @@ class Siglip2ImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_resize: bool, patch_size: int, max_num_patches: int, @@ -131,8 +131,8 @@ class Siglip2ImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], **kwargs, ) -> BatchFeature: diff --git a/src/transformers/models/siglip2/modeling_siglip2.py b/src/transformers/models/siglip2/modeling_siglip2.py index eb3bf5d4a34..26b18b18f65 100644 --- a/src/transformers/models/siglip2/modeling_siglip2.py +++ b/src/transformers/models/siglip2/modeling_siglip2.py @@ -21,7 +21,7 @@ import math import warnings from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np import torch @@ -67,8 +67,8 @@ class Siglip2VisionOutput(ModelOutput): image_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -96,8 +96,8 @@ class Siglip2TextOutput(ModelOutput): text_embeds: Optional[torch.FloatTensor] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -130,7 +130,7 @@ class Siglip2Output(ModelOutput): text_model_output: BaseModelOutputWithPooling = None vision_model_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output"] else getattr(self, k).to_tuple() for k in self.keys() @@ -217,7 +217,7 @@ class Siglip2VisionEmbeddings(nn.Module): Args: pixel_values (`torch.FloatTensor`): Pixel values of shape (batch_size, max_num_patches, num_channels * patch_size * patch_size) - spatial_shapes (`List[Tuple[int, int]]`): + spatial_shapes (`list[tuple[int, int]]`): Spatial shapes of shape (batch_size, 2) to resize the positional embeddings to """ @@ -289,7 +289,7 @@ class Siglip2Attention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -361,7 +361,7 @@ class Siglip2EncoderLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): diff --git a/src/transformers/models/siglip2/modular_siglip2.py b/src/transformers/models/siglip2/modular_siglip2.py index f9c46122b54..803c1b070b3 100644 --- a/src/transformers/models/siglip2/modular_siglip2.py +++ b/src/transformers/models/siglip2/modular_siglip2.py @@ -209,7 +209,7 @@ class Siglip2VisionEmbeddings(nn.Module): Args: pixel_values (`torch.FloatTensor`): Pixel values of shape (batch_size, max_num_patches, num_channels * patch_size * patch_size) - spatial_shapes (`List[Tuple[int, int]]`): + spatial_shapes (`list[tuple[int, int]]`): Spatial shapes of shape (batch_size, 2) to resize the positional embeddings to """ diff --git a/src/transformers/models/siglip2/processing_siglip2.py b/src/transformers/models/siglip2/processing_siglip2.py index 16e0ea1b6b8..4c2c83d9e83 100644 --- a/src/transformers/models/siglip2/processing_siglip2.py +++ b/src/transformers/models/siglip2/processing_siglip2.py @@ -16,7 +16,7 @@ Image/Text processor class for SigLIP2. """ -from typing import List, Optional, Union +from typing import Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput @@ -69,8 +69,8 @@ class Siglip2Processor(ProcessorMixin): def __call__( self, - images: Optional[Union[ImageInput, List[ImageInput], List[List[ImageInput]]]] = None, - text: Optional[Union[TextInput, "PreTokenizedInput", List[TextInput], List["PreTokenizedInput"]]] = None, + images: Optional[Union[ImageInput, list[ImageInput], list[list[ImageInput]]]] = None, + text: Optional[Union[TextInput, "PreTokenizedInput", list[TextInput], list["PreTokenizedInput"]]] = None, audio=None, videos=None, **kwargs: Unpack[Siglip2ProcessorKwargs], @@ -83,10 +83,10 @@ class Siglip2Processor(ProcessorMixin): of the above two methods for more information. Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/smolvlm/image_processing_smolvlm.py b/src/transformers/models/smolvlm/image_processing_smolvlm.py index 37db39540b2..5226f544cf5 100644 --- a/src/transformers/models/smolvlm/image_processing_smolvlm.py +++ b/src/transformers/models/smolvlm/image_processing_smolvlm.py @@ -21,7 +21,7 @@ # limitations under the License. import math from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -55,7 +55,7 @@ MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum def _resize_output_size_rescale_to_max_len( height: int, width: int, min_len: Optional[int] = 1, max_len: Optional[int] = None -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -91,8 +91,8 @@ def _resize_output_size_rescale_to_max_len( def _resize_output_size_scale_below_upper_bound( - height: int, width: int, max_len: Optional[Dict[str, int]] = None -) -> Tuple[int, int]: + height: int, width: int, max_len: Optional[dict[str, int]] = None +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -100,7 +100,7 @@ def _resize_output_size_scale_below_upper_bound( Height of the input image. width (`int`): Width of the input image. - max_len (`Dict[str, int]`, *optional*, defaults to the maximum size of the image): + max_len (`dict[str, int]`, *optional*, defaults to the maximum size of the image): Defines the maximum dimensions of the image. Returns: The output size of the image after resizing. @@ -125,7 +125,7 @@ def get_resize_output_image_size( image, resolution_max_side: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Get the output size of the image after resizing given a dictionary specifying the max and min sizes. Args: @@ -149,8 +149,8 @@ def get_resize_output_image_size( def get_max_height_width( - images_list: List[List[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images_list: list[list[np.ndarray]], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -167,14 +167,14 @@ def get_max_height_width( def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -194,7 +194,7 @@ def convert_to_rgb( Args: image (`np.ndarray`): The image to convert. - palette (List[int], *optional*): + palette (list[int], *optional*): The palette to use if given. data_format (ChannelDimension or str, *optional*): The channel dimension format for the output image. If not provided, it will be the same as the input image. @@ -274,11 +274,11 @@ class SmolVLMImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. If set to `True`, the image is normalized to have a mean of `image_mean` and a standard deviation of `image_std`. - image_mean (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -293,15 +293,15 @@ class SmolVLMImageProcessor(BaseImageProcessor): self, do_convert_rgb: bool = True, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.LANCZOS, do_image_splitting: bool = True, - max_image_size: Optional[Dict[str, int]] = None, + max_image_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, **kwargs, ) -> None: @@ -322,7 +322,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.LANCZOS, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -334,7 +334,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): Resampling filter to use when resizing the image. @@ -379,7 +379,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): def split_image( self, image, - max_image_size: Dict[str, int], + max_image_size: dict[str, int], resample: PILImageResampling = PILImageResampling.LANCZOS, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -395,7 +395,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Images to split. - max_image_size (`Dict[str, int]`): + max_image_size (`dict[str, int]`): Maximum size of the output image. If the image is larger than this size, it will be split into patches of this size, and the original image will be concatenated with the patches, resized to max_size. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`): @@ -497,7 +497,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -523,7 +523,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -534,7 +534,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): For a list of images, for each images, pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width. For each sample in the batch, pads the sample with empty images to the max_number of images per sample in the batch. Optionally returns a pixel mask. Args: - images (`List[np.ndarray]`): + images (`list[np.ndarray]`): List of list of images to pad. Pads to the largest height and width in the batch. constant_values (`float` or `Iterable[float]`, *optional*): The value to use for the padding if `mode` is `"constant"`. @@ -602,15 +602,15 @@ class SmolVLMImageProcessor(BaseImageProcessor): images: ImageInput, do_convert_rgb: Optional[bool] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_image_splitting: Optional[bool] = None, do_rescale: Optional[bool] = None, - max_image_size: Optional[Dict[str, int]] = None, + max_image_size: Optional[dict[str, int]] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, return_row_col_info: bool = False, @@ -626,7 +626,7 @@ class SmolVLMImageProcessor(BaseImageProcessor): Whether to convert the image to RGB. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. With the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only @@ -642,9 +642,9 @@ class SmolVLMImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): diff --git a/src/transformers/models/smolvlm/modeling_smolvlm.py b/src/transformers/models/smolvlm/modeling_smolvlm.py index 090cbd36377..383450aae1f 100644 --- a/src/transformers/models/smolvlm/modeling_smolvlm.py +++ b/src/transformers/models/smolvlm/modeling_smolvlm.py @@ -20,7 +20,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -181,7 +181,7 @@ class SmolVLMVisionAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -253,7 +253,7 @@ class SmolVLMEncoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): @@ -310,7 +310,7 @@ class SmolVLMEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -409,7 +409,7 @@ class SmolVLMVisionTransformer(SmolVLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -493,10 +493,10 @@ class SmolVLMBaseModelOutputWithPast(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None class SmolVLMSimpleMLP(nn.Module): @@ -695,7 +695,7 @@ class SmolVLMModel(SmolVLMPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -706,7 +706,7 @@ class SmolVLMModel(SmolVLMPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, SmolVLMBaseModelOutputWithPast]: + ) -> Union[tuple, SmolVLMBaseModelOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. @@ -817,10 +817,10 @@ class SmolVLMCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - image_hidden_states: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + image_hidden_states: Optional[tuple[torch.FloatTensor]] = None class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ... @@ -884,7 +884,7 @@ class SmolVLMForConditionalGeneration(SmolVLMPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -897,7 +897,7 @@ class SmolVLMForConditionalGeneration(SmolVLMPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, SmolVLMCausalLMOutputWithPast]: + ) -> Union[tuple, SmolVLMCausalLMOutputWithPast]: r""" pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*): Mask to avoid performing attention on padding pixel indices. diff --git a/src/transformers/models/smolvlm/modular_smolvlm.py b/src/transformers/models/smolvlm/modular_smolvlm.py index c26bd23e76b..32fb122563f 100644 --- a/src/transformers/models/smolvlm/modular_smolvlm.py +++ b/src/transformers/models/smolvlm/modular_smolvlm.py @@ -13,7 +13,7 @@ # 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. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -258,7 +258,7 @@ class SmolVLMModel(Idefics3Model): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, pixel_attention_mask: Optional[torch.BoolTensor] = None, @@ -269,7 +269,7 @@ class SmolVLMModel(Idefics3Model): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, SmolVLMBaseModelOutputWithPast]: + ) -> Union[tuple, SmolVLMBaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/smolvlm/processing_smolvlm.py b/src/transformers/models/smolvlm/processing_smolvlm.py index 8613a2f88c6..ada719a70e0 100644 --- a/src/transformers/models/smolvlm/processing_smolvlm.py +++ b/src/transformers/models/smolvlm/processing_smolvlm.py @@ -17,7 +17,7 @@ Processor class for SmolVLM. """ from datetime import timedelta -from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Optional, Union from ...feature_extraction_utils import BatchFeature from ...image_utils import ImageInput, make_nested_list_of_images @@ -105,7 +105,7 @@ def get_image_prompt_string( class SmolVLMImagesKwargs(ImagesKwargs, total=False): return_row_col_info: Optional[bool] - max_image_size: Optional[Dict[str, int]] + max_image_size: Optional[dict[str, int]] class SmolVLMProcessorKwargs(ProcessingKwargs, total=False): @@ -264,8 +264,8 @@ class SmolVLMProcessor(ProcessorMixin): def __call__( self, - images: Union[ImageInput, List[ImageInput], List[List[ImageInput]]] = None, - text: Union[TextInput, "PreTokenizedInput", List[TextInput], List["PreTokenizedInput"]] = None, + images: Union[ImageInput, list[ImageInput], list[list[ImageInput]]] = None, + text: Union[TextInput, "PreTokenizedInput", list[TextInput], list["PreTokenizedInput"]] = None, audio=None, videos: VideoInput = None, **kwargs: Unpack[SmolVLMProcessorKwargs], @@ -301,18 +301,18 @@ class SmolVLMProcessor(ProcessorMixin): ``` Args: - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, *optional*): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`, *optional*): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch - tensor. If is of type `List[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. - text (`Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]`, *optional*): + tensor. If is of type `list[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. + text (`Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). Wherever an image token, `` is encountered it is expanded to `` + `` + `` * `image_seq_len` * `. - videos (`List[PIL.Image.Image]`, `np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`, *optional*): + videos (`list[PIL.Image.Image]`, `np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`, *optional*): The video or batch of videos to be prepared. Each video can be a list of PIL frames, NumPy array or PyTorch - tensor. If is of type `List[VideoInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. + tensor. If is of type `list[VideoInput]`, it's assumed that this is for a single prompt i.e. of batch size 1. return_tensors (`Union[str, TensorType]`, *optional*): If set, will return tensors of a particular framework. See [`PreTrainedTokenizerFast.__call__`] for more information. @@ -412,7 +412,7 @@ class SmolVLMProcessor(ProcessorMixin): ] Args: - conversation (`Union[List[Dict, [str, str]], List[List[Dict[str, str]]]]`): + conversation (`Union[list[Dict, [str, str]], list[list[dict[str, str]]]]`): The conversation to format. chat_template (`Optional[str]`, *optional*): The Jinja template to use for formatting the conversation. If not provided, the tokenizer's diff --git a/src/transformers/models/smolvlm/video_processing_smolvlm.py b/src/transformers/models/smolvlm/video_processing_smolvlm.py index dbfc94ba2f8..d65b8affea8 100644 --- a/src/transformers/models/smolvlm/video_processing_smolvlm.py +++ b/src/transformers/models/smolvlm/video_processing_smolvlm.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -68,7 +68,7 @@ FRAME_TIMESTAMP_MESSAGE = "\nFrame from {timestamp}:" MAX_IMAGE_SIZE = 4096 # 4k resolution as absolute maximum -def get_max_height_width(videos: list["torch.Tensor"]) -> List[int]: +def get_max_height_width(videos: list["torch.Tensor"]) -> list[int]: """ Get the maximum height and width across all videos in a batch. """ @@ -213,7 +213,7 @@ class SmolVLMVideoProcessor(BaseVideoProcessor): Args: video (`torch.Tensor`): Video to pad. - padded_size (`Tuple[int, int]`): + padded_size (`tuple[int, int]`): Height and width to pad. max_num_frames (`int`): The maximum number of frames to which video will be padded. @@ -315,8 +315,8 @@ class SmolVLMVideoProcessor(BaseVideoProcessor): def _preprocess( self, - videos: List["torch.Tensor"], - video_metadata: Union[List[VideoMetadata], List[dict]], + videos: list["torch.Tensor"], + video_metadata: Union[list[VideoMetadata], list[dict]], do_convert_rgb: bool, do_resize: bool, size: SizeDict, @@ -326,8 +326,8 @@ class SmolVLMVideoProcessor(BaseVideoProcessor): do_normalize: bool, do_pad: bool, do_sample_frames: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], fps: Optional[int] = None, num_frames: Optional[int] = None, skip_secs: Optional[int] = 0, diff --git a/src/transformers/models/speech_encoder_decoder/modeling_flax_speech_encoder_decoder.py b/src/transformers/models/speech_encoder_decoder/modeling_flax_speech_encoder_decoder.py index 97325f2d661..d2d23d29007 100644 --- a/src/transformers/models/speech_encoder_decoder/modeling_flax_speech_encoder_decoder.py +++ b/src/transformers/models/speech_encoder_decoder/modeling_flax_speech_encoder_decoder.py @@ -15,7 +15,7 @@ """Classes to support Flax Speech-Encoder-Decoder architectures""" import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import flax.linen as nn import jax @@ -86,7 +86,7 @@ SPEECH_ENCODER_DECODER_INPUTS_DOCSTRING = r""" Args: inputs (`jnp.ndarray` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, feature_dim)`, *optional*): Float values of input raw speech waveform or speech features. Values can be obtained by loading a `.flac` - or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile + or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `inputs`, either the [`Wav2Vec2Processor`] or [`Speech2TextProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -128,7 +128,7 @@ SPEECH_ENCODER_DECODER_ENCODE_INPUTS_DOCSTRING = r""" Args: inputs (`jnp.ndarray` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, feature_dim)`, *optional*): Float values of input raw speech waveform or speech features. Values can be obtained by loading a *.flac* - or *.wav* audio file into an array of type *List[float]* or a *numpy.ndarray*, *e.g.* via the soundfile + or *.wav* audio file into an array of type *list[float]* or a *numpy.ndarray*, *e.g.* via the soundfile library (*pip install soundfile*). To prepare the array into *inputs*, either the [`Wav2Vec2Processor`] or [`Speech2TextProcessor`] should be used for padding and conversion into a tensor of type *torch.FloatTensor*. @@ -182,7 +182,7 @@ SPEECH_ENCODER_DECODER_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.decoder.max_position_embeddings - 1]`. - past_key_values (`Dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, np.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -341,7 +341,7 @@ class FlaxSpeechEncoderDecoderModel(FlaxPreTrainedModel): def __init__( self, config: SpeechEncoderDecoderConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -374,7 +374,7 @@ class FlaxSpeechEncoderDecoderModel(FlaxPreTrainedModel): super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: encoder_input_shape, decoder_input_shape = input_shape # init input DeviceArrays diff --git a/src/transformers/models/speech_encoder_decoder/modeling_speech_encoder_decoder.py b/src/transformers/models/speech_encoder_decoder/modeling_speech_encoder_decoder.py index db131a2cbfe..b4582949fe6 100644 --- a/src/transformers/models/speech_encoder_decoder/modeling_speech_encoder_decoder.py +++ b/src/transformers/models/speech_encoder_decoder/modeling_speech_encoder_decoder.py @@ -14,7 +14,7 @@ # limitations under the License. """Classes to support Speech-Encoder-Text-Decoder architectures""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -324,8 +324,8 @@ class SpeechEncoderDecoderModel(PreTrainedModel, GenerationMixin): attention_mask: Optional[torch.FloatTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -335,11 +335,11 @@ class SpeechEncoderDecoderModel(PreTrainedModel, GenerationMixin): input_features: Optional[torch.FloatTensor] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" inputs (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, feature_dim)`, *optional*): Float values of input raw speech waveform or speech features. Values can be obtained by loading a `.flac` - or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile + or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `inputs`, either the [`Wav2Vec2Processor`] or [`Speech2TextProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. @@ -369,12 +369,12 @@ class SpeechEncoderDecoderModel(PreTrainedModel, GenerationMixin): (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]` input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*): Float values of input raw speech waveform. Values can be obtained by loading a *.flac* or *.wav* audio file - into an array of type *List[float]* or a *numpy.ndarray*, *e.g.* via the soundfile library (*pip install + into an array of type *list[float]* or a *numpy.ndarray*, *e.g.* via the soundfile library (*pip install soundfile*). To prepare the array into *input_values*, the [`Wav2Vec2Processor`] should be used for padding and conversion into a tensor of type *torch.FloatTensor*. See [`Wav2Vec2Processor.__call__`] for details. input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, feature_size)`, *optional*): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be obtained - by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* + by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`Speech2TextFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~Speech2TextFeatureExtractor.__call__`] diff --git a/src/transformers/models/speech_to_text/configuration_speech_to_text.py b/src/transformers/models/speech_to_text/configuration_speech_to_text.py index 27a0995a0be..a08d4fddfd4 100644 --- a/src/transformers/models/speech_to_text/configuration_speech_to_text.py +++ b/src/transformers/models/speech_to_text/configuration_speech_to_text.py @@ -88,7 +88,7 @@ class Speech2TextConfig(PretrainedConfig): just in case (e.g., 512 or 1024 or 2048). num_conv_layers (`int`, *optional*, defaults to 2): Number of 1D convolutional layers in the conv module. - conv_kernel_sizes (`Tuple[int]`, *optional*, defaults to `(5, 5)`): + conv_kernel_sizes (`tuple[int]`, *optional*, defaults to `(5, 5)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the conv module. The length of `conv_kernel_sizes` has to match `num_conv_layers`. conv_channels (`int`, *optional*, defaults to 1024): diff --git a/src/transformers/models/speech_to_text/feature_extraction_speech_to_text.py b/src/transformers/models/speech_to_text/feature_extraction_speech_to_text.py index 3abbfacb8de..64627033671 100644 --- a/src/transformers/models/speech_to_text/feature_extraction_speech_to_text.py +++ b/src/transformers/models/speech_to_text/feature_extraction_speech_to_text.py @@ -16,7 +16,7 @@ Feature extractor class for Speech2Text """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -166,8 +166,8 @@ class Speech2TextFeatureExtractor(SequenceFeatureExtractor): return x def normalize( - self, input_features: List[np.ndarray], attention_mask: Optional[np.ndarray] = None - ) -> List[np.ndarray]: + self, input_features: list[np.ndarray], attention_mask: Optional[np.ndarray] = None + ) -> list[np.ndarray]: lengths = attention_mask.sum(-1) if attention_mask is not None else [x.shape[0] for x in input_features] return [ self.utterance_cmvn(x, n, self.normalize_means, self.normalize_vars, self.padding_value) @@ -176,7 +176,7 @@ class Speech2TextFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, truncation: bool = False, @@ -190,7 +190,7 @@ class Speech2TextFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. diff --git a/src/transformers/models/speech_to_text/modeling_speech_to_text.py b/src/transformers/models/speech_to_text/modeling_speech_to_text.py index b1c07dba9e6..d9fbf2faec9 100755 --- a/src/transformers/models/speech_to_text/modeling_speech_to_text.py +++ b/src/transformers/models/speech_to_text/modeling_speech_to_text.py @@ -15,7 +15,7 @@ """PyTorch Speech2Text model.""" import math -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -243,14 +243,14 @@ class Speech2TextAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -437,7 +437,7 @@ class Speech2TextDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -618,7 +618,7 @@ class Speech2TextEncoder(Speech2TextPreTrainedModel): Args: input_features (`torch.LongTensor` of shape `(batch_size, sequence_length, feature_size)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `torch.FloatTensor`. See @@ -1109,18 +1109,18 @@ class Speech2TextModel(Speech2TextPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, feature_size)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be obtained - by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* + by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~Speech2TextFeatureExtractor.__call__`] @@ -1270,19 +1270,19 @@ class Speech2TextForConditionalGeneration(Speech2TextPreTrainedModel, Generation head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, feature_size)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be obtained - by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* + by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~Speech2TextFeatureExtractor.__call__`] diff --git a/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py b/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py index 33fe9314866..fad6a026b15 100755 --- a/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py +++ b/src/transformers/models/speech_to_text/modeling_tf_speech_to_text.py @@ -17,7 +17,7 @@ from __future__ import annotations import random -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -268,11 +268,11 @@ class TFSpeech2TextAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -514,9 +514,9 @@ class TFSpeech2TextDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training=False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -691,7 +691,7 @@ SPEECH_TO_TEXT_INPUTS_DOCSTRING = r""" Args: input_features (`tf.Tensor` of shape `(batch_size, sequence_length, feature_size)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be obtained - by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* + by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of floats. See [`~Speech2TextFeatureExtractor.__call__`] @@ -740,7 +740,7 @@ SPEECH_TO_TEXT_INPUTS_DOCSTRING = r""" encoder_outputs (`tf.FloatTensor`, *optional*): hidden states at the output of the last layer of the encoder. Used in the cross-attention of the decoder. of shape `(batch_size, sequence_length, hidden_size)` is a sequence of - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -847,7 +847,7 @@ class TFSpeech2TextEncoder(keras.layers.Layer): Args: input_features (`tf.Tensor` of shape `(batch_size, sequence_length, feature_size)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of floats. See [`~Speech2TextFeatureExtractor.__call__`] @@ -1052,7 +1052,7 @@ class TFSpeech2TextDecoder(keras.layers.Layer): - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers` with each tuple having 2 tuples each of which has 2 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`): Contains precomputed key and value hidden-states of the attention blocks. Can be used to speed up decoding. @@ -1346,7 +1346,7 @@ class TFSpeech2TextModel(TFSpeech2TextPreTrainedModel): decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -1354,7 +1354,7 @@ class TFSpeech2TextModel(TFSpeech2TextPreTrainedModel): return_dict: Optional[bool] = None, training: bool = False, **kwargs, - ) -> Union[Tuple, TFSeq2SeqModelOutput]: + ) -> Union[tuple, TFSeq2SeqModelOutput]: outputs = self.model( input_features=input_features, attention_mask=attention_mask, @@ -1445,7 +1445,7 @@ class TFSpeech2TextForConditionalGeneration(TFSpeech2TextPreTrainedModel, TFCaus decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, labels: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1454,7 +1454,7 @@ class TFSpeech2TextForConditionalGeneration(TFSpeech2TextPreTrainedModel, TFCaus return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs, - ) -> Union[Tuple, TFSeq2SeqLMOutput]: + ) -> Union[tuple, TFSeq2SeqLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., diff --git a/src/transformers/models/speech_to_text/tokenization_speech_to_text.py b/src/transformers/models/speech_to_text/tokenization_speech_to_text.py index abb6dd5da7a..e1e694cb958 100644 --- a/src/transformers/models/speech_to_text/tokenization_speech_to_text.py +++ b/src/transformers/models/speech_to_text/tokenization_speech_to_text.py @@ -18,7 +18,7 @@ import json import os from pathlib import Path from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece @@ -97,7 +97,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): vocab_files_names = VOCAB_FILES_NAMES model_input_names = ["input_ids", "attention_mask"] - prefix_tokens: List[int] = [] + prefix_tokens: list[int] = [] def __init__( self, @@ -112,7 +112,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): tgt_lang=None, lang_codes=None, additional_special_tokens=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -158,7 +158,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.encoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: vocab = self.encoder.copy() vocab.update(self.added_tokens_encoder) return vocab @@ -177,7 +177,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): lang_code_id = self.lang_code_to_id[tgt_lang] self.prefix_tokens = [lang_code_id] - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -187,7 +187,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): """Converts an index (integer) in a token (str) using the decoder.""" return self.decoder.get(index, self.unk_token) - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """Converts a sequence of tokens (strings for sub-words) in a single string.""" current_sub_tokens = [] out_string = "" @@ -203,7 +203,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): out_string += decoded.upper() if self.do_upper_case else decoded return out_string.strip() - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """Build model inputs from a sequence by appending eos_token_id.""" if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + [self.eos_token_id] @@ -211,22 +211,22 @@ class Speech2TextTokenizer(PreTrainedTokenizer): return self.prefix_tokens + token_ids_0 + token_ids_1 + [self.eos_token_id] def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -240,12 +240,12 @@ class Speech2TextTokenizer(PreTrainedTokenizer): return prefix_ones + ([0] * len(token_ids_0)) + suffix_ones return prefix_ones + ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones - def __getstate__(self) -> Dict: + def __getstate__(self) -> dict: state = self.__dict__.copy() state["sp_model"] = None return state - def __setstate__(self, d: Dict) -> None: + def __setstate__(self, d: dict) -> None: self.__dict__ = d # for backward compatibility @@ -254,7 +254,7 @@ class Speech2TextTokenizer(PreTrainedTokenizer): self.sp_model = load_spm(self.spm_file, self.sp_model_kwargs) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: save_dir = Path(save_directory) assert save_dir.is_dir(), f"{save_directory} should be a directory" vocab_save_path = save_dir / ( @@ -276,13 +276,13 @@ class Speech2TextTokenizer(PreTrainedTokenizer): return (str(vocab_save_path), str(spm_save_path)) -def load_spm(path: str, sp_model_kwargs: Dict[str, Any]) -> sentencepiece.SentencePieceProcessor: +def load_spm(path: str, sp_model_kwargs: dict[str, Any]) -> sentencepiece.SentencePieceProcessor: spm = sentencepiece.SentencePieceProcessor(**sp_model_kwargs) spm.Load(str(path)) return spm -def load_json(path: str) -> Union[Dict, List]: +def load_json(path: str) -> Union[dict, list]: with open(path, "r") as f: return json.load(f) diff --git a/src/transformers/models/speecht5/configuration_speecht5.py b/src/transformers/models/speecht5/configuration_speecht5.py index 3a0ac5dcc9e..b73471a29dc 100644 --- a/src/transformers/models/speecht5/configuration_speecht5.py +++ b/src/transformers/models/speecht5/configuration_speecht5.py @@ -84,14 +84,14 @@ class SpeechT5Config(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the speech encoder pre-net. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the speech encoder pre-net. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the speech encoder pre-net. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -354,18 +354,18 @@ class SpeechT5HifiGanConfig(PretrainedConfig): The sampling rate at which the output audio will be generated, expressed in hertz (Hz). upsample_initial_channel (`int`, *optional*, defaults to 512): The number of input channels into the upsampling network. - upsample_rates (`Tuple[int]` or `List[int]`, *optional*, defaults to `[4, 4, 4, 4]`): + upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[4, 4, 4, 4]`): A tuple of integers defining the stride of each 1D convolutional layer in the upsampling network. The length of *upsample_rates* defines the number of convolutional layers and has to match the length of *upsample_kernel_sizes*. - upsample_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[8, 8, 8, 8]`): + upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 8, 8]`): A tuple of integers defining the kernel size of each 1D convolutional layer in the upsampling network. The length of *upsample_kernel_sizes* defines the number of convolutional layers and has to match the length of *upsample_rates*. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`): A tuple of integers defining the kernel sizes of the 1D convolutional layers in the multi-receptive field fusion (MRF) module. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the multi-receptive field fusion (MRF) module. initializer_range (`float`, *optional*, defaults to 0.01): diff --git a/src/transformers/models/speecht5/feature_extraction_speecht5.py b/src/transformers/models/speecht5/feature_extraction_speecht5.py index e6b277644ce..822ae01a88d 100644 --- a/src/transformers/models/speecht5/feature_extraction_speecht5.py +++ b/src/transformers/models/speecht5/feature_extraction_speecht5.py @@ -15,7 +15,7 @@ """Feature extractor class for SpeechT5.""" import warnings -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import numpy as np @@ -138,8 +138,8 @@ class SpeechT5FeatureExtractor(SequenceFeatureExtractor): @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def zero_mean_unit_var_norm( - input_values: List[np.ndarray], attention_mask: List[np.ndarray], padding_value: float = 0.0 - ) -> List[np.ndarray]: + input_values: list[np.ndarray], attention_mask: list[np.ndarray], padding_value: float = 0.0 + ) -> list[np.ndarray]: """ Every array in the list is normalized to have zero mean and unit variance """ @@ -179,8 +179,8 @@ class SpeechT5FeatureExtractor(SequenceFeatureExtractor): def __call__( self, - audio: Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None, - audio_target: Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None, + audio: Optional[Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]]] = None, + audio_target: Optional[Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]]] = None, padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, truncation: bool = False, @@ -197,11 +197,11 @@ class SpeechT5FeatureExtractor(SequenceFeatureExtractor): spectrogram features. Args: - audio (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`, *optional*): + audio (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`, *optional*): The sequence or batch of sequences to be processed. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. This outputs waveform features. Must be mono channel audio, not stereo, i.e. single float per timestep. - audio_target (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`, *optional*): + audio_target (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`, *optional*): The sequence or batch of sequences to be processed as targets. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. This outputs log-mel spectrogram features. @@ -296,7 +296,7 @@ class SpeechT5FeatureExtractor(SequenceFeatureExtractor): def _process_audio( self, - speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], is_target: bool = False, padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, @@ -381,7 +381,7 @@ class SpeechT5FeatureExtractor(SequenceFeatureExtractor): return padded_inputs - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: output = super().to_dict() # Don't serialize these as they are derived from the other properties. diff --git a/src/transformers/models/speecht5/modeling_speecht5.py b/src/transformers/models/speecht5/modeling_speecht5.py index 9967e2b0451..24b83368215 100644 --- a/src/transformers/models/speecht5/modeling_speecht5.py +++ b/src/transformers/models/speecht5/modeling_speecht5.py @@ -15,7 +15,7 @@ """PyTorch SpeechT5 model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -88,7 +88,7 @@ def shift_spectrograms_right( # Copied from transformers.models.wav2vec2.modeling_wav2vec2._compute_mask_indices def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -814,7 +814,7 @@ class SpeechT5TextDecoderPrenet(nn.Module): self, input_ids: torch.Tensor, attention_mask: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, ): if input_ids is not None: input_shape = input_ids.size() @@ -888,12 +888,12 @@ class SpeechT5Attention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, position_bias: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1124,7 +1124,7 @@ class SpeechT5DecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ): @@ -1269,7 +1269,7 @@ class SpeechT5Encoder(SpeechT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: """ Args: hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, feature_size)`): @@ -1398,7 +1398,7 @@ class SpeechT5EncoderWithSpeechPrenet(SpeechT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: hidden_states, attention_mask = self.prenet(input_values, attention_mask) outputs = self.wrapped_encoder( @@ -1440,7 +1440,7 @@ class SpeechT5EncoderWithTextPrenet(SpeechT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: hidden_states = self.prenet(input_values) outputs = self.wrapped_encoder( @@ -1476,7 +1476,7 @@ class SpeechT5EncoderWithoutPrenet(SpeechT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: return self.wrapped_encoder( hidden_states=input_values, attention_mask=attention_mask, @@ -1511,12 +1511,12 @@ class SpeechT5Decoder(SpeechT5PreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, feature_size)`): @@ -1717,12 +1717,12 @@ class SpeechT5DecoderWithSpeechPrenet(SpeechT5PreTrainedModel): speaker_embeddings: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: decoder_hidden_states = self.prenet(input_values, speaker_embeddings) outputs = self.wrapped_decoder( @@ -1769,12 +1769,12 @@ class SpeechT5DecoderWithTextPrenet(SpeechT5PreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: decoder_hidden_states, attention_mask = self.prenet(input_values, attention_mask, past_key_values) outputs = self.wrapped_decoder( @@ -1815,12 +1815,12 @@ class SpeechT5DecoderWithoutPrenet(SpeechT5PreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: outputs = self.wrapped_decoder( hidden_states=input_values, attention_mask=attention_mask, @@ -2020,14 +2020,14 @@ class SpeechT5Model(SpeechT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, speaker_embeddings: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" input_values (`torch.Tensor` of shape `(batch_size, sequence_length)`): Depending on which encoder is being used, the `input_values` are either: float values of the input raw @@ -2176,18 +2176,18 @@ class SpeechT5ForSpeechToText(SpeechT5PreTrainedModel, GenerationMixin): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, Seq2SeqLMOutput]: + ) -> Union[tuple, Seq2SeqLMOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a *.flac* or *.wav* audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (*pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (*pip install soundfile*). To prepare the array into `input_values`, the [`SpeechT5Processor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`SpeechT5Processor.__call__`] for details. decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): @@ -2323,7 +2323,7 @@ def _generate_speech( vocoder: Optional[nn.Module] = None, output_cross_attentions: bool = False, return_output_lengths: bool = False, -) -> Union[torch.FloatTensor, Tuple[torch.FloatTensor, torch.FloatTensor]]: +) -> Union[torch.FloatTensor, tuple[torch.FloatTensor, torch.FloatTensor]]: if speaker_embeddings is None: raise ValueError( """`speaker_embeddings` must be specified. For example, you can use a speaker embeddings by following @@ -2506,8 +2506,8 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -2515,7 +2515,7 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): speaker_embeddings: Optional[torch.FloatTensor] = None, labels: Optional[torch.FloatTensor] = None, stop_labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqSpectrogramOutput]: + ) -> Union[tuple, Seq2SeqSpectrogramOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -2642,7 +2642,7 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): output_cross_attentions: bool = False, return_output_lengths: bool = False, **kwargs, - ) -> Union[torch.FloatTensor, Tuple[torch.FloatTensor, torch.FloatTensor]]: + ) -> Union[torch.FloatTensor, tuple[torch.FloatTensor, torch.FloatTensor]]: r""" Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. @@ -2688,11 +2688,11 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): - **spectrograms** (*optional*, returned when no `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, output_sequence_length, config.num_mel_bins)` -- The predicted log-mel spectrograms that are padded to the maximum length. - - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `List[Int]` -- A list of + - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each spectrogram. - **waveforms** (*optional*, returned when a `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, num_frames)` -- The predicted speech waveforms that are padded to the maximum length. - - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `List[Int]` -- A list of all + - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each waveform. - **cross_attentions** (*optional*, returned when `output_cross_attentions` is `True`) `torch.FloatTensor` of shape `(batch_size, config.decoder_layers, config.decoder_attention_heads, @@ -2733,7 +2733,7 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): vocoder: Optional[nn.Module] = None, output_cross_attentions: bool = False, return_output_lengths: bool = False, - ) -> Union[torch.FloatTensor, Tuple[torch.FloatTensor, torch.FloatTensor]]: + ) -> Union[torch.FloatTensor, tuple[torch.FloatTensor, torch.FloatTensor]]: r""" Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. @@ -2784,11 +2784,11 @@ class SpeechT5ForTextToSpeech(SpeechT5PreTrainedModel): - **spectrograms** (*optional*, returned when no `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, output_sequence_length, config.num_mel_bins)` -- The predicted log-mel spectrograms that are padded to the maximum length. - - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `List[Int]` -- A list of + - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each spectrogram. - **waveforms** (*optional*, returned when a `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, num_frames)` -- The predicted speech waveforms that are padded to the maximum length. - - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `List[Int]` -- A list of all + - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each waveform. - **cross_attentions** (*optional*, returned when `output_cross_attentions` is `True`) `torch.FloatTensor` of shape `(batch_size, config.decoder_layers, config.decoder_attention_heads, @@ -2859,8 +2859,8 @@ class SpeechT5ForSpeechToSpeech(SpeechT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -2868,11 +2868,11 @@ class SpeechT5ForSpeechToSpeech(SpeechT5PreTrainedModel): speaker_embeddings: Optional[torch.FloatTensor] = None, labels: Optional[torch.FloatTensor] = None, stop_labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, Seq2SeqSpectrogramOutput]: + ) -> Union[tuple, Seq2SeqSpectrogramOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a *.flac* or *.wav* audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (*pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (*pip install soundfile*). To prepare the array into `input_values`, the [`SpeechT5Processor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`SpeechT5Processor.__call__`] for details. decoder_input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.num_mel_bins)`): @@ -2997,7 +2997,7 @@ class SpeechT5ForSpeechToSpeech(SpeechT5PreTrainedModel): input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. - Values can be obtained by loading a *.flac* or *.wav* audio file into an array of type `List[float]` or + Values can be obtained by loading a *.flac* or *.wav* audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (*pip install soundfile*). To prepare the array into `input_values`, the [`SpeechT5Processor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`SpeechT5Processor.__call__`] for details. @@ -3039,11 +3039,11 @@ class SpeechT5ForSpeechToSpeech(SpeechT5PreTrainedModel): - **spectrograms** (*optional*, returned when no `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, output_sequence_length, config.num_mel_bins)` -- The predicted log-mel spectrograms that are padded to the maximum length. - - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `List[Int]` -- A list of + - **spectrogram_lengths** (*optional*, returned when no `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each spectrogram. - **waveforms** (*optional*, returned when a `vocoder` is provided) `torch.FloatTensor` of shape `(batch_size, num_frames)` -- The predicted speech waveforms that are padded to the maximum length. - - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `List[Int]` -- A list of all + - **waveform_lengths** (*optional*, returned when a `vocoder` is provided) `list[Int]` -- A list of all the concrete lengths for each waveform. - **cross_attentions** (*optional*, returned when `output_cross_attentions` is `True`) `torch.FloatTensor` of shape `(batch_size, config.decoder_layers, config.decoder_attention_heads, diff --git a/src/transformers/models/speecht5/tokenization_speecht5.py b/src/transformers/models/speecht5/tokenization_speecht5.py index 5817fb20fe0..de19db8568e 100644 --- a/src/transformers/models/speecht5/tokenization_speecht5.py +++ b/src/transformers/models/speecht5/tokenization_speecht5.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -86,7 +86,7 @@ class SpeechT5Tokenizer(PreTrainedTokenizer): unk_token="", pad_token="", normalize=False, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -149,7 +149,7 @@ class SpeechT5Tokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Take as input a string and return a list of strings (tokens) for words/sub-words""" return self.sp_model.encode(text, out_type=str) @@ -182,7 +182,7 @@ class SpeechT5Tokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string.strip() - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """Build model inputs from a sequence by appending eos_token_id.""" if token_ids_1 is None: return token_ids_0 + [self.eos_token_id] @@ -190,8 +190,8 @@ class SpeechT5Tokenizer(PreTrainedTokenizer): return token_ids_0 + token_ids_1 + [self.eos_token_id] def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: if already_has_special_tokens: return super().get_special_tokens_mask( token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True @@ -202,7 +202,7 @@ class SpeechT5Tokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + suffix_ones return ([0] * len(token_ids_0)) + ([0] * len(token_ids_1)) + suffix_ones - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/splinter/modeling_splinter.py b/src/transformers/models/splinter/modeling_splinter.py index 372c678edf0..06d2917b6a2 100755 --- a/src/transformers/models/splinter/modeling_splinter.py +++ b/src/transformers/models/splinter/modeling_splinter.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -64,7 +64,7 @@ class SplinterEmbeddings(nn.Module): position_ids: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, past_key_values_length: Optional[int] = 0, - ) -> Tuple: + ) -> tuple: if input_ids is not None: input_shape = input_ids.size() else: @@ -131,9 +131,9 @@ class SplinterSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -281,9 +281,9 @@ class SplinterAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -352,9 +352,9 @@ class SplinterLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -431,12 +431,12 @@ class SplinterEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -576,12 +576,12 @@ class SplinterModel(SplinterPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0, @@ -767,7 +767,7 @@ class SplinterForQuestionAnswering(SplinterPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, question_positions: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" token_type_ids (`torch.LongTensor` of shape `batch_size, sequence_length`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0, @@ -883,8 +883,8 @@ class SplinterForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -920,7 +920,7 @@ class SplinterForPreTraining(SplinterPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, question_positions: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, SplinterForPreTrainingOutput]: + ) -> Union[tuple, SplinterForPreTrainingOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_questions, sequence_length)`): Indices of input sequence tokens in the vocabulary. diff --git a/src/transformers/models/splinter/tokenization_splinter.py b/src/transformers/models/splinter/tokenization_splinter.py index 67243d4ee43..926225bbfe6 100644 --- a/src/transformers/models/splinter/tokenization_splinter.py +++ b/src/transformers/models/splinter/tokenization_splinter.py @@ -18,7 +18,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -189,8 +189,8 @@ class SplinterTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a pair of sequence for question answering tasks by concatenating and adding special tokens. A Splinter sequence has the following format: @@ -199,13 +199,13 @@ class SplinterTokenizer(PreTrainedTokenizer): - pair of sequences for question answering: `[CLS] question_tokens [QUESTION] . [SEP] context_tokens [SEP]` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): The question token IDs if pad_on_right, else context tokens IDs - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): The context token IDs if pad_on_right, else question token IDs Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -221,22 +221,22 @@ class SplinterTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + question_suffix + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -249,8 +249,8 @@ class SplinterTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create the token type IDs corresponding to the sequences passed. [What are token type IDs?](../glossary#token-type-ids) @@ -258,11 +258,11 @@ class SplinterTokenizer(PreTrainedTokenizer): Should be overridden in a subclass if the model has a special way of building those. Args: - token_ids_0 (`List[int]`): The first tokenized sequence. - token_ids_1 (`List[int]`, *optional*): The second tokenized sequence. + token_ids_0 (`list[int]`): The first tokenized sequence. + token_ids_1 (`list[int]`, *optional*): The second tokenized sequence. Returns: - `List[int]`: The token type ids. + `list[int]`: The token type ids. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -277,7 +277,7 @@ class SplinterTokenizer(PreTrainedTokenizer): # Input is context-then-question return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + question_suffix + sep) * [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/splinter/tokenization_splinter_fast.py b/src/transformers/models/splinter/tokenization_splinter_fast.py index 85dd01a2be0..548d8e6c63b 100644 --- a/src/transformers/models/splinter/tokenization_splinter_fast.py +++ b/src/transformers/models/splinter/tokenization_splinter_fast.py @@ -15,7 +15,7 @@ """Fast Tokenization classes for Splinter.""" import json -from typing import List, Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -125,8 +125,8 @@ class SplinterTokenizerFast(PreTrainedTokenizerFast): return self.convert_tokens_to_ids(self.question_token) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a pair of sequence for question answering tasks by concatenating and adding special tokens. A Splinter sequence has the following format: @@ -135,13 +135,13 @@ class SplinterTokenizerFast(PreTrainedTokenizerFast): - pair of sequences for question answering: `[CLS] question_tokens [QUESTION] . [SEP] context_tokens [SEP]` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): The question token IDs if pad_on_right, else context tokens IDs - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): The context token IDs if pad_on_right, else question token IDs Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] @@ -157,8 +157,8 @@ class SplinterTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + token_ids_1 + question_suffix + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create the token type IDs corresponding to the sequences passed. [What are token type IDs?](../glossary#token-type-ids) @@ -166,11 +166,11 @@ class SplinterTokenizerFast(PreTrainedTokenizerFast): Should be overridden in a subclass if the model has a special way of building those. Args: - token_ids_0 (`List[int]`): The first tokenized sequence. - token_ids_1 (`List[int]`, *optional*): The second tokenized sequence. + token_ids_0 (`list[int]`): The first tokenized sequence. + token_ids_1 (`list[int]`, *optional*): The second tokenized sequence. Returns: - `List[int]`: The token type ids. + `list[int]`: The token type ids. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -185,7 +185,7 @@ class SplinterTokenizerFast(PreTrainedTokenizerFast): # Input is context-then-question return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + question_suffix + sep) * [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/squeezebert/modeling_squeezebert.py b/src/transformers/models/squeezebert/modeling_squeezebert.py index 9b71d118f4b..8175baf687b 100644 --- a/src/transformers/models/squeezebert/modeling_squeezebert.py +++ b/src/transformers/models/squeezebert/modeling_squeezebert.py @@ -15,7 +15,7 @@ """PyTorch SqueezeBert model.""" import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -477,7 +477,7 @@ class SqueezeBertModel(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -567,7 +567,7 @@ class SqueezeBertForMaskedLM(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -640,7 +640,7 @@ class SqueezeBertForSequenceClassification(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -726,7 +726,7 @@ class SqueezeBertForMultipleChoice(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -831,7 +831,7 @@ class SqueezeBertForTokenClassification(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -898,7 +898,7 @@ class SqueezeBertForQuestionAnswering(SqueezeBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.transformer( diff --git a/src/transformers/models/squeezebert/tokenization_squeezebert.py b/src/transformers/models/squeezebert/tokenization_squeezebert.py index 804d5f336f9..a9bda8bbbf8 100644 --- a/src/transformers/models/squeezebert/tokenization_squeezebert.py +++ b/src/transformers/models/squeezebert/tokenization_squeezebert.py @@ -17,7 +17,7 @@ import collections import os import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer, _is_control, _is_punctuation, _is_whitespace from ...utils import logging @@ -187,8 +187,8 @@ class SqueezeBertTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A SqueezeBERT sequence has the following format: @@ -212,8 +212,8 @@ class SqueezeBertTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -239,7 +239,7 @@ class SqueezeBertTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( diff --git a/src/transformers/models/squeezebert/tokenization_squeezebert_fast.py b/src/transformers/models/squeezebert/tokenization_squeezebert_fast.py index c0f96a78a89..b6e460d43a6 100644 --- a/src/transformers/models/squeezebert/tokenization_squeezebert_fast.py +++ b/src/transformers/models/squeezebert/tokenization_squeezebert_fast.py @@ -15,7 +15,7 @@ """Tokenization classes for SqueezeBERT.""" import json -from typing import Optional, Tuple +from typing import Optional from tokenizers import normalizers @@ -139,7 +139,7 @@ class SqueezeBertTokenizerFast(PreTrainedTokenizerFast): return output - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) return tuple(files) diff --git a/src/transformers/models/stablelm/configuration_stablelm.py b/src/transformers/models/stablelm/configuration_stablelm.py index b9a2193aa8e..b919470c7f3 100644 --- a/src/transformers/models/stablelm/configuration_stablelm.py +++ b/src/transformers/models/stablelm/configuration_stablelm.py @@ -96,11 +96,11 @@ class StableLmConfig(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/stablelm/modeling_stablelm.py b/src/transformers/models/stablelm/modeling_stablelm.py index be9af304d51..df4e41bcd21 100755 --- a/src/transformers/models/stablelm/modeling_stablelm.py +++ b/src/transformers/models/stablelm/modeling_stablelm.py @@ -20,7 +20,7 @@ """PyTorch StableLM model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -224,8 +224,8 @@ class StableLmAttention(nn.Module): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: bsz, q_len, _ = hidden_states.size() query_states = self.q_proj(hidden_states) @@ -311,8 +311,8 @@ class StableLmSdpaAttention(StableLmAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: if output_attentions: # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented. logger.warning_once( @@ -433,9 +433,9 @@ class StableLmFlashAttention2(StableLmAttention): output_attentions: bool = False, use_cache: bool = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # StableLmFlashAttention2 attention does not support output_attentions output_attentions = False @@ -537,12 +537,12 @@ class StableLmDecoderLayer(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -563,7 +563,7 @@ class StableLmDecoderLayer(nn.Module): (see `past_key_values`). cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*): Indices depicting the position of the input sequence tokens in the sequence - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -678,7 +678,7 @@ class StableLmModel(StableLmPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -965,7 +965,7 @@ class StableLmForCausalLM(StableLmPreTrainedModel, GenerationMixin): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, diff --git a/src/transformers/models/starcoder2/configuration_starcoder2.py b/src/transformers/models/starcoder2/configuration_starcoder2.py index 2e5dad55709..795fcb4696b 100644 --- a/src/transformers/models/starcoder2/configuration_starcoder2.py +++ b/src/transformers/models/starcoder2/configuration_starcoder2.py @@ -95,11 +95,11 @@ class Starcoder2Config(PretrainedConfig): `beta_slow` (`float`, *optional*): Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear ramp function. If unspecified, it defaults to 1. - `short_factor` (`List[float]`, *optional*): + `short_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to short contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 - `long_factor` (`List[float]`, *optional*): + `long_factor` (`list[float]`, *optional*): Only used with 'longrope'. The scaling factor to be applied to long contexts (< `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden size divided by the number of attention heads divided by 2 diff --git a/src/transformers/models/starcoder2/modeling_starcoder2.py b/src/transformers/models/starcoder2/modeling_starcoder2.py index 6e102d80142..b0179a518bb 100644 --- a/src/transformers/models/starcoder2/modeling_starcoder2.py +++ b/src/transformers/models/starcoder2/modeling_starcoder2.py @@ -24,7 +24,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -60,7 +60,7 @@ class Starcoder2MLP(nn.Module): self.act = ACT2FN[config.hidden_act] self.residual_dropout = config.residual_dropout - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -161,12 +161,12 @@ class Starcoder2Attention(nn.Module): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -225,9 +225,9 @@ class Starcoder2DecoderLayer(GradientCheckpointingLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, # necessary, but kept here for BC **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: residual = hidden_states hidden_states = self.input_layernorm(hidden_states) @@ -354,7 +354,7 @@ class Starcoder2Model(Starcoder2PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, diff --git a/src/transformers/models/starcoder2/modular_starcoder2.py b/src/transformers/models/starcoder2/modular_starcoder2.py index d415870e494..72267437133 100644 --- a/src/transformers/models/starcoder2/modular_starcoder2.py +++ b/src/transformers/models/starcoder2/modular_starcoder2.py @@ -19,7 +19,7 @@ # limitations under the License. """PyTorch Starcoder2 model.""" -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -60,7 +60,7 @@ class Starcoder2MLP(nn.Module): self.act = ACT2FN[config.hidden_act] self.residual_dropout = config.residual_dropout - def forward(self, hidden_states: Optional[Tuple[torch.FloatTensor]]) -> torch.FloatTensor: + def forward(self, hidden_states: Optional[tuple[torch.FloatTensor]]) -> torch.FloatTensor: hidden_states = self.c_fc(hidden_states) hidden_states = self.act(hidden_states) hidden_states = self.c_proj(hidden_states) @@ -80,12 +80,12 @@ class Starcoder2Attention(MistralAttention): def forward( self, hidden_states: torch.Tensor, - position_embeddings: Tuple[torch.Tensor, torch.Tensor], + position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor], past_key_value: Optional[Cache] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -169,7 +169,7 @@ class Starcoder2Model(MistralModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, diff --git a/src/transformers/models/superglue/configuration_superglue.py b/src/transformers/models/superglue/configuration_superglue.py index caebe86d9c7..7ca9e229b7f 100644 --- a/src/transformers/models/superglue/configuration_superglue.py +++ b/src/transformers/models/superglue/configuration_superglue.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, Optional from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -39,9 +39,9 @@ class SuperGlueConfig(PretrainedConfig): The config object or dictionary of the keypoint detector. hidden_size (`int`, *optional*, defaults to 256): The dimension of the descriptors. - keypoint_encoder_sizes (`List[int]`, *optional*, defaults to `[32, 64, 128, 256]`): + keypoint_encoder_sizes (`list[int]`, *optional*, defaults to `[32, 64, 128, 256]`): The sizes of the keypoint encoder layers. - gnn_layers_types (`List[str]`, *optional*, defaults to `['self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross']`): + gnn_layers_types (`list[str]`, *optional*, defaults to `['self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross', 'self', 'cross']`): The types of the GNN layers. Must be either 'self' or 'cross'. num_attention_heads (`int`, *optional*, defaults to 4): The number of heads in the GNN layers. @@ -73,8 +73,8 @@ class SuperGlueConfig(PretrainedConfig): self, keypoint_detector_config: "SuperPointConfig" = None, hidden_size: int = 256, - keypoint_encoder_sizes: Optional[List[int]] = None, - gnn_layers_types: Optional[List[str]] = None, + keypoint_encoder_sizes: Optional[list[int]] = None, + gnn_layers_types: Optional[list[str]] = None, num_attention_heads: int = 4, sinkhorn_iterations: int = 100, matching_threshold: float = 0.0, diff --git a/src/transformers/models/superglue/convert_superglue_to_hf.py b/src/transformers/models/superglue/convert_superglue_to_hf.py index cfff39acdfd..f9374a64090 100644 --- a/src/transformers/models/superglue/convert_superglue_to_hf.py +++ b/src/transformers/models/superglue/convert_superglue_to_hf.py @@ -15,7 +15,6 @@ import argparse import gc import os import re -from typing import List import torch from datasets import load_dataset @@ -98,7 +97,7 @@ ORIGINAL_TO_CONVERTED_KEY_MAPPING = { } -def convert_old_keys_to_new_keys(state_dict_keys: List[str], conversion_mapping=ORIGINAL_TO_CONVERTED_KEY_MAPPING): +def convert_old_keys_to_new_keys(state_dict_keys: list[str], conversion_mapping=ORIGINAL_TO_CONVERTED_KEY_MAPPING): """ This function should be applied only once, on the concatenated keys to efficiently rename using the key mappings. diff --git a/src/transformers/models/superglue/image_processing_superglue.py b/src/transformers/models/superglue/image_processing_superglue.py index e39c4f933bf..ab6108e205b 100644 --- a/src/transformers/models/superglue/image_processing_superglue.py +++ b/src/transformers/models/superglue/image_processing_superglue.py @@ -13,7 +13,7 @@ # limitations under the License. """Image processor class for SuperPoint.""" -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -142,7 +142,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 480, "width": 640}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 480, "width": 640}`): Resolution of the output image after `resize` is applied. Only has an effect if `do_resize` is set to `True`. Can be overridden by `size` in the `preprocess` method. resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`): @@ -162,7 +162,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: float = 1 / 255, @@ -184,7 +184,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -195,7 +195,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary of the form `{"height": int, "width": int}`, specifying the size of the output image. data_format (`ChannelDimension` or `str`, *optional*): The channel dimension format of the output image. If not provided, it will be inferred from the input @@ -224,7 +224,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): self, images, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, @@ -244,7 +244,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the output image after `resize` has been applied. If `size["shortest_edge"]` >= 384, the image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"]/ crop_pct)`, after which the image is cropped to @@ -341,23 +341,23 @@ class SuperGlueImageProcessor(BaseImageProcessor): def post_process_keypoint_matching( self, outputs: "KeypointMatchingOutput", - target_sizes: Union[TensorType, List[Tuple]], + target_sizes: Union[TensorType, list[tuple]], threshold: float = 0.0, - ) -> List[Dict[str, torch.Tensor]]: + ) -> list[dict[str, torch.Tensor]]: """ Converts the raw output of [`KeypointMatchingOutput`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. Args: outputs ([`KeypointMatchingOutput`]): Raw outputs of the model. - target_sizes (`torch.Tensor` or `List[Tuple[Tuple[int, int]]]`, *optional*): - Tensor of shape `(batch_size, 2, 2)` or list of tuples of tuples (`Tuple[int, int]`) containing the + target_sizes (`torch.Tensor` or `list[tuple[tuple[int, int]]]`, *optional*): + Tensor of shape `(batch_size, 2, 2)` or list of tuples of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. This must be the original image size (before any processing). threshold (`float`, *optional*, defaults to 0.0): Threshold to filter out the matches with low scores. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image + `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image of the pair, the matching scores and the matching indices. """ if outputs.mask.shape[0] != len(target_sizes): @@ -365,7 +365,7 @@ class SuperGlueImageProcessor(BaseImageProcessor): if not all(len(target_size) == 2 for target_size in target_sizes): raise ValueError("Each element of target_sizes must contain the size (h, w) of each image of the batch") - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): image_pair_sizes = torch.tensor(target_sizes, device=outputs.mask.device) else: if target_sizes.shape[1] != 2 or target_sizes.shape[2] != 2: diff --git a/src/transformers/models/superglue/modeling_superglue.py b/src/transformers/models/superglue/modeling_superglue.py index d5d5fbb47fe..d597d04b649 100644 --- a/src/transformers/models/superglue/modeling_superglue.py +++ b/src/transformers/models/superglue/modeling_superglue.py @@ -15,7 +15,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -31,18 +31,18 @@ from ..auto import AutoModelForKeypointDetection logger = logging.get_logger(__name__) -def concat_pairs(tensor_tuple0: Tuple[torch.Tensor], tensor_tuple1: Tuple[torch.Tensor]) -> Tuple[torch.Tensor]: +def concat_pairs(tensor_tuple0: tuple[torch.Tensor], tensor_tuple1: tuple[torch.Tensor]) -> tuple[torch.Tensor]: """ Concatenate two tuples of tensors pairwise Args: - tensor_tuple0 (`Tuple[torch.Tensor]`): + tensor_tuple0 (`tuple[torch.Tensor]`): Tuple of tensors. - tensor_tuple1 (`Tuple[torch.Tensor]`): + tensor_tuple1 (`tuple[torch.Tensor]`): Tuple of tensors. Returns: - (`Tuple[torch.Tensor]`): Tuple of concatenated tensors. + (`tuple[torch.Tensor]`): Tuple of concatenated tensors. """ return tuple([torch.cat([tensor0, tensor1]) for tensor0, tensor1 in zip(tensor_tuple0, tensor_tuple1)]) @@ -166,11 +166,11 @@ class KeypointMatchingOutput(ModelOutput): Scores of predicted matches. keypoints (`torch.FloatTensor` of shape `(batch_size, num_keypoints, 2)`): Absolute (x, y) coordinates of predicted keypoints in a given image. - hidden_states (`Tuple[torch.FloatTensor, ...]`, *optional*): + hidden_states (`tuple[torch.FloatTensor, ...]`, *optional*): Tuple of `torch.FloatTensor` (one for the output of each stage) of shape `(batch_size, 2, num_channels, num_keypoints)`, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) - attentions (`Tuple[torch.FloatTensor, ...]`, *optional*): + attentions (`tuple[torch.FloatTensor, ...]`, *optional*): Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, 2, num_heads, num_keypoints, num_keypoints)`, returned when `output_attentions=True` is passed or when `config.output_attentions=True`) """ @@ -180,8 +180,8 @@ class KeypointMatchingOutput(ModelOutput): matching_scores: Optional[torch.FloatTensor] = None keypoints: Optional[torch.FloatTensor] = None mask: Optional[torch.IntTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class SuperGlueMultiLayerPerceptron(nn.Module): @@ -220,7 +220,7 @@ class SuperGlueKeypointEncoder(nn.Module): keypoints: torch.Tensor, scores: torch.Tensor, output_hidden_states: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[tuple[torch.Tensor]]]: scores = scores.unsqueeze(2) hidden_state = torch.cat([keypoints, scores], dim=2) all_hidden_states = () if output_hidden_states else None @@ -271,9 +271,9 @@ class SuperGlueSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -416,9 +416,9 @@ class SuperGlueAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -454,7 +454,7 @@ class SuperGlueAttentionalPropagation(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, output_hidden_states: bool = False, - ) -> Tuple[torch.Tensor, Optional[Tuple[torch.Tensor]], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[tuple[torch.Tensor]], Optional[tuple[torch.Tensor]]]: attention_outputs = self.attention( descriptors, attention_mask=attention_mask, @@ -489,7 +489,7 @@ class SuperGlueAttentionalGNN(nn.Module): mask: Optional[torch.Tensor] = None, output_attentions: bool = False, output_hidden_states: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[Tuple], Optional[Tuple]]: + ) -> tuple[torch.Tensor, Optional[tuple], Optional[tuple]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -609,7 +609,7 @@ class SuperGlueForKeypointMatching(SuperGluePreTrainedModel): mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Tuple[torch.Tensor, torch.Tensor, Tuple, Tuple]: + ) -> tuple[torch.Tensor, torch.Tensor, tuple, tuple]: """ Perform keypoint matching between two images. @@ -752,7 +752,7 @@ class SuperGlueForKeypointMatching(SuperGluePreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, KeypointMatchingOutput]: + ) -> Union[tuple, KeypointMatchingOutput]: r""" Examples: diff --git a/src/transformers/models/superpoint/configuration_superpoint.py b/src/transformers/models/superpoint/configuration_superpoint.py index 82104e682bc..1e427196072 100644 --- a/src/transformers/models/superpoint/configuration_superpoint.py +++ b/src/transformers/models/superpoint/configuration_superpoint.py @@ -11,7 +11,6 @@ # 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. -from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -63,7 +62,7 @@ class SuperPointConfig(PretrainedConfig): def __init__( self, - encoder_hidden_sizes: List[int] = [64, 64, 128, 128], + encoder_hidden_sizes: list[int] = [64, 64, 128, 128], decoder_hidden_size: int = 256, keypoint_decoder_dim: int = 65, descriptor_decoder_dim: int = 256, diff --git a/src/transformers/models/superpoint/image_processing_superpoint.py b/src/transformers/models/superpoint/image_processing_superpoint.py index e0835934df3..dcd0c09f919 100644 --- a/src/transformers/models/superpoint/image_processing_superpoint.py +++ b/src/transformers/models/superpoint/image_processing_superpoint.py @@ -13,7 +13,7 @@ # limitations under the License. """Image processor class for SuperPoint.""" -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -104,7 +104,7 @@ class SuperPointImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"height": 480, "width": 640}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 480, "width": 640}`): Resolution of the output image after `resize` is applied. Only has an effect if `do_resize` is set to `True`. Can be overridden by `size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -122,7 +122,7 @@ class SuperPointImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: float = 1 / 255, do_grayscale: bool = False, @@ -141,7 +141,7 @@ class SuperPointImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -152,7 +152,7 @@ class SuperPointImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary of the form `{"height": int, "width": int}`, specifying the size of the output image. data_format (`ChannelDimension` or `str`, *optional*): The channel dimension format of the output image. If not provided, it will be inferred from the input @@ -181,7 +181,7 @@ class SuperPointImageProcessor(BaseImageProcessor): self, images, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_grayscale: Optional[bool] = None, @@ -199,7 +199,7 @@ class SuperPointImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the output image after `resize` has been applied. If `size["shortest_edge"]` >= 384, the image is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the image will be matched to `int(size["shortest_edge"]/ crop_pct)`, after which the image is cropped to @@ -290,8 +290,8 @@ class SuperPointImageProcessor(BaseImageProcessor): return BatchFeature(data=data, tensor_type=return_tensors) def post_process_keypoint_detection( - self, outputs: "SuperPointKeypointDescriptionOutput", target_sizes: Union[TensorType, List[Tuple]] - ) -> List[Dict[str, "torch.Tensor"]]: + self, outputs: "SuperPointKeypointDescriptionOutput", target_sizes: Union[TensorType, list[tuple]] + ) -> list[dict[str, "torch.Tensor"]]: """ Converts the raw output of [`SuperPointForKeypointDetection`] into lists of keypoints, scores and descriptors with coordinates absolute to the original image sizes. @@ -299,18 +299,18 @@ class SuperPointImageProcessor(BaseImageProcessor): Args: outputs ([`SuperPointKeypointDescriptionOutput`]): Raw outputs of the model containing keypoints in a relative (x, y) format, with scores and descriptors. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. This must be the original image size (before any processing). Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the keypoints in absolute format according + `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in absolute format according to target_sizes, scores and descriptors for an image in the batch as predicted by the model. """ if len(outputs.mask) != len(target_sizes): raise ValueError("Make sure that you pass in as many target sizes as the batch dimension of the mask") - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): image_sizes = torch.tensor(target_sizes, device=outputs.mask.device) else: if target_sizes.shape[1] != 2: diff --git a/src/transformers/models/superpoint/modeling_superpoint.py b/src/transformers/models/superpoint/modeling_superpoint.py index 63d717add70..bee932db5c8 100644 --- a/src/transformers/models/superpoint/modeling_superpoint.py +++ b/src/transformers/models/superpoint/modeling_superpoint.py @@ -14,7 +14,7 @@ """PyTorch SuperPoint model.""" from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -37,7 +37,7 @@ logger = logging.get_logger(__name__) def remove_keypoints_from_borders( keypoints: torch.Tensor, scores: torch.Tensor, border: int, height: int, width: int -) -> Tuple[torch.Tensor, torch.Tensor]: +) -> tuple[torch.Tensor, torch.Tensor]: """Removes keypoints (and their associated scores) that are too close to the border""" mask_h = (keypoints[:, 0] >= border) & (keypoints[:, 0] < (height - border)) mask_w = (keypoints[:, 1] >= border) & (keypoints[:, 1] < (width - border)) @@ -45,7 +45,7 @@ def remove_keypoints_from_borders( return keypoints[mask], scores[mask] -def top_k_keypoints(keypoints: torch.Tensor, scores: torch.Tensor, k: int) -> Tuple[torch.Tensor, torch.Tensor]: +def top_k_keypoints(keypoints: torch.Tensor, scores: torch.Tensor, k: int) -> tuple[torch.Tensor, torch.Tensor]: """Keeps the k keypoints with highest score""" if k >= len(keypoints): return keypoints, scores @@ -103,7 +103,7 @@ class SuperPointKeypointDescriptionOutput(ModelOutput): scores: Optional[torch.FloatTensor] = None descriptors: Optional[torch.FloatTensor] = None mask: Optional[torch.BoolTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None class SuperPointConvBlock(nn.Module): @@ -169,7 +169,7 @@ class SuperPointEncoder(nn.Module): input, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: all_hidden_states = () if output_hidden_states else None for conv_block in self.conv_blocks: @@ -215,7 +215,7 @@ class SuperPointInterestPointDecoder(nn.Module): config.decoder_hidden_size, config.keypoint_decoder_dim, kernel_size=1, stride=1, padding=0 ) - def forward(self, encoded: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def forward(self, encoded: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: scores = self._get_pixel_scores(encoded) keypoints, scores = self._extract_keypoints(scores) @@ -232,7 +232,7 @@ class SuperPointInterestPointDecoder(nn.Module): scores = simple_nms(scores, self.nms_radius) return scores - def _extract_keypoints(self, scores: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def _extract_keypoints(self, scores: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: """ Based on their scores, extract the pixels that represent the keypoints that will be used for descriptors computation. The keypoints are in the form of relative (x, y) coordinates. @@ -387,7 +387,7 @@ class SuperPointForKeypointDetection(SuperPointPreTrainedModel): labels: Optional[torch.LongTensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SuperPointKeypointDescriptionOutput]: + ) -> Union[tuple, SuperPointKeypointDescriptionOutput]: r""" Examples: diff --git a/src/transformers/models/swiftformer/configuration_swiftformer.py b/src/transformers/models/swiftformer/configuration_swiftformer.py index 4d16d690f76..c0b4428a432 100644 --- a/src/transformers/models/swiftformer/configuration_swiftformer.py +++ b/src/transformers/models/swiftformer/configuration_swiftformer.py @@ -43,13 +43,13 @@ class SwiftFormerConfig(PretrainedConfig): The size (resolution) of each image num_channels (`int`, *optional*, defaults to 3): The number of input channels - depths (`List[int]`, *optional*, defaults to `[3, 3, 6, 4]`): + depths (`list[int]`, *optional*, defaults to `[3, 3, 6, 4]`): Depth of each stage - embed_dims (`List[int]`, *optional*, defaults to `[48, 56, 112, 220]`): + embed_dims (`list[int]`, *optional*, defaults to `[48, 56, 112, 220]`): The embedding dimension at each stage mlp_ratio (`int`, *optional*, defaults to 4): Ratio of size of the hidden dimensionality of an MLP to the dimensionality of its input. - downsamples (`List[bool]`, *optional*, defaults to `[True, True, True, True]`): + downsamples (`list[bool]`, *optional*, defaults to `[True, True, True, True]`): Whether or not to downsample inputs between two stages. hidden_act (`str`, *optional*, defaults to `"gelu"`): The non-linear activation function (string). `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. diff --git a/src/transformers/models/swiftformer/modeling_swiftformer.py b/src/transformers/models/swiftformer/modeling_swiftformer.py index 71b7ab8d962..10e626131a6 100644 --- a/src/transformers/models/swiftformer/modeling_swiftformer.py +++ b/src/transformers/models/swiftformer/modeling_swiftformer.py @@ -15,7 +15,7 @@ """PyTorch SwiftFormer model.""" import collections.abc -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -423,7 +423,7 @@ class SwiftFormerModel(SwiftFormerPreTrainedModel): pixel_values: Optional[torch.Tensor] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithNoAttention]: + ) -> Union[tuple, BaseModelOutputWithNoAttention]: output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states ) diff --git a/src/transformers/models/swiftformer/modeling_tf_swiftformer.py b/src/transformers/models/swiftformer/modeling_tf_swiftformer.py index b7dc0e4f94d..612c2406a1d 100644 --- a/src/transformers/models/swiftformer/modeling_tf_swiftformer.py +++ b/src/transformers/models/swiftformer/modeling_tf_swiftformer.py @@ -15,7 +15,7 @@ """TensorFlow SwiftFormer model.""" import collections.abc -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -656,7 +656,7 @@ class TFSwiftFormerMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFBaseModelOutputWithNoAttention]: + ) -> Union[tuple, TFBaseModelOutputWithNoAttention]: r""" """ output_hidden_states = ( @@ -718,7 +718,7 @@ class TFSwiftFormerModel(TFSwiftFormerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithNoAttention, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithNoAttention, tuple[tf.Tensor]]: outputs = self.swiftformer( pixel_values=pixel_values, output_hidden_states=output_hidden_states, diff --git a/src/transformers/models/swin/configuration_swin.py b/src/transformers/models/swin/configuration_swin.py index cfc00021cbf..41462d51edb 100644 --- a/src/transformers/models/swin/configuration_swin.py +++ b/src/transformers/models/swin/configuration_swin.py @@ -75,12 +75,12 @@ class SwinConfig(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. encoder_stride (`int`, *optional*, defaults to 32): Factor to increase the spatial resolution by in the decoder head for masked image modeling. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/swin/modeling_swin.py b/src/transformers/models/swin/modeling_swin.py index 72d3b27e45f..99fa5b45dbc 100644 --- a/src/transformers/models/swin/modeling_swin.py +++ b/src/transformers/models/swin/modeling_swin.py @@ -18,7 +18,7 @@ import collections.abc import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -67,9 +67,9 @@ class SwinEncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -103,9 +103,9 @@ class SwinModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -139,9 +139,9 @@ class SwinMaskedImageModelingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None reconstruction: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @property def logits(self): @@ -184,9 +184,9 @@ class SwinImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None def window_partition(input_feature, window_size): @@ -280,7 +280,7 @@ class SwinEmbeddings(nn.Module): pixel_values: Optional[torch.FloatTensor], bool_masked_pos: Optional[torch.BoolTensor] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: _, num_channels, height, width = pixel_values.shape embeddings, output_dimensions = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) @@ -334,7 +334,7 @@ class SwinPatchEmbeddings(nn.Module): pixel_values = nn.functional.pad(pixel_values, pad_values) return pixel_values - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: _, num_channels, height, width = pixel_values.shape # pad the input to be divisible by self.patch_size, if needed pixel_values = self.maybe_pad(pixel_values, height, width) @@ -351,7 +351,7 @@ class SwinPatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -359,7 +359,7 @@ class SwinPatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -374,7 +374,7 @@ class SwinPatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -485,7 +485,7 @@ class SwinSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -580,7 +580,7 @@ class SwinAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -674,11 +674,11 @@ class SwinLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: if not always_partition: self.set_shift_and_window_size(input_dimensions) else: @@ -769,11 +769,11 @@ class SwinStage(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, always_partition: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: height, width = input_dimensions for i, layer_module in enumerate(self.blocks): layer_head_mask = head_mask[i] if head_mask is not None else None @@ -825,14 +825,14 @@ class SwinEncoder(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, always_partition: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, SwinEncoderOutput]: + ) -> Union[tuple, SwinEncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -972,7 +972,7 @@ class SwinModel(SwinPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SwinModelOutput]: + ) -> Union[tuple, SwinModelOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1067,7 +1067,7 @@ class SwinForMaskedImageModeling(SwinPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SwinMaskedImageModelingOutput]: + ) -> Union[tuple, SwinMaskedImageModelingOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1182,7 +1182,7 @@ class SwinForImageClassification(SwinPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SwinImageClassifierOutput]: + ) -> Union[tuple, SwinImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/swin/modeling_tf_swin.py b/src/transformers/models/swin/modeling_tf_swin.py index a627d133d62..5b9f9f272ff 100644 --- a/src/transformers/models/swin/modeling_tf_swin.py +++ b/src/transformers/models/swin/modeling_tf_swin.py @@ -22,7 +22,7 @@ import warnings from collections.abc import Iterable from dataclasses import dataclass from functools import partial -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import tensorflow as tf @@ -93,9 +93,9 @@ class TFSwinEncoderOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - reshaped_hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + reshaped_hidden_states: tuple[tf.Tensor, ...] | None = None @dataclass @@ -129,9 +129,9 @@ class TFSwinModelOutput(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None pooler_output: tf.Tensor | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - reshaped_hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + reshaped_hidden_states: tuple[tf.Tensor, ...] | None = None @dataclass @@ -165,9 +165,9 @@ class TFSwinMaskedImageModelingOutput(ModelOutput): loss: tf.Tensor | None = None reconstruction: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - reshaped_hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + reshaped_hidden_states: tuple[tf.Tensor, ...] | None = None @property def logits(self): @@ -210,9 +210,9 @@ class TFSwinImageClassifierOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None - reshaped_hidden_states: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None + reshaped_hidden_states: tuple[tf.Tensor, ...] | None = None def window_partition(input_feature: tf.Tensor, window_size: int) -> tf.Tensor: @@ -309,7 +309,7 @@ class TFSwinEmbeddings(keras.layers.Layer): def call( self, pixel_values: tf.Tensor, bool_masked_pos: Optional[bool] = None, training: bool = False - ) -> Tuple[tf.Tensor, Tuple[int, int]]: + ) -> tuple[tf.Tensor, tuple[int, int]]: embeddings, output_dimensions = self.patch_embeddings(pixel_values, training=training) embeddings = self.norm(embeddings, training=training) batch_size, seq_len, _ = shape_list(embeddings) @@ -366,7 +366,7 @@ class TFSwinPatchEmbeddings(keras.layers.Layer): pixel_values = tf.pad(pixel_values, pad_values) return pixel_values - def call(self, pixel_values: tf.Tensor, training: bool = False) -> Tuple[tf.Tensor, Tuple[int, int]]: + def call(self, pixel_values: tf.Tensor, training: bool = False) -> tuple[tf.Tensor, tuple[int, int]]: _, num_channels, height, width = shape_list(pixel_values) if tf.executing_eagerly() and num_channels != self.num_channels: raise ValueError( @@ -404,7 +404,7 @@ class TFSwinPatchMerging(keras.layers.Layer): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -413,7 +413,7 @@ class TFSwinPatchMerging(keras.layers.Layer): """ def __init__( - self, input_resolution: Tuple[int, int], dim: int, norm_layer: Optional[Callable] = None, **kwargs + self, input_resolution: tuple[int, int], dim: int, norm_layer: Optional[Callable] = None, **kwargs ) -> None: super().__init__(**kwargs) self.input_resolution = input_resolution @@ -433,7 +433,7 @@ class TFSwinPatchMerging(keras.layers.Layer): return input_feature - def call(self, input_feature: tf.Tensor, input_dimensions: Tuple[int, int], training: bool = False) -> tf.Tensor: + def call(self, input_feature: tf.Tensor, input_dimensions: tuple[int, int], training: bool = False) -> tf.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, _, num_channels = shape_list(input_feature) @@ -575,7 +575,7 @@ class TFSwinSelfAttention(keras.layers.Layer): head_mask: tf.Tensor | None = None, output_attentions: bool = False, training: bool = False, - ) -> Tuple[tf.Tensor, ...]: + ) -> tuple[tf.Tensor, ...]: batch_size, dim, _ = shape_list(hidden_states) mixed_query_layer = self.query(hidden_states) @@ -746,7 +746,7 @@ class TFSwinLayer(keras.layers.Layer): self, config, dim, - input_resolution: Tuple[int, int], + input_resolution: tuple[int, int], num_heads: int, drop_path_rate: float = 0.0, shift_size: int = 0, @@ -801,7 +801,7 @@ class TFSwinLayer(keras.layers.Layer): def maybe_pad( self, hidden_states: tf.Tensor, window_size: int, height: int, width: int - ) -> Tuple[tf.Tensor, tf.Tensor]: + ) -> tuple[tf.Tensor, tf.Tensor]: pad_right = (window_size - width % window_size) % window_size pad_bottom = (window_size - height % window_size) % window_size pad_values = [[0, 0], [0, pad_bottom], [0, pad_right], [0, 0]] @@ -812,7 +812,7 @@ class TFSwinLayer(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: tf.Tensor | None = None, output_attentions: bool = False, training: bool = False, @@ -904,10 +904,10 @@ class TFSwinStage(keras.layers.Layer): self, config: SwinConfig, dim: int, - input_resolution: Tuple[int, int], + input_resolution: tuple[int, int], depth: int, num_heads: int, - drop_path: List[float], + drop_path: list[float], downsample: Optional[Callable], **kwargs, ) -> None: @@ -943,11 +943,11 @@ class TFSwinStage(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor, ...]: + ) -> tuple[tf.Tensor, ...]: height, width = input_dimensions for i, layer_module in enumerate(self.blocks): layer_head_mask = head_mask[i] if head_mask is not None else None @@ -985,7 +985,7 @@ class TFSwinStage(keras.layers.Layer): class TFSwinEncoder(keras.layers.Layer): - def __init__(self, config: SwinConfig, grid_size: Tuple[int, int], **kwargs): + def __init__(self, config: SwinConfig, grid_size: tuple[int, int], **kwargs): super().__init__(**kwargs) self.num_layers = len(config.depths) self.config = config @@ -1009,13 +1009,13 @@ class TFSwinEncoder(keras.layers.Layer): def call( self, hidden_states: tf.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: tf.Tensor | None = None, output_attentions: bool = False, output_hidden_states: bool = False, return_dict: bool = True, training: bool = False, - ) -> Union[Tuple[tf.Tensor, ...], TFSwinEncoderOutput]: + ) -> Union[tuple[tf.Tensor, ...], TFSwinEncoderOutput]: all_input_dimensions = () all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None @@ -1187,7 +1187,7 @@ class AdaptiveAveragePooling1D(keras.layers.Layer): shape = tf.TensorShape([input_shape[0], input_shape[1], self.output_size[0]]) return shape - def get_config(self) -> Dict[str, Any]: + def get_config(self) -> dict[str, Any]: config = { "output_size": self.output_size, "data_format": self.data_format, @@ -1217,7 +1217,7 @@ class TFSwinMainLayer(keras.layers.Layer): def get_input_embeddings(self) -> TFSwinPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List]): + def _prune_heads(self, heads_to_prune: dict[int, list]): """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -1225,7 +1225,7 @@ class TFSwinMainLayer(keras.layers.Layer): for layer, heads in heads_to_prune.items(): self.encoder.layer[layer].attention.prune_heads(heads) - def get_head_mask(self, head_mask: Optional[Any]) -> List: + def get_head_mask(self, head_mask: Optional[Any]) -> list: if head_mask is not None: raise NotImplementedError return [None] * len(self.config.depths) @@ -1240,7 +1240,7 @@ class TFSwinMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFSwinModelOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFSwinModelOutput, tuple[tf.Tensor, ...]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1336,7 +1336,7 @@ class TFSwinModel(TFSwinPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFSwinModelOutput, Tuple[tf.Tensor, ...]]: + ) -> Union[TFSwinModelOutput, tuple[tf.Tensor, ...]]: r""" bool_masked_pos (`tf.Tensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1453,7 +1453,7 @@ class TFSwinForMaskedImageModeling(TFSwinPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple, TFSwinMaskedImageModelingOutput]: + ) -> Union[tuple, TFSwinMaskedImageModelingOutput]: r""" bool_masked_pos (`tf.Tensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1587,7 +1587,7 @@ class TFSwinForImageClassification(TFSwinPreTrainedModel, TFSequenceClassificati output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor, ...], TFSwinImageClassifierOutput]: + ) -> Union[tuple[tf.Tensor, ...], TFSwinImageClassifierOutput]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/swin2sr/image_processing_swin2sr_fast.py b/src/transformers/models/swin2sr/image_processing_swin2sr_fast.py index 7b8e6843e6b..e1e3840ab56 100644 --- a/src/transformers/models/swin2sr/image_processing_swin2sr_fast.py +++ b/src/transformers/models/swin2sr/image_processing_swin2sr_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for Swin2SR.""" -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature, ChannelDimension, get_image_size from ...image_processing_utils_fast import ( @@ -95,7 +95,7 @@ class Swin2SRImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], + images: list["torch.Tensor"], do_rescale: bool, rescale_factor: float, do_pad: bool, diff --git a/src/transformers/models/swin2sr/modeling_swin2sr.py b/src/transformers/models/swin2sr/modeling_swin2sr.py index 44e12cfe02a..9a6030b4a0f 100644 --- a/src/transformers/models/swin2sr/modeling_swin2sr.py +++ b/src/transformers/models/swin2sr/modeling_swin2sr.py @@ -17,7 +17,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -56,8 +56,8 @@ class Swin2SREncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.swin.modeling_swin.window_partition @@ -139,7 +139,7 @@ class Swin2SREmbeddings(nn.Module): self.dropout = nn.Dropout(config.hidden_dropout_prob) self.window_size = config.window_size - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor]: embeddings, output_dimensions = self.patch_embeddings(pixel_values) if self.position_embeddings is not None: @@ -165,7 +165,7 @@ class Swin2SRPatchEmbeddings(nn.Module): self.projection = nn.Conv2d(num_channels, config.embed_dim, kernel_size=patch_size, stride=patch_size) self.layernorm = nn.LayerNorm(config.embed_dim) if normalize_patches else None - def forward(self, embeddings: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, embeddings: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: embeddings = self.projection(embeddings) _, _, height, width = embeddings.shape output_dimensions = (height, width) @@ -197,7 +197,7 @@ class Swin2SRPatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -205,7 +205,7 @@ class Swin2SRPatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -220,7 +220,7 @@ class Swin2SRPatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -320,7 +320,7 @@ class Swin2SRSelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -430,7 +430,7 @@ class Swin2SRAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -493,7 +493,7 @@ class Swin2SRLayer(nn.Module): self.output = Swin2SROutput(config, dim) self.layernorm_after = nn.LayerNorm(dim, eps=config.layer_norm_eps) - def _compute_window_shift(self, target_window_size, target_shift_size) -> Tuple[Tuple[int, int], Tuple[int, int]]: + def _compute_window_shift(self, target_window_size, target_shift_size) -> tuple[tuple[int, int], tuple[int, int]]: window_size = [r if r <= w else w for r, w in zip(self.input_resolution, target_window_size)] shift_size = [0 if r <= w else s for r, w, s in zip(self.input_resolution, window_size, target_shift_size)] return window_size, shift_size @@ -536,10 +536,10 @@ class Swin2SRLayer(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: height, width = input_dimensions batch_size, _, channels = hidden_states.size() shortcut = hidden_states @@ -634,10 +634,10 @@ class Swin2SRStage(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: residual = hidden_states height, width = input_dimensions @@ -689,12 +689,12 @@ class Swin2SREncoder(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, Swin2SREncoderOutput]: + ) -> Union[tuple, Swin2SREncoderOutput]: all_input_dimensions = () all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -811,7 +811,7 @@ class Swin2SRModel(Swin2SRPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1038,7 +1038,7 @@ class Swin2SRForImageSuperResolution(Swin2SRPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageSuperResolutionOutput]: + ) -> Union[tuple, ImageSuperResolutionOutput]: r""" Example: ```python diff --git a/src/transformers/models/swinv2/configuration_swinv2.py b/src/transformers/models/swinv2/configuration_swinv2.py index addb30e6a10..8fe49325908 100644 --- a/src/transformers/models/swinv2/configuration_swinv2.py +++ b/src/transformers/models/swinv2/configuration_swinv2.py @@ -71,11 +71,11 @@ class Swinv2Config(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. encoder_stride (`int`, *optional*, defaults to 32): Factor to increase the spatial resolution by in the decoder head for masked image modeling. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. diff --git a/src/transformers/models/swinv2/modeling_swinv2.py b/src/transformers/models/swinv2/modeling_swinv2.py index e3ed52baad0..0ad4821f0c3 100644 --- a/src/transformers/models/swinv2/modeling_swinv2.py +++ b/src/transformers/models/swinv2/modeling_swinv2.py @@ -18,7 +18,7 @@ import collections.abc import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -68,9 +68,9 @@ class Swinv2EncoderOutput(ModelOutput): """ last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -105,9 +105,9 @@ class Swinv2ModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -142,9 +142,9 @@ class Swinv2MaskedImageModelingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None reconstruction: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None @property def logits(self): @@ -188,9 +188,9 @@ class Swinv2ImageClassifierOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None - reshaped_hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None + reshaped_hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None # Copied from transformers.models.swin.modeling_swin.window_partition @@ -323,7 +323,7 @@ class Swinv2Embeddings(nn.Module): pixel_values: Optional[torch.FloatTensor], bool_masked_pos: Optional[torch.BoolTensor] = None, interpolate_pos_encoding: bool = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: _, num_channels, height, width = pixel_values.shape embeddings, output_dimensions = self.patch_embeddings(pixel_values) embeddings = self.norm(embeddings) @@ -378,7 +378,7 @@ class Swinv2PatchEmbeddings(nn.Module): pixel_values = nn.functional.pad(pixel_values, pad_values) return pixel_values - def forward(self, pixel_values: Optional[torch.FloatTensor]) -> Tuple[torch.Tensor, Tuple[int]]: + def forward(self, pixel_values: Optional[torch.FloatTensor]) -> tuple[torch.Tensor, tuple[int]]: _, num_channels, height, width = pixel_values.shape # pad the input to be divisible by self.patch_size, if needed pixel_values = self.maybe_pad(pixel_values, height, width) @@ -395,7 +395,7 @@ class Swinv2PatchMerging(nn.Module): Patch Merging Layer. Args: - input_resolution (`Tuple[int]`): + input_resolution (`tuple[int]`): Resolution of input feature. dim (`int`): Number of input channels. @@ -403,7 +403,7 @@ class Swinv2PatchMerging(nn.Module): Normalization layer class. """ - def __init__(self, input_resolution: Tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: + def __init__(self, input_resolution: tuple[int], dim: int, norm_layer: nn.Module = nn.LayerNorm) -> None: super().__init__() self.input_resolution = input_resolution self.dim = dim @@ -418,7 +418,7 @@ class Swinv2PatchMerging(nn.Module): return input_feature - def forward(self, input_feature: torch.Tensor, input_dimensions: Tuple[int, int]) -> torch.Tensor: + def forward(self, input_feature: torch.Tensor, input_dimensions: tuple[int, int]) -> torch.Tensor: height, width = input_dimensions # `dim` is height * width batch_size, dim, num_channels = input_feature.shape @@ -517,7 +517,7 @@ class Swinv2SelfAttention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: batch_size, dim, num_channels = hidden_states.shape mixed_query_layer = self.query(hidden_states) @@ -626,7 +626,7 @@ class Swinv2Attention(nn.Module): attention_mask: Optional[torch.FloatTensor] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self(hidden_states, attention_mask, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) outputs = (attention_output,) + self_outputs[1:] # add attentions if we output them @@ -688,7 +688,7 @@ class Swinv2Layer(nn.Module): self.output = Swinv2Output(config, dim) self.layernorm_after = nn.LayerNorm(dim, eps=config.layer_norm_eps) - def _compute_window_shift(self, target_window_size, target_shift_size) -> Tuple[Tuple[int, int], Tuple[int, int]]: + def _compute_window_shift(self, target_window_size, target_shift_size) -> tuple[tuple[int, int], tuple[int, int]]: window_size = [r if r <= w else w for r, w in zip(self.input_resolution, target_window_size)] shift_size = [0 if r <= w else s for r, w, s in zip(self.input_resolution, window_size, target_shift_size)] return window_size, shift_size @@ -731,10 +731,10 @@ class Swinv2Layer(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: height, width = input_dimensions batch_size, _, channels = hidden_states.size() shortcut = hidden_states @@ -819,10 +819,10 @@ class Swinv2Stage(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: height, width = input_dimensions for i, layer_module in enumerate(self.blocks): layer_head_mask = head_mask[i] if head_mask is not None else None @@ -880,13 +880,13 @@ class Swinv2Encoder(nn.Module): def forward( self, hidden_states: torch.Tensor, - input_dimensions: Tuple[int, int], + input_dimensions: tuple[int, int], head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, output_hidden_states_before_downsampling: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple, Swinv2EncoderOutput]: + ) -> Union[tuple, Swinv2EncoderOutput]: all_hidden_states = () if output_hidden_states else None all_reshaped_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1029,7 +1029,7 @@ class Swinv2Model(Swinv2PreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Swinv2ModelOutput]: + ) -> Union[tuple, Swinv2ModelOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1126,7 +1126,7 @@ class Swinv2ForMaskedImageModeling(Swinv2PreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Swinv2MaskedImageModelingOutput]: + ) -> Union[tuple, Swinv2MaskedImageModelingOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). @@ -1242,7 +1242,7 @@ class Swinv2ForImageClassification(Swinv2PreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Swinv2ImageClassifierOutput]: + ) -> Union[tuple, Swinv2ImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/switch_transformers/modeling_switch_transformers.py b/src/transformers/models/switch_transformers/modeling_switch_transformers.py index b4694d3e324..83331231133 100644 --- a/src/transformers/models/switch_transformers/modeling_switch_transformers.py +++ b/src/transformers/models/switch_transformers/modeling_switch_transformers.py @@ -17,7 +17,7 @@ import copy import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -141,7 +141,7 @@ class SwitchTransformersTop1Router(nn.Module): self.ignore_padding_tokens = config.router_ignore_padding_tokens self.dtype = getattr(torch, config.router_dtype) - def _compute_router_probabilities(self, hidden_states: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: + def _compute_router_probabilities(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: r""" Computes router probabilities from input hidden states. @@ -182,7 +182,7 @@ class SwitchTransformersTop1Router(nn.Module): if not (hasattr(self.classifier, "SCB") or hasattr(self.classifier, "CB")): self.classifier = self.classifier.to(self.dtype) - def forward(self, hidden_states: torch.Tensor) -> Tuple: + def forward(self, hidden_states: torch.Tensor) -> tuple: r""" Generic forward function for every Router class. Each Router expects to have the same input hidden states (`hidden_states`) corresponding to the hidden states for each token, the `expert_capacity` corresponding to the @@ -196,7 +196,7 @@ class SwitchTransformersTop1Router(nn.Module): hidden_states (`torch.Tensor`) : [num_groups, tokens_per_group, hidden_dim] inputs to send to experts. Returns: - Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`] Tuple containing the expert index, the router probs + tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`] Tuple containing the expert index, the router probs and the router logits. The router probabilities and logits are required to compute the loss. """ router_probs, router_logits = self._compute_router_probabilities(hidden_states) @@ -1320,8 +1320,8 @@ class SwitchTransformersModel(SwitchTransformersPreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1330,7 +1330,7 @@ class SwitchTransformersModel(SwitchTransformersPreTrainedModel): output_router_logits: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqMoEModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqMoEModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. SWITCH_TRANSFORMERS is a model with relative position @@ -1541,8 +1541,8 @@ class SwitchTransformersForConditionalGeneration(SwitchTransformersPreTrainedMod head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1552,7 +1552,7 @@ class SwitchTransformersForConditionalGeneration(SwitchTransformersPreTrainedMod output_router_logits: Optional[bool] = True, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqMoEOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqMoEOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. SWITCH_TRANSFORMERS is a model with relative position @@ -1850,7 +1850,7 @@ class SwitchTransformersEncoderModel(SwitchTransformersPreTrainedModel): output_hidden_states: Optional[bool] = None, output_router_logits: Optional[bool] = True, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], MoEModelOutput]: + ) -> Union[tuple[torch.FloatTensor], MoEModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. SWITCH_TRANSFORMERS is a model with relative position diff --git a/src/transformers/models/t5/modeling_flax_t5.py b/src/transformers/models/t5/modeling_flax_t5.py index b5ce4776825..c829a084e4d 100644 --- a/src/transformers/models/t5/modeling_flax_t5.py +++ b/src/transformers/models/t5/modeling_flax_t5.py @@ -15,7 +15,7 @@ """Flax T5 model.""" import copy -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -936,7 +936,7 @@ class FlaxT5PreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: T5Config, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -953,7 +953,7 @@ class FlaxT5PreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") diff --git a/src/transformers/models/t5/modeling_t5.py b/src/transformers/models/t5/modeling_t5.py index 94c90dbfe06..5339e8e2d90 100644 --- a/src/transformers/models/t5/modeling_t5.py +++ b/src/transformers/models/t5/modeling_t5.py @@ -18,7 +18,7 @@ import copy import math import os import warnings -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -189,7 +189,7 @@ PARALLELIZE_DOCSTRING = r""" it will evenly distribute blocks across all devices. Args: - device_map (`Dict[int, list]`, *optional*): + device_map (`dict[int, list]`, *optional*): A dictionary that maps attention modules to devices. Note that the embedding module and LMHead are always automatically mapped to the first device (for esoteric reasons). That means that the first device should have fewer attention modules mapped to it than other devices. For reference, the t5 models have the @@ -1430,8 +1430,8 @@ class T5Model(T5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1439,7 +1439,7 @@ class T5Model(T5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you @@ -1681,8 +1681,8 @@ class T5ForConditionalGeneration(T5PreTrainedModel, GenerationMixin): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1691,7 +1691,7 @@ class T5ForConditionalGeneration(T5PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you @@ -1980,7 +1980,7 @@ class T5EncoderModel(T5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you @@ -2049,7 +2049,7 @@ class T5ForSequenceClassification(T5PreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -2057,7 +2057,7 @@ class T5ForSequenceClassification(T5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you @@ -2213,7 +2213,7 @@ class T5ForTokenClassification(T5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you @@ -2321,7 +2321,7 @@ class T5ForQuestionAnswering(T5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -2330,7 +2330,7 @@ class T5ForQuestionAnswering(T5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you diff --git a/src/transformers/models/t5/modeling_tf_t5.py b/src/transformers/models/t5/modeling_tf_t5.py index ab25938bb02..c1dae48de5a 100644 --- a/src/transformers/models/t5/modeling_tf_t5.py +++ b/src/transformers/models/t5/modeling_tf_t5.py @@ -21,7 +21,7 @@ import copy import itertools import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -738,7 +738,7 @@ class TFT5MainLayer(keras.layers.Layer): output_hidden_states=None, return_dict=None, training=False, - ) -> Tuple: + ) -> tuple: if input_ids is not None and inputs_embeds is not None: err_msg_prefix = "decoder_" if self.is_decoder else "" raise ValueError( @@ -1210,7 +1210,7 @@ class TFT5Model(TFT5PreTrainedModel): head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -1218,7 +1218,7 @@ class TFT5Model(TFT5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSeq2SeqModelOutput]: + ) -> Union[tuple, TFSeq2SeqModelOutput]: r""" Returns: @@ -1387,7 +1387,7 @@ class TFT5ForConditionalGeneration(TFT5PreTrainedModel, TFCausalLanguageModeling head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, encoder_outputs: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, labels: np.ndarray | tf.Tensor | None = None, @@ -1396,7 +1396,7 @@ class TFT5ForConditionalGeneration(TFT5PreTrainedModel, TFCausalLanguageModeling output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFSeq2SeqLMOutput]: + ) -> Union[tuple, TFSeq2SeqLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., @@ -1624,7 +1624,7 @@ class TFT5EncoderModel(TFT5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutput]: + ) -> Union[tuple, TFBaseModelOutput]: r""" Returns: diff --git a/src/transformers/models/t5/tokenization_t5.py b/src/transformers/models/t5/tokenization_t5.py index 01447232b89..439c3de9561 100644 --- a/src/transformers/models/t5/tokenization_t5.py +++ b/src/transformers/models/t5/tokenization_t5.py @@ -18,7 +18,7 @@ import os import re import warnings from shutil import copyfile -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple +from typing import TYPE_CHECKING, Any, Optional import sentencepiece as spm @@ -73,7 +73,7 @@ class T5Tokenizer(PreTrainedTokenizer): accessible as "" where "{%d}" is a number between 0 and extra_ids-1. These tokens can be retrieved by calling get_sentinel_tokens method and token ids can be by calling get_sentinel_token_ids method - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -132,7 +132,7 @@ class T5Tokenizer(PreTrainedTokenizer): pad_token="", extra_ids=100, additional_special_tokens=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, legacy=None, add_prefix_space=True, **kwargs, @@ -248,22 +248,22 @@ class T5Tokenizer(PreTrainedTokenizer): return vocab def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -283,7 +283,7 @@ class T5Tokenizer(PreTrainedTokenizer): def get_sentinel_token_ids(self): return [self.convert_tokens_to_ids(token) for token in self.get_sentinel_tokens()] - def _add_eos_if_not_present(self, token_ids: List[int]) -> List[int]: + def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]: """Do not add eos again if user already added it.""" if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( @@ -295,20 +295,20 @@ class T5Tokenizer(PreTrainedTokenizer): return token_ids + [self.eos_token_id] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] @@ -317,8 +317,8 @@ class T5Tokenizer(PreTrainedTokenizer): return len(token_ids_0 + eos + token_ids_1 + eos) * [0] def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -327,13 +327,13 @@ class T5Tokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = self._add_eos_if_not_present(token_ids_0) if token_ids_1 is None: @@ -357,7 +357,7 @@ class T5Tokenizer(PreTrainedTokenizer): self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file) - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Converts a string to a list of tokens. If `self.legacy` is set to `False`, a prefix token is added unless the first token is special. @@ -429,7 +429,7 @@ class T5Tokenizer(PreTrainedTokenizer): out_string += self.sp_model.decode(current_sub_tokens) return out_string.strip() - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/t5/tokenization_t5_fast.py b/src/transformers/models/t5/tokenization_t5_fast.py index e4b3fe57095..bdba1a7928c 100644 --- a/src/transformers/models/t5/tokenization_t5_fast.py +++ b/src/transformers/models/t5/tokenization_t5_fast.py @@ -18,7 +18,7 @@ import os import re import warnings from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging @@ -69,7 +69,7 @@ class T5TokenizerFast(PreTrainedTokenizerFast): Add a number of extra ids added to the vocabulary for use as sentinels. These tokens are accessible as "" where "{%d}" is a number between 0 and extra_ids-1. These tokens can be retrieved by calling get_sentinel_tokens method and token ids can be by calling get_sentinel_token_ids method - additional_special_tokens (`List[str]`, *optional*): + additional_special_tokens (`list[str]`, *optional*): Additional special tokens used by the tokenizer. add_prefix_space (`bool`, *optional*): Whether or not the tokenizer should automatically add a prefix space @@ -81,7 +81,7 @@ class T5TokenizerFast(PreTrainedTokenizerFast): model_input_names = ["input_ids", "attention_mask"] slow_tokenizer_class = T5Tokenizer - prefix_tokens: List[int] = [] + prefix_tokens: list[int] = [] def __init__( self, @@ -153,7 +153,7 @@ class T5TokenizerFast(PreTrainedTokenizerFast): return max_model_length - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " @@ -174,8 +174,8 @@ class T5TokenizerFast(PreTrainedTokenizerFast): return (out_vocab_file,) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -184,13 +184,13 @@ class T5TokenizerFast(PreTrainedTokenizerFast): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = token_ids_0 + [self.eos_token_id] if token_ids_1 is None: @@ -200,20 +200,20 @@ class T5TokenizerFast(PreTrainedTokenizerFast): return self.prefix_tokens + token_ids_0 + token_ids_1 def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] diff --git a/src/transformers/models/table_transformer/modeling_table_transformer.py b/src/transformers/models/table_transformer/modeling_table_transformer.py index d86966a82df..4938ea378df 100644 --- a/src/transformers/models/table_transformer/modeling_table_transformer.py +++ b/src/transformers/models/table_transformer/modeling_table_transformer.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -166,17 +166,17 @@ class TableTransformerObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - decoder_attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + decoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + decoder_attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None encoder_last_hidden_state: Optional[torch.FloatTensor] = None - encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None - encoder_attentions: Optional[Tuple[torch.FloatTensor]] = None + encoder_hidden_states: Optional[tuple[torch.FloatTensor]] = None + encoder_attentions: Optional[tuple[torch.FloatTensor]] = None # Copied from transformers.models.detr.modeling_detr.DetrFrozenBatchNorm2d with Detr->TableTransformer @@ -461,7 +461,7 @@ class TableTransformerAttention(nn.Module): key_value_states: Optional[torch.Tensor] = None, spatial_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer # for the decoder @@ -1100,7 +1100,7 @@ class TableTransformerModel(TableTransformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], TableTransformerModelOutput]: + ) -> Union[tuple[torch.FloatTensor], TableTransformerModelOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1255,11 +1255,11 @@ class TableTransformerForObjectDetection(TableTransformerPreTrainedModel): encoder_outputs: Optional[torch.FloatTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, - labels: Optional[List[Dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], TableTransformerObjectDetectionOutput]: + ) -> Union[tuple[torch.FloatTensor], TableTransformerObjectDetectionOutput]: r""" decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*): Not used by default. Can be used to mask object queries. @@ -1269,7 +1269,7 @@ class TableTransformerForObjectDetection(TableTransformerPreTrainedModel): decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*): Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an embedded representation. - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: 'class_labels' and 'boxes' (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding boxes diff --git a/src/transformers/models/tapas/configuration_tapas.py b/src/transformers/models/tapas/configuration_tapas.py index 58769e99a72..a40d4ba6393 100644 --- a/src/transformers/models/tapas/configuration_tapas.py +++ b/src/transformers/models/tapas/configuration_tapas.py @@ -60,7 +60,7 @@ class TapasConfig(PretrainedConfig): max_position_embeddings (`int`, *optional*, defaults to 1024): The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048). - type_vocab_sizes (`List[int]`, *optional*, defaults to `[3, 256, 256, 2, 256, 256, 10]`): + type_vocab_sizes (`list[int]`, *optional*, defaults to `[3, 256, 256, 2, 256, 256, 10]`): The vocabulary sizes of the `token_type_ids` passed when calling [`TapasModel`]. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. @@ -113,7 +113,7 @@ class TapasConfig(PretrainedConfig): Whether to restart position indexes at every cell (i.e. use relative position embeddings). disable_per_token_loss (`bool`, *optional*, defaults to `False`): Whether to disable any (strong or weak) supervision on cells. - aggregation_labels (`Dict[int, label]`, *optional*): + aggregation_labels (`dict[int, label]`, *optional*): The aggregation labels used to aggregate the results. For example, the WTQ models have the following aggregation labels: `{0: "NONE", 1: "SUM", 2: "AVERAGE", 3: "COUNT"}` no_aggregation_label_index (`int`, *optional*): diff --git a/src/transformers/models/tapas/modeling_tapas.py b/src/transformers/models/tapas/modeling_tapas.py index 5ac999f3cab..f6463d95a24 100644 --- a/src/transformers/models/tapas/modeling_tapas.py +++ b/src/transformers/models/tapas/modeling_tapas.py @@ -18,7 +18,7 @@ import enum import math import os from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -66,8 +66,8 @@ class TableQuestionAnsweringOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None logits_aggregation: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None def load_tf_weights_in_tapas(model, config, tf_checkpoint_path): @@ -427,9 +427,9 @@ class TapasAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -498,9 +498,9 @@ class TapasLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -778,7 +778,7 @@ class TapasModel(TapasPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, 7)`, *optional*): Token indices that encode tabular structure. Indices can be obtained using [`AutoTokenizer`]. See this @@ -926,7 +926,7 @@ class TapasForMaskedLM(TapasPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, 7)`, *optional*): Token indices that encode tabular structure. Indices can be obtained using [`AutoTokenizer`]. See this @@ -1066,7 +1066,7 @@ class TapasForQuestionAnswering(TapasPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TableQuestionAnsweringOutput]: + ) -> Union[tuple, TableQuestionAnsweringOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, 7)`, *optional*): Token indices that encode tabular structure. Indices can be obtained using [`AutoTokenizer`]. See this @@ -1389,7 +1389,7 @@ class TapasForSequenceClassification(TapasPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length, 7)`, *optional*): Token indices that encode tabular structure. Indices can be obtained using [`AutoTokenizer`]. See this diff --git a/src/transformers/models/tapas/modeling_tf_tapas.py b/src/transformers/models/tapas/modeling_tf_tapas.py index 4672260c169..5f0da5e5192 100644 --- a/src/transformers/models/tapas/modeling_tf_tapas.py +++ b/src/transformers/models/tapas/modeling_tf_tapas.py @@ -19,7 +19,7 @@ from __future__ import annotations import enum import math from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -113,8 +113,8 @@ class TFTableQuestionAnsweringOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None logits_aggregation: tf.Tensor | None = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None class TFTapasEmbeddings(keras.layers.Layer): @@ -275,10 +275,10 @@ class TFTapasSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -413,10 +413,10 @@ class TFTapasAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -530,10 +530,10 @@ class TFTapasLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -626,13 +626,13 @@ class TFTapasEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -790,7 +790,7 @@ class TFTapasLMPredictionHead(keras.layers.Layer): self.input_embeddings.weight = value self.input_embeddings.vocab_size = shape_list(value)[0] - def get_bias(self) -> Dict[str, tf.Variable]: + def get_bias(self) -> dict[str, tf.Variable]: return {"bias": self.bias} def set_bias(self, value: tf.Variable): @@ -869,7 +869,7 @@ class TFTapasMainLayer(keras.layers.Layer): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if input_ids is not None and inputs_embeds is not None: raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time") elif input_ids is not None: @@ -1027,7 +1027,7 @@ TAPAS_START_DOCSTRING = r""" TAPAS_INPUTS_DOCSTRING = r""" Args: - input_ids (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_ids (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1104,7 +1104,7 @@ class TFTapasModel(TFTapasPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: r""" Returns: @@ -1187,7 +1187,7 @@ class TFTapasForMaskedLM(TFTapasPreTrainedModel, TFMaskedLanguageModelingLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1409,7 +1409,7 @@ class TFTapasForQuestionAnswering(TFTapasPreTrainedModel): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTableQuestionAnsweringOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTableQuestionAnsweringOutput, tuple[tf.Tensor]]: r""" table_mask (`tf.Tensor` of shape `(batch_size, seq_length)`, *optional*): Mask for the table. Indicates which tokens belong to the table (1). Question tokens, table headers and @@ -1736,7 +1736,7 @@ class TFTapasForSequenceClassification(TFTapasPreTrainedModel, TFSequenceClassif return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/tapas/tokenization_tapas.py b/src/transformers/models/tapas/tokenization_tapas.py index e19ad5a5a34..87468ceb1a3 100644 --- a/src/transformers/models/tapas/tokenization_tapas.py +++ b/src/transformers/models/tapas/tokenization_tapas.py @@ -24,7 +24,7 @@ import re import unicodedata from collections.abc import Generator from dataclasses import dataclass -from typing import Callable, Dict, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np @@ -70,16 +70,16 @@ class TokenCoordinates: @dataclass class TokenizedTable: - rows: List[List[List[str]]] - selected_tokens: List[TokenCoordinates] + rows: list[list[list[str]]] + selected_tokens: list[TokenCoordinates] @dataclass(frozen=True) class SerializedExample: - tokens: List[str] - column_ids: List[int] - row_ids: List[int] - segment_ids: List[int] + tokens: list[str] + column_ids: list[int] + row_ids: list[int] + segment_ids: list[int] def _is_inner_wordpiece(token: str): @@ -255,7 +255,7 @@ class TapasTokenizer(PreTrainedTokenizer): min_question_length=None, max_question_length=None, model_max_length: int = 512, - additional_special_tokens: Optional[List[str]] = None, + additional_special_tokens: Optional[list[str]] = None, clean_up_tokenization_spaces=True, **kwargs, ): @@ -370,7 +370,7 @@ class TapasTokenizer(PreTrainedTokenizer): out_string = " ".join(tokens).replace(" ##", "").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: index = 0 if os.path.isdir(save_directory): vocab_file = os.path.join( @@ -390,84 +390,84 @@ class TapasTokenizer(PreTrainedTokenizer): index += 1 return (vocab_file,) - def create_attention_mask_from_sequences(self, query_ids: List[int], table_values: List[TableValue]) -> List[int]: + def create_attention_mask_from_sequences(self, query_ids: list[int], table_values: list[TableValue]) -> list[int]: """ Creates the attention mask according to the query token IDs and a list of table values. Args: - query_ids (`List[int]`): list of token IDs corresponding to the ID. - table_values (`List[TableValue]`): lift of table values, which are named tuples containing the + query_ids (`list[int]`): list of token IDs corresponding to the ID. + table_values (`list[TableValue]`): lift of table values, which are named tuples containing the token value, the column ID and the row ID of said token. Returns: - `List[int]`: List of ints containing the attention mask values. + `list[int]`: List of ints containing the attention mask values. """ return [1] * (1 + len(query_ids) + 1 + len(table_values)) def create_segment_token_type_ids_from_sequences( - self, query_ids: List[int], table_values: List[TableValue] - ) -> List[int]: + self, query_ids: list[int], table_values: list[TableValue] + ) -> list[int]: """ Creates the segment token type IDs according to the query token IDs and a list of table values. Args: - query_ids (`List[int]`): list of token IDs corresponding to the ID. - table_values (`List[TableValue]`): lift of table values, which are named tuples containing the + query_ids (`list[int]`): list of token IDs corresponding to the ID. + table_values (`list[TableValue]`): lift of table values, which are named tuples containing the token value, the column ID and the row ID of said token. Returns: - `List[int]`: List of ints containing the segment token type IDs values. + `list[int]`: List of ints containing the segment token type IDs values. """ table_ids = list(zip(*table_values))[0] if table_values else [] return [0] * (1 + len(query_ids) + 1) + [1] * len(table_ids) def create_column_token_type_ids_from_sequences( - self, query_ids: List[int], table_values: List[TableValue] - ) -> List[int]: + self, query_ids: list[int], table_values: list[TableValue] + ) -> list[int]: """ Creates the column token type IDs according to the query token IDs and a list of table values. Args: - query_ids (`List[int]`): list of token IDs corresponding to the ID. - table_values (`List[TableValue]`): lift of table values, which are named tuples containing the + query_ids (`list[int]`): list of token IDs corresponding to the ID. + table_values (`list[TableValue]`): lift of table values, which are named tuples containing the token value, the column ID and the row ID of said token. Returns: - `List[int]`: List of ints containing the column token type IDs values. + `list[int]`: List of ints containing the column token type IDs values. """ table_column_ids = list(zip(*table_values))[1] if table_values else [] return [0] * (1 + len(query_ids) + 1) + list(table_column_ids) def create_row_token_type_ids_from_sequences( - self, query_ids: List[int], table_values: List[TableValue] - ) -> List[int]: + self, query_ids: list[int], table_values: list[TableValue] + ) -> list[int]: """ Creates the row token type IDs according to the query token IDs and a list of table values. Args: - query_ids (`List[int]`): list of token IDs corresponding to the ID. - table_values (`List[TableValue]`): lift of table values, which are named tuples containing the + query_ids (`list[int]`): list of token IDs corresponding to the ID. + table_values (`list[TableValue]`): lift of table values, which are named tuples containing the token value, the column ID and the row ID of said token. Returns: - `List[int]`: List of ints containing the row token type IDs values. + `list[int]`: List of ints containing the row token type IDs values. """ table_row_ids = list(zip(*table_values))[2] if table_values else [] return [0] * (1 + len(query_ids) + 1) + list(table_row_ids) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a question and flattened table for question answering or sequence classification tasks by concatenating and adding special tokens. Args: - token_ids_0 (`List[int]`): The ids of the question. - token_ids_1 (`List[int]`, *optional*): The ids of the flattened table. + token_ids_0 (`list[int]`): The ids of the question. + token_ids_1 (`list[int]`, *optional*): The ids of the flattened table. Returns: - `List[int]`: The model input with special tokens. + `list[int]`: The model input with special tokens. """ if token_ids_1 is None: raise ValueError("With TAPAS, you must provide both question IDs and table IDs.") @@ -475,22 +475,22 @@ class TapasTokenizer(PreTrainedTokenizer): return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] + token_ids_1 def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of question IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): List of flattened table IDs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -511,13 +511,13 @@ class TapasTokenizer(PreTrainedTokenizer): TextInput, PreTokenizedInput, EncodedInput, - List[TextInput], - List[PreTokenizedInput], - List[EncodedInput], + list[TextInput], + list[PreTokenizedInput], + list[EncodedInput], ] ] = None, - answer_coordinates: Optional[Union[List[Tuple], List[List[Tuple]]]] = None, - answer_text: Optional[Union[List[TextInput], List[List[TextInput]]]] = None, + answer_coordinates: Optional[Union[list[tuple], list[list[tuple]]]] = None, + answer_text: Optional[Union[list[TextInput], list[list[TextInput]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -541,17 +541,17 @@ class TapasTokenizer(PreTrainedTokenizer): table (`pd.DataFrame`): Table containing tabular data. Note that all cell values must be text. Use *.astype(str)* on a Pandas dataframe to convert it to string. - queries (`str` or `List[str]`): + queries (`str` or `list[str]`): Question or batch of questions related to a table to be encoded. Note that in case of a batch, all questions must refer to the **same** table. - answer_coordinates (`List[Tuple]` or `List[List[Tuple]]`, *optional*): + answer_coordinates (`list[Tuple]` or `list[list[Tuple]]`, *optional*): Answer coordinates of each table-question pair in the batch. In case only a single table-question pair is provided, then the answer_coordinates must be a single list of one or more tuples. Each tuple must be a (row_index, column_index) pair. The first data row (not the column header row) has index 0. The first column has index 0. In case a batch of table-question pairs is provided, then the answer_coordinates must be a list of lists of tuples (each list corresponding to a single table-question pair). - answer_text (`List[str]` or `List[List[str]]`, *optional*): + answer_text (`list[str]` or `list[list[str]]`, *optional*): Answer text of each table-question pair in the batch. In case only a single table-question pair is provided, then the answer_text must be a single list of one or more strings. Each string must be the answer text of a corresponding answer coordinate. In case a batch of table-question pairs is provided, @@ -572,7 +572,7 @@ class TapasTokenizer(PreTrainedTokenizer): if not valid_query: raise ValueError( - "queries input must of type `str` (single example), `List[str]` (batch or single pretokenized" + "queries input must of type `str` (single example), `list[str]` (batch or single pretokenized" " example). " ) is_batched = isinstance(queries, (list, tuple)) @@ -628,13 +628,13 @@ class TapasTokenizer(PreTrainedTokenizer): table: "pd.DataFrame", queries: Optional[ Union[ - List[TextInput], - List[PreTokenizedInput], - List[EncodedInput], + list[TextInput], + list[PreTokenizedInput], + list[EncodedInput], ] ] = None, - answer_coordinates: Optional[List[List[Tuple]]] = None, - answer_text: Optional[List[List[TextInput]]] = None, + answer_coordinates: Optional[list[list[tuple]]] = None, + answer_text: Optional[list[list[TextInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -664,15 +664,15 @@ class TapasTokenizer(PreTrainedTokenizer): table (`pd.DataFrame`): Table containing tabular data. Note that all cell values must be text. Use *.astype(str)* on a Pandas dataframe to convert it to string. - queries (`List[str]`): + queries (`list[str]`): Batch of questions related to a table to be encoded. Note that all questions must refer to the **same** table. - answer_coordinates (`List[Tuple]` or `List[List[Tuple]]`, *optional*): + answer_coordinates (`list[Tuple]` or `list[list[Tuple]]`, *optional*): Answer coordinates of each table-question pair in the batch. Each tuple must be a (row_index, column_index) pair. The first data row (not the column header row) has index 0. The first column has index 0. The answer_coordinates must be a list of lists of tuples (each list corresponding to a single table-question pair). - answer_text (`List[str]` or `List[List[str]]`, *optional*): + answer_text (`list[str]` or `list[list[str]]`, *optional*): Answer text of each table-question pair in the batch. In case a batch of table-question pairs is provided, then the answer_coordinates must be a list of lists of strings (each list corresponding to a single table-question pair). Each string must be the answer text of a corresponding answer coordinate. @@ -738,12 +738,12 @@ class TapasTokenizer(PreTrainedTokenizer): self, table, queries: Union[ - List[TextInput], - List[PreTokenizedInput], - List[EncodedInput], + list[TextInput], + list[PreTokenizedInput], + list[EncodedInput], ], - answer_coordinates: Optional[List[List[Tuple]]] = None, - answer_text: Optional[List[List[TextInput]]] = None, + answer_coordinates: Optional[list[list[tuple]]] = None, + answer_text: Optional[list[list[TextInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -797,14 +797,14 @@ class TapasTokenizer(PreTrainedTokenizer): self, raw_table: "pd.DataFrame", raw_queries: Union[ - List[TextInput], - List[PreTokenizedInput], - List[EncodedInput], + list[TextInput], + list[PreTokenizedInput], + list[EncodedInput], ], tokenized_table: Optional[TokenizedTable] = None, - queries_tokens: Optional[List[List[str]]] = None, - answer_coordinates: Optional[List[List[Tuple]]] = None, - answer_text: Optional[List[List[TextInput]]] = None, + queries_tokens: Optional[list[list[str]]] = None, + answer_coordinates: Optional[list[list[tuple]]] = None, + answer_text: Optional[list[list[TextInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -884,7 +884,7 @@ class TapasTokenizer(PreTrainedTokenizer): max_length: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Prepare a table and a string for the model. This method does not return token type IDs, attention masks, etc. which are necessary for the model to work correctly. Use that method if you want to build your processing on @@ -894,7 +894,7 @@ class TapasTokenizer(PreTrainedTokenizer): table (`pd.DataFrame`): Table containing tabular data. Note that all cell values must be text. Use *.astype(str)* on a Pandas dataframe to convert it to string. - query (`str` or `List[str]`): + query (`str` or `list[str]`): Question related to a table to be encoded. """ encoded_inputs = self.encode_plus( @@ -921,8 +921,8 @@ class TapasTokenizer(PreTrainedTokenizer): EncodedInput, ] ] = None, - answer_coordinates: Optional[List[Tuple]] = None, - answer_text: Optional[List[TextInput]] = None, + answer_coordinates: Optional[list[tuple]] = None, + answer_text: Optional[list[TextInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -945,13 +945,13 @@ class TapasTokenizer(PreTrainedTokenizer): table (`pd.DataFrame`): Table containing tabular data. Note that all cell values must be text. Use *.astype(str)* on a Pandas dataframe to convert it to string. - query (`str` or `List[str]`): + query (`str` or `list[str]`): Question related to a table to be encoded. - answer_coordinates (`List[Tuple]` or `List[List[Tuple]]`, *optional*): + answer_coordinates (`list[Tuple]` or `list[list[Tuple]]`, *optional*): Answer coordinates of each table-question pair in the batch. The answer_coordinates must be a single list of one or more tuples. Each tuple must be a (row_index, column_index) pair. The first data row (not the column header row) has index 0. The first column has index 0. - answer_text (`List[str]` or `List[List[str]]`, *optional*): + answer_text (`list[str]` or `list[list[str]]`, *optional*): Answer text of each table-question pair in the batch. The answer_text must be a single list of one or more strings. Each string must be the answer text of a corresponding answer coordinate. """ @@ -1004,8 +1004,8 @@ class TapasTokenizer(PreTrainedTokenizer): PreTokenizedInput, EncodedInput, ], - answer_coordinates: Optional[List[Tuple]] = None, - answer_text: Optional[List[TextInput]] = None, + answer_coordinates: Optional[list[tuple]] = None, + answer_text: Optional[list[TextInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -1064,8 +1064,8 @@ class TapasTokenizer(PreTrainedTokenizer): ], tokenized_table: Optional[TokenizedTable] = None, query_tokens: Optional[TokenizedTable] = None, - answer_coordinates: Optional[List[Tuple]] = None, - answer_text: Optional[List[TextInput]] = None, + answer_coordinates: Optional[list[tuple]] = None, + answer_text: Optional[list[TextInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TapasTruncationStrategy] = False, @@ -1093,13 +1093,13 @@ class TapasTokenizer(PreTrainedTokenizer): The original query before any transformation (like tokenization) was applied to it. tokenized_table (`TokenizedTable`): The table after tokenization. - query_tokens (`List[str]`): + query_tokens (`list[str]`): The query after tokenization. - answer_coordinates (`List[Tuple]` or `List[List[Tuple]]`, *optional*): + answer_coordinates (`list[Tuple]` or `list[list[Tuple]]`, *optional*): Answer coordinates of each table-question pair in the batch. The answer_coordinates must be a single list of one or more tuples. Each tuple must be a (row_index, column_index) pair. The first data row (not the column header row) has index 0. The first column has index 0. - answer_text (`List[str]` or `List[List[str]]`, *optional*): + answer_text (`list[str]` or `list[list[str]]`, *optional*): Answer text of each table-question pair in the batch. The answer_text must be a single list of one or more strings. Each string must be the answer text of a corresponding answer coordinate. """ @@ -1250,18 +1250,18 @@ class TapasTokenizer(PreTrainedTokenizer): def _get_truncated_table_rows( self, - query_tokens: List[str], + query_tokens: list[str], tokenized_table: TokenizedTable, num_rows: int, num_columns: int, max_length: int, truncation_strategy: Union[str, TapasTruncationStrategy], - ) -> Tuple[int, int]: + ) -> tuple[int, int]: """ Truncates a sequence pair in-place following the strategy. Args: - query_tokens (`List[str]`): + query_tokens (`list[str]`): List of strings corresponding to the tokenized query. tokenized_table (`TokenizedTable`): Tokenized table @@ -1360,7 +1360,7 @@ class TapasTokenizer(PreTrainedTokenizer): sequence length of the model. Args: - question_tokens (`List[String]`): + question_tokens (`list[String]`): List of question tokens. Returns: `int`: the number of tokens left for the table, given the model max length. """ @@ -1772,7 +1772,7 @@ class TapasTokenizer(PreTrainedTokenizer): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -1784,7 +1784,7 @@ class TapasTokenizer(PreTrainedTokenizer): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. @@ -1908,10 +1908,10 @@ class TapasTokenizer(PreTrainedTokenizer): Returns: `tuple` comprising various elements depending on the inputs: - - predicted_answer_coordinates (`List[List[[tuple]]` of length `batch_size`): Predicted answer coordinates + - predicted_answer_coordinates (`list[list[[tuple]]` of length `batch_size`): Predicted answer coordinates as a list of lists of tuples. Each element in the list contains the predicted answer coordinates of a single example in the batch, as a list of tuples. Each tuple is a cell, i.e. (row index, column index). - - predicted_aggregation_indices (`List[int]`of length `batch_size`, *optional*, returned when + - predicted_aggregation_indices (`list[int]`of length `batch_size`, *optional*, returned when `logits_aggregation` is provided): Predicted aggregation operator indices of the aggregation head. """ # converting to numpy arrays to work with PT/TF @@ -2245,7 +2245,7 @@ class NumericValue: class NumericValueSpan: begin_index: Optional[int] = None end_index: Optional[int] = None - values: List[NumericValue] = None + values: list[NumericValue] = None @dataclass @@ -2258,7 +2258,7 @@ class Cell: class Question: original_text: str # The original raw question string. text: str # The question string after normalization. - numeric_spans: Optional[List[NumericValueSpan]] = None + numeric_spans: Optional[list[NumericValueSpan]] = None # Below: all functions from number_utils.py as well as 2 functions (namely get_all_spans and normalize_for_match) @@ -2545,8 +2545,8 @@ def parse_text(text): # - https://github.com/google-research/tapas/blob/master/tapas/utils/text_utils.py -_PrimitiveNumericValue = Union[float, Tuple[Optional[float], Optional[float], Optional[float]]] -_SortKeyFn = Callable[[NumericValue], Tuple[float, Ellipsis]] +_PrimitiveNumericValue = Union[float, tuple[Optional[float], Optional[float], Optional[float]]] +_SortKeyFn = Callable[[NumericValue], tuple[float, Ellipsis]] _DATE_TUPLE_SIZE = 3 diff --git a/src/transformers/models/textnet/configuration_textnet.py b/src/transformers/models/textnet/configuration_textnet.py index 61ecaaeba8e..90f73f54164 100644 --- a/src/transformers/models/textnet/configuration_textnet.py +++ b/src/transformers/models/textnet/configuration_textnet.py @@ -42,25 +42,25 @@ class TextNetConfig(BackboneConfigMixin, PretrainedConfig): The num of channels in out for the initial convolution layer. stem_act_func (`str`, *optional*, defaults to `"relu"`): The activation function for the initial convolution layer. - image_size (`Tuple[int, int]`, *optional*, defaults to `[640, 640]`): + image_size (`tuple[int, int]`, *optional*, defaults to `[640, 640]`): The size (resolution) of each image. - conv_layer_kernel_sizes (`List[List[List[int]]]`, *optional*): + conv_layer_kernel_sizes (`list[list[list[int]]]`, *optional*): A list of stage-wise kernel sizes. If `None`, defaults to: `[[[3, 3], [3, 3], [3, 3]], [[3, 3], [1, 3], [3, 3], [3, 1]], [[3, 3], [3, 3], [3, 1], [1, 3]], [[3, 3], [3, 1], [1, 3], [3, 3]]]`. - conv_layer_strides (`List[List[int]]`, *optional*): + conv_layer_strides (`list[list[int]]`, *optional*): A list of stage-wise strides. If `None`, defaults to: `[[1, 2, 1], [2, 1, 1, 1], [2, 1, 1, 1], [2, 1, 1, 1]]`. - hidden_sizes (`List[int]`, *optional*, defaults to `[64, 64, 128, 256, 512]`): + hidden_sizes (`list[int]`, *optional*, defaults to `[64, 64, 128, 256, 512]`): Dimensionality (hidden size) at each stage. batch_norm_eps (`float`, *optional*, defaults to 1e-05): The epsilon used by the batch normalization layers. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. diff --git a/src/transformers/models/textnet/image_processing_textnet.py b/src/transformers/models/textnet/image_processing_textnet.py index f75d1db097a..f274c6a6a74 100644 --- a/src/transformers/models/textnet/image_processing_textnet.py +++ b/src/transformers/models/textnet/image_processing_textnet.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for TextNet.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -56,7 +56,7 @@ class TextNetImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 640}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 640}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -67,7 +67,7 @@ class TextNetImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `False`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -78,10 +78,10 @@ class TextNetImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.485, 0.456, 0.406]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.485, 0.456, 0.406]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.229, 0.224, 0.225]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.229, 0.224, 0.225]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -94,16 +94,16 @@ class TextNetImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = False, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = IMAGENET_DEFAULT_MEAN, - image_std: Optional[Union[float, List[float]]] = IMAGENET_DEFAULT_STD, + image_mean: Optional[Union[float, list[float]]] = IMAGENET_DEFAULT_MEAN, + image_std: Optional[Union[float, list[float]]] = IMAGENET_DEFAULT_STD, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -148,7 +148,7 @@ class TextNetImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -161,7 +161,7 @@ class TextNetImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. size_divisor (`int`, *optional*, defaults to `32`): Ensures height and width are rounded to a multiple of this value after resizing. @@ -204,7 +204,7 @@ class TextNetImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, @@ -212,8 +212,8 @@ class TextNetImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -229,7 +229,7 @@ class TextNetImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. size_divisor (`int`, *optional*, defaults to `32`): @@ -239,7 +239,7 @@ class TextNetImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -247,9 +247,9 @@ class TextNetImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): diff --git a/src/transformers/models/textnet/modeling_textnet.py b/src/transformers/models/textnet/modeling_textnet.py index 6fd5e15d903..02c0fe4582c 100644 --- a/src/transformers/models/textnet/modeling_textnet.py +++ b/src/transformers/models/textnet/modeling_textnet.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch TextNet model.""" -from typing import Any, List, Optional, Tuple, Union +from typing import Any, Optional, Union import torch import torch.nn as nn @@ -245,7 +245,7 @@ class TextNetModel(TextNetPreTrainedModel): @auto_docstring def forward( self, pixel_values: Tensor, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None - ) -> Union[Tuple[Any, List[Any]], Tuple[Any], BaseModelOutputWithPoolingAndNoAttention]: + ) -> Union[tuple[Any, list[Any]], tuple[Any], BaseModelOutputWithPoolingAndNoAttention]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -381,7 +381,7 @@ class TextNetBackbone(TextNetPreTrainedModel, BackboneMixin): @auto_docstring def forward( self, pixel_values: Tensor, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None - ) -> Union[Tuple[Tuple], BackboneOutput]: + ) -> Union[tuple[tuple], BackboneOutput]: r""" Examples: diff --git a/src/transformers/models/time_series_transformer/configuration_time_series_transformer.py b/src/transformers/models/time_series_transformer/configuration_time_series_transformer.py index bc063774389..a42e95ccc5d 100644 --- a/src/transformers/models/time_series_transformer/configuration_time_series_transformer.py +++ b/src/transformers/models/time_series_transformer/configuration_time_series_transformer.py @@ -14,7 +14,7 @@ # limitations under the License. """Time Series Transformer model configuration""" -from typing import List, Optional, Union +from typing import Optional, Union from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -135,14 +135,14 @@ class TimeSeriesTransformerConfig(PretrainedConfig): distribution_output: str = "student_t", loss: str = "nll", input_size: int = 1, - lags_sequence: List[int] = [1, 2, 3, 4, 5, 6, 7], + lags_sequence: list[int] = [1, 2, 3, 4, 5, 6, 7], scaling: Optional[Union[str, bool]] = "mean", num_dynamic_real_features: int = 0, num_static_categorical_features: int = 0, num_static_real_features: int = 0, num_time_features: int = 0, - cardinality: Optional[List[int]] = None, - embedding_dimension: Optional[List[int]] = None, + cardinality: Optional[list[int]] = None, + embedding_dimension: Optional[list[int]] = None, encoder_ffn_dim: int = 32, decoder_ffn_dim: int = 32, encoder_attention_heads: int = 2, diff --git a/src/transformers/models/time_series_transformer/modeling_time_series_transformer.py b/src/transformers/models/time_series_transformer/modeling_time_series_transformer.py index d6101489113..83b69af4a10 100644 --- a/src/transformers/models/time_series_transformer/modeling_time_series_transformer.py +++ b/src/transformers/models/time_series_transformer/modeling_time_series_transformer.py @@ -15,7 +15,7 @@ # limitations under the License. """PyTorch Time Series Transformer model.""" -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -62,7 +62,7 @@ class TimeSeriesFeatureEmbedder(nn.Module): List of embedding dimensions of the categorical features. """ - def __init__(self, cardinalities: List[int], embedding_dims: List[int]) -> None: + def __init__(self, cardinalities: list[int], embedding_dims: list[int]) -> None: super().__init__() self.num_features = len(cardinalities) @@ -99,7 +99,7 @@ class TimeSeriesStdScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -135,7 +135,7 @@ class TimeSeriesMeanScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: torch.Tensor - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -186,7 +186,7 @@ class TimeSeriesNOPScaler(nn.Module): def forward( self, data: torch.Tensor, observed_indicator: Optional[torch.Tensor] = None - ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: """ Parameters: data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`): @@ -362,7 +362,7 @@ class TimeSeriesTransformerAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -461,7 +461,7 @@ class TimeSeriesTransformerEncoderLayer(nn.Module): attention_mask: torch.FloatTensor, layer_head_mask: torch.FloatTensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: + ) -> tuple[torch.FloatTensor, Optional[torch.FloatTensor]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -551,7 +551,7 @@ class TimeSeriesTransformerDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, cache_position: Optional[torch.Tensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -787,7 +787,7 @@ class TimeSeriesTransformerEncoder(TimeSeriesTransformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -924,14 +924,14 @@ class TimeSeriesTransformerDecoder(TimeSeriesTransformerPreTrainedModel): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithPastAndCrossAttentions]: r""" Args: attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1282,14 +1282,14 @@ class TimeSeriesTransformerModel(TimeSeriesTransformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Seq2SeqTSModelOutput, Tuple]: + ) -> Union[Seq2SeqTSModelOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`): Past values of the time series, that serve as context in order to predict the future. The sequence size of @@ -1544,14 +1544,14 @@ class TimeSeriesTransformerForPrediction(TimeSeriesTransformerPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, output_hidden_states: Optional[bool] = None, output_attentions: Optional[bool] = None, use_cache: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Seq2SeqTSModelOutput, Tuple]: + ) -> Union[Seq2SeqTSModelOutput, tuple]: r""" past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`): Past values of the time series, that serve as context in order to predict the future. The sequence size of diff --git a/src/transformers/models/timesfm/configuration_timesfm.py b/src/transformers/models/timesfm/configuration_timesfm.py index bd371cf1b21..04566ab34ab 100644 --- a/src/transformers/models/timesfm/configuration_timesfm.py +++ b/src/transformers/models/timesfm/configuration_timesfm.py @@ -14,8 +14,6 @@ # limitations under the License. """TimesFM model configuration""" -from typing import List - from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -57,7 +55,7 @@ class TimesFmConfig(PretrainedConfig): The tolerance for the quantile loss. rms_norm_eps (`float`, *optional*, defaults to 1e-06): The epsilon used by the RMS normalization layers. - quantiles (`List[float]`, *optional*, defaults to `[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]`): + quantiles (`list[float]`, *optional*, defaults to `[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]`): The quantiles to predict. pad_val (`float`, *optional*, defaults to 1123581321.0): The value used to pad the predictions. @@ -92,7 +90,7 @@ class TimesFmConfig(PretrainedConfig): num_attention_heads: int = 16, tolerance: float = 1e-6, rms_norm_eps: float = 1e-6, - quantiles: List[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], + quantiles: list[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], pad_val: float = 1123581321.0, attention_dropout: float = 0.0, use_positional_embedding: bool = False, diff --git a/src/transformers/models/timesformer/modeling_timesformer.py b/src/transformers/models/timesformer/modeling_timesformer.py index 31454a7108c..19358f0c30e 100644 --- a/src/transformers/models/timesformer/modeling_timesformer.py +++ b/src/transformers/models/timesformer/modeling_timesformer.py @@ -15,7 +15,7 @@ """PyTorch TimeSformer model.""" import collections -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn.functional @@ -245,7 +245,7 @@ class TimeSformerAttention(nn.Module): self, hidden_states: torch.Tensor, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, output_attentions) attention_output = self.output(self_outputs[0]) @@ -512,7 +512,7 @@ class TimesformerModel(TimesformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: r""" Examples: @@ -531,7 +531,7 @@ class TimesformerModel(TimesformerPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -555,7 +555,7 @@ class TimesformerModel(TimesformerPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) @@ -642,7 +642,7 @@ class TimesformerForVideoClassification(TimesformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutput]: + ) -> Union[tuple, ImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., @@ -667,7 +667,7 @@ class TimesformerForVideoClassification(TimesformerPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -691,7 +691,7 @@ class TimesformerForVideoClassification(TimesformerPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/timm_backbone/configuration_timm_backbone.py b/src/transformers/models/timm_backbone/configuration_timm_backbone.py index 6000698c924..7f8c1c22e9f 100644 --- a/src/transformers/models/timm_backbone/configuration_timm_backbone.py +++ b/src/transformers/models/timm_backbone/configuration_timm_backbone.py @@ -40,7 +40,7 @@ class TimmBackboneConfig(PretrainedConfig): Whether to output only the features or also the logits. use_pretrained_backbone (`bool`, *optional*, defaults to `True`): Whether to use a pretrained backbone. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). Will default to the last stage if unset. freeze_batch_norm_2d (`bool`, *optional*, defaults to `False`): diff --git a/src/transformers/models/timm_backbone/modeling_timm_backbone.py b/src/transformers/models/timm_backbone/modeling_timm_backbone.py index 60a8b404635..91e0224866b 100644 --- a/src/transformers/models/timm_backbone/modeling_timm_backbone.py +++ b/src/transformers/models/timm_backbone/modeling_timm_backbone.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch @@ -126,7 +126,7 @@ class TimmBackbone(PreTrainedModel, BackboneMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[BackboneOutput, Tuple[Tensor, ...]]: + ) -> Union[BackboneOutput, tuple[Tensor, ...]]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/timm_wrapper/configuration_timm_wrapper.py b/src/transformers/models/timm_wrapper/configuration_timm_wrapper.py index 4313ad854ad..3d542de6aa7 100644 --- a/src/transformers/models/timm_wrapper/configuration_timm_wrapper.py +++ b/src/transformers/models/timm_wrapper/configuration_timm_wrapper.py @@ -15,7 +15,7 @@ """Configuration for TimmWrapper models""" -from typing import Any, Dict +from typing import Any from ...configuration_utils import PretrainedConfig from ...utils import is_timm_available, logging, requires_backends @@ -66,7 +66,7 @@ class TimmWrapperConfig(PretrainedConfig): super().__init__(**kwargs) @classmethod - def from_dict(cls, config_dict: Dict[str, Any], **kwargs): + def from_dict(cls, config_dict: dict[str, Any], **kwargs): label_names = config_dict.get("label_names", None) is_custom_model = "num_labels" in kwargs or "id2label" in kwargs @@ -105,7 +105,7 @@ class TimmWrapperConfig(PretrainedConfig): return super().from_dict(config_dict, **kwargs) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: output = super().to_dict() output["num_classes"] = self.num_labels output["label_names"] = list(self.id2label.values()) diff --git a/src/transformers/models/timm_wrapper/image_processing_timm_wrapper.py b/src/transformers/models/timm_wrapper/image_processing_timm_wrapper.py index de54f8a936c..94ca3efedb1 100644 --- a/src/transformers/models/timm_wrapper/image_processing_timm_wrapper.py +++ b/src/transformers/models/timm_wrapper/image_processing_timm_wrapper.py @@ -14,7 +14,7 @@ # limitations under the License. import os -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch @@ -41,7 +41,7 @@ class TimmWrapperImageProcessor(BaseImageProcessor): Wrapper class for timm models to be used within transformers. Args: - pretrained_cfg (`Dict[str, Any]`): + pretrained_cfg (`dict[str, Any]`): The configuration of the pretrained model used to resolve evaluation and training transforms. architecture (`Optional[str]`, *optional*): @@ -52,7 +52,7 @@ class TimmWrapperImageProcessor(BaseImageProcessor): def __init__( self, - pretrained_cfg: Dict[str, Any], + pretrained_cfg: dict[str, Any], architecture: Optional[str] = None, **kwargs, ): @@ -72,7 +72,7 @@ class TimmWrapperImageProcessor(BaseImageProcessor): transform.__class__.__name__ == "ToTensor" for transform in self.val_transforms.transforms ) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. """ @@ -85,7 +85,7 @@ class TimmWrapperImageProcessor(BaseImageProcessor): @classmethod def get_image_processor_dict( cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs - ) -> Tuple[Dict[str, Any], Dict[str, Any]]: + ) -> tuple[dict[str, Any], dict[str, Any]]: """ Get the image processor dict for the model. """ diff --git a/src/transformers/models/timm_wrapper/modeling_timm_wrapper.py b/src/transformers/models/timm_wrapper/modeling_timm_wrapper.py index 28fb807ccd8..57c96aef27b 100644 --- a/src/transformers/models/timm_wrapper/modeling_timm_wrapper.py +++ b/src/transformers/models/timm_wrapper/modeling_timm_wrapper.py @@ -14,7 +14,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import Tensor, nn @@ -52,8 +52,8 @@ class TimmWrapperModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor pooler_output: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring @@ -71,7 +71,7 @@ class TimmWrapperPreTrainedModel(PreTrainedModel): super().__init__(*args, **kwargs) @staticmethod - def _fix_state_dict_key_on_load(key) -> Tuple[str, bool]: + def _fix_state_dict_key_on_load(key) -> tuple[str, bool]: """ Overrides original method that renames `gamma` and `beta` to `weight` and `bias`. We don't want this behavior for timm wrapped models. Instead, this method adds a @@ -124,11 +124,11 @@ class TimmWrapperModel(TimmWrapperPreTrainedModel): self, pixel_values: torch.FloatTensor, output_attentions: Optional[bool] = None, - output_hidden_states: Optional[Union[bool, List[int]]] = None, + output_hidden_states: Optional[Union[bool, list[int]]] = None, return_dict: Optional[bool] = None, do_pooling: Optional[bool] = None, **kwargs, - ) -> Union[TimmWrapperModelOutput, Tuple[Tensor, ...]]: + ) -> Union[TimmWrapperModelOutput, tuple[Tensor, ...]]: r""" output_attentions (`bool`, *optional*): Whether or not to return the attentions tensors of all attention layers. Not compatible with timm wrapped models. @@ -243,10 +243,10 @@ class TimmWrapperForImageClassification(TimmWrapperPreTrainedModel): pixel_values: torch.FloatTensor, labels: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, - output_hidden_states: Optional[Union[bool, List[int]]] = None, + output_hidden_states: Optional[Union[bool, list[int]]] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[ImageClassifierOutput, Tuple[Tensor, ...]]: + ) -> Union[ImageClassifierOutput, tuple[Tensor, ...]]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/trocr/modeling_trocr.py b/src/transformers/models/trocr/modeling_trocr.py index 7a75ffcbeaa..11b5b4a415f 100644 --- a/src/transformers/models/trocr/modeling_trocr.py +++ b/src/transformers/models/trocr/modeling_trocr.py @@ -16,7 +16,7 @@ import copy import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -182,11 +182,11 @@ class TrOCRAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -331,7 +331,7 @@ class TrOCRDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ): @@ -764,14 +764,14 @@ class TrOCRForCausalLM(TrOCRPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*): Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`: diff --git a/src/transformers/models/trocr/processing_trocr.py b/src/transformers/models/trocr/processing_trocr.py index 6fb5f281ecd..fe5b7901b12 100644 --- a/src/transformers/models/trocr/processing_trocr.py +++ b/src/transformers/models/trocr/processing_trocr.py @@ -18,7 +18,7 @@ Processor class for TrOCR. import warnings from contextlib import contextmanager -from typing import List, Union +from typing import Union from ...image_processing_utils import BatchFeature from ...image_utils import ImageInput @@ -72,7 +72,7 @@ class TrOCRProcessor(ProcessorMixin): def __call__( self, images: ImageInput = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[TrOCRProcessorKwargs], diff --git a/src/transformers/models/tvp/configuration_tvp.py b/src/transformers/models/tvp/configuration_tvp.py index 803acb220d0..4a48574e11e 100644 --- a/src/transformers/models/tvp/configuration_tvp.py +++ b/src/transformers/models/tvp/configuration_tvp.py @@ -189,7 +189,7 @@ class TvpConfig(PretrainedConfig): Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) if output["backbone_config"] is not None: diff --git a/src/transformers/models/tvp/image_processing_tvp.py b/src/transformers/models/tvp/image_processing_tvp.py index 119a88c9c8f..d906c5e6bc7 100644 --- a/src/transformers/models/tvp/image_processing_tvp.py +++ b/src/transformers/models/tvp/image_processing_tvp.py @@ -15,7 +15,7 @@ """Image processor class for TVP.""" from collections.abc import Iterable -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -50,7 +50,7 @@ logger = logging.get_logger(__name__) # Copied from transformers.models.vivit.image_processing_vivit.make_batched -def make_batched(videos) -> List[List[ImageInput]]: +def make_batched(videos) -> list[list[ImageInput]]: if isinstance(videos, (list, tuple)) and isinstance(videos[0], (list, tuple)) and is_valid_image(videos[0][0]): return videos @@ -67,7 +67,7 @@ def get_resize_output_image_size( input_image: np.ndarray, max_size: int = 448, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: height, width = get_image_size(input_image, input_data_format) if height >= width: ratio = width * 1.0 / height @@ -90,7 +90,7 @@ class TvpImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"longest_edge": 448}`): + size (`dict[str, int]` *optional*, defaults to `{"longest_edge": 448}`): Size of the output image after resizing. The longest edge of the image will be resized to `size["longest_edge"]` while maintaining the aspect ratio of the original image. Can be overridden by `size` in the `preprocess` method. @@ -100,7 +100,7 @@ class TvpImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): Size of the image after applying the center crop. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -111,7 +111,7 @@ class TvpImageProcessor(BaseImageProcessor): in the `preprocess` method. do_pad (`bool`, *optional*, defaults to `True`): Whether to pad the image. Can be overridden by the `do_pad` parameter in the `preprocess` method. - pad_size (`Dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): + pad_size (`dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): Size of the image after applying the padding. Can be overridden by the `pad_size` parameter in the `preprocess` method. constant_values (`Union[float, Iterable[float]]`, *optional*, defaults to 0): @@ -124,10 +124,10 @@ class TvpImageProcessor(BaseImageProcessor): do_flip_channel_order (`bool`, *optional*, defaults to `True`): Whether to flip the color channels from RGB to BGR. Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -137,20 +137,20 @@ class TvpImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, constant_values: Union[float, Iterable[float]] = 0, pad_mode: PaddingMode = PaddingMode.CONSTANT, do_normalize: bool = True, do_flip_channel_order: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -177,7 +177,7 @@ class TvpImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -189,7 +189,7 @@ class TvpImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. If `size` is of the form `{"height": h, "width": w}`, the output image will have the size `(h, w)`. If `size` is of the form `{"longest_edge": s}`, the output image will have its longest edge of length `s` while keeping the aspect ratio of the original image. @@ -220,7 +220,7 @@ class TvpImageProcessor(BaseImageProcessor): def pad_image( self, image: np.ndarray, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, constant_values: Union[float, Iterable[float]] = 0, pad_mode: PaddingMode = PaddingMode.CONSTANT, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -233,7 +233,7 @@ class TvpImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to pad. - pad_size (`Dict[str, int]`) + pad_size (`dict[str, int]`) Size of the output image with pad. constant_values (`Union[float, Iterable[float]]`) The fill value to use when padding the image. @@ -268,20 +268,20 @@ class TvpImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, constant_values: Optional[Union[float, Iterable[float]]] = None, pad_mode: PaddingMode = None, do_normalize: Optional[bool] = None, do_flip_channel_order: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, **kwargs, @@ -340,22 +340,22 @@ class TvpImageProcessor(BaseImageProcessor): @filter_out_non_signature_kwargs() def preprocess( self, - videos: Union[ImageInput, List[ImageInput], List[List[ImageInput]]], + videos: Union[ImageInput, list[ImageInput], list[list[ImageInput]]], do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_pad: Optional[bool] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, constant_values: Optional[Union[float, Iterable[float]]] = None, pad_mode: PaddingMode = None, do_normalize: Optional[bool] = None, do_flip_channel_order: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -364,18 +364,18 @@ class TvpImageProcessor(BaseImageProcessor): Preprocess an image or batch of images. Args: - videos (`ImageInput` or `List[ImageInput]` or `List[List[ImageInput]]`): + videos (`ImageInput` or `list[ImageInput]` or `list[list[ImageInput]]`): Frames to preprocess. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after applying resize. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_centre_crop`): Whether to centre crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after applying the centre crop. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -383,7 +383,7 @@ class TvpImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_pad (`bool`, *optional*, defaults to `True`): Whether to pad the image. Can be overridden by the `do_pad` parameter in the `preprocess` method. - pad_size (`Dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): + pad_size (`dict[str, int]`, *optional*, defaults to `{"height": 448, "width": 448}`): Size of the image after applying the padding. Can be overridden by the `pad_size` parameter in the `preprocess` method. constant_values (`Union[float, Iterable[float]]`, *optional*, defaults to 0): @@ -394,9 +394,9 @@ class TvpImageProcessor(BaseImageProcessor): Whether to normalize the image. do_flip_channel_order (`bool`, *optional*, defaults to `self.do_flip_channel_order`): Whether to flip the channel order of the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/tvp/modeling_tvp.py b/src/transformers/models/tvp/modeling_tvp.py index 02ff2c8c79c..01932573f01 100644 --- a/src/transformers/models/tvp/modeling_tvp.py +++ b/src/transformers/models/tvp/modeling_tvp.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple +from typing import Optional import torch import torch.utils.checkpoint @@ -54,8 +54,8 @@ class TvpVideoGroundingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None class TvpLoss(nn.Module): @@ -65,7 +65,7 @@ class TvpLoss(nn.Module): ground-truth / prediction (supervise class and box). Args: - losses (`List[str]`): + losses (`list[str]`): List of all the losses to be applied. """ @@ -122,7 +122,7 @@ class TvpLoss(nn.Module): Args: logits (`torch.FloatTensor`): The output logits of head module. - labels (`List[torch.FloatTensor]`): + labels (`list[torch.FloatTensor]`): List of tensors ([start, end, duration]), which contains start time, end time of the video corresponding to the text, and also the duration. """ duration, start_time, end_time = labels @@ -862,7 +862,7 @@ class TvpForVideoGrounding(TvpPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, pixel_values: Optional[torch.FloatTensor] = None, attention_mask: Optional[torch.LongTensor] = None, - labels: Optional[Tuple[torch.Tensor]] = None, + labels: Optional[tuple[torch.Tensor]] = None, head_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, diff --git a/src/transformers/models/tvp/processing_tvp.py b/src/transformers/models/tvp/processing_tvp.py index 76baae91346..f24b9a6fc19 100644 --- a/src/transformers/models/tvp/processing_tvp.py +++ b/src/transformers/models/tvp/processing_tvp.py @@ -55,12 +55,12 @@ class TvpProcessor(ProcessorMixin): the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, `List[List[PIL.Image.Image]]`, `List[List[np.ndarray]]`,: - `List[List[torch.Tensor]]`): The video or batch of videos to be prepared. Each video should be a list + videos (`list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`, `list[list[PIL.Image.Image]]`, `list[list[np.ndarray]]`,: + `list[list[torch.Tensor]]`): The video or batch of videos to be prepared. Each video should be a list of frames, which can be either PIL images or NumPy arrays. In case of NumPy arrays/PyTorch tensors, each frame should be of shape (H, W, C), where H and W are frame height and width, and C is a number of channels. diff --git a/src/transformers/models/udop/configuration_udop.py b/src/transformers/models/udop/configuration_udop.py index 6ed28c78c8f..d55717af2b8 100644 --- a/src/transformers/models/udop/configuration_udop.py +++ b/src/transformers/models/udop/configuration_udop.py @@ -52,7 +52,7 @@ class UdopConfig(PretrainedConfig): The number of buckets to use for each attention layer. relative_attention_max_distance (`int`, *optional*, defaults to 128): The maximum distance of the longer sequences for the bucket separation. - relative_bias_args (`List[dict]`, *optional*, defaults to `[{'type': '1d'}, {'type': 'horizontal'}, {'type': 'vertical'}]`): + relative_bias_args (`list[dict]`, *optional*, defaults to `[{'type': '1d'}, {'type': 'horizontal'}, {'type': 'vertical'}]`): A list of dictionaries containing the arguments for the relative bias layers. dropout_rate (`float`, *optional*, defaults to 0.1): The ratio for all dropout layers. diff --git a/src/transformers/models/udop/modeling_udop.py b/src/transformers/models/udop/modeling_udop.py index 537f87641c8..4cca934bcea 100644 --- a/src/transformers/models/udop/modeling_udop.py +++ b/src/transformers/models/udop/modeling_udop.py @@ -22,7 +22,7 @@ from abc import ABC, abstractmethod from collections.abc import Sequence from copy import deepcopy from dataclasses import dataclass -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Optional, Union import torch from torch import Tensor, nn @@ -94,10 +94,10 @@ class BaseModelOutputWithAttentionMask(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None attention_mask: Optional[torch.FloatTensor] = None - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None - cross_attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None + cross_attentions: Optional[tuple[torch.FloatTensor]] = None def get_visual_bbox(image_size=224, patch_size=16): @@ -928,11 +928,11 @@ class RelativePositionBiasBase(nn.Module, ABC): def prepare_input( self, attention_mask: Optional[Tensor] = None, - bbox: Optional[Dict[str, Any]] = None, + bbox: Optional[dict[str, Any]] = None, ) -> Tensor: pass - def get_bucket(self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None) -> Tensor: + def get_bucket(self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None) -> Tensor: relative_position = self.prepare_input(attention_mask, bbox) rp_bucket: Tensor = get_relative_position_bucket( relative_position, @@ -952,7 +952,7 @@ class RelativePositionBiasBase(nn.Module, ABC): return relative_position.to(torch.long) - def forward(self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None) -> Tensor: + def forward(self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None) -> Tensor: # re-using pretrained model with subsequent addition of prefix_bucket if self.expand and self.prefix_bucket: new_bias = nn.Embedding(self.relative_attention_num_buckets + 2, self.num_heads) @@ -989,7 +989,7 @@ class RelativePositionBias1D(RelativePositionBiasBase): """ super().__init__(scaling_factor=scaling_factor, max_distance=max_distance, **kwargs) - def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None) -> Tensor: + def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None) -> Tensor: if self.scaling_factor != 1: raise ValueError("No need to scale 1d features") relative_position = self.get_relative_position( @@ -1007,7 +1007,7 @@ class RelativePositionBiasHorizontal(RelativePositionBiasBase): """ super().__init__(scaling_factor=scaling_factor, max_distance=max_distance, **kwargs) - def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None) -> Tensor: + def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None) -> Tensor: if not self.scaling_factor > 1.0: raise ValueError("Need to scale the values of bboxes, as there are in small (0,1) range") if bbox is None: @@ -1026,7 +1026,7 @@ class RelativePositionBiasVertical(RelativePositionBiasBase): """ super().__init__(scaling_factor=scaling_factor, max_distance=max_distance, **kwargs) - def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None) -> Tensor: + def prepare_input(self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None) -> Tensor: if not self.scaling_factor > 1.0: raise ValueError("Need to scale the values of bboxes, as there are in small (0,1) range") if bbox is None: @@ -1050,7 +1050,7 @@ class RelativePositionBiasAggregated(nn.Module): self.biases = nn.ModuleList(modules) def forward( - self, attention_mask: Optional[Tensor] = None, bbox: Optional[Dict[str, Any]] = None + self, attention_mask: Optional[Tensor] = None, bbox: Optional[dict[str, Any]] = None ) -> Union[float, Tensor]: output = 0.0 for bias in self.biases: # type: ignore @@ -1539,9 +1539,9 @@ class UdopModel(UdopPreTrainedModel): self, input_ids: Optional[Tensor] = None, attention_mask: Optional[Tensor] = None, - bbox: Optional[Dict[str, Any]] = None, + bbox: Optional[dict[str, Any]] = None, pixel_values: Optional[Tensor] = None, - visual_bbox: Optional[Dict[str, Any]] = None, + visual_bbox: Optional[dict[str, Any]] = None, decoder_input_ids: Optional[Tensor] = None, decoder_attention_mask: Optional[Tensor] = None, inputs_embeds: Optional[Tensor] = None, @@ -1556,7 +1556,7 @@ class UdopModel(UdopPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[Tensor, ...]: + ) -> tuple[Tensor, ...]: r""" bbox (`torch.LongTensor` of shape `({0}, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1744,9 +1744,9 @@ class UdopForConditionalGeneration(UdopPreTrainedModel, GenerationMixin): self, input_ids: Optional[Tensor] = None, attention_mask: Optional[Tensor] = None, - bbox: Optional[Dict[str, Any]] = None, + bbox: Optional[dict[str, Any]] = None, pixel_values: Optional[Tensor] = None, - visual_bbox: Optional[Dict[str, Any]] = None, + visual_bbox: Optional[dict[str, Any]] = None, decoder_input_ids: Optional[Tensor] = None, decoder_attention_mask: Optional[Tensor] = None, inputs_embeds: Optional[Tensor] = None, @@ -1762,7 +1762,7 @@ class UdopForConditionalGeneration(UdopPreTrainedModel, GenerationMixin): return_dict: Optional[bool] = None, labels: Optional[Tensor] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[Tensor, ...]: + ) -> tuple[Tensor, ...]: r""" bbox (`torch.LongTensor` of shape `({0}, 4)`, *optional*): Bounding boxes of each input sequence tokens. Selected in the range `[0, @@ -1980,16 +1980,16 @@ class UdopEncoderModel(UdopPreTrainedModel): def forward( self, input_ids: Optional[Tensor] = None, - bbox: Optional[Dict[str, Any]] = None, + bbox: Optional[dict[str, Any]] = None, attention_mask: Optional[Tensor] = None, pixel_values: Optional[Tensor] = None, - visual_bbox: Optional[Dict[str, Any]] = None, + visual_bbox: Optional[dict[str, Any]] = None, head_mask: Optional[Tensor] = None, inputs_embeds: Optional[Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutputWithAttentionMask]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutputWithAttentionMask]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. T5 is a model with relative position embeddings so you diff --git a/src/transformers/models/udop/processing_udop.py b/src/transformers/models/udop/processing_udop.py index ce8847a5c3a..c29bb25d777 100644 --- a/src/transformers/models/udop/processing_udop.py +++ b/src/transformers/models/udop/processing_udop.py @@ -16,7 +16,7 @@ Processor class for UDOP. """ -from typing import List, Optional, Union +from typing import Optional, Union from transformers import logging @@ -30,8 +30,8 @@ logger = logging.get_logger(__name__) class UdopTextKwargs(TextKwargs, total=False): - word_labels: Optional[Union[List[int], List[List[int]]]] - boxes: Union[List[List[int]], List[List[List[int]]]] + word_labels: Optional[Union[list[int], list[list[int]]]] + boxes: Union[list[list[int]], list[list[list[int]]]] class UdopProcessorKwargs(ProcessingKwargs, total=False): @@ -86,7 +86,7 @@ class UdopProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, # The following is to capture `text_pair` argument that may be passed as a positional argument. # See transformers.processing_utils.ProcessorMixin.prepare_and_validate_optional_call_args for more details, # or this conversation for more context: https://github.com/huggingface/transformers/pull/32544#discussion_r1720208116 diff --git a/src/transformers/models/udop/tokenization_udop.py b/src/transformers/models/udop/tokenization_udop.py index 86ae0744d54..29b4b3ee24e 100644 --- a/src/transformers/models/udop/tokenization_udop.py +++ b/src/transformers/models/udop/tokenization_udop.py @@ -18,7 +18,7 @@ import os import re import warnings from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import sentencepiece as spm @@ -182,16 +182,16 @@ class UdopTokenizer(PreTrainedTokenizer): pad_token (`str`, *optional*, defaults to `""`): The token used for padding, for example when batching sequences of different lengths. - sep_token_box (`List[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): + sep_token_box (`list[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's CrossEntropyLoss. only_label_first_subword (`bool`, *optional*, defaults to `True`): Whether or not to only label the first subword, in case word labels are provided. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): @@ -255,7 +255,7 @@ class UdopTokenizer(PreTrainedTokenizer): pad_token_label=-100, only_label_first_subword=True, additional_special_tokens=None, - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, legacy=True, add_prefix_space=True, **kwargs, @@ -308,22 +308,22 @@ class UdopTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: return super().get_special_tokens_mask( @@ -346,7 +346,7 @@ class UdopTokenizer(PreTrainedTokenizer): return [self.convert_tokens_to_ids(token) for token in self.get_sentinel_tokens()] # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer._add_eos_if_not_present - def _add_eos_if_not_present(self, token_ids: List[int]) -> List[int]: + def _add_eos_if_not_present(self, token_ids: list[int]) -> list[int]: """Do not add eos again if user already added it.""" if len(token_ids) > 0 and token_ids[-1] == self.eos_token_id: warnings.warn( @@ -359,20 +359,20 @@ class UdopTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.create_token_type_ids_from_sequences def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ eos = [self.eos_token_id] @@ -382,8 +382,8 @@ class UdopTokenizer(PreTrainedTokenizer): # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.build_inputs_with_special_tokens def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A sequence has the following format: @@ -392,13 +392,13 @@ class UdopTokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ token_ids_0 = self._add_eos_if_not_present(token_ids_0) if token_ids_1 is None: @@ -419,7 +419,7 @@ class UdopTokenizer(PreTrainedTokenizer): self.sp_model.Load(self.vocab_file) # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.tokenize - def tokenize(self, text: "TextInput", **kwargs) -> List[str]: + def tokenize(self, text: "TextInput", **kwargs) -> list[str]: """ Converts a string to a list of tokens. If `self.legacy` is set to `False`, a prefix token is added unless the first token is special. @@ -489,7 +489,7 @@ class UdopTokenizer(PreTrainedTokenizer): return out_string.strip() # Copied from transformers.models.t5.tokenization_t5.T5Tokenizer.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -509,13 +509,13 @@ class UdopTokenizer(PreTrainedTokenizer): @add_end_docstrings(UDOP_ENCODE_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, **kwargs, ) -> BatchEncoding: @@ -543,10 +543,10 @@ class UdopTokenizer(PreTrainedTokenizer): def call_boxes( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -569,16 +569,16 @@ class UdopTokenizer(PreTrainedTokenizer): sequences with word-level normalized bounding boxes and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - boxes (`List[List[int]]`, `List[List[List[int]]]`): + boxes (`list[list[int]]`, `list[list[list[int]]]`): Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale. - word_labels (`List[int]`, `List[List[int]]`, *optional*): + word_labels (`list[int]`, `list[list[int]]`, *optional*): Word-level integer labels (for token classification tasks such as FUNSD, CORD). """ @@ -606,18 +606,18 @@ class UdopTokenizer(PreTrainedTokenizer): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = words if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be words if not isinstance(text, (list, tuple)): raise ValueError( - "Words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -695,13 +695,13 @@ class UdopTokenizer(PreTrainedTokenizer): def batch_encode_plus_boxes( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -724,7 +724,7 @@ class UdopTokenizer(PreTrainedTokenizer): Tokenize and prepare for the model a list of sequences or a list of pairs of sequences. Args: - batch_text_or_text_pairs (`List[str]`, `List[Tuple[str, str]]`, `List[List[str]]`, `List[Tuple[List[str], List[str]]]`, and for not-fast tokenizers, also `List[List[int]]`, `List[Tuple[List[int], List[int]]]`): + batch_text_or_text_pairs (`list[str]`, `list[tuple[str, str]]`, `list[list[str]]`, `list[tuple[list[str], list[str]]]`, and for not-fast tokenizers, also `list[list[int]]`, `list[tuple[list[int], list[int]]]`): Batch of sequences or pair of sequences to be encoded. This can be a list of string/string-sequences/int-sequences or a list of pair of string/string-sequences/int-sequence (see details in `encode_plus`). @@ -768,8 +768,8 @@ class UdopTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput, EncodedInput], text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -777,16 +777,16 @@ class UdopTokenizer(PreTrainedTokenizer): stride: int = 0, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Args: Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary. Same as doing `self.convert_tokens_to_ids(self.tokenize(text))`. - text (`str`, `List[str]` or `List[int]`): + text (`str`, `list[str]` or `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -811,8 +811,8 @@ class UdopTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -841,11 +841,11 @@ class UdopTokenizer(PreTrainedTokenizer): Args: - text (`str`, `List[str]` or (for non-fast tokenizers) `List[int]`): + text (`str`, `list[str]` or (for non-fast tokenizers) `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -888,13 +888,13 @@ class UdopTokenizer(PreTrainedTokenizer): def _batch_encode_plus_boxes( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -947,8 +947,8 @@ class UdopTokenizer(PreTrainedTokenizer): self, batch_text_or_text_pairs, is_pair: Optional[bool] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1020,8 +1020,8 @@ class UdopTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -1075,8 +1075,8 @@ class UdopTokenizer(PreTrainedTokenizer): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -1105,9 +1105,9 @@ class UdopTokenizer(PreTrainedTokenizer): labeled with -100, such that they will be ignored by the loss function. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The first sequence to be encoded. This can be a string, a list of strings or a list of list of strings. - text_pair (`List[str]` or `List[int]`, *optional*): + text_pair (`list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a list of strings (words of a single example) or a list of list of strings (words of a batch of examples). """ @@ -1273,30 +1273,30 @@ class UdopTokenizer(PreTrainedTokenizer): # Copied from transformers.models.layoutxlm.tokenization_layoutxlm.LayoutXLMTokenizer.truncate_sequences def truncate_sequences( self, - ids: List[int], - token_boxes: List[List[int]], - pair_ids: Optional[List[int]] = None, - pair_token_boxes: Optional[List[List[int]]] = None, - labels: Optional[List[int]] = None, + ids: list[int], + token_boxes: list[list[int]], + pair_ids: Optional[list[int]] = None, + pair_token_boxes: Optional[list[list[int]]] = None, + labels: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Truncates a sequence pair in-place following the strategy. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - token_boxes (`List[List[int]]`): + token_boxes (`list[list[int]]`): Bounding boxes of the first sequence. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - pair_token_boxes (`List[List[int]]`, *optional*): + pair_token_boxes (`list[list[int]]`, *optional*): Bounding boxes of the second sequence. - labels (`List[int]`, *optional*): + labels (`list[int]`, *optional*): Labels of the first sequence (for token classification tasks). num_tokens_to_remove (`int`, *optional*, defaults to 0): Number of tokens to remove using the truncation strategy. @@ -1320,7 +1320,7 @@ class UdopTokenizer(PreTrainedTokenizer): sequence returned. The value of this argument defines the number of additional tokens. Returns: - `Tuple[List[int], List[int], List[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of + `tuple[list[int], list[int], list[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of overflowing tokens. """ if num_tokens_to_remove <= 0: @@ -1399,7 +1399,7 @@ class UdopTokenizer(PreTrainedTokenizer): # Copied from transformers.models.layoutxlm.tokenization_layoutxlm.LayoutXLMTokenizer._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -1411,7 +1411,7 @@ class UdopTokenizer(PreTrainedTokenizer): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. diff --git a/src/transformers/models/udop/tokenization_udop_fast.py b/src/transformers/models/udop/tokenization_udop_fast.py index 941bb8d6f73..a9f18c1b2b2 100644 --- a/src/transformers/models/udop/tokenization_udop_fast.py +++ b/src/transformers/models/udop/tokenization_udop_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from ...tokenization_utils_base import ( BatchEncoding, @@ -179,16 +179,16 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): token instead. pad_token (`str`, *optional*, defaults to `""`): The token used for padding, for example when batching sequences of different lengths. - sep_token_box (`List[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): + sep_token_box (`list[int]`, *optional*, defaults to `[1000, 1000, 1000, 1000]`): The bounding box to use for the special [SEP] token. - pad_token_box (`List[int]`, *optional*, defaults to `[0, 0, 0, 0]`): + pad_token_box (`list[int]`, *optional*, defaults to `[0, 0, 0, 0]`): The bounding box to use for the special [PAD] token. pad_token_label (`int`, *optional*, defaults to -100): The label to use for padding tokens. Defaults to -100, which is the `ignore_index` of PyTorch's CrossEntropyLoss. only_label_first_subword (`bool`, *optional*, defaults to `True`): Whether or not to only label the first subword, in case word labels are provided. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -237,13 +237,13 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): @add_end_docstrings(UDOP_ENCODE_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, **kwargs, ) -> BatchEncoding: @@ -272,10 +272,10 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): @add_end_docstrings(UDOP_ENCODE_KWARGS_DOCSTRING) def call_boxes( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None, - boxes: Optional[Union[List[List[int]], List[List[List[int]]]]] = None, - word_labels: Optional[Union[List[int], List[List[int]]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[PreTokenizedInput, list[PreTokenizedInput]]] = None, + boxes: Optional[Union[list[list[int]], list[list[list[int]]]]] = None, + word_labels: Optional[Union[list[int], list[list[int]]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -298,16 +298,16 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): sequences with word-level normalized bounding boxes and optional labels. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string, a list of strings (words of a single example or questions of a batch of examples) or a list of list of strings (batch of words). - text_pair (`List[str]`, `List[List[str]]`): + text_pair (`list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence should be a list of strings (pretokenized string). - boxes (`List[List[int]]`, `List[List[List[int]]]`): + boxes (`list[list[int]]`, `list[list[list[int]]]`): Word-level bounding boxes. Each bounding box should be normalized to be on a 0-1000 scale. - word_labels (`List[int]`, `List[List[int]]`, *optional*): + word_labels (`list[int]`, `list[list[int]]`, *optional*): Word-level integer labels (for token classification tasks such as FUNSD, CORD). """ @@ -335,18 +335,18 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): if text_pair is not None: # in case text + text_pair are provided, text = questions, text_pair = words if not _is_valid_text_input(text): - raise ValueError("text input must of type `str` (single example) or `List[str]` (batch of examples). ") + raise ValueError("text input must of type `str` (single example) or `list[str]` (batch of examples). ") if not isinstance(text_pair, (list, tuple)): raise ValueError( - "words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) else: # in case only text is provided => must be words if not isinstance(text, (list, tuple)): raise ValueError( - "Words must of type `List[str]` (single pretokenized example), " - "or `List[List[str]]` (batch of pretokenized examples)." + "Words must of type `list[str]` (single pretokenized example), " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None: @@ -422,7 +422,7 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): ) # Copied from transformers.models.layoutxlm.tokenization_layoutxlm_fast.LayoutXLMTokenizerFast.tokenize - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: batched_input = [(text, pair)] if pair else [text] self._tokenizer.encode_special_tokens = kwargs.pop( @@ -438,13 +438,13 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): def batch_encode_plus_boxes( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -473,7 +473,7 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): Args: - batch_text_or_text_pairs (`List[str]`, `List[Tuple[str, str]]`, `List[List[str]]`, `List[Tuple[List[str], List[str]]]`, and for not-fast tokenizers, also `List[List[int]]`, `List[Tuple[List[int], List[int]]]`): + batch_text_or_text_pairs (`list[str]`, `list[tuple[str, str]]`, `list[list[str]]`, `list[tuple[list[str], list[str]]]`, and for not-fast tokenizers, also `list[list[int]]`, `list[tuple[list[int], list[int]]]`): Batch of sequences or pair of sequences to be encoded. This can be a list of string/string-sequences/int-sequences or a list of pair of string/string-sequences/int-sequence (see details in `encode_plus`). @@ -516,13 +516,13 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): def _batch_encode_plus_boxes( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], ], is_pair: Optional[bool] = None, - boxes: Optional[List[List[List[int]]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[list[int]]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -563,9 +563,9 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): ) # Convert encoding to dict - # `Tokens` has type: Tuple[ - # List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]], - # List[EncodingFast] + # `Tokens` has type: tuple[ + # list[dict[str, list[list[int]]]] or list[dict[str, 2D-Tensor]], + # list[EncodingFast] # ] # with nested dimensions corresponding to batch, overflows, sequence length tokens_and_encodings = [ @@ -679,8 +679,8 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[int]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[int]] = None, add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy = TruncationStrategy.DO_NOT_TRUNCATE, @@ -747,8 +747,8 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput, EncodedInput], text_pair: Optional[Union[TextInput, PreTokenizedInput, EncodedInput]] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -756,16 +756,16 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): stride: int = 0, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Args: Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary. Same as doing `self.convert_tokens_to_ids(self.tokenize(text))`. - text (`str`, `List[str]` or `List[int]`): + text (`str`, `list[str]` or `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -790,8 +790,8 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): self, text: Union[TextInput, PreTokenizedInput], text_pair: Optional[PreTokenizedInput] = None, - boxes: Optional[List[List[int]]] = None, - word_labels: Optional[List[List[int]]] = None, + boxes: Optional[list[list[int]]] = None, + word_labels: Optional[list[list[int]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, @@ -820,11 +820,11 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): Args: - text (`str`, `List[str]` or (for non-fast tokenizers) `List[int]`): + text (`str`, `list[str]` or (for non-fast tokenizers) `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -867,7 +867,7 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.layoutxlm.tokenization_layoutxlm_fast.LayoutXLMTokenizerFast._pad def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -879,7 +879,7 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. @@ -955,8 +955,8 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): return encoded_inputs def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -965,13 +965,13 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -980,20 +980,20 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): return token_ids_0 + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -1004,7 +1004,7 @@ class UdopTokenizerFast(PreTrainedTokenizerFast): return len(token_ids_0 + sep + token_ids_1 + sep) * [0] # Copied from transformers.models.layoutxlm.tokenization_layoutxlm_fast.LayoutXLMTokenizerFast.save_vocabulary - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/umt5/modeling_umt5.py b/src/transformers/models/umt5/modeling_umt5.py index f8bc1642c35..d5bb6718baf 100644 --- a/src/transformers/models/umt5/modeling_umt5.py +++ b/src/transformers/models/umt5/modeling_umt5.py @@ -16,7 +16,7 @@ import copy import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -260,7 +260,7 @@ class UMT5Attention(nn.Module): self, hidden_states: torch.Tensor, encoder_hidden_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cache_position: Optional[torch.Tensor] = None, @@ -1047,8 +1047,8 @@ class UMT5Model(UMT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.Tensor] = None, decoder_inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -1056,7 +1056,7 @@ class UMT5Model(UMT5PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so @@ -1263,8 +1263,8 @@ class UMT5ForConditionalGeneration(UMT5PreTrainedModel, GenerationMixin): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, - past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.Tensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1273,7 +1273,7 @@ class UMT5ForConditionalGeneration(UMT5PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so @@ -1499,7 +1499,7 @@ class UMT5EncoderModel(UMT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutput]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so you @@ -1569,7 +1569,7 @@ class UMT5ForSequenceClassification(UMT5PreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[List[torch.FloatTensor]] = None, + encoder_outputs: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, @@ -1577,7 +1577,7 @@ class UMT5ForSequenceClassification(UMT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Seq2SeqSequenceClassifierOutput]: + ) -> Union[tuple, Seq2SeqSequenceClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so @@ -1736,7 +1736,7 @@ class UMT5ForTokenClassification(UMT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so you @@ -1846,7 +1846,7 @@ class UMT5ForQuestionAnswering(UMT5PreTrainedModel): head_mask: Optional[torch.FloatTensor] = None, decoder_head_mask: Optional[torch.FloatTensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.Tensor]]] = None, start_positions: Optional[torch.LongTensor] = None, end_positions: Optional[torch.LongTensor] = None, inputs_embeds: Optional[torch.FloatTensor] = None, @@ -1855,7 +1855,7 @@ class UMT5ForQuestionAnswering(UMT5PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqQuestionAnsweringModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`): Indices of input sequence tokens in the vocabulary. UMT5 is a model with relative position embeddings so diff --git a/src/transformers/models/unispeech/configuration_unispeech.py b/src/transformers/models/unispeech/configuration_unispeech.py index f304e566e81..a71ba7f1b7d 100644 --- a/src/transformers/models/unispeech/configuration_unispeech.py +++ b/src/transformers/models/unispeech/configuration_unispeech.py @@ -78,13 +78,13 @@ class UniSpeechConfig(PretrainedConfig): feat_extract_activation (`str, *optional*, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 2, 2)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 2, 2)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. diff --git a/src/transformers/models/unispeech/modeling_unispeech.py b/src/transformers/models/unispeech/modeling_unispeech.py index 4f5fa3805ae..02fa44891f5 100755 --- a/src/transformers/models/unispeech/modeling_unispeech.py +++ b/src/transformers/models/unispeech/modeling_unispeech.py @@ -22,7 +22,7 @@ import math import warnings from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -86,8 +86,8 @@ class UniSpeechForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class UniSpeechSamePadLayer(nn.Module): @@ -348,14 +348,14 @@ class UniSpeechAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -914,7 +914,7 @@ class UniSpeechPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1109,7 +1109,7 @@ class UniSpeechModel(UniSpeechPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechBaseModelOutput]: + ) -> Union[tuple, UniSpeechBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1228,7 +1228,7 @@ class UniSpeechForPreTraining(UniSpeechPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechForPreTrainingOutput]: + ) -> Union[tuple, UniSpeechForPreTrainingOutput]: r""" Example: @@ -1387,7 +1387,7 @@ class UniSpeechForCTC(UniSpeechPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1510,11 +1510,11 @@ class UniSpeechForSequenceClassification(UniSpeechPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`UniSpeechProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/unispeech/modular_unispeech.py b/src/transformers/models/unispeech/modular_unispeech.py index 884e031d26a..e39d5bdd658 100644 --- a/src/transformers/models/unispeech/modular_unispeech.py +++ b/src/transformers/models/unispeech/modular_unispeech.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -74,8 +74,8 @@ class UniSpeechForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class UniSpeechPositionalConvEmbedding(Wav2Vec2PositionalConvEmbedding): @@ -252,7 +252,7 @@ class UniSpeechModel(UniSpeechPreTrainedModel, Wav2Vec2Model): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechBaseModelOutput]: + ) -> Union[tuple, UniSpeechBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -371,7 +371,7 @@ class UniSpeechForPreTraining(UniSpeechPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechForPreTrainingOutput]: + ) -> Union[tuple, UniSpeechForPreTrainingOutput]: r""" Example: diff --git a/src/transformers/models/unispeech_sat/configuration_unispeech_sat.py b/src/transformers/models/unispeech_sat/configuration_unispeech_sat.py index 24996c825ea..2bebff802e8 100644 --- a/src/transformers/models/unispeech_sat/configuration_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/configuration_unispeech_sat.py @@ -79,13 +79,13 @@ class UniSpeechSatConfig(PretrainedConfig): feat_extract_activation (`str, *optional*, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 2, 2)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 2, 2)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -155,13 +155,13 @@ class UniSpeechSatConfig(PretrainedConfig): instance of [`UniSpeechSatForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 256): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py index 1a84b2e6d2e..72375f8e904 100755 --- a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py @@ -22,7 +22,7 @@ import math import warnings from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -89,8 +89,8 @@ class UniSpeechSatForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class UniSpeechSatSamePadLayer(nn.Module): @@ -351,14 +351,14 @@ class UniSpeechSatAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -917,7 +917,7 @@ class UniSpeechSatPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1111,7 +1111,7 @@ class UniSpeechSatModel(UniSpeechSatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechSatBaseModelOutput]: + ) -> Union[tuple, UniSpeechSatBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1237,7 +1237,7 @@ class UniSpeechSatForPreTraining(UniSpeechSatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechSatForPreTrainingOutput]: + ) -> Union[tuple, UniSpeechSatForPreTrainingOutput]: r""" Example: @@ -1380,7 +1380,7 @@ class UniSpeechSatForCTC(UniSpeechSatPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1503,11 +1503,11 @@ class UniSpeechSatForSequenceClassification(UniSpeechSatPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`UniSpeechSatProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1617,11 +1617,11 @@ class UniSpeechSatForAudioFrameClassification(UniSpeechSatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`UniSpeechSatProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1799,11 +1799,11 @@ class UniSpeechSatForXVector(UniSpeechSatPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`UniSpeechSatProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/unispeech_sat/modular_unispeech_sat.py b/src/transformers/models/unispeech_sat/modular_unispeech_sat.py index 57e8da9e4d5..91565ba4313 100644 --- a/src/transformers/models/unispeech_sat/modular_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/modular_unispeech_sat.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -80,8 +80,8 @@ class UniSpeechSatForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class UniSpeechSatPositionalConvEmbedding(Wav2Vec2PositionalConvEmbedding): @@ -261,7 +261,7 @@ class UniSpeechSatModel(UniSpeechSatPreTrainedModel, Wav2Vec2Model): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechSatBaseModelOutput]: + ) -> Union[tuple, UniSpeechSatBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -387,7 +387,7 @@ class UniSpeechSatForPreTraining(UniSpeechSatPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, UniSpeechSatForPreTrainingOutput]: + ) -> Union[tuple, UniSpeechSatForPreTrainingOutput]: r""" Example: diff --git a/src/transformers/models/univnet/configuration_univnet.py b/src/transformers/models/univnet/configuration_univnet.py index 0a3811ee3a2..4c1bad6da25 100644 --- a/src/transformers/models/univnet/configuration_univnet.py +++ b/src/transformers/models/univnet/configuration_univnet.py @@ -40,15 +40,15 @@ class UnivNetConfig(PretrainedConfig): num_mel_bins (`int`, *optional*, defaults to 100): The number of frequency bins in the conditioning log-mel spectrogram. This should correspond to the value used in the [`UnivNetFeatureExtractor`] class. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 3, 3]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 3, 3]`): A tuple of integers defining the kernel sizes of the 1D convolutional layers in the UnivNet residual network. The length of `resblock_kernel_sizes` defines the number of resnet blocks and should match that of `resblock_stride_sizes` and `resblock_dilation_sizes`. - resblock_stride_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[8, 8, 4]`): + resblock_stride_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 4]`): A tuple of integers defining the stride sizes of the 1D convolutional layers in the UnivNet residual network. The length of `resblock_stride_sizes` should match that of `resblock_kernel_sizes` and `resblock_dilation_sizes`. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 9, 27], [1, 3, 9, 27], [1, 3, 9, 27]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 9, 27], [1, 3, 9, 27], [1, 3, 9, 27]]`): A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the UnivNet residual network. The length of `resblock_dilation_sizes` should match that of `resblock_kernel_sizes` and `resblock_stride_sizes`. The length of each nested list in diff --git a/src/transformers/models/univnet/feature_extraction_univnet.py b/src/transformers/models/univnet/feature_extraction_univnet.py index 5c657636239..059226afe1d 100644 --- a/src/transformers/models/univnet/feature_extraction_univnet.py +++ b/src/transformers/models/univnet/feature_extraction_univnet.py @@ -13,7 +13,7 @@ # limitations under the License. """Feature extractor class for UnivNetModel.""" -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import numpy as np @@ -260,7 +260,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): return noise - def batch_decode(self, waveforms, waveform_lengths=None) -> List[np.ndarray]: + def batch_decode(self, waveforms, waveform_lengths=None) -> list[np.ndarray]: r""" Removes padding from generated audio after running [`UnivNetModel.forward`]. This returns a ragged list of 1D audio waveform arrays and not a single tensor/array because in general the waveforms will have different @@ -273,7 +273,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): The batched lengths of each waveform before padding. Returns: - `List[np.ndarray]`: A ragged list of 1D waveform arrays with padding removed. + `list[np.ndarray]`: A ragged list of 1D waveform arrays with padding removed. """ # Collapse the batched waveform tensor to a list of 1D audio waveforms waveforms = [waveform.detach().to(device="cpu", copy=True).numpy() for waveform in waveforms] @@ -285,7 +285,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], sampling_rate: Optional[int] = None, padding: Union[bool, str, PaddingStrategy] = True, max_length: Optional[int] = None, @@ -303,7 +303,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -417,7 +417,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): mel_spectrograms = [self.mel_spectrogram(waveform) for waveform in input_features] - if isinstance(input_features[0], List): + if isinstance(input_features[0], list): batched_speech["input_features"] = [np.asarray(mel, dtype=np.float32) for mel in mel_spectrograms] else: batched_speech["input_features"] = [mel.astype(np.float32) for mel in mel_spectrograms] @@ -444,7 +444,7 @@ class UnivNetFeatureExtractor(SequenceFeatureExtractor): return batched_speech - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: output = super().to_dict() # Don't serialize these as they are derived from the other properties. diff --git a/src/transformers/models/univnet/modeling_univnet.py b/src/transformers/models/univnet/modeling_univnet.py index 712b531e5e3..5f82aaa34d5 100644 --- a/src/transformers/models/univnet/modeling_univnet.py +++ b/src/transformers/models/univnet/modeling_univnet.py @@ -14,7 +14,7 @@ """PyTorch UnivNetModel model.""" from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -161,7 +161,7 @@ class UnivNetKernelPredictor(nn.Module): Tensor containing the log-mel spectrograms. Returns: - Tuple[`torch.FloatTensor, `torch.FloatTensor`]: tuple of tensors where the first element is the tensor of + tuple[`torch.FloatTensor, `torch.FloatTensor`]: tuple of tensors where the first element is the tensor of location variable convolution kernels of shape `(batch_size, self.conv_layers, self.conv_in_channels, self.conv_out_channels, self.conv_kernel_size, seq_length)` and the second element is the tensor of location variable convolution biases of shape `(batch_size, self.conv_layers. self.conv_out_channels, @@ -474,7 +474,7 @@ class UnivNetModel(PreTrainedModel): padding_mask: Optional[torch.FloatTensor] = None, generator: Optional[torch.Generator] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], UnivNetModelOutput]: + ) -> Union[tuple[torch.FloatTensor], UnivNetModelOutput]: r""" input_features (`torch.FloatTensor`): Tensor containing the log-mel spectrograms. Can be batched and of shape `(batch_size, sequence_length, diff --git a/src/transformers/models/upernet/configuration_upernet.py b/src/transformers/models/upernet/configuration_upernet.py index a6c60f95cd5..17af62c4b25 100644 --- a/src/transformers/models/upernet/configuration_upernet.py +++ b/src/transformers/models/upernet/configuration_upernet.py @@ -52,7 +52,7 @@ class UperNetConfig(PretrainedConfig): The number of hidden units in the convolutional layers. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - pool_scales (`Tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): + pool_scales (`tuple[int]`, *optional*, defaults to `[1, 2, 3, 6]`): Pooling scales used in Pooling Pyramid Module applied on the last feature map. use_auxiliary_head (`bool`, *optional*, defaults to `True`): Whether to use an auxiliary head during training. diff --git a/src/transformers/models/upernet/modeling_upernet.py b/src/transformers/models/upernet/modeling_upernet.py index d808b7ab3b9..a531fa2abe3 100644 --- a/src/transformers/models/upernet/modeling_upernet.py +++ b/src/transformers/models/upernet/modeling_upernet.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch UperNet model. Based on OpenMMLab's implementation, found in https://github.com/open-mmlab/mmsegmentation.""" -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -37,10 +37,10 @@ class UperNetConvModule(nn.Module): self, in_channels: int, out_channels: int, - kernel_size: Union[int, Tuple[int, int]], - padding: Union[int, Tuple[int, int], str] = 0, + kernel_size: Union[int, tuple[int, int]], + padding: Union[int, tuple[int, int], str] = 0, bias: bool = False, - dilation: Union[int, Tuple[int, int]] = 1, + dilation: Union[int, tuple[int, int]] = 1, ) -> None: super().__init__() self.conv = nn.Conv2d( @@ -84,7 +84,7 @@ class UperNetPyramidPoolingModule(nn.Module): Pyramid Pooling Module (PPM) used in PSPNet. Args: - pool_scales (`Tuple[int]`): + pool_scales (`tuple[int]`): Pooling scales used in Pooling Pyramid Module. in_channels (`int`): Input channels. @@ -94,7 +94,7 @@ class UperNetPyramidPoolingModule(nn.Module): align_corners argument of F.interpolate. """ - def __init__(self, pool_scales: Tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: + def __init__(self, pool_scales: tuple[int, ...], in_channels: int, channels: int, align_corners: bool) -> None: super().__init__() self.pool_scales = pool_scales self.align_corners = align_corners @@ -106,7 +106,7 @@ class UperNetPyramidPoolingModule(nn.Module): self.blocks.append(block) self.add_module(str(i), block) - def forward(self, x: torch.Tensor) -> List[torch.Tensor]: + def forward(self, x: torch.Tensor) -> list[torch.Tensor]: ppm_outs = [] for ppm in self.blocks: ppm_out = ppm(x) @@ -218,7 +218,7 @@ class UperNetFCNHead(nn.Module): """ def __init__( - self, config, in_channels, in_index: int = 2, kernel_size: int = 3, dilation: Union[int, Tuple[int, int]] = 1 + self, config, in_channels, in_index: int = 2, kernel_size: int = 3, dilation: Union[int, tuple[int, int]] = 1 ) -> None: super().__init__() diff --git a/src/transformers/models/video_llava/configuration_video_llava.py b/src/transformers/models/video_llava/configuration_video_llava.py index 402d9946731..561d2e3a925 100644 --- a/src/transformers/models/video_llava/configuration_video_llava.py +++ b/src/transformers/models/video_llava/configuration_video_llava.py @@ -47,7 +47,7 @@ class VideoLlavaConfig(PretrainedConfig): vision_feature_select_strategy (`str`, *optional*, defaults to `"default"`): The feature selection strategy used to select the vision feature from the CLIP backbone. Can be either "full" to select all features or "default" to select features without `CLS`. - vision_feature_layer (`Union[int, List[int]]`, *optional*, defaults to -2): + vision_feature_layer (`Union[int, list[int]]`, *optional*, defaults to -2): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. diff --git a/src/transformers/models/video_llava/image_processing_video_llava.py b/src/transformers/models/video_llava/image_processing_video_llava.py index 94f60de0915..fa3aeeeb2b3 100644 --- a/src/transformers/models/video_llava/image_processing_video_llava.py +++ b/src/transformers/models/video_llava/image_processing_video_llava.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Video-LLaVA.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -53,7 +53,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by `do_resize` in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` method. @@ -62,7 +62,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the `preprocess` method. - crop_size (`Dict[str, int]` *optional*, defaults to 224): + crop_size (`dict[str, int]` *optional*, defaults to 224): Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -73,10 +73,10 @@ class VideoLlavaImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): + image_mean (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): + image_std (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -89,15 +89,15 @@ class VideoLlavaImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BICUBIC, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: bool = True, **kwargs, ) -> None: @@ -122,7 +122,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -135,7 +135,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`): Resampling filter to use when resiizing the image. @@ -171,18 +171,18 @@ class VideoLlavaImageProcessor(BaseImageProcessor): @filter_out_non_signature_kwargs() def preprocess( self, - images: Optional[List[ImageInput]] = None, - videos: Optional[List[VideoInput]] = None, + images: Optional[list[ImageInput]] = None, + videos: Optional[list[VideoInput]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, @@ -200,7 +200,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): passing in videos with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with the longest edge resized to keep the input aspect ratio. resample (`int`, *optional*, defaults to `self.resample`): @@ -208,7 +208,7 @@ class VideoLlavaImageProcessor(BaseImageProcessor): has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_center_crop`): Whether to center crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image. @@ -216,9 +216,9 @@ class VideoLlavaImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to `True`. do_convert_rgb (`bool`, *optional*, defaults to `self.do_convert_rgb`): @@ -327,13 +327,13 @@ class VideoLlavaImageProcessor(BaseImageProcessor): self, image: ImageInput = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_center_crop: Optional[bool] = None, crop_size: Optional[int] = None, do_convert_rgb: Optional[bool] = None, diff --git a/src/transformers/models/video_llava/modeling_video_llava.py b/src/transformers/models/video_llava/modeling_video_llava.py index 8994bf805ed..b945466685f 100644 --- a/src/transformers/models/video_llava/modeling_video_llava.py +++ b/src/transformers/models/video_llava/modeling_video_llava.py @@ -15,7 +15,7 @@ """PyTorch VideoLlava model.""" from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -69,9 +69,9 @@ class VideoLlavaModelOutputWithPast(ModelOutput): """ last_hidden_state: torch.FloatTensor = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None video_hidden_states: Optional[torch.FloatTensor] = None @@ -113,9 +113,9 @@ class VideoLlavaCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None video_hidden_states: Optional[torch.FloatTensor] = None @@ -205,7 +205,7 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): def get_image_features( self, pixel_values_images: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): """ @@ -214,7 +214,7 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): Args: pixel_values_images (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -258,7 +258,7 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): def get_video_features( self, pixel_values_videos: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, ): """ Obtains video last hidden states from the vision tower and apply multimodal projection. @@ -266,7 +266,7 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): Args: pixel_values_videos (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`) The tensors corresponding to the input videos. - vision_feature_layer (`Union[int, List[int]]`, *optional*): + vision_feature_layer (`Union[int, list[int]]`, *optional*): The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features. @@ -304,9 +304,9 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): pixel_values_videos: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -314,7 +314,7 @@ class VideoLlavaModel(VideoLlavaPreTrainedModel): return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Union[Tuple, VideoLlavaModelOutputWithPast]: + ) -> Union[tuple, VideoLlavaModelOutputWithPast]: r""" pixel_values_images (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)): The tensors corresponding to the input images. Pixel values can be obtained using @@ -452,7 +452,7 @@ class VideoLlavaForConditionalGeneration(VideoLlavaPreTrainedModel, GenerationMi def get_image_features( self, pixel_values_images: torch.FloatTensor, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, ): return self.model.get_image_features( @@ -487,9 +487,9 @@ class VideoLlavaForConditionalGeneration(VideoLlavaPreTrainedModel, GenerationMi pixel_values_videos: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layer: Optional[Union[int, List[int]]] = None, + vision_feature_layer: Optional[Union[int, list[int]]] = None, vision_feature_select_strategy: Optional[str] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -499,7 +499,7 @@ class VideoLlavaForConditionalGeneration(VideoLlavaPreTrainedModel, GenerationMi cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **kwargs: Unpack[KwargsForCausalLM], - ) -> Union[Tuple, VideoLlavaCausalLMOutputWithPast]: + ) -> Union[tuple, VideoLlavaCausalLMOutputWithPast]: r""" pixel_values_images (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)): The tensors corresponding to the input images. Pixel values can be obtained using @@ -530,7 +530,7 @@ class VideoLlavaForConditionalGeneration(VideoLlavaPreTrainedModel, GenerationMi ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' diff --git a/src/transformers/models/video_llava/processing_video_llava.py b/src/transformers/models/video_llava/processing_video_llava.py index 7a6edb8cff2..d0b061892d6 100644 --- a/src/transformers/models/video_llava/processing_video_llava.py +++ b/src/transformers/models/video_llava/processing_video_llava.py @@ -16,7 +16,7 @@ Processor class for VideoLlava. """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -89,7 +89,7 @@ class VideoLlavaProcessor(ProcessorMixin): def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, images: ImageInput = None, videos: ImageInput = None, padding: Union[bool, str, PaddingStrategy] = False, @@ -105,15 +105,15 @@ class VideoLlavaProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`TextInput`, `PreTokenizedInput`, `List[TextInput]`, `List[PreTokenizedInput]`): + text (`TextInput`, `PreTokenizedInput`, `list[TextInput]`, `list[PreTokenizedInput]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image should be of shape (C, H, W), where C is a number of channels, H and W are image height and width. - videos (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + videos (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): Video frames to preprocess. Expects a single or batch of video frames in NumPy array or PyTorch tensor. Each video should be of shape (T, C, H, W), where T is number of frames, C is number of channels, H and W are image height and width. diff --git a/src/transformers/models/videomae/image_processing_videomae.py b/src/transformers/models/videomae/image_processing_videomae.py index fa0459fabbd..48950683191 100644 --- a/src/transformers/models/videomae/image_processing_videomae.py +++ b/src/transformers/models/videomae/image_processing_videomae.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for VideoMAE.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -48,7 +48,7 @@ if is_vision_available(): logger = logging.get_logger(__name__) -def make_batched(videos) -> List[List[ImageInput]]: +def make_batched(videos) -> list[list[ImageInput]]: if isinstance(videos, (list, tuple)) and isinstance(videos[0], (list, tuple)) and is_valid_image(videos[0][0]): return videos @@ -70,7 +70,7 @@ class VideoMAEImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 224}`): Size of the output image after resizing. The shortest edge of the image will be resized to `size["shortest_edge"]` while maintaining the aspect ratio of the original image. Can be overridden by `size` in the `preprocess` method. @@ -80,7 +80,7 @@ class VideoMAEImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after applying the center crop. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -92,10 +92,10 @@ class VideoMAEImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -105,15 +105,15 @@ class VideoMAEImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -136,7 +136,7 @@ class VideoMAEImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -148,7 +148,7 @@ class VideoMAEImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. If `size` is of the form `{"height": h, "width": w}`, the output image will have the size `(h, w)`. If `size` is of the form `{"shortest_edge": s}`, the output image will have its shortest edge of length `s` while keeping the aspect ratio of the original image. @@ -181,15 +181,15 @@ class VideoMAEImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -239,15 +239,15 @@ class VideoMAEImageProcessor(BaseImageProcessor): self, videos: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -261,14 +261,14 @@ class VideoMAEImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after applying resize. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_centre_crop`): Whether to centre crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after applying the centre crop. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between [0 - 1]. @@ -276,9 +276,9 @@ class VideoMAEImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/videomae/modeling_videomae.py b/src/transformers/models/videomae/modeling_videomae.py index e6a114a522b..c418a3b49c5 100755 --- a/src/transformers/models/videomae/modeling_videomae.py +++ b/src/transformers/models/videomae/modeling_videomae.py @@ -17,7 +17,7 @@ import collections.abc from copy import deepcopy from dataclasses import dataclass -from typing import Callable, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -60,8 +60,8 @@ class VideoMAEDecoderOutput(ModelOutput): """ logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -86,8 +86,8 @@ class VideoMAEForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None # sin-cos position encoding @@ -257,7 +257,7 @@ class VideoMAESelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: k_bias = torch.zeros_like(self.v_bias, requires_grad=False) if self.q_bias is not None else None keys = nn.functional.linear(input=hidden_states, weight=self.key.weight, bias=k_bias) values = nn.functional.linear(input=hidden_states, weight=self.value.weight, bias=self.v_bias) @@ -323,7 +323,7 @@ class VideoMAEAttention(nn.Module): self.output = VideoMAESelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -346,7 +346,7 @@ class VideoMAEAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -407,7 +407,7 @@ class VideoMAELayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in VideoMAE, layernorm is applied before self-attention head_mask, @@ -544,7 +544,7 @@ class VideoMAEModel(VideoMAEPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). Each video in the @@ -568,7 +568,7 @@ class VideoMAEModel(VideoMAEPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -592,7 +592,7 @@ class VideoMAEModel(VideoMAEPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) @@ -939,7 +939,7 @@ class VideoMAEForVideoClassification(VideoMAEPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutput]: + ) -> Union[tuple, ImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., @@ -964,7 +964,7 @@ class VideoMAEForVideoClassification(VideoMAEPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -988,7 +988,7 @@ class VideoMAEForVideoClassification(VideoMAEPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/vilt/image_processing_vilt.py b/src/transformers/models/vilt/image_processing_vilt.py index 3c557f311f2..6c9a727f0f9 100644 --- a/src/transformers/models/vilt/image_processing_vilt.py +++ b/src/transformers/models/vilt/image_processing_vilt.py @@ -15,7 +15,7 @@ """Image processor class for Vilt.""" from collections.abc import Iterable -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -46,7 +46,7 @@ if is_vision_available(): logger = logging.get_logger(__name__) -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -54,7 +54,7 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -62,7 +62,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -72,8 +72,8 @@ def make_pixel_mask( def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -95,7 +95,7 @@ def get_resize_output_image_size( longer: int = 1333, size_divisor: int = 32, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: input_height, input_width = get_image_size(input_image, input_data_format) min_size, max_size = shorter, longer @@ -129,7 +129,7 @@ class ViltImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 384}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 384}`): Resize the shorter side of the input to `size["shortest_edge"]`. The longer side will be limited to under `int((1333 / 800) * size["shortest_edge"])` while preserving the aspect ratio. Only has an effect if `do_resize` is set to `True`. Can be overridden by the `size` parameter in the `preprocess` method. @@ -148,11 +148,11 @@ class ViltImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -166,14 +166,14 @@ class ViltImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BICUBIC, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, **kwargs, ) -> None: @@ -196,7 +196,7 @@ class ViltImageProcessor(BaseImageProcessor): self.do_pad = do_pad @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure `pad_and_return_pixel_mask` is updated if image processor is created using from_dict and kwargs e.g. `ViltImageProcessor.from_pretrained(checkpoint, @@ -210,7 +210,7 @@ class ViltImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], size_divisor: int = 32, resample: PILImageResampling = PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -227,7 +227,7 @@ class ViltImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Controls the size of the output image. Should be of the form `{"shortest_edge": int}`. size_divisor (`int`, *optional*, defaults to 32): The image is resized to a size that is a multiple of this value. @@ -258,7 +258,7 @@ class ViltImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], + output_size: tuple[int, int], constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -284,7 +284,7 @@ class ViltImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], + images: list[np.ndarray], constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = True, return_tensors: Optional[Union[str, TensorType]] = None, @@ -342,14 +342,14 @@ class ViltImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, size_divisor: Optional[int] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, @@ -364,7 +364,7 @@ class ViltImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Controls the size of the image after `resize`. The shortest edge of the image is resized to `size["shortest_edge"]` whilst preserving the aspect ratio. If the longest edge of this resized image is > `int(size["shortest_edge"] * (1333 / 800))`, then the image is resized again to make the longest @@ -379,9 +379,9 @@ class ViltImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to normalize the image by if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to normalize the image by if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): Whether to pad the image to the (max_height, max_width) in the batch. If `True`, a pixel mask is also diff --git a/src/transformers/models/vilt/image_processing_vilt_fast.py b/src/transformers/models/vilt/image_processing_vilt_fast.py index 764e1203bf6..d55142f83a9 100644 --- a/src/transformers/models/vilt/image_processing_vilt_fast.py +++ b/src/transformers/models/vilt/image_processing_vilt_fast.py @@ -14,7 +14,7 @@ # limitations under the License. """Fast Image processor class for Vilt.""" -from typing import List, Optional, Union +from typing import Optional, Union from ...image_processing_utils import BatchFeature from ...image_processing_utils_fast import ( @@ -91,8 +91,8 @@ class ViltImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]], **kwargs, ) -> BatchFeature: @@ -149,7 +149,7 @@ class ViltImageProcessorFast(BaseImageProcessorFast): Args: images (`torch.Tensor`): Image or batch of images to resize. - size (`Dict[str, int]`): Size dictionary with shortest_edge key. + size (`dict[str, int]`): Size dictionary with shortest_edge key. interpolation (`F.InterpolationMode`, *optional*): Interpolation method to use. size_divisor (`int`, *optional*): Value to ensure height/width are divisible by. diff --git a/src/transformers/models/vilt/modeling_vilt.py b/src/transformers/models/vilt/modeling_vilt.py index d690ce1e45c..6ce00d9f397 100755 --- a/src/transformers/models/vilt/modeling_vilt.py +++ b/src/transformers/models/vilt/modeling_vilt.py @@ -17,7 +17,7 @@ import collections.abc import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -52,11 +52,11 @@ class ViltForImagesAndTextClassificationOutput(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - hidden_states (`List[tuple(torch.FloatTensor)]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): + hidden_states (`list[tuple(torch.FloatTensor)]`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`): List of tuples of `torch.FloatTensor` (one for each image-text pair, each tuple containing the output of the embeddings + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states of the model at the output of each layer plus the initial embedding outputs. - attentions (`List[tuple(torch.FloatTensor)]`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`): + attentions (`list[tuple(torch.FloatTensor)]`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`): List of tuples of `torch.FloatTensor` (one for each image-text pair, each tuple containing the attention weights of shape `(batch_size, num_heads, sequence_length, sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. @@ -64,8 +64,8 @@ class ViltForImagesAndTextClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[List[Tuple[torch.FloatTensor]]] = None - attentions: Optional[List[Tuple[torch.FloatTensor]]] = None + hidden_states: Optional[list[tuple[torch.FloatTensor]]] = None + attentions: Optional[list[tuple[torch.FloatTensor]]] = None class ViltEmbeddings(nn.Module): @@ -619,7 +619,7 @@ class ViltModel(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[BaseModelOutputWithPooling, Tuple[torch.FloatTensor]]: + ) -> Union[BaseModelOutputWithPooling, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -778,7 +778,7 @@ class ViltForMaskedLM(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[MaskedLMOutput, Tuple[torch.FloatTensor]]: + ) -> Union[MaskedLMOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -956,7 +956,7 @@ class ViltForQuestionAnswering(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1062,7 +1062,7 @@ class ViltForImageAndTextRetrieval(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[SequenceClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[SequenceClassifierOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1167,7 +1167,7 @@ class ViltForImagesAndTextClassification(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[ViltForImagesAndTextClassificationOutput, Tuple[torch.FloatTensor]]: + ) -> Union[ViltForImagesAndTextClassificationOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. @@ -1297,7 +1297,7 @@ class ViltForTokenClassification(ViltPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[TokenClassifierOutput, Tuple[torch.FloatTensor]]: + ) -> Union[TokenClassifierOutput, tuple[torch.FloatTensor]]: r""" image_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`, *optional*): Optionally, instead of passing `pixel_values`, you can choose to directly pass an embedded representation. diff --git a/src/transformers/models/vilt/processing_vilt.py b/src/transformers/models/vilt/processing_vilt.py index 2c46fd82d12..67761fae943 100644 --- a/src/transformers/models/vilt/processing_vilt.py +++ b/src/transformers/models/vilt/processing_vilt.py @@ -17,7 +17,7 @@ Processor class for ViLT. """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessorMixin from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy @@ -64,7 +64,7 @@ class ViltProcessor(ProcessorMixin): def __call__( self, images, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy] = None, diff --git a/src/transformers/models/vipllava/configuration_vipllava.py b/src/transformers/models/vipllava/configuration_vipllava.py index f748201f7d7..9ad117c7546 100644 --- a/src/transformers/models/vipllava/configuration_vipllava.py +++ b/src/transformers/models/vipllava/configuration_vipllava.py @@ -43,7 +43,7 @@ class VipLlavaConfig(PretrainedConfig): The activation function used by the multimodal projector. projector_layernorm_eps (`float`, *optional*, defaults to 1e-05): The layer norm epsilon of the projector layernorm - vision_feature_layers (`Union[int, List[int]]`, *optional*, defaults to `[-2, -5, -8, -11, 6]`): + vision_feature_layers (`Union[int, list[int]]`, *optional*, defaults to `[-2, -5, -8, -11, 6]`): The vision feature layer, or list of layers to select the vision features from. image_seq_length (`int`, *optional*, defaults to 576): Sequence length of one image embedding. diff --git a/src/transformers/models/vipllava/modeling_vipllava.py b/src/transformers/models/vipllava/modeling_vipllava.py index e451f3d9af8..55b90db70cb 100644 --- a/src/transformers/models/vipllava/modeling_vipllava.py +++ b/src/transformers/models/vipllava/modeling_vipllava.py @@ -20,7 +20,7 @@ # limitations under the License. from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -101,9 +101,9 @@ class VipLlavaCausalLMOutputWithPast(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - past_key_values: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + past_key_values: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None image_hidden_states: Optional[torch.FloatTensor] = None @@ -183,7 +183,7 @@ class VipLlavaModel(VipLlavaPreTrainedModel): self.language_model.set_input_embeddings(value) def get_image_features( - self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, List[int]]] = None + self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, list[int]]] = None ): """ Obtains image last hidden states from the vision tower and apply multimodal projection. @@ -191,7 +191,7 @@ class VipLlavaModel(VipLlavaPreTrainedModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layers (`Union[int, List[int]]`): + vision_feature_layers (`Union[int, list[int]]`): The vision feature layer, or the list of indexes of the layers to select the vision feature. Returns: @@ -220,18 +220,18 @@ class VipLlavaModel(VipLlavaPreTrainedModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layers: Optional[Union[int, List[int]]] = None, + vision_feature_layers: Optional[Union[int, list[int]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **lm_kwargs, - ) -> Union[Tuple, VipLlavaModelOutputWithPast]: + ) -> Union[tuple, VipLlavaModelOutputWithPast]: r""" - vision_feature_layers (`Union[int, List[int]]`, *optional*): + vision_feature_layers (`Union[int, list[int]]`, *optional*): The vision feature layer, or the list of indexes of the layers to select the vision feature. """ @@ -333,7 +333,7 @@ class VipLlavaForConditionalGeneration(VipLlavaPreTrainedModel, GenerationMixin) return self.model def get_image_features( - self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, List[int]]] = None + self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, list[int]]] = None ): return self.model.get_image_features(pixel_values=pixel_values, vision_feature_layers=vision_feature_layers) @@ -358,9 +358,9 @@ class VipLlavaForConditionalGeneration(VipLlavaPreTrainedModel, GenerationMixin) pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layers: Optional[Union[int, List[int]]] = None, + vision_feature_layers: Optional[Union[int, list[int]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -369,13 +369,13 @@ class VipLlavaForConditionalGeneration(VipLlavaPreTrainedModel, GenerationMixin) cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **lm_kwargs, - ) -> Union[Tuple, VipLlavaCausalLMOutputWithPast]: + ) -> Union[tuple, VipLlavaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. - vision_feature_layers (`Union[int, List[int]]`, *optional*): + vision_feature_layers (`Union[int, list[int]]`, *optional*): The vision feature layer, or the list of indexes of the layers to select the vision feature. diff --git a/src/transformers/models/vipllava/modular_vipllava.py b/src/transformers/models/vipllava/modular_vipllava.py index a673bba7a99..e9ac9772234 100644 --- a/src/transformers/models/vipllava/modular_vipllava.py +++ b/src/transformers/models/vipllava/modular_vipllava.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -72,7 +72,7 @@ class VipLlavaPreTrainedModel(LlavaPreTrainedModel): class VipLlavaModel(LlavaModel): def get_image_features( - self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, List[int]]] = None + self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, list[int]]] = None ): """ Obtains image last hidden states from the vision tower and apply multimodal projection. @@ -80,7 +80,7 @@ class VipLlavaModel(LlavaModel): Args: pixel_values (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`) The tensors corresponding to the input images. - vision_feature_layers (`Union[int, List[int]]`): + vision_feature_layers (`Union[int, list[int]]`): The vision feature layer, or the list of indexes of the layers to select the vision feature. Returns: @@ -109,18 +109,18 @@ class VipLlavaModel(LlavaModel): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layers: Optional[Union[int, List[int]]] = None, + vision_feature_layers: Optional[Union[int, list[int]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, **lm_kwargs, - ) -> Union[Tuple, VipLlavaModelOutputWithPast]: + ) -> Union[tuple, VipLlavaModelOutputWithPast]: r""" - vision_feature_layers (`Union[int, List[int]]`, *optional*): + vision_feature_layers (`Union[int, list[int]]`, *optional*): The vision feature layer, or the list of indexes of the layers to select the vision feature. """ @@ -185,7 +185,7 @@ class VipLlavaModel(LlavaModel): class VipLlavaForConditionalGeneration(LlavaForConditionalGeneration): def get_image_features( - self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, List[int]]] = None + self, pixel_values: torch.FloatTensor, vision_feature_layers: Optional[Union[int, list[int]]] = None ): return self.model.get_image_features(pixel_values=pixel_values, vision_feature_layers=vision_feature_layers) @@ -195,9 +195,9 @@ class VipLlavaForConditionalGeneration(LlavaForConditionalGeneration): pixel_values: torch.FloatTensor = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, - vision_feature_layers: Optional[Union[int, List[int]]] = None, + vision_feature_layers: Optional[Union[int, list[int]]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -206,13 +206,13 @@ class VipLlavaForConditionalGeneration(LlavaForConditionalGeneration): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **lm_kwargs, - ) -> Union[Tuple, VipLlavaCausalLMOutputWithPast]: + ) -> Union[tuple, VipLlavaCausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. - vision_feature_layers (`Union[int, List[int]]`, *optional*): + vision_feature_layers (`Union[int, list[int]]`, *optional*): The vision feature layer, or the list of indexes of the layers to select the vision feature. diff --git a/src/transformers/models/vision_encoder_decoder/modeling_flax_vision_encoder_decoder.py b/src/transformers/models/vision_encoder_decoder/modeling_flax_vision_encoder_decoder.py index 53424220e8b..c5467852c28 100644 --- a/src/transformers/models/vision_encoder_decoder/modeling_flax_vision_encoder_decoder.py +++ b/src/transformers/models/vision_encoder_decoder/modeling_flax_vision_encoder_decoder.py @@ -15,7 +15,7 @@ """Classes to support Vision-Encoder-Text-Decoder architectures""" import os -from typing import Optional, Tuple, Union +from typing import Optional, Union import flax.linen as nn import jax @@ -151,7 +151,7 @@ VISION_ENCODER_DECODER_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`jnp.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.decoder.max_position_embeddings - 1]`. - past_key_values (`Dict[str, jnp.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, jnp.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -280,7 +280,7 @@ class FlaxVisionEncoderDecoderModel(FlaxPreTrainedModel): def __init__( self, config: VisionEncoderDecoderConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -310,7 +310,7 @@ class FlaxVisionEncoderDecoderModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: encoder_input_shape, decoder_input_shape = input_shape # init input tensors diff --git a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py index f8597701a83..2158342f15a 100644 --- a/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py +++ b/src/transformers/models/vision_encoder_decoder/modeling_tf_vision_encoder_decoder.py @@ -18,7 +18,7 @@ from __future__ import annotations import re import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -85,7 +85,7 @@ VISION_ENCODER_DECODER_START_DOCSTRING = r""" VISION_ENCODER_DECODER_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using the vision's model's image processor. For example, using [`AutoImageProcessor`]. See [`ViTImageProcessor.__call__`] for details. decoder_input_ids (`np.ndarray` or `tf.Tensor` of shape `(batch_size, target_sequence_length)`, *optional*): @@ -462,8 +462,8 @@ class TFVisionEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLos pixel_values: np.ndarray | tf.Tensor | None = None, decoder_input_ids: np.ndarray | tf.Tensor | None = None, decoder_attention_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Union[Tuple, TFBaseModelOutput]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + encoder_outputs: Optional[Union[tuple, TFBaseModelOutput]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None = None, labels: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -472,7 +472,7 @@ class TFVisionEncoderDecoderModel(TFPreTrainedModel, TFCausalLanguageModelingLos return_dict: Optional[bool] = None, training: bool = False, **kwargs, - ) -> Union[TFSeq2SeqLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSeq2SeqLMOutput, tuple[tf.Tensor]]: r""" Returns: diff --git a/src/transformers/models/vision_encoder_decoder/modeling_vision_encoder_decoder.py b/src/transformers/models/vision_encoder_decoder/modeling_vision_encoder_decoder.py index c82f4b63113..2202558cfab 100644 --- a/src/transformers/models/vision_encoder_decoder/modeling_vision_encoder_decoder.py +++ b/src/transformers/models/vision_encoder_decoder/modeling_vision_encoder_decoder.py @@ -17,7 +17,7 @@ import gc import os import tempfile -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -441,8 +441,8 @@ class VisionEncoderDecoderModel(PreTrainedModel, GenerationMixin): pixel_values: Optional[torch.FloatTensor] = None, decoder_input_ids: Optional[torch.LongTensor] = None, decoder_attention_mask: Optional[torch.BoolTensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, decoder_inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -450,7 +450,7 @@ class VisionEncoderDecoderModel(PreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.FloatTensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.FloatTensor], Seq2SeqLMOutput]: r""" decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*): Indices of decoder input sequence tokens in the vocabulary. diff --git a/src/transformers/models/vision_text_dual_encoder/modeling_flax_vision_text_dual_encoder.py b/src/transformers/models/vision_text_dual_encoder/modeling_flax_vision_text_dual_encoder.py index fca25c04299..44c5e6c1729 100644 --- a/src/transformers/models/vision_text_dual_encoder/modeling_flax_vision_text_dual_encoder.py +++ b/src/transformers/models/vision_text_dual_encoder/modeling_flax_vision_text_dual_encoder.py @@ -14,7 +14,7 @@ # limitations under the License. """Flax VisionTextDualEncoder model.""" -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -223,7 +223,7 @@ class FlaxVisionTextDualEncoderModel(FlaxPreTrainedModel): def __init__( self, config: VisionTextDualEncoderConfig, - input_shape: Optional[Tuple] = None, + input_shape: Optional[tuple] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -240,7 +240,7 @@ class FlaxVisionTextDualEncoderModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensor input_ids = jnp.zeros(input_shape[0], dtype="i4") position_ids = jnp.broadcast_to(jnp.arange(jnp.atleast_2d(input_ids).shape[-1]), input_shape[0]) diff --git a/src/transformers/models/vision_text_dual_encoder/modeling_tf_vision_text_dual_encoder.py b/src/transformers/models/vision_text_dual_encoder/modeling_tf_vision_text_dual_encoder.py index 7d4b9613aed..c1ad6c5a454 100644 --- a/src/transformers/models/vision_text_dual_encoder/modeling_tf_vision_text_dual_encoder.py +++ b/src/transformers/models/vision_text_dual_encoder/modeling_tf_vision_text_dual_encoder.py @@ -17,7 +17,7 @@ from __future__ import annotations import re -from typing import Optional, Tuple, Union +from typing import Optional, Union import tensorflow as tf @@ -357,7 +357,7 @@ class TFVisionTextDualEncoderModel(TFPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFCLIPOutput]: + ) -> Union[tuple[tf.Tensor], TFCLIPOutput]: r""" Returns: @@ -602,7 +602,7 @@ class TFVisionTextDualEncoderModel(TFPreTrainedModel): Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. + `dict[str, tf.Tensor]`: The dummy inputs. """ input_ids = tf.constant(DUMMY_INPUTS, dtype=tf.int32) batch_size, seq_len = input_ids.shape diff --git a/src/transformers/models/vision_text_dual_encoder/modeling_vision_text_dual_encoder.py b/src/transformers/models/vision_text_dual_encoder/modeling_vision_text_dual_encoder.py index f7c3e1816f7..c581807bc44 100755 --- a/src/transformers/models/vision_text_dual_encoder/modeling_vision_text_dual_encoder.py +++ b/src/transformers/models/vision_text_dual_encoder/modeling_vision_text_dual_encoder.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch VisionTextDualEncoder model.""" -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -196,7 +196,7 @@ class VisionTextDualEncoderModel(PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], CLIPOutput]: + ) -> Union[tuple[torch.Tensor], CLIPOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. diff --git a/src/transformers/models/vision_text_dual_encoder/processing_vision_text_dual_encoder.py b/src/transformers/models/vision_text_dual_encoder/processing_vision_text_dual_encoder.py index 48952dc60b9..0cdf97329c3 100644 --- a/src/transformers/models/vision_text_dual_encoder/processing_vision_text_dual_encoder.py +++ b/src/transformers/models/vision_text_dual_encoder/processing_vision_text_dual_encoder.py @@ -17,7 +17,7 @@ Processor class for VisionTextDualEncoder """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...image_utils import ImageInput from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack, _validate_images_text_input_order @@ -70,7 +70,7 @@ class VisionTextDualEncoderProcessor(ProcessorMixin): def __call__( self, images: Optional[ImageInput] = None, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] = None, audio=None, videos=None, **kwargs: Unpack[VisionTextDualEncoderProcessorKwargs], @@ -83,11 +83,11 @@ class VisionTextDualEncoderProcessor(ProcessorMixin): of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`): + images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`): The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch tensor. Both channels-first and channels-last formats are supported. diff --git a/src/transformers/models/visual_bert/modeling_visual_bert.py b/src/transformers/models/visual_bert/modeling_visual_bert.py index 84e375ad22c..ee228e5f428 100755 --- a/src/transformers/models/visual_bert/modeling_visual_bert.py +++ b/src/transformers/models/visual_bert/modeling_visual_bert.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -549,8 +549,8 @@ class VisualBertForPreTrainingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None prediction_logits: Optional[torch.FloatTensor] = None seq_relationship_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring( @@ -612,7 +612,7 @@ class VisualBertModel(VisualBertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPooling]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPooling]: r""" visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*): The embedded representation of the visual inputs, generally derived using using an object detector. @@ -803,7 +803,7 @@ class VisualBertForPreTraining(VisualBertPreTrainedModel): return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, sentence_image_labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], VisualBertForPreTrainingOutput]: + ) -> Union[tuple[torch.Tensor], VisualBertForPreTrainingOutput]: r""" visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*): The embedded representation of the visual inputs, generally derived using using an object detector. @@ -947,7 +947,7 @@ class VisualBertForMultipleChoice(VisualBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1135,7 +1135,7 @@ class VisualBertForQuestionAnswering(VisualBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*): The embedded representation of the visual inputs, generally derived using using an object detector. @@ -1273,7 +1273,7 @@ class VisualBertForVisualReasoning(VisualBertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*): The embedded representation of the visual inputs, generally derived using using an object detector. @@ -1448,7 +1448,7 @@ class VisualBertForRegionToPhraseAlignment(VisualBertPreTrainedModel): return_dict: Optional[bool] = None, region_to_phrase_position: Optional[torch.LongTensor] = None, labels: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*): The embedded representation of the visual inputs, generally derived using using an object detector. diff --git a/src/transformers/models/vit/image_processing_vit.py b/src/transformers/models/vit/image_processing_vit.py index 654e56ab910..8da5f302d47 100644 --- a/src/transformers/models/vit/image_processing_vit.py +++ b/src/transformers/models/vit/image_processing_vit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for ViT.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -64,10 +64,10 @@ class ViTImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_convert_rgb (`bool`, *optional*): @@ -79,13 +79,13 @@ class ViTImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_rgb: Optional[bool] = None, **kwargs, ) -> None: @@ -105,7 +105,7 @@ class ViTImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -117,7 +117,7 @@ class ViTImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image. resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BILINEAR`): `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BILINEAR`. @@ -155,13 +155,13 @@ class ViTImageProcessor(BaseImageProcessor): self, images: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -176,7 +176,7 @@ class ViTImageProcessor(BaseImageProcessor): passing in images with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. resample (`PILImageResampling` filter, *optional*, defaults to `self.resample`): @@ -188,9 +188,9 @@ class ViTImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/vit/modeling_flax_vit.py b/src/transformers/models/vit/modeling_flax_vit.py index 8e86248cde1..d62ef1b6b92 100644 --- a/src/transformers/models/vit/modeling_flax_vit.py +++ b/src/transformers/models/vit/modeling_flax_vit.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -451,7 +451,7 @@ class FlaxViTPreTrainedModel(FlaxPreTrainedModel): input_shape = (1, config.image_size, config.image_size, config.num_channels) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors pixel_values = jnp.zeros(input_shape, dtype=self.dtype) diff --git a/src/transformers/models/vit/modeling_tf_vit.py b/src/transformers/models/vit/modeling_tf_vit.py index e18b38e597f..fae583a79bf 100644 --- a/src/transformers/models/vit/modeling_tf_vit.py +++ b/src/transformers/models/vit/modeling_tf_vit.py @@ -18,7 +18,7 @@ from __future__ import annotations import collections.abc import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -259,7 +259,7 @@ class TFViTSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) mixed_key_layer = self.key(inputs=hidden_states) @@ -355,7 +355,7 @@ class TFViTAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, head_mask=head_mask, output_attentions=output_attentions, training=training ) @@ -453,7 +453,7 @@ class TFViTLayer(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( # in ViT, layernorm is applied before self-attention input_tensor=self.layernorm_before(inputs=hidden_states), @@ -514,7 +514,7 @@ class TFViTEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -588,7 +588,7 @@ class TFViTMainLayer(keras.layers.Layer): interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: if pixel_values is None: raise ValueError("You have to specify pixel_values") @@ -704,7 +704,7 @@ VIT_START_DOCSTRING = r""" VIT_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ViTImageProcessor.__call__`] for details. @@ -761,7 +761,7 @@ class TFViTModel(TFViTPreTrainedModel): interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPooling, tuple[tf.Tensor]]: outputs = self.vit( pixel_values=pixel_values, head_mask=head_mask, @@ -860,7 +860,7 @@ class TFViTForImageClassification(TFViTPreTrainedModel, TFSequenceClassification return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/vit/modeling_vit.py b/src/transformers/models/vit/modeling_vit.py index b057958166f..6e85320a0fa 100644 --- a/src/transformers/models/vit/modeling_vit.py +++ b/src/transformers/models/vit/modeling_vit.py @@ -16,7 +16,7 @@ import collections.abc import math -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -224,7 +224,7 @@ class ViTSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -283,7 +283,7 @@ class ViTAttention(nn.Module): self.output = ViTSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -306,7 +306,7 @@ class ViTAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -364,7 +364,7 @@ class ViTLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViT, layernorm is applied before self-attention head_mask, @@ -505,7 +505,7 @@ class ViTModel(ViTPreTrainedModel): def get_input_embeddings(self) -> ViTPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -523,7 +523,7 @@ class ViTModel(ViTPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*): Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). diff --git a/src/transformers/models/vit_mae/modeling_tf_vit_mae.py b/src/transformers/models/vit_mae/modeling_tf_vit_mae.py index 822f0c4041b..93540add7df 100644 --- a/src/transformers/models/vit_mae/modeling_tf_vit_mae.py +++ b/src/transformers/models/vit_mae/modeling_tf_vit_mae.py @@ -20,7 +20,7 @@ import collections.abc import math from copy import deepcopy from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -77,8 +77,8 @@ class TFViTMAEModelOutput(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None mask: Optional[tf.Tensor] = None ids_restore: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -100,8 +100,8 @@ class TFViTMAEDecoderOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None @dataclass @@ -132,8 +132,8 @@ class TFViTMAEForPreTrainingOutput(ModelOutput): logits: Optional[tf.Tensor] = None mask: Optional[tf.Tensor] = None ids_restore: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None def get_2d_sincos_pos_embed(embed_dim, grid_size, add_cls_token=False): @@ -447,7 +447,7 @@ class TFViTMAESelfAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) mixed_key_layer = self.key(inputs=hidden_states) @@ -545,7 +545,7 @@ class TFViTMAEAttention(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, head_mask=head_mask, output_attentions=output_attentions, training=training ) @@ -646,7 +646,7 @@ class TFViTMAELayer(keras.layers.Layer): head_mask: tf.Tensor, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attention_outputs = self.attention( # in ViTMAE, layernorm is applied before self-attention input_tensor=self.layernorm_before(inputs=hidden_states), @@ -708,7 +708,7 @@ class TFViTMAEEncoder(keras.layers.Layer): output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None @@ -782,7 +782,7 @@ class TFViTMAEMainLayer(keras.layers.Layer): return_dict: Optional[bool] = None, training: bool = False, interpolate_pos_encoding: bool = False, - ) -> Union[TFViTMAEModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFViTMAEModelOutput, tuple[tf.Tensor]]: embedding_output, mask, ids_restore = self.embeddings( pixel_values=pixel_values, training=training, @@ -892,7 +892,7 @@ VIT_MAE_START_DOCSTRING = r""" VIT_MAE_INPUTS_DOCSTRING = r""" Args: - pixel_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` ``Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): + pixel_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` ``dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `(batch_size, num_channels, height, width)`): Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ViTImageProcessor.__call__`] for details. @@ -950,7 +950,7 @@ class TFViTMAEModel(TFViTMAEPreTrainedModel): return_dict: Optional[bool] = None, training: bool = False, interpolate_pos_encoding: bool = False, - ) -> Union[TFViTMAEModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFViTMAEModelOutput, tuple[tf.Tensor]]: r""" Returns: @@ -1219,12 +1219,12 @@ class TFViTMAEForPreTraining(TFViTMAEPreTrainedModel): ) return patchified_pixel_values - def unpatchify(self, patchified_pixel_values, original_image_size: Optional[Tuple[int, int]] = None): + def unpatchify(self, patchified_pixel_values, original_image_size: Optional[tuple[int, int]] = None): """ Args: patchified_pixel_values (`tf.Tensor` of shape `(batch_size, num_patches, patch_size**2 * num_channels)`: Patchified pixel values. - original_image_size (`Tuple[int, int]`, *optional*): + original_image_size (`tuple[int, int]`, *optional*): Original image size. Returns: @@ -1301,7 +1301,7 @@ class TFViTMAEForPreTraining(TFViTMAEPreTrainedModel): return_dict: Optional[bool] = None, training: bool = False, interpolate_pos_encoding: bool = False, - ) -> Union[TFViTMAEForPreTrainingOutput, Tuple[tf.Tensor]]: + ) -> Union[TFViTMAEForPreTrainingOutput, tuple[tf.Tensor]]: r""" Returns: diff --git a/src/transformers/models/vit_mae/modeling_vit_mae.py b/src/transformers/models/vit_mae/modeling_vit_mae.py index 326f193713f..61cf29f8569 100755 --- a/src/transformers/models/vit_mae/modeling_vit_mae.py +++ b/src/transformers/models/vit_mae/modeling_vit_mae.py @@ -17,7 +17,7 @@ import collections.abc from copy import deepcopy from dataclasses import dataclass -from typing import Callable, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -60,8 +60,8 @@ class ViTMAEModelOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None mask: Optional[torch.LongTensor] = None ids_restore: Optional[torch.LongTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -83,8 +83,8 @@ class ViTMAEDecoderOutput(ModelOutput): """ logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -115,8 +115,8 @@ class ViTMAEForPreTrainingOutput(ModelOutput): logits: Optional[torch.FloatTensor] = None mask: Optional[torch.LongTensor] = None ids_restore: Optional[torch.LongTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None def get_2d_sincos_pos_embed(embed_dim, grid_size, add_cls_token=False): @@ -404,7 +404,7 @@ class ViTMAESelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -465,7 +465,7 @@ class ViTMAEAttention(nn.Module): self.output = ViTMAESelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -488,7 +488,7 @@ class ViTMAEAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -549,7 +549,7 @@ class ViTMAELayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViTMAE, layernorm is applied before self-attention head_mask, @@ -689,7 +689,7 @@ class ViTMAEModel(ViTMAEPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, ViTMAEModelOutput]: + ) -> Union[tuple, ViTMAEModelOutput]: r""" interpolate_pos_encoding (`bool`, *optional*, default `False`): Whether to interpolate the pre-trained position encodings. This is mainly used to use the model on higher @@ -969,12 +969,12 @@ class ViTMAEForPreTraining(ViTMAEPreTrainedModel): ) return patchified_pixel_values - def unpatchify(self, patchified_pixel_values, original_image_size: Optional[Tuple[int, int]] = None): + def unpatchify(self, patchified_pixel_values, original_image_size: Optional[tuple[int, int]] = None): """ Args: patchified_pixel_values (`torch.FloatTensor` of shape `(batch_size, num_patches, patch_size**2 * num_channels)`: Patchified pixel values. - original_image_size (`Tuple[int, int]`, *optional*): + original_image_size (`tuple[int, int]`, *optional*): Original image size. Returns: @@ -1051,7 +1051,7 @@ class ViTMAEForPreTraining(ViTMAEPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, interpolate_pos_encoding: bool = False, - ) -> Union[Tuple, ViTMAEForPreTrainingOutput]: + ) -> Union[tuple, ViTMAEForPreTrainingOutput]: r""" interpolate_pos_encoding (`bool`, *optional*, default `False`): Whether to interpolate the pre-trained position encodings. This is mainly used to use the model on higher diff --git a/src/transformers/models/vit_msn/modeling_vit_msn.py b/src/transformers/models/vit_msn/modeling_vit_msn.py index 7684eb2b277..25efc1ac4de 100644 --- a/src/transformers/models/vit_msn/modeling_vit_msn.py +++ b/src/transformers/models/vit_msn/modeling_vit_msn.py @@ -15,7 +15,7 @@ """PyTorch ViT MSN (masked siamese network) model.""" import collections.abc -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -222,7 +222,7 @@ class ViTMSNSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -283,7 +283,7 @@ class ViTMSNAttention(nn.Module): self.output = ViTMSNSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -306,7 +306,7 @@ class ViTMSNAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -367,7 +367,7 @@ class ViTMSNLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in ViTMSN, layernorm is applied before self-attention head_mask, @@ -496,7 +496,7 @@ class ViTMSNModel(ViTMSNPreTrainedModel): def get_input_embeddings(self) -> ViTMSNPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel diff --git a/src/transformers/models/vitdet/configuration_vitdet.py b/src/transformers/models/vitdet/configuration_vitdet.py index cd91dce9b29..7cc7f6b875c 100644 --- a/src/transformers/models/vitdet/configuration_vitdet.py +++ b/src/transformers/models/vitdet/configuration_vitdet.py @@ -62,9 +62,9 @@ class VitDetConfig(BackboneConfigMixin, PretrainedConfig): Whether to add a bias to the queries, keys and values. drop_path_rate (`float`, *optional*, defaults to 0.0): Stochastic depth rate. - window_block_indices (`List[int]`, *optional*, defaults to `[]`): + window_block_indices (`list[int]`, *optional*, defaults to `[]`): List of indices of blocks that should have window attention instead of regular global self-attention. - residual_block_indices (`List[int]`, *optional*, defaults to `[]`): + residual_block_indices (`list[int]`, *optional*, defaults to `[]`): List of indices of blocks that should have an extra residual block after the MLP. use_absolute_position_embeddings (`bool`, *optional*, defaults to `True`): Whether to add absolute position embeddings to the patch embeddings. @@ -72,12 +72,12 @@ class VitDetConfig(BackboneConfigMixin, PretrainedConfig): Whether to add relative position embeddings to the attention maps. window_size (`int`, *optional*, defaults to 0): The size of the attention window. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/vitdet/modeling_vitdet.py b/src/transformers/models/vitdet/modeling_vitdet.py index 6b31137cf01..d327650c065 100644 --- a/src/transformers/models/vitdet/modeling_vitdet.py +++ b/src/transformers/models/vitdet/modeling_vitdet.py @@ -16,7 +16,7 @@ import collections.abc import math -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -172,9 +172,9 @@ def add_decomposed_relative_positions(attn, queries, rel_pos_h, rel_pos_w, q_siz Relative position embeddings (Lh, num_channels) for height axis. rel_pos_w (`torch.Tensor`): Relative position embeddings (Lw, num_channels) for width axis. - q_size (`Tuple[int]`): + q_size (`tuple[int]`): Spatial sequence size of query q with (queries_height, queries_width). - k_size (`Tuple[int]`): + k_size (`tuple[int]`): Spatial sequence size of key k with (keys_height, keys_width). Returns: @@ -207,7 +207,7 @@ class VitDetAttention(nn.Module): Args: config (`VitDetConfig`): Model configuration. - input_size (`Tuple[int]`, *optional*): + input_size (`tuple[int]`, *optional*): Input resolution, only required in case relative position embeddings are added. """ super().__init__() @@ -417,9 +417,9 @@ def window_unpartition(windows, window_size, pad_height_width, height_width): Input tokens with [batch_size * num_windows, window_size, window_size, num_channels]. window_size (`int`): Window size. - pad_height_width (`Tuple[int]`): + pad_height_width (`tuple[int]`): Padded height and width (padded_height, padded_width). - height_width (`Tuple[int]`): + height_width (`tuple[int]`): Original height and width before padding. Returns: @@ -482,7 +482,7 @@ class VitDetLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: hidden_states = hidden_states.permute(0, 2, 3, 1) shortcut = hidden_states @@ -668,7 +668,7 @@ class VitDetModel(VitDetPreTrainedModel): def get_input_embeddings(self) -> VitDetEmbeddings: return self.embeddings.projection - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base class PreTrainedModel @@ -684,7 +684,7 @@ class VitDetModel(VitDetPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Examples: diff --git a/src/transformers/models/vitmatte/configuration_vitmatte.py b/src/transformers/models/vitmatte/configuration_vitmatte.py index b9f78043306..ff99c8287ee 100644 --- a/src/transformers/models/vitmatte/configuration_vitmatte.py +++ b/src/transformers/models/vitmatte/configuration_vitmatte.py @@ -15,7 +15,6 @@ """VitMatte model configuration""" import copy -from typing import List from ...configuration_utils import PretrainedConfig from ...utils import logging @@ -57,9 +56,9 @@ class VitMatteConfig(PretrainedConfig): The epsilon used by the batch norm layers. initializer_range (`float`, *optional*, defaults to 0.02): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. - convstream_hidden_sizes (`List[int]`, *optional*, defaults to `[48, 96, 192]`): + convstream_hidden_sizes (`list[int]`, *optional*, defaults to `[48, 96, 192]`): The output channels of the ConvStream module. - fusion_hidden_sizes (`List[int]`, *optional*, defaults to `[256, 128, 64, 32]`): + fusion_hidden_sizes (`list[int]`, *optional*, defaults to `[256, 128, 64, 32]`): The output channels of the Fusion blocks. Example: @@ -89,8 +88,8 @@ class VitMatteConfig(PretrainedConfig): hidden_size: int = 384, batch_norm_eps: float = 1e-5, initializer_range: float = 0.02, - convstream_hidden_sizes: List[int] = [48, 96, 192], - fusion_hidden_sizes: List[int] = [256, 128, 64, 32], + convstream_hidden_sizes: list[int] = [48, 96, 192], + fusion_hidden_sizes: list[int] = [256, 128, 64, 32], **kwargs, ): super().__init__(**kwargs) @@ -125,7 +124,7 @@ class VitMatteConfig(PretrainedConfig): def to_dict(self): """ Serializes this instance to a Python dictionary. Override the default [`~PretrainedConfig.to_dict`]. Returns: - `Dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, any]`: Dictionary of all the attributes that make up this configuration instance, """ output = copy.deepcopy(self.__dict__) output["backbone_config"] = self.backbone_config.to_dict() diff --git a/src/transformers/models/vitmatte/image_processing_vitmatte.py b/src/transformers/models/vitmatte/image_processing_vitmatte.py index 464a67ef63a..15d3ec60460 100644 --- a/src/transformers/models/vitmatte/image_processing_vitmatte.py +++ b/src/transformers/models/vitmatte/image_processing_vitmatte.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for ViTMatte.""" -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -53,10 +53,10 @@ class VitMatteImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_pad (`bool`, *optional*, defaults to `True`): @@ -73,8 +73,8 @@ class VitMatteImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: bool = True, size_divisibility: int = 32, **kwargs, @@ -137,8 +137,8 @@ class VitMatteImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_pad: Optional[bool] = None, size_divisibility: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -160,9 +160,9 @@ class VitMatteImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. do_pad (`bool`, *optional*, defaults to `self.do_pad`): Whether to pad the image. diff --git a/src/transformers/models/vitmatte/modeling_vitmatte.py b/src/transformers/models/vitmatte/modeling_vitmatte.py index 9e094285866..ff672d106b7 100644 --- a/src/transformers/models/vitmatte/modeling_vitmatte.py +++ b/src/transformers/models/vitmatte/modeling_vitmatte.py @@ -15,7 +15,7 @@ """PyTorch ViTMatte model.""" from dataclasses import dataclass -from typing import Optional, Tuple +from typing import Optional import torch from torch import nn @@ -50,8 +50,8 @@ class ImageMattingOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None alphas: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @auto_docstring diff --git a/src/transformers/models/vitpose/image_processing_vitpose.py b/src/transformers/models/vitpose/image_processing_vitpose.py index fc1a8719391..77cd0c92d77 100644 --- a/src/transformers/models/vitpose/image_processing_vitpose.py +++ b/src/transformers/models/vitpose/image_processing_vitpose.py @@ -16,7 +16,7 @@ import itertools import math -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -54,7 +54,7 @@ logger = logging.get_logger(__name__) # inspired by https://github.com/ViTAE-Transformer/ViTPose/blob/d5216452796c90c6bc29f5c5ec0bdba94366768a/mmpose/datasets/datasets/base/kpt_2d_sview_rgb_img_top_down_dataset.py#L132 def box_to_center_and_scale( - box: Union[Tuple, List, np.ndarray], + box: Union[tuple, list, np.ndarray], image_width: int, image_height: int, normalize_factor: float = 200.0, @@ -117,7 +117,7 @@ def coco_to_pascal_voc(bboxes: np.ndarray) -> np.ndarray: return bboxes -def get_keypoint_predictions(heatmaps: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def get_keypoint_predictions(heatmaps: np.ndarray) -> tuple[np.ndarray, np.ndarray]: """Get keypoint predictions from score maps. Args: @@ -332,7 +332,7 @@ class VitPoseImageProcessor(BaseImageProcessor): Args: do_affine_transform (`bool`, *optional*, defaults to `True`): Whether to apply an affine transformation to the input images. - size (`Dict[str, int]` *optional*, defaults to `{"height": 256, "width": 192}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 256, "width": 192}`): Resolution of the image after `affine_transform` is applied. Only has an effect if `do_affine_transform` is set to `True`. Can be overridden by `size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -342,9 +342,9 @@ class VitPoseImageProcessor(BaseImageProcessor): method. do_normalize (`bool`, *optional*, defaults to `True`): Whether or not to normalize the input with mean and standard deviation. - image_mean (`List[int]`, defaults to `[0.485, 0.456, 0.406]`, *optional*): + image_mean (`list[int]`, defaults to `[0.485, 0.456, 0.406]`, *optional*): The sequence of means for each channel, to be used when normalizing images. - image_std (`List[int]`, defaults to `[0.229, 0.224, 0.225]`, *optional*): + image_std (`list[int]`, defaults to `[0.229, 0.224, 0.225]`, *optional*): The sequence of standard deviations for each channel, to be used when normalizing images. """ @@ -353,12 +353,12 @@ class VitPoseImageProcessor(BaseImageProcessor): def __init__( self, do_affine_transform: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ): super().__init__(**kwargs) @@ -374,10 +374,10 @@ class VitPoseImageProcessor(BaseImageProcessor): def affine_transform( self, image: np.array, - center: Tuple[float], - scale: Tuple[float], + center: tuple[float], + scale: tuple[float], rotation: float, - size: Dict[str, int], + size: dict[str, int], data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.array: @@ -387,13 +387,13 @@ class VitPoseImageProcessor(BaseImageProcessor): Args: image (`np.array`): Image to transform. - center (`Tuple[float]`): + center (`tuple[float]`): Center of the bounding box (x, y). - scale (`Tuple[float]`): + scale (`tuple[float]`): Scale of the bounding box with respect to height/width. rotation (`float`): Rotation angle in degrees. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the destination image. data_format (`ChannelDimension`, *optional*, defaults to `ChannelDimension.FIRST`): The channel dimension format of the output image. @@ -423,14 +423,14 @@ class VitPoseImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - boxes: Union[List[List[float]], np.ndarray], + boxes: Union[list[list[float]], np.ndarray], do_affine_transform: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -443,13 +443,13 @@ class VitPoseImageProcessor(BaseImageProcessor): Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - boxes (`List[List[List[float]]]` or `np.ndarray`): + boxes (`list[list[list[float]]]` or `np.ndarray`): List or array of bounding boxes for each image. Each box should be a list of 4 floats representing the bounding box coordinates in COCO format (top_left_x, top_left_y, width, height). do_affine_transform (`bool`, *optional*, defaults to `self.do_affine_transform`): Whether to apply an affine transformation to the input images. - size (`Dict[str, int]` *optional*, defaults to `self.size`): + size (`dict[str, int]` *optional*, defaults to `self.size`): Dictionary in the format `{"height": h, "width": w}` specifying the size of the output image after resizing. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -458,9 +458,9 @@ class VitPoseImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean to use if `do_normalize` is set to `True`. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation to use if `do_normalize` is set to `True`. return_tensors (`str` or [`~utils.TensorType`], *optional*, defaults to `'np'`): If set, will return tensors of a particular framework. Acceptable values are: @@ -597,10 +597,10 @@ class VitPoseImageProcessor(BaseImageProcessor): def post_process_pose_estimation( self, outputs: "VitPoseEstimatorOutput", - boxes: Union[List[List[List[float]]], np.ndarray], + boxes: Union[list[list[list[float]]], np.ndarray], kernel_size: int = 11, threshold: Optional[float] = None, - target_sizes: Union[TensorType, List[Tuple]] = None, + target_sizes: Union[TensorType, list[tuple]] = None, ): """ Transform the heatmaps into keypoint predictions and transform them back to the image. @@ -608,18 +608,18 @@ class VitPoseImageProcessor(BaseImageProcessor): Args: outputs (`VitPoseEstimatorOutput`): VitPoseForPoseEstimation model outputs. - boxes (`List[List[List[float]]]` or `np.ndarray`): + boxes (`list[list[list[float]]]` or `np.ndarray`): List or array of bounding boxes for each image. Each box should be a list of 4 floats representing the bounding box coordinates in COCO format (top_left_x, top_left_y, width, height). kernel_size (`int`, *optional*, defaults to 11): Gaussian kernel size (K) for modulation. threshold (`float`, *optional*, defaults to None): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will be resize with the default value. Returns: - `List[List[Dict]]`: A list of dictionaries, each dictionary containing the keypoints and boxes for an image + `list[list[Dict]]`: A list of dictionaries, each dictionary containing the keypoints and boxes for an image in the batch as predicted by the model. """ @@ -658,12 +658,12 @@ class VitPoseImageProcessor(BaseImageProcessor): labels = torch.arange(0, num_keypoints) bboxes_xyxy = torch.tensor(coco_to_pascal_voc(all_boxes)) - results: List[List[Dict[str, torch.Tensor]]] = [] + results: list[list[dict[str, torch.Tensor]]] = [] pose_bbox_pairs = zip(poses, scores, bboxes_xyxy) for image_bboxes in boxes: - image_results: List[Dict[str, torch.Tensor]] = [] + image_results: list[dict[str, torch.Tensor]] = [] for _ in image_bboxes: # Unpack the next pose and bbox_xyxy from the iterator pose, score, bbox_xyxy = next(pose_bbox_pairs) diff --git a/src/transformers/models/vitpose/modeling_vitpose.py b/src/transformers/models/vitpose/modeling_vitpose.py index d872bd75db2..e63f69bed63 100644 --- a/src/transformers/models/vitpose/modeling_vitpose.py +++ b/src/transformers/models/vitpose/modeling_vitpose.py @@ -15,7 +15,7 @@ """PyTorch VitPose model.""" from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -60,8 +60,8 @@ class VitPoseEstimatorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None heatmaps: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring diff --git a/src/transformers/models/vitpose_backbone/configuration_vitpose_backbone.py b/src/transformers/models/vitpose_backbone/configuration_vitpose_backbone.py index 439e596273d..b3edc62f485 100644 --- a/src/transformers/models/vitpose_backbone/configuration_vitpose_backbone.py +++ b/src/transformers/models/vitpose_backbone/configuration_vitpose_backbone.py @@ -35,7 +35,7 @@ class VitPoseBackboneConfig(BackboneConfigMixin, PretrainedConfig): Args: image_size (`int`, *optional*, defaults to `[256, 192]`): The size (resolution) of each image. - patch_size (`List[int]`, *optional*, defaults to `[16, 16]`): + patch_size (`list[int]`, *optional*, defaults to `[16, 16]`): The size (resolution) of each patch. num_channels (`int`, *optional*, defaults to 3): The number of input channels. @@ -64,12 +64,12 @@ class VitPoseBackboneConfig(BackboneConfigMixin, PretrainedConfig): The epsilon used by the layer normalization layers. qkv_bias (`bool`, *optional*, defaults to `True`): Whether to add a bias to the queries, keys and values. - out_features (`List[str]`, *optional*): + out_features (`list[str]`, *optional*): If used as backbone, list of features to output. Can be any of `"stem"`, `"stage1"`, `"stage2"`, etc. (depending on how many stages the model has). If unset and `out_indices` is set, will default to the corresponding stages. If unset and `out_indices` is unset, will default to the last stage. Must be in the same order as defined in the `stage_names` attribute. - out_indices (`List[int]`, *optional*): + out_indices (`list[int]`, *optional*): If used as backbone, list of indices of features to output. Can be any of 0, 1, 2, etc. (depending on how many stages the model has). If unset and `out_features` is set, will default to the corresponding stages. If unset and `out_features` is unset, will default to the last stage. Must be in the diff --git a/src/transformers/models/vitpose_backbone/modeling_vitpose_backbone.py b/src/transformers/models/vitpose_backbone/modeling_vitpose_backbone.py index 8f329de6e17..7579ecb9fbb 100644 --- a/src/transformers/models/vitpose_backbone/modeling_vitpose_backbone.py +++ b/src/transformers/models/vitpose_backbone/modeling_vitpose_backbone.py @@ -20,7 +20,7 @@ This code is the same as the original Vision Transformer (ViT) with 2 modificati """ import collections.abc -from typing import Callable, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -154,7 +154,7 @@ class VitPoseBackboneSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -215,7 +215,7 @@ class VitPoseBackboneAttention(nn.Module): self.output = VitPoseBackboneSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -238,7 +238,7 @@ class VitPoseBackboneAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -317,7 +317,7 @@ class VitPoseBackboneLayer(nn.Module): dataset_index: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: # Validate dataset_index when using multiple experts if self.num_experts > 1 and dataset_index is None: raise ValueError( diff --git a/src/transformers/models/vits/configuration_vits.py b/src/transformers/models/vits/configuration_vits.py index 2d33601885a..96407d1ac0d 100644 --- a/src/transformers/models/vits/configuration_vits.py +++ b/src/transformers/models/vits/configuration_vits.py @@ -77,18 +77,18 @@ class VitsConfig(PretrainedConfig): Number of channels used by the speaker embeddings. Is zero for single-speaker models. upsample_initial_channel (`int`, *optional*, defaults to 512): The number of input channels into the HiFi-GAN upsampling network. - upsample_rates (`Tuple[int]` or `List[int]`, *optional*, defaults to `[8, 8, 2, 2]`): + upsample_rates (`tuple[int]` or `list[int]`, *optional*, defaults to `[8, 8, 2, 2]`): A tuple of integers defining the stride of each 1D convolutional layer in the HiFi-GAN upsampling network. The length of `upsample_rates` defines the number of convolutional layers and has to match the length of `upsample_kernel_sizes`. - upsample_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[16, 16, 4, 4]`): + upsample_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[16, 16, 4, 4]`): A tuple of integers defining the kernel size of each 1D convolutional layer in the HiFi-GAN upsampling network. The length of `upsample_kernel_sizes` defines the number of convolutional layers and has to match the length of `upsample_rates`. - resblock_kernel_sizes (`Tuple[int]` or `List[int]`, *optional*, defaults to `[3, 7, 11]`): + resblock_kernel_sizes (`tuple[int]` or `list[int]`, *optional*, defaults to `[3, 7, 11]`): A tuple of integers defining the kernel sizes of the 1D convolutional layers in the HiFi-GAN multi-receptive field fusion (MRF) module. - resblock_dilation_sizes (`Tuple[Tuple[int]]` or `List[List[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): + resblock_dilation_sizes (`tuple[tuple[int]]` or `list[list[int]]`, *optional*, defaults to `[[1, 3, 5], [1, 3, 5], [1, 3, 5]]`): A nested tuple of integers defining the dilation rates of the dilated 1D convolutional layers in the HiFi-GAN multi-receptive field fusion (MRF) module. leaky_relu_slope (`float`, *optional*, defaults to 0.1): diff --git a/src/transformers/models/vits/modeling_vits.py b/src/transformers/models/vits/modeling_vits.py index ac95b933316..65b77a4ccf2 100644 --- a/src/transformers/models/vits/modeling_vits.py +++ b/src/transformers/models/vits/modeling_vits.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import torch @@ -64,9 +64,9 @@ class VitsModelOutput(ModelOutput): waveform: Optional[torch.FloatTensor] = None sequence_lengths: Optional[torch.FloatTensor] = None - spectrogram: Optional[Tuple[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + spectrogram: Optional[tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @dataclass @@ -97,8 +97,8 @@ class VitsTextEncoderOutput(ModelOutput): last_hidden_state: Optional[torch.FloatTensor] = None prior_means: Optional[torch.FloatTensor] = None prior_log_variances: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None @torch.jit.script @@ -897,7 +897,7 @@ class VitsAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1122,7 +1122,7 @@ class VitsEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1209,7 +1209,7 @@ class VitsTextEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], VitsTextEncoderOutput]: + ) -> Union[tuple[torch.Tensor], VitsTextEncoderOutput]: hidden_states = self.embed_tokens(input_ids) * math.sqrt(self.config.hidden_size) encoder_outputs = self.encoder( @@ -1311,7 +1311,7 @@ class VitsModel(VitsPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.FloatTensor] = None, - ) -> Union[Tuple[Any], VitsModelOutput]: + ) -> Union[tuple[Any], VitsModelOutput]: r""" speaker_id (`int`, *optional*): Which speaker embedding to use. Only used for multispeaker models. diff --git a/src/transformers/models/vits/tokenization_vits.py b/src/transformers/models/vits/tokenization_vits.py index ca40c80c124..cf2d47bc4e8 100644 --- a/src/transformers/models/vits/tokenization_vits.py +++ b/src/transformers/models/vits/tokenization_vits.py @@ -17,7 +17,7 @@ import json import os import re -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union from ...tokenization_utils import PreTrainedTokenizer from ...utils import is_phonemizer_available, is_uroman_available, logging @@ -141,7 +141,7 @@ class VitsTokenizer(PreTrainedTokenizer): def prepare_for_tokenization( self, text: str, is_split_into_words: bool = False, normalize: Optional[bool] = None, **kwargs - ) -> Tuple[str, Dict[str, Any]]: + ) -> tuple[str, dict[str, Any]]: """ Performs any necessary transformations before tokenization. @@ -159,11 +159,11 @@ class VitsTokenizer(PreTrainedTokenizer): Whether or not to apply punctuation and casing normalization to the text inputs. Typically, VITS is trained on lower-cased and un-punctuated text. Hence, normalization is used to ensure that the input text consists only of lower-case characters. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Keyword arguments to use for the tokenization. Returns: - `Tuple[str, Dict[str, Any]]`: The prepared text and the unused kwargs. + `tuple[str, dict[str, Any]]`: The prepared text and the unused kwargs. """ normalize = normalize if normalize is not None else self.normalize @@ -204,7 +204,7 @@ class VitsTokenizer(PreTrainedTokenizer): return filtered_text, kwargs - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string by inserting the `` token at the boundary between adjacent characters.""" tokens = list(text) @@ -215,7 +215,7 @@ class VitsTokenizer(PreTrainedTokenizer): return tokens - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: if self.add_blank and len(tokens) > 1: tokens = tokens[1::2] return "".join(tokens) @@ -228,7 +228,7 @@ class VitsTokenizer(PreTrainedTokenizer): """Converts an index (integer) in a token (str) using the vocab.""" return self.decoder.get(index) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Union[Tuple[str], None]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Union[tuple[str], None]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/vivit/configuration_vivit.py b/src/transformers/models/vivit/configuration_vivit.py index 42863454e81..02c776b3500 100644 --- a/src/transformers/models/vivit/configuration_vivit.py +++ b/src/transformers/models/vivit/configuration_vivit.py @@ -36,7 +36,7 @@ class VivitConfig(PretrainedConfig): The size (resolution) of each image. num_frames (`int`, *optional*, defaults to 32): The number of frames in each video. - tubelet_size (`List[int]`, *optional*, defaults to `[2, 16, 16]`): + tubelet_size (`list[int]`, *optional*, defaults to `[2, 16, 16]`): The size (resolution) of each tubelet. num_channels (`int`, *optional*, defaults to 3): The number of input channels. diff --git a/src/transformers/models/vivit/image_processing_vivit.py b/src/transformers/models/vivit/image_processing_vivit.py index 7e984f02519..140f7fc5fe7 100644 --- a/src/transformers/models/vivit/image_processing_vivit.py +++ b/src/transformers/models/vivit/image_processing_vivit.py @@ -14,7 +14,7 @@ # limitations under the License. """Image processor class for Vivit.""" -from typing import Dict, List, Optional, Union +from typing import Optional, Union import numpy as np @@ -50,7 +50,7 @@ if is_vision_available(): logger = logging.get_logger(__name__) -def make_batched(videos) -> List[List[ImageInput]]: +def make_batched(videos) -> list[list[ImageInput]]: if isinstance(videos, (list, tuple)) and isinstance(videos[0], (list, tuple)) and is_valid_image(videos[0][0]): return videos @@ -71,7 +71,7 @@ class VivitImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 256}`): Size of the output image after resizing. The shortest edge of the image will be resized to `size["shortest_edge"]` while maintaining the aspect ratio of the original image. Can be overridden by `size` in the `preprocess` method. @@ -81,7 +81,7 @@ class VivitImageProcessor(BaseImageProcessor): do_center_crop (`bool`, *optional*, defaults to `True`): Whether to center crop the image to the specified `crop_size`. Can be overridden by the `do_center_crop` parameter in the `preprocess` method. - crop_size (`Dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): + crop_size (`dict[str, int]`, *optional*, defaults to `{"height": 224, "width": 224}`): Size of the image after applying the center crop. Can be overridden by the `crop_size` parameter in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `True`): @@ -96,10 +96,10 @@ class VivitImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. """ @@ -109,16 +109,16 @@ class VivitImageProcessor(BaseImageProcessor): def __init__( self, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_center_crop: bool = True, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 127.5, offset: bool = True, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, **kwargs, ) -> None: super().__init__(**kwargs) @@ -142,7 +142,7 @@ class VivitImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[Union[str, ChannelDimension]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -154,7 +154,7 @@ class VivitImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the output image. If `size` is of the form `{"height": h, "width": w}`, the output image will have the size `(h, w)`. If `size` is of the form `{"shortest_edge": s}`, the output image will have its shortest edge of length `s` while keeping the aspect ratio of the original image. @@ -228,16 +228,16 @@ class VivitImageProcessor(BaseImageProcessor): self, image: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, offset: Optional[bool] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, data_format: Optional[ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, ) -> np.ndarray: @@ -291,16 +291,16 @@ class VivitImageProcessor(BaseImageProcessor): self, videos: ImageInput, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = None, do_center_crop: Optional[bool] = None, - crop_size: Optional[Dict[str, int]] = None, + crop_size: Optional[dict[str, int]] = None, do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, offset: Optional[bool] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, return_tensors: Optional[Union[str, TensorType]] = None, data_format: ChannelDimension = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -314,14 +314,14 @@ class VivitImageProcessor(BaseImageProcessor): to 255. If passing in frames with pixel values between 0 and 1, set `do_rescale=False`. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after applying resize. resample (`PILImageResampling`, *optional*, defaults to `self.resample`): Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`, Only has an effect if `do_resize` is set to `True`. do_center_crop (`bool`, *optional*, defaults to `self.do_centre_crop`): Whether to centre crop the image. - crop_size (`Dict[str, int]`, *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]`, *optional*, defaults to `self.crop_size`): Size of the image after applying the centre crop. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): Whether to rescale the image values between `[-1 - 1]` if `offset` is `True`, `[0, 1]` otherwise. @@ -331,9 +331,9 @@ class VivitImageProcessor(BaseImageProcessor): Whether to scale the image in both negative and positive directions. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. return_tensors (`str` or `TensorType`, *optional*): The type of tensors to return. Can be one of: diff --git a/src/transformers/models/vivit/modeling_vivit.py b/src/transformers/models/vivit/modeling_vivit.py index 2ca76996ad1..0617e20de3b 100755 --- a/src/transformers/models/vivit/modeling_vivit.py +++ b/src/transformers/models/vivit/modeling_vivit.py @@ -14,7 +14,7 @@ # limitations under the License. """PyTorch ViViT model.""" -from typing import Callable, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -215,7 +215,7 @@ class VivitSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -276,7 +276,7 @@ class VivitAttention(nn.Module): self.output = VivitSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -299,7 +299,7 @@ class VivitAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -521,7 +521,7 @@ class VivitModel(VivitPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], BaseModelOutputWithPooling]: + ) -> Union[tuple[torch.FloatTensor], BaseModelOutputWithPooling]: r""" Examples: @@ -540,7 +540,7 @@ class VivitModel(VivitPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -564,7 +564,7 @@ class VivitModel(VivitPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) @@ -668,7 +668,7 @@ class VivitForVideoClassification(VivitPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.FloatTensor], ImageClassifierOutput]: + ) -> Union[tuple[torch.FloatTensor], ImageClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the image classification/regression loss. Indices should be in `[0, ..., @@ -693,7 +693,7 @@ class VivitForVideoClassification(VivitPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -717,7 +717,7 @@ class VivitForVideoClassification(VivitPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/vjepa2/modeling_vjepa2.py b/src/transformers/models/vjepa2/modeling_vjepa2.py index 8e13434307c..bb139d4aeee 100644 --- a/src/transformers/models/vjepa2/modeling_vjepa2.py +++ b/src/transformers/models/vjepa2/modeling_vjepa2.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -55,8 +55,8 @@ class VJEPA2WithMaskedInputPredictorOutput(ModelOutput): last_hidden_state: torch.FloatTensor masked_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None target_hidden_state: Optional[torch.FloatTensor] = None @@ -88,8 +88,8 @@ class VJEPA2WithMaskedInputModelOutput(ModelOutput): last_hidden_state: torch.FloatTensor masked_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None predictor_output: Optional[VJEPA2WithMaskedInputPredictorOutput] = None def to_tuple(self): @@ -329,7 +329,7 @@ class VJEPA2RopeAttention(nn.Module): position_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, head_mask: Optional[torch.Tensor] = None, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: mixed_query_layer = self.query(hidden_states) key_layer = self.transpose_for_scores(self.key(hidden_states)) @@ -450,7 +450,7 @@ class VJEPA2Layer(GradientCheckpointingLayer): position_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: # Self-Attention residual = hidden_states hidden_states = self.norm1(hidden_states) @@ -537,7 +537,7 @@ class VJEPA2Encoder(nn.Module): ) -def apply_masks(tensor: torch.Tensor, masks: List[torch.Tensor]) -> torch.Tensor: +def apply_masks(tensor: torch.Tensor, masks: list[torch.Tensor]) -> torch.Tensor: """ Args: tensor (`torch.Tensor`): @@ -586,10 +586,10 @@ class VJEPA2PredictorEmbeddings(nn.Module): def forward( self, hidden_states: torch.Tensor, - context_mask: List[torch.Tensor], - target_mask: List[torch.Tensor], + context_mask: list[torch.Tensor], + target_mask: list[torch.Tensor], mask_index: int = 1, - ) -> Tuple[torch.Tensor, torch.Tensor]: + ) -> tuple[torch.Tensor, torch.Tensor]: """ hidden_states : encoder outputs (context) context_mask: tokens of the context (outputs from the encoder) @@ -697,8 +697,8 @@ class VJEPA2Predictor(nn.Module): def forward( self, encoder_hidden_states: torch.Tensor, - context_mask: List[torch.Tensor], - target_mask: List[torch.Tensor], + context_mask: list[torch.Tensor], + target_mask: list[torch.Tensor], head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, output_hidden_states: bool = False, @@ -772,7 +772,7 @@ class VJEPA2PoolerSelfAttention(nn.Module): hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -846,7 +846,7 @@ class VJEPA2PoolerCrossAttention(nn.Module): values: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, q_seq_length, embed_dim = queries.shape @@ -903,7 +903,7 @@ class VJEPA2PoolerSelfAttentionLayer(GradientCheckpointingLayer): hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: """ Args: hidden_states (`torch.FloatTensor`): @@ -950,7 +950,7 @@ class VJEPA2PoolerCrossAttentionLayer(GradientCheckpointingLayer): hidden_state: torch.Tensor, attention_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, ...]: + ) -> tuple[torch.Tensor, ...]: # Apply cross-attention residual = queries hidden_state = self.layer_norm1(hidden_state) @@ -1080,9 +1080,9 @@ class VJEPA2Model(VJEPA2PreTrainedModel): self, pixel_values_videos: torch.Tensor, context_head_mask: Optional[torch.Tensor] = None, - context_mask: Optional[List[torch.Tensor]] = None, + context_mask: Optional[list[torch.Tensor]] = None, target_head_mask: Optional[torch.Tensor] = None, - target_mask: Optional[List[torch.Tensor]] = None, + target_mask: Optional[list[torch.Tensor]] = None, skip_predictor: bool = False, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, @@ -1191,7 +1191,7 @@ class VJEPA2ForVideoClassification(VJEPA2PreTrainedModel): labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, - ) -> Union[Tuple, ImageClassifierOutput]: + ) -> Union[tuple, ImageClassifierOutput]: r""" pixel_values_videos (`torch.Tensor` with shape `[batch size x num_frames x num_channels x height x width]`): The input video pixels which is processed by VJEPA2VideoProcessor. diff --git a/src/transformers/models/wav2vec2/configuration_wav2vec2.py b/src/transformers/models/wav2vec2/configuration_wav2vec2.py index 4598e7c255a..e6fffc0ffa4 100644 --- a/src/transformers/models/wav2vec2/configuration_wav2vec2.py +++ b/src/transformers/models/wav2vec2/configuration_wav2vec2.py @@ -78,13 +78,13 @@ class Wav2Vec2Config(PretrainedConfig): extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. feat_quantizer_dropout (`float`, *optional*, defaults to 0.0): The dropout probability for quantized feature encoder states. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -156,13 +156,13 @@ class Wav2Vec2Config(PretrainedConfig): instance of [`Wav2Vec2ForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 256): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/wav2vec2/feature_extraction_wav2vec2.py b/src/transformers/models/wav2vec2/feature_extraction_wav2vec2.py index 3dde386b32a..6bca69e82d0 100644 --- a/src/transformers/models/wav2vec2/feature_extraction_wav2vec2.py +++ b/src/transformers/models/wav2vec2/feature_extraction_wav2vec2.py @@ -16,7 +16,7 @@ Feature extractor class for Wav2Vec2 """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -79,8 +79,8 @@ class Wav2Vec2FeatureExtractor(SequenceFeatureExtractor): @staticmethod def zero_mean_unit_var_norm( - input_values: List[np.ndarray], attention_mask: List[np.ndarray], padding_value: float = 0.0 - ) -> List[np.ndarray]: + input_values: list[np.ndarray], attention_mask: list[np.ndarray], padding_value: float = 0.0 + ) -> list[np.ndarray]: """ Every array in the list is normalized to have zero mean and unit variance """ @@ -101,7 +101,7 @@ class Wav2Vec2FeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, truncation: bool = False, @@ -115,7 +115,7 @@ class Wav2Vec2FeatureExtractor(SequenceFeatureExtractor): Main method to featurize and prepare for the model one or several sequence(s). Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. diff --git a/src/transformers/models/wav2vec2/modeling_flax_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_flax_wav2vec2.py index b4127201d21..ca5eb700ee8 100644 --- a/src/transformers/models/wav2vec2/modeling_flax_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_flax_wav2vec2.py @@ -15,7 +15,7 @@ """Flax Wav2Vec2 model.""" from functools import partial -from typing import Optional, Tuple, Union +from typing import Optional, Union import flax import flax.linen as nn @@ -67,8 +67,8 @@ class FlaxWav2Vec2BaseModelOutput(ModelOutput): last_hidden_state: jnp.ndarray = None extract_features: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None @flax.struct.dataclass @@ -102,12 +102,12 @@ class FlaxWav2Vec2ForPreTrainingOutput(ModelOutput): projected_states: jnp.ndarray = None projected_quantized_states: jnp.ndarray = None codevector_perplexity: jnp.ndarray = None - hidden_states: Optional[Tuple[jnp.ndarray]] = None - attentions: Optional[Tuple[jnp.ndarray]] = None + hidden_states: Optional[tuple[jnp.ndarray]] = None + attentions: Optional[tuple[jnp.ndarray]] = None def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[np.ndarray] = None, @@ -179,7 +179,7 @@ def _compute_mask_indices( return spec_aug_mask -def _sample_negative_indices(features_shape: Tuple, num_negatives: int, attention_mask: Optional[np.ndarray] = None): +def _sample_negative_indices(features_shape: tuple, num_negatives: int, attention_mask: Optional[np.ndarray] = None): """ Sample `num_negatives` vectors from feature vectors. """ @@ -255,7 +255,7 @@ WAV2VEC2_INPUTS_DOCSTRING = r""" Args: input_values (`jnp.ndarray` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `jnp.ndarray`. See [`Wav2Vec2Processor.__call__`] for details. attention_mask (`jnp.ndarray` of shape `(batch_size, sequence_length)`, *optional*): @@ -474,7 +474,7 @@ class FlaxWav2Vec2Attention(nn.Module): key_value_states: Optional[jnp.ndarray] = None, attention_mask: Optional[jnp.ndarray] = None, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # get query proj @@ -857,7 +857,7 @@ class FlaxWav2Vec2PreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: Wav2Vec2Config, - input_shape: Tuple = (1, 1024), + input_shape: tuple = (1, 1024), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -866,7 +866,7 @@ class FlaxWav2Vec2PreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_values = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_values) diff --git a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py index 5ba5462d71d..657674c3ff9 100644 --- a/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_tf_wav2vec2.py @@ -18,7 +18,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -80,8 +80,8 @@ class TFWav2Vec2BaseModelOutput(ModelOutput): last_hidden_state: Optional[tf.Tensor] = None extract_features: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor] | None = None - attentions: Tuple[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor] | None = None + attentions: tuple[tf.Tensor] | None = None def _sample_without_replacement(distribution, num_samples): @@ -110,7 +110,7 @@ def _scatter_values_on_batch_indices(values, batch_indices, output_shape): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, min_masks: int = 0, @@ -726,11 +726,11 @@ class TFWav2Vec2Attention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -924,7 +924,7 @@ class TFWav2Vec2EncoderLayer(keras.layers.Layer): attention_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attn_residual = hidden_states hidden_states, attn_weights, _ = self.attention( hidden_states, attention_mask=attention_mask, training=training @@ -983,7 +983,7 @@ class TFWav2Vec2EncoderLayerStableLayerNorm(keras.layers.Layer): attention_mask: tf.Tensor | None = None, output_attentions: Optional[bool] = False, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: attn_residual = hidden_states hidden_states = self.layer_norm(hidden_states) hidden_states, attn_weights, _ = self.attention( @@ -1035,7 +1035,7 @@ class TFWav2Vec2Encoder(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1117,7 +1117,7 @@ class TFWav2Vec2EncoderStableLayerNorm(keras.layers.Layer): output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, training: Optional[bool] = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None @@ -1441,7 +1441,7 @@ WAV2VEC2_START_DOCSTRING = r""" WAV2VEC2_INPUTS_DOCSTRING = r""" Args: - input_values (`np.ndarray`, `tf.Tensor`, `List[tf.Tensor]` `Dict[str, tf.Tensor]` or `Dict[str, np.ndarray]` and each example must have the shape `({0})`): + input_values (`np.ndarray`, `tf.Tensor`, `list[tf.Tensor]` `dict[str, tf.Tensor]` or `dict[str, np.ndarray]` and each example must have the shape `({0})`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.__call__`] and @@ -1520,7 +1520,7 @@ class TFWav2Vec2Model(TFWav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: """ Returns: @@ -1627,7 +1627,7 @@ class TFWav2Vec2ForCTC(TFWav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1794,7 +1794,7 @@ class TFWav2Vec2ForSequenceClassification(TFWav2Vec2PreTrainedModel): return_dict: bool | None = None, labels: tf.Tensor | None = None, training: bool = False, - ) -> TFSequenceClassifierOutput | Tuple[tf.Tensor]: + ) -> TFSequenceClassifierOutput | tuple[tf.Tensor]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict output_hidden_states = True if self.config.use_weighted_layer_sum else output_hidden_states diff --git a/src/transformers/models/wav2vec2/modeling_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_wav2vec2.py index ecde2d66e34..9ed86d274e2 100755 --- a/src/transformers/models/wav2vec2/modeling_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_wav2vec2.py @@ -17,7 +17,7 @@ import math import warnings from dataclasses import dataclass -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -110,14 +110,14 @@ class Wav2Vec2ForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None contrastive_loss: Optional[torch.FloatTensor] = None diversity_loss: Optional[torch.FloatTensor] = None def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -236,7 +236,7 @@ def _compute_mask_indices( def _sample_negative_indices( - features_shape: Tuple, num_negatives: int, mask_time_indices: Optional[np.ndarray] = None + features_shape: tuple, num_negatives: int, mask_time_indices: Optional[np.ndarray] = None ): """ Sample `num_negatives` vectors from feature vectors. @@ -540,14 +540,14 @@ class Wav2Vec2Attention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -1228,7 +1228,7 @@ class Wav2Vec2PreTrainedModel(PreTrainedModel): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. local_files_only(`bool`, *optional*, defaults to `False`): @@ -1500,7 +1500,7 @@ class Wav2Vec2Model(Wav2Vec2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2BaseModelOutput]: + ) -> Union[tuple, Wav2Vec2BaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1625,7 +1625,7 @@ class Wav2Vec2ForPreTraining(Wav2Vec2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2ForPreTrainingOutput]: + ) -> Union[tuple, Wav2Vec2ForPreTrainingOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1800,7 +1800,7 @@ class Wav2Vec2ForMaskedLM(Wav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.wav2vec2( @@ -1913,7 +1913,7 @@ class Wav2Vec2ForCTC(Wav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -2036,11 +2036,11 @@ class Wav2Vec2ForSequenceClassification(Wav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2Processor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -2150,11 +2150,11 @@ class Wav2Vec2ForAudioFrameClassification(Wav2Vec2PreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2Processor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -2332,11 +2332,11 @@ class Wav2Vec2ForXVector(Wav2Vec2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2Processor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/wav2vec2/processing_wav2vec2.py b/src/transformers/models/wav2vec2/processing_wav2vec2.py index 1df8ff89236..1a01f582c2c 100644 --- a/src/transformers/models/wav2vec2/processing_wav2vec2.py +++ b/src/transformers/models/wav2vec2/processing_wav2vec2.py @@ -18,7 +18,7 @@ Speech processor class for Wav2Vec2 import warnings from contextlib import contextmanager -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import AudioInput, PreTokenizedInput, TextInput @@ -75,7 +75,7 @@ class Wav2Vec2Processor(ProcessorMixin): def __call__( self, audio: AudioInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, images=None, videos=None, **kwargs: Unpack[Wav2Vec2ProcessorKwargs], diff --git a/src/transformers/models/wav2vec2/tokenization_wav2vec2.py b/src/transformers/models/wav2vec2/tokenization_wav2vec2.py index 1f468150775..aebb4f350e1 100644 --- a/src/transformers/models/wav2vec2/tokenization_wav2vec2.py +++ b/src/transformers/models/wav2vec2/tokenization_wav2vec2.py @@ -19,7 +19,7 @@ import os import warnings from dataclasses import dataclass from itertools import groupby -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -87,7 +87,7 @@ WAV2VEC2_KWARGS_DOCSTRING = r""" Whether or not to print more information and warnings. """ -ListOfDict = List[Dict[str, Union[int, str]]] +ListOfDict = list[dict[str, Union[int, str]]] @dataclass @@ -98,18 +98,18 @@ class Wav2Vec2CTCTokenizerOutput(ModelOutput): Args: text (list of `str` or `str`): Decoded logits in text from. Usually the speech transcription. - char_offsets (list of `List[Dict[str, Union[int, str]]]` or `List[Dict[str, Union[int, str]]]`): + char_offsets (list of `list[dict[str, Union[int, str]]]` or `list[dict[str, Union[int, str]]]`): Offsets of the decoded characters. In combination with sampling rate and model downsampling rate char offsets can be used to compute time stamps for each character. Total logit score of the beam associated with produced text. - word_offsets (list of `List[Dict[str, Union[int, str]]]` or `List[Dict[str, Union[int, str]]]`): + word_offsets (list of `list[dict[str, Union[int, str]]]` or `list[dict[str, Union[int, str]]]`): Offsets of the decoded words. In combination with sampling rate and model downsampling rate word offsets can be used to compute time stamps for each word. """ - text: Union[List[str], str] - char_offsets: Union[List[ListOfDict], ListOfDict] = None - word_offsets: Union[List[ListOfDict], ListOfDict] = None + text: Union[list[str], str] + char_offsets: Union[list[ListOfDict], ListOfDict] = None + word_offsets: Union[list[ListOfDict], ListOfDict] = None class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): @@ -248,12 +248,12 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.decoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: vocab = dict(self.encoder) vocab.update(self.added_tokens_encoder) return vocab - def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int: + def _add_tokens(self, new_tokens: Union[list[str], list[AddedToken]], special_tokens: bool = False) -> int: # Overwritten to never strip! to_add = [] for token in new_tokens: @@ -284,12 +284,12 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): def convert_tokens_to_string( self, - tokens: List[str], + tokens: list[str], group_tokens: bool = True, spaces_between_special_tokens: bool = False, output_char_offsets: bool = False, output_word_offsets: bool = False, - ) -> Dict[str, Union[str, float]]: + ) -> dict[str, Union[str, float]]: """ Converts a connectionist-temporal-classification (CTC) output tokens into a single string. """ @@ -347,8 +347,8 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): @staticmethod def _compute_offsets( - char_repetitions: List[int], chars: List[str], ctc_token: int - ) -> List[Dict[str, Union[str, int]]]: + char_repetitions: list[int], chars: list[str], ctc_token: int + ) -> list[dict[str, Union[str, int]]]: end_indices = np.asarray(char_repetitions).cumsum() start_indices = np.concatenate(([0], end_indices[:-1])) @@ -362,8 +362,8 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): @staticmethod def _get_word_offsets( - offsets: Dict[str, Union[str, float]], word_delimiter_char: str = " " - ) -> Dict[str, Union[str, float]]: + offsets: dict[str, Union[str, float]], word_delimiter_char: str = " " + ) -> dict[str, Union[str, float]]: word_offsets = [] last_state = "SPACE" @@ -402,7 +402,7 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, group_tokens: bool = True, @@ -457,18 +457,18 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): # because we need docs for `output_char_offsets` here def batch_decode( self, - sequences: Union[List[int], List[List[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], + sequences: Union[list[int], list[list[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, output_char_offsets: bool = False, output_word_offsets: bool = False, **kwargs, - ) -> List[str]: + ) -> list[str]: """ Convert a list of lists of token ids into a list of strings by calling decode. Args: - sequences (`Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]`): + sequences (`Union[list[int], list[list[int]], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -502,7 +502,7 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): Will be passed to the underlying model specific decode method. Returns: - `List[str]` or [`~models.wav2vec2.tokenization_wav2vec2.Wav2Vec2CTCTokenizerOutput`]: The list of decoded + `list[str]` or [`~models.wav2vec2.tokenization_wav2vec2.Wav2Vec2CTCTokenizerOutput`]: The list of decoded sentences. Will be a [`~models.wav2vec2.tokenization_wav2vec2.Wav2Vec2CTCTokenizerOutput`] when `output_char_offsets == True` or `output_word_offsets == True`. """ @@ -527,7 +527,7 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): # and `output_word_offsets` here def decode( self, - token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], + token_ids: Union[int, list[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, output_char_offsets: bool = False, @@ -541,7 +541,7 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -630,7 +630,7 @@ class Wav2Vec2CTCTokenizer(PreTrainedTokenizer): **kwargs, ) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -777,7 +777,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): @add_end_docstrings(WAV2VEC2_KWARGS_DOCSTRING) def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], padding: Union[bool, str, PaddingStrategy] = False, max_length: Optional[int] = None, pad_to_multiple_of: Optional[int] = None, @@ -791,7 +791,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): sequences. Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy array or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -842,7 +842,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.decoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: return dict(self.encoder, **self.added_tokens_encoder) def _convert_token_to_id(self, token: str) -> int: @@ -854,7 +854,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): result = self.decoder.get(index, self.unk_token) return result - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """ Converts a connectionist-temporal-classification (CTC) output tokens into a single string. """ @@ -874,7 +874,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, **kwargs, @@ -907,7 +907,7 @@ class Wav2Vec2Tokenizer(PreTrainedTokenizer): else: return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/wav2vec2_bert/configuration_wav2vec2_bert.py b/src/transformers/models/wav2vec2_bert/configuration_wav2vec2_bert.py index 62799ef0550..2462976cfbb 100644 --- a/src/transformers/models/wav2vec2_bert/configuration_wav2vec2_bert.py +++ b/src/transformers/models/wav2vec2_bert/configuration_wav2vec2_bert.py @@ -110,13 +110,13 @@ class Wav2Vec2BertConfig(PretrainedConfig): instance of [`Wav2Vec2BertForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 768): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/wav2vec2_bert/modeling_wav2vec2_bert.py b/src/transformers/models/wav2vec2_bert/modeling_wav2vec2_bert.py index f7c666b9d33..b8a60f3d3d0 100644 --- a/src/transformers/models/wav2vec2_bert/modeling_wav2vec2_bert.py +++ b/src/transformers/models/wav2vec2_bert/modeling_wav2vec2_bert.py @@ -6,7 +6,7 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -265,7 +265,7 @@ class Wav2Vec2BertSelfAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, relative_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() @@ -804,7 +804,7 @@ class Wav2Vec2BertPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1002,11 +1002,11 @@ class Wav2Vec2BertModel(Wav2Vec2BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2BertBaseModelOutput]: + ) -> Union[tuple, Wav2Vec2BertBaseModelOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -1099,11 +1099,11 @@ class Wav2Vec2BertForCTC(Wav2Vec2BertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): @@ -1210,11 +1210,11 @@ class Wav2Vec2BertForSequenceClassification(Wav2Vec2BertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1305,11 +1305,11 @@ class Wav2Vec2BertForAudioFrameClassification(Wav2Vec2BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1468,11 +1468,11 @@ class Wav2Vec2BertForXVector(Wav2Vec2BertPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/wav2vec2_bert/modular_wav2vec2_bert.py b/src/transformers/models/wav2vec2_bert/modular_wav2vec2_bert.py index 6dd421ff856..3427a01808a 100644 --- a/src/transformers/models/wav2vec2_bert/modular_wav2vec2_bert.py +++ b/src/transformers/models/wav2vec2_bert/modular_wav2vec2_bert.py @@ -1,5 +1,5 @@ import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -221,7 +221,7 @@ class Wav2Vec2BertSelfAttention(Wav2Vec2ConformerSelfAttention, nn.Module): attention_mask: Optional[torch.Tensor] = None, relative_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() @@ -713,11 +713,11 @@ class Wav2Vec2BertModel(Wav2Vec2Model, Wav2Vec2BertPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2BertBaseModelOutput]: + ) -> Union[tuple, Wav2Vec2BertBaseModelOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): @@ -784,11 +784,11 @@ class Wav2Vec2BertForCTC(Wav2Vec2ConformerForCTC): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): @@ -880,11 +880,11 @@ class Wav2Vec2BertForSequenceClassification(Wav2Vec2ForSequenceClassification): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2BertProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -955,11 +955,11 @@ class Wav2Vec2BertForAudioFrameClassification(Wav2Vec2ConformerForAudioFrameClas output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1021,11 +1021,11 @@ class Wav2Vec2BertForXVector(Wav2Vec2ConformerForXVector): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/wav2vec2_bert/processing_wav2vec2_bert.py b/src/transformers/models/wav2vec2_bert/processing_wav2vec2_bert.py index dbd40c94ea5..4cf34a4a3f7 100644 --- a/src/transformers/models/wav2vec2_bert/processing_wav2vec2_bert.py +++ b/src/transformers/models/wav2vec2_bert/processing_wav2vec2_bert.py @@ -17,7 +17,7 @@ Speech processor class for Wav2Vec2-BERT """ import warnings -from typing import List, Optional, Union +from typing import Optional, Union from ...processing_utils import ProcessingKwargs, ProcessorMixin, Unpack from ...tokenization_utils_base import AudioInput, PreTokenizedInput, TextInput @@ -72,7 +72,7 @@ class Wav2Vec2BertProcessor(ProcessorMixin): def __call__( self, audio: AudioInput = None, - text: Optional[Union[str, List[str], TextInput, PreTokenizedInput]] = None, + text: Optional[Union[str, list[str], TextInput, PreTokenizedInput]] = None, images=None, videos=None, **kwargs: Unpack[Wav2Vec2BertProcessorKwargs], @@ -84,12 +84,12 @@ class Wav2Vec2BertProcessor(ProcessorMixin): PreTrainedTokenizer's [`~PreTrainedTokenizer.__call__`] if `text` is not `None`. Please refer to the docstring of the above two methods for more information. Args: - audio (`np.ndarray`, `torch.Tensor`, `List[np.ndarray]`, `List[torch.Tensor]`): + audio (`np.ndarray`, `torch.Tensor`, `list[np.ndarray]`, `list[torch.Tensor]`): The audio or batch of audios to be prepared. Each audio can be NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each audio should be of shape (C, T), where C is a number of channels, and T the sample length of the audio. - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). diff --git a/src/transformers/models/wav2vec2_conformer/configuration_wav2vec2_conformer.py b/src/transformers/models/wav2vec2_conformer/configuration_wav2vec2_conformer.py index b8a894e8764..493e96f4403 100644 --- a/src/transformers/models/wav2vec2_conformer/configuration_wav2vec2_conformer.py +++ b/src/transformers/models/wav2vec2_conformer/configuration_wav2vec2_conformer.py @@ -79,13 +79,13 @@ class Wav2Vec2ConformerConfig(PretrainedConfig): extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. feat_quantizer_dropout (`float`, *optional*, defaults to 0.0): The dropout probability for quantized feature encoder states. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -153,13 +153,13 @@ class Wav2Vec2ConformerConfig(PretrainedConfig): instance of [`Wav2Vec2ConformerForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 256): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py b/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py index f481a02c7f2..70042f7b93b 100644 --- a/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py +++ b/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py @@ -7,7 +7,7 @@ import math import warnings from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -70,8 +70,8 @@ class Wav2Vec2ConformerForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None contrastive_loss: Optional[torch.FloatTensor] = None diversity_loss: Optional[torch.FloatTensor] = None @@ -467,7 +467,7 @@ class Wav2Vec2ConformerSelfAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, relative_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() @@ -966,7 +966,7 @@ class Wav2Vec2ConformerPreTrainedModel(PreTrainedModel): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1168,7 +1168,7 @@ class Wav2Vec2ConformerModel(Wav2Vec2ConformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2ConformerBaseModelOutput]: + ) -> Union[tuple, Wav2Vec2ConformerBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1281,7 +1281,7 @@ class Wav2Vec2ConformerForPreTraining(Wav2Vec2ConformerPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, Wav2Vec2ConformerForPreTrainingOutput]: + ) -> Union[tuple, Wav2Vec2ConformerForPreTrainingOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1485,7 +1485,7 @@ class Wav2Vec2ConformerForCTC(Wav2Vec2ConformerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1596,11 +1596,11 @@ class Wav2Vec2ConformerForSequenceClassification(Wav2Vec2ConformerPreTrainedMode output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1698,11 +1698,11 @@ class Wav2Vec2ConformerForAudioFrameClassification(Wav2Vec2ConformerPreTrainedMo output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1868,11 +1868,11 @@ class Wav2Vec2ConformerForXVector(Wav2Vec2ConformerPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`Wav2Vec2ConformerProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/wav2vec2_conformer/modular_wav2vec2_conformer.py b/src/transformers/models/wav2vec2_conformer/modular_wav2vec2_conformer.py index 4b066b09a74..fc3444a545f 100644 --- a/src/transformers/models/wav2vec2_conformer/modular_wav2vec2_conformer.py +++ b/src/transformers/models/wav2vec2_conformer/modular_wav2vec2_conformer.py @@ -1,6 +1,6 @@ import math from dataclasses import dataclass -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch from torch import nn @@ -70,8 +70,8 @@ class Wav2Vec2ConformerForPreTrainingOutput(ModelOutput): projected_states: Optional[torch.FloatTensor] = None projected_quantized_states: Optional[torch.FloatTensor] = None codevector_perplexity: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None contrastive_loss: Optional[torch.FloatTensor] = None diversity_loss: Optional[torch.FloatTensor] = None @@ -269,7 +269,7 @@ class Wav2Vec2ConformerSelfAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, relative_position_embeddings: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: # self-attention mechanism batch_size, sequence_length, hidden_size = hidden_states.size() diff --git a/src/transformers/models/wav2vec2_phoneme/tokenization_wav2vec2_phoneme.py b/src/transformers/models/wav2vec2_phoneme/tokenization_wav2vec2_phoneme.py index 9f60bf346a3..0715e3ce60f 100644 --- a/src/transformers/models/wav2vec2_phoneme/tokenization_wav2vec2_phoneme.py +++ b/src/transformers/models/wav2vec2_phoneme/tokenization_wav2vec2_phoneme.py @@ -18,7 +18,7 @@ import json import os from dataclasses import dataclass from itertools import groupby -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Optional, Union import numpy as np @@ -56,7 +56,7 @@ VOCAB_FILES_NAMES = { # Wav2Vec2Phoneme has no max input length -ListOfDict = List[Dict[str, Union[int, str]]] +ListOfDict = list[dict[str, Union[int, str]]] @dataclass @@ -67,14 +67,14 @@ class Wav2Vec2PhonemeCTCTokenizerOutput(ModelOutput): Args: text (list of `str` or `str`): Decoded logits in text from. Usually the speech transcription. - char_offsets (list of `List[Dict[str, Union[int, str]]]` or `List[Dict[str, Union[int, str]]]`): + char_offsets (list of `list[dict[str, Union[int, str]]]` or `list[dict[str, Union[int, str]]]`): Offsets of the decoded characters. In combination with sampling rate and model downsampling rate char offsets can be used to compute time stamps for each character. Total logit score of the beam associated with produced text. """ - text: Union[List[str], str] - char_offsets: Union[List[ListOfDict], ListOfDict] = None + text: Union[list[str], str] + char_offsets: Union[list[ListOfDict], ListOfDict] = None class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): @@ -156,12 +156,12 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): def vocab_size(self) -> int: return len(self.decoder) - def get_vocab(self) -> Dict: + def get_vocab(self) -> dict: vocab = dict(self.encoder.copy()) vocab.update(self.added_tokens_encoder) return vocab - def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int: + def _add_tokens(self, new_tokens: Union[list[str], list[AddedToken]], special_tokens: bool = False) -> int: # Overwritten to never strip! to_add = [] for token in new_tokens: @@ -190,7 +190,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): is_split_into_words: bool = False, phonemizer_lang: Optional[str] = None, do_phonemize: Optional[bool] = None, - ) -> Tuple[str, Dict[str, Any]]: + ) -> tuple[str, dict[str, Any]]: """ Performs any necessary transformations before tokenization. @@ -212,7 +212,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): Returns: - `Tuple[str, Dict[str, Any]]`: The prepared text and the unused kwargs. + `tuple[str, dict[str, Any]]`: The prepared text and the unused kwargs. """ if is_split_into_words: text = " " + text @@ -336,7 +336,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): def convert_tokens_to_string( self, - tokens: List[str], + tokens: list[str], group_tokens: bool = True, spaces_between_special_tokens: bool = False, filter_word_delimiter_token: bool = True, @@ -386,8 +386,8 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): @staticmethod def _compute_offsets( - char_repetitions: List[int], chars: List[str], ctc_token: int, word_delimiter_token: Optional[int] = None - ) -> List[Dict[str, Union[str, int]]]: + char_repetitions: list[int], chars: list[str], ctc_token: int, word_delimiter_token: Optional[int] = None + ) -> list[dict[str, Union[str, int]]]: end_indices = np.asarray(char_repetitions).cumsum() start_indices = np.concatenate(([0], end_indices[:-1])) @@ -406,7 +406,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, group_tokens: bool = True, @@ -453,7 +453,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): # overwritten from `tokenization_utils_base.py` because we need docs for `output_char_offsets` here def decode( self, - token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], + token_ids: Union[int, list[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, output_char_offsets: bool = False, @@ -466,7 +466,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -509,17 +509,17 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): # we need docs for `output_char_offsets` here def batch_decode( self, - sequences: Union[List[int], List[List[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], + sequences: Union[list[int], list[list[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, output_char_offsets: bool = False, **kwargs, - ) -> List[str]: + ) -> list[str]: """ Convert a list of lists of token ids into a list of strings by calling decode. Args: - sequences (`Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]`): + sequences (`Union[list[int], list[list[int]], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -542,7 +542,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): Will be passed to the underlying model specific decode method. Returns: - `List[str]` or [`~models.wav2vec2.tokenization_wav2vec2_phoneme.Wav2Vec2PhonemeCTCTokenizerOutput`]: The + `list[str]` or [`~models.wav2vec2.tokenization_wav2vec2_phoneme.Wav2Vec2PhonemeCTCTokenizerOutput`]: The decoded sentence. Will be a [`~models.wav2vec2.tokenization_wav2vec2_phoneme.Wav2Vec2PhonemeCTCTokenizerOutput`] when `output_char_offsets == True`. @@ -563,7 +563,7 @@ class Wav2Vec2PhonemeCTCTokenizer(PreTrainedTokenizer): return batch_decoded - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py b/src/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py index 5d7685163ac..46cc2211b8c 100644 --- a/src/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py +++ b/src/transformers/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.py @@ -22,7 +22,7 @@ from collections.abc import Iterable from contextlib import contextmanager, nullcontext from dataclasses import dataclass from multiprocessing import Pool, get_context, get_start_method -from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -40,7 +40,7 @@ if TYPE_CHECKING: from ...tokenization_utils import PreTrainedTokenizerBase -ListOfDict = List[Dict[str, Union[int, str]]] +ListOfDict = list[dict[str, Union[int, str]]] @dataclass @@ -55,15 +55,15 @@ class Wav2Vec2DecoderWithLMOutput(ModelOutput): Total logit score of the beams associated with produced text. lm_score (list of `float`): Fused lm_score of the beams associated with produced text. - word_offsets (list of `List[Dict[str, Union[int, str]]]` or `List[Dict[str, Union[int, str]]]`): + word_offsets (list of `list[dict[str, Union[int, str]]]` or `list[dict[str, Union[int, str]]]`): Offsets of the decoded words. In combination with sampling rate and model downsampling rate word offsets can be used to compute time stamps for each word. """ - text: Union[List[List[str]], List[str], str] - logit_score: Union[List[List[float]], List[float], float] = None - lm_score: Union[List[List[float]], List[float], float] = None - word_offsets: Union[List[List[ListOfDict]], List[ListOfDict], ListOfDict] = None + text: Union[list[list[str]], list[str], str] + logit_score: Union[list[list[float]], list[float], float] = None + lm_score: Union[list[list[float]], list[float], float] = None + word_offsets: Union[list[list[ListOfDict]], list[ListOfDict], ListOfDict] = None class Wav2Vec2ProcessorWithLM(ProcessorMixin): @@ -344,7 +344,7 @@ class Wav2Vec2ProcessorWithLM(ProcessorMixin): token_min_logp (`int`, *optional*): Tokens below this logp are skipped unless they are argmax of frame Defaults to pyctcdecode's DEFAULT_MIN_TOKEN_LOGP. - hotwords (`List[str]`, *optional*): + hotwords (`list[str]`, *optional*): List of words with extra importance, can be OOV for LM hotword_weight (`int`, *optional*): Weight factor for hotword importance Defaults to pyctcdecode's DEFAULT_HOTWORD_WEIGHT. @@ -503,7 +503,7 @@ class Wav2Vec2ProcessorWithLM(ProcessorMixin): token_min_logp (`int`, *optional*): Tokens with log-probs below token_min_logp are skipped unless they are have the maximum log-prob for an utterance. Defaults to pyctcdecode's DEFAULT_MIN_TOKEN_LOGP. - hotwords (`List[str]`, *optional*): + hotwords (`list[str]`, *optional*): List of words with extra importance which can be missing from the LM's vocabulary, e.g. ["huggingface"] hotword_weight (`int`, *optional*): Weight multiplier that boosts hotword scores. Defaults to pyctcdecode's DEFAULT_HOTWORD_WEIGHT. diff --git a/src/transformers/models/wavlm/configuration_wavlm.py b/src/transformers/models/wavlm/configuration_wavlm.py index 62f9ae92d2b..a137792e2a8 100644 --- a/src/transformers/models/wavlm/configuration_wavlm.py +++ b/src/transformers/models/wavlm/configuration_wavlm.py @@ -75,13 +75,13 @@ class WavLMConfig(PretrainedConfig): feat_extract_activation (`str, `optional`, defaults to `"gelu"`): The non-linear activation function (function or string) in the 1D convolutional layers of the feature extractor. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported. - conv_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): + conv_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 512, 512, 512)`): A tuple of integers defining the number of input and output channels of each 1D convolutional layer in the feature encoder. The length of *conv_dim* defines the number of 1D convolutional layers. - conv_stride (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): + conv_stride (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 2, 2, 2, 2, 2, 2)`): A tuple of integers defining the stride of each 1D convolutional layer in the feature encoder. The length of *conv_stride* defines the number of convolutional layers and has to match the length of *conv_dim*. - conv_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): + conv_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(10, 3, 3, 3, 3, 3, 3)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the feature encoder. The length of *conv_kernel* defines the number of convolutional layers and has to match the length of *conv_dim*. @@ -142,13 +142,13 @@ class WavLMConfig(PretrainedConfig): instance of [`WavLMForSequenceClassification`]. classifier_proj_size (`int`, *optional*, defaults to 256): Dimensionality of the projection before token mean-pooling for classification. - tdnn_dim (`Tuple[int]` or `List[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): + tdnn_dim (`tuple[int]` or `list[int]`, *optional*, defaults to `(512, 512, 512, 512, 1500)`): A tuple of integers defining the number of output channels of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_dim* defines the number of *TDNN* layers. - tdnn_kernel (`Tuple[int]` or `List[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): + tdnn_kernel (`tuple[int]` or `list[int]`, *optional*, defaults to `(5, 3, 3, 1, 1)`): A tuple of integers defining the kernel size of each 1D convolutional layer in the *TDNN* module of the *XVector* model. The length of *tdnn_kernel* has to match the length of *tdnn_dim*. - tdnn_dilation (`Tuple[int]` or `List[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): + tdnn_dilation (`tuple[int]` or `list[int]`, *optional*, defaults to `(1, 2, 3, 1, 1)`): A tuple of integers defining the dilation factor of each 1D convolutional layer in *TDNN* module of the *XVector* model. The length of *tdnn_dilation* has to match the length of *tdnn_dim*. xvector_output_dim (`int`, *optional*, defaults to 512): diff --git a/src/transformers/models/wavlm/modeling_wavlm.py b/src/transformers/models/wavlm/modeling_wavlm.py index 07e6cb4efc5..d718d4958a1 100755 --- a/src/transformers/models/wavlm/modeling_wavlm.py +++ b/src/transformers/models/wavlm/modeling_wavlm.py @@ -6,7 +6,7 @@ # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import torch @@ -150,7 +150,7 @@ class WavLMAttention(nn.Module): position_bias: Optional[torch.Tensor] = None, output_attentions: bool = False, index=0, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Attention layer with relative attention""" bsz, tgt_len, _ = hidden_states.size() @@ -862,7 +862,7 @@ class WavLMAdapter(nn.Module): def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -1079,7 +1079,7 @@ class WavLMModel(WavLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, WavLMBaseModelOutput]: + ) -> Union[tuple, WavLMBaseModelOutput]: r""" mask_time_indices (`torch.BoolTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices to mask extracted features for contrastive loss. When in training mode, model learns to predict @@ -1224,7 +1224,7 @@ class WavLMForCTC(WavLMPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, CausalLMOutput]: + ) -> Union[tuple, CausalLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, target_length)`, *optional*): Labels for connectionist temporal classification. Note that `target_length` has to be smaller or equal to @@ -1347,11 +1347,11 @@ class WavLMForSequenceClassification(WavLMPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`WavLMProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1461,11 +1461,11 @@ class WavLMForAudioFrameClassification(WavLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`WavLMProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): @@ -1643,11 +1643,11 @@ class WavLMForXVector(WavLMPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: Optional[torch.Tensor] = None, - ) -> Union[Tuple, XVectorOutput]: + ) -> Union[tuple, XVectorOutput]: r""" input_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)`): Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`WavLMProcessor.__call__`] for details. labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): diff --git a/src/transformers/models/wavlm/modular_wavlm.py b/src/transformers/models/wavlm/modular_wavlm.py index 7a8464449c2..53d29edc0e7 100644 --- a/src/transformers/models/wavlm/modular_wavlm.py +++ b/src/transformers/models/wavlm/modular_wavlm.py @@ -1,5 +1,5 @@ import math -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.nn as nn @@ -81,7 +81,7 @@ class WavLMAttention(nn.Module): position_bias: Optional[torch.Tensor] = None, output_attentions: bool = False, index=0, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Attention layer with relative attention""" bsz, tgt_len, _ = hidden_states.size() diff --git a/src/transformers/models/whisper/configuration_whisper.py b/src/transformers/models/whisper/configuration_whisper.py index bc15e925660..a8837cb1cdd 100644 --- a/src/transformers/models/whisper/configuration_whisper.py +++ b/src/transformers/models/whisper/configuration_whisper.py @@ -127,11 +127,11 @@ class WhisperConfig(PretrainedConfig): Begin of stream token id. eos_token_id (`int`, *optional*, defaults to 50256): End of stream token id. - suppress_tokens (`List[int]`, *optional*): + suppress_tokens (`list[int]`, *optional*): A list containing the non-speech tokens that will be used by the logit processor in the `generate` function. NON_SPEECH_TOKENS and NON_SPEECH_TOKENS_MULTI each correspond to the `english-only` and the `multilingual` model. - begin_suppress_tokens (`List[int]`, *optional*, defaults to `[220,50256]`): + begin_suppress_tokens (`list[int]`, *optional*, defaults to `[220,50256]`): A list containing tokens that will be suppressed at the beginning of the sampling process. Initialized as the token for `" "` (`blank_token_id`) and the `eos_token_id` use_weighted_layer_sum (`bool`, *optional*, defaults to `False`): diff --git a/src/transformers/models/whisper/convert_openai_to_hf.py b/src/transformers/models/whisper/convert_openai_to_hf.py index 343fb5513b5..5684154717a 100755 --- a/src/transformers/models/whisper/convert_openai_to_hf.py +++ b/src/transformers/models/whisper/convert_openai_to_hf.py @@ -21,7 +21,7 @@ import os import tempfile import urllib import warnings -from typing import Any, List, Optional, Tuple +from typing import Any, Optional import torch from huggingface_hub.utils import insecure_hashlib @@ -184,7 +184,7 @@ def _download(url: str, root: str) -> Any: def convert_openai_whisper_to_tfms( checkpoint_path, pytorch_dump_folder_path -) -> Tuple[WhisperForConditionalGeneration, bool, int]: +) -> tuple[WhisperForConditionalGeneration, bool, int]: if ".pt" not in checkpoint_path: root = os.path.dirname(pytorch_dump_folder_path) or "." original_checkpoint = _download(_MODELS[checkpoint_path], root) @@ -252,7 +252,7 @@ def convert_openai_whisper_to_tfms( # Adapted from https://github.com/openai/tiktoken/issues/60#issuecomment-1499977960 -def _bpe(mergeable_ranks, token: bytes, max_rank=None) -> List[bytes]: +def _bpe(mergeable_ranks, token: bytes, max_rank=None) -> list[bytes]: parts = [bytes([b]) for b in token] while True: min_idx = None diff --git a/src/transformers/models/whisper/english_normalizer.py b/src/transformers/models/whisper/english_normalizer.py index e2b9ac65649..265ea04b533 100644 --- a/src/transformers/models/whisper/english_normalizer.py +++ b/src/transformers/models/whisper/english_normalizer.py @@ -18,7 +18,7 @@ import unicodedata from collections.abc import Iterator from fractions import Fraction from re import Match -from typing import List, Optional, Union +from typing import Optional, Union import regex @@ -210,7 +210,7 @@ class EnglishNumberNormalizer: } self.literal_words = {"one", "ones"} - def process_words(self, words: List[str]) -> Iterator[str]: + def process_words(self, words: list[str]) -> Iterator[str]: prefix: Optional[str] = None value: Optional[Union[str, int]] = None skip = False diff --git a/src/transformers/models/whisper/feature_extraction_whisper.py b/src/transformers/models/whisper/feature_extraction_whisper.py index 575bfb1c54f..00726d82cce 100644 --- a/src/transformers/models/whisper/feature_extraction_whisper.py +++ b/src/transformers/models/whisper/feature_extraction_whisper.py @@ -16,7 +16,7 @@ Feature extractor class for Whisper """ -from typing import List, Optional, Union +from typing import Optional, Union import numpy as np @@ -169,8 +169,8 @@ class WhisperFeatureExtractor(SequenceFeatureExtractor): @staticmethod # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm def zero_mean_unit_var_norm( - input_values: List[np.ndarray], attention_mask: List[np.ndarray], padding_value: float = 0.0 - ) -> List[np.ndarray]: + input_values: list[np.ndarray], attention_mask: list[np.ndarray], padding_value: float = 0.0 + ) -> list[np.ndarray]: """ Every array in the list is normalized to have zero mean and unit variance """ @@ -191,7 +191,7 @@ class WhisperFeatureExtractor(SequenceFeatureExtractor): def __call__( self, - raw_speech: Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]], + raw_speech: Union[np.ndarray, list[float], list[np.ndarray], list[list[float]]], truncation: bool = True, pad_to_multiple_of: Optional[int] = None, return_tensors: Optional[Union[str, TensorType]] = None, @@ -209,7 +209,7 @@ class WhisperFeatureExtractor(SequenceFeatureExtractor): the STFT computation if available, otherwise a slower NumPy based one. Args: - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -316,7 +316,7 @@ class WhisperFeatureExtractor(SequenceFeatureExtractor): ) input_features = extract_fbank_features(input_features[0], device) - if isinstance(input_features[0], List): + if isinstance(input_features[0], list): padded_inputs["input_features"] = [np.asarray(feature, dtype=np.float32) for feature in input_features] else: diff --git a/src/transformers/models/whisper/generation_whisper.py b/src/transformers/models/whisper/generation_whisper.py index cdcf0d8ac08..0a1d280a725 100644 --- a/src/transformers/models/whisper/generation_whisper.py +++ b/src/transformers/models/whisper/generation_whisper.py @@ -17,7 +17,7 @@ import math import warnings import zlib from collections.abc import Iterator -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -349,16 +349,16 @@ class WhisperGenerationMixin(GenerationMixin): generation_config: Optional[GenerationConfig] = None, logits_processor: Optional[LogitsProcessorList] = None, stopping_criteria: Optional[StoppingCriteriaList] = None, - prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None, + prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], list[int]]] = None, synced_gpus: bool = False, return_timestamps: Optional[bool] = None, task: Optional[str] = None, - language: Optional[Union[str, List[str]]] = None, + language: Optional[Union[str, list[str]]] = None, is_multilingual: Optional[bool] = None, prompt_ids: Optional[torch.Tensor] = None, prompt_condition_type: Optional[str] = None, # first-segment, all-segments condition_on_prev_tokens: Optional[bool] = None, - temperature: Optional[Union[float, Tuple[float, ...]]] = None, + temperature: Optional[Union[float, tuple[float, ...]]] = None, compression_ratio_threshold: Optional[float] = None, logprob_threshold: Optional[float] = None, no_speech_threshold: Optional[float] = None, @@ -389,7 +389,7 @@ class WhisperGenerationMixin(GenerationMixin): Parameters: input_features (`torch.Tensor` of shape `(batch_size, feature_size, sequence_length)`, *optional*): Float values of log-mel features extracted from the raw speech waveform. The raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] for details. @@ -408,7 +408,7 @@ class WhisperGenerationMixin(GenerationMixin): Custom stopping criteria that complement the default stopping criteria built from arguments and a generation config. If a stopping criteria is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], List[int]]`, *optional*): + prefix_allowed_tokens_fn (`Callable[[int, torch.Tensor], list[int]]`, *optional*): If provided, this function constraints the beam search to allowed tokens only at each step. If not provided no constraint is applied. This function takes 2 arguments: the batch ID `batch_id` and `input_ids`. It has to return a list with the allowed tokens for the next generation step conditioned @@ -492,16 +492,16 @@ class WhisperGenerationMixin(GenerationMixin): force_unique_generate_call (`bool`, *optional*): Whether to force a unique call to the underlying GenerationMixin's [`~generation.GenerationMixin.generate`] method. This is useful for assisted decoding and testing purposes to ensure that only one call to [`~generation.GenerationMixin.generate`] is made and therefore decoder input token ids and eos token ids are returned. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. Return: - [`~utils.ModelOutput`] or `Dict[str, Any]` or `torch.LongTensor`: + [`~utils.ModelOutput`] or `dict[str, Any]` or `torch.LongTensor`: A: - [`~utils.ModelOutput`] when `return_dict_in_generate=True` and (`return_timestamps=False` or `force_unique_generate_call=True`), including the decoder input ids and end of sequence id. - - `Dict[str, Any]` when (`return_dict_in_generate=True` and `return_timestamps=True`) or `return_segments=True` or `return_token_timestamps=True`. + - `dict[str, Any]` when (`return_dict_in_generate=True` and `return_timestamps=True`) or `return_segments=True` or `return_token_timestamps=True`. - `torch.LongTensor` in all other cases, excluding the decoder input ids and end of sequence id. The possible [`~utils.ModelOutput`] types are: @@ -1384,7 +1384,7 @@ class WhisperGenerationMixin(GenerationMixin): generation_config.task = task def _retrieve_init_tokens(self, input_features, batch_size, generation_config, config, num_segment_frames, kwargs): - def replace_or_add(lst: List[int], num: int, itr: Iterator[int]): + def replace_or_add(lst: list[int], num: int, itr: Iterator[int]): """short function to replace num with a itr in lst""" found = any(i in lst for i in itr) if found: @@ -1551,7 +1551,7 @@ class WhisperGenerationMixin(GenerationMixin): Parameters: input_features (`torch.Tensor` of shape `(batch_size, feature_size, sequence_length)`, *optional*): Float values of log-mel features extracted from the raw speech waveform. The raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] for details. diff --git a/src/transformers/models/whisper/modeling_flax_whisper.py b/src/transformers/models/whisper/modeling_flax_whisper.py index 97891615c3f..8cb98f2385d 100644 --- a/src/transformers/models/whisper/modeling_flax_whisper.py +++ b/src/transformers/models/whisper/modeling_flax_whisper.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -101,7 +101,7 @@ WHISPER_INPUTS_DOCSTRING = r""" Args: input_features (`numpy.ndarray` of shape `(batch_size, feature_size, sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`WhisperFeatureExtractor`] should be used for extracting the features, padding and conversion into a tensor of type `numpy.ndarray`. See [`~WhisperFeatureExtractor.__call__`] @@ -138,7 +138,7 @@ WHISPER_ENCODE_INPUTS_DOCSTRING = r""" Args: input_features (`numpy.ndarray` of shape `(batch_size, feature_size, sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`WhisperFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `numpy.ndarray`. See [`~WhisperFeatureExtractor.__call__`]. @@ -175,7 +175,7 @@ WHISPER_DECODE_INPUTS_DOCSTRING = r""" decoder_position_ids (`numpy.ndarray` of shape `(batch_size, sequence_length)`, *optional*): Indices of positions of each decoder input sequence tokens in the position embeddings. Selected in the range `[0, config.max_position_embeddings - 1]`. - past_key_values (`Dict[str, numpy.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): + past_key_values (`dict[str, numpy.ndarray]`, *optional*, returned by `init_cache` or when passing previous `past_key_values`): Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*. output_attentions (`bool`, *optional*): @@ -230,7 +230,7 @@ class FlaxWhisperAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: is_cross_attention = key_value_states is not None batch_size = hidden_states.shape[0] @@ -318,7 +318,7 @@ class FlaxWhisperAttention(nn.Module): return hidden_state.reshape(hidden_state.shape[:2] + (self.embed_dim,)) @nn.compact - def _concatenate_to_cache(self, key, value, query, attention_mask) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]: + def _concatenate_to_cache(self, key, value, query, attention_mask) -> tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]: # detect if we're initializing by absence of existing cache data. is_initialized = self.has_variable("cache", "cached_key") cached_key = self.variable("cache", "cached_key", jnp.zeros, key.shape, key.dtype) @@ -382,7 +382,7 @@ class FlaxWhisperEncoderLayer(nn.Module): attention_mask: jnp.ndarray, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask) @@ -514,7 +514,7 @@ class FlaxWhisperDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) @@ -680,7 +680,7 @@ class FlaxWhisperEncoder(nn.Module): output_hidden_states: bool = False, return_dict: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: if input_features.shape[1:] != (self.config.num_mel_bins, self.config.max_source_positions * 2): raise ValueError( "input_features.shape[1:], must be equal to (self.config.num_mel_bins," @@ -756,7 +756,7 @@ class FlaxWhisperDecoder(nn.Module): output_hidden_states: bool = False, return_dict: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: input_embeds = self.embed_tokens(input_ids) position_embeds = self.embed_positions(position_ids) @@ -867,7 +867,7 @@ class FlaxWhisperPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: WhisperConfig, - input_shape: Optional[Tuple[int]] = None, + input_shape: Optional[tuple[int]] = None, seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -886,7 +886,7 @@ class FlaxWhisperPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_features = jnp.zeros(input_shape, dtype="f4") input_features = input_features.at[(..., -1)].set(self.config.eos_token_id) @@ -1599,7 +1599,7 @@ class FlaxWhisperForAudioClassification(FlaxWhisperPreTrainedModel): module_class = FlaxWhisperForAudioClassificationModule dtype: jnp.dtype = jnp.float32 - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_features = jnp.zeros(input_shape, dtype="f4") input_features = input_features.at[(..., -1)].set(self.config.eos_token_id) diff --git a/src/transformers/models/whisper/modeling_tf_whisper.py b/src/transformers/models/whisper/modeling_tf_whisper.py index 9778e223991..f5ca846b81e 100644 --- a/src/transformers/models/whisper/modeling_tf_whisper.py +++ b/src/transformers/models/whisper/modeling_tf_whisper.py @@ -18,7 +18,7 @@ from __future__ import annotations import math import random -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -194,11 +194,11 @@ class TFWhisperAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -442,9 +442,9 @@ class TFWhisperDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training=False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -557,12 +557,12 @@ class TFWhisperPreTrainedModel(TFPreTrainedModel): return input_lengths @property - def dummy_inputs(self) -> Dict[str, tf.Tensor]: + def dummy_inputs(self) -> dict[str, tf.Tensor]: """ Dummy inputs to build the network. Returns: - `Dict[str, tf.Tensor]`: The dummy inputs. + `dict[str, tf.Tensor]`: The dummy inputs. """ return { self.main_input_name: tf.random.uniform( @@ -600,7 +600,7 @@ WHISPER_INPUTS_DOCSTRING = r""" Args: input_features (`tf.Tensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be obtained - by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* + by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `tf.Tensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -728,7 +728,7 @@ class TFWhisperEncoder(keras.layers.Layer): Args: input_features (`tf.Tensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of fbank features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the fbank features, padding and conversion into a tensor of type `tf.Tensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -1262,15 +1262,15 @@ class TFWhisperModel(TFWhisperPreTrainedModel): head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, - decoder_inputs_embeds: Optional[Tuple[Union[np.ndarray, tf.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, + decoder_inputs_embeds: Optional[tuple[Union[np.ndarray, tf.Tensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqModelOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqModelOutput]: r""" Returns: @@ -1385,16 +1385,16 @@ class TFWhisperForConditionalGeneration(TFWhisperPreTrainedModel, TFCausalLangua head_mask: np.ndarray | tf.Tensor | None = None, decoder_head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - encoder_outputs: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, - decoder_inputs_embeds: Optional[Tuple[Union[np.ndarray, tf.Tensor]]] = None, + encoder_outputs: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, + decoder_inputs_embeds: Optional[tuple[Union[np.ndarray, tf.Tensor]]] = None, labels: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[Tuple[tf.Tensor], TFSeq2SeqLMOutput]: + ) -> Union[tuple[tf.Tensor], TFSeq2SeqLMOutput]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` @@ -1476,7 +1476,7 @@ class TFWhisperForConditionalGeneration(TFWhisperPreTrainedModel, TFCausalLangua inputs: Optional[tf.Tensor] = None, generation_config: Optional[GenerationConfig] = None, logits_processor: Optional[TFLogitsProcessorList] = None, - seed: Optional[List[int]] = None, + seed: Optional[list[int]] = None, return_timestamps: Optional[bool] = None, task: Optional[str] = None, language: Optional[str] = None, @@ -1516,7 +1516,7 @@ class TFWhisperForConditionalGeneration(TFWhisperPreTrainedModel, TFCausalLangua Custom logits processors that complement the default logits processors built from arguments and generation config. If a logit processor is passed that is already created with the arguments or a generation config an error is thrown. This feature is intended for advanced users. - seed (`List[int]`, *optional*): + seed (`list[int]`, *optional*): Random seed to control sampling, containing two integers, used when `do_sample` is `True`. See the `seed` argument from stateless functions in `tf.random`. return_timestamps (`bool`, *optional*): @@ -1538,7 +1538,7 @@ class TFWhisperForConditionalGeneration(TFWhisperPreTrainedModel, TFCausalLangua Whether to return token-level timestamps with the text. This can be used with or without the `return_timestamps` option. To get word-level timestamps, use the tokenizer to group the tokens into words. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*. diff --git a/src/transformers/models/whisper/modeling_whisper.py b/src/transformers/models/whisper/modeling_whisper.py index 9d55199fcc8..613f5fb45a7 100644 --- a/src/transformers/models/whisper/modeling_whisper.py +++ b/src/transformers/models/whisper/modeling_whisper.py @@ -15,7 +15,7 @@ """PyTorch Whisper model.""" import math -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -81,7 +81,7 @@ def shift_tokens_right(input_ids: torch.Tensor, pad_token_id: int, decoder_start # Copied from transformers.models.wav2vec2.modeling_wav2vec2._compute_mask_indices def _compute_mask_indices( - shape: Tuple[int, int], + shape: tuple[int, int], mask_prob: float, mask_length: int, attention_mask: Optional[torch.LongTensor] = None, @@ -295,7 +295,7 @@ class WhisperAttention(nn.Module): # TODO: we need a refactor so that the different attention modules can get their specific kwargs # ATM, we have mixed things encoder, decoder, and encoder-decoder attn **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -649,7 +649,7 @@ class WhisperEncoder(WhisperPreTrainedModel): Args: input_features (`torch.LongTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values of mel features extracted from the raw speech waveform. Raw speech waveform can be - obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a + obtained by loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -1107,20 +1107,20 @@ class WhisperModel(WhisperPreTrainedModel): head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqModelOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqModelOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -1276,21 +1276,21 @@ class WhisperForConditionalGeneration(WhisperGenerationMixin, WhisperPreTrainedM head_mask: Optional[torch.Tensor] = None, decoder_head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, - past_key_values: Optional[Union[EncoderDecoderCache, Tuple[torch.FloatTensor]]] = None, - decoder_inputs_embeds: Optional[Tuple[torch.FloatTensor]] = None, - decoder_position_ids: Optional[Tuple[torch.LongTensor]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[EncoderDecoderCache, tuple[torch.FloatTensor]]] = None, + decoder_inputs_embeds: Optional[tuple[torch.FloatTensor]] = None, + decoder_position_ids: Optional[tuple[torch.LongTensor]] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple[torch.Tensor], Seq2SeqLMOutput]: + ) -> Union[tuple[torch.Tensor], Seq2SeqLMOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] @@ -1470,10 +1470,10 @@ class WhisperForCausalLM(WhisperPreTrainedModel, GenerationMixin): self, input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[torch.FloatTensor]] = None, + encoder_outputs: Optional[tuple[torch.FloatTensor]] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, @@ -1481,7 +1481,7 @@ class WhisperForCausalLM(WhisperPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" encoder_outputs (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention @@ -1615,16 +1615,16 @@ class WhisperForAudioClassification(WhisperPreTrainedModel): self, input_features: Optional[torch.LongTensor] = None, head_mask: Optional[torch.Tensor] = None, - encoder_outputs: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + encoder_outputs: Optional[tuple[tuple[torch.FloatTensor]]] = None, labels: Optional[torch.LongTensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" input_features (`torch.FloatTensor` of shape `(batch_size, feature_size, sequence_length)`): Float values mel features extracted from the raw speech waveform. Raw speech waveform can be obtained by - loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via + loading a `.flac` or `.wav` audio file into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_features`, the [`AutoFeatureExtractor`] should be used for extracting the mel features, padding and conversion into a tensor of type `torch.FloatTensor`. See [`~WhisperFeatureExtractor.__call__`] diff --git a/src/transformers/models/whisper/tokenization_whisper.py b/src/transformers/models/whisper/tokenization_whisper.py index b713566bfcd..4cc730e5f3b 100644 --- a/src/transformers/models/whisper/tokenization_whisper.py +++ b/src/transformers/models/whisper/tokenization_whisper.py @@ -18,7 +18,7 @@ import json import os import warnings from functools import lru_cache -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import regex as re @@ -404,7 +404,7 @@ class WhisperTokenizer(PreTrainedTokenizer): self.predict_timestamps = predict_timestamps if predict_timestamps is not None else self.predict_timestamps @property - def prefix_tokens(self) -> List[int]: + def prefix_tokens(self) -> list[int]: bos_token_id = self.convert_tokens_to_ids("<|startoftranscript|>") translate_token_id = self.convert_tokens_to_ids("<|translate|>") transcribe_token_id = self.convert_tokens_to_ids("<|transcribe|>") @@ -438,7 +438,7 @@ class WhisperTokenizer(PreTrainedTokenizer): return bos_sequence # Copied from transformers.models.speech_to_text.tokenization_speech_to_text.Speech2TextTokenizer.build_inputs_with_special_tokens - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """Build model inputs from a sequence by appending eos_token_id.""" if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + [self.eos_token_id] @@ -447,22 +447,22 @@ class WhisperTokenizer(PreTrainedTokenizer): # Copied from transformers.models.speech_to_text.tokenization_speech_to_text.Speech2TextTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -577,7 +577,7 @@ class WhisperTokenizer(PreTrainedTokenizer): Compute offsets for a given tokenized input Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. time_precision (`float`, *optional*, defaults to 0.02): The time ratio to convert from token to time. @@ -656,7 +656,7 @@ class WhisperTokenizer(PreTrainedTokenizer): Pre-process the token ids for decoding by removing the prompt tokens ids and timestamp token ids. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Typically, obtained using the `__call__` method of the tokenizer. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens from the token ids. If `True`, the prompt token ids will be @@ -692,7 +692,7 @@ class WhisperTokenizer(PreTrainedTokenizer): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. Will remove the previous tokens (pre-prompt) @@ -752,7 +752,7 @@ class WhisperTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: Union[int, List[int]], + token_ids: Union[int, list[int]], skip_special_tokens: bool = False, normalize: bool = False, basic_normalize: bool = False, @@ -798,7 +798,7 @@ class WhisperTokenizer(PreTrainedTokenizer): text = bytearray([self.byte_decoder[c] for c in text]).decode("utf-8", errors=self.errors) return text - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return @@ -876,7 +876,7 @@ class WhisperTokenizer(PreTrainedTokenizer): batch_encoding.convert_to_tensors(tensor_type=return_tensors) return batch_encoding["input_ids"] - def _strip_prompt(self, token_ids: List[int], prompt_token_id: int, decoder_start_token_id: int): + def _strip_prompt(self, token_ids: list[int], prompt_token_id: int, decoder_start_token_id: int): if not isinstance(token_ids, list): token_ids = self._convert_to_list(token_ids) @@ -1277,7 +1277,7 @@ def _collate_word_timestamps(tokenizer, tokens, token_timestamps, language, retu def _combine_tokens_into_words( tokenizer, - tokens: List[int], + tokens: list[int], language: Optional[str] = None, prepend_punctuations: str = "\"'“¡¿([{-", append_punctuations: str = "\"'.。,,!!??::”)]}、", @@ -1301,7 +1301,7 @@ def _combine_tokens_into_words( return words, word_tokens, token_indices -def _split_tokens_on_unicode(tokenizer, tokens: List[int]): +def _split_tokens_on_unicode(tokenizer, tokens: list[int]): """Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points.""" decoded_full = tokenizer.decode(tokens, decode_with_timestamps=True) replacement_char = "\ufffd" @@ -1332,7 +1332,7 @@ def _split_tokens_on_unicode(tokenizer, tokens: List[int]): return words, word_tokens, token_indices -def _split_tokens_on_spaces(tokenizer, tokens: List[int]): +def _split_tokens_on_spaces(tokenizer, tokens: list[int]): """Combine tokens into words by splitting at whitespace and punctuation tokens.""" subwords, subword_tokens_list, subword_indices_list = _split_tokens_on_unicode(tokenizer, tokens) words = [] diff --git a/src/transformers/models/whisper/tokenization_whisper_fast.py b/src/transformers/models/whisper/tokenization_whisper_fast.py index 59abecaa269..07f4fdfcb00 100644 --- a/src/transformers/models/whisper/tokenization_whisper_fast.py +++ b/src/transformers/models/whisper/tokenization_whisper_fast.py @@ -19,7 +19,7 @@ import os import re import warnings from functools import lru_cache -from typing import List, Optional, Tuple +from typing import Optional import numpy as np from tokenizers import AddedToken, processors @@ -210,7 +210,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): Compute offsets for a given tokenized input Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. time_precision (`float`, *optional*, defaults to 0.02): The time ratio to convert from token to time. @@ -291,7 +291,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): Pre-process the token ids for decoding by removing the prompt tokens ids and timestamp token ids. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Typically, obtained using the `__call__` method of the tokenizer. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens from the token ids. If `True`, the prompt token ids will be @@ -329,7 +329,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. Will remove the previous tokens (pre-prompt) @@ -436,7 +436,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): normalizer = BasicTextNormalizer(remove_diacritics=remove_diacritics) return normalizer(text) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: files = self._tokenizer.model.save(save_directory, name=filename_prefix) normalizer_file = os.path.join( @@ -493,7 +493,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): @property # Copied from transformers.models.whisper.tokenization_whisper.WhisperTokenizer.prefix_tokens - def prefix_tokens(self) -> List[int]: + def prefix_tokens(self) -> list[int]: bos_token_id = self.convert_tokens_to_ids("<|startoftranscript|>") translate_token_id = self.convert_tokens_to_ids("<|translate|>") transcribe_token_id = self.convert_tokens_to_ids("<|transcribe|>") @@ -527,7 +527,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): return bos_sequence # Copied from transformers.models.whisper.tokenization_whisper.WhisperTokenizer.build_inputs_with_special_tokens - def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> List[int]: + def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None) -> list[int]: """Build model inputs from a sequence by appending eos_token_id.""" if token_ids_1 is None: return self.prefix_tokens + token_ids_0 + [self.eos_token_id] @@ -536,22 +536,22 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): # Copied from transformers.models.whisper.tokenization_whisper.WhisperTokenizer.get_special_tokens_mask def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -601,7 +601,7 @@ class WhisperTokenizerFast(PreTrainedTokenizerFast): return batch_encoding["input_ids"] # Copied from transformers.models.whisper.tokenization_whisper.WhisperTokenizer._strip_prompt - def _strip_prompt(self, token_ids: List[int], prompt_token_id: int, decoder_start_token_id: int): + def _strip_prompt(self, token_ids: list[int], prompt_token_id: int, decoder_start_token_id: int): if not isinstance(token_ids, list): token_ids = self._convert_to_list(token_ids) diff --git a/src/transformers/models/x_clip/modeling_x_clip.py b/src/transformers/models/x_clip/modeling_x_clip.py index 5f07ab0cc50..6c98381a63b 100644 --- a/src/transformers/models/x_clip/modeling_x_clip.py +++ b/src/transformers/models/x_clip/modeling_x_clip.py @@ -16,7 +16,7 @@ import copy from dataclasses import dataclass -from typing import Any, Callable, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -85,7 +85,7 @@ class XCLIPOutput(ModelOutput): vision_model_output: BaseModelOutputWithPooling = None mit_output: BaseModelOutputWithPooling = None - def to_tuple(self) -> Tuple[Any]: + def to_tuple(self) -> tuple[Any]: return tuple( self[k] if k not in ["text_model_output", "vision_model_output", "mit_output"] @@ -271,7 +271,7 @@ class XCLIPAttention(nn.Module): attention_mask: Optional[torch.Tensor] = None, causal_attention_mask: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor]]: """Input shape: Batch x Time x Channel""" batch_size, seq_length, embed_dim = hidden_states.shape @@ -353,7 +353,7 @@ class XCLIPEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -451,7 +451,7 @@ class XCLIPVisionEncoderLayer(nn.Module): attention_mask: torch.Tensor, causal_attention_mask: torch.Tensor, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.FloatTensor]: + ) -> tuple[torch.FloatTensor]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -582,7 +582,7 @@ class XCLIPEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -674,7 +674,7 @@ class XCLIPTextTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -750,7 +750,7 @@ class XCLIPTextModel(XCLIPPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -799,7 +799,7 @@ class XCLIPVisionEncoder(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" Args: inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`): @@ -896,7 +896,7 @@ class XCLIPVisionTransformer(nn.Module): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -948,7 +948,7 @@ class XCLIPVisionModel(XCLIPPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: r""" Examples: @@ -968,7 +968,7 @@ class XCLIPVisionModel(XCLIPPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -992,7 +992,7 @@ class XCLIPVisionModel(XCLIPPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) @@ -1048,7 +1048,7 @@ class XCLIPMultiframeIntegrationTransformer(nn.Module): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: residual = hidden_states # add position embeddings @@ -1294,7 +1294,7 @@ class XCLIPModel(XCLIPPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -1318,7 +1318,7 @@ class XCLIPModel(XCLIPPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) @@ -1389,7 +1389,7 @@ class XCLIPModel(XCLIPPreTrainedModel): output_hidden_states: Optional[bool] = None, interpolate_pos_encoding: bool = False, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XCLIPOutput]: + ) -> Union[tuple, XCLIPOutput]: r""" return_loss (`bool`, *optional*): Whether or not to return the contrastive loss. @@ -1412,7 +1412,7 @@ class XCLIPModel(XCLIPPreTrainedModel): ... Decode the video with PyAV decoder. ... Args: ... container (`av.container.input.InputContainer`): PyAV container. - ... indices (`List[int]`): List of frame indices to decode. + ... indices (`list[int]`): List of frame indices to decode. ... Returns: ... result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3). ... ''' @@ -1436,7 +1436,7 @@ class XCLIPModel(XCLIPPreTrainedModel): ... frame_sample_rate (`int`): Sample every n-th frame. ... seg_len (`int`): Maximum allowed index of sample's last frame. ... Returns: - ... indices (`List[int]`): List of sampled frame indices + ... indices (`list[int]`): List of sampled frame indices ... ''' ... converted_len = int(clip_len * frame_sample_rate) ... end_idx = np.random.randint(converted_len, seg_len) diff --git a/src/transformers/models/x_clip/processing_x_clip.py b/src/transformers/models/x_clip/processing_x_clip.py index 66568a4fee2..06b4b08d514 100644 --- a/src/transformers/models/x_clip/processing_x_clip.py +++ b/src/transformers/models/x_clip/processing_x_clip.py @@ -68,12 +68,12 @@ class XCLIPProcessor(ProcessorMixin): docstring of the above two methods for more information. Args: - text (`str`, `List[str]`, `List[List[str]]`): + text (`str`, `list[str]`, `list[list[str]]`): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - videos (`List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, `List[List[PIL.Image.Image]]`, `List[List[np.ndarray]]`,: - `List[List[torch.Tensor]]`): The video or batch of videos to be prepared. Each video should be a list + videos (`list[PIL.Image.Image]`, `list[np.ndarray]`, `list[torch.Tensor]`, `list[list[PIL.Image.Image]]`, `list[list[np.ndarray]]`,: + `list[list[torch.Tensor]]`): The video or batch of videos to be prepared. Each video should be a list of frames, which can be either PIL images or NumPy arrays. In case of NumPy arrays/PyTorch tensors, each frame should be of shape (H, W, C), where H and W are frame height and width, and C is a number of channels. diff --git a/src/transformers/models/xglm/modeling_flax_xglm.py b/src/transformers/models/xglm/modeling_flax_xglm.py index d98af5f3e92..1366148d9a3 100644 --- a/src/transformers/models/xglm/modeling_flax_xglm.py +++ b/src/transformers/models/xglm/modeling_flax_xglm.py @@ -17,7 +17,7 @@ import math import random from functools import partial -from typing import Optional, Tuple +from typing import Optional import flax.linen as nn import jax @@ -205,7 +205,7 @@ class FlaxXGLMAttention(nn.Module): attention_mask: Optional[jnp.ndarray] = None, init_cache: bool = False, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -341,7 +341,7 @@ class FlaxXGLMDecoderLayer(nn.Module): init_cache: bool = False, output_attentions: bool = True, deterministic: bool = True, - ) -> Tuple[jnp.ndarray]: + ) -> tuple[jnp.ndarray]: residual = hidden_states hidden_states = self.self_attn_layer_norm(hidden_states) @@ -544,7 +544,7 @@ class FlaxXGLMPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: XGLMConfig, - input_shape: Tuple[int] = (1, 1), + input_shape: tuple[int] = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -553,7 +553,7 @@ class FlaxXGLMPreTrainedModel(FlaxPreTrainedModel): module = self.module_class(config=config, dtype=dtype, **kwargs) super().__init__(config, module, input_shape=input_shape, seed=seed, dtype=dtype, _do_init=_do_init) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") attention_mask = jnp.ones_like(input_ids) diff --git a/src/transformers/models/xglm/modeling_tf_xglm.py b/src/transformers/models/xglm/modeling_tf_xglm.py index 4acf2bfe7f0..4ca60b5e5ad 100644 --- a/src/transformers/models/xglm/modeling_tf_xglm.py +++ b/src/transformers/models/xglm/modeling_tf_xglm.py @@ -18,7 +18,7 @@ from __future__ import annotations import math import random -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np import tensorflow as tf @@ -181,11 +181,11 @@ class TFXGLMAttention(keras.layers.Layer): self, hidden_states: tf.Tensor, key_value_states: tf.Tensor | None = None, - past_key_value: Tuple[Tuple[tf.Tensor]] | None = None, + past_key_value: tuple[tuple[tf.Tensor]] | None = None, attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor | None]: + ) -> tuple[tf.Tensor, tf.Tensor | None]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -355,9 +355,9 @@ class TFXGLMDecoderLayer(keras.layers.Layer): encoder_attention_mask: tf.Tensor | None = None, layer_head_mask: tf.Tensor | None = None, cross_attn_layer_head_mask: tf.Tensor | None = None, - past_key_value: Tuple[tf.Tensor] | None = None, + past_key_value: tuple[tf.Tensor] | None = None, training: Optional[bool] = False, - ) -> Tuple[tf.Tensor, tf.Tensor, Tuple[Tuple[tf.Tensor]]]: + ) -> tuple[tf.Tensor, tf.Tensor, tuple[tuple[tf.Tensor]]]: """ Args: hidden_states (`tf.Tensor`): input to the layer of shape *(batch, seq_len, embed_dim)* @@ -525,7 +525,7 @@ class TFXGLMMainLayer(keras.layers.Layer): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -533,7 +533,7 @@ class TFXGLMMainLayer(keras.layers.Layer): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs: Any, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -753,7 +753,7 @@ XGLM_INPUTS_DOCSTRING = r""" - 1 indicates the head is **not masked**, - 0 indicates the head is **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.num_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.num_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -818,7 +818,7 @@ class TFXGLMModel(TFXGLMPreTrainedModel): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, @@ -826,7 +826,7 @@ class TFXGLMModel(TFXGLMPreTrainedModel): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs: Any, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: outputs = self.model( input_ids=input_ids, attention_mask=attention_mask, @@ -929,7 +929,7 @@ class TFXGLMForCausalLM(TFXGLMPreTrainedModel, TFCausalLanguageModelingLoss): encoder_attention_mask: np.ndarray | tf.Tensor | None = None, head_mask: np.ndarray | tf.Tensor | None = None, cross_attn_head_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, labels: np.ndarray | tf.Tensor | None = None, use_cache: Optional[bool] = None, @@ -938,7 +938,7 @@ class TFXGLMForCausalLM(TFXGLMPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, training: Optional[bool] = False, **kwargs: Any, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" labels (`np.ndarray` or `tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set diff --git a/src/transformers/models/xglm/modeling_xglm.py b/src/transformers/models/xglm/modeling_xglm.py index 28bd37731f0..08520e5d3ab 100755 --- a/src/transformers/models/xglm/modeling_xglm.py +++ b/src/transformers/models/xglm/modeling_xglm.py @@ -15,7 +15,7 @@ """PyTorch XGLM model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -136,11 +136,11 @@ class XGLMAttention(nn.Module): self, hidden_states: torch.Tensor, key_value_states: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: """Input shape: Batch x Time x Channel""" # if key_value_states are provided this layer is used as a cross-attention layer @@ -291,7 +291,7 @@ class XGLMDecoderLayer(nn.Module): encoder_attention_mask: Optional[torch.Tensor] = None, layer_head_mask: Optional[torch.Tensor] = None, cross_attn_layer_head_mask: Optional[torch.Tensor] = None, - past_key_value: Optional[Tuple[torch.Tensor]] = None, + past_key_value: Optional[tuple[torch.Tensor]] = None, output_attentions: Optional[bool] = False, use_cache: Optional[bool] = True, ) -> torch.Tensor: @@ -441,13 +441,13 @@ class XGLMModel(XGLMPreTrainedModel): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention of @@ -647,7 +647,7 @@ class XGLMForCausalLM(XGLMPreTrainedModel, GenerationMixin): encoder_attention_mask: Optional[torch.Tensor] = None, head_mask: Optional[torch.Tensor] = None, cross_attn_head_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, use_cache: Optional[bool] = None, @@ -655,7 +655,7 @@ class XGLMForCausalLM(XGLMPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention of diff --git a/src/transformers/models/xglm/tokenization_xglm.py b/src/transformers/models/xglm/tokenization_xglm.py index e54191a57f5..9e0a8706683 100644 --- a/src/transformers/models/xglm/tokenization_xglm.py +++ b/src/transformers/models/xglm/tokenization_xglm.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -109,7 +109,7 @@ class XGLMTokenizer(PreTrainedTokenizer): cls_token="", unk_token="", pad_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs @@ -173,8 +173,8 @@ class XGLMTokenizer(PreTrainedTokenizer): self.sp_model.LoadFromSerializedProto(self.sp_model_proto) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -183,13 +183,13 @@ class XGLMTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -198,22 +198,22 @@ class XGLMTokenizer(PreTrainedTokenizer): return sep + token_ids_0 + sep + sep + token_ids_1 def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -226,20 +226,20 @@ class XGLMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -258,7 +258,7 @@ class XGLMTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: return self.sp_model.encode(text, out_type=str) def _convert_token_to_id(self, token): @@ -281,7 +281,7 @@ class XGLMTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/xglm/tokenization_xglm_fast.py b/src/transformers/models/xglm/tokenization_xglm_fast.py index 77b9a8bb8ea..a9c8b3aac25 100644 --- a/src/transformers/models/xglm/tokenization_xglm_fast.py +++ b/src/transformers/models/xglm/tokenization_xglm_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils_fast import PreTrainedTokenizerFast from ...utils import is_sentencepiece_available, logging @@ -77,7 +77,7 @@ class XGLMTokenizerFast(PreTrainedTokenizerFast): token instead. pad_token (`str`, *optional*, defaults to `""`): The token used for padding, for example when batching sequences of different lengths. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -121,8 +121,8 @@ class XGLMTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -131,13 +131,13 @@ class XGLMTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -146,20 +146,20 @@ class XGLMTokenizerFast(PreTrainedTokenizerFast): return sep + token_ids_0 + sep + sep + token_ids_1 def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -169,7 +169,7 @@ class XGLMTokenizerFast(PreTrainedTokenizerFast): return len(sep + token_ids_0) * [0] return len(sep + token_ids_0 + sep + sep + token_ids_1) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/xlm/modeling_tf_xlm.py b/src/transformers/models/xlm/modeling_tf_xlm.py index d161778c07d..29b21d3aadc 100644 --- a/src/transformers/models/xlm/modeling_tf_xlm.py +++ b/src/transformers/models/xlm/modeling_tf_xlm.py @@ -21,7 +21,7 @@ from __future__ import annotations import itertools import warnings from dataclasses import dataclass -from typing import Dict, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -400,7 +400,7 @@ class TFXLMMainLayer(keras.layers.Layer): output_hidden_states=None, return_dict=None, training=False, - ) -> Union[TFBaseModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutput, tuple[tf.Tensor]]: # removed: src_enc=None, src_len=None if input_ids is not None and inputs_embeds is not None: @@ -600,8 +600,8 @@ class TFXLMWithLMHeadModelOutput(ModelOutput): """ logits: Optional[tf.Tensor] = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None XLM_START_DOCSTRING = r""" @@ -687,7 +687,7 @@ XLM_INPUTS_DOCSTRING = r""" Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, tf.Tensor]`, *optional*): + cache (`dict[str, tf.Tensor]`, *optional*): Dictionary string to `tf.Tensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -744,14 +744,14 @@ class TFXLMModel(TFXLMPreTrainedModel): token_type_ids: tf.Tensor | None = None, position_ids: tf.Tensor | None = None, lengths: tf.Tensor | None = None, - cache: Dict[str, tf.Tensor] | None = None, + cache: dict[str, tf.Tensor] | None = None, head_mask: tf.Tensor | None = None, inputs_embeds: tf.Tensor | None = None, output_attentions: bool | None = None, output_hidden_states: bool | None = None, return_dict: bool | None = None, training: bool = False, - ) -> TFBaseModelOutput | Tuple[tf.Tensor]: + ) -> TFBaseModelOutput | tuple[tf.Tensor]: outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -881,14 +881,14 @@ class TFXLMWithLMHeadModel(TFXLMPreTrainedModel): token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFXLMWithLMHeadModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLMWithLMHeadModelOutput, tuple[tf.Tensor]]: transformer_outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -957,7 +957,7 @@ class TFXLMForSequenceClassification(TFXLMPreTrainedModel, TFSequenceClassificat token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -965,7 +965,7 @@ class TFXLMForSequenceClassification(TFXLMPreTrainedModel, TFSequenceClassificat return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1068,7 +1068,7 @@ class TFXLMForMultipleChoice(TFXLMPreTrainedModel, TFMultipleChoiceLoss): token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1076,7 +1076,7 @@ class TFXLMForMultipleChoice(TFXLMPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: if input_ids is not None: num_choices = shape_list(input_ids)[1] seq_length = shape_list(input_ids)[2] @@ -1184,7 +1184,7 @@ class TFXLMForTokenClassification(TFXLMPreTrainedModel, TFTokenClassificationLos token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1192,7 +1192,7 @@ class TFXLMForTokenClassification(TFXLMPreTrainedModel, TFTokenClassificationLos return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1273,7 +1273,7 @@ class TFXLMForQuestionAnsweringSimple(TFXLMPreTrainedModel, TFQuestionAnsweringL token_type_ids: np.ndarray | tf.Tensor | None = None, position_ids: np.ndarray | tf.Tensor | None = None, lengths: np.ndarray | tf.Tensor | None = None, - cache: Optional[Dict[str, tf.Tensor]] = None, + cache: Optional[dict[str, tf.Tensor]] = None, head_mask: np.ndarray | tf.Tensor | None = None, inputs_embeds: np.ndarray | tf.Tensor | None = None, output_attentions: Optional[bool] = None, @@ -1282,7 +1282,7 @@ class TFXLMForQuestionAnsweringSimple(TFXLMPreTrainedModel, TFQuestionAnsweringL start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/xlm/modeling_xlm.py b/src/transformers/models/xlm/modeling_xlm.py index df017e27b18..73679597944 100755 --- a/src/transformers/models/xlm/modeling_xlm.py +++ b/src/transformers/models/xlm/modeling_xlm.py @@ -19,7 +19,7 @@ PyTorch XLM model. import itertools import math from dataclasses import dataclass -from typing import Callable, Dict, Optional, Tuple, Union +from typing import Callable, Optional, Union import numpy as np import torch @@ -310,7 +310,7 @@ class XLMSQuADHead(nn.Module): is_impossible: Optional[torch.LongTensor] = None, p_mask: Optional[torch.FloatTensor] = None, return_dict: bool = False, - ) -> Union[XLMSquadHeadOutput, Tuple[torch.FloatTensor]]: + ) -> Union[XLMSquadHeadOutput, tuple[torch.FloatTensor]]: r""" hidden_states (`torch.FloatTensor` of shape `(batch_size, seq_len, hidden_size)`): Final hidden states of the model on the sequence tokens. @@ -688,8 +688,8 @@ class XLMForQuestionAnsweringOutput(ModelOutput): end_top_log_probs: Optional[torch.FloatTensor] = None end_top_index: Optional[torch.LongTensor] = None cls_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring @@ -787,14 +787,14 @@ class XLMModel(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # Dummy kwargs for now - ) -> Union[Tuple, BaseModelOutput]: + ) -> Union[tuple, BaseModelOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -808,7 +808,7 @@ class XLMModel(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1027,7 +1027,7 @@ class XLMWithLMHeadModel(XLMPreTrainedModel, GenerationMixin): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, @@ -1035,7 +1035,7 @@ class XLMWithLMHeadModel(XLMPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1049,7 +1049,7 @@ class XLMWithLMHeadModel(XLMPreTrainedModel, GenerationMixin): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1120,14 +1120,14 @@ class XLMForSequenceClassification(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1141,7 +1141,7 @@ class XLMForSequenceClassification(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1233,7 +1233,7 @@ class XLMForQuestionAnsweringSimple(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, start_positions: Optional[torch.Tensor] = None, @@ -1241,7 +1241,7 @@ class XLMForQuestionAnsweringSimple(XLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1255,7 +1255,7 @@ class XLMForQuestionAnsweringSimple(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1337,7 +1337,7 @@ class XLMForQuestionAnswering(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, start_positions: Optional[torch.Tensor] = None, @@ -1348,7 +1348,7 @@ class XLMForQuestionAnswering(XLMPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, XLMForQuestionAnsweringOutput]: + ) -> Union[tuple, XLMForQuestionAnsweringOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1362,7 +1362,7 @@ class XLMForQuestionAnswering(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1462,14 +1462,14 @@ class XLMForTokenClassification(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" langs (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): A parallel sequence of tokens to be used to indicate the language of each token in the input. Indices are @@ -1483,7 +1483,7 @@ class XLMForTokenClassification(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. @@ -1553,14 +1553,14 @@ class XLMForMultipleChoice(XLMPreTrainedModel): token_type_ids: Optional[torch.Tensor] = None, position_ids: Optional[torch.Tensor] = None, lengths: Optional[torch.Tensor] = None, - cache: Optional[Dict[str, torch.Tensor]] = None, + cache: Optional[dict[str, torch.Tensor]] = None, head_mask: Optional[torch.Tensor] = None, inputs_embeds: Optional[torch.Tensor] = None, labels: Optional[torch.Tensor] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1594,7 +1594,7 @@ class XLMForMultipleChoice(XLMPreTrainedModel): Length of each sentence that can be used to avoid performing attention on padding token indices. You can also use *attention_mask* for the same result (see above), kept here for compatibility. Indices selected in `[0, ..., input_ids.size(-1)]`. - cache (`Dict[str, torch.FloatTensor]`, *optional*): + cache (`dict[str, torch.FloatTensor]`, *optional*): Dictionary string to `torch.FloatTensor` that contains precomputed hidden states (key and values in the attention blocks) as computed by the model (see `cache` output below). Can be used to speed up sequential decoding. diff --git a/src/transformers/models/xlm/tokenization_xlm.py b/src/transformers/models/xlm/tokenization_xlm.py index a0e6b45e114..14d8aae2764 100644 --- a/src/transformers/models/xlm/tokenization_xlm.py +++ b/src/transformers/models/xlm/tokenization_xlm.py @@ -19,7 +19,7 @@ import os import re import sys import unicodedata -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import PreTrainedTokenizer from ...utils import logging @@ -473,8 +473,8 @@ class XLMTokenizer(PreTrainedTokenizer): return out_string def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM sequence has the following format: @@ -500,8 +500,8 @@ class XLMTokenizer(PreTrainedTokenizer): return bos + token_ids_0 + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. @@ -527,7 +527,7 @@ class XLMTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] return [1] + ([0] * len(token_ids_0)) + [1] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/xlm_roberta/modeling_flax_xlm_roberta.py b/src/transformers/models/xlm_roberta/modeling_flax_xlm_roberta.py index b7fdeda1b23..bdbc06620a1 100644 --- a/src/transformers/models/xlm_roberta/modeling_flax_xlm_roberta.py +++ b/src/transformers/models/xlm_roberta/modeling_flax_xlm_roberta.py @@ -15,7 +15,7 @@ # limitations under the License. """Flax XLM-RoBERTa model.""" -from typing import Callable, Optional, Tuple +from typing import Callable, Optional import flax.linen as nn import jax @@ -741,7 +741,7 @@ class FlaxXLMRobertaPreTrainedModel(FlaxPreTrainedModel): def __init__( self, config: XLMRobertaConfig, - input_shape: Tuple = (1, 1), + input_shape: tuple = (1, 1), seed: int = 0, dtype: jnp.dtype = jnp.float32, _do_init: bool = True, @@ -759,7 +759,7 @@ class FlaxXLMRobertaPreTrainedModel(FlaxPreTrainedModel): gradient_checkpointing=True, ) - def init_weights(self, rng: jax.random.PRNGKey, input_shape: Tuple, params: FrozenDict = None) -> FrozenDict: + def init_weights(self, rng: jax.random.PRNGKey, input_shape: tuple, params: FrozenDict = None) -> FrozenDict: # init input tensors input_ids = jnp.zeros(input_shape, dtype="i4") token_type_ids = jnp.ones_like(input_ids) diff --git a/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py b/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py index 5f4e9d006c3..03d93224e4c 100644 --- a/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py +++ b/src/transformers/models/xlm_roberta/modeling_tf_xlm_roberta.py @@ -19,7 +19,7 @@ from __future__ import annotations import math import warnings -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -334,10 +334,10 @@ class TFXLMRobertaSelfAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: batch_size = shape_list(hidden_states)[0] mixed_query_layer = self.query(inputs=hidden_states) @@ -472,10 +472,10 @@ class TFXLMRobertaAttention(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor, encoder_attention_mask: tf.Tensor, - past_key_value: Tuple[tf.Tensor], + past_key_value: tuple[tf.Tensor], output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: self_outputs = self.self_attention( hidden_states=input_tensor, attention_mask=attention_mask, @@ -589,10 +589,10 @@ class TFXLMRobertaLayer(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_value: Tuple[tf.Tensor] | None, + past_key_value: tuple[tf.Tensor] | None, output_attentions: bool, training: bool = False, - ) -> Tuple[tf.Tensor]: + ) -> tuple[tf.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -685,13 +685,13 @@ class TFXLMRobertaEncoder(keras.layers.Layer): head_mask: tf.Tensor, encoder_hidden_states: tf.Tensor | None, encoder_attention_mask: tf.Tensor | None, - past_key_values: Tuple[Tuple[tf.Tensor]] | None, + past_key_values: tuple[tuple[tf.Tensor]] | None, use_cache: Optional[bool], output_attentions: bool, output_hidden_states: bool, return_dict: bool, training: bool = False, - ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPastAndCrossAttentions, tuple[tf.Tensor]]: all_hidden_states = () if output_hidden_states else None all_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -800,13 +800,13 @@ class TFXLMRobertaMainLayer(keras.layers.Layer): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFBaseModelOutputWithPoolingAndCrossAttentions, tuple[tf.Tensor]]: if not self.config.is_decoder: use_cache = False @@ -1000,13 +1000,13 @@ class TFXLMRobertaModel(TFXLMRobertaPreTrainedModel): inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: Optional[bool] = False, - ) -> Union[Tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple, TFBaseModelOutputWithPoolingAndCrossAttentions]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1018,7 +1018,7 @@ class TFXLMRobertaModel(TFXLMRobertaPreTrainedModel): - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1158,7 +1158,7 @@ class TFXLMRobertaForMaskedLM(TFXLMRobertaPreTrainedModel, TFMaskedLanguageModel return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMaskedLMOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMaskedLMOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1261,14 +1261,14 @@ class TFXLMRobertaForCausalLM(TFXLMRobertaPreTrainedModel, TFCausalLanguageModel inputs_embeds: np.ndarray | tf.Tensor | None = None, encoder_hidden_states: np.ndarray | tf.Tensor | None = None, encoder_attention_mask: np.ndarray | tf.Tensor | None = None, - past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]] = None, + past_key_values: Optional[tuple[tuple[Union[np.ndarray, tf.Tensor]]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFCausalLMOutputWithCrossAttentions, Tuple[tf.Tensor]]: + ) -> Union[TFCausalLMOutputWithCrossAttentions, tuple[tf.Tensor]]: r""" encoder_hidden_states (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*): Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if @@ -1280,7 +1280,7 @@ class TFXLMRobertaForCausalLM(TFXLMRobertaPreTrainedModel, TFCausalLanguageModel - 1 for tokens that are **not masked**, - 0 for tokens that are **masked**. - past_key_values (`Tuple[Tuple[tf.Tensor]]` of length `config.n_layers`) + past_key_values (`tuple[tuple[tf.Tensor]]` of length `config.n_layers`) contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding. If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all @@ -1426,7 +1426,7 @@ class TFXLMRobertaForSequenceClassification(TFXLMRobertaPreTrainedModel, TFSeque return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFSequenceClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1518,7 +1518,7 @@ class TFXLMRobertaForMultipleChoice(TFXLMRobertaPreTrainedModel, TFMultipleChoic return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFMultipleChoiceModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFMultipleChoiceModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1627,7 +1627,7 @@ class TFXLMRobertaForTokenClassification(TFXLMRobertaPreTrainedModel, TFTokenCla return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFTokenClassifierOutput, Tuple[tf.Tensor]]: + ) -> Union[TFTokenClassifierOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1719,7 +1719,7 @@ class TFXLMRobertaForQuestionAnswering(TFXLMRobertaPreTrainedModel, TFQuestionAn start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: Optional[bool] = False, - ) -> Union[TFQuestionAnsweringModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFQuestionAnsweringModelOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py b/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py index 6a702985dbb..323e97a3e53 100644 --- a/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py +++ b/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py @@ -16,7 +16,7 @@ """PyTorch XLM-RoBERTa model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -176,9 +176,9 @@ class XLMRobertaSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -286,9 +286,9 @@ class XLMRobertaSdpaSelfAttention(XLMRobertaSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -429,9 +429,9 @@ class XLMRobertaAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: self_outputs = self.self( hidden_states, attention_mask, @@ -500,9 +500,9 @@ class XLMRobertaLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -579,12 +579,12 @@ class XLMRobertaEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: all_hidden_states = () if output_hidden_states else None all_self_attentions = () if output_attentions else None all_cross_attentions = () if output_attentions and self.config.add_cross_attention else None @@ -752,12 +752,12 @@ class XLMRobertaModel(XLMRobertaPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -924,13 +924,13 @@ class XLMRobertaForCausalLM(XLMRobertaPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1059,7 +1059,7 @@ class XLMRobertaForMaskedLM(XLMRobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1176,7 +1176,7 @@ class XLMRobertaForSequenceClassification(XLMRobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1271,7 +1271,7 @@ class XLMRobertaForMultipleChoice(XLMRobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1382,7 +1382,7 @@ class XLMRobertaForTokenClassification(XLMRobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: @@ -1484,7 +1484,7 @@ class XLMRobertaForQuestionAnswering(XLMRobertaPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" token_type_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,1]`: diff --git a/src/transformers/models/xlm_roberta/tokenization_xlm_roberta.py b/src/transformers/models/xlm_roberta/tokenization_xlm_roberta.py index 04d725bd6ca..149a09f5ed6 100644 --- a/src/transformers/models/xlm_roberta/tokenization_xlm_roberta.py +++ b/src/transformers/models/xlm_roberta/tokenization_xlm_roberta.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -113,7 +113,7 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): unk_token="", pad_token="", mask_token="", - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -169,8 +169,8 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): self.sp_model.LoadFromSerializedProto(self.sp_model_proto) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -179,13 +179,13 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -195,22 +195,22 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -223,20 +223,20 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -256,7 +256,7 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): vocab.update(self.added_tokens_encoder) return vocab - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: # TODO check if the t5/llama PR also applies here return self.sp_model.encode(text, out_type=str) @@ -280,7 +280,7 @@ class XLMRobertaTokenizer(PreTrainedTokenizer): out_string = "".join(tokens).replace(SPIECE_UNDERLINE, " ").strip() return out_string - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py b/src/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py index a7bee70f1d6..bcdea2325fc 100644 --- a/src/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py +++ b/src/transformers/models/xlm_roberta/tokenization_xlm_roberta_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -81,7 +81,7 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["NOTUSED", "NOTUSED"]`): Additional special tokens used by the tokenizer. """ @@ -121,8 +121,8 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM-RoBERTa sequence has the following format: @@ -131,13 +131,13 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: ` A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ if token_ids_1 is None: @@ -147,20 +147,20 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast): return cls + token_ids_0 + sep + sep + token_ids_1 + sep def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. XLM-RoBERTa does not make use of token type ids, therefore a list of zeros is returned. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of zeros. + `list[int]`: List of zeros. """ @@ -171,7 +171,7 @@ class XLMRobertaTokenizerFast(PreTrainedTokenizerFast): return len(cls + token_ids_0 + sep) * [0] return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0] - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/xlm_roberta_xl/modeling_xlm_roberta_xl.py b/src/transformers/models/xlm_roberta_xl/modeling_xlm_roberta_xl.py index 6040e400215..a0162b8252b 100644 --- a/src/transformers/models/xlm_roberta_xl/modeling_xlm_roberta_xl.py +++ b/src/transformers/models/xlm_roberta_xl/modeling_xlm_roberta_xl.py @@ -15,7 +15,7 @@ """PyTorch XLM RoBERTa xl,xxl model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -173,9 +173,9 @@ class XLMRobertaXLSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -283,9 +283,9 @@ class XLMRobertaXLSdpaSelfAttention(XLMRobertaXLSelfAttention): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: if self.position_embedding_type != "absolute" or output_attentions or head_mask is not None: # TODO: Improve this warning with e.g. `model.config._attn_implementation = "manual"` once implemented. logger.warning_once( @@ -744,12 +744,12 @@ class XLMRobertaXLModel(XLMRobertaXLPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -915,13 +915,13 @@ class XLMRobertaXLForCausalLM(XLMRobertaXLPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple, CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple, CausalLMOutputWithCrossAttentions]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in @@ -1074,7 +1074,7 @@ class XLMRobertaXLForMaskedLM(XLMRobertaXLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -1177,7 +1177,7 @@ class XLMRobertaXLForSequenceClassification(XLMRobertaXLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1259,7 +1259,7 @@ class XLMRobertaXLForMultipleChoice(XLMRobertaXLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See @@ -1359,7 +1359,7 @@ class XLMRobertaXLForTokenClassification(XLMRobertaXLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1456,7 +1456,7 @@ class XLMRobertaXLForQuestionAnswering(XLMRobertaXLPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.roberta( diff --git a/src/transformers/models/xlnet/modeling_tf_xlnet.py b/src/transformers/models/xlnet/modeling_tf_xlnet.py index 2383352ae6a..c61b6b23f56 100644 --- a/src/transformers/models/xlnet/modeling_tf_xlnet.py +++ b/src/transformers/models/xlnet/modeling_tf_xlnet.py @@ -21,7 +21,7 @@ from __future__ import annotations import warnings from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import numpy as np import tensorflow as tf @@ -846,7 +846,7 @@ class TFXLNetModelOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -864,9 +864,9 @@ class TFXLNetModelOutput(ModelOutput): """ last_hidden_state: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -882,7 +882,7 @@ class TFXLNetLMHeadModelOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -901,9 +901,9 @@ class TFXLNetLMHeadModelOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -916,7 +916,7 @@ class TFXLNetForSequenceClassificationOutput(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -935,9 +935,9 @@ class TFXLNetForSequenceClassificationOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -950,7 +950,7 @@ class TFXLNetForTokenClassificationOutput(ModelOutput): Classification loss. logits (`tf.Tensor` of shape `(batch_size, sequence_length, config.num_labels)`): Classification scores (before SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -969,9 +969,9 @@ class TFXLNetForTokenClassificationOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -986,7 +986,7 @@ class TFXLNetForMultipleChoiceOutput(ModelOutput): *num_choices* is the second dimension of the input tensors. (see *input_ids* above). Classification scores (before SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1005,9 +1005,9 @@ class TFXLNetForMultipleChoiceOutput(ModelOutput): loss: tf.Tensor | None = None logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None @dataclass @@ -1022,7 +1022,7 @@ class TFXLNetForQuestionAnsweringSimpleOutput(ModelOutput): Span-start scores (before SoftMax). end_logits (`tf.Tensor` of shape `(batch_size, sequence_length,)`): Span-end scores (before SoftMax). - mems (`List[tf.Tensor]` of length `config.n_layers`): + mems (`list[tf.Tensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1042,9 +1042,9 @@ class TFXLNetForQuestionAnsweringSimpleOutput(ModelOutput): loss: tf.Tensor | None = None start_logits: Optional[tf.Tensor] = None end_logits: Optional[tf.Tensor] = None - mems: List[tf.Tensor] | None = None - hidden_states: Tuple[tf.Tensor, ...] | None = None - attentions: Tuple[tf.Tensor, ...] | None = None + mems: list[tf.Tensor] | None = None + hidden_states: tuple[tf.Tensor, ...] | None = None + attentions: tuple[tf.Tensor, ...] | None = None XLNET_START_DOCSTRING = r""" @@ -1105,7 +1105,7 @@ XLNET_INPUTS_DOCSTRING = r""" - 0 for tokens that are **masked**. [What are attention masks?](../glossary#attention-mask) - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1194,7 +1194,7 @@ class TFXLNetModel(TFXLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, training: bool = False, - ) -> Union[TFXLNetModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetModelOutput, tuple[tf.Tensor]]: outputs = self.transformer( input_ids=input_ids, attention_mask=attention_mask, @@ -1303,7 +1303,7 @@ class TFXLNetLMHeadModel(TFXLNetPreTrainedModel, TFCausalLanguageModelingLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFXLNetLMHeadModelOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetLMHeadModelOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the cross entropy classification loss. Indices should be in `[0, ..., @@ -1438,7 +1438,7 @@ class TFXLNetForSequenceClassification(TFXLNetPreTrainedModel, TFSequenceClassif return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFXLNetForSequenceClassificationOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetForSequenceClassificationOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -1539,7 +1539,7 @@ class TFXLNetForMultipleChoice(TFXLNetPreTrainedModel, TFMultipleChoiceLoss): return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFXLNetForMultipleChoiceOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetForMultipleChoiceOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices]` @@ -1653,7 +1653,7 @@ class TFXLNetForTokenClassification(TFXLNetPreTrainedModel, TFTokenClassificatio return_dict: Optional[bool] = None, labels: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFXLNetForTokenClassificationOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetForTokenClassificationOutput, tuple[tf.Tensor]]: r""" labels (`tf.Tensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1744,7 +1744,7 @@ class TFXLNetForQuestionAnsweringSimple(TFXLNetPreTrainedModel, TFQuestionAnswer start_positions: np.ndarray | tf.Tensor | None = None, end_positions: np.ndarray | tf.Tensor | None = None, training: bool = False, - ) -> Union[TFXLNetForQuestionAnsweringSimpleOutput, Tuple[tf.Tensor]]: + ) -> Union[TFXLNetForQuestionAnsweringSimpleOutput, tuple[tf.Tensor]]: r""" start_positions (`tf.Tensor` of shape `(batch_size,)`, *optional*): Labels for position (index) of the start of the labelled span for computing the token classification loss. diff --git a/src/transformers/models/xlnet/modeling_xlnet.py b/src/transformers/models/xlnet/modeling_xlnet.py index 5a677f1d716..ea9f06adeb3 100755 --- a/src/transformers/models/xlnet/modeling_xlnet.py +++ b/src/transformers/models/xlnet/modeling_xlnet.py @@ -19,7 +19,7 @@ PyTorch XLNet model. import warnings from dataclasses import dataclass -from typing import Callable, List, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch from torch import nn @@ -843,7 +843,7 @@ class XLNetModelOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -861,9 +861,9 @@ class XLNetModelOutput(ModelOutput): """ last_hidden_state: torch.FloatTensor - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -879,7 +879,7 @@ class XLNetLMHeadModelOutput(ModelOutput): `num_predict` corresponds to `target_mapping.shape[1]`. If `target_mapping` is `None`, then `num_predict` corresponds to `sequence_length`. - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -898,9 +898,9 @@ class XLNetLMHeadModelOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -913,7 +913,7 @@ class XLNetForSequenceClassificationOutput(ModelOutput): Classification (or regression if config.num_labels==1) loss. logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`): Classification (or regression if config.num_labels==1) scores (before SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -932,9 +932,9 @@ class XLNetForSequenceClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -947,7 +947,7 @@ class XLNetForTokenClassificationOutput(ModelOutput): Classification loss. logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.num_labels)`): Classification scores (before SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -966,9 +966,9 @@ class XLNetForTokenClassificationOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -983,7 +983,7 @@ class XLNetForMultipleChoiceOutput(ModelOutput): *num_choices* is the second dimension of the input tensors. (see *input_ids* above). Classification scores (before SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1002,9 +1002,9 @@ class XLNetForMultipleChoiceOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1019,7 +1019,7 @@ class XLNetForQuestionAnsweringSimpleOutput(ModelOutput): Span-start scores (before SoftMax). end_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length,)`): Span-end scores (before SoftMax). - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1039,9 +1039,9 @@ class XLNetForQuestionAnsweringSimpleOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None start_logits: Optional[torch.FloatTensor] = None end_logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @dataclass @@ -1064,7 +1064,7 @@ class XLNetForQuestionAnsweringOutput(ModelOutput): Indices for the top `config.start_n_top * config.end_n_top` end token possibilities (beam-search). cls_logits (`torch.FloatTensor` of shape `(batch_size,)`, *optional*, returned if `start_positions` or `end_positions` is not provided): Log probabilities for the `is_impossible` label of the answers. - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states. Can be used (see `mems` input) to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1087,9 +1087,9 @@ class XLNetForQuestionAnsweringOutput(ModelOutput): end_top_log_probs: Optional[torch.FloatTensor] = None end_top_index: Optional[torch.LongTensor] = None cls_logits: Optional[torch.FloatTensor] = None - mems: Optional[List[torch.FloatTensor]] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + mems: Optional[list[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None @auto_docstring @@ -1238,9 +1238,9 @@ class XLNetModel(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete after depreciation warning is removed - ) -> Union[Tuple, XLNetModelOutput]: + ) -> Union[tuple, XLNetModelOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1566,9 +1566,9 @@ class XLNetLMHeadModel(XLNetPreTrainedModel, GenerationMixin): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetLMHeadModelOutput]: + ) -> Union[tuple, XLNetLMHeadModelOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1705,7 +1705,7 @@ class XLNetLMHeadModel(XLNetPreTrainedModel, GenerationMixin): ) @staticmethod - def _reorder_cache(mems: List[torch.Tensor], beam_idx: torch.Tensor) -> List[torch.Tensor]: + def _reorder_cache(mems: list[torch.Tensor], beam_idx: torch.Tensor) -> list[torch.Tensor]: """ This function is used to re-order the `mems` cache if [`~PreTrainedModel.beam_search`] or [`~PreTrainedModel.beam_sample`] is called. This is required to match `mems` with the correct beam_idx at every @@ -1751,9 +1751,9 @@ class XLNetForSequenceClassification(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetForSequenceClassificationOutput]: + ) -> Union[tuple, XLNetForSequenceClassificationOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1879,9 +1879,9 @@ class XLNetForTokenClassification(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetForTokenClassificationOutput]: + ) -> Union[tuple, XLNetForTokenClassificationOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -1989,7 +1989,7 @@ class XLNetForMultipleChoice(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetForMultipleChoiceOutput]: + ) -> Union[tuple, XLNetForMultipleChoiceOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -2016,7 +2016,7 @@ class XLNetForMultipleChoice(XLNetPreTrainedModel): - 0 for tokens that are **not masked**. You can only uses one of `input_mask` and `attention_mask`. - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -2136,9 +2136,9 @@ class XLNetForQuestionAnsweringSimple(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetForQuestionAnsweringSimpleOutput]: + ) -> Union[tuple, XLNetForQuestionAnsweringSimpleOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. @@ -2265,9 +2265,9 @@ class XLNetForQuestionAnswering(XLNetPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, # delete when `use_cache` is removed in XLNetModel - ) -> Union[Tuple, XLNetForQuestionAnsweringOutput]: + ) -> Union[tuple, XLNetForQuestionAnsweringOutput]: r""" - mems (`List[torch.FloatTensor]` of length `config.n_layers`): + mems (`list[torch.FloatTensor]` of length `config.n_layers`): Contains pre-computed hidden-states (see `mems` output below) . Can be used to speed up sequential decoding. The token ids which have their past given to this model should not be passed as `input_ids` as they have already been computed. diff --git a/src/transformers/models/xlnet/tokenization_xlnet.py b/src/transformers/models/xlnet/tokenization_xlnet.py index 6f956fe84cb..330eec9d845 100644 --- a/src/transformers/models/xlnet/tokenization_xlnet.py +++ b/src/transformers/models/xlnet/tokenization_xlnet.py @@ -17,7 +17,7 @@ import os import unicodedata from shutil import copyfile -from typing import Any, Dict, List, Optional, Tuple +from typing import Any, Optional import sentencepiece as spm @@ -92,7 +92,7 @@ class XLNetTokenizer(PreTrainedTokenizer): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `['', '']`): + additional_special_tokens (`list[str]`, *optional*, defaults to `['', '']`): Additional special tokens used by the tokenizer. sp_model_kwargs (`dict`, *optional*): Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for @@ -132,7 +132,7 @@ class XLNetTokenizer(PreTrainedTokenizer): cls_token="", mask_token="", additional_special_tokens=["", ""], - sp_model_kwargs: Optional[Dict[str, Any]] = None, + sp_model_kwargs: Optional[dict[str, Any]] = None, **kwargs, ) -> None: # Mask token behave like a normal word, i.e. include the space before it @@ -205,7 +205,7 @@ class XLNetTokenizer(PreTrainedTokenizer): return outputs - def _tokenize(self, text: str) -> List[str]: + def _tokenize(self, text: str) -> list[str]: """Tokenize a string.""" text = self.preprocess_text(text) pieces = self.sp_model.encode(text, out_type=str) @@ -240,7 +240,7 @@ class XLNetTokenizer(PreTrainedTokenizer): def _decode( self, - token_ids: List[int], + token_ids: list[int], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, spaces_between_special_tokens: bool = True, @@ -284,8 +284,8 @@ class XLNetTokenizer(PreTrainedTokenizer): return text def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLNet sequence has the following format: @@ -294,13 +294,13 @@ class XLNetTokenizer(PreTrainedTokenizer): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -309,22 +309,22 @@ class XLNetTokenizer(PreTrainedTokenizer): return token_ids_0 + sep + token_ids_1 + sep + cls def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` method. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. Returns: - `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. + `list[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. """ if already_has_special_tokens: @@ -337,8 +337,8 @@ class XLNetTokenizer(PreTrainedTokenizer): return ([0] * len(token_ids_0)) + [1, 1] def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet sequence pair mask has the following format: @@ -351,13 +351,13 @@ class XLNetTokenizer(PreTrainedTokenizer): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls_segment_id = [2] @@ -366,7 +366,7 @@ class XLNetTokenizer(PreTrainedTokenizer): return len(token_ids_0 + sep) * [0] + cls_segment_id return len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] + cls_segment_id - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not os.path.isdir(save_directory): logger.error(f"Vocabulary path ({save_directory}) should be a directory") return diff --git a/src/transformers/models/xlnet/tokenization_xlnet_fast.py b/src/transformers/models/xlnet/tokenization_xlnet_fast.py index 054ba0c3054..56cd2a50e1b 100644 --- a/src/transformers/models/xlnet/tokenization_xlnet_fast.py +++ b/src/transformers/models/xlnet/tokenization_xlnet_fast.py @@ -16,7 +16,7 @@ import os from shutil import copyfile -from typing import List, Optional, Tuple +from typing import Optional from ...tokenization_utils import AddedToken from ...tokenization_utils_fast import PreTrainedTokenizerFast @@ -97,7 +97,7 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): mask_token (`str`, *optional*, defaults to `""`): The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. - additional_special_tokens (`List[str]`, *optional*, defaults to `["", ""]`): + additional_special_tokens (`list[str]`, *optional*, defaults to `["", ""]`): Additional special tokens used by the tokenizer. Attributes: @@ -153,8 +153,8 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): self.vocab_file = vocab_file def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLNet sequence has the following format: @@ -163,13 +163,13 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): - pair of sequences: `A B ` Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs to which the special tokens will be added. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. + `list[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. """ sep = [self.sep_token_id] cls = [self.cls_token_id] @@ -178,8 +178,8 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): return token_ids_0 + sep + token_ids_1 + sep + cls def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLNet sequence pair mask has the following format: @@ -192,13 +192,13 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s). Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of IDs. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): Optional second list of IDs for sequence pairs. Returns: - `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). + `list[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s). """ sep = [self.sep_token_id] cls_segment_id = [2] @@ -207,7 +207,7 @@ class XLNetTokenizerFast(PreTrainedTokenizerFast): return len(token_ids_0 + sep) * [0] + cls_segment_id return len(token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] + cls_segment_id - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: if not self.can_save_slow_tokenizer: raise ValueError( "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow " diff --git a/src/transformers/models/xmod/modeling_xmod.py b/src/transformers/models/xmod/modeling_xmod.py index aac258d6a47..2d794aa6580 100644 --- a/src/transformers/models/xmod/modeling_xmod.py +++ b/src/transformers/models/xmod/modeling_xmod.py @@ -15,7 +15,7 @@ """PyTorch X-MOD model.""" import math -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -173,9 +173,9 @@ class XmodSelfAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: mixed_query_layer = self.query(hidden_states) # If this is instantiated as a cross-attention module, the keys @@ -317,9 +317,9 @@ class XmodAttention(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: residual = hidden_states if self.pre_norm: hidden_states = self.output.LayerNorm(hidden_states) @@ -447,9 +447,9 @@ class XmodLayer(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_value: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_value: Optional[tuple[tuple[torch.FloatTensor]]] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: # decoder uni-directional self-attention cached key/values tuple is at positions 1,2 self_attn_past_key_value = past_key_value[:2] if past_key_value is not None else None self_attention_outputs = self.attention( @@ -536,12 +536,12 @@ class XmodEncoder(nn.Module): head_mask: Optional[torch.FloatTensor] = None, encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = False, output_hidden_states: Optional[bool] = False, return_dict: Optional[bool] = True, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPastAndCrossAttentions]: if self.gradient_checkpointing and self.training: if use_cache: logger.warning_once( @@ -751,12 +751,12 @@ class XmodModel(XmodPreTrainedModel): inputs_embeds: Optional[torch.Tensor] = None, encoder_hidden_states: Optional[torch.Tensor] = None, encoder_attention_mask: Optional[torch.Tensor] = None, - past_key_values: Optional[List[torch.FloatTensor]] = None, + past_key_values: Optional[list[torch.FloatTensor]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: + ) -> Union[tuple[torch.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index @@ -907,13 +907,13 @@ class XmodForCausalLM(XmodPreTrainedModel, GenerationMixin): encoder_hidden_states: Optional[torch.FloatTensor] = None, encoder_attention_mask: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, - past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None, + past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, **kwargs, - ) -> Union[Tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: + ) -> Union[tuple[torch.Tensor], CausalLMOutputWithCrossAttentions]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index @@ -1040,7 +1040,7 @@ class XmodForMaskedLM(XmodPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MaskedLMOutput]: + ) -> Union[tuple[torch.Tensor], MaskedLMOutput]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index @@ -1151,7 +1151,7 @@ class XmodForSequenceClassification(XmodPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]: + ) -> Union[tuple[torch.Tensor], SequenceClassifierOutput]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index @@ -1240,7 +1240,7 @@ class XmodForMultipleChoice(XmodPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]: + ) -> Union[tuple[torch.Tensor], MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1354,7 +1354,7 @@ class XmodForTokenClassification(XmodPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]: + ) -> Union[tuple[torch.Tensor], TokenClassifierOutput]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index @@ -1450,7 +1450,7 @@ class XmodForQuestionAnswering(XmodPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], QuestionAnsweringModelOutput]: + ) -> Union[tuple[torch.Tensor], QuestionAnsweringModelOutput]: r""" lang_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Indices of the language adapters that should be activated for each sample, respectively. Default: the index diff --git a/src/transformers/models/yolos/configuration_yolos.py b/src/transformers/models/yolos/configuration_yolos.py index 307b2e6f335..3d2fdb2ba34 100644 --- a/src/transformers/models/yolos/configuration_yolos.py +++ b/src/transformers/models/yolos/configuration_yolos.py @@ -57,7 +57,7 @@ class YolosConfig(PretrainedConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (`float`, *optional*, defaults to 1e-12): The epsilon used by the layer normalization layers. - image_size (`List[int]`, *optional*, defaults to `[512, 864]`): + image_size (`list[int]`, *optional*, defaults to `[512, 864]`): The size (resolution) of each image. patch_size (`int`, *optional*, defaults to 16): The size (resolution) of each patch. diff --git a/src/transformers/models/yolos/image_processing_yolos.py b/src/transformers/models/yolos/image_processing_yolos.py index 06536d89e59..c859260088a 100644 --- a/src/transformers/models/yolos/image_processing_yolos.py +++ b/src/transformers/models/yolos/image_processing_yolos.py @@ -16,7 +16,7 @@ import pathlib from collections.abc import Iterable -from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Any, Callable, Optional, Union import numpy as np @@ -86,8 +86,8 @@ SUPPORTED_ANNOTATION_FORMATS = (AnnotationFormat.COCO_DETECTION, AnnotationForma # Copied from transformers.models.detr.image_processing_detr.get_max_height_width def get_max_height_width( - images: List[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None -) -> List[int]: + images: list[np.ndarray], input_data_format: Optional[Union[str, ChannelDimension]] = None +) -> list[int]: """ Get the maximum height and width across all images in a batch. """ @@ -104,13 +104,13 @@ def get_max_height_width( def get_size_with_aspect_ratio( - image_size: Tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 -) -> Tuple[int, int]: + image_size: tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size with multiple of divisible_size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -158,7 +158,7 @@ def get_image_size_for_max_height_width( max_height: int, max_width: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio. Important, even if image_height < max_height and image_width < max_width, the image will be resized @@ -191,10 +191,10 @@ def get_image_size_for_max_height_width( # Copied from transformers.models.detr.image_processing_detr.get_resize_output_image_size def get_resize_output_image_size( input_image: np.ndarray, - size: Union[int, Tuple[int, int], List[int]], + size: Union[int, tuple[int, int], list[int]], max_size: Optional[int] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size. If the desired output size is a tuple or list, the output image size is returned as is. If the desired output size is an integer, the output @@ -203,7 +203,7 @@ def get_resize_output_image_size( Args: input_image (`np.ndarray`): The image to resize. - size (`int` or `Tuple[int, int]` or `List[int]`): + size (`int` or `tuple[int, int]` or `list[int]`): The desired output size. max_size (`int`, *optional*): The maximum allowed output size. @@ -257,7 +257,7 @@ def safe_squeeze(arr: np.ndarray, axis: Optional[int] = None) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.normalize_annotation -def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: +def normalize_annotation(annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -272,7 +272,7 @@ def normalize_annotation(annotation: Dict, image_size: Tuple[int, int]) -> Dict: # Copied from transformers.models.detr.image_processing_detr.max_across_indices -def max_across_indices(values: Iterable[Any]) -> List[Any]: +def max_across_indices(values: Iterable[Any]) -> list[Any]: """ Return the maximum value across all indices of an iterable of values. """ @@ -281,7 +281,7 @@ def max_across_indices(values: Iterable[Any]) -> List[Any]: # Copied from transformers.models.detr.image_processing_detr.make_pixel_mask def make_pixel_mask( - image: np.ndarray, output_size: Tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None + image: np.ndarray, output_size: tuple[int, int], input_data_format: Optional[Union[str, ChannelDimension]] = None ) -> np.ndarray: """ Make a pixel mask for the image, where 1 indicates a valid pixel and 0 indicates padding. @@ -289,7 +289,7 @@ def make_pixel_mask( Args: image (`np.ndarray`): Image to make the pixel mask for. - output_size (`Tuple[int, int]`): + output_size (`tuple[int, int]`): Output size of the mask. """ input_height, input_width = get_image_size(image, channel_dim=input_data_format) @@ -304,7 +304,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int) -> np.ndar Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -432,11 +432,11 @@ def masks_to_boxes(masks: np.ndarray) -> np.ndarray: # Copied from transformers.models.detr.image_processing_detr.prepare_coco_panoptic_annotation with DETR->YOLOS def prepare_coco_panoptic_annotation( image: np.ndarray, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for YOLOS. """ @@ -473,7 +473,7 @@ def prepare_coco_panoptic_annotation( # Copied from transformers.models.detr.image_processing_detr.get_segmentation_image def get_segmentation_image( - masks: np.ndarray, input_size: Tuple, target_size: Tuple, stuff_equiv_classes, deduplicate=False + masks: np.ndarray, input_size: tuple, target_size: tuple, stuff_equiv_classes, deduplicate=False ): h, w = input_size final_h, final_w = target_size @@ -498,7 +498,7 @@ def get_segmentation_image( # Copied from transformers.models.detr.image_processing_detr.get_mask_area -def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: int) -> np.ndarray: +def get_mask_area(seg_img: np.ndarray, target_size: tuple[int, int], n_classes: int) -> np.ndarray: final_h, final_w = target_size np_seg_img = seg_img.astype(np.uint8) np_seg_img = np_seg_img.reshape(final_h, final_w, 3) @@ -508,7 +508,7 @@ def get_mask_area(seg_img: np.ndarray, target_size: Tuple[int, int], n_classes: # Copied from transformers.models.detr.image_processing_detr.score_labels_from_class_probabilities -def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: +def score_labels_from_class_probabilities(logits: np.ndarray) -> tuple[np.ndarray, np.ndarray]: probs = scipy.special.softmax(logits, axis=-1) labels = probs.argmax(-1, keepdims=True) scores = np.take_along_axis(probs, labels, axis=-1) @@ -518,9 +518,9 @@ def score_labels_from_class_probabilities(logits: np.ndarray) -> Tuple[np.ndarra # Copied from transformers.models.detr.image_processing_detr.resize_annotation def resize_annotation( - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, resample: PILImageResampling = PILImageResampling.NEAREST, ): @@ -528,11 +528,11 @@ def resize_annotation( Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -600,7 +600,7 @@ def convert_segmentation_to_rle(segmentation): segmentation (`torch.Tensor` or `numpy.array`): A segmentation map of shape `(height, width)` where each value denotes a segment or class id. Returns: - `List[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. + `list[List]`: A list of lists, where each list is the run-length encoding of a segment / class id. """ segment_ids = torch.unique(segmentation) @@ -631,7 +631,7 @@ def remove_low_and_no_objects(masks, scores, labels, object_mask_threshold, num_ Raises: `ValueError`: Raised when the first dimension doesn't match in all input tensors. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region + `tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`]`: The `masks`, `scores` and `labels` without the region < `object_mask_threshold`. """ if not (masks.shape[0] == scores.shape[0] == labels.shape[0]): @@ -668,14 +668,14 @@ def compute_segments( pred_labels, mask_threshold: float = 0.5, overlap_mask_area_threshold: float = 0.8, - label_ids_to_fuse: Optional[Set[int]] = None, - target_size: Optional[Tuple[int, int]] = None, + label_ids_to_fuse: Optional[set[int]] = None, + target_size: Optional[tuple[int, int]] = None, ): height = mask_probs.shape[1] if target_size is None else target_size[0] width = mask_probs.shape[2] if target_size is None else target_size[1] segmentation = torch.zeros((height, width), dtype=torch.int32, device=mask_probs.device) - segments: List[Dict] = [] + segments: list[dict] = [] if target_size is not None: mask_probs = nn.functional.interpolate( @@ -689,7 +689,7 @@ def compute_segments( mask_labels = mask_probs.argmax(0) # [height, width] # Keep track of instances of each class - stuff_memory_list: Dict[str, int] = {} + stuff_memory_list: dict[str, int] = {} for k in range(pred_labels.shape[0]): pred_class = pred_labels[k].item() should_fuse = pred_class in label_ids_to_fuse @@ -733,7 +733,7 @@ class YolosImageProcessor(BaseImageProcessor): do_resize (`bool`, *optional*, defaults to `True`): Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by the `do_resize` parameter in the `preprocess` method. - size (`Dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): + size (`dict[str, int]` *optional*, defaults to `{"shortest_edge": 800, "longest_edge": 1333}`): Size of the image's `(height, width)` dimensions after resizing. Can be overridden by the `size` parameter in the `preprocess` method. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. @@ -755,10 +755,10 @@ class YolosImageProcessor(BaseImageProcessor): do_normalize: Controls whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_MEAN`): Mean values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_DEFAULT_STD`): Standard deviation values to use when normalizing the image. Can be a single value or a list of values, one for each channel. Can be overridden by the `image_std` parameter in the `preprocess` method. do_pad (`bool`, *optional*, defaults to `True`): @@ -766,7 +766,7 @@ class YolosImageProcessor(BaseImageProcessor): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -778,16 +778,16 @@ class YolosImageProcessor(BaseImageProcessor): self, format: Union[str, AnnotationFormat] = AnnotationFormat.COCO_DETECTION, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> None: if "pad_and_return_pixel_mask" in kwargs: @@ -846,7 +846,7 @@ class YolosImageProcessor(BaseImageProcessor): @classmethod # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.from_dict with Detr->Yolos - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `YolosImageProcessor.from_pretrained(checkpoint, size=600, @@ -863,12 +863,12 @@ class YolosImageProcessor(BaseImageProcessor): def prepare_annotation( self, image: np.ndarray, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into DETR model. """ @@ -896,7 +896,7 @@ class YolosImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], resample: PILImageResampling = PILImageResampling.BILINEAR, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -909,7 +909,7 @@ class YolosImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -968,7 +968,7 @@ class YolosImageProcessor(BaseImageProcessor): orig_size, size, resample: PILImageResampling = PILImageResampling.NEAREST, - ) -> Dict: + ) -> dict: """ Resize the annotation to match the resized image. If size is an int, smaller edge of the mask will be matched to this number. @@ -1005,7 +1005,7 @@ class YolosImageProcessor(BaseImageProcessor): return rescale(image, rescale_factor, data_format=data_format, input_data_format=input_data_format) # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.normalize_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: """ Normalize the boxes in the annotation from `[top_left_x, top_left_y, bottom_right_x, bottom_right_y]` to `[center_x, center_y, width, height]` format and from absolute to relative pixel values. @@ -1015,12 +1015,12 @@ class YolosImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor._update_annotation_for_padded_image def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -1060,8 +1060,8 @@ class YolosImageProcessor(BaseImageProcessor): def _pad_image( self, image: np.ndarray, - output_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + output_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, constant_values: Union[float, Iterable[float]] = 0, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, @@ -1092,15 +1092,15 @@ class YolosImageProcessor(BaseImageProcessor): def pad( self, - images: List[np.ndarray], - annotations: Optional[List[Dict[str, Any]]] = None, + images: list[np.ndarray], + annotations: Optional[list[dict[str, Any]]] = None, constant_values: Union[float, Iterable[float]] = 0, return_pixel_mask: bool = False, return_tensors: Optional[Union[str, TensorType]] = None, data_format: Optional[ChannelDimension] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, update_bboxes: bool = True, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, ) -> BatchFeature: """ Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width @@ -1109,7 +1109,7 @@ class YolosImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to pad. - annotations (`List[Dict[str, any]]`, *optional*): + annotations (`list[dict[str, any]]`, *optional*): Annotations to pad along with the images. If provided, the bounding boxes will be updated to match the padded images. constant_values (`float` or `Iterable[float]`, *optional*): @@ -1131,7 +1131,7 @@ class YolosImageProcessor(BaseImageProcessor): Whether to update the bounding boxes in the annotations to match the padded images. If the bounding boxes have not been converted to relative coordinates and `(centre_x, centre_y, width, height)` format, the bounding boxes will not be updated. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1179,24 +1179,24 @@ class YolosImageProcessor(BaseImageProcessor): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, do_resize: Optional[bool] = None, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample=None, # PILImageResampling do_rescale: Optional[bool] = None, rescale_factor: Optional[Union[int, float]] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_convert_annotations: Optional[bool] = None, do_pad: Optional[bool] = None, format: Optional[Union[str, AnnotationFormat]] = None, return_tensors: Optional[Union[TensorType, str]] = None, data_format: Union[str, ChannelDimension] = ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]] = None, - pad_size: Optional[Dict[str, int]] = None, + pad_size: Optional[dict[str, int]] = None, **kwargs, ) -> BatchFeature: """ @@ -1206,15 +1206,15 @@ class YolosImageProcessor(BaseImageProcessor): images (`ImageInput`): Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`. - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks): @@ -1223,7 +1223,7 @@ class YolosImageProcessor(BaseImageProcessor): Path to the directory containing the segmentation masks. do_resize (`bool`, *optional*, defaults to self.do_resize): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to self.size): + size (`dict[str, int]`, *optional*, defaults to self.size): Size of the image's `(height, width)` dimensions after resizing. Available options are: - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`. Do NOT keep the aspect ratio. @@ -1241,9 +1241,9 @@ class YolosImageProcessor(BaseImageProcessor): Rescale factor to use when rescaling the image. do_normalize (`bool`, *optional*, defaults to self.do_normalize): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean): + image_mean (`float` or `list[float]`, *optional*, defaults to self.image_mean): Mean to use when normalizing the image. - image_std (`float` or `List[float]`, *optional*, defaults to self.image_std): + image_std (`float` or `list[float]`, *optional*, defaults to self.image_std): Standard deviation to use when normalizing the image. do_convert_annotations (`bool`, *optional*, defaults to self.do_convert_annotations): Whether to convert the annotations to the format expected by the model. Converts the bounding @@ -1265,7 +1265,7 @@ class YolosImageProcessor(BaseImageProcessor): - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. - `"none"` or `ChannelDimension.NONE`: image in (height, width) format. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -1451,7 +1451,7 @@ class YolosImageProcessor(BaseImageProcessor): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -1481,7 +1481,7 @@ class YolosImageProcessor(BaseImageProcessor): # Copied from transformers.models.detr.image_processing_detr.DetrImageProcessor.post_process_object_detection with Detr->Yolos def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None ): """ Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, top_left_y, @@ -1492,11 +1492,11 @@ class YolosImageProcessor(BaseImageProcessor): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size `(height, width)` of each image in the batch. If unset, predictions will not be resized. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -1515,7 +1515,7 @@ class YolosImageProcessor(BaseImageProcessor): # Convert from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/yolos/image_processing_yolos_fast.py b/src/transformers/models/yolos/image_processing_yolos_fast.py index 120722f05a4..87ed1cd06a6 100644 --- a/src/transformers/models/yolos/image_processing_yolos_fast.py +++ b/src/transformers/models/yolos/image_processing_yolos_fast.py @@ -5,7 +5,7 @@ # modular_yolos.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 import pathlib -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union from ...image_processing_utils import BatchFeature, get_size_dict from ...image_processing_utils_fast import ( @@ -68,7 +68,7 @@ class YolosFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): method. If `True`, padding will be applied to the bottom and right of the image with zeros. If `pad_size` is provided, the image will be padded to the specified dimensions. Otherwise, the image will be padded to the maximum height and width of the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest height and width in the batch. @@ -79,7 +79,7 @@ class YolosFastImageProcessorKwargs(DefaultFastImageProcessorKwargs): format: Optional[Union[str, AnnotationFormat]] do_convert_annotations: Optional[bool] do_pad: Optional[bool] - pad_size: Optional[Dict[str, int]] + pad_size: Optional[dict[str, int]] return_segmentation_masks: Optional[bool] @@ -92,7 +92,7 @@ def convert_coco_poly_to_mask(segmentations, height: int, width: int, device: to Convert a COCO polygon annotation to a mask. Args: - segmentations (`List[List[float]]`): + segmentations (`list[list[float]]`): List of polygons, each polygon represented by a list of x-y coordinates. height (`int`): Height of the mask. @@ -236,11 +236,11 @@ def rgb_to_id(color): def prepare_coco_panoptic_annotation( image: torch.Tensor, - target: Dict, + target: dict, masks_path: Union[str, pathlib.Path], return_masks: bool = True, input_data_format: Union[ChannelDimension, str] = None, -) -> Dict: +) -> dict: """ Prepare a coco panoptic annotation for YOLOS. """ @@ -284,13 +284,13 @@ def prepare_coco_panoptic_annotation( def get_size_with_aspect_ratio( - image_size: Tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 -) -> Tuple[int, int]: + image_size: tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size with multiple of divisible_size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -374,7 +374,7 @@ class YolosImageProcessorFast(BaseImageProcessorFast): super().__init__(**kwargs) @classmethod - def from_dict(cls, image_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs): """ Overrides the `from_dict` method from the base class to make sure parameters are updated if image processor is created using from_dict and kwargs e.g. `YolosImageProcessorFast.from_pretrained(checkpoint, size=600, @@ -390,12 +390,12 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def prepare_annotation( self, image: torch.Tensor, - target: Dict, + target: dict, format: Optional[AnnotationFormat] = None, return_segmentation_masks: Optional[bool] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, - ) -> Dict: + ) -> dict: """ Prepare an annotation for feeding into YOLOS model. """ @@ -475,9 +475,9 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def resize_annotation( self, - annotation: Dict[str, Any], - orig_size: Tuple[int, int], - target_size: Tuple[int, int], + annotation: dict[str, Any], + orig_size: tuple[int, int], + target_size: tuple[int, int], threshold: float = 0.5, interpolation: "F.InterpolationMode" = None, ): @@ -485,11 +485,11 @@ class YolosImageProcessorFast(BaseImageProcessorFast): Resizes an annotation to a target size. Args: - annotation (`Dict[str, Any]`): + annotation (`dict[str, Any]`): The annotation dictionary. - orig_size (`Tuple[int, int]`): + orig_size (`tuple[int, int]`): The original size of the input image. - target_size (`Tuple[int, int]`): + target_size (`tuple[int, int]`): The target size of the image, as returned by the preprocessing `resize` step. threshold (`float`, *optional*, defaults to 0.5): The threshold used to binarize the segmentation masks. @@ -526,7 +526,7 @@ class YolosImageProcessorFast(BaseImageProcessorFast): return new_annotation - def normalize_annotation(self, annotation: Dict, image_size: Tuple[int, int]) -> Dict: + def normalize_annotation(self, annotation: dict, image_size: tuple[int, int]) -> dict: image_height, image_width = image_size norm_annotation = {} for key, value in annotation.items(): @@ -543,12 +543,12 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def _update_annotation_for_padded_image( self, - annotation: Dict, - input_image_size: Tuple[int, int], - output_image_size: Tuple[int, int], + annotation: dict, + input_image_size: tuple[int, int], + output_image_size: tuple[int, int], padding, update_bboxes, - ) -> Dict: + ) -> dict: """ Update the annotation for a padded image. """ @@ -579,8 +579,8 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def pad( self, image: torch.Tensor, - padded_size: Tuple[int, int], - annotation: Optional[Dict[str, Any]] = None, + padded_size: tuple[int, int], + annotation: Optional[dict[str, Any]] = None, update_bboxes: bool = True, fill: int = 0, ): @@ -610,20 +610,20 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def preprocess( self, images: ImageInput, - annotations: Optional[Union[AnnotationType, List[AnnotationType]]] = None, + annotations: Optional[Union[AnnotationType, list[AnnotationType]]] = None, masks_path: Optional[Union[str, pathlib.Path]] = None, **kwargs: Unpack[YolosFastImageProcessorKwargs], ) -> BatchFeature: r""" - annotations (`AnnotationType` or `List[AnnotationType]`, *optional*): + annotations (`AnnotationType` or `list[AnnotationType]`, *optional*): List of annotations associated with the image or batch of images. If annotation is for object detection, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a + - "annotations" (`list[Dict]`): List of annotations for an image. Each annotation should be a dictionary. An image can have no annotations, in which case the list should be empty. If annotation is for segmentation, the annotations should be a dictionary with the following keys: - "image_id" (`int`): The image id. - - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary. + - "segments_info" (`list[Dict]`): List of segments for an image. Each segment should be a dictionary. An image can have no segments, in which case the list should be empty. - "file_name" (`str`): The file name of the image. masks_path (`str` or `pathlib.Path`, *optional*): @@ -647,8 +647,8 @@ class YolosImageProcessorFast(BaseImageProcessorFast): def _preprocess( self, - images: List["torch.Tensor"], - annotations: Optional[Union[AnnotationType, List[AnnotationType]]], + images: list["torch.Tensor"], + annotations: Optional[Union[AnnotationType, list[AnnotationType]]], masks_path: Optional[Union[str, pathlib.Path]], return_segmentation_masks: bool, do_resize: bool, @@ -658,10 +658,10 @@ class YolosImageProcessorFast(BaseImageProcessorFast): rescale_factor: float, do_normalize: bool, do_convert_annotations: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_pad: bool, - pad_size: Optional[Dict[str, int]], + pad_size: Optional[dict[str, int]], format: Optional[Union[str, AnnotationFormat]], return_tensors: Optional[Union[str, TensorType]], **kwargs, @@ -774,7 +774,7 @@ class YolosImageProcessorFast(BaseImageProcessorFast): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -807,7 +807,7 @@ class YolosImageProcessorFast(BaseImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, @@ -818,14 +818,14 @@ class YolosImageProcessorFast(BaseImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -848,7 +848,7 @@ class YolosImageProcessorFast(BaseImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/yolos/modeling_yolos.py b/src/transformers/models/yolos/modeling_yolos.py index a42982fb932..f1a7f4fab82 100755 --- a/src/transformers/models/yolos/modeling_yolos.py +++ b/src/transformers/models/yolos/modeling_yolos.py @@ -16,7 +16,7 @@ import collections.abc from dataclasses import dataclass -from typing import Callable, Dict, List, Optional, Set, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -69,13 +69,13 @@ class YolosObjectDetectionOutput(ModelOutput): """ loss: Optional[torch.FloatTensor] = None - loss_dict: Optional[Dict] = None + loss_dict: Optional[dict] = None logits: Optional[torch.FloatTensor] = None pred_boxes: Optional[torch.FloatTensor] = None - auxiliary_outputs: Optional[List[Dict]] = None + auxiliary_outputs: Optional[list[dict]] = None last_hidden_state: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor]] = None - attentions: Optional[Tuple[torch.FloatTensor]] = None + hidden_states: Optional[tuple[torch.FloatTensor]] = None + attentions: Optional[tuple[torch.FloatTensor]] = None class YolosEmbeddings(nn.Module): @@ -276,7 +276,7 @@ class YolosSelfAttention(nn.Module): def forward( self, hidden_states, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: key_layer = self.transpose_for_scores(self.key(hidden_states)) value_layer = self.transpose_for_scores(self.value(hidden_states)) query_layer = self.transpose_for_scores(self.query(hidden_states)) @@ -337,7 +337,7 @@ class YolosAttention(nn.Module): self.output = YolosSelfOutput(config) self.pruned_heads = set() - def prune_heads(self, heads: Set[int]) -> None: + def prune_heads(self, heads: set[int]) -> None: if len(heads) == 0: return heads, index = find_pruneable_heads_and_indices( @@ -360,7 +360,7 @@ class YolosAttention(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_outputs = self.attention(hidden_states, head_mask, output_attentions) attention_output = self.output(self_outputs[0], hidden_states) @@ -421,7 +421,7 @@ class YolosLayer(nn.Module): hidden_states: torch.Tensor, head_mask: Optional[torch.Tensor] = None, output_attentions: bool = False, - ) -> Union[Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor]]: + ) -> Union[tuple[torch.Tensor, torch.Tensor], tuple[torch.Tensor]]: self_attention_outputs = self.attention( self.layernorm_before(hidden_states), # in Yolos, layernorm is applied before self-attention head_mask, @@ -570,7 +570,7 @@ class YolosModel(YolosPreTrainedModel): def get_input_embeddings(self) -> YolosPatchEmbeddings: return self.embeddings.patch_embeddings - def _prune_heads(self, heads_to_prune: Dict[int, List[int]]) -> None: + def _prune_heads(self, heads_to_prune: dict[int, list[int]]) -> None: """ Prunes heads of the model. @@ -590,7 +590,7 @@ class YolosModel(YolosPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithPooling]: + ) -> Union[tuple, BaseModelOutputWithPooling]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -707,13 +707,13 @@ class YolosForObjectDetection(YolosPreTrainedModel): def forward( self, pixel_values: torch.FloatTensor, - labels: Optional[List[Dict]] = None, + labels: Optional[list[dict]] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, YolosObjectDetectionOutput]: + ) -> Union[tuple, YolosObjectDetectionOutput]: r""" - labels (`List[Dict]` of len `(batch_size,)`, *optional*): + labels (`list[Dict]` of len `(batch_size,)`, *optional*): Labels for computing the bipartite matching loss. List of dicts, each dictionary containing at least the following 2 keys: `'class_labels'` and `'boxes'` (the class labels and bounding boxes of an image in the batch respectively). The class labels themselves should be a `torch.LongTensor` of len `(number of bounding diff --git a/src/transformers/models/yolos/modular_yolos.py b/src/transformers/models/yolos/modular_yolos.py index 5ffdbe2e2ba..f5fec1ba80d 100644 --- a/src/transformers/models/yolos/modular_yolos.py +++ b/src/transformers/models/yolos/modular_yolos.py @@ -1,4 +1,4 @@ -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from transformers.models.detr.image_processing_detr_fast import DetrImageProcessorFast @@ -18,13 +18,13 @@ logger = logging.get_logger(__name__) def get_size_with_aspect_ratio( - image_size: Tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 -) -> Tuple[int, int]: + image_size: tuple[int, int], size: int, max_size: Optional[int] = None, mod_size: int = 16 +) -> tuple[int, int]: """ Computes the output image size given the input image size and the desired output size with multiple of divisible_size. Args: - image_size (`Tuple[int, int]`): + image_size (`tuple[int, int]`): The input image size. size (`int`): The desired output size. @@ -80,7 +80,7 @@ class YolosImageProcessorFast(DetrImageProcessorFast): original image size (before any data augmentation). For visualization, this should be the image size after data augment, but before padding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ logger.warning_once( @@ -113,7 +113,7 @@ class YolosImageProcessorFast(DetrImageProcessorFast): return results def post_process_object_detection( - self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, List[Tuple]] = None, top_k: int = 100 + self, outputs, threshold: float = 0.5, target_sizes: Union[TensorType, list[tuple]] = None, top_k: int = 100 ): """ Converts the raw output of [`YolosForObjectDetection`] into final bounding boxes in (top_left_x, @@ -124,14 +124,14 @@ class YolosImageProcessorFast(DetrImageProcessorFast): Raw outputs of the model. threshold (`float`, *optional*): Score threshold to keep object detection predictions. - target_sizes (`torch.Tensor` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`torch.Tensor` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. top_k (`int`, *optional*, defaults to 100): Keep only top k bounding boxes before filtering by thresholding. Returns: - `List[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image + `list[Dict]`: A list of dictionaries, each dictionary containing the scores, labels and boxes for an image in the batch as predicted by the model. """ out_logits, out_bbox = outputs.logits, outputs.pred_boxes @@ -154,7 +154,7 @@ class YolosImageProcessorFast(DetrImageProcessorFast): # and from relative [0, 1] to absolute [0, height] coordinates if target_sizes is not None: - if isinstance(target_sizes, List): + if isinstance(target_sizes, list): img_h = torch.Tensor([i[0] for i in target_sizes]) img_w = torch.Tensor([i[1] for i in target_sizes]) else: diff --git a/src/transformers/models/yoso/modeling_yoso.py b/src/transformers/models/yoso/modeling_yoso.py index 9bebac23031..be705885988 100644 --- a/src/transformers/models/yoso/modeling_yoso.py +++ b/src/transformers/models/yoso/modeling_yoso.py @@ -16,7 +16,7 @@ import math from pathlib import Path -from typing import Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -699,7 +699,7 @@ class YosoModel(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, BaseModelOutputWithCrossAttentions]: + ) -> Union[tuple, BaseModelOutputWithCrossAttentions]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -797,7 +797,7 @@ class YosoForMaskedLM(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MaskedLMOutput]: + ) -> Union[tuple, MaskedLMOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should be in `[-100, 0, ..., @@ -888,7 +888,7 @@ class YosoForSequenceClassification(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutput]: + ) -> Union[tuple, SequenceClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., @@ -971,7 +971,7 @@ class YosoForMultipleChoice(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, MultipleChoiceModelOutput]: + ) -> Union[tuple, MultipleChoiceModelOutput]: r""" input_ids (`torch.LongTensor` of shape `(batch_size, num_choices, sequence_length)`): Indices of input sequence tokens in the vocabulary. @@ -1078,7 +1078,7 @@ class YosoForTokenClassification(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, TokenClassifierOutput]: + ) -> Union[tuple, TokenClassifierOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the token classification loss. Indices should be in `[0, ..., config.num_labels - 1]`. @@ -1156,7 +1156,7 @@ class YosoForQuestionAnswering(YosoPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, QuestionAnsweringModelOutput]: + ) -> Union[tuple, QuestionAnsweringModelOutput]: return_dict = return_dict if return_dict is not None else self.config.use_return_dict outputs = self.yoso( diff --git a/src/transformers/models/zamba/modeling_zamba.py b/src/transformers/models/zamba/modeling_zamba.py index ae00519790a..ec6803e2d12 100644 --- a/src/transformers/models/zamba/modeling_zamba.py +++ b/src/transformers/models/zamba/modeling_zamba.py @@ -20,7 +20,7 @@ """PyTorch Zamba model.""" import math -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch import torch.utils.checkpoint @@ -148,8 +148,8 @@ class ZambaHybridDynamicCache(DynamicCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: # Update the cache if self.key_cache[layer_idx].shape[-1] == 0: self.key_cache[layer_idx] = key_states @@ -183,11 +183,11 @@ class ZambaHybridDynamicCache(DynamicCache): return 0 return self.key_cache[layer_idx].shape[-2] - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor], tuple[torch.Tensor]]: raise NotImplementedError("ZambaHybridDynamicCache does not have a legacy cache equivalent.") @classmethod - def from_legacy_cache(cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None) -> "DynamicCache": + def from_legacy_cache(cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None) -> "DynamicCache": raise NotImplementedError("ZambaHybridDynamicCache does not have a legacy cache equivalent.") @@ -256,7 +256,7 @@ class ZambaAttention(nn.Module): attention_mask: Optional[torch.Tensor], past_key_value: Optional[ZambaHybridDynamicCache] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -601,7 +601,7 @@ class ZambaAttentionDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): output of previous Mamba layer of shape `(batch, seq_len, embed_dim)` @@ -665,7 +665,7 @@ class ZambaMambaDecoderLayer(nn.Module): cache_position: Optional[torch.LongTensor] = None, transformer_hidden_states: Optional[torch.Tensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -731,7 +731,7 @@ class ZambaHybridLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, cache_position: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -835,7 +835,7 @@ class ZambaPreTrainedModel(PreTrainedModel): cls, config, torch_dtype: Optional[torch.dtype] = None, - device_map: Optional[Union[str, Dict[str, int]]] = None, + device_map: Optional[Union[str, dict[str, int]]] = None, hard_check_only: bool = False, check_device_map: bool = False, ): @@ -929,7 +929,7 @@ class ZambaModel(ZambaPreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1109,7 +1109,7 @@ class ZambaForCausalLM(ZambaPreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **loss_kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1272,14 +1272,14 @@ class ZambaForSequenceClassification(ZambaPreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/zamba2/modeling_zamba2.py b/src/transformers/models/zamba2/modeling_zamba2.py index dd7b6fde58e..25f64745537 100644 --- a/src/transformers/models/zamba2/modeling_zamba2.py +++ b/src/transformers/models/zamba2/modeling_zamba2.py @@ -22,7 +22,7 @@ import math import re from itertools import cycle -from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch from torch import nn @@ -148,8 +148,8 @@ class Zamba2HybridDynamicCache(DynamicCache): key_states: torch.Tensor, value_states: torch.Tensor, layer_idx: int, - cache_kwargs: Optional[Dict[str, Any]] = None, - ) -> Tuple[torch.Tensor, torch.Tensor]: + cache_kwargs: Optional[dict[str, Any]] = None, + ) -> tuple[torch.Tensor, torch.Tensor]: # Update the cache if self.key_cache[layer_idx].shape[-1] == 0: self.key_cache[layer_idx] = key_states @@ -181,11 +181,11 @@ class Zamba2HybridDynamicCache(DynamicCache): return 0 return self.key_cache[layer_idx].shape[-2] - def to_legacy_cache(self) -> Tuple[Tuple[torch.Tensor], Tuple[torch.Tensor]]: + def to_legacy_cache(self) -> tuple[tuple[torch.Tensor], tuple[torch.Tensor]]: raise NotImplementedError("Zamba2HybridDynamicCache does not have a legacy cache equivalent.") @classmethod - def from_legacy_cache(cls, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]] = None) -> "DynamicCache": + def from_legacy_cache(cls, past_key_values: Optional[tuple[tuple[torch.FloatTensor]]] = None) -> "DynamicCache": raise NotImplementedError("Zamba2HybridDynamicCache does not have a legacy cache equivalent.") def update_conv_state( @@ -406,9 +406,9 @@ class Zamba2Attention(nn.Module): layer_idx: int, attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Zamba2HybridDynamicCache] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -1001,7 +1001,7 @@ class Zamba2AttentionDecoderLayer(nn.Module): output_attentions: Optional[bool] = False, position_embeddings: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): output of previous Mamba layer of shape `(batch, seq_len, embed_dim)` @@ -1018,7 +1018,7 @@ class Zamba2AttentionDecoderLayer(nn.Module): use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -1065,7 +1065,7 @@ class Zamba2MambaDecoderLayer(nn.Module): cache_position: Optional[torch.LongTensor] = None, transformer_hidden_states: Optional[torch.Tensor] = None, **kwargs, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1133,7 +1133,7 @@ class Zamba2HybridLayer(nn.Module): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, position_embeddings: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -1149,7 +1149,7 @@ class Zamba2HybridLayer(nn.Module): use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -1290,7 +1290,7 @@ class Zamba2Model(Zamba2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states @@ -1528,7 +1528,7 @@ class Zamba2ForCausalLM(Zamba2PreTrainedModel, GenerationMixin): cache_position: Optional[torch.LongTensor] = None, logits_to_keep: Union[int, torch.Tensor] = 0, **loss_kwargs, - ) -> Union[Tuple, CausalLMOutputWithPast]: + ) -> Union[tuple, CausalLMOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., @@ -1691,14 +1691,14 @@ class Zamba2ForSequenceClassification(Zamba2PreTrainedModel): input_ids: Optional[torch.LongTensor] = None, attention_mask: Optional[torch.Tensor] = None, position_ids: Optional[torch.LongTensor] = None, - past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, + past_key_values: Optional[Union[Cache, list[torch.FloatTensor]]] = None, inputs_embeds: Optional[torch.FloatTensor] = None, labels: Optional[torch.LongTensor] = None, use_cache: Optional[bool] = None, output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple, SequenceClassifierOutputWithPast]: + ) -> Union[tuple, SequenceClassifierOutputWithPast]: r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., diff --git a/src/transformers/models/zamba2/modular_zamba2.py b/src/transformers/models/zamba2/modular_zamba2.py index 241a4bc3932..5c5c3a5599b 100644 --- a/src/transformers/models/zamba2/modular_zamba2.py +++ b/src/transformers/models/zamba2/modular_zamba2.py @@ -16,7 +16,7 @@ import math import re from itertools import cycle -from typing import Callable, Optional, Tuple, Union +from typing import Callable, Optional, Union import torch import torch.utils.checkpoint @@ -242,9 +242,9 @@ class Zamba2Attention(ZambaAttention): layer_idx: int, attention_mask: Optional[torch.Tensor] = None, past_key_value: Optional[Zamba2HybridDynamicCache] = None, - position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]] = None, + position_embeddings: Optional[tuple[torch.Tensor, torch.Tensor]] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + ) -> tuple[torch.Tensor, Optional[torch.Tensor], Optional[tuple[torch.Tensor]]]: input_shape = hidden_states.shape[:-1] hidden_shape = (*input_shape, -1, self.head_dim) @@ -778,7 +778,7 @@ class Zamba2AttentionDecoderLayer(ZambaAttentionDecoderLayer): output_attentions: Optional[bool] = False, position_embeddings: Optional[torch.LongTensor] = None, **kwargs: Unpack[FlashAttentionKwargs], - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): output of previous Mamba layer of shape `(batch, seq_len, embed_dim)` @@ -795,7 +795,7 @@ class Zamba2AttentionDecoderLayer(ZambaAttentionDecoderLayer): use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -848,7 +848,7 @@ class Zamba2HybridLayer(ZambaHybridLayer): output_attentions: Optional[bool] = False, use_cache: Optional[bool] = False, position_embeddings: Optional[torch.LongTensor] = None, - ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + ) -> tuple[torch.FloatTensor, Optional[tuple[torch.FloatTensor, torch.FloatTensor]]]: """ Args: hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` @@ -864,7 +864,7 @@ class Zamba2HybridLayer(ZambaHybridLayer): use_cache (`bool`, *optional*): If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`). - position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): + position_embeddings (`tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*): Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`, with `head_dim` being the embedding dimension of each attention head. """ @@ -1045,7 +1045,7 @@ class Zamba2Model(ZambaModel, Zamba2PreTrainedModel): output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, cache_position: Optional[torch.LongTensor] = None, - ) -> Union[Tuple, BaseModelOutputWithPast]: + ) -> Union[tuple, BaseModelOutputWithPast]: output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions output_hidden_states = ( output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states diff --git a/src/transformers/models/zoedepth/configuration_zoedepth.py b/src/transformers/models/zoedepth/configuration_zoedepth.py index bffedf32123..cab3b713e13 100644 --- a/src/transformers/models/zoedepth/configuration_zoedepth.py +++ b/src/transformers/models/zoedepth/configuration_zoedepth.py @@ -37,7 +37,7 @@ class ZoeDepthConfig(PretrainedConfig): documentation from [`PretrainedConfig`] for more information. Args: - backbone_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*, defaults to `BeitConfig()`): + backbone_config (`Union[dict[str, Any], PretrainedConfig]`, *optional*, defaults to `BeitConfig()`): The configuration of the backbone model. backbone (`str`, *optional*): Name of backbone to use when `backbone_config` is `None`. If `use_pretrained_backbone` is `True`, this @@ -64,9 +64,9 @@ class ZoeDepthConfig(PretrainedConfig): - "project" passes information to the other tokens by concatenating the readout to all other tokens before projecting the representation to the original feature dimension D using a linear layer followed by a GELU non-linearity. - reassemble_factors (`List[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): + reassemble_factors (`list[int]`, *optional*, defaults to `[4, 2, 1, 0.5]`): The up/downsampling factors of the reassemble layers. - neck_hidden_sizes (`List[str]`, *optional*, defaults to `[96, 192, 384, 768]`): + neck_hidden_sizes (`list[str]`, *optional*, defaults to `[96, 192, 384, 768]`): The hidden sizes to project to for the feature maps of the backbone. fusion_hidden_size (`int`, *optional*, defaults to 256): The number of channels before fusion. @@ -82,7 +82,7 @@ class ZoeDepthConfig(PretrainedConfig): Whether to add a projection layer before the depth estimation head. bottleneck_features (`int`, *optional*, defaults to 256): The number of features in the bottleneck layer. - num_attractors (`List[int], *optional*, defaults to `[16, 8, 4, 1]`): + num_attractors (`list[int], *optional*, defaults to `[16, 8, 4, 1]`): The number of attractors to use in each stage. bin_embedding_dim (`int`, *optional*, defaults to 128): The dimension of the bin embeddings. @@ -99,7 +99,7 @@ class ZoeDepthConfig(PretrainedConfig): bin_centers_type (`str`, *optional*, defaults to `"softplus"`): Activation type used for bin centers. Can be "normed" or "softplus". For "normed" bin centers, linear normalization trick is applied. This results in bounded bin centers. For "softplus", softplus activation is used and thus are unbounded. - bin_configurations (`List[dict]`, *optional*, defaults to `[{'n_bins': 64, 'min_depth': 0.001, 'max_depth': 10.0}]`): + bin_configurations (`list[dict]`, *optional*, defaults to `[{'n_bins': 64, 'min_depth': 0.001, 'max_depth': 10.0}]`): Configuration for each of the bin heads. Each configuration should consist of the following keys: - name (`str`): The name of the bin head - only required in case of multiple bin configurations. diff --git a/src/transformers/models/zoedepth/image_processing_zoedepth.py b/src/transformers/models/zoedepth/image_processing_zoedepth.py index 6407a3cf958..2fb125e2b79 100644 --- a/src/transformers/models/zoedepth/image_processing_zoedepth.py +++ b/src/transformers/models/zoedepth/image_processing_zoedepth.py @@ -16,7 +16,7 @@ import math from collections.abc import Iterable -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -67,7 +67,7 @@ def get_resize_output_image_size( keep_aspect_ratio: bool, multiple: int, input_data_format: Optional[Union[str, ChannelDimension]] = None, -) -> Tuple[int, int]: +) -> tuple[int, int]: def constrain_to_multiple_of(val, multiple, min_val=0): x = (np.round(val / multiple) * multiple).astype(int) @@ -115,15 +115,15 @@ class ZoeDepthImageProcessor(BaseImageProcessor): do_normalize (`bool`, *optional*, defaults to `True`): Whether to normalize the image. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): + image_mean (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_MEAN`): Mean to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): + image_std (`float` or `list[float]`, *optional*, defaults to `IMAGENET_STANDARD_STD`): Standard deviation to use if normalizing the image. This is a float or list of floats the length of the number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. do_resize (`bool`, *optional*, defaults to `True`): Whether to resize the image's (height, width) dimensions. Can be overridden by `do_resize` in `preprocess`. - size (`Dict[str, int]` *optional*, defaults to `{"height": 384, "width": 512}`): + size (`dict[str, int]` *optional*, defaults to `{"height": 384, "width": 512}`): Size of the image after resizing. Size of the image after resizing. If `keep_aspect_ratio` is `True`, the image is resized by choosing the smaller of the height and width scaling factors and using it for both dimensions. If `ensure_multiple_of` is also set, the image is further resized to a size that is a multiple of this value. @@ -152,10 +152,10 @@ class ZoeDepthImageProcessor(BaseImageProcessor): do_rescale: bool = True, rescale_factor: Union[int, float] = 1 / 255, do_normalize: bool = True, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_resize: bool = True, - size: Optional[Dict[str, int]] = None, + size: Optional[dict[str, int]] = None, resample: PILImageResampling = PILImageResampling.BILINEAR, keep_aspect_ratio: bool = True, ensure_multiple_of: int = 32, @@ -179,7 +179,7 @@ class ZoeDepthImageProcessor(BaseImageProcessor): def resize( self, image: np.ndarray, - size: Dict[str, int], + size: dict[str, int], keep_aspect_ratio: bool = False, ensure_multiple_of: int = 1, resample: PILImageResampling = PILImageResampling.BILINEAR, @@ -194,7 +194,7 @@ class ZoeDepthImageProcessor(BaseImageProcessor): Args: image (`np.ndarray`): Image to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Target size of the output image. keep_aspect_ratio (`bool`, *optional*, defaults to `False`): If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. @@ -303,8 +303,8 @@ class ZoeDepthImageProcessor(BaseImageProcessor): do_rescale: Optional[bool] = None, rescale_factor: Optional[float] = None, do_normalize: Optional[bool] = None, - image_mean: Optional[Union[float, List[float]]] = None, - image_std: Optional[Union[float, List[float]]] = None, + image_mean: Optional[Union[float, list[float]]] = None, + image_std: Optional[Union[float, list[float]]] = None, do_resize: Optional[bool] = None, size: Optional[int] = None, keep_aspect_ratio: Optional[bool] = None, @@ -329,13 +329,13 @@ class ZoeDepthImageProcessor(BaseImageProcessor): Rescale factor to rescale the image by if `do_rescale` is set to `True`. do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Image mean. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Image standard deviation. do_resize (`bool`, *optional*, defaults to `self.do_resize`): Whether to resize the image. - size (`Dict[str, int]`, *optional*, defaults to `self.size`): + size (`dict[str, int]`, *optional*, defaults to `self.size`): Size of the image after resizing. If `keep_aspect_ratio` is `True`, he image is resized by choosing the smaller of the height and width scaling factors and using it for both dimensions. If `ensure_multiple_of` is also set, the image is further resized to a size that is a multiple of this value. @@ -453,11 +453,11 @@ class ZoeDepthImageProcessor(BaseImageProcessor): def post_process_depth_estimation( self, outputs: "ZoeDepthDepthEstimatorOutput", - source_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, + source_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, outputs_flipped: Optional[Union["ZoeDepthDepthEstimatorOutput", None]] = None, do_remove_padding: Optional[Union[bool, None]] = None, - ) -> List[Dict[str, TensorType]]: + ) -> list[dict[str, TensorType]]: """ Converts the raw output of [`ZoeDepthDepthEstimatorOutput`] into final depth predictions and depth PIL images. Only supports PyTorch. @@ -465,12 +465,12 @@ class ZoeDepthImageProcessor(BaseImageProcessor): Args: outputs ([`ZoeDepthDepthEstimatorOutput`]): Raw outputs of the model. - source_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the source size + source_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the source size (height, width) of each image in the batch before preprocessing. This argument should be dealt as "required" unless the user passes `do_remove_padding=False` as input to this function. - target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. outputs_flipped ([`ZoeDepthDepthEstimatorOutput`], *optional*): Raw outputs of the model from flipped input (averaged out in the end). @@ -480,7 +480,7 @@ class ZoeDepthImageProcessor(BaseImageProcessor): parameter exists here in case the user changed the image preprocessing to not include padding. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ requires_backends(self, "torch") diff --git a/src/transformers/models/zoedepth/image_processing_zoedepth_fast.py b/src/transformers/models/zoedepth/image_processing_zoedepth_fast.py index abc72cd8cd1..c8c9aca5be4 100644 --- a/src/transformers/models/zoedepth/image_processing_zoedepth_fast.py +++ b/src/transformers/models/zoedepth/image_processing_zoedepth_fast.py @@ -15,10 +15,7 @@ """Fast Image processor class for ZoeDepth.""" from typing import ( - Dict, - List, Optional, - Tuple, Union, ) @@ -134,7 +131,7 @@ class ZoeDepthImageProcessorFast(BaseImageProcessorFast): Args: images (`torch.Tensor`): Images to resize. - size (`Dict[str, int]`): + size (`dict[str, int]`): Target size of the output image. keep_aspect_ratio (`bool`, *optional*, defaults to `False`): If `True`, the image is resized to the largest possible size such that the aspect ratio is preserved. @@ -189,8 +186,8 @@ class ZoeDepthImageProcessorFast(BaseImageProcessorFast): do_rescale: bool, rescale_factor: Optional[float], do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, ) -> BatchFeature: @@ -218,11 +215,11 @@ class ZoeDepthImageProcessorFast(BaseImageProcessorFast): def post_process_depth_estimation( self, outputs: "ZoeDepthDepthEstimatorOutput", - source_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, - target_sizes: Optional[Union[TensorType, List[Tuple[int, int]], None]] = None, + source_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, + target_sizes: Optional[Union[TensorType, list[tuple[int, int]], None]] = None, outputs_flipped: Optional[Union["ZoeDepthDepthEstimatorOutput", None]] = None, do_remove_padding: Optional[Union[bool, None]] = None, - ) -> List[Dict[str, TensorType]]: + ) -> list[dict[str, TensorType]]: """ Converts the raw output of [`ZoeDepthDepthEstimatorOutput`] into final depth predictions and depth PIL images. Only supports PyTorch. @@ -230,12 +227,12 @@ class ZoeDepthImageProcessorFast(BaseImageProcessorFast): Args: outputs ([`ZoeDepthDepthEstimatorOutput`]): Raw outputs of the model. - source_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the source size + source_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the source size (height, width) of each image in the batch before preprocessing. This argument should be dealt as "required" unless the user passes `do_remove_padding=False` as input to this function. - target_sizes (`TensorType` or `List[Tuple[int, int]]`, *optional*): - Tensor of shape `(batch_size, 2)` or list of tuples (`Tuple[int, int]`) containing the target size + target_sizes (`TensorType` or `list[tuple[int, int]]`, *optional*): + Tensor of shape `(batch_size, 2)` or list of tuples (`tuple[int, int]`) containing the target size (height, width) of each image in the batch. If left to None, predictions will not be resized. outputs_flipped ([`ZoeDepthDepthEstimatorOutput`], *optional*): Raw outputs of the model from flipped input (averaged out in the end). @@ -245,7 +242,7 @@ class ZoeDepthImageProcessorFast(BaseImageProcessorFast): parameter exists here in case the user changed the image preprocessing to not include padding. Returns: - `List[Dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth + `list[dict[str, TensorType]]`: A list of dictionaries of tensors representing the processed depth predictions. """ requires_backends(self, "torch") diff --git a/src/transformers/models/zoedepth/modeling_zoedepth.py b/src/transformers/models/zoedepth/modeling_zoedepth.py index 5084ffff359..eaea8d9dfd6 100644 --- a/src/transformers/models/zoedepth/modeling_zoedepth.py +++ b/src/transformers/models/zoedepth/modeling_zoedepth.py @@ -16,7 +16,7 @@ import math from dataclasses import dataclass -from typing import List, Optional, Tuple, Union +from typing import Optional, Union import torch import torch.utils.checkpoint @@ -63,8 +63,8 @@ class ZoeDepthDepthEstimatorOutput(ModelOutput): loss: Optional[torch.FloatTensor] = None predicted_depth: Optional[torch.FloatTensor] = None domain_logits: Optional[torch.FloatTensor] = None - hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None - attentions: Optional[Tuple[torch.FloatTensor, ...]] = None + hidden_states: Optional[tuple[torch.FloatTensor, ...]] = None + attentions: Optional[tuple[torch.FloatTensor, ...]] = None class ZoeDepthReassembleStage(nn.Module): @@ -100,10 +100,10 @@ class ZoeDepthReassembleStage(nn.Module): nn.Sequential(nn.Linear(2 * hidden_size, hidden_size), ACT2FN[config.hidden_act]) ) - def forward(self, hidden_states: List[torch.Tensor], patch_height, patch_width) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height, patch_width) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length + 1, hidden_size)`): List of hidden states from the backbone. """ batch_size = hidden_states[0].shape[0] @@ -320,10 +320,10 @@ class ZoeDepthNeck(nn.Module): # fusion self.fusion_stage = ZoeDepthFeatureFusionStage(config) - def forward(self, hidden_states: List[torch.Tensor], patch_height, patch_width) -> List[torch.Tensor]: + def forward(self, hidden_states: list[torch.Tensor], patch_height, patch_width) -> list[torch.Tensor]: """ Args: - hidden_states (`List[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): + hidden_states (`list[torch.FloatTensor]`, each of shape `(batch_size, sequence_length, hidden_size)` or `(batch_size, hidden_size, height, width)`): List of hidden states from the backbone. """ if not isinstance(hidden_states, (tuple, list)): @@ -366,7 +366,7 @@ class ZoeDepthRelativeDepthEstimationHead(nn.Module): self.conv2 = nn.Conv2d(features // 2, config.num_relative_features, kernel_size=3, stride=1, padding=1) self.conv3 = nn.Conv2d(config.num_relative_features, 1, kernel_size=1, stride=1, padding=0) - def forward(self, hidden_states: List[torch.Tensor]) -> torch.Tensor: + def forward(self, hidden_states: list[torch.Tensor]) -> torch.Tensor: # use last features hidden_states = hidden_states[self.head_in_index] @@ -630,7 +630,7 @@ class ZoeDepthAttractorLayer(nn.Module): Whether to interpolate the previous bin embeddings to the size of the input features. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`]: + `tuple[`torch.Tensor`, `torch.Tensor`]: New bin centers normed and scaled. """ if prev_bin_embedding is not None: @@ -723,7 +723,7 @@ class ZoeDepthAttractorLayerUnnormed(nn.Module): Whether to interpolate the previous bin embeddings to the size of the input features. Returns: - `Tuple[`torch.Tensor`, `torch.Tensor`]: + `tuple[`torch.Tensor`, `torch.Tensor`]: New bin centers unbounded. Two outputs just to keep the API consistent with the normed version. """ if prev_bin_embedding is not None: @@ -824,7 +824,7 @@ class ZoeDepthMultiheadAttention(nn.Module): values: torch.Tensor, attention_mask: Optional[torch.FloatTensor] = None, output_attentions: Optional[bool] = False, - ) -> Tuple[torch.Tensor]: + ) -> tuple[torch.Tensor]: query_layer = self.transpose_for_scores(self.query(queries)) key_layer = self.transpose_for_scores(self.key(keys)) value_layer = self.transpose_for_scores(self.value(values)) @@ -1272,7 +1272,7 @@ class ZoeDepthForDepthEstimation(ZoeDepthPreTrainedModel): output_attentions: Optional[bool] = None, output_hidden_states: Optional[bool] = None, return_dict: Optional[bool] = None, - ) -> Union[Tuple[torch.Tensor], DepthEstimatorOutput]: + ) -> Union[tuple[torch.Tensor], DepthEstimatorOutput]: r""" labels (`torch.LongTensor` of shape `(batch_size, height, width)`, *optional*): Ground truth depth estimation maps for computing the loss. diff --git a/src/transformers/onnx/config.py b/src/transformers/onnx/config.py index 5a9cb33c1c6..2a47127b385 100644 --- a/src/transformers/onnx/config.py +++ b/src/transformers/onnx/config.py @@ -17,7 +17,7 @@ import warnings from abc import ABC, abstractmethod from collections import OrderedDict from collections.abc import Iterable, Mapping -from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Optional, Union import numpy as np from packaging import version @@ -110,7 +110,7 @@ class OnnxConfig(ABC): } def __init__( - self, config: "PretrainedConfig", task: str = "default", patching_specs: Optional[List[PatchingSpec]] = None + self, config: "PretrainedConfig", task: str = "default", patching_specs: Optional[list[PatchingSpec]] = None ): self._config = config @@ -425,7 +425,7 @@ class OnnxConfig(ABC): setattr(spec.o, spec.name, orig_op) @classmethod - def flatten_output_collection_property(cls, name: str, field: Iterable[Any]) -> Dict[str, Any]: + def flatten_output_collection_property(cls, name: str, field: Iterable[Any]) -> dict[str, Any]: """ Flatten any potential nested structure expanding the name of the field with the index of the element within the structure. @@ -435,7 +435,7 @@ class OnnxConfig(ABC): field: The structure to, potentially, be flattened Returns: - (Dict[str, Any]): Outputs with flattened structure and key mapping this new structure. + (dict[str, Any]): Outputs with flattened structure and key mapping this new structure. """ from itertools import chain @@ -579,7 +579,7 @@ class OnnxConfigWithPast(OnnxConfig, ABC): flattened_output[f"{name}.{idx}.key"] = t[0] flattened_output[f"{name}.{idx}.value"] = t[1] - def flatten_output_collection_property(self, name: str, field: Iterable[Any]) -> Dict[str, Any]: + def flatten_output_collection_property(self, name: str, field: Iterable[Any]) -> dict[str, Any]: flattened_output = {} if name in ["present", "past_key_values"]: for idx, t in enumerate(field): @@ -609,7 +609,7 @@ class OnnxSeq2SeqConfigWithPast(OnnxConfigWithPast): return common_outputs @property - def num_layers(self) -> Tuple[int]: + def num_layers(self) -> tuple[int]: try: num_layers = super().num_layers num_layers = (num_layers, num_layers) @@ -625,7 +625,7 @@ class OnnxSeq2SeqConfigWithPast(OnnxConfigWithPast): return num_layers @property - def num_attention_heads(self) -> Tuple[int]: + def num_attention_heads(self) -> tuple[int]: try: num_attention_heads = super().num_attention_heads num_attention_heads = (num_attention_heads, num_attention_heads) diff --git a/src/transformers/onnx/convert.py b/src/transformers/onnx/convert.py index f1cfb6b1368..27a47c4ea94 100644 --- a/src/transformers/onnx/convert.py +++ b/src/transformers/onnx/convert.py @@ -17,7 +17,7 @@ from collections.abc import Iterable from inspect import signature from itertools import chain from pathlib import Path -from typing import TYPE_CHECKING, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np from packaging.version import Version, parse @@ -88,7 +88,7 @@ def export_pytorch( output: Path, tokenizer: Optional["PreTrainedTokenizer"] = None, device: str = "cpu", -) -> Tuple[List[str], List[str]]: +) -> tuple[list[str], list[str]]: """ Export a PyTorch model to an ONNX Intermediate Representation (IR) @@ -107,7 +107,7 @@ def export_pytorch( The device on which the ONNX model will be exported. Either `cpu` or `cuda`. Returns: - `Tuple[List[str], List[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from + `tuple[list[str], list[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from the ONNX configuration. """ @@ -146,11 +146,11 @@ def export_pytorch( model.to(device) model_inputs_device = {} for k, v in model_inputs.items(): - if isinstance(v, Tuple): + if isinstance(v, tuple): model_inputs_device[k] = tuple( x.to(device) if isinstance(x, torch.Tensor) else None for x in v ) - elif isinstance(v, List): + elif isinstance(v, list): model_inputs_device[k] = [ tuple(x.to(device) if isinstance(x, torch.Tensor) else None for x in t) for t in v ] @@ -190,7 +190,7 @@ def export_tensorflow( opset: int, output: Path, tokenizer: Optional["PreTrainedTokenizer"] = None, -) -> Tuple[List[str], List[str]]: +) -> tuple[list[str], list[str]]: """ Export a TensorFlow model to an ONNX Intermediate Representation (IR) @@ -207,7 +207,7 @@ def export_tensorflow( Directory to store the exported ONNX model. Returns: - `Tuple[List[str], List[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from + `tuple[list[str], list[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from the ONNX configuration. """ import onnx @@ -257,7 +257,7 @@ def export( output: Path, tokenizer: Optional["PreTrainedTokenizer"] = None, device: str = "cpu", -) -> Tuple[List[str], List[str]]: +) -> tuple[list[str], list[str]]: """ Export a Pytorch or TensorFlow model to an ONNX Intermediate Representation (IR) @@ -277,7 +277,7 @@ def export( export on CUDA devices. Returns: - `Tuple[List[str], List[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from + `tuple[list[str], list[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from the ONNX configuration. """ if not (is_torch_available() or is_tf_available()): @@ -320,7 +320,7 @@ def validate_model_outputs( preprocessor: Union["PreTrainedTokenizer", "FeatureExtractionMixin", "ProcessorMixin"], reference_model: Union["PreTrainedModel", "TFPreTrainedModel"], onnx_model: Path, - onnx_named_outputs: List[str], + onnx_named_outputs: list[str], atol: float, tokenizer: Optional["PreTrainedTokenizer"] = None, ): @@ -440,7 +440,7 @@ def validate_model_outputs( def ensure_model_and_config_inputs_match( model: Union["PreTrainedModel", "TFPreTrainedModel"], model_inputs: Iterable[str] -) -> Tuple[bool, List[str]]: +) -> tuple[bool, list[str]]: """ :param model_inputs: :param config_inputs: :return: diff --git a/src/transformers/onnx/features.py b/src/transformers/onnx/features.py index 5bd61d3cb58..a24aa79a596 100644 --- a/src/transformers/onnx/features.py +++ b/src/transformers/onnx/features.py @@ -1,6 +1,6 @@ import os from functools import partial, reduce -from typing import TYPE_CHECKING, Callable, Dict, Optional, Tuple, Type, Union +from typing import TYPE_CHECKING, Callable, Optional, Union import transformers @@ -54,7 +54,7 @@ if not is_torch_available() and not is_tf_available(): def supported_features_mapping( *supported_features: str, onnx_config_cls: Optional[str] = None -) -> Dict[str, Callable[[PretrainedConfig], OnnxConfig]]: +) -> dict[str, Callable[[PretrainedConfig], OnnxConfig]]: """ Generate the mapping between supported the features and their corresponding OnnxConfig for a given model. @@ -556,7 +556,7 @@ class FeaturesManager: @staticmethod def get_supported_features_for_model_type( model_type: str, model_name: Optional[str] = None - ) -> Dict[str, Callable[[PretrainedConfig], OnnxConfig]]: + ) -> dict[str, Callable[[PretrainedConfig], OnnxConfig]]: """ Tries to retrieve the feature -> OnnxConfig constructor map from the model type. @@ -599,7 +599,7 @@ class FeaturesManager: raise RuntimeError("Cannot export model to ONNX using TensorFlow because no TensorFlow package was found.") @staticmethod - def get_model_class_for_feature(feature: str, framework: str = "pt") -> Type: + def get_model_class_for_feature(feature: str, framework: str = "pt") -> type: """ Attempts to retrieve an AutoModel class from a feature name. @@ -711,7 +711,7 @@ class FeaturesManager: @staticmethod def check_supported_model_or_raise( model: Union["PreTrainedModel", "TFPreTrainedModel"], feature: str = "default" - ) -> Tuple[str, Callable]: + ) -> tuple[str, Callable]: """ Check whether or not the model has the requested features. diff --git a/src/transformers/optimization.py b/src/transformers/optimization.py index dd1d35a0b1b..29d3b6f7b94 100644 --- a/src/transformers/optimization.py +++ b/src/transformers/optimization.py @@ -618,7 +618,7 @@ class Adafactor(Optimizer): Iterable of parameters to optimize or dictionaries defining parameter groups. lr (`float`, *optional*): The external learning rate. - eps (`Tuple[float, float]`, *optional*, defaults to `(1e-30, 0.001)`): + eps (`tuple[float, float]`, *optional*, defaults to `(1e-30, 0.001)`): Regularization constants for square gradient and parameter scale respectively clip_threshold (`float`, *optional*, defaults to 1.0): Threshold of root mean square of final gradient update diff --git a/src/transformers/optimization_tf.py b/src/transformers/optimization_tf.py index cfd49613af9..1633d369fd3 100644 --- a/src/transformers/optimization_tf.py +++ b/src/transformers/optimization_tf.py @@ -134,7 +134,7 @@ def create_optimizer( The weight decay to use. power (`float`, *optional*, defaults to 1.0): The power to use for PolynomialDecay. - include_in_weight_decay (`List[str]`, *optional*): + include_in_weight_decay (`list[str]`, *optional*): List of the parameter names (or re patterns) to apply weight decay to. If none is passed, weight decay is applied to all parameters except bias and layer norm parameters. """ @@ -201,15 +201,15 @@ class AdamWeightDecay(Adam): Beyond](https://huggingface.co/papers/1904.09237). weight_decay_rate (`float`, *optional*, defaults to 0.0): The weight decay to apply. - include_in_weight_decay (`List[str]`, *optional*): + include_in_weight_decay (`list[str]`, *optional*): List of the parameter names (or re patterns) to apply weight decay to. If none is passed, weight decay is applied to all parameters by default (unless they are in `exclude_from_weight_decay`). - exclude_from_weight_decay (`List[str]`, *optional*): + exclude_from_weight_decay (`list[str]`, *optional*): List of the parameter names (or re patterns) to exclude from applying weight decay to. If a `include_in_weight_decay` is passed, the names in it will supersede this list. name (`str`, *optional*, defaults to `"AdamWeightDecay"`): Optional name for the operations created when applying gradients. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`, `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is clip gradients by value, `decay` is included for backward compatibility to allow time inverse decay of learning rate. `lr` is included for backward compatibility, recommended to use diff --git a/src/transformers/pipelines/__init__.py b/src/transformers/pipelines/__init__.py index 93a84f3bf61..79decc50d91 100755 --- a/src/transformers/pipelines/__init__.py +++ b/src/transformers/pipelines/__init__.py @@ -466,7 +466,7 @@ for task, values in SUPPORTED_TASKS.items(): PIPELINE_REGISTRY = PipelineRegistry(supported_tasks=SUPPORTED_TASKS, task_aliases=TASK_ALIASES) -def get_supported_tasks() -> List[str]: +def get_supported_tasks() -> list[str]: """ Returns a list of supported task strings. """ @@ -500,7 +500,7 @@ def get_task(model: str, token: Optional[str] = None, **deprecated_kwargs) -> st return task -def check_task(task: str) -> Tuple[str, Dict, Any]: +def check_task(task: str) -> tuple[str, dict, Any]: """ Checks an incoming task string, to validate it's correct and return the default Pipeline and Model classes, and default models if they exist. @@ -577,65 +577,65 @@ from typing import Literal, overload @overload -def pipeline(task: Literal[None], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> Pipeline: ... +def pipeline(task: Literal[None], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> Pipeline: ... @overload -def pipeline(task: Literal["audio-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> AudioClassificationPipeline: ... +def pipeline(task: Literal["audio-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> AudioClassificationPipeline: ... @overload -def pipeline(task: Literal["automatic-speech-recognition"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> AutomaticSpeechRecognitionPipeline: ... +def pipeline(task: Literal["automatic-speech-recognition"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> AutomaticSpeechRecognitionPipeline: ... @overload -def pipeline(task: Literal["depth-estimation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> DepthEstimationPipeline: ... +def pipeline(task: Literal["depth-estimation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> DepthEstimationPipeline: ... @overload -def pipeline(task: Literal["document-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> DocumentQuestionAnsweringPipeline: ... +def pipeline(task: Literal["document-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> DocumentQuestionAnsweringPipeline: ... @overload -def pipeline(task: Literal["feature-extraction"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> FeatureExtractionPipeline: ... +def pipeline(task: Literal["feature-extraction"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> FeatureExtractionPipeline: ... @overload -def pipeline(task: Literal["fill-mask"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> FillMaskPipeline: ... +def pipeline(task: Literal["fill-mask"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> FillMaskPipeline: ... @overload -def pipeline(task: Literal["image-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageClassificationPipeline: ... +def pipeline(task: Literal["image-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageClassificationPipeline: ... @overload -def pipeline(task: Literal["image-feature-extraction"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageFeatureExtractionPipeline: ... +def pipeline(task: Literal["image-feature-extraction"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageFeatureExtractionPipeline: ... @overload -def pipeline(task: Literal["image-segmentation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageSegmentationPipeline: ... +def pipeline(task: Literal["image-segmentation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageSegmentationPipeline: ... @overload -def pipeline(task: Literal["image-text-to-text"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageTextToTextPipeline: ... +def pipeline(task: Literal["image-text-to-text"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageTextToTextPipeline: ... @overload -def pipeline(task: Literal["image-to-image"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageToImagePipeline: ... +def pipeline(task: Literal["image-to-image"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageToImagePipeline: ... @overload -def pipeline(task: Literal["image-to-text"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageToTextPipeline: ... +def pipeline(task: Literal["image-to-text"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ImageToTextPipeline: ... @overload -def pipeline(task: Literal["mask-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> MaskGenerationPipeline: ... +def pipeline(task: Literal["mask-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> MaskGenerationPipeline: ... @overload -def pipeline(task: Literal["object-detection"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ObjectDetectionPipeline: ... +def pipeline(task: Literal["object-detection"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ObjectDetectionPipeline: ... @overload -def pipeline(task: Literal["question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> QuestionAnsweringPipeline: ... +def pipeline(task: Literal["question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> QuestionAnsweringPipeline: ... @overload -def pipeline(task: Literal["summarization"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> SummarizationPipeline: ... +def pipeline(task: Literal["summarization"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> SummarizationPipeline: ... @overload -def pipeline(task: Literal["table-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TableQuestionAnsweringPipeline: ... +def pipeline(task: Literal["table-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TableQuestionAnsweringPipeline: ... @overload -def pipeline(task: Literal["text-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextClassificationPipeline: ... +def pipeline(task: Literal["text-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextClassificationPipeline: ... @overload -def pipeline(task: Literal["text-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextGenerationPipeline: ... +def pipeline(task: Literal["text-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextGenerationPipeline: ... @overload -def pipeline(task: Literal["text-to-audio"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextToAudioPipeline: ... +def pipeline(task: Literal["text-to-audio"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TextToAudioPipeline: ... @overload -def pipeline(task: Literal["text2text-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> Text2TextGenerationPipeline: ... +def pipeline(task: Literal["text2text-generation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> Text2TextGenerationPipeline: ... @overload -def pipeline(task: Literal["token-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TokenClassificationPipeline: ... +def pipeline(task: Literal["token-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TokenClassificationPipeline: ... @overload -def pipeline(task: Literal["translation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TranslationPipeline: ... +def pipeline(task: Literal["translation"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> TranslationPipeline: ... @overload -def pipeline(task: Literal["video-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> VideoClassificationPipeline: ... +def pipeline(task: Literal["video-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> VideoClassificationPipeline: ... @overload -def pipeline(task: Literal["visual-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> VisualQuestionAnsweringPipeline: ... +def pipeline(task: Literal["visual-question-answering"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> VisualQuestionAnsweringPipeline: ... @overload -def pipeline(task: Literal["zero-shot-audio-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotAudioClassificationPipeline: ... +def pipeline(task: Literal["zero-shot-audio-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotAudioClassificationPipeline: ... @overload -def pipeline(task: Literal["zero-shot-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotClassificationPipeline: ... +def pipeline(task: Literal["zero-shot-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotClassificationPipeline: ... @overload -def pipeline(task: Literal["zero-shot-image-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotImageClassificationPipeline: ... +def pipeline(task: Literal["zero-shot-image-classification"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotImageClassificationPipeline: ... @overload -def pipeline(task: Literal["zero-shot-object-detection"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[Dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotObjectDetectionPipeline: ... +def pipeline(task: Literal["zero-shot-object-detection"], model: Optional[Union[str, "PreTrainedModel", "TFPreTrainedModel"]] = None, config: Optional[Union[str, PretrainedConfig]] = None, tokenizer: Optional[Union[str, PreTrainedTokenizer, "PreTrainedTokenizerFast"]] = None, feature_extractor: Optional[Union[str, PreTrainedFeatureExtractor]] = None, image_processor: Optional[Union[str, BaseImageProcessor]] = None, processor: Optional[Union[str, ProcessorMixin]] = None, framework: Optional[str] = None, revision: Optional[str] = None, use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any) -> ZeroShotObjectDetectionPipeline: ... # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # The part of the file above was automatically generated from the code. @@ -657,10 +657,10 @@ def pipeline( use_fast: bool = True, token: Optional[Union[str, bool]] = None, device: Optional[Union[int, str, "torch.device"]] = None, - device_map: Optional[Union[str, Dict[str, Union[int, str]]]] = None, + device_map: Optional[Union[str, dict[str, Union[int, str]]]] = None, torch_dtype: Optional[Union[str, "torch.dtype"]] = None, trust_remote_code: Optional[bool] = None, - model_kwargs: Optional[Dict[str, Any]] = None, + model_kwargs: Optional[dict[str, Any]] = None, pipeline_class: Optional[Any] = None, **kwargs: Any, ) -> Pipeline: @@ -787,7 +787,7 @@ def pipeline( device (`int` or `str` or `torch.device`): Defines the device (*e.g.*, `"cpu"`, `"cuda:1"`, `"mps"`, or a GPU ordinal rank like `1`) on which this pipeline will be allocated. - device_map (`str` or `Dict[str, Union[int, str, torch.device]`, *optional*): + device_map (`str` or `dict[str, Union[int, str, torch.device]`, *optional*): Sent directly as `model_kwargs` (just a simpler shortcut). When `accelerate` library is present, set `device_map="auto"` to compute the most optimized `device_map` automatically (see [here](https://huggingface.co/docs/accelerate/main/en/package_reference/big_modeling#accelerate.cpu_offload) @@ -806,10 +806,10 @@ def pipeline( Whether or not to allow for custom code defined on the Hub in their own modeling, configuration, tokenization or even pipeline files. This option should only be set to `True` for repositories you trust and in which you have read the code, as it will execute code present on the Hub on your local machine. - model_kwargs (`Dict[str, Any]`, *optional*): + model_kwargs (`dict[str, Any]`, *optional*): Additional dictionary of keyword arguments passed along to the model's `from_pretrained(..., **model_kwargs)` function. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the specific pipeline init (see the documentation for the corresponding pipeline class for possible values). diff --git a/src/transformers/pipelines/audio_classification.py b/src/transformers/pipelines/audio_classification.py index 62813fbb5e1..1715d247e52 100644 --- a/src/transformers/pipelines/audio_classification.py +++ b/src/transformers/pipelines/audio_classification.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import subprocess -from typing import Any, Dict, List, Union +from typing import Any, Union import numpy as np import requests @@ -103,7 +103,7 @@ class AudioClassificationPipeline(Pipeline): self.check_model_type(MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES) - def __call__(self, inputs: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> List[Dict[str, Any]]: + def __call__(self, inputs: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> list[dict[str, Any]]: """ Classify the sequence(s) given as inputs. See the [`AutomaticSpeechRecognitionPipeline`] documentation for more information. diff --git a/src/transformers/pipelines/audio_utils.py b/src/transformers/pipelines/audio_utils.py index 72a5f51db61..63e18c03524 100644 --- a/src/transformers/pipelines/audio_utils.py +++ b/src/transformers/pipelines/audio_utils.py @@ -2,7 +2,7 @@ import datetime import platform import subprocess -from typing import Optional, Tuple, Union +from typing import Optional, Union import numpy as np @@ -135,7 +135,7 @@ def ffmpeg_microphone_live( sampling_rate: int, chunk_length_s: float, stream_chunk_s: Optional[int] = None, - stride_length_s: Optional[Union[Tuple[float, float], float]] = None, + stride_length_s: Optional[Union[tuple[float, float], float]] = None, format_for_conversion: str = "f32le", ffmpeg_input_device: Optional[str] = None, ffmpeg_additional_args: Optional[list[str]] = None, @@ -226,7 +226,7 @@ def ffmpeg_microphone_live( yield item -def chunk_bytes_iter(iterator, chunk_len: int, stride: Tuple[int, int], stream: bool = False): +def chunk_bytes_iter(iterator, chunk_len: int, stride: tuple[int, int], stream: bool = False): """ Reads raw bytes from an iterator and does chunks of length `chunk_len`. Optionally adds `stride` to each chunks to get overlaps. `stream` is used to return partial results even if a full `chunk_len` is not yet available. diff --git a/src/transformers/pipelines/automatic_speech_recognition.py b/src/transformers/pipelines/automatic_speech_recognition.py index 5fa177fe2ba..a24493767ef 100644 --- a/src/transformers/pipelines/automatic_speech_recognition.py +++ b/src/transformers/pipelines/automatic_speech_recognition.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections import defaultdict -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union import numpy as np import requests @@ -211,7 +211,7 @@ class AutomaticSpeechRecognitionPipeline(ChunkPipeline): super().__init__(model, tokenizer, feature_extractor, device=device, torch_dtype=torch_dtype, **kwargs) - def __call__(self, inputs: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> List[Dict[str, Any]]: + def __call__(self, inputs: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> list[dict[str, Any]]: """ Transcribe the audio sequence(s) given as inputs to text. See the [`AutomaticSpeechRecognitionPipeline`] documentation for more information. @@ -262,7 +262,7 @@ class AutomaticSpeechRecognitionPipeline(ChunkPipeline): Return: `Dict`: A dictionary with the following keys: - **text** (`str`): The recognized text. - - **chunks** (*optional(, `List[Dict]`) + - **chunks** (*optional(, `list[Dict]`) When using `return_timestamps`, the `chunks` will become a list containing all the various text chunks identified by the model, *e.g.* `[{"text": "hi ", "timestamp": (0.5, 0.9)}, {"text": "there", "timestamp": (1.0, 1.5)}]`. The original full text can roughly be recovered by doing @@ -547,7 +547,7 @@ class AutomaticSpeechRecognitionPipeline(ChunkPipeline): return {"is_last": is_last, **out, **extra} def postprocess( - self, model_outputs, decoder_kwargs: Optional[Dict] = None, return_timestamps=None, return_language=None + self, model_outputs, decoder_kwargs: Optional[dict] = None, return_timestamps=None, return_language=None ): # Optional return types optional = {} diff --git a/src/transformers/pipelines/base.py b/src/transformers/pipelines/base.py index 8878ad00b9d..cc32871c12d 100644 --- a/src/transformers/pipelines/base.py +++ b/src/transformers/pipelines/base.py @@ -27,7 +27,7 @@ from abc import ABC, abstractmethod from collections import UserDict from contextlib import contextmanager from os.path import abspath, exists -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Optional, Union from ..dynamic_module_utils import custom_object_save from ..feature_extraction_utils import PreTrainedFeatureExtractor @@ -56,7 +56,7 @@ from ..utils import ( ) -GenericTensor = Union[List["GenericTensor"], "torch.Tensor", "tf.Tensor"] +GenericTensor = Union[list["GenericTensor"], "torch.Tensor", "tf.Tensor"] if is_tf_available(): import tensorflow as tf @@ -209,7 +209,7 @@ def pad_collate_fn(tokenizer, feature_extractor): def infer_framework_load_model( model, config: AutoConfig, - model_classes: Optional[Dict[str, Tuple[type]]] = None, + model_classes: Optional[dict[str, tuple[type]]] = None, task: Optional[str] = None, framework: Optional[str] = None, **model_kwargs, @@ -313,7 +313,7 @@ def infer_framework_load_model( def infer_framework_from_model( model, - model_classes: Optional[Dict[str, Tuple[type]]] = None, + model_classes: Optional[dict[str, tuple[type]]] = None, task: Optional[str] = None, framework: Optional[str] = None, **model_kwargs, @@ -385,8 +385,8 @@ def get_framework(model, revision: Optional[str] = None): def get_default_model_and_revision( - targeted_task: Dict, framework: Optional[str], task_options: Optional[Any] -) -> Tuple[str, str]: + targeted_task: dict, framework: Optional[str], task_options: Optional[Any] +) -> tuple[str, str]: """ Select a default model to use for a given task. Defaults to pytorch if ambiguous. @@ -434,7 +434,7 @@ def load_assistant_model( model: "PreTrainedModel", assistant_model: Optional[Union[str, "PreTrainedModel"]], assistant_tokenizer: Optional[PreTrainedTokenizer], -) -> Tuple[Optional["PreTrainedModel"], Optional[PreTrainedTokenizer]]: +) -> tuple[Optional["PreTrainedModel"], Optional[PreTrainedTokenizer]]: """ Prepares the assistant model and the assistant tokenizer for a pipeline whose model that can call `generate`. @@ -563,7 +563,7 @@ class PipelineDataFormat: raise NotImplementedError() @abstractmethod - def save(self, data: Union[dict, List[dict]]): + def save(self, data: Union[dict, list[dict]]): """ Save the provided data object with the representation for the current [`~pipelines.PipelineDataFormat`]. @@ -572,7 +572,7 @@ class PipelineDataFormat: """ raise NotImplementedError() - def save_binary(self, data: Union[dict, List[dict]]) -> str: + def save_binary(self, data: Union[dict, list[dict]]) -> str: """ Save the provided data object as a pickle-formatted binary data on the disk. @@ -656,12 +656,12 @@ class CsvPipelineDataFormat(PipelineDataFormat): else: yield row[self.column[0]] - def save(self, data: List[dict]): + def save(self, data: list[dict]): """ Save the provided data object with the representation for the current [`~pipelines.PipelineDataFormat`]. Args: - data (`List[dict]`): The data to store. + data (`list[dict]`): The data to store. """ with open(self.output_path, "w") as f: if len(data) > 0: @@ -750,7 +750,7 @@ class PipedPipelineDataFormat(PipelineDataFormat): """ print(data) - def save_binary(self, data: Union[dict, List[dict]]) -> str: + def save_binary(self, data: Union[dict, list[dict]]) -> str: if self.output_path is None: raise KeyError( "When using piped input on pipeline outputting large object requires an output file path. " @@ -1109,7 +1109,7 @@ class Pipeline(_ScikitCompat, PushToHubMixin): A path to the directory where to saved. It will be created if it doesn't exist. safe_serialization (`str`): Whether to save the model using `safetensors` or the traditional way for PyTorch or Tensorflow. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -1231,7 +1231,7 @@ class Pipeline(_ScikitCompat, PushToHubMixin): Recursive on lists **only**. Return: - `Dict[str, torch.Tensor]`: The same as `inputs` but on the proper device. + `dict[str, torch.Tensor]`: The same as `inputs` but on the proper device. """ return self._ensure_tensor_on_device(inputs, self.device) @@ -1253,12 +1253,12 @@ class Pipeline(_ScikitCompat, PushToHubMixin): else: return inputs - def check_model_type(self, supported_models: Union[List[str], dict]): + def check_model_type(self, supported_models: Union[list[str], dict]): """ Check if the model class is in supported by the pipeline. Args: - supported_models (`List[str]` or `dict`): + supported_models (`list[str]` or `dict`): The list of models supported by the pipeline, or a dictionary with model class values. """ if not isinstance(supported_models, list): # Create from a model mapping @@ -1299,7 +1299,7 @@ class Pipeline(_ScikitCompat, PushToHubMixin): raise NotImplementedError("_sanitize_parameters not implemented") @abstractmethod - def preprocess(self, input_: Any, **preprocess_parameters: Dict) -> Dict[str, GenericTensor]: + def preprocess(self, input_: Any, **preprocess_parameters: dict) -> dict[str, GenericTensor]: """ Preprocess will take the `input_` of a specific pipeline and return a dictionary of everything necessary for `_forward` to run properly. It should contain at least one tensor, but might have arbitrary other items. @@ -1307,7 +1307,7 @@ class Pipeline(_ScikitCompat, PushToHubMixin): raise NotImplementedError("preprocess not implemented") @abstractmethod - def _forward(self, input_tensors: Dict[str, GenericTensor], **forward_parameters: Dict) -> ModelOutput: + def _forward(self, input_tensors: dict[str, GenericTensor], **forward_parameters: dict) -> ModelOutput: """ _forward will receive the prepared dictionary from `preprocess` and run it on the model. This method might involve the GPU or the CPU and should be agnostic to it. Isolating this function is the reason for `preprocess` @@ -1320,7 +1320,7 @@ class Pipeline(_ScikitCompat, PushToHubMixin): raise NotImplementedError("_forward not implemented") @abstractmethod - def postprocess(self, model_outputs: ModelOutput, **postprocess_parameters: Dict) -> Any: + def postprocess(self, model_outputs: ModelOutput, **postprocess_parameters: dict) -> Any: """ Postprocess will receive the raw outputs of the `_forward` method, generally tensors, and reformat them into something more friendly. Generally it will output a list or a dict or results (containing just strings and @@ -1491,16 +1491,16 @@ class ChunkPipeline(Pipeline): class PipelineRegistry: - def __init__(self, supported_tasks: Dict[str, Any], task_aliases: Dict[str, str]) -> None: + def __init__(self, supported_tasks: dict[str, Any], task_aliases: dict[str, str]) -> None: self.supported_tasks = supported_tasks self.task_aliases = task_aliases - def get_supported_tasks(self) -> List[str]: + def get_supported_tasks(self) -> list[str]: supported_task = list(self.supported_tasks.keys()) + list(self.task_aliases.keys()) supported_task.sort() return supported_task - def check_task(self, task: str) -> Tuple[str, Dict, Any]: + def check_task(self, task: str) -> tuple[str, dict, Any]: if task in self.task_aliases: task = self.task_aliases[task] if task in self.supported_tasks: @@ -1523,9 +1523,9 @@ class PipelineRegistry: self, task: str, pipeline_class: type, - pt_model: Optional[Union[type, Tuple[type]]] = None, - tf_model: Optional[Union[type, Tuple[type]]] = None, - default: Optional[Dict] = None, + pt_model: Optional[Union[type, tuple[type]]] = None, + tf_model: Optional[Union[type, tuple[type]]] = None, + default: Optional[dict] = None, type: Optional[str] = None, ) -> None: if task in self.supported_tasks: diff --git a/src/transformers/pipelines/depth_estimation.py b/src/transformers/pipelines/depth_estimation.py index 235daf2cc2c..50de4c1ca6d 100644 --- a/src/transformers/pipelines/depth_estimation.py +++ b/src/transformers/pipelines/depth_estimation.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload from ..utils import ( add_end_docstrings, @@ -53,19 +53,19 @@ class DepthEstimationPipeline(Pipeline): self.check_model_type(MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES) @overload - def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> Dict[str, Any]: ... + def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> dict[str, Any]: ... @overload - def __call__(self, inputs: List[Union[str, "Image.Image"]], **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: list[Union[str, "Image.Image"]], **kwargs: Any) -> list[dict[str, Any]]: ... def __call__( - self, inputs: Union[str, List[str], "Image.Image", List["Image.Image"]], **kwargs: Any - ) -> Union[Dict[str, Any], List[Dict[str, Any]]]: + self, inputs: Union[str, list[str], "Image.Image", list["Image.Image"]], **kwargs: Any + ) -> Union[dict[str, Any], list[dict[str, Any]]]: """ Predict the depth(s) of the image(s) passed as inputs. Args: - inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image diff --git a/src/transformers/pipelines/document_question_answering.py b/src/transformers/pipelines/document_question_answering.py index 2d70a3534df..1aa4ab3a500 100644 --- a/src/transformers/pipelines/document_question_answering.py +++ b/src/transformers/pipelines/document_question_answering.py @@ -13,7 +13,7 @@ # limitations under the License. import re -from typing import Any, Dict, List, Optional, Tuple, Union, overload +from typing import Any, Optional, Union, overload import numpy as np @@ -214,23 +214,23 @@ class DocumentQuestionAnsweringPipeline(ChunkPipeline): self, image: Union["Image.Image", str], question: str, - word_boxes: Optional[Tuple[str, List[float]]] = None, + word_boxes: Optional[tuple[str, list[float]]] = None, **kwargs: Any, - ) -> List[Dict[str, Any]]: ... + ) -> list[dict[str, Any]]: ... @overload - def __call__(self, image: Dict[str, Any], **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, image: dict[str, Any], **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, image: List[Dict[str, Any]], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, image: list[dict[str, Any]], **kwargs: Any) -> list[list[dict[str, Any]]]: ... def __call__( self, - image: Union["Image.Image", str, List[Dict[str, Any]]], + image: Union["Image.Image", str, list[dict[str, Any]]], question: Optional[str] = None, - word_boxes: Optional[Tuple[str, List[float]]] = None, + word_boxes: Optional[tuple[str, list[float]]] = None, **kwargs: Any, - ) -> Union[Dict[str, Any], List[Dict[str, Any]]]: + ) -> Union[dict[str, Any], list[dict[str, Any]]]: """ Answer the question(s) given as inputs by using the document(s). A document is defined as an image and an optional list of (word, box) tuples which represent the text in the document. If the `word_boxes` are not @@ -256,7 +256,7 @@ class DocumentQuestionAnsweringPipeline(ChunkPipeline): broadcasted to multiple questions. question (`str`): A question to ask of the document. - word_boxes (`List[str, Tuple[float, float, float, float]]`, *optional*): + word_boxes (`list[str, tuple[float, float, float, float]]`, *optional*): A list of words and bounding boxes (normalized 0->1000). If you provide this optional input, then the pipeline will use these words and boxes instead of running OCR on the image to derive them for models that need them (e.g. LayoutLM). This allows you to reuse OCR'd results across many invocations of the @@ -309,7 +309,7 @@ class DocumentQuestionAnsweringPipeline(ChunkPipeline): padding="do_not_pad", doc_stride=None, max_seq_len=None, - word_boxes: Optional[Tuple[str, List[float]]] = None, + word_boxes: Optional[tuple[str, list[float]]] = None, lang=None, tesseract_config="", timeout=None, diff --git a/src/transformers/pipelines/feature_extraction.py b/src/transformers/pipelines/feature_extraction.py index 70971d83b20..83ea91ee6ae 100644 --- a/src/transformers/pipelines/feature_extraction.py +++ b/src/transformers/pipelines/feature_extraction.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union +from typing import Any, Union from ..utils import add_end_docstrings from .base import GenericTensor, Pipeline, build_pipeline_init_args @@ -56,7 +56,7 @@ class FeatureExtractionPipeline(Pipeline): return preprocess_params, {}, postprocess_params - def preprocess(self, inputs, **tokenize_kwargs) -> Dict[str, GenericTensor]: + def preprocess(self, inputs, **tokenize_kwargs) -> dict[str, GenericTensor]: model_inputs = self.tokenizer(inputs, return_tensors=self.framework, **tokenize_kwargs) return model_inputs @@ -73,12 +73,12 @@ class FeatureExtractionPipeline(Pipeline): elif self.framework == "tf": return model_outputs[0].numpy().tolist() - def __call__(self, *args: Union[str, List[str]], **kwargs: Any) -> Union[Any, List[Any]]: + def __call__(self, *args: Union[str, list[str]], **kwargs: Any) -> Union[Any, list[Any]]: """ Extract the features of the input(s) text. Args: - args (`str` or `List[str]`): One or several texts (or one list of texts) to get the features of. + args (`str` or `list[str]`): One or several texts (or one list of texts) to get the features of. Return: A nested list of `float`: The features computed by the model. diff --git a/src/transformers/pipelines/fill_mask.py b/src/transformers/pipelines/fill_mask.py index 18c225b1c5a..c4076fbaf19 100644 --- a/src/transformers/pipelines/fill_mask.py +++ b/src/transformers/pipelines/fill_mask.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload import numpy as np @@ -24,7 +24,7 @@ logger = logging.get_logger(__name__) r""" top_k (`int`, *optional*, defaults to 5): The number of predictions to return. - targets (`str` or `List[str]`, *optional*): + targets (`str` or `list[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). @@ -113,7 +113,7 @@ class FillMaskPipeline(Pipeline): def preprocess( self, inputs, return_tensors=None, tokenizer_kwargs=None, **preprocess_parameters - ) -> Dict[str, GenericTensor]: + ) -> dict[str, GenericTensor]: if return_tensors is None: return_tensors = self.framework if tokenizer_kwargs is None: @@ -246,21 +246,21 @@ class FillMaskPipeline(Pipeline): return preprocess_params, {}, postprocess_params @overload - def __call__(self, inputs: str, **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: str, **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, inputs: List[str], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, inputs: list[str], **kwargs: Any) -> list[list[dict[str, Any]]]: ... def __call__( - self, inputs: Union[str, List[str]], **kwargs: Any - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + self, inputs: Union[str, list[str]], **kwargs: Any + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Fill the masked token in the text(s) given as inputs. Args: - inputs (`str` or `List[str]`): + inputs (`str` or `list[str]`): One or several texts (or one list of prompts) with masked tokens. - targets (`str` or `List[str]`, *optional*): + targets (`str` or `list[str]`, *optional*): When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower). diff --git a/src/transformers/pipelines/image_classification.py b/src/transformers/pipelines/image_classification.py index bd179a5a667..f1156054c2d 100644 --- a/src/transformers/pipelines/image_classification.py +++ b/src/transformers/pipelines/image_classification.py @@ -11,7 +11,7 @@ # 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. -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload import numpy as np @@ -123,19 +123,19 @@ class ImageClassificationPipeline(Pipeline): return preprocess_params, {}, postprocess_params @overload - def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, inputs: Union[List[str], List["Image.Image"]], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, inputs: Union[list[str], list["Image.Image"]], **kwargs: Any) -> list[list[dict[str, Any]]]: ... def __call__( - self, inputs: Union[str, List[str], "Image.Image", List["Image.Image"]], **kwargs: Any - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + self, inputs: Union[str, list[str], "Image.Image", list["Image.Image"]], **kwargs: Any + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Assign labels to the image(s) passed as inputs. Args: - inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image diff --git a/src/transformers/pipelines/image_feature_extraction.py b/src/transformers/pipelines/image_feature_extraction.py index ef8d6e6a00a..1333bf6fb8f 100644 --- a/src/transformers/pipelines/image_feature_extraction.py +++ b/src/transformers/pipelines/image_feature_extraction.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union +from typing import Any, Union from ..utils import add_end_docstrings, is_vision_available from .base import GenericTensor, Pipeline, build_pipeline_init_args @@ -59,7 +59,7 @@ class ImageFeatureExtractionPipeline(Pipeline): return preprocess_params, {}, postprocess_params - def preprocess(self, image, timeout=None, **image_processor_kwargs) -> Dict[str, GenericTensor]: + def preprocess(self, image, timeout=None, **image_processor_kwargs) -> dict[str, GenericTensor]: image = load_image(image, timeout=timeout) model_inputs = self.image_processor(image, return_tensors=self.framework, **image_processor_kwargs) if self.framework == "pt": @@ -90,12 +90,12 @@ class ImageFeatureExtractionPipeline(Pipeline): elif self.framework == "tf": return outputs.numpy().tolist() - def __call__(self, *args: Union[str, "Image.Image", List["Image.Image"], List[str]], **kwargs: Any) -> List[Any]: + def __call__(self, *args: Union[str, "Image.Image", list["Image.Image"], list[str]], **kwargs: Any) -> list[Any]: """ Extract the features of the input(s). Args: - images (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + images (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image diff --git a/src/transformers/pipelines/image_segmentation.py b/src/transformers/pipelines/image_segmentation.py index ef596e2f533..8947dcf8cba 100644 --- a/src/transformers/pipelines/image_segmentation.py +++ b/src/transformers/pipelines/image_segmentation.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload import numpy as np @@ -91,19 +91,19 @@ class ImageSegmentationPipeline(Pipeline): return preprocess_kwargs, {}, postprocess_kwargs @overload - def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, inputs: Union[List[str], List["Image.Image"]], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, inputs: Union[list[str], list["Image.Image"]], **kwargs: Any) -> list[list[dict[str, Any]]]: ... def __call__( - self, inputs: Union[str, "Image.Image", List[str], List["Image.Image"]], **kwargs: Any - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + self, inputs: Union[str, "Image.Image", list[str], list["Image.Image"]], **kwargs: Any + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Perform segmentation (detect masks & classes) in the image(s) passed as inputs. Args: - inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing an HTTP(S) link pointing to an image diff --git a/src/transformers/pipelines/image_text_to_text.py b/src/transformers/pipelines/image_text_to_text.py index 23f914d48c1..b205e63b05d 100644 --- a/src/transformers/pipelines/image_text_to_text.py +++ b/src/transformers/pipelines/image_text_to_text.py @@ -15,7 +15,7 @@ import enum from collections.abc import Iterable # pylint: disable=g-importing-member -from typing import Any, Dict, List, Optional, Union, overload +from typing import Any, Optional, Union, overload from ..generation import GenerationConfig from ..processing_utils import ProcessingKwargs, Unpack @@ -56,7 +56,7 @@ class Chat: actually a batch of samples rather than messages in the same conversation.""" def __init__( - self, messages: Dict, images: Optional[Union[str, List[str], "Image.Image", List["Image.Image"]]] = None + self, messages: dict, images: Optional[Union[str, list[str], "Image.Image", list["Image.Image"]]] = None ): for message in messages: if not ("role" in message and "content" in message): @@ -67,7 +67,7 @@ class Chat: def add_images_to_messages( - messages: dict, images: Optional[Union[str, List[str], "Image.Image", List["Image.Image"]]] + messages: dict, images: Optional[Union[str, list[str], "Image.Image", list["Image.Image"]]] ): """ Retrieve and combine images from the chat and the images passed as input. @@ -257,37 +257,37 @@ class ImageTextToTextPipeline(Pipeline): image: Optional[Union[str, "Image.Image"]] = None, text: Optional[str] = None, **kwargs: Any, - ) -> List[Dict[str, Any]]: ... + ) -> list[dict[str, Any]]: ... @overload def __call__( self, - image: Optional[Union[List[str], List["Image.Image"]]] = None, - text: Optional[List[str]] = None, + image: Optional[Union[list[str], list["Image.Image"]]] = None, + text: Optional[list[str]] = None, **kwargs: Any, - ) -> List[List[Dict[str, Any]]]: ... + ) -> list[list[dict[str, Any]]]: ... def __call__( self, images: Optional[ Union[ str, - List[str], - List[List[str]], + list[str], + list[list[str]], "Image.Image", - List["Image.Image"], - List[List["Image.Image"]], - List[dict], + list["Image.Image"], + list[list["Image.Image"]], + list[dict], ] ] = None, - text: Optional[Union[str, List[str], List[dict]]] = None, + text: Optional[Union[str, list[str], list[dict]]] = None, **kwargs, - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Generate a text given text and the image(s) passed as inputs. Args: - images (`str`, `List[str]`, `PIL.Image, `List[PIL.Image]`, `List[Dict[str, Union[str, PIL.Image]]]`): + images (`str`, `list[str]`, `PIL.Image, `list[PIL.Image]`, `list[dict[str, Union[str, PIL.Image]]]`): The pipeline handles three types of images: - A string containing a HTTP(s) link pointing to an image @@ -296,7 +296,7 @@ class ImageTextToTextPipeline(Pipeline): The pipeline accepts either a single image or a batch of images. Finally, this pipeline also supports the chat format (see `text`) containing images and text in this argument. - text (str, List[str], `List[Dict[str, Union[str, PIL.Image]]]`): + text (str, list[str], `list[dict[str, Union[str, PIL.Image]]]`): The text to be used for generation. If a list of strings is passed, the length of the list should be the same as the number of images. Text can also follow the chat format: a list of dictionaries where each dictionary represents a message in a conversation. Each dictionary should have two keys: 'role' diff --git a/src/transformers/pipelines/image_to_image.py b/src/transformers/pipelines/image_to_image.py index 23fe98ef9e9..a16c2b4d6ba 100644 --- a/src/transformers/pipelines/image_to_image.py +++ b/src/transformers/pipelines/image_to_image.py @@ -11,7 +11,7 @@ # 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. -from typing import Any, List, Union, overload +from typing import Any, Union, overload import numpy as np @@ -88,16 +88,16 @@ class ImageToImagePipeline(Pipeline): def __call__(self, images: Union[str, "Image.Image"], **kwargs: Any) -> "Image.Image": ... @overload - def __call__(self, images: Union[List[str], List["Image.Image"]], **kwargs: Any) -> List["Image.Image"]: ... + def __call__(self, images: Union[list[str], list["Image.Image"]], **kwargs: Any) -> list["Image.Image"]: ... def __call__( - self, images: Union[str, List[str], "Image.Image", List["Image.Image"]], **kwargs: Any - ) -> Union["Image.Image", List["Image.Image"]]: + self, images: Union[str, list[str], "Image.Image", list["Image.Image"]], **kwargs: Any + ) -> Union["Image.Image", list["Image.Image"]]: """ Transform the image(s) passed as inputs. Args: - images (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + images (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image @@ -112,7 +112,7 @@ class ImageToImagePipeline(Pipeline): the call may block forever. Return: - An image (Image.Image) or a list of images (List["Image.Image"]) containing result(s). If the input is a + An image (Image.Image) or a list of images (list["Image.Image"]) containing result(s). If the input is a single image, the return will be also a single image, if the input is a list of several images, it will return a list of transformed images. """ diff --git a/src/transformers/pipelines/image_to_text.py b/src/transformers/pipelines/image_to_text.py index 62942fb96d8..1ad25a7c26d 100644 --- a/src/transformers/pipelines/image_to_text.py +++ b/src/transformers/pipelines/image_to_text.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload from ..generation import GenerationConfig from ..utils import ( @@ -112,17 +112,17 @@ class ImageToTextPipeline(Pipeline): return preprocess_params, forward_params, {} @overload - def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: Union[str, "Image.Image"], **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, inputs: Union[List[str], List["Image.Image"]], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, inputs: Union[list[str], list["Image.Image"]], **kwargs: Any) -> list[list[dict[str, Any]]]: ... - def __call__(self, inputs: Union[str, List[str], "Image.Image", List["Image.Image"]], **kwargs): + def __call__(self, inputs: Union[str, list[str], "Image.Image", list["Image.Image"]], **kwargs): """ Assign labels to the image(s) passed as inputs. Args: - inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a HTTP(s) link pointing to an image diff --git a/src/transformers/pipelines/mask_generation.py b/src/transformers/pipelines/mask_generation.py index 5f32b8df8ad..31b168a6f66 100644 --- a/src/transformers/pipelines/mask_generation.py +++ b/src/transformers/pipelines/mask_generation.py @@ -1,5 +1,5 @@ from collections import defaultdict -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, overload +from typing import TYPE_CHECKING, Any, Optional, Union, overload from ..image_utils import load_image from ..utils import ( @@ -129,16 +129,16 @@ class MaskGenerationPipeline(ChunkPipeline): return preprocess_kwargs, forward_params, postprocess_kwargs @overload - def __call__(self, image: Union[str, "Image.Image"], *args: Any, **kwargs: Any) -> Dict[str, Any]: ... + def __call__(self, image: Union[str, "Image.Image"], *args: Any, **kwargs: Any) -> dict[str, Any]: ... @overload def __call__( - self, image: Union[List[str], List["Image.Image"]], *args: Any, **kwargs: Any - ) -> List[Dict[str, Any]]: ... + self, image: Union[list[str], list["Image.Image"]], *args: Any, **kwargs: Any + ) -> list[dict[str, Any]]: ... def __call__( - self, image: Union[str, "Image.Image", List[str], List["Image.Image"]], *args: Any, **kwargs: Any - ) -> Union[Dict[str, Any], List[Dict[str, Any]]]: + self, image: Union[str, "Image.Image", list[str], list["Image.Image"]], *args: Any, **kwargs: Any + ) -> Union[dict[str, Any], list[dict[str, Any]]]: """ Generates binary segmentation masks diff --git a/src/transformers/pipelines/object_detection.py b/src/transformers/pipelines/object_detection.py index 3b67ee49a08..868b9ad2ea7 100644 --- a/src/transformers/pipelines/object_detection.py +++ b/src/transformers/pipelines/object_detection.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any, Dict, List, Union, overload +from typing import TYPE_CHECKING, Any, Union, overload from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import Pipeline, build_pipeline_init_args @@ -69,19 +69,19 @@ class ObjectDetectionPipeline(Pipeline): return preprocess_params, {}, postprocess_kwargs @overload - def __call__(self, image: Union[str, "Image.Image"], *args: Any, **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, image: Union[str, "Image.Image"], *args: Any, **kwargs: Any) -> list[dict[str, Any]]: ... @overload def __call__( - self, image: Union[List[str], List["Image.Image"]], *args: Any, **kwargs: Any - ) -> List[List[Dict[str, Any]]]: ... + self, image: Union[list[str], list["Image.Image"]], *args: Any, **kwargs: Any + ) -> list[list[dict[str, Any]]]: ... - def __call__(self, *args, **kwargs) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + def __call__(self, *args, **kwargs) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Detect objects (bounding boxes & classes) in the image(s) passed as inputs. Args: - inputs (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing an HTTP(S) link pointing to an image @@ -105,7 +105,7 @@ class ObjectDetectionPipeline(Pipeline): - **label** (`str`) -- The class label identified by the model. - **score** (`float`) -- The score attributed by the model for that label. - - **box** (`List[Dict[str, int]]`) -- The bounding box of detected object in image's original size. + - **box** (`list[dict[str, int]]`) -- The bounding box of detected object in image's original size. """ # After deprecation of this is completed, remove the default `None` value for `images` if "images" in kwargs and "inputs" not in kwargs: @@ -176,7 +176,7 @@ class ObjectDetectionPipeline(Pipeline): return annotation - def _get_bounding_box(self, box: "torch.Tensor") -> Dict[str, int]: + def _get_bounding_box(self, box: "torch.Tensor") -> dict[str, int]: """ Turns list [xmin, xmax, ymin, ymax] into dict { "xmin": xmin, ... } @@ -184,7 +184,7 @@ class ObjectDetectionPipeline(Pipeline): box (`torch.Tensor`): Tensor containing the coordinates in corners format. Returns: - bbox (`Dict[str, int]`): Dict containing the coordinates in corners format. + bbox (`dict[str, int]`): Dict containing the coordinates in corners format. """ if self.framework != "pt": raise ValueError("The ObjectDetectionPipeline is only available in PyTorch.") diff --git a/src/transformers/pipelines/question_answering.py b/src/transformers/pipelines/question_answering.py index 4e4fc2fc279..12a0d09a00e 100644 --- a/src/transformers/pipelines/question_answering.py +++ b/src/transformers/pipelines/question_answering.py @@ -2,7 +2,7 @@ import inspect import types import warnings from collections.abc import Iterable -from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union +from typing import TYPE_CHECKING, Optional, Union import numpy as np @@ -45,7 +45,7 @@ if is_torch_available(): def decode_spans( start: np.ndarray, end: np.ndarray, topk: int, max_answer_len: int, undesired_tokens: np.ndarray -) -> Tuple: +) -> tuple: """ Take the output of any `ModelForQuestionAnswering` and will generate probabilities for each span to be the actual answer. @@ -285,8 +285,8 @@ class QuestionAnsweringPipeline(ChunkPipeline): @staticmethod def create_sample( - question: Union[str, List[str]], context: Union[str, List[str]] - ) -> Union[SquadExample, List[SquadExample]]: + question: Union[str, list[str]], context: Union[str, list[str]] + ) -> Union[SquadExample, list[SquadExample]]: """ QuestionAnsweringPipeline leverages the [`SquadExample`] internally. This helper method encapsulate all the logic for converting question(s) and context(s) to [`SquadExample`]. @@ -294,8 +294,8 @@ class QuestionAnsweringPipeline(ChunkPipeline): We currently support extractive question answering. Arguments: - question (`str` or `List[str]`): The question(s) asked. - context (`str` or `List[str]`): The context(s) in which we will look for the answer. + question (`str` or `list[str]`): The question(s) asked. + context (`str` or `list[str]`): The context(s) in which we will look for the answer. Returns: One or a list of [`SquadExample`]: The corresponding [`SquadExample`] grouping question and context. @@ -352,9 +352,9 @@ class QuestionAnsweringPipeline(ChunkPipeline): Answer the question(s) given as inputs by using the context(s). Args: - question (`str` or `List[str]`): + question (`str` or `list[str]`): One or several question(s) (must be used in conjunction with the `context` argument). - context (`str` or `List[str]`): + context (`str` or `list[str]`): One or several context(s) associated with the question(s) (must be used in conjunction with the `question` argument). top_k (`int`, *optional*, defaults to 1): @@ -624,7 +624,7 @@ class QuestionAnsweringPipeline(ChunkPipeline): return answers[0] return answers - def get_answer(self, answers: List[Dict], target: str) -> Optional[Dict]: + def get_answer(self, answers: list[dict], target: str) -> Optional[dict]: for answer in answers: if answer["answer"].lower() == target.lower(): return answer @@ -632,7 +632,7 @@ class QuestionAnsweringPipeline(ChunkPipeline): def get_indices( self, enc: "tokenizers.Encoding", s: int, e: int, sequence_index: int, align_to_words: bool - ) -> Tuple[int, int]: + ) -> tuple[int, int]: if align_to_words: try: start_word = enc.token_to_word(s) @@ -648,7 +648,7 @@ class QuestionAnsweringPipeline(ChunkPipeline): end_index = enc.offsets[e][1] return start_index, end_index - def span_to_answer(self, text: str, start: int, end: int) -> Dict[str, Union[str, int]]: + def span_to_answer(self, text: str, start: int, end: int) -> dict[str, Union[str, int]]: """ When decoding from token probabilities, this method maps token indexes to actual word in the initial context. diff --git a/src/transformers/pipelines/table_question_answering.py b/src/transformers/pipelines/table_question_answering.py index fa241fcd53a..4e941c4b6ed 100644 --- a/src/transformers/pipelines/table_question_answering.py +++ b/src/transformers/pipelines/table_question_answering.py @@ -38,9 +38,9 @@ class TableQuestionAnsweringArgumentHandler(ArgumentHandler): def __call__(self, table=None, query=None, **kwargs): # Returns tqa_pipeline_inputs of shape: # [ - # {"table": pd.DataFrame, "query": List[str]}, + # {"table": pd.DataFrame, "query": list[str]}, # ..., - # {"table": pd.DataFrame, "query" : List[str]} + # {"table": pd.DataFrame, "query" : list[str]} # ] requires_backends(self, "pandas") import pandas as pd @@ -316,7 +316,7 @@ class TableQuestionAnsweringPipeline(Pipeline): table (`pd.DataFrame` or `Dict`): Pandas DataFrame or dictionary that will be converted to a DataFrame containing all the table values. See above for an example of dictionary. - query (`str` or `List[str]`): + query (`str` or `list[str]`): Query or list of queries that will be sent to the model alongside the table. sequential (`bool`, *optional*, defaults to `False`): Whether to do inference sequentially or as a batch. Batching is faster, but models like SQA require the @@ -348,8 +348,8 @@ class TableQuestionAnsweringPipeline(Pipeline): - **answer** (`str`) -- The answer of the query given the table. If there is an aggregator, the answer will be preceded by `AGGREGATOR >`. - - **coordinates** (`List[Tuple[int, int]]`) -- Coordinates of the cells of the answers. - - **cells** (`List[str]`) -- List of strings made up of the answer cell values. + - **coordinates** (`list[tuple[int, int]]`) -- Coordinates of the cells of the answers. + - **cells** (`list[str]`) -- List of strings made up of the answer cell values. - **aggregator** (`str`) -- If the model has an aggregator, this returns the aggregator. """ pipeline_inputs = self._args_parser(*args, **kwargs) diff --git a/src/transformers/pipelines/text2text_generation.py b/src/transformers/pipelines/text2text_generation.py index 7bd2e47195e..8800a91bbb4 100644 --- a/src/transformers/pipelines/text2text_generation.py +++ b/src/transformers/pipelines/text2text_generation.py @@ -1,6 +1,6 @@ import enum import warnings -from typing import Any, Dict, List, Union +from typing import Any, Union from ..generation import GenerationConfig from ..tokenization_utils import TruncationStrategy @@ -155,12 +155,12 @@ class Text2TextGenerationPipeline(Pipeline): del inputs["token_type_ids"] return inputs - def __call__(self, *args: Union[str, List[str]], **kwargs: Any) -> List[Dict[str, str]]: + def __call__(self, *args: Union[str, list[str]], **kwargs: Any) -> list[dict[str, str]]: r""" Generate the output text(s) using text(s) given as inputs. Args: - args (`str` or `List[str]`): + args (`str` or `list[str]`): Input text for the encoder. return_tensors (`bool`, *optional*, defaults to `False`): Whether or not to include the tensors of predictions (as token indices) in the outputs. @@ -277,7 +277,7 @@ class SummarizationPipeline(Text2TextGenerationPipeline): Summarize the text(s) given as inputs. Args: - documents (*str* or `List[str]`): + documents (*str* or `list[str]`): One or several articles (or one list of articles) to summarize. return_text (`bool`, *optional*, defaults to `True`): Whether or not to include the decoded texts in the outputs @@ -378,7 +378,7 @@ class TranslationPipeline(Text2TextGenerationPipeline): Translate the text(s) given as inputs. Args: - args (`str` or `List[str]`): + args (`str` or `list[str]`): Texts to be translated. return_tensors (`bool`, *optional*, defaults to `False`): Whether or not to include the tensors of predictions (as token indices) in the outputs. diff --git a/src/transformers/pipelines/text_classification.py b/src/transformers/pipelines/text_classification.py index a50174e8c95..4ea0fc8b658 100644 --- a/src/transformers/pipelines/text_classification.py +++ b/src/transformers/pipelines/text_classification.py @@ -1,6 +1,6 @@ import inspect import warnings -from typing import Any, Dict, List, Union +from typing import Any, Union import numpy as np @@ -122,14 +122,14 @@ class TextClassificationPipeline(Pipeline): def __call__( self, - inputs: Union[str, List[str], Dict[str, str], List[Dict[str, str]]], + inputs: Union[str, list[str], dict[str, str], list[dict[str, str]]], **kwargs: Any, - ) -> List[Dict[str, Any]]: + ) -> list[dict[str, Any]]: """ Classify the text(s) given as inputs. Args: - inputs (`str` or `List[str]` or `Dict[str]`, or `List[Dict[str]]`): + inputs (`str` or `list[str]` or `dict[str]`, or `list[dict[str]]`): One or several texts to classify. In order to use text pairs for your classification, you can send a dictionary containing `{"text", "text_pair"}` keys, or a list of those. top_k (`int`, *optional*, defaults to `1`): @@ -169,7 +169,7 @@ class TextClassificationPipeline(Pipeline): else: return result - def preprocess(self, inputs, **tokenizer_kwargs) -> Dict[str, GenericTensor]: + def preprocess(self, inputs, **tokenizer_kwargs) -> dict[str, GenericTensor]: return_tensors = self.framework if isinstance(inputs, dict): return self.tokenizer(**inputs, return_tensors=return_tensors, **tokenizer_kwargs) diff --git a/src/transformers/pipelines/text_generation.py b/src/transformers/pipelines/text_generation.py index e9812a26af8..3de9a87c0f2 100644 --- a/src/transformers/pipelines/text_generation.py +++ b/src/transformers/pipelines/text_generation.py @@ -1,7 +1,7 @@ import enum import itertools import types -from typing import Any, Dict, List, overload +from typing import Any, overload from ..generation import GenerationConfig from ..utils import ModelOutput, add_end_docstrings, is_tf_available, is_torch_available @@ -19,7 +19,7 @@ if is_tf_available(): from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_CAUSAL_LM_MAPPING_NAMES -ChatType = List[Dict[str, str]] +ChatType = list[dict[str, str]] class ReturnType(enum.Enum): @@ -33,7 +33,7 @@ class Chat: to this format because the rest of the pipeline code tends to assume that lists of messages are actually a batch of samples rather than messages in the same conversation.""" - def __init__(self, messages: Dict): + def __init__(self, messages: dict): for message in messages: if not ("role" in message and "content" in message): raise ValueError("When passing chat dicts as input, each dict must have a 'role' and 'content' key.") @@ -234,23 +234,23 @@ class TextGenerationPipeline(Pipeline): return super()._parse_and_tokenize(*args, **kwargs) @overload - def __call__(self, text_inputs: str, **kwargs: Any) -> List[Dict[str, str]]: ... + def __call__(self, text_inputs: str, **kwargs: Any) -> list[dict[str, str]]: ... @overload - def __call__(self, text_inputs: List[str], **kwargs: Any) -> List[List[Dict[str, str]]]: ... + def __call__(self, text_inputs: list[str], **kwargs: Any) -> list[list[dict[str, str]]]: ... @overload - def __call__(self, text_inputs: ChatType, **kwargs: Any) -> List[Dict[str, ChatType]]: ... + def __call__(self, text_inputs: ChatType, **kwargs: Any) -> list[dict[str, ChatType]]: ... @overload - def __call__(self, text_inputs: List[ChatType], **kwargs: Any) -> List[List[Dict[str, ChatType]]]: ... + def __call__(self, text_inputs: list[ChatType], **kwargs: Any) -> list[list[dict[str, ChatType]]]: ... def __call__(self, text_inputs, **kwargs): """ Complete the prompt(s) given as inputs. Args: - text_inputs (`str`, `List[str]`, List[Dict[str, str]], or `List[List[Dict[str, str]]]`): + text_inputs (`str`, `list[str]`, list[dict[str, str]], or `list[list[dict[str, str]]]`): One or several prompts (or one list of prompts) to complete. If strings or a list of string are passed, this pipeline will continue each prompt. Alternatively, a "chat", in the form of a list of dicts with "role" and "content" keys, can be passed, or a list of such chats. When chats are passed, diff --git a/src/transformers/pipelines/text_to_audio.py b/src/transformers/pipelines/text_to_audio.py index e9ca2a74a6d..79b0e9b35f3 100644 --- a/src/transformers/pipelines/text_to_audio.py +++ b/src/transformers/pipelines/text_to_audio.py @@ -11,7 +11,7 @@ # 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.from typing import List, Union -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload from ..generation import GenerationConfig from ..utils import is_torch_available @@ -174,19 +174,19 @@ class TextToAudioPipeline(Pipeline): return output @overload - def __call__(self, text_inputs: str, **forward_params: Any) -> Dict[str, Any]: ... + def __call__(self, text_inputs: str, **forward_params: Any) -> dict[str, Any]: ... @overload - def __call__(self, text_inputs: List[str], **forward_params: Any) -> List[Dict[str, Any]]: ... + def __call__(self, text_inputs: list[str], **forward_params: Any) -> list[dict[str, Any]]: ... def __call__( - self, text_inputs: Union[str, List[str]], **forward_params - ) -> Union[Dict[str, Any], List[Dict[str, Any]]]: + self, text_inputs: Union[str, list[str]], **forward_params + ) -> Union[dict[str, Any], list[dict[str, Any]]]: """ Generates speech/audio from the inputs. See the [`TextToAudioPipeline`] documentation for more information. Args: - text_inputs (`str` or `List[str]`): + text_inputs (`str` or `list[str]`): The text(s) to generate. forward_params (`dict`, *optional*): Parameters passed to the model generation/forward method. `forward_params` are always passed to the diff --git a/src/transformers/pipelines/token_classification.py b/src/transformers/pipelines/token_classification.py index e8fded5530c..e976ee9b187 100644 --- a/src/transformers/pipelines/token_classification.py +++ b/src/transformers/pipelines/token_classification.py @@ -1,6 +1,6 @@ import types import warnings -from typing import Any, Dict, List, Optional, Tuple, Union, overload +from typing import Any, Optional, Union, overload import numpy as np @@ -29,7 +29,7 @@ class TokenClassificationArgumentHandler(ArgumentHandler): Handles arguments for token classification. """ - def __call__(self, inputs: Union[str, List[str]], **kwargs): + def __call__(self, inputs: Union[str, list[str]], **kwargs): if inputs is not None and isinstance(inputs, (list, tuple)) and len(inputs) > 0: inputs = list(inputs) batch_size = len(inputs) @@ -63,7 +63,7 @@ class AggregationStrategy(ExplicitEnum): @add_end_docstrings( build_pipeline_init_args(has_tokenizer=True), r""" - ignore_labels (`List[str]`, defaults to `["O"]`): + ignore_labels (`list[str]`, defaults to `["O"]`): A list of labels to ignore. grouped_entities (`bool`, *optional*, defaults to `False`): DEPRECATED, use `aggregation_strategy` instead. Whether or not to group the tokens corresponding to the @@ -150,7 +150,7 @@ class TokenClassificationPipeline(ChunkPipeline): grouped_entities: Optional[bool] = None, ignore_subwords: Optional[bool] = None, aggregation_strategy: Optional[AggregationStrategy] = None, - offset_mapping: Optional[List[Tuple[int, int]]] = None, + offset_mapping: Optional[list[tuple[int, int]]] = None, stride: Optional[int] = None, ): preprocess_params = {} @@ -218,19 +218,19 @@ class TokenClassificationPipeline(ChunkPipeline): return preprocess_params, {}, postprocess_params @overload - def __call__(self, inputs: str, **kwargs: Any) -> List[Dict[str, str]]: ... + def __call__(self, inputs: str, **kwargs: Any) -> list[dict[str, str]]: ... @overload - def __call__(self, inputs: List[str], **kwargs: Any) -> List[List[Dict[str, str]]]: ... + def __call__(self, inputs: list[str], **kwargs: Any) -> list[list[dict[str, str]]]: ... def __call__( - self, inputs: Union[str, List[str]], **kwargs: Any - ) -> Union[List[Dict[str, str]], List[List[Dict[str, str]]]]: + self, inputs: Union[str, list[str]], **kwargs: Any + ) -> Union[list[dict[str, str]], list[list[dict[str, str]]]]: """ Classify each token of the text(s) given as inputs. Args: - inputs (`str` or `List[str]`): + inputs (`str` or `list[str]`): One or several texts (or one list of texts) for token classification. Return: @@ -371,10 +371,10 @@ class TokenClassificationPipeline(ChunkPipeline): sentence: str, input_ids: np.ndarray, scores: np.ndarray, - offset_mapping: Optional[List[Tuple[int, int]]], + offset_mapping: Optional[list[tuple[int, int]]], special_tokens_mask: np.ndarray, aggregation_strategy: AggregationStrategy, - ) -> List[dict]: + ) -> list[dict]: """Fuse various numpy arrays into dicts with all the information needed for aggregation""" pre_entities = [] for idx, token_scores in enumerate(scores): @@ -428,7 +428,7 @@ class TokenClassificationPipeline(ChunkPipeline): pre_entities.append(pre_entity) return pre_entities - def aggregate(self, pre_entities: List[dict], aggregation_strategy: AggregationStrategy) -> List[dict]: + def aggregate(self, pre_entities: list[dict], aggregation_strategy: AggregationStrategy) -> list[dict]: if aggregation_strategy in {AggregationStrategy.NONE, AggregationStrategy.SIMPLE}: entities = [] for pre_entity in pre_entities: @@ -450,7 +450,7 @@ class TokenClassificationPipeline(ChunkPipeline): return entities return self.group_entities(entities) - def aggregate_word(self, entities: List[dict], aggregation_strategy: AggregationStrategy) -> dict: + def aggregate_word(self, entities: list[dict], aggregation_strategy: AggregationStrategy) -> dict: word = self.tokenizer.convert_tokens_to_string([entity["word"] for entity in entities]) if aggregation_strategy == AggregationStrategy.FIRST: scores = entities[0]["scores"] @@ -480,7 +480,7 @@ class TokenClassificationPipeline(ChunkPipeline): } return new_entity - def aggregate_words(self, entities: List[dict], aggregation_strategy: AggregationStrategy) -> List[dict]: + def aggregate_words(self, entities: list[dict], aggregation_strategy: AggregationStrategy) -> list[dict]: """ Override tokens from a given word that disagree to force agreement on word boundaries. @@ -508,7 +508,7 @@ class TokenClassificationPipeline(ChunkPipeline): word_entities.append(self.aggregate_word(word_group, aggregation_strategy)) return word_entities - def group_sub_entities(self, entities: List[dict]) -> dict: + def group_sub_entities(self, entities: list[dict]) -> dict: """ Group together the adjacent tokens with the same entity predicted. @@ -529,7 +529,7 @@ class TokenClassificationPipeline(ChunkPipeline): } return entity_group - def get_tag(self, entity_name: str) -> Tuple[str, str]: + def get_tag(self, entity_name: str) -> tuple[str, str]: if entity_name.startswith("B-"): bi = "B" tag = entity_name[2:] @@ -543,7 +543,7 @@ class TokenClassificationPipeline(ChunkPipeline): tag = entity_name return bi, tag - def group_entities(self, entities: List[dict]) -> List[dict]: + def group_entities(self, entities: list[dict]) -> list[dict]: """ Find and group together the adjacent tokens with the same entity predicted. diff --git a/src/transformers/pipelines/video_classification.py b/src/transformers/pipelines/video_classification.py index dffc21fed2d..761e2151287 100644 --- a/src/transformers/pipelines/video_classification.py +++ b/src/transformers/pipelines/video_classification.py @@ -13,7 +13,7 @@ # limitations under the License. import warnings from io import BytesIO -from typing import Any, Dict, List, Optional, Union, overload +from typing import Any, Optional, Union, overload import requests @@ -78,17 +78,17 @@ class VideoClassificationPipeline(Pipeline): return preprocess_params, {}, postprocess_params @overload - def __call__(self, inputs: str, **kwargs: Any) -> List[Dict[str, Any]]: ... + def __call__(self, inputs: str, **kwargs: Any) -> list[dict[str, Any]]: ... @overload - def __call__(self, inputs: List[str], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, inputs: list[str], **kwargs: Any) -> list[list[dict[str, Any]]]: ... - def __call__(self, inputs: Optional[Union[str, List[str]]] = None, **kwargs): + def __call__(self, inputs: Optional[Union[str, list[str]]] = None, **kwargs): """ Assign labels to the video(s) passed as inputs. Args: - inputs (`str`, `List[str]`): + inputs (`str`, `list[str]`): The pipeline handles three types of videos: - A string containing a http link pointing to a video diff --git a/src/transformers/pipelines/visual_question_answering.py b/src/transformers/pipelines/visual_question_answering.py index a5c90124868..1df9986ce4d 100644 --- a/src/transformers/pipelines/visual_question_answering.py +++ b/src/transformers/pipelines/visual_question_answering.py @@ -1,4 +1,4 @@ -from typing import List, Optional, Union +from typing import Optional, Union from ..generation import GenerationConfig from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging @@ -89,8 +89,8 @@ class VisualQuestionAnsweringPipeline(Pipeline): def __call__( self, - image: Union["Image.Image", str, List["Image.Image"], List[str], "KeyDataset"], - question: Optional[Union[str, List[str]]] = None, + image: Union["Image.Image", str, list["Image.Image"], list[str], "KeyDataset"], + question: Optional[Union[str, list[str]]] = None, **kwargs, ): r""" @@ -103,7 +103,7 @@ class VisualQuestionAnsweringPipeline(Pipeline): - `pipeline([{"image": image, "question": question}, {"image": image, "question": question}])` Args: - image (`str`, `List[str]`, `PIL.Image`, `List[PIL.Image]` or `KeyDataset`): + image (`str`, `list[str]`, `PIL.Image`, `list[PIL.Image]` or `KeyDataset`): The pipeline handles three types of images: - A string containing a http link pointing to an image @@ -122,7 +122,7 @@ class VisualQuestionAnsweringPipeline(Pipeline): >>> oracle(image=KeyDataset(dataset, "image"), question="What's in this image?") ``` - question (`str`, `List[str]`): + question (`str`, `list[str]`): The question(s) asked. If given a single question, it can be broadcasted to multiple images. If multiple images and questions are given, each and every question will be broadcasted to all images (same effect as a Cartesian product) diff --git a/src/transformers/pipelines/zero_shot_audio_classification.py b/src/transformers/pipelines/zero_shot_audio_classification.py index a21e74e2092..f554dfb5fc0 100644 --- a/src/transformers/pipelines/zero_shot_audio_classification.py +++ b/src/transformers/pipelines/zero_shot_audio_classification.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from collections import UserDict -from typing import Any, Dict, List, Union +from typing import Any, Union import numpy as np import requests @@ -67,17 +67,17 @@ class ZeroShotAudioClassificationPipeline(Pipeline): raise ValueError(f"The {self.__class__} is only available in PyTorch.") # No specific FOR_XXX available yet - def __call__(self, audios: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> List[Dict[str, Any]]: + def __call__(self, audios: Union[np.ndarray, bytes, str, dict], **kwargs: Any) -> list[dict[str, Any]]: """ Assign labels to the audio(s) passed as inputs. Args: - audios (`str`, `List[str]`, `np.array` or `List[np.array]`): + audios (`str`, `list[str]`, `np.array` or `list[np.array]`): The pipeline handles three types of inputs: - A string containing a http link pointing to an audio - A string containing a local path to an audio - An audio loaded in numpy - candidate_labels (`List[str]`): + candidate_labels (`list[str]`): The candidate labels for this audio. They will be formatted using *hypothesis_template*. hypothesis_template (`str`, *optional*, defaults to `"This is a sound of {}"`): The format used in conjunction with *candidate_labels* to attempt the audio classification by diff --git a/src/transformers/pipelines/zero_shot_classification.py b/src/transformers/pipelines/zero_shot_classification.py index f4aee3341e3..42773df2ad1 100644 --- a/src/transformers/pipelines/zero_shot_classification.py +++ b/src/transformers/pipelines/zero_shot_classification.py @@ -1,5 +1,5 @@ import inspect -from typing import List, Union +from typing import Union import numpy as np @@ -162,7 +162,7 @@ class ZeroShotClassificationPipeline(ChunkPipeline): def __call__( self, - sequences: Union[str, List[str]], + sequences: Union[str, list[str]], *args, **kwargs, ): @@ -171,9 +171,9 @@ class ZeroShotClassificationPipeline(ChunkPipeline): information. Args: - sequences (`str` or `List[str]`): + sequences (`str` or `list[str]`): The sequence(s) to classify, will be truncated if the model input is too large. - candidate_labels (`str` or `List[str]`): + candidate_labels (`str` or `list[str]`): The set of possible class labels to classify each sequence into. Can be a single label, a string of comma-separated labels, or a list of labels. hypothesis_template (`str`, *optional*, defaults to `"This example is {}."`): @@ -193,8 +193,8 @@ class ZeroShotClassificationPipeline(ChunkPipeline): A `dict` or a list of `dict`: Each result comes as a dictionary with the following keys: - **sequence** (`str`) -- The sequence for which this is the output. - - **labels** (`List[str]`) -- The labels sorted by order of likelihood. - - **scores** (`List[float]`) -- The probabilities for each of the labels. + - **labels** (`list[str]`) -- The labels sorted by order of likelihood. + - **scores** (`list[float]`) -- The probabilities for each of the labels. """ if len(args) == 0: pass diff --git a/src/transformers/pipelines/zero_shot_image_classification.py b/src/transformers/pipelines/zero_shot_image_classification.py index bae5333735b..2629185ba31 100644 --- a/src/transformers/pipelines/zero_shot_image_classification.py +++ b/src/transformers/pipelines/zero_shot_image_classification.py @@ -1,6 +1,6 @@ import warnings from collections import UserDict -from typing import Any, Dict, List, Union, overload +from typing import Any, Union, overload from ..utils import ( add_end_docstrings, @@ -76,32 +76,32 @@ class ZeroShotImageClassificationPipeline(Pipeline): @overload def __call__( - self, image: Union[str, "Image.Image"], candidate_labels: List[str], **kwargs: Any - ) -> List[Dict[str, Any]]: ... + self, image: Union[str, "Image.Image"], candidate_labels: list[str], **kwargs: Any + ) -> list[dict[str, Any]]: ... @overload def __call__( - self, image: Union[List[str], List["Image.Image"]], candidate_labels: List[str], **kwargs: Any - ) -> List[List[Dict[str, Any]]]: ... + self, image: Union[list[str], list["Image.Image"]], candidate_labels: list[str], **kwargs: Any + ) -> list[list[dict[str, Any]]]: ... def __call__( self, - image: Union[str, List[str], "Image.Image", List["Image.Image"]], - candidate_labels: List[str], + image: Union[str, list[str], "Image.Image", list["Image.Image"]], + candidate_labels: list[str], **kwargs: Any, - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Assign labels to the image(s) passed as inputs. Args: - image (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`): + image (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image - A string containing a local path to an image - An image loaded in PIL directly - candidate_labels (`List[str]`): + candidate_labels (`list[str]`): The candidate labels for this image. They will be formatted using *hypothesis_template*. hypothesis_template (`str`, *optional*, defaults to `"This is a photo of {}"`): diff --git a/src/transformers/pipelines/zero_shot_object_detection.py b/src/transformers/pipelines/zero_shot_object_detection.py index 2a92b8d152d..024b9035631 100644 --- a/src/transformers/pipelines/zero_shot_object_detection.py +++ b/src/transformers/pipelines/zero_shot_object_detection.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Optional, Union, overload +from typing import Any, Optional, Union, overload from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends from .base import ChunkPipeline, build_pipeline_init_args @@ -64,23 +64,23 @@ class ZeroShotObjectDetectionPipeline(ChunkPipeline): @overload def __call__( - self, image: Union[str, "Image.Image"], candidate_labels: Union[str, List[str]], **kwargs: Any - ) -> List[Dict[str, Any]]: ... + self, image: Union[str, "Image.Image"], candidate_labels: Union[str, list[str]], **kwargs: Any + ) -> list[dict[str, Any]]: ... @overload - def __call__(self, image: List[Dict[str, Any]], **kwargs: Any) -> List[List[Dict[str, Any]]]: ... + def __call__(self, image: list[dict[str, Any]], **kwargs: Any) -> list[list[dict[str, Any]]]: ... def __call__( self, - image: Union[str, "Image.Image", List[Dict[str, Any]]], - candidate_labels: Optional[Union[str, List[str]]] = None, + image: Union[str, "Image.Image", list[dict[str, Any]]], + candidate_labels: Optional[Union[str, list[str]]] = None, **kwargs: Any, - ) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]: + ) -> Union[list[dict[str, Any]], list[list[dict[str, Any]]]]: """ Detect objects (bounding boxes & classes) in the image(s) passed as inputs. Args: - image (`str`, `PIL.Image` or `List[Dict[str, Any]]`): + image (`str`, `PIL.Image` or `list[dict[str, Any]]`): The pipeline handles three types of images: - A string containing an http url pointing to an image @@ -109,7 +109,7 @@ class ZeroShotObjectDetectionPipeline(ChunkPipeline): ``` - candidate_labels (`str` or `List[str]` or `List[List[str]]`): + candidate_labels (`str` or `list[str]` or `list[list[str]]`): What the model should recognize in the image. threshold (`float`, *optional*, defaults to 0.1): @@ -130,7 +130,7 @@ class ZeroShotObjectDetectionPipeline(ChunkPipeline): - **label** (`str`) -- Text query corresponding to the found object. - **score** (`float`) -- Score corresponding to the object (between 0 and 1). - - **box** (`Dict[str,int]`) -- Bounding box of the detected object in image's original size. It is a + - **box** (`dict[str,int]`) -- Bounding box of the detected object in image's original size. It is a dictionary with `x_min`, `x_max`, `y_min`, `y_max` keys. """ if "text_queries" in kwargs: @@ -221,7 +221,7 @@ class ZeroShotObjectDetectionPipeline(ChunkPipeline): return results - def _get_bounding_box(self, box: "torch.Tensor") -> Dict[str, int]: + def _get_bounding_box(self, box: "torch.Tensor") -> dict[str, int]: """ Turns list [xmin, xmax, ymin, ymax] into dict { "xmin": xmin, ... } @@ -229,7 +229,7 @@ class ZeroShotObjectDetectionPipeline(ChunkPipeline): box (`torch.Tensor`): Tensor containing the coordinates in corners format. Returns: - bbox (`Dict[str, int]`): Dict containing the coordinates in corners format. + bbox (`dict[str, int]`): Dict containing the coordinates in corners format. """ if self.framework != "pt": raise ValueError("The ZeroShotObjectDetectionPipeline is only available in PyTorch.") diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index 02ea9b4210f..e7adab4b1d4 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -24,7 +24,7 @@ import typing import warnings from dataclasses import dataclass from pathlib import Path -from typing import Any, Dict, Optional, TypedDict, Union +from typing import Any, Optional, TypedDict, Union import numpy as np import typing_extensions @@ -155,11 +155,11 @@ class ImagesKwargs(TypedDict, total=False): Attributes: do_resize (`bool`, *optional*): Whether to resize the image. - size (`Dict[str, int]`, *optional*): + size (`dict[str, int]`, *optional*): Resize the shorter side of the input to `size["shortest_edge"]`. size_divisor (`int`, *optional*): The size by which to make sure both the height and width can be divided. - crop_size (`Dict[str, int]`, *optional*): + crop_size (`dict[str, int]`, *optional*): Desired output size when applying center-cropping. resample (`PILImageResampling`, *optional*): Resampling filter to use if resizing the image. @@ -169,13 +169,13 @@ class ImagesKwargs(TypedDict, total=False): Scale factor to use if rescaling the image. do_normalize (`bool`, *optional*): Whether to normalize the image. - image_mean (`float` or `List[float]`, *optional*): + image_mean (`float` or `list[float]`, *optional*): Mean to use if normalizing the image. - image_std (`float` or `List[float]`, *optional*): + image_std (`float` or `list[float]`, *optional*): Standard deviation to use if normalizing the image. do_pad (`bool`, *optional*): Whether to pad the image to the `(max_height, max_width)` of the images in the batch. - pad_size (`Dict[str, int]`, *optional*): + pad_size (`dict[str, int]`, *optional*): The size `{"height": int, "width" int}` to pad the images to. do_center_crop (`bool`, *optional*): Whether to center crop the image. @@ -190,7 +190,7 @@ class ImagesKwargs(TypedDict, total=False): do_resize: Optional[bool] size: Optional[dict[str, int]] size_divisor: Optional[int] - crop_size: Optional[Dict[str, int]] + crop_size: Optional[dict[str, int]] resample: Optional[Union["PILImageResampling", int]] do_rescale: Optional[bool] rescale_factor: Optional[float] @@ -214,7 +214,7 @@ class VideosKwargs(TypedDict, total=False): Whether to convert the video to RGB fromat. do_resize (`bool`): Whether to resize the video. - size (`Dict[str, int]`, *optional*): + size (`dict[str, int]`, *optional*): Resize the shorter side of the input to `size["shortest_edge"]`. default_to_square (`bool`, *optional*, defaults to `self.default_to_square`): Whether to default to a square when resizing, if size is an int. @@ -228,9 +228,9 @@ class VideosKwargs(TypedDict, total=False): Scale factor to use if rescaling the video. do_normalize (`bool`, *optional*): Whether to normalize the video. - image_mean (`float` or `List[float]`, *optional*): + image_mean (`float` or `list[float]`, *optional*): Mean to use if normalizing the video. - image_std (`float` or `List[float]`, *optional*): + image_std (`float` or `list[float]`, *optional*): Standard deviation to use if normalizing the video. do_pad (`bool`, *optional*): Whether to pad the video to the `(max_height, max_width)` of the videos in the batch. @@ -244,7 +244,7 @@ class VideosKwargs(TypedDict, total=False): Maximum number of frames to sample when `do_sample_frames=True`. fps (`int`, *optional*): Target frames to sample per second when `do_sample_frames=True`. - crop_size (`Dict[str, int]`, *optional*): + crop_size (`dict[str, int]`, *optional*): Desired output size when applying center-cropping. data_format (`ChannelDimension` or `str`, *optional*): The channel dimension format for the output video. @@ -265,7 +265,7 @@ class VideosKwargs(TypedDict, total=False): image_std: Optional[Union[float, list[float]]] do_pad: Optional[bool] do_center_crop: Optional[bool] - crop_size: Optional[Dict[str, int]] + crop_size: Optional[dict[str, int]] data_format: Optional[ChannelDimension] input_data_format: Optional[Union[str, ChannelDimension]] device: Optional[str] @@ -282,7 +282,7 @@ class AudioKwargs(TypedDict, total=False): Attributes: sampling_rate (`int`, *optional*): The sampling rate at which the `raw_speech` input was sampled. - raw_speech (`np.ndarray`, `List[float]`, `List[np.ndarray]`, `List[List[float]]`): + raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`): The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not stereo, i.e. single float per timestep. @@ -377,13 +377,13 @@ class TokenizerChatTemplateKwargs(TypedDict, total=False): """ Keyword arguments for tokenizer's `apply_chat_template`, when it is called from within a processor. - tools (`List[Dict]`, *optional*): + tools (`list[Dict]`, *optional*): A list of tools (callable functions) that will be accessible to the model. If the template does not support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, giving the name, description and argument types for the tool. See our [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) for more information. - documents (`List[Dict[str, str]]`, *optional*): + documents (`list[dict[str, str]]`, *optional*): A list of dicts representing documents that will be accessible to the model if it is performing RAG (retrieval-augmented generation). If the template does not support RAG, this argument will have no effect. We recommend that each document should be a dict containing "title" and "text" keys. Please @@ -550,7 +550,7 @@ class ProcessorMixin(PushToHubMixin): Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this processor instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this processor instance. """ output = copy.deepcopy(self.__dict__) @@ -635,7 +635,7 @@ class ProcessorMixin(PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -772,7 +772,7 @@ class ProcessorMixin(PushToHubMixin): specify the folder name here. Returns: - `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the processor object. + `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the processor object. """ cache_dir = kwargs.pop("cache_dir", None) force_download = kwargs.pop("force_download", False) @@ -981,11 +981,11 @@ class ProcessorMixin(PushToHubMixin): Instantiates a type of [`~processing_utils.ProcessingMixin`] from a Python dictionary of parameters. Args: - processor_dict (`Dict[str, Any]`): + processor_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the processor object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [`~processing_utils.ProcessingMixin.to_dict`] method. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the processor object. Returns: @@ -1405,7 +1405,7 @@ class ProcessorMixin(PushToHubMixin): ] Args: - conversation (`Union[List[Dict, [str, str]], List[List[Dict[str, str]]]]`): + conversation (`Union[list[Dict, [str, str]], list[list[dict[str, str]]]]`): The conversation to format. chat_template (`Optional[str]`, *optional*): The Jinja template to use for formatting the conversation. If not provided, the tokenizer's @@ -1607,7 +1607,7 @@ class ProcessorMixin(PushToHubMixin): Additional arguments to be passed to the tokenizer's `batch_decode method`. Returns: - `List[str]`: The decoded text. + `list[str]`: The decoded text. """ return self.tokenizer.batch_decode(generated_outputs, skip_special_tokens=skip_special_tokens, **kwargs) diff --git a/src/transformers/pytorch_utils.py b/src/transformers/pytorch_utils.py index 9bb02bff963..e4bd74720a8 100644 --- a/src/transformers/pytorch_utils.py +++ b/src/transformers/pytorch_utils.py @@ -192,7 +192,7 @@ def apply_chunking_to_forward( The chunk size of a chunked tensor: `num_chunks = len(input_tensors[0]) / chunk_size`. chunk_dim (`int`): The dimension over which the `input_tensors` should be chunked. - input_tensors (`Tuple[torch.Tensor]`): + input_tensors (`tuple[torch.Tensor]`): The input tensors of `forward_fn` which will be chunked Returns: @@ -257,13 +257,13 @@ def find_pruneable_heads_and_indices( Finds the heads and their indices taking `already_pruned_heads` into account. Args: - heads (`List[int]`): List of the indices of heads to prune. + heads (`list[int]`): List of the indices of heads to prune. n_heads (`int`): The number of heads in the model. head_size (`int`): The size of each head. already_pruned_heads (`Set[int]`): A set of already pruned heads. Returns: - `Tuple[Set[int], torch.LongTensor]`: A tuple with the indices of heads to prune taking `already_pruned_heads` + `tuple[Set[int], torch.LongTensor]`: A tuple with the indices of heads to prune taking `already_pruned_heads` into account and the indices of rows/columns to keep in the layer weight. """ mask = torch.ones(n_heads, head_size) diff --git a/src/transformers/quantizers/auto.py b/src/transformers/quantizers/auto.py index 97933a818e4..b332550b424 100644 --- a/src/transformers/quantizers/auto.py +++ b/src/transformers/quantizers/auto.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import warnings -from typing import Dict, Optional, Union +from typing import Optional, Union from ..models.auto.configuration_auto import AutoConfig from ..utils import logging @@ -111,7 +111,7 @@ class AutoQuantizationConfig: """ @classmethod - def from_dict(cls, quantization_config_dict: Dict): + def from_dict(cls, quantization_config_dict: dict): quant_method = quantization_config_dict.get("quant_method", None) # We need a special care for bnb models to make sure everything is BC .. if quantization_config_dict.get("load_in_8bit", False) or quantization_config_dict.get("load_in_4bit", False): @@ -152,7 +152,7 @@ class AutoHfQuantizer: """ @classmethod - def from_config(cls, quantization_config: Union[QuantizationConfigMixin, Dict], **kwargs): + def from_config(cls, quantization_config: Union[QuantizationConfigMixin, dict], **kwargs): # Convert it to a QuantizationConfig if the q_config is a dict if isinstance(quantization_config, dict): quantization_config = AutoQuantizationConfig.from_dict(quantization_config) diff --git a/src/transformers/quantizers/base.py b/src/transformers/quantizers/base.py index 2818b9d26e4..d54c5f29662 100644 --- a/src/transformers/quantizers/base.py +++ b/src/transformers/quantizers/base.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from abc import ABC, abstractmethod -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from ..utils import is_torch_available from ..utils.quantization_config import QuantizationConfigMixin, QuantizationMethod @@ -38,9 +38,9 @@ class HfQuantizer(ABC): Attributes quantization_config (`transformers.utils.quantization_config.QuantizationConfigMixin`): The quantization config that defines the quantization parameters of your model that you want to quantize. - modules_to_not_convert (`List[str]`, *optional*): + modules_to_not_convert (`list[str]`, *optional*): The list of module names to not convert when quantizing the model. - required_packages (`List[str]`, *optional*): + required_packages (`list[str]`, *optional*): The list of required pip packages to install prior to using the quantizer requires_calibration (`bool`): Whether the quantization method requires to calibrate the model before using it. @@ -79,7 +79,7 @@ class HfQuantizer(ABC): """ return torch_dtype - def update_device_map(self, device_map: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]: + def update_device_map(self, device_map: Optional[dict[str, Any]]) -> Optional[dict[str, Any]]: """ Override this method if you want to pass a override the existing device map with a new one. E.g. for bitsandbytes, since `accelerate` is a hard requirement, if no device_map is @@ -103,50 +103,50 @@ class HfQuantizer(ABC): """ return torch_dtype - def update_missing_keys(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys(self, model, missing_keys: list[str], prefix: str) -> list[str]: """ Override this method if you want to adjust the `missing_keys`. Args: - missing_keys (`List[str]`, *optional*): + missing_keys (`list[str]`, *optional*): The list of missing keys in the checkpoint compared to the state dict of the model """ return missing_keys - def update_unexpected_keys(self, model, unexpected_keys: List[str], prefix: str) -> List[str]: + def update_unexpected_keys(self, model, unexpected_keys: list[str], prefix: str) -> list[str]: """ Override this method if you want to adjust the `unexpected_keys`. Args: - unexpected_keys (`List[str]`, *optional*): + unexpected_keys (`list[str]`, *optional*): The list of unexpected keys in the checkpoint compared to the state dict of the model """ return unexpected_keys - def update_missing_keys_after_loading(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys_after_loading(self, model, missing_keys: list[str], prefix: str) -> list[str]: """ Override this method if you want to adjust the `missing_keys` after loading the model params, but before the model is post-processed. Args: - missing_keys (`List[str]`, *optional*): + missing_keys (`list[str]`, *optional*): The list of missing keys in the checkpoint compared to the state dict of the model """ return missing_keys - def update_expected_keys(self, model, expected_keys: List[str], loaded_keys: List[str]) -> List[str]: + def update_expected_keys(self, model, expected_keys: list[str], loaded_keys: list[str]) -> list[str]: """ Override this method if you want to adjust the `update_expected_keys`. Args: - expected_keys (`List[str]`, *optional*): + expected_keys (`list[str]`, *optional*): The list of the expected keys in the initialized model. - loaded_keys (`List[str]`, *optional*): + loaded_keys (`list[str]`, *optional*): The list of the loaded keys in the checkpoint. """ return expected_keys - def get_special_dtypes_update(self, model, torch_dtype: "torch.dtype") -> Dict[str, "torch.dtype"]: + def get_special_dtypes_update(self, model, torch_dtype: "torch.dtype") -> dict[str, "torch.dtype"]: """ returns dtypes for modules that are not quantized - used for the computation of the device_map in case one passes a str as a device_map. The method will use the `modules_to_not_convert` that is modified @@ -165,7 +165,7 @@ class HfQuantizer(ABC): if any(m in name for m in self.modules_to_not_convert) } - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: """adjust max_memory argument for infer_auto_device_map() if extra memory is needed for quantization""" return max_memory @@ -174,7 +174,7 @@ class HfQuantizer(ABC): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: """ @@ -271,8 +271,8 @@ class HfQuantizer(ABC): @staticmethod def get_modules_to_not_convert( model: "PreTrainedModel", - skip_modules: Optional[List[str]] = None, - keep_in_fp32_modules: Optional[List[str]] = None, + skip_modules: Optional[list[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, add_default_skips: bool = False, ): from ..integrations import get_keys_to_not_convert diff --git a/src/transformers/quantizers/quantizer_awq.py b/src/transformers/quantizers/quantizer_awq.py index 9d69c62997a..5f674ad0690 100644 --- a/src/transformers/quantizers/quantizer_awq.py +++ b/src/transformers/quantizers/quantizer_awq.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import importlib.metadata -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, Optional from packaging import version @@ -100,7 +100,7 @@ class AwqQuantizer(HfQuantizer): return torch_dtype def _process_model_before_weight_loading( - self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[List[str]] = None, **kwargs + self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[list[str]] = None, **kwargs ): from ..integrations import replace_quantization_scales, replace_with_awq_linear diff --git a/src/transformers/quantizers/quantizer_bitnet.py b/src/transformers/quantizers/quantizer_bitnet.py index ae808433663..90cbab4303a 100644 --- a/src/transformers/quantizers/quantizer_bitnet.py +++ b/src/transformers/quantizers/quantizer_bitnet.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Optional, Union from .base import HfQuantizer @@ -81,7 +81,7 @@ class BitNetHfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations import replace_with_bitnet_linear @@ -97,7 +97,7 @@ class BitNetHfQuantizer(HfQuantizer): pre_quantized=self.pre_quantized, ) - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: max_memory = {key: val * 0.90 for key, val in max_memory.items()} return max_memory diff --git a/src/transformers/quantizers/quantizer_bnb_4bit.py b/src/transformers/quantizers/quantizer_bnb_4bit.py index b25d61cdb49..409c5d47b0b 100644 --- a/src/transformers/quantizers/quantizer_bnb_4bit.py +++ b/src/transformers/quantizers/quantizer_bnb_4bit.py @@ -13,7 +13,7 @@ # limitations under the License. import importlib from functools import cached_property -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from packaging import version @@ -143,7 +143,7 @@ class Bnb4BitHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: import bitsandbytes as bnb @@ -165,8 +165,8 @@ class Bnb4BitHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): """ combines logic from _load_state_dict_into_meta_model and .integrations.bitsandbytes.py::set_module_quantized_tensor_to_device() @@ -252,7 +252,7 @@ class Bnb4BitHfQuantizer(HfQuantizer): module._parameters[tensor_name] = new_value # Copied from transformers.quantizers.quantizer_bnb_8bit.Bnb8BitHfQuantizer.adjust_max_memory - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: # need more space for buffers that are created during quantization max_memory = {key: val * 0.90 for key, val in max_memory.items()} return max_memory @@ -295,7 +295,7 @@ class Bnb4BitHfQuantizer(HfQuantizer): self, model: "PreTrainedModel", device_map, - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations import replace_with_bnb_linear diff --git a/src/transformers/quantizers/quantizer_bnb_8bit.py b/src/transformers/quantizers/quantizer_bnb_8bit.py index f88035459f8..e1391426ec7 100644 --- a/src/transformers/quantizers/quantizer_bnb_8bit.py +++ b/src/transformers/quantizers/quantizer_bnb_8bit.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import importlib -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from packaging import version @@ -126,7 +126,7 @@ class Bnb8BitHfQuantizer(HfQuantizer): " make sure you have the latest version of `bitsandbytes` installed" ) - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: # need more space for buffers that are created during quantization max_memory = {key: val * 0.90 for key, val in max_memory.items()} return max_memory @@ -169,7 +169,7 @@ class Bnb8BitHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ): import bitsandbytes as bnb @@ -192,8 +192,8 @@ class Bnb8BitHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): """ combines logic from _load_state_dict_into_meta_model and .integrations.bitsandbytes.py::set_module_quantized_tensor_to_device() @@ -258,7 +258,7 @@ class Bnb8BitHfQuantizer(HfQuantizer): self, model: "PreTrainedModel", device_map, - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations import replace_with_bnb_linear diff --git a/src/transformers/quantizers/quantizer_compressed_tensors.py b/src/transformers/quantizers/quantizer_compressed_tensors.py index 7aa120f00c6..7df4fb7ca90 100644 --- a/src/transformers/quantizers/quantizer_compressed_tensors.py +++ b/src/transformers/quantizers/quantizer_compressed_tensors.py @@ -15,7 +15,6 @@ import os import re -from typing import List from ..utils import is_compressed_tensors_available, is_torch_available, logging from ..utils.quantization_config import CompressedTensorsConfig @@ -56,7 +55,7 @@ class CompressedTensorsHfQuantizer(HfQuantizer): self.run_compressed = quantization_config.run_compressed self.quantization_config = quantization_config - def update_missing_keys_after_loading(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys_after_loading(self, model, missing_keys: list[str], prefix: str) -> list[str]: """ Update missing keys after loading the model. This is necessary for compressed tensors to load the model correctly. We expect weights to be present in missing keys. @@ -78,12 +77,12 @@ class CompressedTensorsHfQuantizer(HfQuantizer): key for key in missing_keys if not any(re.match(f".*{pattern}", key) for pattern in expected_missing_keys) ] - def update_unexpected_keys(self, model, unexpected_keys: List[str], prefix: str) -> List[str]: + def update_unexpected_keys(self, model, unexpected_keys: list[str], prefix: str) -> list[str]: """ Override this method if you want to adjust the `unexpected_keys`. Args: - unexpected_keys (`List[str]`, *optional*): + unexpected_keys (`list[str]`, *optional*): The list of unexpected keys in the checkpoint compared to the state dict of the model """ diff --git a/src/transformers/quantizers/quantizer_eetq.py b/src/transformers/quantizers/quantizer_eetq.py index 988f90789ac..61c93909007 100644 --- a/src/transformers/quantizers/quantizer_eetq.py +++ b/src/transformers/quantizers/quantizer_eetq.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Optional from .base import HfQuantizer @@ -111,7 +111,7 @@ class EetqHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ): from eetq import EetqLinear @@ -135,8 +135,8 @@ class EetqHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): """ quantizes weights into qweight and weight_scales @@ -155,7 +155,7 @@ class EetqHfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations import replace_with_eetq_linear diff --git a/src/transformers/quantizers/quantizer_fbgemm_fp8.py b/src/transformers/quantizers/quantizer_fbgemm_fp8.py index 7015d73be14..003ea515b31 100644 --- a/src/transformers/quantizers/quantizer_fbgemm_fp8.py +++ b/src/transformers/quantizers/quantizer_fbgemm_fp8.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Optional from .base import HfQuantizer @@ -110,7 +110,7 @@ class FbgemmFp8HfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ): from ..integrations import FbgemmFp8Linear, FbgemmFp8Llama4TextExperts @@ -141,8 +141,8 @@ class FbgemmFp8HfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): """ Quantizes weights into weight and weight_scale @@ -204,7 +204,7 @@ class FbgemmFp8HfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations import replace_with_fbgemm_fp8_linear @@ -226,7 +226,7 @@ class FbgemmFp8HfQuantizer(HfQuantizer): model.config.quantization_config = self.quantization_config - def update_missing_keys(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys(self, model, missing_keys: list[str], prefix: str) -> list[str]: from ..integrations import FbgemmFp8Linear, FbgemmFp8Llama4TextExperts not_missing_keys = [] diff --git a/src/transformers/quantizers/quantizer_finegrained_fp8.py b/src/transformers/quantizers/quantizer_finegrained_fp8.py index ed87b70bbc9..1a2ab58d983 100644 --- a/src/transformers/quantizers/quantizer_finegrained_fp8.py +++ b/src/transformers/quantizers/quantizer_finegrained_fp8.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Optional from ..utils import is_accelerate_available, is_torch_available, is_torch_xpu_available, logging from .base import HfQuantizer @@ -86,8 +86,8 @@ class FineGrainedFP8HfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): """ Quantizes weights to FP8 format using Block-wise quantization @@ -139,7 +139,7 @@ class FineGrainedFP8HfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ): from ..integrations.finegrained_fp8 import FP8Linear @@ -160,7 +160,7 @@ class FineGrainedFP8HfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): from ..integrations.finegrained_fp8 import replace_with_fp8_linear @@ -180,7 +180,7 @@ class FineGrainedFP8HfQuantizer(HfQuantizer): def _process_model_after_weight_loading(self, model: "PreTrainedModel", **kwargs): return model - def update_missing_keys(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys(self, model, missing_keys: list[str], prefix: str) -> list[str]: from ..integrations import FP8Linear not_missing_keys = [] diff --git a/src/transformers/quantizers/quantizer_higgs.py b/src/transformers/quantizers/quantizer_higgs.py index 83c102f16c3..b0ec31e4765 100644 --- a/src/transformers/quantizers/quantizer_higgs.py +++ b/src/transformers/quantizers/quantizer_higgs.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, Any, Dict, List, Optional +from typing import TYPE_CHECKING, Any, Optional from ..utils.logging import tqdm from .base import HfQuantizer @@ -87,8 +87,8 @@ class HiggsHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: Optional[List[str]] = None, + state_dict: dict[str, Any], + unexpected_keys: Optional[list[str]] = None, ): from ..integrations import quantize_with_higgs @@ -158,7 +158,7 @@ class HiggsHfQuantizer(HfQuantizer): ) self.quantization_config.tune_metadata[name] = module.tune_metadata.to_dict() - def update_missing_keys(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys(self, model, missing_keys: list[str], prefix: str) -> list[str]: from ..integrations import HiggsLinear higgs_names = {name for name, module in model.named_modules() if isinstance(module, HiggsLinear)} @@ -183,7 +183,7 @@ class HiggsHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: from ..integrations import HiggsLinear diff --git a/src/transformers/quantizers/quantizer_hqq.py b/src/transformers/quantizers/quantizer_hqq.py index 07dda2588cb..a0cc0170e18 100755 --- a/src/transformers/quantizers/quantizer_hqq.py +++ b/src/transformers/quantizers/quantizer_hqq.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import TYPE_CHECKING, Any, Dict, List +from typing import TYPE_CHECKING, Any from ..integrations import prepare_for_hqq_linear from ..utils import is_accelerate_available, is_hqq_available, is_torch_available, logging @@ -92,8 +92,8 @@ class HqqHfQuantizer(HfQuantizer): self.using_multi_gpu = len(set(device_map.values())) > 1 def update_missing_keys( - self, model: "PreTrainedModel", missing_keys: List[str], prefix: str, **kwargs - ) -> List[str]: + self, model: "PreTrainedModel", missing_keys: list[str], prefix: str, **kwargs + ) -> list[str]: if self.pre_quantized: return [key for key in missing_keys if ("weight" not in key)] else: @@ -101,8 +101,8 @@ class HqqHfQuantizer(HfQuantizer): # Adds missing keys for HQQLinear modules that are loaded but the model with initialized with torch.nn.Linear def update_expected_keys( - self, model: "PreTrainedModel", expected_keys: List[str], loaded_keys: List[str] - ) -> List[str]: + self, model: "PreTrainedModel", expected_keys: list[str], loaded_keys: list[str] + ) -> list[str]: if not self.pre_quantized: return expected_keys @@ -166,7 +166,7 @@ class HqqHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: if is_hqq_available(): @@ -190,8 +190,8 @@ class HqqHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: List[str], + state_dict: dict[str, Any], + unexpected_keys: list[str], ): """ Each nn.Linear layer is processed here. diff --git a/src/transformers/quantizers/quantizer_quanto.py b/src/transformers/quantizers/quantizer_quanto.py index be760f0d430..2ecf52351ec 100644 --- a/src/transformers/quantizers/quantizer_quanto.py +++ b/src/transformers/quantizers/quantizer_quanto.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import importlib -from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union +from typing import TYPE_CHECKING, Any, Optional, Union from packaging import version @@ -87,7 +87,7 @@ class QuantoHfQuantizer(HfQuantizer): torch_dtype = torch.float32 return torch_dtype - def update_missing_keys(self, model, missing_keys: List[str], prefix: str) -> List[str]: + def update_missing_keys(self, model, missing_keys: list[str], prefix: str) -> list[str]: if is_optimum_quanto_available(): from optimum.quanto import QModuleMixin @@ -108,7 +108,7 @@ class QuantoHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: """ @@ -134,7 +134,7 @@ class QuantoHfQuantizer(HfQuantizer): else: return False - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: max_memory = {key: val * 0.90 for key, val in max_memory.items()} return max_memory @@ -177,7 +177,7 @@ class QuantoHfQuantizer(HfQuantizer): ) def _process_model_before_weight_loading( - self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[List[str]] = None, **kwargs + self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[list[str]] = None, **kwargs ): from ..integrations import replace_with_quanto_layers diff --git a/src/transformers/quantizers/quantizer_quark.py b/src/transformers/quantizers/quantizer_quark.py index 374360b1cb8..0a14a01ee45 100644 --- a/src/transformers/quantizers/quantizer_quark.py +++ b/src/transformers/quantizers/quantizer_quark.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import TYPE_CHECKING, Any, Dict +from typing import TYPE_CHECKING, Any from ..file_utils import is_torch_available from .base import HfQuantizer @@ -87,7 +87,7 @@ class QuarkHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: return True diff --git a/src/transformers/quantizers/quantizer_spqr.py b/src/transformers/quantizers/quantizer_spqr.py index 4cf1193edbf..e8dbeed0df3 100644 --- a/src/transformers/quantizers/quantizer_spqr.py +++ b/src/transformers/quantizers/quantizer_spqr.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, Optional from .base import HfQuantizer @@ -65,7 +65,7 @@ class SpQRHfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): self.modules_to_not_convert = self.get_modules_to_not_convert( diff --git a/src/transformers/quantizers/quantizer_torchao.py b/src/transformers/quantizers/quantizer_torchao.py index 22b2a88ee4d..c0b337b5666 100644 --- a/src/transformers/quantizers/quantizer_torchao.py +++ b/src/transformers/quantizers/quantizer_torchao.py @@ -25,7 +25,7 @@ from .quantizers_utils import get_module_from_name if TYPE_CHECKING: from ..modeling_utils import PreTrainedModel -from typing import Any, Dict, List +from typing import Any from ..utils import is_torch_available, is_torchao_available, logging from ..utils.quantization_config import TorchAoConfig @@ -174,13 +174,13 @@ class TorchAoHfQuantizer(HfQuantizer): "`pip install --upgrade accelerate`" ) - def adjust_max_memory(self, max_memory: Dict[str, Union[int, str]]) -> Dict[str, Union[int, str]]: + def adjust_max_memory(self, max_memory: dict[str, Union[int, str]]) -> dict[str, Union[int, str]]: # need more space for the quantization parameters (e.g. scale). Tested with int4 wo and group size = 128 max_memory = {key: val * 0.9 for key, val in max_memory.items()} return max_memory def _process_model_before_weight_loading( - self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[List[str]] = None, **kwargs + self, model: "PreTrainedModel", keep_in_fp32_modules: Optional[list[str]] = None, **kwargs ): self.modules_to_not_convert = self.get_modules_to_not_convert( model, self.quantization_config.modules_to_not_convert, keep_in_fp32_modules @@ -200,7 +200,7 @@ class TorchAoHfQuantizer(HfQuantizer): model: "PreTrainedModel", param_value: "torch.Tensor", param_name: str, - state_dict: Dict[str, Any], + state_dict: dict[str, Any], **kwargs, ) -> bool: if self.quantization_config.quant_type == "autoquant": @@ -227,8 +227,8 @@ class TorchAoHfQuantizer(HfQuantizer): param_value: "torch.Tensor", param_name: str, target_device: "torch.device", - state_dict: Dict[str, Any], - unexpected_keys: List[str], + state_dict: dict[str, Any], + unexpected_keys: list[str], ): """ Each nn.Linear layer that needs to be quantized is processed here. diff --git a/src/transformers/quantizers/quantizer_vptq.py b/src/transformers/quantizers/quantizer_vptq.py index 85483357448..23ef6fa3f4a 100644 --- a/src/transformers/quantizers/quantizer_vptq.py +++ b/src/transformers/quantizers/quantizer_vptq.py @@ -11,7 +11,7 @@ # 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. -from typing import TYPE_CHECKING, List, Optional +from typing import TYPE_CHECKING, Optional from .base import HfQuantizer @@ -68,7 +68,7 @@ class VptqHfQuantizer(HfQuantizer): def _process_model_before_weight_loading( self, model: "PreTrainedModel", - keep_in_fp32_modules: Optional[List[str]] = None, + keep_in_fp32_modules: Optional[list[str]] = None, **kwargs, ): """ diff --git a/src/transformers/quantizers/quantizers_utils.py b/src/transformers/quantizers/quantizers_utils.py index bbed8317049..94955495f56 100644 --- a/src/transformers/quantizers/quantizers_utils.py +++ b/src/transformers/quantizers/quantizers_utils.py @@ -11,10 +11,10 @@ # 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. -from typing import Any, Tuple +from typing import Any -def get_module_from_name(module, tensor_name: str) -> Tuple[Any, str]: +def get_module_from_name(module, tensor_name: str) -> tuple[Any, str]: if "." in tensor_name: module_name, tensor_name = tensor_name.rsplit(".", 1) module = module.get_submodule(module_name) diff --git a/src/transformers/tf_utils.py b/src/transformers/tf_utils.py index c3770cb1237..11d07f8d7ed 100644 --- a/src/transformers/tf_utils.py +++ b/src/transformers/tf_utils.py @@ -33,7 +33,7 @@ def shape_list(tensor: Union[tf.Tensor, np.ndarray]) -> list[int]: tensor (`tf.Tensor` or `np.ndarray`): The tensor we want the shape of. Returns: - `List[int]`: The shape of the tensor as a list. + `list[int]`: The shape of the tensor as a list. """ if isinstance(tensor, np.ndarray): return list(tensor.shape) diff --git a/src/transformers/tokenization_utils.py b/src/transformers/tokenization_utils.py index 51382d776f0..b20a41e2cb0 100644 --- a/src/transformers/tokenization_utils.py +++ b/src/transformers/tokenization_utils.py @@ -467,7 +467,7 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): Returns the added tokens in the vocabulary as a dictionary of index to AddedToken. Returns: - `Dict[str, int]`: The added tokens. + `dict[str, int]`: The added tokens. """ return dict(sorted(self._added_tokens_decoder.items(), key=lambda item: item[0])) @@ -491,7 +491,7 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): something we should change. Returns: - `Dict[str, int]`: The added tokens. + `dict[str, int]`: The added tokens. """ return self._added_tokens_encoder @@ -516,7 +516,7 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): vocab which is why they have to be handled specifically. Args: - new_tokens (`List[str]`or `List[tokenizers.AddedToken]`): + new_tokens (`list[str]`or `list[tokenizers.AddedToken]`): Token(s) to add in vocabulary. A token is counted as added if it's not already in the vocabulary (tested by checking if the tokenizer assign the index of the `unk_token` to them). If a token is part of the vocabulary then we simply mark this token as an `AddedToken` which allows to control the @@ -632,7 +632,7 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): Passed along to the model-specific `prepare_for_tokenization` preprocessing method. Returns: - `List[str]`: The list of tokens. + `list[str]`: The list of tokens. """ split_special_tokens = kwargs.pop("split_special_tokens", self.split_special_tokens) @@ -713,10 +713,10 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): vocabulary. Args: - tokens (`str` or `List[str]`): One or several token(s) to convert to token id(s). + tokens (`str` or `list[str]`): One or several token(s) to convert to token id(s). Returns: - `int` or `List[int]`: The token id or list of token ids. + `int` or `list[int]`: The token id or list of token ids. """ if tokens is None: return None @@ -995,11 +995,11 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): Whether or not the input is already pre-tokenized (e.g., split into words). If set to `True`, the tokenizer assumes the input is already split into words (for instance, by splitting it on whitespace) which it will tokenize. This is useful for NER or token classification. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Keyword arguments to use for the tokenization. Returns: - `Tuple[str, Dict[str, Any]]`: The prepared text and the unused kwargs. + `tuple[str, dict[str, Any]]`: The prepared text and the unused kwargs. """ return (text, kwargs) @@ -1011,9 +1011,9 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids of the first sequence. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): List of ids of the second sequence. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. @@ -1047,13 +1047,13 @@ class PreTrainedTokenizer(PreTrainedTokenizerBase): added tokens. Args: - ids (`int` or `List[int]`): + ids (`int` or `list[int]`): The token id (or token ids) to convert to tokens. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. Returns: - `str` or `List[str]`: The decoded token(s). + `str` or `list[str]`: The decoded token(s). """ if isinstance(ids, int): if ids in self._added_tokens_decoder: diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index 51e5fcb82b1..e77ed640932 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -28,7 +28,7 @@ from collections.abc import Mapping, Sequence, Sized from contextlib import contextmanager from dataclasses import dataclass from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Dict, List, NamedTuple, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, NamedTuple, Optional, Union import numpy as np from packaging import version @@ -132,14 +132,14 @@ LARGE_INTEGER = int(1e20) # This is used when we need something big but slightl # Define type aliases and NamedTuples TextInput = str -PreTokenizedInput = List[str] -EncodedInput = List[int] -TextInputPair = Tuple[str, str] -PreTokenizedInputPair = Tuple[List[str], List[str]] -EncodedInputPair = Tuple[List[int], List[int]] +PreTokenizedInput = list[str] +EncodedInput = list[int] +TextInputPair = tuple[str, str] +PreTokenizedInputPair = tuple[list[str], list[str]] +EncodedInputPair = tuple[list[int], list[int]] # Define type aliases for text-related non-text modalities -AudioInput = Union["np.ndarray", "torch.Tensor", List["np.ndarray"], List["torch.Tensor"]] +AudioInput = Union["np.ndarray", "torch.Tensor", list["np.ndarray"], list["torch.Tensor"]] # Slow tokenizers used to be saved in three separated files SPECIAL_TOKENS_MAP_FILE = "special_tokens_map.json" @@ -219,7 +219,7 @@ class BatchEncoding(UserDict): def __init__( self, - data: Optional[Dict[str, Any]] = None, + data: Optional[dict[str, Any]] = None, encoding: Optional[Union[EncodingFast, Sequence[EncodingFast]]] = None, tensor_type: Union[None, str, TensorType] = None, prepend_batch_axis: bool = False, @@ -299,14 +299,14 @@ class BatchEncoding(UserDict): # provided by HuggingFace tokenizers library. @property - def encodings(self) -> Optional[List[EncodingFast]]: + def encodings(self) -> Optional[list[EncodingFast]]: """ - `Optional[List[tokenizers.Encoding]]`: The list all encodings from the tokenization process. Returns `None` if + `Optional[list[tokenizers.Encoding]]`: The list all encodings from the tokenization process. Returns `None` if the input was tokenized through Python (i.e., not a fast) tokenizer. """ return self._encodings - def tokens(self, batch_index: int = 0) -> List[str]: + def tokens(self, batch_index: int = 0) -> list[str]: """ Return the list of tokens (sub-parts of the input strings after word/subword splitting and before conversion to integer indices) at a given batch index (only works for the output of a fast tokenizer). @@ -315,7 +315,7 @@ class BatchEncoding(UserDict): batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: - `List[str]`: The list of tokens at that index. + `list[str]`: The list of tokens at that index. """ if not self._encodings: raise ValueError( @@ -324,7 +324,7 @@ class BatchEncoding(UserDict): ) return self._encodings[batch_index].tokens - def sequence_ids(self, batch_index: int = 0) -> List[Optional[int]]: + def sequence_ids(self, batch_index: int = 0) -> list[Optional[int]]: """ Return a list mapping the tokens to the id of their original sentences: @@ -337,7 +337,7 @@ class BatchEncoding(UserDict): batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: - `List[Optional[int]]`: A list indicating the sequence id corresponding to each token. Special tokens added + `list[Optional[int]]`: A list indicating the sequence id corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding sequence. """ @@ -348,7 +348,7 @@ class BatchEncoding(UserDict): ) return self._encodings[batch_index].sequence_ids - def words(self, batch_index: int = 0) -> List[Optional[int]]: + def words(self, batch_index: int = 0) -> list[Optional[int]]: """ Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer. @@ -356,7 +356,7 @@ class BatchEncoding(UserDict): batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: - `List[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the + `list[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word (several tokens will be mapped to the same word index if they are parts of that word). """ @@ -372,7 +372,7 @@ class BatchEncoding(UserDict): ) return self.word_ids(batch_index) - def word_ids(self, batch_index: int = 0) -> List[Optional[int]]: + def word_ids(self, batch_index: int = 0) -> list[Optional[int]]: """ Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer. @@ -380,7 +380,7 @@ class BatchEncoding(UserDict): batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: - `List[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the + `list[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word (several tokens will be mapped to the same word index if they are parts of that word). """ @@ -630,7 +630,7 @@ class BatchEncoding(UserDict): or 1) the provided word index belongs to. Returns: - `CharSpan` or `List[CharSpan]`: Span(s) of the associated character or characters in the string. CharSpan + `CharSpan` or `list[CharSpan]`: Span(s) of the associated character or characters in the string. CharSpan are NamedTuple with: - start: index of the first character associated to the token in the original string @@ -674,7 +674,7 @@ class BatchEncoding(UserDict): Returns: - `int` or `List[int]`: Index or indices of the associated encoded token(s). + `int` or `list[int]`: Index or indices of the associated encoded token(s). """ if not self._encodings: @@ -890,7 +890,7 @@ class SpecialTokensMixin: def add_special_tokens( self, - special_tokens_dict: Dict[str, Union[str, AddedToken, Sequence[Union[str, AddedToken]]]], + special_tokens_dict: dict[str, Union[str, AddedToken, Sequence[Union[str, AddedToken]]]], replace_additional_special_tokens=True, ) -> int: """ @@ -1041,7 +1041,7 @@ class SpecialTokensMixin: return self._add_tokens(new_tokens, special_tokens=special_tokens) - def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int: + def _add_tokens(self, new_tokens: Union[list[str], list[AddedToken]], special_tokens: bool = False) -> int: raise NotImplementedError @property @@ -1102,9 +1102,9 @@ class SpecialTokensMixin: return super().__getattr__(key) @property - def special_tokens_map(self) -> Dict[str, Union[str, List[str]]]: + def special_tokens_map(self) -> dict[str, Union[str, list[str]]]: """ - `Dict[str, Union[str, List[str]]]`: A dictionary mapping special token class attributes (`cls_token`, + `dict[str, Union[str, list[str]]]`: A dictionary mapping special token class attributes (`cls_token`, `unk_token`, etc.) to their values (`''`, `''`, etc.). Convert potential tokens of `tokenizers.AddedToken` type to string. @@ -1117,9 +1117,9 @@ class SpecialTokensMixin: return set_attr @property - def special_tokens_map_extended(self) -> Dict[str, Union[str, AddedToken, List[Union[str, AddedToken]]]]: + def special_tokens_map_extended(self) -> dict[str, Union[str, AddedToken, list[Union[str, AddedToken]]]]: """ - `Dict[str, Union[str, tokenizers.AddedToken, List[Union[str, tokenizers.AddedToken]]]]`: A dictionary mapping + `dict[str, Union[str, tokenizers.AddedToken, list[Union[str, tokenizers.AddedToken]]]]`: A dictionary mapping special token class attributes (`cls_token`, `unk_token`, etc.) to their values (`''`, `''`, etc.). Don't convert tokens of `tokenizers.AddedToken` type to string so they can be used to control more finely how @@ -1133,9 +1133,9 @@ class SpecialTokensMixin: return set_attr @property - def all_special_tokens_extended(self) -> List[Union[str, AddedToken]]: + def all_special_tokens_extended(self) -> list[Union[str, AddedToken]]: """ - `List[Union[str, tokenizers.AddedToken]]`: All the special tokens (`''`, `''`, etc.), the order has + `list[Union[str, tokenizers.AddedToken]]`: All the special tokens (`''`, `''`, etc.), the order has nothing to do with the index of each tokens. If you want to know the correct indices, check `self.added_tokens_encoder`. We can't create an order anymore as the keys are `AddedTokens` and not `Strings`. @@ -1154,9 +1154,9 @@ class SpecialTokensMixin: return all_tokens @property - def all_special_tokens(self) -> List[str]: + def all_special_tokens(self) -> list[str]: """ - `List[str]`: A list of the unique special tokens (`''`, `''`, ..., etc.). + `list[str]`: A list of the unique special tokens (`''`, `''`, ..., etc.). Convert tokens of `tokenizers.AddedToken` type to string. """ @@ -1164,15 +1164,15 @@ class SpecialTokensMixin: return all_toks @property - def all_special_ids(self) -> List[int]: + def all_special_ids(self) -> list[int]: """ - `List[int]`: List the ids of the special tokens(`''`, `''`, etc.) mapped to class attributes. + `list[int]`: List the ids of the special tokens(`''`, `''`, etc.) mapped to class attributes. """ all_toks = self.all_special_tokens all_ids = self.convert_tokens_to_ids(all_toks) return all_ids - def _set_model_specific_special_tokens(self, special_tokens: List[str]): + def _set_model_specific_special_tokens(self, special_tokens: list[str]): """ Adds new special tokens to the "SPECIAL_TOKENS_ATTRIBUTES" list which will be part of "self.special_tokens" and saved as a special token in tokenizer's config. @@ -1305,14 +1305,14 @@ ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING = r""" INIT_TOKENIZER_DOCSTRING = r""" Class attributes (overridden by derived classes) - - **vocab_files_names** (`Dict[str, str]`) -- A dictionary with, as keys, the `__init__` keyword name of each + - **vocab_files_names** (`dict[str, str]`) -- A dictionary with, as keys, the `__init__` keyword name of each vocabulary file required by the model, and as associated values, the filename for saving the associated file (string). - - **pretrained_vocab_files_map** (`Dict[str, Dict[str, str]]`) -- A dictionary of dictionaries, with the + - **pretrained_vocab_files_map** (`dict[str, dict[str, str]]`) -- A dictionary of dictionaries, with the high-level keys being the `__init__` keyword name of each vocabulary file required by the model, the low-level being the `short-cut-names` of the pretrained models with, as associated values, the `url` to the associated pretrained vocabulary file. - - **model_input_names** (`List[str]`) -- A list of inputs expected in the forward pass of the model. + - **model_input_names** (`list[str]`) -- A list of inputs expected in the forward pass of the model. - **padding_side** (`str`) -- The default value for the side on which the model should have padding applied. Should be `'right'` or `'left'`. - **truncation_side** (`str`) -- The default value for the side on which the model should have truncation @@ -1333,7 +1333,7 @@ INIT_TOKENIZER_DOCSTRING = r""" chat_template (`str`, *optional*): A Jinja template string that will be used to format lists of chat messages. See https://huggingface.co/docs/transformers/chat_templating for a full description. - model_input_names (`List[string]`, *optional*): + model_input_names (`list[string]`, *optional*): The list of inputs accepted by the forward pass of the model (like `"token_type_ids"` or `"attention_mask"`). Default value is picked from the class attribute of the same name. bos_token (`str` or `tokenizers.AddedToken`, *optional*): @@ -1380,13 +1380,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Handles shared (mostly boiler plate) methods for those two classes. """ - vocab_files_names: Dict[str, str] = {} - pretrained_vocab_files_map: Dict[str, Dict[str, str]] = {} + vocab_files_names: dict[str, str] = {} + pretrained_vocab_files_map: dict[str, dict[str, str]] = {} _auto_class: Optional[str] = None # first name has to correspond to main model input name # to make sure `tokenizer.pad(...)` works correctly - model_input_names: List[str] = ["input_ids", "token_type_ids", "attention_mask"] + model_input_names: list[str] = ["input_ids", "token_type_ids", "attention_mask"] padding_side: str = "right" truncation_side: str = "right" slow_tokenizer_class = None @@ -1488,7 +1488,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): self._processor_class = processor_class @property - def added_tokens_decoder(self) -> Dict[int, AddedToken]: + def added_tokens_decoder(self) -> dict[int, AddedToken]: raise NotImplementedError() def __repr__(self) -> str: @@ -1504,7 +1504,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def __len__(self) -> int: raise NotImplementedError() - def get_vocab(self) -> Dict[str, int]: + def get_vocab(self) -> dict[str, int]: """ Returns the vocabulary as a dictionary of token to index. @@ -1512,15 +1512,15 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): vocab. Returns: - `Dict[str, int]`: The vocabulary. + `dict[str, int]`: The vocabulary. """ raise NotImplementedError() def apply_chat_template( self, - conversation: Union[List[Dict[str, str]], List[List[Dict[str, str]]]], - tools: Optional[List[Union[Dict, Callable]]] = None, - documents: Optional[List[Dict[str, str]]] = None, + conversation: Union[list[dict[str, str]], list[list[dict[str, str]]]], + tools: Optional[list[Union[dict, Callable]]] = None, + documents: Optional[list[dict[str, str]]] = None, chat_template: Optional[str] = None, add_generation_prompt: bool = False, continue_final_message: bool = False, @@ -1531,24 +1531,24 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): return_tensors: Optional[Union[str, TensorType]] = None, return_dict: bool = False, return_assistant_tokens_mask: bool = False, - tokenizer_kwargs: Optional[Dict[str, Any]] = None, + tokenizer_kwargs: Optional[dict[str, Any]] = None, **kwargs, - ) -> Union[str, List[int], List[str], List[List[int]], BatchEncoding]: + ) -> Union[str, list[int], list[str], list[list[int]], BatchEncoding]: """ Converts a list of dictionaries 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. Args: - conversation (Union[List[Dict[str, str]], List[List[Dict[str, str]]]]): A list of dicts + conversation (Union[list[dict[str, str]], list[list[dict[str, str]]]]): A list of dicts with "role" and "content" keys, representing the chat history so far. - tools (`List[Union[Dict, Callable]]`, *optional*): + tools (`list[Union[Dict, Callable]]`, *optional*): A list of tools (callable functions) that will be accessible to the model. If the template does not support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, giving the name, description and argument types for the tool. See our [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) for more information. - documents (`List[Dict[str, str]]`, *optional*): + documents (`list[dict[str, str]]`, *optional*): A list of dicts representing documents that will be accessible to the model if it is performing RAG (retrieval-augmented generation). If the template does not support RAG, this argument will have no effect. We recommend that each document should be a dict containing "title" and "text" keys. Please @@ -1593,7 +1593,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): - `'jax'`: Return JAX `jnp.ndarray` objects. return_dict (`bool`, defaults to `False`): Whether to return a dictionary with named outputs. Has no effect if tokenize is `False`. - tokenizer_kwargs (`Dict[str: Any]`, *optional*): Additional kwargs to pass to the tokenizer. + tokenizer_kwargs (`dict[str: Any]`, *optional*): Additional kwargs to pass to the tokenizer. return_assistant_tokens_mask (`bool`, defaults to `False`): Whether to return a mask of the assistant generated tokens. For tokens generated by the assistant, the mask will contain 1. For user and system tokens, the mask will contain 0. @@ -1601,7 +1601,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): **kwargs: Additional kwargs to pass to the template renderer. Will be accessible by the chat template. Returns: - `Union[List[int], Dict]`: A list of token ids representing the tokenized chat so far, including control tokens. This + `Union[list[int], Dict]`: A list of token ids representing the tokenized chat so far, including control tokens. This output is ready to pass to the model, either directly or via methods like `generate()`. If `return_dict` is set, will return a dict of tokenizer outputs instead. """ @@ -1695,7 +1695,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): else: return rendered_chat - def get_chat_template(self, chat_template: Optional[str] = None, tools: Optional[List[Dict]] = None) -> str: + def get_chat_template(self, chat_template: Optional[str] = None, tools: Optional[list[dict]] = None) -> str: """ Retrieve the chat template string used for tokenizing chat messages. This template is used internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat @@ -1706,7 +1706,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): A Jinja template or the name of a template to use for this conversion. It is usually not necessary to pass anything to this argument, as the model's template will be used by default. - tools (`List[Dict]`, *optional*): + tools (`list[Dict]`, *optional*): A list of tools (callable functions) that will be accessible to the model. If the template does not support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, giving the name, description and argument types for the tool. See our @@ -1786,7 +1786,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -2174,8 +2174,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): init_kwargs["name_or_path"] = pretrained_model_name_or_path #### Handle tokenizer serialization of added and special tokens - added_tokens_decoder: Dict[int, AddedToken] = {} - added_tokens_map: Dict[str, AddedToken] = {} + added_tokens_decoder: dict[int, AddedToken] = {} + added_tokens_map: dict[str, AddedToken] = {} # if we have info on the slow added tokens if "added_tokens_decoder" in init_kwargs: for idx, token in init_kwargs["added_tokens_decoder"].items(): @@ -2373,7 +2373,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): filename_prefix: Optional[str] = None, push_to_hub: bool = False, **kwargs, - ) -> Tuple[str]: + ) -> tuple[str]: """ Save the full tokenizer state. @@ -2403,7 +2403,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. Returns: @@ -2538,10 +2538,10 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def _save_pretrained( self, save_directory: Union[str, os.PathLike], - file_names: Tuple[str], + file_names: tuple[str], legacy_format: Optional[bool] = None, filename_prefix: Optional[str] = None, - ) -> Tuple[str]: + ) -> tuple[str]: """ Save a tokenizer using the slow-tokenizer/legacy format: vocabulary + added tokens. @@ -2570,7 +2570,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): return file_names + vocab_files + (added_tokens_file,) - def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: + def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> tuple[str]: """ Save only the vocabulary of the tokenizer (vocabulary + added tokens). @@ -2588,7 +2588,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): """ raise NotImplementedError - def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> List[str]: + def tokenize(self, text: str, pair: Optional[str] = None, add_special_tokens: bool = False, **kwargs) -> list[str]: """ Converts a string into a sequence of tokens, replacing unknown tokens with the `unk_token`. @@ -2604,7 +2604,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): [`~PreTrainedTokenizerBase.__call__`] Returns: - `List[str]`: The list of tokens. + `list[str]`: The list of tokens. """ raise NotImplementedError @@ -2615,7 +2615,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): """, """ Returns: - `List[int]`, `torch.Tensor`, `tf.Tensor` or `np.ndarray`: The tokenized ids of the text. + `list[int]`, `torch.Tensor`, `tf.Tensor` or `np.ndarray`: The tokenized ids of the text. """, ) def encode( @@ -2630,18 +2630,18 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): padding_side: Optional[str] = None, return_tensors: Optional[Union[str, TensorType]] = None, **kwargs, - ) -> List[int]: + ) -> list[int]: """ Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary. Same as doing `self.convert_tokens_to_ids(self.tokenize(text))`. Args: - text (`str`, `List[str]` or `List[int]`): + text (`str`, `list[str]` or `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -2772,11 +2772,11 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def __call__( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput], None] = None, - text_pair: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, - text_target: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput], None] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None, + text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, + text_target: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput], None] = None, text_pair_target: Optional[ - Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] + Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]] ] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, @@ -2801,19 +2801,19 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): sequences. Args: - text (`str`, `List[str]`, `List[List[str]]`, *optional*): + text (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). - text_pair_target (`str`, `List[str]`, `List[List[str]]`, *optional*): + text_pair_target (`str`, `list[str]`, `list[list[str]]`, *optional*): The sequence or batch of sequences to be encoded as target texts. Each sequence can be a string or a list of strings (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set `is_split_into_words=True` (to lift the ambiguity with a batch of sequences). @@ -2869,8 +2869,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def _call_one( self, - text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]], - text_pair: Optional[Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]] = None, + text: Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]], + text_pair: Optional[Union[TextInput, PreTokenizedInput, list[TextInput], list[PreTokenizedInput]]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy, None] = None, @@ -2913,14 +2913,14 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): if not _is_valid_text_input(text): raise ValueError( - "text input must be of type `str` (single example), `List[str]` (batch or single pretokenized example) " - "or `List[List[str]]` (batch of pretokenized examples)." + "text input must be of type `str` (single example), `list[str]` (batch or single pretokenized example) " + "or `list[list[str]]` (batch of pretokenized examples)." ) if text_pair is not None and not _is_valid_text_input(text_pair): raise ValueError( - "text input must be of type `str` (single example), `List[str]` (batch or single pretokenized example) " - "or `List[List[str]]` (batch of pretokenized examples)." + "text input must be of type `str` (single example), `list[str]` (batch or single pretokenized example) " + "or `list[list[str]]` (batch of pretokenized examples)." ) if is_split_into_words: @@ -3018,11 +3018,11 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Args: - text (`str`, `List[str]` or (for non-fast tokenizers) `List[int]`): + text (`str`, `list[str]` or (for non-fast tokenizers) `list[int]`): The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). - text_pair (`str`, `List[str]` or `List[int]`, *optional*): + text_pair (`str`, `list[str]` or `list[int]`, *optional*): Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using the `tokenize` method) or a list of integers (tokenized string ids using the `convert_tokens_to_ids` method). @@ -3089,12 +3089,12 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], - List[PreTokenizedInputPair], - List[EncodedInput], - List[EncodedInputPair], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], + list[PreTokenizedInputPair], + list[EncodedInput], + list[EncodedInputPair], ], add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, @@ -3125,7 +3125,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Args: - batch_text_or_text_pairs (`List[str]`, `List[Tuple[str, str]]`, `List[List[str]]`, `List[Tuple[List[str], List[str]]]`, and for not-fast tokenizers, also `List[List[int]]`, `List[Tuple[List[int], List[int]]]`): + batch_text_or_text_pairs (`list[str]`, `list[tuple[str, str]]`, `list[list[str]]`, `list[tuple[list[str], list[str]]]`, and for not-fast tokenizers, also `list[list[int]]`, `list[tuple[list[int], list[int]]]`): Batch of sequences or pair of sequences to be encoded. This can be a list of string/string-sequences/int-sequences or a list of pair of string/string-sequences/int-sequence (see details in `encode_plus`). @@ -3166,12 +3166,12 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def _batch_encode_plus( self, batch_text_or_text_pairs: Union[ - List[TextInput], - List[TextInputPair], - List[PreTokenizedInput], - List[PreTokenizedInputPair], - List[EncodedInput], - List[EncodedInputPair], + list[TextInput], + list[TextInputPair], + list[PreTokenizedInput], + list[PreTokenizedInputPair], + list[EncodedInput], + list[EncodedInputPair], ], add_special_tokens: bool = True, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, @@ -3198,10 +3198,10 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): self, encoded_inputs: Union[ BatchEncoding, - List[BatchEncoding], - Dict[str, EncodedInput], - Dict[str, List[EncodedInput]], - List[Dict[str, EncodedInput]], + list[BatchEncoding], + dict[str, EncodedInput], + dict[str, list[EncodedInput]], + list[dict[str, EncodedInput]], ], padding: Union[bool, str, PaddingStrategy] = True, max_length: Optional[int] = None, @@ -3230,13 +3230,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Args: - encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `Dict[str, List[int]]`, `Dict[str, List[List[int]]` or `List[Dict[str, List[int]]]`): - Tokenized inputs. Can represent one input ([`BatchEncoding`] or `Dict[str, List[int]]`) or a batch of - tokenized inputs (list of [`BatchEncoding`], *Dict[str, List[List[int]]]* or *List[Dict[str, - List[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader + encoded_inputs ([`BatchEncoding`], list of [`BatchEncoding`], `dict[str, list[int]]`, `dict[str, list[list[int]]` or `list[dict[str, list[int]]]`): + Tokenized inputs. Can represent one input ([`BatchEncoding`] or `dict[str, list[int]]`) or a batch of + tokenized inputs (list of [`BatchEncoding`], *dict[str, list[list[int]]]* or *list[dict[str, + list[int]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader collate function. - Instead of `List[int]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see + Instead of `list[int]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors), see the note above for the return type. padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`): Select a strategy to pad the returned sequences (according to the model's padding side and padding @@ -3374,8 +3374,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): return BatchEncoding(batch_outputs, tensor_type=return_tensors) def create_token_type_ids_from_sequences( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Create the token type IDs corresponding to the sequences passed. [What are token type IDs?](../glossary#token-type-ids) @@ -3383,11 +3383,11 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Should be overridden in a subclass if the model has a special way of building those. Args: - token_ids_0 (`List[int]`): The first tokenized sequence. - token_ids_1 (`List[int]`, *optional*): The second tokenized sequence. + token_ids_0 (`list[int]`): The first tokenized sequence. + token_ids_1 (`list[int]`, *optional*): The second tokenized sequence. Returns: - `List[int]`: The token type ids. + `list[int]`: The token type ids. """ cls_len = int(getattr(self, "cls_token_id", None) is not None) sep_len = int(getattr(self, "sep_token_id", None) is not None) @@ -3398,8 +3398,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): return [0] * (cls_len + len(token_ids_0) + sep_len) + [1] * (len(token_ids_1) + sep_len) def build_inputs_with_special_tokens( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None + ) -> list[int]: """ Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. @@ -3407,11 +3407,11 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): This implementation does not add special tokens and this method should be overridden in a subclass. Args: - token_ids_0 (`List[int]`): The first tokenized sequence. - token_ids_1 (`List[int]`, *optional*): The second tokenized sequence. + token_ids_0 (`list[int]`): The first tokenized sequence. + token_ids_1 (`list[int]`, *optional*): The second tokenized sequence. Returns: - `List[int]`: The model input with special tokens. + `list[int]`: The model input with special tokens. """ if token_ids_1 is None: return token_ids_0 @@ -3420,8 +3420,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): @add_end_docstrings(ENCODE_KWARGS_DOCSTRING, ENCODE_PLUS_ADDITIONAL_KWARGS_DOCSTRING) def prepare_for_model( self, - ids: List[int], - pair_ids: Optional[List[int]] = None, + ids: list[int], + pair_ids: Optional[list[int]] = None, add_special_tokens: bool = True, padding: Union[bool, str, PaddingStrategy] = False, truncation: Union[bool, str, TruncationStrategy, None] = None, @@ -3448,10 +3448,10 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): overflowing tokens. Such a combination of arguments will raise an error. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. """ @@ -3557,20 +3557,20 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def truncate_sequences( self, - ids: List[int], - pair_ids: Optional[List[int]] = None, + ids: list[int], + pair_ids: Optional[list[int]] = None, num_tokens_to_remove: int = 0, truncation_strategy: Union[str, TruncationStrategy] = "longest_first", stride: int = 0, - ) -> Tuple[List[int], List[int], List[int]]: + ) -> tuple[list[int], list[int], list[int]]: """ Truncates a sequence pair in-place following the strategy. Args: - ids (`List[int]`): + ids (`list[int]`): Tokenized input ids of the first sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. - pair_ids (`List[int]`, *optional*): + pair_ids (`list[int]`, *optional*): Tokenized input ids of the second sequence. Can be obtained from a string by chaining the `tokenize` and `convert_tokens_to_ids` methods. num_tokens_to_remove (`int`, *optional*, defaults to 0): @@ -3595,7 +3595,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): sequence returned. The value of this argument defines the number of additional tokens. Returns: - `Tuple[List[int], List[int], List[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of + `tuple[list[int], list[int], list[int]]`: The truncated `ids`, the truncated `pair_ids` and the list of overflowing tokens. Note: The *longest_first* strategy returns empty list of overflowing tokens if a pair of sequences (or a batch of pairs) is provided. """ @@ -3681,7 +3681,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def _pad( self, - encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding], + encoded_inputs: Union[dict[str, EncodedInput], BatchEncoding], max_length: Optional[int] = None, padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD, pad_to_multiple_of: Optional[int] = None, @@ -3693,7 +3693,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Args: encoded_inputs: - Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`). + Dictionary of tokenized inputs (`list[int]`) or batch of tokenized inputs (`list[list[int]]`). max_length: maximum length of the returned list and optionally padding length (see below). Will truncate by taking into account the special tokens. padding_strategy: PaddingStrategy to use for padding. @@ -3761,13 +3761,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): return encoded_inputs - def convert_tokens_to_string(self, tokens: List[str]) -> str: + def convert_tokens_to_string(self, tokens: list[str]) -> str: """ Converts a sequence of tokens in a single string. The most simple way to do it is `" ".join(tokens)` but we often want to remove sub-word tokenization artifacts at the same time. Args: - tokens (`List[str]`): The token to join in a string. + tokens (`list[str]`): The token to join in a string. Returns: `str`: The joined tokens. @@ -3776,16 +3776,16 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def batch_decode( self, - sequences: Union[List[int], List[List[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], + sequences: Union[list[int], list[list[int]], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, **kwargs, - ) -> List[str]: + ) -> list[str]: """ Convert a list of lists of token ids into a list of strings by calling decode. Args: - sequences (`Union[List[int], List[List[int]], np.ndarray, torch.Tensor, tf.Tensor]`): + sequences (`Union[list[int], list[list[int]], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -3796,7 +3796,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Will be passed to the underlying model specific decode method. Returns: - `List[str]`: The list of decoded sentences. + `list[str]`: The list of decoded sentences. """ return [ self.decode( @@ -3810,7 +3810,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def decode( self, - token_ids: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], + token_ids: Union[int, list[int], "np.ndarray", "torch.Tensor", "tf.Tensor"], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, **kwargs, @@ -3822,7 +3822,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. Args: - token_ids (`Union[int, List[int], np.ndarray, torch.Tensor, tf.Tensor]`): + token_ids (`Union[int, list[int], np.ndarray, torch.Tensor, tf.Tensor]`): List of tokenized input ids. Can be obtained using the `__call__` method. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. @@ -3847,7 +3847,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def _decode( self, - token_ids: Union[int, List[int]], + token_ids: Union[int, list[int]], skip_special_tokens: bool = False, clean_up_tokenization_spaces: Optional[bool] = None, **kwargs, @@ -3855,16 +3855,16 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): raise NotImplementedError def get_special_tokens_mask( - self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False - ) -> List[int]: + self, token_ids_0: list[int], token_ids_1: Optional[list[int]] = None, already_has_special_tokens: bool = False + ) -> list[int]: """ Retrieves sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer `prepare_for_model` or `encode_plus` methods. Args: - token_ids_0 (`List[int]`): + token_ids_0 (`list[int]`): List of ids of the first sequence. - token_ids_1 (`List[int]`, *optional*): + token_ids_1 (`list[int]`, *optional*): List of ids of the second sequence. already_has_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not the token list is already formatted with special tokens for the model. @@ -3910,13 +3910,13 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): ) return out_string - def _eventual_warn_about_too_long_sequence(self, ids: List[int], max_length: Optional[int], verbose: bool): + def _eventual_warn_about_too_long_sequence(self, ids: list[int], max_length: Optional[int], verbose: bool): """ Depending on the input and internal state we might trigger a warning about a sequence that is too long for its corresponding model Args: - ids (`List[str]`): The ids produced by the tokenization + ids (`list[str]`): The ids produced by the tokenization max_length (`int`, *optional*): The max_length desired (does not trigger a warning if it is set) verbose (`bool`): Whether or not to print more information and warnings. @@ -3983,8 +3983,8 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): def prepare_seq2seq_batch( self, - src_texts: List[str], - tgt_texts: Optional[List[str]] = None, + src_texts: list[str], + tgt_texts: Optional[list[str]] = None, max_length: Optional[int] = None, max_target_length: Optional[int] = None, padding: str = "longest", @@ -3996,7 +3996,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin): Prepare model inputs for translation. For best performance, translate one sentence at a time. Arguments: - src_texts (`List[str]`): + src_texts (`list[str]`): List of documents to summarize or source language texts. tgt_texts (`list`, *optional*): List of summaries or target language texts. @@ -4104,12 +4104,12 @@ For a more complete example, see the implementation of `prepare_seq2seq_batch`. return model_inputs -def get_fast_tokenizer_file(tokenization_files: List[str]) -> str: +def get_fast_tokenizer_file(tokenization_files: list[str]) -> str: """ Get the tokenization file to use for this version of transformers. Args: - tokenization_files (`List[str]`): The list of available configuration files. + tokenization_files (`list[str]`): The list of available configuration files. Returns: `str`: The tokenization file to use. diff --git a/src/transformers/tokenization_utils_fast.py b/src/transformers/tokenization_utils_fast.py index 3636a1e67c1..9249fe5435b 100644 --- a/src/transformers/tokenization_utils_fast.py +++ b/src/transformers/tokenization_utils_fast.py @@ -265,7 +265,7 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): Returns the added tokens in the vocabulary as a dictionary of index to AddedToken. Returns: - `Dict[str, int]`: The added tokens. + `dict[str, int]`: The added tokens. """ return self._tokenizer.get_added_tokens_decoder() @@ -274,7 +274,7 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): Returns the added tokens in the vocabulary as a dictionary of token to index. Returns: - `Dict[str, int]`: The added tokens. + `dict[str, int]`: The added tokens. """ return {k.content: v for v, k in sorted(self.added_tokens_decoder.items(), key=lambda item: item[0])} @@ -354,7 +354,7 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): tokens (`str` or `Iterable[str]`): One or several token(s) to convert to token id(s). Returns: - `int` or `List[int]`: The token id or list of token ids. + `int` or `list[int]`: The token id or list of token ids. """ if isinstance(tokens, str): return self._convert_token_to_id_with_added_voc(tokens) @@ -405,13 +405,13 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): added tokens. Args: - ids (`int` or `List[int]`): + ids (`int` or `list[int]`): The token id (or token ids) to convert to tokens. skip_special_tokens (`bool`, *optional*, defaults to `False`): Whether or not to remove special tokens in the decoding. Returns: - `str` or `List[str]`: The decoded token(s). + `str` or `list[str]`: The decoded token(s). """ if isinstance(ids, int): return self._tokenizer.id_to_token(ids) @@ -551,9 +551,9 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): ) # Convert encoding to dict - # `Tokens` has type: Tuple[ - # List[Dict[str, List[List[int]]]] or List[Dict[str, 2D-Tensor]], - # List[EncodingFast] + # `Tokens` has type: tuple[ + # list[dict[str, list[list[int]]]] or list[dict[str, 2D-Tensor]], + # list[EncodingFast] # ] # with nested dimensions corresponding to batch, overflows, sequence length tokens_and_encodings = [ @@ -749,7 +749,7 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): as the current one. Args: - text_iterator (generator of `List[str]`): + text_iterator (generator of `list[str]`): The training corpus. Should be a generator of batches of texts, for instance a list of lists of texts if you have everything in memory. vocab_size (`int`): @@ -758,10 +758,10 @@ class PreTrainedTokenizerFast(PreTrainedTokenizerBase): The total number of sequences in the iterator. This is used to provide meaningful progress tracking new_special_tokens (list of `str` or `AddedToken`, *optional*): A list of new special tokens to add to the tokenizer you are training. - special_tokens_map (`Dict[str, str]`, *optional*): + special_tokens_map (`dict[str, str]`, *optional*): If you want to rename some of the special tokens this tokenizer uses, pass along a mapping old special token name to new special token name in this argument. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to the trainer from the 🤗 Tokenizers library. Returns: diff --git a/src/transformers/trainer.py b/src/transformers/trainer.py index 113e67faa28..bd0affc9ad3 100755 --- a/src/transformers/trainer.py +++ b/src/transformers/trainer.py @@ -34,7 +34,7 @@ import warnings from collections.abc import Mapping from functools import partial from pathlib import Path -from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, Tuple, Union +from typing import TYPE_CHECKING, Any, Callable, Iterator, Optional, Union # Integrations must be imported before ML frameworks: @@ -349,7 +349,7 @@ class Trainer: `torch.Generator` for the randomization that must be identical on all processes (and the Trainer will manually set the seed of this `generator` at each epoch) or have a `set_epoch()` method that internally sets the seed of the RNGs used. - eval_dataset (Union[`torch.utils.data.Dataset`, Dict[str, `torch.utils.data.Dataset`, `datasets.Dataset`]), *optional*): + eval_dataset (Union[`torch.utils.data.Dataset`, dict[str, `torch.utils.data.Dataset`, `datasets.Dataset`]), *optional*): The dataset to use for evaluation. If it is a [`~datasets.Dataset`], columns not accepted by the `model.forward()` method are automatically removed. If it is a dictionary, it will evaluate on each dataset prepending the dictionary key to the metric name. @@ -379,10 +379,10 @@ class Trainer: detailed in [here](callback). If you want to remove one of the default callbacks used, use the [`Trainer.remove_callback`] method. - optimizers (`Tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]`, *optional*, defaults to `(None, None)`): + optimizers (`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]`, *optional*, defaults to `(None, None)`): A tuple containing the optimizer and the scheduler to use. Will default to an instance of [`AdamW`] on your model and a scheduler given by [`get_linear_schedule_with_warmup`] controlled by `args`. - optimizer_cls_and_kwargs (`Tuple[Type[torch.optim.Optimizer], Dict[str, Any]]`, *optional*): + optimizer_cls_and_kwargs (`tuple[Type[torch.optim.Optimizer], dict[str, Any]]`, *optional*): A tuple containing the optimizer class and keyword arguments to use. Overrides `optim` and `optim_args` in `args`. Incompatible with the `optimizers` argument. @@ -1328,7 +1328,7 @@ class Trainer: is_layerwise_supported (bool): Whether layerwise optimization is supported. Returns: - Tuple[Any, Any]: Optimizer class and updated optimizer kwargs. + tuple[Any, Any]: Optimizer class and updated optimizer kwargs. """ is_layerwise = optimizer_name.lower().endswith("layerwise") if is_layerwise and args.parallel_mode == ParallelMode.DISTRIBUTED and is_layerwise_supported: @@ -2112,12 +2112,12 @@ class Trainer: If a `str`, local path to a saved checkpoint as saved by a previous instance of [`Trainer`]. If a `bool` and equals `True`, load the last checkpoint in *args.output_dir* as saved by a previous instance of [`Trainer`]. If present, training will resume from the model/optimizer/scheduler states loaded here. - trial (`optuna.Trial` or `Dict[str, Any]`, *optional*): + trial (`optuna.Trial` or `dict[str, Any]`, *optional*): The trial run or the hyperparameter dictionary for hyperparameter search. - ignore_keys_for_eval (`List[str]`, *optional*) + ignore_keys_for_eval (`list[str]`, *optional*) A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions for evaluation during the training. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments used to hide deprecated arguments """ if resume_from_checkpoint is False: @@ -3576,19 +3576,19 @@ class Trainer: Args: - hp_space (`Callable[["optuna.Trial"], Dict[str, float]]`, *optional*): + hp_space (`Callable[["optuna.Trial"], dict[str, float]]`, *optional*): A function that defines the hyperparameter search space. Will default to [`~trainer_utils.default_hp_space_optuna`] or [`~trainer_utils.default_hp_space_ray`] or [`~trainer_utils.default_hp_space_sigopt`] depending on your backend. - compute_objective (`Callable[[Dict[str, float]], float]`, *optional*): + compute_objective (`Callable[[dict[str, float]], float]`, *optional*): A function computing the objective to minimize or maximize from the metrics returned by the `evaluate` method. Will default to [`~trainer_utils.default_compute_objective`]. n_trials (`int`, *optional*, defaults to 100): The number of trial runs to test. - direction (`str` or `List[str]`, *optional*, defaults to `"minimize"`): + direction (`str` or `list[str]`, *optional*, defaults to `"minimize"`): If it's single objective optimization, direction is `str`, can be `"minimize"` or `"maximize"`, you should pick `"minimize"` when optimizing the validation loss, `"maximize"` when optimizing one or - several metrics. If it's multi objectives optimization, direction is `List[str]`, can be List of + several metrics. If it's multi objectives optimization, direction is `list[str]`, can be List of `"minimize"` and `"maximize"`, you should pick `"minimize"` when optimizing the validation loss, `"maximize"` when optimizing one or several metrics. backend (`str` or [`~training_utils.HPSearchBackend`], *optional*): @@ -3596,7 +3596,7 @@ class Trainer: on which one is installed. If all are installed, will default to optuna. hp_name (`Callable[["optuna.Trial"], str]]`, *optional*): A function that defines the trial/run name. Will default to None. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments for each backend: - `optuna`: parameters from @@ -3611,7 +3611,7 @@ class Trainer: [sigopt.Connection.set_proxies](https://docs.sigopt.com/support/faq#how-do-i-use-sigopt-with-a-proxy). Returns: - [`trainer_utils.BestRun` or `List[trainer_utils.BestRun]`]: All the information about the best run or best + [`trainer_utils.BestRun` or `list[trainer_utils.BestRun]`]: All the information about the best run or best runs for multi-objective optimization. Experiment summary can be found in `run_summary` attribute for Ray backend. """ @@ -3642,7 +3642,7 @@ class Trainer: Subclass and override this method to inject custom behavior. Args: - logs (`Dict[str, float]`): + logs (`dict[str, float]`): The values to log. start_time (`Optional[float]`): The start of training. @@ -3727,7 +3727,7 @@ class Trainer: Args: model (`nn.Module`): The model to train. - inputs (`Dict[str, Union[torch.Tensor, Any]]`): + inputs (`dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. The dictionary will be unpacked before being fed to the model. Most models expect the targets under the @@ -3811,7 +3811,7 @@ class Trainer: Args: model (`nn.Module`): The model to compute the loss for. - inputs (`Dict[str, Union[torch.Tensor, Any]]`): + inputs (`dict[str, Union[torch.Tensor, Any]]`): The input data for the model. return_outputs (`bool`, *optional*, defaults to `False`): Whether to return the model outputs along with the loss. @@ -4143,7 +4143,7 @@ class Trainer: You can also subclass and override this method to inject custom behavior. Args: - eval_dataset (Union[`Dataset`, Dict[str, `Dataset`]), *optional*): + eval_dataset (Union[`Dataset`, dict[str, `Dataset`]), *optional*): Pass a dataset if you wish to override `self.eval_dataset`. If it is a [`~datasets.Dataset`], columns not accepted by the `model.forward()` method are automatically removed. If it is a dictionary, it will evaluate on each dataset, prepending the dictionary key to the metric name. Datasets must implement the @@ -4161,7 +4161,7 @@ class Trainer: - ignore_keys (`List[str]`, *optional*): + ignore_keys (`list[str]`, *optional*): A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions. metric_key_prefix (`str`, *optional*, defaults to `"eval"`): @@ -4245,7 +4245,7 @@ class Trainer: test_dataset (`Dataset`): Dataset to run the predictions on. If it is an `datasets.Dataset`, columns not accepted by the `model.forward()` method are automatically removed. Has to implement the method `__len__` - ignore_keys (`List[str]`, *optional*): + ignore_keys (`list[str]`, *optional*): A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions. metric_key_prefix (`str`, *optional*, defaults to `"test"`): @@ -4264,7 +4264,7 @@ class Trainer: - predictions (`np.ndarray`): The predictions on `test_dataset`. - label_ids (`np.ndarray`, *optional*): The labels (if the dataset contained some). - - metrics (`Dict[str, float]`, *optional*): The potential dictionary of metrics (if the dataset contained + - metrics (`dict[str, float]`, *optional*): The potential dictionary of metrics (if the dataset contained labels). """ # memory metrics - must set up as early as possible @@ -4545,19 +4545,19 @@ class Trainer: Args: model (`nn.Module`): The model to evaluate. - inputs (`Dict[str, Union[torch.Tensor, Any]]`): + inputs (`dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. The dictionary will be unpacked before being fed to the model. Most models expect the targets under the argument `labels`. Check your model's documentation for all accepted arguments. prediction_loss_only (`bool`): Whether or not to return the loss only. - ignore_keys (`List[str]`, *optional*): + ignore_keys (`list[str]`, *optional*): A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions. Return: - Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, + tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and labels (each being optional). """ has_labels = False if len(self.label_names) == 0 else all(inputs.get(k) is not None for k in self.label_names) @@ -4642,7 +4642,7 @@ class Trainer: model or subclass and override this method. Args: - inputs (`Dict[str, Union[torch.Tensor, Any]]`): + inputs (`dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. Returns: @@ -4692,20 +4692,20 @@ class Trainer: license (`str`, *optional*): The license of the model. Will default to the license of the pretrained model used, if the original model given to the `Trainer` comes from a repo on the Hub. - tags (`str` or `List[str]`, *optional*): + tags (`str` or `list[str]`, *optional*): Some tags to be included in the metadata of the model card. model_name (`str`, *optional*): The name of the model. finetuned_from (`str`, *optional*): The name of the model used to fine-tune this one (if applicable). Will default to the name of the repo of the original model given to the `Trainer` (if it comes from the Hub). - tasks (`str` or `List[str]`, *optional*): + tasks (`str` or `list[str]`, *optional*): One or several task identifiers, to be included in the metadata of the model card. - dataset_tags (`str` or `List[str]`, *optional*): + dataset_tags (`str` or `list[str]`, *optional*): One or several dataset tags, to be included in the metadata of the model card. - dataset (`str` or `List[str]`, *optional*): + dataset (`str` or `list[str]`, *optional*): One or several dataset identifiers, to be included in the metadata of the model card. - dataset_args (`str` or `List[str]`, *optional*): + dataset_args (`str` or `list[str]`, *optional*): One or several dataset arguments, to be included in the metadata of the model card. """ if not self.is_world_process_zero(): @@ -4838,7 +4838,7 @@ class Trainer: Token with write permission to overwrite Trainer's original args. revision (`str`, *optional*): The git revision to commit from. Defaults to the head of the "main" branch. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments passed along to [`~Trainer.create_model_card`]. Returns: @@ -5282,7 +5282,7 @@ class Trainer: def get_batch_samples( self, epoch_iterator: Iterator, num_batches: int, device: torch.device - ) -> Tuple[list, Optional[torch.Tensor]]: + ) -> tuple[list, Optional[torch.Tensor]]: """ Collects a specified number of batches from the epoch iterator and optionally counts the number of items in the batches to properly scale the loss. """ diff --git a/src/transformers/trainer_callback.py b/src/transformers/trainer_callback.py index e4c465dfe55..7102f7a5bed 100644 --- a/src/transformers/trainer_callback.py +++ b/src/transformers/trainer_callback.py @@ -69,7 +69,7 @@ class TrainerState: total_flos (`float`, *optional*, defaults to 0): The total number of floating operations done by the model since the beginning of training (stored as floats to avoid overflow). - log_history (`List[Dict[str, float]]`, *optional*): + log_history (`list[dict[str, float]]`, *optional*): The list of logs done since the beginning of training. best_metric (`float`, *optional*): When tracking the best model, the value of the best metric encountered so far. @@ -88,7 +88,7 @@ class TrainerState: is_hyper_param_search (`bool`, *optional*, defaults to `False`): Whether we are in the process of a hyper parameter search using Trainer.hyperparameter_search. This will impact the way data will be logged in TensorBoard. - stateful_callbacks (`List[StatefulTrainerCallback]`, *optional*): + stateful_callbacks (`list[StatefulTrainerCallback]`, *optional*): Callbacks attached to the `Trainer` that should have their states be saved or restored. Relevant callbacks should implement a `state` and `from_state` function. """ @@ -321,11 +321,11 @@ class TrainerCallback: The current dataloader used for training. eval_dataloader (`torch.utils.data.DataLoader`, *optional*): The current dataloader used for evaluation. - metrics (`Dict[str, float]`): + metrics (`dict[str, float]`): The metrics computed by the last evaluation phase. Those are only accessible in the event `on_evaluate`. - logs (`Dict[str, float]`): + logs (`dict[str, float]`): The values to log. Those are only accessible in the event `on_log`. diff --git a/src/transformers/trainer_pt_utils.py b/src/transformers/trainer_pt_utils.py index e806fe47b3e..2a47bf6744e 100644 --- a/src/transformers/trainer_pt_utils.py +++ b/src/transformers/trainer_pt_utils.py @@ -271,7 +271,7 @@ class DistributedSamplerWithLoop(DistributedSampler): Dataset used for sampling. batch_size (`int`): The batch size used with this sampler - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): All other keyword arguments passed to `DistributedSampler`. """ @@ -644,7 +644,7 @@ class LengthGroupedSampler(Sampler): lengths = [len(feature[model_input_name]) for feature in dataset] elif isinstance(lengths, torch.Tensor): logger.info( - "If lengths is a torch.Tensor, LengthGroupedSampler will be slow. Converting lengths to List[int]..." + "If lengths is a torch.Tensor, LengthGroupedSampler will be slow. Converting lengths to list[int]..." ) lengths = lengths.tolist() @@ -708,7 +708,7 @@ class DistributedLengthGroupedSampler(DistributedSampler): elif isinstance(lengths, torch.Tensor): logger.info( "If lengths is a torch.Tensor, DistributedLengthGroupedSampler will be slow. Converting lengths to" - " List[int]..." + " list[int]..." ) lengths = lengths.tolist() @@ -941,11 +941,11 @@ def metrics_format(self, metrics: dict[str, float]) -> dict[str, float]: Reformat Trainer metrics values to a human-readable format. Args: - metrics (`Dict[str, float]`): + metrics (`dict[str, float]`): The metrics returned from train/evaluate/predict Returns: - metrics (`Dict[str, float]`): The reformatted metrics + metrics (`dict[str, float]`): The reformatted metrics """ metrics_copy = metrics.copy() @@ -971,7 +971,7 @@ def log_metrics(self, split, metrics): Args: split (`str`): Mode/split name: one of `train`, `eval`, `test` - metrics (`Dict[str, float]`): + metrics (`dict[str, float]`): The metrics returned from train/evaluate/predictmetrics: metrics dict Notes on memory reports: @@ -1061,7 +1061,7 @@ def save_metrics(self, split, metrics, combined=True): Args: split (`str`): Mode/split name: one of `train`, `eval`, `test`, `all` - metrics (`Dict[str, float]`): + metrics (`dict[str, float]`): The metrics returned from train/evaluate/predict combined (`bool`, *optional*, defaults to `True`): Creates combined metrics by updating `all_results.json` with metrics of this call diff --git a/src/transformers/trainer_seq2seq.py b/src/transformers/trainer_seq2seq.py index e3ae588cd04..4cbcad1f9de 100644 --- a/src/transformers/trainer_seq2seq.py +++ b/src/transformers/trainer_seq2seq.py @@ -154,7 +154,7 @@ class Seq2SeqTrainer(Trainer): Pass a dataset if you wish to override `self.eval_dataset`. If it is an [`~datasets.Dataset`], columns not accepted by the `model.forward()` method are automatically removed. It must implement the `__len__` method. - ignore_keys (`List[str]`, *optional*): + ignore_keys (`list[str]`, *optional*): A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions. metric_key_prefix (`str`, *optional*, defaults to `"eval"`): @@ -207,7 +207,7 @@ class Seq2SeqTrainer(Trainer): test_dataset (`Dataset`): Dataset to run the predictions on. If it is a [`~datasets.Dataset`], columns not accepted by the `model.forward()` method are automatically removed. Has to implement the method `__len__` - ignore_keys (`List[str]`, *optional*): + ignore_keys (`list[str]`, *optional*): A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions. metric_key_prefix (`str`, *optional*, defaults to `"eval"`): @@ -233,7 +233,7 @@ class Seq2SeqTrainer(Trainer): - predictions (`np.ndarray`): The predictions on `test_dataset`. - label_ids (`np.ndarray`, *optional*): The labels (if the dataset contained some). - - metrics (`Dict[str, float]`, *optional*): The potential dictionary of metrics (if the dataset contained + - metrics (`dict[str, float]`, *optional*): The potential dictionary of metrics (if the dataset contained labels). """ @@ -270,7 +270,7 @@ class Seq2SeqTrainer(Trainer): Args: model (`nn.Module`): The model to evaluate. - inputs (`Dict[str, Union[torch.Tensor, Any]]`): + inputs (`dict[str, Union[torch.Tensor, Any]]`): The inputs and targets of the model. The dictionary will be unpacked before being fed to the model. Most models expect the targets under the @@ -281,7 +281,7 @@ class Seq2SeqTrainer(Trainer): Additional `generate` specific kwargs. Return: - Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and + tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and labels (each being optional). """ diff --git a/src/transformers/trainer_utils.py b/src/transformers/trainer_utils.py index 5556c1d43ec..0766b8c2ed1 100644 --- a/src/transformers/trainer_utils.py +++ b/src/transformers/trainer_utils.py @@ -260,7 +260,7 @@ class BestRun(NamedTuple): with run-{run_id}). objective (`float`): The objective that was obtained for this run. - hyperparameters (`Dict[str, Any]`): + hyperparameters (`dict[str, Any]`): The hyperparameters picked to get this run. run_summary (`Optional[Any]`): A summary of tuning experiments. `ray.tune.ExperimentAnalysis` object for Ray backend. @@ -278,7 +278,7 @@ def default_compute_objective(metrics: dict[str, float]) -> float: metrics are provided to the [`Trainer`], the sum of all metrics otherwise. Args: - metrics (`Dict[str, float]`): The metrics returned by the evaluate method. + metrics (`dict[str, float]`): The metrics returned by the evaluate method. Return: `float`: The objective to minimize or maximize @@ -876,7 +876,7 @@ def check_target_module_exists(optim_target_modules, key: str, return_is_regex: """A helper method to check if the passed module's key name matches any of the target modules in the optim_target_modules. Args: - optim_target_modules (`Union[str, List[str]]`): + optim_target_modules (`Union[str, list[str]]`): A list of strings to try to match. Can be also a full string. key (`str`): A key to search any matches in optim_target_modules diff --git a/src/transformers/training_args.py b/src/transformers/training_args.py index 37a1db57614..23656684ceb 100644 --- a/src/transformers/training_args.py +++ b/src/transformers/training_args.py @@ -445,7 +445,7 @@ class TrainingArguments: set to warn or lower (default), `False` otherwise. remove_unused_columns (`bool`, *optional*, defaults to `True`): Whether or not to automatically remove the columns unused by the model forward method. - label_names (`List[str]`, *optional*): + label_names (`list[str]`, *optional*): The list of keys in your dictionary of inputs that correspond to the labels. Will eventually default to the list of argument names accepted by the model that contain the word "label", @@ -503,7 +503,7 @@ class TrainingArguments: - min_num_params (`int`, *optional*, defaults to `0`): FSDP's minimum number of parameters for Default Auto Wrapping. (useful only when `fsdp` field is passed). - - transformer_layer_cls_to_wrap (`List[str]`, *optional*): + - transformer_layer_cls_to_wrap (`list[str]`, *optional*): List of transformer layer class names (case-sensitive) to wrap, e.g, `BertLayer`, `GPTJBlock`, `T5Block` .... (useful only when `fsdp` flag is passed). - backward_prefetch (`str`, *optional*) @@ -621,7 +621,7 @@ class TrainingArguments: Column name for precomputed lengths. If the column exists, grouping by length will use these values rather than computing them on train startup. Ignored unless `group_by_length` is `True` and the dataset is an instance of `Dataset`. - report_to (`str` or `List[str]`, *optional*, defaults to `"all"`): + report_to (`str` or `list[str]`, *optional*, defaults to `"all"`): The list of integrations to report the results and logs to. Supported platforms are `"azure_ml"`, `"clearml"`, `"codecarbon"`, `"comet_ml"`, `"dagshub"`, `"dvclive"`, `"flyte"`, `"mlflow"`, `"neptune"`, `"swanlab"`, `"tensorboard"`, and `"wandb"`. Use `"all"` to report to all integrations installed, `"none"` @@ -699,7 +699,7 @@ class TrainingArguments: Key word arguments to be passed to the `gradient_checkpointing_enable` method. include_inputs_for_metrics (`bool`, *optional*, defaults to `False`): This argument is deprecated. Use `include_for_metrics` instead, e.g, `include_for_metrics = ["inputs"]`. - include_for_metrics (`List[str]`, *optional*, defaults to `[]`): + include_for_metrics (`list[str]`, *optional*, defaults to `[]`): Include additional data in the `compute_metrics` function if needed for metrics computation. Possible options to add to `include_for_metrics` list: - `"inputs"`: Input data passed to the model, intended for calculating input dependent metrics. @@ -768,7 +768,7 @@ class TrainingArguments: for instruction fine-tuning. Check out the [original paper](https://huggingface.co/papers/2310.05914) and the [original code](https://github.com/neelsjain/NEFTune). Support transformers `PreTrainedModel` and also `PeftModel` from peft. The original paper used values in the range [5.0, 15.0]. - optim_target_modules (`Union[str, List[str]]`, *optional*): + optim_target_modules (`Union[str, list[str]]`, *optional*): The target modules to optimize, i.e. the module names that you would like to train. Currently used for the GaLore algorithm (https://huggingface.co/papers/2403.03507) and APOLLO algorithm (https://huggingface.co/papers/2412.05270). See GaLore implementation (https://github.com/jiaweizzhao/GaLore) and APOLLO implementation (https://github.com/zhuhanqing/APOLLO) for more details. @@ -2802,7 +2802,7 @@ class TrainingArguments: Logger log level to use on the main process. Possible choices are the log levels as strings: `"debug"`, `"info"`, `"warning"`, `"error"` and `"critical"`, plus a `"passive"` level which doesn't set anything and lets the application set the level. - report_to (`str` or `List[str]`, *optional*, defaults to `"all"`): + report_to (`str` or `list[str]`, *optional*, defaults to `"all"`): The list of integrations to report the results and logs to. Supported platforms are `"azure_ml"`, `"clearml"`, `"codecarbon"`, `"comet_ml"`, `"dagshub"`, `"dvclive"`, `"flyte"`, `"mlflow"`, `"neptune"`, `"swanlab"`, `"tensorboard"`, and `"wandb"`. Use `"all"` to report to all integrations diff --git a/src/transformers/utils/args_doc.py b/src/transformers/utils/args_doc.py index 7048a223bb6..da8243ff168 100644 --- a/src/transformers/utils/args_doc.py +++ b/src/transformers/utils/args_doc.py @@ -17,7 +17,7 @@ import inspect import os import textwrap from pathlib import Path -from typing import List, Optional, Tuple, Union, get_args +from typing import Optional, Union, get_args import regex as re @@ -239,7 +239,7 @@ class ModelArgs: input_values = { "description": """ Float values of input raw speech waveform. Values can be obtained by loading a `.flac` or `.wav` audio file - into an array of type `List[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install + into an array of type `list[float]` or a `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into `input_values`, the [`AutoProcessor`] should be used for padding and conversion into a tensor of type `torch.FloatTensor`. See [`{processor_class}.__call__`] for details. """, @@ -796,7 +796,7 @@ def parse_docstring(docstring, max_indent_level=0): return params, remainder_docstring -def contains_type(type_hint, target_type) -> Tuple[bool, Optional[object]]: +def contains_type(type_hint, target_type) -> tuple[bool, Optional[object]]: """ Check if a "nested" type hint contains a specific target type, return the first-level type containing the target_type if found. @@ -833,7 +833,7 @@ def get_model_name(obj): return "model" -def get_placeholders_dict(placeholders: List, model_name: str) -> dict: +def get_placeholders_dict(placeholders: list, model_name: str) -> dict: """ Get the dictionary of placeholders for the given model name. """ @@ -880,7 +880,7 @@ def format_args_docstring(args, model_name): return args -def source_args_doc(args_classes: Union[object, List[object]]) -> dict: +def source_args_doc(args_classes: Union[object, list[object]]) -> dict: if isinstance(args_classes, (list, tuple)): args_classes_dict = {} for args_class in args_classes: diff --git a/src/transformers/utils/backbone_utils.py b/src/transformers/utils/backbone_utils.py index a6ab0046bc5..2374455aca8 100644 --- a/src/transformers/utils/backbone_utils.py +++ b/src/transformers/utils/backbone_utils.py @@ -91,9 +91,9 @@ def _align_output_features_output_indices( - `out_indices` and `out_features` set: input `out_indices` and `out_features` are returned. Args: - out_features (`List[str]`): The names of the features for the backbone to output. - out_indices (`List[int]` or `Tuple[int]`): The indices of the features for the backbone to output. - stage_names (`List[str]`): The names of the stages of the backbone. + out_features (`list[str]`): The names of the features for the backbone to output. + out_indices (`list[int]` or `tuple[int]`): The indices of the features for the backbone to output. + stage_names (`list[str]`): The names of the stages of the backbone. """ if out_indices is None and out_features is None: out_indices = [len(stage_names) - 1] @@ -122,9 +122,9 @@ def get_aligned_output_features_output_indices( - `out_indices` and `out_features` set: they are verified to be aligned. Args: - out_features (`List[str]`): The names of the features for the backbone to output. - out_indices (`List[int]` or `Tuple[int]`): The indices of the features for the backbone to output. - stage_names (`List[str]`): The names of the stages of the backbone. + out_features (`list[str]`): The names of the features for the backbone to output. + out_indices (`list[int]` or `tuple[int]`): The indices of the features for the backbone to output. + stage_names (`list[str]`): The names of the stages of the backbone. """ out_indices = list(out_indices) if out_indices is not None else None # First verify that the out_features and out_indices are valid diff --git a/src/transformers/utils/chat_template_utils.py b/src/transformers/utils/chat_template_utils.py index d91dcace910..e30b26f0cb9 100644 --- a/src/transformers/utils/chat_template_utils.py +++ b/src/transformers/utils/chat_template_utils.py @@ -134,13 +134,13 @@ def _parse_type_hint(hint: str) -> dict: f"The type hint {str(hint).replace('typing.', '')} is a Tuple with a single element, which " "we do not automatically convert to JSON schema as it is rarely necessary. If this input can contain " "more than one element, we recommend " - "using a List[] type instead, or if it really is a single element, remove the Tuple[] wrapper and just " + "using a list[] type instead, or if it really is a single element, remove the tuple[] wrapper and just " "pass the element directly." ) if ... in args: raise TypeHintParsingException( "Conversion of '...' is not supported in Tuple type hints. " - "Use List[] types for variable-length" + "Use list[] types for variable-length" " inputs instead." ) return {"type": "array", "prefixItems": [_parse_type_hint(t) for t in args]} diff --git a/src/transformers/utils/fx.py b/src/transformers/utils/fx.py index 07015927265..86bcf00c354 100755 --- a/src/transformers/utils/fx.py +++ b/src/transformers/utils/fx.py @@ -1235,9 +1235,9 @@ class HFTracer(Tracer): root (`torch.nn.Module` or `Callable`): Either a `torch.nn.Module`` or a function to be traced through. If root is not a [`~transformers.PreTrainedModel`], then `dummy_inputs` must be passed, otherwise tracing will fail. - concrete_args (`Dict[str, Any], *optional*): + concrete_args (`dict[str, Any], *optional*): Concrete arguments that should not be treated as Proxies - dummy_inputs (`Dict[str, Any]`, *optional*): + dummy_inputs (`dict[str, Any]`, *optional*): The dummy inputs needed to handle data-dependent control-flow if `root` is not a [`~transformers.PreTrainedModel`]. It can also be used when `root` is a [`~transformers.PreTrainedModel`] to specify custom dummy inputs for a subset or all the model inputs. @@ -1448,7 +1448,7 @@ def symbolic_trace( Args: model ([`PretrainedModel`]): The model to trace. - input_names (`List[str]`, *optional*): + input_names (`list[str]`, *optional*): The names of the inputs of the traced model. If unset, model.dummy_inputs.keys() are used instead. disable_check (`bool`, *optional*, defaults to `False`): If `True`, no check is done before trying to trace the model, this is mostly usesul for debugging purposes. diff --git a/src/transformers/utils/generic.py b/src/transformers/utils/generic.py index 067af41c2e8..cfeaaa4fd4c 100644 --- a/src/transformers/utils/generic.py +++ b/src/transformers/utils/generic.py @@ -26,7 +26,7 @@ from contextlib import ExitStack, contextmanager from dataclasses import fields, is_dataclass from enum import Enum from functools import partial, wraps -from typing import Any, Callable, ContextManager, List, Optional, TypedDict +from typing import Any, Callable, ContextManager, Optional, TypedDict import numpy as np from packaging import version @@ -990,5 +990,5 @@ class GeneralInterface(MutableMapping): def register(cls, key: str, value: Callable): cls._global_mapping.update({key: value}) - def valid_keys(self) -> List[str]: + def valid_keys(self) -> list[str]: return list(self.keys()) diff --git a/src/transformers/utils/hub.py b/src/transformers/utils/hub.py index 3a9852452bd..0f177db15bf 100644 --- a/src/transformers/utils/hub.py +++ b/src/transformers/utils/hub.py @@ -275,7 +275,7 @@ def cached_file( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -341,7 +341,7 @@ def cached_files( This can be either: - a string, the *model id* of a model repo on huggingface.co. - a path to a *directory* potentially containing the file. - filenames (`List[str]`): + filenames (`list[str]`): The name of all the files to locate in `path_or_repo`. cache_dir (`str` or `os.PathLike`, *optional*): Path to a directory in which a downloaded pretrained model configuration should be cached if the standard @@ -352,7 +352,7 @@ def cached_files( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): @@ -590,7 +590,7 @@ def download_url(url, proxies=None): Args: url (`str`): The url of the file to download. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. @@ -881,7 +881,7 @@ class PushToHubMixin: Branch to push the uploaded files to. commit_description (`str`, *optional*): The description of the commit that will be created - tags (`List[str]`, *optional*): + tags (`list[str]`, *optional*): List of tags to push on the Hub. Examples: @@ -1137,7 +1137,7 @@ def create_and_tag_model_card( Args: repo_id (`str`): The repo_id where to look for the model card. - tags (`List[str]`, *optional*): + tags (`list[str]`, *optional*): The list of tags to add in the model card token (`str`, *optional*): Authentication token, obtained with `huggingface_hub.HfApi.login` method. Will default to the stored token. diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index 420955e998e..f4dfdec73e8 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -31,7 +31,7 @@ from enum import Enum from functools import lru_cache from itertools import chain from types import ModuleType -from typing import Any, Dict, FrozenSet, List, Optional, Set, Tuple, Union +from typing import Any, Optional, Union from packaging import version @@ -42,7 +42,7 @@ logger = logging.get_logger(__name__) # pylint: disable=invalid-name # TODO: This doesn't work for all packages (`bs4`, `faiss`, etc.) Talk to Sylvain to see how to do with it better. -def _is_package_available(pkg_name: str, return_version: bool = False) -> Union[Tuple[bool, str], bool]: +def _is_package_available(pkg_name: str, return_version: bool = False) -> Union[tuple[bool, str], bool]: # Check if the package spec exists and grab its version to avoid importing a local directory package_exists = importlib.util.find_spec(pkg_name) is not None package_version = "N/A" @@ -1929,8 +1929,8 @@ def is_torch_fx_proxy(x): return False -BACKENDS_T = FrozenSet[str] -IMPORT_STRUCTURE_T = Dict[BACKENDS_T, Dict[str, Set[str]]] +BACKENDS_T = frozenset[str] +IMPORT_STRUCTURE_T = dict[BACKENDS_T, dict[str, set[str]]] class _LazyModule(ModuleType): @@ -1946,8 +1946,8 @@ class _LazyModule(ModuleType): module_file: str, import_structure: IMPORT_STRUCTURE_T, module_spec: Optional[importlib.machinery.ModuleSpec] = None, - extra_objects: Optional[Dict[str, object]] = None, - explicit_import_shortcut: Optional[Dict[str, List[str]]] = None, + extra_objects: Optional[dict[str, object]] = None, + explicit_import_shortcut: Optional[dict[str, list[str]]] = None, ): super().__init__(name) @@ -2161,7 +2161,7 @@ class VersionComparison(Enum): @lru_cache() -def split_package_version(package_version_str) -> Tuple[str, str, str]: +def split_package_version(package_version_str) -> tuple[str, str, str]: pattern = r"([a-zA-Z0-9_-]+)([!<>=~]+)([0-9.]+)" match = re.match(pattern, package_version_str) if match: diff --git a/src/transformers/utils/metrics.py b/src/transformers/utils/metrics.py index 16379831a97..e5960c5b538 100644 --- a/src/transformers/utils/metrics.py +++ b/src/transformers/utils/metrics.py @@ -2,7 +2,7 @@ import functools import logging import time from enum import Enum -from typing import Any, Callable, List, Optional, Tuple, Union +from typing import Any, Callable, Optional, Union import torch @@ -98,7 +98,7 @@ def traced( *, span_name=None, standalone=False, - additional_attributes: Optional[List[Tuple[str, str, Union[Any, Callable[[Any], Any]]]]] = None, + additional_attributes: Optional[list[tuple[str, str, Union[Any, Callable[[Any], Any]]]]] = None, ): """ Decorator to trace function calls with OpenTelemetry. @@ -301,7 +301,7 @@ class ContinuousBatchProcessorMetrics: logger.warning(f"Failed to record TTFT metric: {e}") @traced - def record_batch_metrics(self, requests_in_batch: List) -> None: + def record_batch_metrics(self, requests_in_batch: list) -> None: """Record metrics about the batch composition including decode/prefill ratio and batch fill percentage. Args: diff --git a/src/transformers/utils/notebook.py b/src/transformers/utils/notebook.py index 22a44d858e7..42e24c77818 100644 --- a/src/transformers/utils/notebook.py +++ b/src/transformers/utils/notebook.py @@ -212,7 +212,7 @@ class NotebookTrainingTracker(NotebookProgressBar): An object tracking the updates of an ongoing training with progress bars and a nice table reporting metrics. Args: - num_steps (`int`): The number of steps during training. column_names (`List[str]`, *optional*): + num_steps (`int`): The number of steps during training. column_names (`list[str]`, *optional*): The list of column names for the metrics table (will be inferred from the first call to [`~utils.notebook.NotebookTrainingTracker.write_line`] if not set). """ @@ -238,7 +238,7 @@ class NotebookTrainingTracker(NotebookProgressBar): Write the values in the inner table. Args: - values (`Dict[str, float]`): The values to display. + values (`dict[str, float]`): The values to display. """ if self.inner_table is None: self.inner_table = [list(values.keys()), list(values.values())] diff --git a/src/transformers/utils/peft_utils.py b/src/transformers/utils/peft_utils.py index 3eb62a09905..0a7263b5a6b 100644 --- a/src/transformers/utils/peft_utils.py +++ b/src/transformers/utils/peft_utils.py @@ -54,7 +54,7 @@ def find_adapter_config_file( resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or *bool*, *optional*): diff --git a/src/transformers/utils/quantization_config.py b/src/transformers/utils/quantization_config.py index 0085b4ae0d6..90ae9c98e46 100644 --- a/src/transformers/utils/quantization_config.py +++ b/src/transformers/utils/quantization_config.py @@ -23,7 +23,7 @@ import os from dataclasses import dataclass, is_dataclass from enum import Enum from inspect import Parameter, signature -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union from packaging import version @@ -106,12 +106,12 @@ class QuantizationConfigMixin: Instantiates a [`QuantizationConfigMixin`] from a Python dictionary of parameters. Args: - config_dict (`Dict[str, Any]`): + config_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the configuration object. return_unused_kwargs (`bool`,*optional*, defaults to `False`): Whether or not to return a list of unused keyword arguments. Used for `from_pretrained` method in `PreTrainedModel`. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the configuration object. Returns: @@ -149,10 +149,10 @@ class QuantizationConfigMixin: writer.write(json_string) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ return copy.deepcopy(self.__dict__) @@ -188,11 +188,11 @@ class QuantizationConfigMixin: returning all the unused kwargs. Args: - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Dictionary of attributes to tentatively update this class. Returns: - `Dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. + `dict[str, Any]`: Dictionary containing all the key-value pairs that were not used to update the instance. """ to_remove = [] for key, value in kwargs.items(): @@ -291,9 +291,9 @@ class HqqConfig(QuantizationConfigMixin): dynamic_config (dict, *optional*): Parameters for dynamic configuration. The key is the name tag of the layer and the value is a quantization config. If set, each layer specified by its id will use its dedicated quantization configuration. - skip_modules (`List[str]`, *optional*, defaults to `['lm_head']`): + skip_modules (`list[str]`, *optional*, defaults to `['lm_head']`): List of `nn.Linear` layers to skip. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from which to initialize the configuration object. """ @@ -304,7 +304,7 @@ class HqqConfig(QuantizationConfigMixin): view_as_float: bool = False, axis: Optional[int] = None, dynamic_config: Optional[dict] = None, - skip_modules: List[str] = ["lm_head"], + skip_modules: list[str] = ["lm_head"], **kwargs, ): if is_hqq_available(): @@ -353,7 +353,7 @@ class HqqConfig(QuantizationConfigMixin): pass @classmethod - def from_dict(cls, config: Dict[str, Any]): + def from_dict(cls, config: dict[str, Any]): """ Override from_dict, used in AutoQuantizationConfig.from_dict in quantizers/auto.py """ @@ -362,10 +362,10 @@ class HqqConfig(QuantizationConfigMixin): instance.skip_modules = config["skip_modules"] return instance - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ return { "quant_config": self.quant_config, @@ -377,12 +377,12 @@ class HqqConfig(QuantizationConfigMixin): config_dict = self.to_dict() return f"{self.__class__.__name__} {json.dumps(config_dict, indent=2, sort_keys=True)}\n" - def to_diff_dict(self) -> Dict[str, Any]: + def to_diff_dict(self) -> dict[str, Any]: """ Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, """ config_dict = self.to_dict() @@ -425,7 +425,7 @@ class BitsAndBytesConfig(QuantizationConfigMixin): These outliers are often in the interval [-60, -6] or [6, 60]. Int8 quantization works well for values of magnitude ~5, but beyond that, there is a significant performance penalty. A good default threshold is 6, but a lower threshold might be needed for more unstable models (small models, fine-tuning). - llm_int8_skip_modules (`List[str]`, *optional*): + llm_int8_skip_modules (`list[str]`, *optional*): An explicit list of the modules that we do not want to convert in 8-bit. This is useful for models such as Jukebox that has several heads in different places and not necessarily at the last position. For example for `CausalLM` models, the last `lm_head` is kept in its original `dtype`. @@ -448,7 +448,7 @@ class BitsAndBytesConfig(QuantizationConfigMixin): quantized again. bnb_4bit_quant_storage (`torch.dtype` or str, *optional*, defaults to `torch.uint8`): This sets the storage type to pack the quanitzed 4-bit prarams. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from which to initialize the configuration object. """ @@ -590,10 +590,10 @@ class BitsAndBytesConfig(QuantizationConfigMixin): else: return None - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ output = copy.deepcopy(self.__dict__) output["bnb_4bit_compute_dtype"] = str(output["bnb_4bit_compute_dtype"]).split(".")[1] @@ -607,13 +607,13 @@ class BitsAndBytesConfig(QuantizationConfigMixin): config_dict = self.to_dict() return f"{self.__class__.__name__} {json.dumps(config_dict, indent=2, sort_keys=True)}\n" - def to_diff_dict(self) -> Dict[str, Any]: + def to_diff_dict(self) -> dict[str, Any]: """ Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, """ config_dict = self.to_dict() @@ -650,7 +650,7 @@ class GPTQConfig(QuantizationConfigMixin): - A string, the *model id* of a predefined tokenizer hosted inside a model repo on huggingface.co. - A path to a *directory* containing vocabulary files required by the tokenizer, for instance saved using the [`~PreTrainedTokenizer.save_pretrained`] method, e.g., `./my_model_directory/`. - dataset (`Union[List[str]]`, *optional*): + dataset (`Union[list[str]]`, *optional*): The dataset used for quantization. You can provide your own dataset in a list of string or just use the original datasets used in GPTQ paper ['wikitext2','c4','c4-new'] group_size (`int`, *optional*, defaults to 128): @@ -668,7 +668,7 @@ class GPTQConfig(QuantizationConfigMixin): quantization using inputs that have passed through the previously quantized layers. checkpoint_format (`str`, *optional*, defaults to `"gptq"`): GPTQ weight format. `gptq`(v1) is supported by both gptqmodel and auto-gptq. `gptq_v2` is gptqmodel only. - meta (`Dict[str, any]`, *optional*): + meta (`dict[str, any]`, *optional*): Properties, such as tooling:version, that do not directly contributes to quantization or quant inference are stored in meta. i.e. `meta.quantizer`: ["optimum:_version_", "gptqmodel:_version_"] backend (`str`, *optional*): @@ -680,7 +680,7 @@ class GPTQConfig(QuantizationConfigMixin): The maximum sequence length that the model can take. block_name_to_quantize (`str`, *optional*): The transformers block name to quantize. If None, we will infer the block name using common patterns (e.g. model.layers) - module_name_preceding_first_block (`List[str]`, *optional*): + module_name_preceding_first_block (`list[str]`, *optional*): The layers that are preceding the first Transformer block. batch_size (`int`, *optional*, defaults to 1): The batch size used when processing the dataset @@ -691,12 +691,12 @@ class GPTQConfig(QuantizationConfigMixin): max_input_length (`int`, *optional*): The maximum input length. This is needed to initialize a buffer that depends on the maximum expected input length. It is specific to the exllama backend with act-order. - exllama_config (`Dict[str, Any]`, *optional*): + exllama_config (`dict[str, Any]`, *optional*): The exllama config. You can specify the version of the exllama kernel through the `version` key. Defaults to `{"version": 1}` if unset. cache_block_outputs (`bool`, *optional*, defaults to `True`): Whether to cache block outputs to reuse as inputs for the succeeding block. - modules_in_block_to_quantize (`List[List[str]]`, *optional*): + modules_in_block_to_quantize (`list[list[str]]`, *optional*): List of list of module names to quantize in the specified block. This argument is useful to exclude certain linear modules from being quantized. The block to quantize can be specified by setting `block_name_to_quantize`. We will quantize each list sequentially. If not set, we will quantize all linear layers. Example: `modules_in_block_to_quantize =[["self_attn.k_proj", "self_attn.v_proj", "self_attn.q_proj"], ["self_attn.o_proj"]]`. @@ -709,26 +709,26 @@ class GPTQConfig(QuantizationConfigMixin): self, bits: int, tokenizer: Any = None, - dataset: Optional[Union[List[str], str]] = None, + dataset: Optional[Union[list[str], str]] = None, group_size: int = 128, damp_percent: float = 0.1, desc_act: bool = False, sym: bool = True, true_sequential: bool = True, checkpoint_format: str = "gptq", - meta: Optional[Dict[str, Any]] = None, + meta: Optional[dict[str, Any]] = None, backend: Optional[str] = None, use_cuda_fp16: bool = False, model_seqlen: Optional[int] = None, block_name_to_quantize: Optional[str] = None, - module_name_preceding_first_block: Optional[List[str]] = None, + module_name_preceding_first_block: Optional[list[str]] = None, batch_size: int = 1, pad_token_id: Optional[int] = None, use_exllama: Optional[bool] = None, max_input_length: Optional[int] = None, - exllama_config: Optional[Dict[str, Any]] = None, + exllama_config: Optional[dict[str, Any]] = None, cache_block_outputs: bool = True, - modules_in_block_to_quantize: Optional[List[List[str]]] = None, + modules_in_block_to_quantize: Optional[list[list[str]]] = None, **kwargs, ): self.quant_method = QuantizationMethod.GPTQ @@ -901,7 +901,7 @@ class AwqConfig(QuantizationConfigMixin): The list of modules to not quantize, useful for quantizing models that explicitly require to have some modules left in their original precision (e.g. Whisper encoder, Llava encoder, Mixtral gate layers). Note you cannot quantize directly with transformers, please refer to `AutoAWQ` documentation for quantizing HF models. - exllama_config (`Dict[str, Any]`, *optional*): + exllama_config (`dict[str, Any]`, *optional*): You can specify the version of the exllama kernel through the `version` key, the maximum sequence length through the `max_input_len` key, and the maximum batch size through the `max_batch_size` key. Defaults to `{"version": 2, "max_input_len": 2048, "max_batch_size": 8}` if unset. @@ -917,8 +917,8 @@ class AwqConfig(QuantizationConfigMixin): do_fuse: Optional[bool] = None, fuse_max_seq_len: Optional[int] = None, modules_to_fuse: Optional[dict] = None, - modules_to_not_convert: Optional[List] = None, - exllama_config: Optional[Dict[str, int]] = None, + modules_to_not_convert: Optional[list] = None, + exllama_config: Optional[dict[str, int]] = None, **kwargs, ): self.quant_method = QuantizationMethod.AWQ @@ -1063,9 +1063,9 @@ class AqlmConfig(QuantizationConfigMixin): Number of codebooks for the Additive Quantization procedure. nbits_per_codebook (`int`, *optional*, defaults to 16): Number of bits encoding a single codebook vector. Codebooks size is 2**nbits_per_codebook. - linear_weights_not_to_quantize (`Optional[List[str]]`, *optional*): + linear_weights_not_to_quantize (`Optional[list[str]]`, *optional*): List of full paths of `nn.Linear` weight parameters that shall not be quantized. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from which to initialize the configuration object. """ @@ -1075,7 +1075,7 @@ class AqlmConfig(QuantizationConfigMixin): out_group_size: int = 1, num_codebooks: int = 1, nbits_per_codebook: int = 16, - linear_weights_not_to_quantize: Optional[List[str]] = None, + linear_weights_not_to_quantize: Optional[list[str]] = None, **kwargs, ): self.quant_method = QuantizationMethod.AQLM @@ -1176,22 +1176,22 @@ class VptqConfig(QuantizationConfigMixin): modules_to_not_convert (`list`, *optional*, default to `None`): The list of modules to not quantize, useful for quantizing models that explicitly require to have some modules left in their original precision (e.g. Whisper encoder, Llava encoder, Mixtral gate layers). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from which to initialize the configuration object. """ def __init__( self, enable_proxy_error: bool = False, - config_for_layers: Dict[str, Any] = {}, - shared_layer_config: Dict[str, Any] = {}, - modules_to_not_convert: Optional[List] = None, + config_for_layers: dict[str, Any] = {}, + shared_layer_config: dict[str, Any] = {}, + modules_to_not_convert: Optional[list] = None, **kwargs, ): self.quant_method = QuantizationMethod.VPTQ self.enable_proxy_error = enable_proxy_error - self.config_for_layers: Dict[str, Any] = config_for_layers - self.shared_layer_config: Dict[str, Any] = shared_layer_config + self.config_for_layers: dict[str, Any] = config_for_layers + self.shared_layer_config: dict[str, Any] = shared_layer_config self.modules_to_not_convert = modules_to_not_convert self.post_init() @@ -1225,7 +1225,7 @@ class QuantoConfig(QuantizationConfigMixin): self, weights="int8", activations=None, - modules_to_not_convert: Optional[List] = None, + modules_to_not_convert: Optional[list] = None, **kwargs, ): self.quant_method = QuantizationMethod.QUANTO @@ -1263,7 +1263,7 @@ class EetqConfig(QuantizationConfigMixin): def __init__( self, weights: str = "int8", - modules_to_not_convert: Optional[List] = None, + modules_to_not_convert: Optional[list] = None, **kwargs, ): self.quant_method = QuantizationMethod.EETQ @@ -1285,7 +1285,7 @@ class CompressedTensorsConfig(QuantizationConfigMixin): This is a wrapper class that handles compressed-tensors quantization config options. It is a wrapper around `compressed_tensors.QuantizationConfig` Args: - config_groups (`typing.Dict[str, typing.Union[ForwardRef('QuantizationScheme'), typing.List[str]]]`, *optional*): + config_groups (`typing.dict[str, typing.Union[ForwardRef('QuantizationScheme'), typing.list[str]]]`, *optional*): dictionary mapping group name to a quantization scheme definition format (`str`, *optional*, defaults to `"dense"`): format the model is represented as. Set `run_compressed` True to execute model as the @@ -1296,9 +1296,9 @@ class CompressedTensorsConfig(QuantizationConfigMixin): specifies quantization of the kv cache. If None, kv cache is not quantized. global_compression_ratio (`typing.Union[float, NoneType]`, *optional*): 0-1 float percentage of model compression - ignore (`typing.Union[typing.List[str], NoneType]`, *optional*): + ignore (`typing.Union[typing.list[str], NoneType]`, *optional*): layer names or types to not quantize, supports regex prefixed by 're:' - sparsity_config (`typing.Dict[str, typing.Any]`, *optional*): + sparsity_config (`typing.dict[str, typing.Any]`, *optional*): configuration for sparsity compression quant_method (`str`, *optional*, defaults to `"compressed-tensors"`): do not override, should be compressed-tensors @@ -1308,13 +1308,13 @@ class CompressedTensorsConfig(QuantizationConfigMixin): def __init__( self, - config_groups: Optional[Dict[str, Union["QuantizationScheme", List[str]]]] = None, # noqa: F821 + config_groups: Optional[dict[str, Union["QuantizationScheme", list[str]]]] = None, # noqa: F821 format: str = "dense", quantization_status: "QuantizationStatus" = "initialized", # noqa: F821 kv_cache_scheme: Optional["QuantizationArgs"] = None, # noqa: F821 global_compression_ratio: Optional[float] = None, - ignore: Optional[List[str]] = None, - sparsity_config: Optional[Dict[str, Any]] = None, + ignore: Optional[list[str]] = None, + sparsity_config: Optional[dict[str, Any]] = None, quant_method: str = "compressed-tensors", run_compressed: bool = True, **kwargs, @@ -1375,12 +1375,12 @@ class CompressedTensorsConfig(QuantizationConfigMixin): Optionally unwraps any args from the nested quantization_config Args: - config_dict (`Dict[str, Any]`): + config_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the configuration object. return_unused_kwargs (`bool`,*optional*, defaults to `False`): Whether or not to return a list of unused keyword arguments. Used for `from_pretrained` method in `PreTrainedModel`. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the configuration object. Returns: @@ -1396,12 +1396,12 @@ class CompressedTensorsConfig(QuantizationConfigMixin): return super().from_dict(config_dict, return_unused_kwargs=return_unused_kwargs, **kwargs) - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Quantization config to be added to config.json Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance. """ quantization_config = {} if self.quantization_config is not None: @@ -1416,12 +1416,12 @@ class CompressedTensorsConfig(QuantizationConfigMixin): return quantization_config - def to_diff_dict(self) -> Dict[str, Any]: + def to_diff_dict(self) -> dict[str, Any]: """ Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, + `dict[str, Any]`: Dictionary of all the attributes that make up this configuration instance, """ config_dict = self.to_dict() @@ -1480,7 +1480,7 @@ class FbgemmFp8Config(QuantizationConfigMixin): def __init__( self, activation_scale_ub: float = 1200.0, - modules_to_not_convert: Optional[List] = None, + modules_to_not_convert: Optional[list] = None, **kwargs, ): self.quant_method = QuantizationMethod.FBGEMM_FP8 @@ -1518,10 +1518,10 @@ class HiggsConfig(QuantizationConfigMixin): self, bits: int = 4, p: int = 2, - modules_to_not_convert: Optional[List[str]] = None, + modules_to_not_convert: Optional[list[str]] = None, hadamard_size: int = 512, group_size: int = 256, - tune_metadata: Optional[Dict[str, Any]] = None, + tune_metadata: Optional[dict[str, Any]] = None, **kwargs, ): if tune_metadata is None: @@ -1554,8 +1554,8 @@ class HiggsConfig(QuantizationConfigMixin): class TorchAoConfig(QuantizationConfigMixin): quant_method: QuantizationMethod quant_type: Union[str, "AOBaseConfig"] # noqa: F821 - modules_to_not_convert: Optional[List] - quant_type_kwargs: Dict[str, Any] + modules_to_not_convert: Optional[list] + quant_type_kwargs: dict[str, Any] include_input_output_embeddings: bool untie_embedding_weights: bool @@ -1575,7 +1575,7 @@ class TorchAoConfig(QuantizationConfigMixin): untie_embedding_weights (`bool`, default to `False`): Whether to untie the weights when we are quantizing input embedding weights that is tied to other weights. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The keyword arguments for the chosen type of quantization, for example, int4_weight_only quantization supports two keyword arguments `group_size` and `inner_k_tiles` currently. More API examples and documentation of arguments can be found in https://github.com/pytorch/ao/tree/main/torchao/quantization#other-available-quantization-techniques @@ -1618,7 +1618,7 @@ class TorchAoConfig(QuantizationConfigMixin): def __init__( self, quant_type: Union[str, "AOBaseConfig"], # noqa: F821 - modules_to_not_convert: Optional[List] = None, + modules_to_not_convert: Optional[list] = None, include_input_output_embeddings: bool = False, untie_embedding_weights: bool = False, **kwargs, @@ -1812,14 +1812,14 @@ class BitNetQuantConfig(QuantizationConfigMixin): of normalizing activations before quantization/packing. rms_norm_eps (`float`, *optional*, defaults to 1e-06): The epsilon value used in the RMSNorm layer for numerical stability. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional keyword arguments that may be used by specific quantization backends or future versions. """ def __init__( self, - modules_to_not_convert: Optional[List] = None, + modules_to_not_convert: Optional[list] = None, linear_class: Optional[str] = "bitlinear", quantization_mode: Optional[str] = "offline", use_rms_norm: Optional[bool] = False, @@ -1861,10 +1861,10 @@ class SpQRConfig(QuantizationConfigMixin): shapes (`Optional`, *optional*): A dictionary holding the shape of each object. We need this because it's impossible to deduce the exact size of the parameters just from bits, beta1, beta2. - modules_to_not_convert (`Optional[List[str]]`, *optional*): + modules_to_not_convert (`Optional[list[str]]`, *optional*): Optionally, provides a list of full paths of `nn.Linear` weight parameters that shall not be quantized. Defaults to None. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional parameters from which to initialize the configuration object. """ @@ -1873,8 +1873,8 @@ class SpQRConfig(QuantizationConfigMixin): bits: int = 3, beta1: int = 16, beta2: int = 16, - shapes: Optional[Dict[str, int]] = None, - modules_to_not_convert: Optional[List[str]] = None, + shapes: Optional[dict[str, int]] = None, + modules_to_not_convert: Optional[list[str]] = None, **kwargs, ): if shapes is None: @@ -1916,7 +1916,7 @@ class FineGrainedFP8Config(QuantizationConfigMixin): Args: activation_scheme (`str`, *optional*, defaults to `"dynamic"`): The scheme used for activation, the defaults and only support scheme for now is "dynamic". - weight_block_size (`typing.Tuple[int, int]`, *optional*, defaults to `(128, 128)`): + weight_block_size (`typing.tuple[int, int]`, *optional*, defaults to `(128, 128)`): The size of the weight blocks for quantization, default is (128, 128). modules_to_not_convert (`list`, *optional*): A list of module names that should not be converted during quantization. @@ -1925,8 +1925,8 @@ class FineGrainedFP8Config(QuantizationConfigMixin): def __init__( self, activation_scheme: str = "dynamic", - weight_block_size: Tuple[int, int] = (128, 128), - modules_to_not_convert: Optional[List] = None, + weight_block_size: tuple[int, int] = (128, 128), + modules_to_not_convert: Optional[list] = None, **kwargs, ): self.quant_method = QuantizationMethod.FP8 diff --git a/src/transformers/video_processing_utils.py b/src/transformers/video_processing_utils.py index c316b8ffb02..87130c7fef7 100644 --- a/src/transformers/video_processing_utils.py +++ b/src/transformers/video_processing_utils.py @@ -17,7 +17,7 @@ import copy import json import os import warnings -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Optional, Union import numpy as np @@ -97,7 +97,7 @@ BASE_VIDEO_PROCESSOR_DOCSTRING = r""" `preprocess` method. do_pad (`bool`, *optional*): Whether to pad the video to the `(max_height, max_width)` of the videos in the batch. - crop_size (`Dict[str, int]` *optional*, defaults to `self.crop_size`): + crop_size (`dict[str, int]` *optional*, defaults to `self.crop_size`): Size of the output video after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` method. do_rescale (`bool`, *optional*, defaults to `self.do_rescale`): @@ -109,11 +109,11 @@ BASE_VIDEO_PROCESSOR_DOCSTRING = r""" do_normalize (`bool`, *optional*, defaults to `self.do_normalize`): Whether to normalize the video. Can be overridden by the `do_normalize` parameter in the `preprocess` method. Can be overridden by the `do_normalize` parameter in the `preprocess` method. - image_mean (`float` or `List[float]`, *optional*, defaults to `self.image_mean`): + image_mean (`float` or `list[float]`, *optional*, defaults to `self.image_mean`): Mean to use if normalizing the video. This is a float or list of floats the length of the number of channels in the video. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be overridden by the `image_mean` parameter in the `preprocess` method. - image_std (`float` or `List[float]`, *optional*, defaults to `self.image_std`): + image_std (`float` or `list[float]`, *optional*, defaults to `self.image_std`): Standard deviation to use if normalizing the video. This is a float or list of floats the length of the number of channels in the video. Can be overridden by the `image_std` parameter in the `preprocess` method. Can be overridden by the `image_std` parameter in the `preprocess` method. @@ -295,7 +295,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): video_metadata: VideoMetadata = None, input_data_format: Optional[Union[str, ChannelDimension]] = None, device: Optional["torch.device"] = None, - ) -> List["torch.Tensor"]: + ) -> list["torch.Tensor"]: """ Prepare the input videos for processing. """ @@ -359,8 +359,8 @@ class BaseVideoProcessor(BaseImageProcessorFast): def _preprocess( self, - videos: List["torch.Tensor"], - video_metadata: Union[List[VideoMetadata], List[dict]], + videos: list["torch.Tensor"], + video_metadata: Union[list[VideoMetadata], list[dict]], do_convert_rgb: bool, do_resize: bool, size: SizeDict, @@ -372,8 +372,8 @@ class BaseVideoProcessor(BaseImageProcessorFast): do_pad: bool, rescale_factor: float, do_normalize: bool, - image_mean: Optional[Union[float, List[float]]], - image_std: Optional[Union[float, List[float]]], + image_mean: Optional[Union[float, list[float]]], + image_std: Optional[Union[float, list[float]]], do_sample_frames: Optional[bool] = None, fps: Optional[int] = None, num_frames: Optional[int] = None, @@ -451,7 +451,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): resume_download: Deprecated and ignored. All downloads are now resumed by default when possible. Will be removed in v5 of Transformers. - proxies (`Dict[str, str]`, *optional*): + proxies (`dict[str, str]`, *optional*): A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request. token (`str` or `bool`, *optional*): @@ -477,7 +477,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): subfolder (`str`, *optional*, defaults to `""`): In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here. - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): The values in kwargs of any keys which are video processor attributes will be used to override the loaded values. Behavior concerning key/value pairs whose keys are *not* video processor attributes is controlled by the `return_unused_kwargs` keyword parameter. @@ -543,7 +543,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). - kwargs (`Dict[str, Any]`, *optional*): + kwargs (`dict[str, Any]`, *optional*): Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. """ use_auth_token = kwargs.pop("use_auth_token", None) @@ -595,7 +595,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): @classmethod def get_video_processor_dict( cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs - ) -> Tuple[Dict[str, Any], Dict[str, Any]]: + ) -> tuple[dict[str, Any], dict[str, Any]]: """ From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used for instantiating a video processor of type [`~video_processing_utils.VideoProcessorBase`] using `from_dict`. @@ -608,7 +608,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): specify the folder name here. Returns: - `Tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the video processor object. + `tuple[Dict, Dict]`: The dictionary(ies) that will be used to instantiate the video processor object. """ cache_dir = kwargs.pop("cache_dir", None) force_download = kwargs.pop("force_download", False) @@ -723,16 +723,16 @@ class BaseVideoProcessor(BaseImageProcessorFast): return video_processor_dict, kwargs @classmethod - def from_dict(cls, video_processor_dict: Dict[str, Any], **kwargs): + def from_dict(cls, video_processor_dict: dict[str, Any], **kwargs): """ Instantiates a type of [`~video_processing_utils.VideoProcessorBase`] from a Python dictionary of parameters. Args: - video_processor_dict (`Dict[str, Any]`): + video_processor_dict (`dict[str, Any]`): Dictionary that will be used to instantiate the video processor object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [`~video_processing_utils.VideoProcessorBase.to_dict`] method. - kwargs (`Dict[str, Any]`): + kwargs (`dict[str, Any]`): Additional parameters from which to initialize the video processor object. Returns: @@ -767,12 +767,12 @@ class BaseVideoProcessor(BaseImageProcessorFast): else: return video_processor - def to_dict(self) -> Dict[str, Any]: + def to_dict(self) -> dict[str, Any]: """ Serializes this instance to a Python dictionary. Returns: - `Dict[str, Any]`: Dictionary of all the attributes that make up this video processor instance. + `dict[str, Any]`: Dictionary of all the attributes that make up this video processor instance. """ output = copy.deepcopy(self.__dict__) output["video_processor_type"] = self.__class__.__name__ @@ -859,7 +859,7 @@ class BaseVideoProcessor(BaseImageProcessorFast): cls._auto_class = auto_class - def fetch_videos(self, video_url_or_urls: Union[str, List[str]]): + def fetch_videos(self, video_url_or_urls: Union[str, list[str]]): """ Convert a single or a list of urls into the corresponding `np.array` objects. diff --git a/src/transformers/video_utils.py b/src/transformers/video_utils.py index 7e23e377d8b..71594bb6bc2 100644 --- a/src/transformers/video_utils.py +++ b/src/transformers/video_utils.py @@ -18,7 +18,7 @@ from collections.abc import Iterable from contextlib import redirect_stdout from dataclasses import dataclass from io import BytesIO -from typing import Callable, Dict, List, Optional, Tuple, Union +from typing import Callable, Optional, Union from urllib.parse import urlparse import numpy as np @@ -56,14 +56,14 @@ logger = logging.get_logger(__name__) VideoInput = Union[ - List["PIL.Image.Image"], + list["PIL.Image.Image"], "np.ndarray", "torch.Tensor", - List["np.ndarray"], - List["torch.Tensor"], - List[List["PIL.Image.Image"]], - List[List["np.ndarrray"]], - List[List["torch.Tensor"]], + list["np.ndarray"], + list["torch.Tensor"], + list[list["PIL.Image.Image"]], + list[list["np.ndarrray"]], + list[list["torch.Tensor"]], ] # noqa @@ -118,7 +118,7 @@ def is_scaled_video(video: np.ndarray) -> bool: return np.min(video) >= 0 and np.max(video) <= 1 -def convert_pil_frames_to_video(videos: List[VideoInput]) -> List[Union["np.ndarray", "torch.Tensor"]]: +def convert_pil_frames_to_video(videos: list[VideoInput]) -> list[Union["np.ndarray", "torch.Tensor"]]: """ Given a batch of videos, converts each video to a 4D array. If video is already in array type, it is simply returned. We assume that all inputs in the list are in the same format, based on the type of the first element. @@ -139,7 +139,7 @@ def convert_pil_frames_to_video(videos: List[VideoInput]) -> List[Union["np.ndar return video_converted -def make_batched_videos(videos) -> List[Union["np.ndarray", "torch.Tensor"]]: +def make_batched_videos(videos) -> list[Union["np.ndarray", "torch.Tensor"]]: """ Ensure that the input is a list of videos. If the input is a single video, it is converted to a list of length 1. If the input is a batch of videos, it is converted to a list of 4D video arrays. Videos passed as list `PIL.Image` @@ -170,7 +170,7 @@ def make_batched_videos(videos) -> List[Union["np.ndarray", "torch.Tensor"]]: return convert_pil_frames_to_video(videos) -def get_video_size(video: np.ndarray, channel_dim: ChannelDimension = None) -> Tuple[int, int]: +def get_video_size(video: np.ndarray, channel_dim: ChannelDimension = None) -> tuple[int, int]: """ Returns the (height, width) dimensions of the video. @@ -270,7 +270,7 @@ def read_video_opencv( return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int) Returns: - Tuple[`np.array`, `VideoMetadata`]: A tuple containing: + tuple[`np.array`, `VideoMetadata`]: A tuple containing: - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]). - `VideoMetadata` object. """ @@ -327,7 +327,7 @@ def read_video_decord( return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int) Returns: - Tuple[`np.array`, `VideoMetadata`]: A tuple containing: + tuple[`np.array`, `VideoMetadata`]: A tuple containing: - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]). - `VideoMetadata` object. """ @@ -370,7 +370,7 @@ def read_video_pyav( return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int) Returns: - Tuple[`np.array`, `VideoMetadata`]: A tuple containing: + tuple[`np.array`, `VideoMetadata`]: A tuple containing: - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]). - `VideoMetadata` object. """ @@ -421,7 +421,7 @@ def read_video_torchvision( return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int) Returns: - Tuple[`np.array`, `VideoMetadata`]: A tuple containing: + tuple[`np.array`, `VideoMetadata`]: A tuple containing: - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]). - `VideoMetadata` object. """ @@ -490,7 +490,7 @@ def load_video( return np.linspace(0, metadata.total_num_frames - 1, num_frames, dtype=int) Returns: - Tuple[`np.array`, Dict]: A tuple containing: + tuple[`np.array`, Dict]: A tuple containing: - Numpy array of frames in RGB (shape: [num_frames, height, width, 3]). - Metadata dictionary. """ @@ -601,7 +601,7 @@ def convert_to_rgb( def pad( video: np.ndarray, - padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], + padding: Union[int, tuple[int, int], Iterable[tuple[int, int]]], mode: PaddingMode = PaddingMode.CONSTANT, constant_values: Union[float, Iterable[float]] = 0.0, data_format: Optional[Union[str, ChannelDimension]] = None, @@ -613,7 +613,7 @@ def pad( Args: video (`np.ndarray`): The video to pad. - padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): + padding (`int` or `tuple[int, int]` or `Iterable[tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can be one of three formats: - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. - `((before, after),)` yields same before and after pad for height and width. @@ -689,8 +689,8 @@ def pad( def group_videos_by_shape( - videos: List["torch.Tensor"], -) -> Tuple[Dict[Tuple[int, int], List["torch.Tensor"]], Dict[int, Tuple[Tuple[int, int], int]]]: + videos: list["torch.Tensor"], +) -> tuple[dict[tuple[int, int], list["torch.Tensor"]], dict[int, tuple[tuple[int, int], int]]]: """ Groups videos by shape. Returns a dictionary with the shape as key and a list of videos with that shape as value, @@ -712,8 +712,8 @@ def group_videos_by_shape( def reorder_videos( - processed_videos: Dict[Tuple[int, int], "torch.Tensor"], grouped_videos_index: Dict[int, Tuple[int, int]] -) -> List["torch.Tensor"]: + processed_videos: dict[tuple[int, int], "torch.Tensor"], grouped_videos_index: dict[int, tuple[int, int]] +) -> list["torch.Tensor"]: """ Reconstructs a list of videos in the original order. """ diff --git a/tests/models/markuplm/test_modeling_markuplm.py b/tests/models/markuplm/test_modeling_markuplm.py index b1c31f5e4b2..7bec38704e3 100644 --- a/tests/models/markuplm/test_modeling_markuplm.py +++ b/tests/models/markuplm/test_modeling_markuplm.py @@ -309,7 +309,7 @@ class MarkupLMModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase feature_extractor_name, processor_name, ): - # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]` + # ValueError: Nodes must be of type `list[str]` (single pretokenized example), or `list[list[str]]` # (batch of pretokenized examples). return True diff --git a/tests/models/timesfm/test_modeling_timesfm.py b/tests/models/timesfm/test_modeling_timesfm.py index 47615ab843c..486e55daf7f 100644 --- a/tests/models/timesfm/test_modeling_timesfm.py +++ b/tests/models/timesfm/test_modeling_timesfm.py @@ -15,7 +15,6 @@ import inspect import unittest -from typing import List import numpy as np import torch @@ -52,7 +51,7 @@ class TimesFmModelTester: num_heads: int = 2, tolerance: float = 1e-6, rms_norm_eps: float = 1e-6, - quantiles: List[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], + quantiles: list[float] = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9], pad_val: float = 1123581321.0, use_positional_embedding: bool = True, initializer_factor: float = 0.0, diff --git a/tests/models/univnet/test_feature_extraction_univnet.py b/tests/models/univnet/test_feature_extraction_univnet.py index 85a26bee874..51a5fb7724d 100644 --- a/tests/models/univnet/test_feature_extraction_univnet.py +++ b/tests/models/univnet/test_feature_extraction_univnet.py @@ -248,7 +248,7 @@ class UnivNetFeatureExtractionTest(SequenceFeatureExtractionTestMixin, unittest. for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2): self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3)) - # Test np.ndarray vs List[np.ndarray] + # Test np.ndarray vs list[np.ndarray] encoded_sequences_1 = feature_extractor(np_speech_inputs, return_tensors="np").input_features encoded_sequences_2 = feature_extractor([np_speech_inputs], return_tensors="np").input_features for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2): diff --git a/tests/test_pipeline_mixin.py b/tests/test_pipeline_mixin.py index 9f6a85b6b12..f71954f76a6 100644 --- a/tests/test_pipeline_mixin.py +++ b/tests/test_pipeline_mixin.py @@ -280,13 +280,13 @@ class PipelineTesterMixin: A model repository id on the Hub. model_architecture (`type`): A subclass of `PretrainedModel` or `PretrainedModel`. - tokenizer_names (`List[str]`): + tokenizer_names (`list[str]`): A list of names of a subclasses of `PreTrainedTokenizerFast` or `PreTrainedTokenizer`. - image_processor_names (`List[str]`): + image_processor_names (`list[str]`): A list of names of subclasses of `BaseImageProcessor`. - feature_extractor_names (`List[str]`): + feature_extractor_names (`list[str]`): A list of names of subclasses of `FeatureExtractionMixin`. - processor_names (`List[str]`): + processor_names (`list[str]`): A list of names of subclasses of `ProcessorMixin`. commit (`str`): The commit hash of the model repository on the Hub. diff --git a/tests/test_sequence_feature_extraction_common.py b/tests/test_sequence_feature_extraction_common.py index 1c704a4e54a..cde16deb75e 100644 --- a/tests/test_sequence_feature_extraction_common.py +++ b/tests/test_sequence_feature_extraction_common.py @@ -123,7 +123,7 @@ class SequenceFeatureExtractionTestMixin(FeatureExtractionSavingTestMixin): batch_size = self.feat_extract_tester.batch_size feature_size = self.feat_extract_tester.feature_size - # test padding for List[int] + numpy + # test padding for list[int] + numpy input_1 = feat_extract.pad(processed_features, padding=False) input_1 = input_1[input_name] @@ -157,7 +157,7 @@ class SequenceFeatureExtractionTestMixin(FeatureExtractionSavingTestMixin): if feature_size > 1: self.assertTrue(input_4.shape[2] == input_5.shape[2] == feature_size) - # test padding for `pad_to_multiple_of` for List[int] + numpy + # test padding for `pad_to_multiple_of` for list[int] + numpy input_6 = feat_extract.pad(processed_features, pad_to_multiple_of=10) input_6 = input_6[input_name] @@ -319,7 +319,7 @@ class SequenceFeatureExtractionTestMixin(FeatureExtractionSavingTestMixin): with self.assertRaises(ValueError): feat_extract.pad(processed_features, padding="max_length", truncation=True)[input_name] - # test truncation for `pad_to_multiple_of` for List[int] + numpy + # test truncation for `pad_to_multiple_of` for list[int] + numpy pad_to_multiple_of = 12 input_8 = feat_extract.pad( processed_features, diff --git a/tests/utils/test_hf_argparser.py b/tests/utils/test_hf_argparser.py index dbcc5b00660..773f244008a 100644 --- a/tests/utils/test_hf_argparser.py +++ b/tests/utils/test_hf_argparser.py @@ -22,7 +22,7 @@ from argparse import Namespace from dataclasses import dataclass, field from enum import Enum from pathlib import Path -from typing import List, Literal, Optional, Union, get_args, get_origin +from typing import Literal, Optional, Union, get_args, get_origin import yaml @@ -121,7 +121,7 @@ class StringLiteralAnnotationExample: required_enum: "BasicEnum" = field() opt: "Optional[bool]" = None baz: "str" = field(default="toto", metadata={"help": "help message"}) - foo_str: "List[str]" = list_field(default=["Hallo", "Bonjour", "Hello"]) + foo_str: "list[str]" = list_field(default=["Hallo", "Bonjour", "Hello"]) if is_python_no_less_than_3_10: @@ -435,11 +435,11 @@ class HfArgumentParserTest(unittest.TestCase): for field in fields.values(): # First verify raw dict - if field.type in (dict, dict): + if field.type is dict: raw_dict_fields.append(field) # Next check for `Union` or `Optional` elif get_origin(field.type) == Union: - if any(arg in (dict, dict) for arg in get_args(field.type)): + if any(arg is dict for arg in get_args(field.type)): optional_dict_fields.append(field) # First check: anything in `raw_dict_fields` is very bad @@ -455,12 +455,15 @@ class HfArgumentParserTest(unittest.TestCase): args = get_args(field.type) # These should be returned as `dict`, `str`, ... # we only care about the first two - self.assertIn(args[0], (dict, dict)) - self.assertEqual( - str(args[1]), - "", - f"Expected field `{field.name}` to have a type signature of at least `typing.Union[dict,str,...]` for CLI compatibility, " - "but `str` not found. Please fix this.", + self.assertIn( + dict, + args, + f"Expected field `{field.name}` to have a type signature of at least `typing.Union[dict,str,...]` for CLI compatibility, but `dict` not found. Please fix this.", + ) + self.assertIn( + str, + args, + f"Expected field `{field.name}` to have a type signature of at least `typing.Union[dict,str,...]` for CLI compatibility, but `str` not found. Please fix this.", ) # Second check: anything in `optional_dict_fields` is bad if it's not in `base_list` diff --git a/utils/check_copies.py b/utils/check_copies.py index 8a468717551..d50d244ebe2 100644 --- a/utils/check_copies.py +++ b/utils/check_copies.py @@ -42,7 +42,7 @@ import os import re import subprocess from collections import OrderedDict -from typing import List, Optional, Tuple, Union +from typing import Optional, Union from transformers.utils import direct_transformers_import @@ -253,7 +253,7 @@ def _sanity_check_splits(splits_1, splits_2, is_class, filename): raise ValueError(f"In {filename}, two code blocks expected to be copies have different structures.") -def find_block_end(lines: List[str], start_index: int, indent: int) -> int: +def find_block_end(lines: list[str], start_index: int, indent: int) -> int: """ Find the end of the class/func block starting at `start_index` in a source code (defined by `lines`). @@ -282,8 +282,8 @@ def find_block_end(lines: List[str], start_index: int, indent: int) -> int: def split_code_into_blocks( - lines: List[str], start_index: int, end_index: int, indent: int, backtrace: bool = False -) -> List[Tuple[str, int, int]]: + lines: list[str], start_index: int, end_index: int, indent: int, backtrace: bool = False +) -> list[tuple[str, int, int]]: """ Split the class/func block starting at `start_index` in a source code (defined by `lines`) into *inner blocks*. @@ -391,7 +391,7 @@ def split_code_into_blocks( def find_code_in_transformers( object_name: str, base_path: Optional[str] = None, return_indices: bool = False -) -> Union[str, Tuple[List[str], int, int]]: +) -> Union[str, tuple[list[str], int, int]]: """ Find and return the source code of an object. @@ -640,7 +640,7 @@ def check_codes_match(observed_code: str, theoretical_code: str) -> Optional[int def is_copy_consistent( filename: str, overwrite: bool = False, buffer: Optional[dict] = None -) -> Optional[List[Tuple[str, int]]]: +) -> Optional[list[tuple[str, int]]]: """ Check if the code commented as a copy in a file matches the original. @@ -936,7 +936,7 @@ def get_model_list(filename: str, start_prompt: str, end_prompt: str) -> str: return "".join(result) -def convert_to_localized_md(model_list: str, localized_model_list: str, format_str: str) -> Tuple[bool, str]: +def convert_to_localized_md(model_list: str, localized_model_list: str, format_str: str) -> tuple[bool, str]: """ Compare the model list from the main README to the one in a localized README. diff --git a/utils/check_doc_toc.py b/utils/check_doc_toc.py index f90ffc2cc91..9de67488eec 100644 --- a/utils/check_doc_toc.py +++ b/utils/check_doc_toc.py @@ -33,7 +33,6 @@ python utils/check_doc_toc.py --fix_and_overwrite import argparse from collections import defaultdict -from typing import List import yaml @@ -41,7 +40,7 @@ import yaml PATH_TO_TOC = "docs/source/en/_toctree.yml" -def clean_model_doc_toc(model_doc: List[dict]) -> List[dict]: +def clean_model_doc_toc(model_doc: list[dict]) -> list[dict]: """ Cleans a section of the table of content of the model documentation (one specific modality) by removing duplicates and sorting models alphabetically. diff --git a/utils/check_docstrings.py b/utils/check_docstrings.py index 00bc5a12206..1c2ce9c04da 100644 --- a/utils/check_docstrings.py +++ b/utils/check_docstrings.py @@ -42,7 +42,7 @@ import operator as op import os import re from pathlib import Path -from typing import Any, Optional, Tuple, Union +from typing import Any, Optional, Union from check_repo import ignore_undocumented from git import Repo @@ -788,7 +788,7 @@ def find_source_file(obj: Any) -> Path: return obj_file.with_suffix(".py") -def match_docstring_with_signature(obj: Any) -> Optional[Tuple[str, str]]: +def match_docstring_with_signature(obj: Any) -> Optional[tuple[str, str]]: """ Matches the docstring of an object with its signature. diff --git a/utils/check_dummies.py b/utils/check_dummies.py index 48a6b2fa718..f429e8b14a7 100644 --- a/utils/check_dummies.py +++ b/utils/check_dummies.py @@ -37,7 +37,7 @@ python utils/check_dummies.py --fix_and_overwrite import argparse import os import re -from typing import Dict, List, Optional +from typing import Optional # All paths are set with the intent you should run this script from the root of the repo with the command @@ -92,7 +92,7 @@ def find_backend(line: str) -> Optional[str]: return "_and_".join(backends) -def read_init() -> Dict[str, List[str]]: +def read_init() -> dict[str, list[str]]: """ Read the init and extract backend-specific objects. @@ -156,7 +156,7 @@ def create_dummy_object(name: str, backend_name: str) -> str: return DUMMY_CLASS.format(name, backend_name) -def create_dummy_files(backend_specific_objects: Optional[Dict[str, List[str]]] = None) -> Dict[str, str]: +def create_dummy_files(backend_specific_objects: Optional[dict[str, list[str]]] = None) -> dict[str, str]: """ Create the content of the dummy files. diff --git a/utils/check_inits.py b/utils/check_inits.py index 9b42aede791..b392d09a898 100644 --- a/utils/check_inits.py +++ b/utils/check_inits.py @@ -39,7 +39,7 @@ import collections import os import re from pathlib import Path -from typing import Dict, List, Optional, Tuple +from typing import Optional # Path is set with the intent you should run this script from the root of the repo. @@ -89,7 +89,7 @@ def find_backend(line: str) -> Optional[str]: return "_and_".join(backends) -def parse_init(init_file) -> Optional[Tuple[Dict[str, List[str]], Dict[str, List[str]]]]: +def parse_init(init_file) -> Optional[tuple[dict[str, list[str]], dict[str, list[str]]]]: """ Read an init_file and parse (per backend) the `_import_structure` objects defined and the `TYPE_CHECKING` objects defined. @@ -232,7 +232,7 @@ def parse_init(init_file) -> Optional[Tuple[Dict[str, List[str]], Dict[str, List return import_dict_objects, type_hint_objects -def analyze_results(import_dict_objects: Dict[str, List[str]], type_hint_objects: Dict[str, List[str]]) -> List[str]: +def analyze_results(import_dict_objects: dict[str, list[str]], type_hint_objects: dict[str, list[str]]) -> list[str]: """ Analyze the differences between _import_structure objects and TYPE_CHECKING objects found in an init. @@ -279,7 +279,7 @@ def analyze_results(import_dict_objects: Dict[str, List[str]], type_hint_objects return errors -def get_transformers_submodules() -> List[str]: +def get_transformers_submodules() -> list[str]: """ Returns the list of Transformers submodules. """ diff --git a/utils/check_repo.py b/utils/check_repo.py index 22cfecab4ef..1706e7345b5 100644 --- a/utils/check_repo.py +++ b/utils/check_repo.py @@ -40,7 +40,6 @@ from collections import OrderedDict from difflib import get_close_matches from importlib.machinery import ModuleSpec from pathlib import Path -from typing import List, Tuple from transformers import is_flax_available, is_tf_available, is_torch_available from transformers.models.auto.auto_factory import get_values @@ -470,7 +469,7 @@ def check_model_list(): # If some modeling modules should be ignored for all checks, they should be added in the nested list # _ignore_modules of this function. -def get_model_modules() -> List[str]: +def get_model_modules() -> list[str]: """Get all the model modules inside the transformers library (except deprecated models).""" _ignore_modules = [ "modeling_auto", @@ -502,7 +501,7 @@ def get_model_modules() -> List[str]: return modules -def get_models(module: types.ModuleType, include_pretrained: bool = False) -> List[Tuple[str, type]]: +def get_models(module: types.ModuleType, include_pretrained: bool = False) -> list[tuple[str, type]]: """ Get the objects in a module that are models. @@ -564,7 +563,7 @@ def check_models_are_in_init(): # If some test_modeling files should be ignored when checking models are all tested, they should be added in the # nested list _ignore_files of this function. -def get_model_test_files() -> List[str]: +def get_model_test_files() -> list[str]: """ Get the model test files. @@ -605,7 +604,7 @@ def get_model_test_files() -> List[str]: # This is a bit hacky but I didn't find a way to import the test_file as a module and read inside the tester class # for the all_model_classes variable. -def find_tested_models(test_file: str) -> List[str]: +def find_tested_models(test_file: str) -> list[str]: """ Parse the content of test_file to detect what's in `all_model_classes`. This detects the models that inherit from the common test class. @@ -640,7 +639,7 @@ def should_be_tested(model_name: str) -> bool: return not is_building_block(model_name) -def check_models_are_tested(module: types.ModuleType, test_file: str) -> List[str]: +def check_models_are_tested(module: types.ModuleType, test_file: str) -> list[str]: """Check models defined in a module are all tested in a given file. Args: @@ -696,7 +695,7 @@ def check_all_models_are_tested(): raise Exception(f"There were {len(failures)} failures:\n" + "\n".join(failures)) -def get_all_auto_configured_models() -> List[str]: +def get_all_auto_configured_models() -> list[str]: """Return the list of all models in at least one auto class.""" result = set() # To avoid duplicates we concatenate all model classes in a set. if is_torch_available(): @@ -725,7 +724,7 @@ def ignore_unautoclassed(model_name: str) -> bool: return False -def check_models_are_auto_configured(module: types.ModuleType, all_auto_models: List[str]) -> List[str]: +def check_models_are_auto_configured(module: types.ModuleType, all_auto_models: list[str]) -> list[str]: """ Check models defined in module are each in an auto class. @@ -916,7 +915,7 @@ def check_objects_being_equally_in_main_init(): _re_decorator = re.compile(r"^\s*@(\S+)\s+$") -def check_decorator_order(filename: str) -> List[int]: +def check_decorator_order(filename: str) -> list[int]: """ Check that in a given test file, the slow decorator is always last. @@ -958,7 +957,7 @@ def check_all_decorator_order(): ) -def find_all_documented_objects() -> List[str]: +def find_all_documented_objects() -> list[str]: """ Parse the content of all doc files to detect which classes and functions it documents. diff --git a/utils/custom_init_isort.py b/utils/custom_init_isort.py index 7d3635c71b5..3d476c809e8 100644 --- a/utils/custom_init_isort.py +++ b/utils/custom_init_isort.py @@ -38,7 +38,7 @@ python utils/custom_init_isort.py --check_only import argparse import os import re -from typing import Any, Callable, List, Optional +from typing import Any, Callable, Optional # Path is defined with the intent you should run this script from the root of the repo. @@ -64,7 +64,7 @@ def get_indent(line: str) -> str: def split_code_in_indented_blocks( code: str, indent_level: str = "", start_prompt: Optional[str] = None, end_prompt: Optional[str] = None -) -> List[str]: +) -> list[str]: """ Split some code into its indented blocks, starting at a given level. @@ -140,7 +140,7 @@ def ignore_underscore_and_lowercase(key: Callable[[Any], str]) -> Callable[[Any] return _inner -def sort_objects(objects: List[Any], key: Optional[Callable[[Any], str]] = None) -> List[Any]: +def sort_objects(objects: list[Any], key: Optional[Callable[[Any], str]] = None) -> list[Any]: """ Sort a list of objects following the rules of isort (all uppercased first, camel-cased second and lower-cased last). diff --git a/utils/deprecate_models.py b/utils/deprecate_models.py index db449ef00a0..5e20ab396b6 100644 --- a/utils/deprecate_models.py +++ b/utils/deprecate_models.py @@ -9,7 +9,7 @@ import argparse import os from collections import defaultdict from pathlib import Path -from typing import Optional, Tuple +from typing import Optional import requests from custom_init_isort import sort_imports_in_all_inits @@ -77,7 +77,7 @@ def insert_tip_to_model_doc(model_doc_path, tip_message): f.write("\n".join(new_model_lines)) -def get_model_doc_path(model: str) -> Tuple[Optional[str], Optional[str]]: +def get_model_doc_path(model: str) -> tuple[Optional[str], Optional[str]]: # Possible variants of the model name in the model doc path model_names = [model, model.replace("_", "-"), model.replace("_", "")] diff --git a/utils/modular_model_converter.py b/utils/modular_model_converter.py index 26ff34396cb..ae272de41fa 100644 --- a/utils/modular_model_converter.py +++ b/utils/modular_model_converter.py @@ -19,7 +19,7 @@ import os import re from abc import ABC, abstractmethod from collections import Counter, defaultdict, deque -from typing import Dict, Optional, Set, Union +from typing import Optional, Union import libcst as cst from check_copies import run_ruff @@ -197,7 +197,7 @@ def get_full_attribute_name(node: Union[cst.Attribute, cst.Name]) -> Optional[st # Transformer class to replace ClassB.call_to_method and ClassB().call_to_method with super().call_to_method class ReplaceMethodCallTransformer(cst.CSTTransformer): - def __init__(self, all_bases: Set[str]): + def __init__(self, all_bases: set[str]): self.all_bases = all_bases def leave_Attribute(self, original_node: cst.Attribute, updated_node: cst.Attribute) -> cst.CSTNode: @@ -473,7 +473,7 @@ class SuperTransformer(cst.CSTTransformer): def find_all_dependencies( - dependency_mapping: Dict[str, set], + dependency_mapping: dict[str, set], start_entity: Optional[str] = None, initial_dependencies: Optional[set] = None, initial_checked_dependencies: Optional[set] = None, @@ -636,11 +636,11 @@ class ModuleMapper(CSTVisitor, ABC): def __init__(self, python_module: cst.Module): # fmt: off self.python_module: cst.Module = python_module # original cst.Module being visited - self.classes: Dict[str, cst.ClassDef] = {} # mapping from class names to Nodes (it will be ordered by default!!) + self.classes: dict[str, cst.ClassDef] = {} # mapping from class names to Nodes (it will be ordered by default!!) self.imports = [] # stores all import statements - self.functions: Dict[str, cst.FunctionDef] = {} # mapping of global scope function names to Nodes + self.functions: dict[str, cst.FunctionDef] = {} # mapping of global scope function names to Nodes self.object_dependency_mapping = defaultdict(set) # immediate function/assignment dependency mapping (i.e. dependencies immediately in the function/assignment definition) - self.assignments: Dict[str, cst.SimpleStatementLine] = {} # mapping of global assignments names to Nodes + self.assignments: dict[str, cst.SimpleStatementLine] = {} # mapping of global assignments names to Nodes self.current_function = None # this keeps track of the current module-scope function self.current_class = None # this keeps track of the current module-scope class self.current_assignment = None # this keeps track of the current module-scope assignment @@ -1266,8 +1266,8 @@ class ModularFileMapper(ModuleMapper): # fmt: off self.model_name = new_name # name of the model being defined. Should be in the format of `llama` or `layout_xlm` or `phi3` - self.model_specific_imported_objects: Dict[str, str] = {} # e.g. {"LlamaModel": "transformers.models.llama.modeling_llama"} - self.model_specific_modules: Dict[str, cst.Module] = {} # e.g. {"transformers.models.llama.modeling_llama": cst.Module} + self.model_specific_imported_objects: dict[str, str] = {} # e.g. {"LlamaModel": "transformers.models.llama.modeling_llama"} + self.model_specific_modules: dict[str, cst.Module] = {} # e.g. {"transformers.models.llama.modeling_llama": cst.Module} self.all_all_to_add = {} # fmt: on diff --git a/utils/notification_service.py b/utils/notification_service.py index b8e997ccc71..850ec337739 100644 --- a/utils/notification_service.py +++ b/utils/notification_service.py @@ -21,7 +21,7 @@ import os import re import sys import time -from typing import Any, Dict, List, Optional, Union +from typing import Any, Optional, Union import requests from get_ci_error_statistics import get_jobs @@ -109,7 +109,7 @@ def handle_stacktraces(test_results): return stacktraces -def dicts_to_sum(objects: Union[Dict[str, Dict], List[dict]]): +def dicts_to_sum(objects: Union[dict[str, dict], list[dict]]): if isinstance(objects, dict): lists = objects.values() else: @@ -126,9 +126,9 @@ class Message: self, title: str, ci_title: str, - model_results: Dict, - additional_results: Dict, - selected_warnings: Optional[List] = None, + model_results: dict, + additional_results: dict, + selected_warnings: Optional[list] = None, prev_ci_artifacts=None, other_ci_artifacts=None, ): @@ -203,15 +203,15 @@ class Message: return f"{int(hours)}h{int(minutes)}m{int(seconds)}s" @property - def header(self) -> Dict: + def header(self) -> dict: return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property - def ci_title_section(self) -> Dict: + def ci_title_section(self) -> dict: return {"type": "section", "text": {"type": "mrkdwn", "text": self.ci_title}} @property - def no_failures(self) -> Dict: + def no_failures(self) -> dict: return { "type": "section", "text": { @@ -227,7 +227,7 @@ class Message: } @property - def failures(self) -> Dict: + def failures(self) -> dict: return { "type": "section", "text": { @@ -246,7 +246,7 @@ class Message: } @property - def warnings(self) -> Dict: + def warnings(self) -> dict: # If something goes wrong, let's avoid the CI report failing to be sent. button_text = "Check warnings (Link not found)" # Use the workflow run link @@ -287,7 +287,7 @@ class Message: return f"{'0'.rjust(rjust)} | {str(report['multi']).rjust(rjust)} | " @property - def category_failures(self) -> Dict: + def category_failures(self) -> dict: if job_name != "run_models_gpu": category_failures_report = "" return {"type": "section", "text": {"type": "mrkdwn", "text": category_failures_report}} @@ -360,7 +360,7 @@ class Message: return entries_changed @property - def model_failures(self) -> List[Dict]: + def model_failures(self) -> list[dict]: # Obtain per-model failures def per_model_sum(model_category_dict): return dicts_to_sum(model_category_dict["failed"].values()) @@ -523,7 +523,7 @@ class Message: return model_failure_sections @property - def additional_failures(self) -> Dict: + def additional_failures(self) -> dict: failures = {k: v["failed"] for k, v in self.additional_results.items()} errors = {k: v["error"] for k, v in self.additional_results.items()} @@ -953,7 +953,7 @@ def retrieve_available_artifacts(): def add_path(self, path: str, gpu: Optional[str] = None): self.paths.append({"name": self.name, "path": path, "gpu": gpu}) - _available_artifacts: Dict[str, Artifact] = {} + _available_artifacts: dict[str, Artifact] = {} directories = filter(os.path.isdir, os.listdir()) for directory in directories: diff --git a/utils/notification_service_doc_tests.py b/utils/notification_service_doc_tests.py index d944f73b637..2e006655ce6 100644 --- a/utils/notification_service_doc_tests.py +++ b/utils/notification_service_doc_tests.py @@ -16,7 +16,6 @@ import json import os import re import time -from typing import Dict, List from get_ci_error_statistics import get_jobs from slack_sdk import WebClient @@ -60,7 +59,7 @@ def extract_first_line_failure(failures_short_lines): class Message: - def __init__(self, title: str, doc_test_results: Dict): + def __init__(self, title: str, doc_test_results: dict): self.title = title self.n_success = sum(job_result["n_success"] for job_result in doc_test_results.values()) @@ -90,11 +89,11 @@ class Message: return f"{int(hours)}h{int(minutes)}m{int(seconds)}s" @property - def header(self) -> Dict: + def header(self) -> dict: return {"type": "header", "text": {"type": "plain_text", "text": self.title}} @property - def no_failures(self) -> Dict: + def no_failures(self) -> dict: return { "type": "section", "text": { @@ -110,7 +109,7 @@ class Message: } @property - def failures(self) -> Dict: + def failures(self) -> dict: return { "type": "section", "text": { @@ -129,7 +128,7 @@ class Message: } @property - def category_failures(self) -> List[Dict]: + def category_failures(self) -> list[dict]: failure_blocks = [] MAX_ERROR_TEXT = 3000 - len("The following examples had failures:\n\n\n\n") - len("[Truncated]\n") @@ -301,7 +300,7 @@ def retrieve_available_artifacts(): def add_path(self, path: str): self.paths.append({"name": self.name, "path": path}) - _available_artifacts: Dict[str, Artifact] = {} + _available_artifacts: dict[str, Artifact] = {} directories = filter(os.path.isdir, os.listdir()) for directory in directories: diff --git a/utils/pr_slow_ci_models.py b/utils/pr_slow_ci_models.py index 4403ade4980..4f6e8015711 100644 --- a/utils/pr_slow_ci_models.py +++ b/utils/pr_slow_ci_models.py @@ -31,7 +31,6 @@ import os.path import re import string from pathlib import Path -from typing import List from git import Repo @@ -39,7 +38,7 @@ from git import Repo PATH_TO_REPO = Path(__file__).parent.parent.resolve() -def get_new_python_files_between_commits(base_commit: str, commits: List[str]) -> List[str]: +def get_new_python_files_between_commits(base_commit: str, commits: list[str]) -> list[str]: """ Get the list of added python files between a base commit and one or several commits. @@ -64,7 +63,7 @@ def get_new_python_files_between_commits(base_commit: str, commits: List[str]) - return code_diff -def get_new_python_files(diff_with_last_commit=False) -> List[str]: +def get_new_python_files(diff_with_last_commit=False) -> list[str]: """ Return a list of python files that have been added between the current head and the main branch. diff --git a/utils/tests_fetcher.py b/utils/tests_fetcher.py index afdd35b9a8d..8e1e520b628 100644 --- a/utils/tests_fetcher.py +++ b/utils/tests_fetcher.py @@ -59,7 +59,7 @@ import re import tempfile from contextlib import contextmanager from pathlib import Path -from typing import Dict, List, Optional, Tuple, Union +from typing import Optional, Union from git import Repo @@ -182,7 +182,7 @@ def keep_doc_examples_only(content: str) -> str: return "\n".join(lines_to_keep) -def get_all_tests() -> List[str]: +def get_all_tests() -> list[str]: """ Walks the `tests` folder to return a list of files/subfolders. This is used to split the tests to run when using parallelism. The split is: @@ -263,7 +263,7 @@ def diff_contains_doc_examples(repo: Repo, branching_point: str, filename: str) return old_content_clean != new_content_clean -def get_impacted_files_from_tiny_model_summary(diff_with_last_commit: bool = False) -> List[str]: +def get_impacted_files_from_tiny_model_summary(diff_with_last_commit: bool = False) -> list[str]: """ Return a list of python modeling files that are impacted by the changes of `tiny_model_summary.json` in between: @@ -379,7 +379,7 @@ def get_impacted_files_from_tiny_model_summary(diff_with_last_commit: bool = Fal return sorted(files) -def get_diff(repo: Repo, base_commit: str, commits: List[str]) -> List[str]: +def get_diff(repo: Repo, base_commit: str, commits: list[str]) -> list[str]: """ Get the diff between a base commit and one or several commits. @@ -421,7 +421,7 @@ def get_diff(repo: Repo, base_commit: str, commits: List[str]) -> List[str]: return code_diff -def get_modified_python_files(diff_with_last_commit: bool = False) -> List[str]: +def get_modified_python_files(diff_with_last_commit: bool = False) -> list[str]: """ Return a list of python files that have been modified between: @@ -451,7 +451,7 @@ def get_modified_python_files(diff_with_last_commit: bool = False) -> List[str]: return get_diff(repo, repo.head.commit, parent_commits) -def get_diff_for_doctesting(repo: Repo, base_commit: str, commits: List[str]) -> List[str]: +def get_diff_for_doctesting(repo: Repo, base_commit: str, commits: list[str]) -> list[str]: """ Get the diff in doc examples between a base commit and one or several commits. @@ -492,7 +492,7 @@ def get_diff_for_doctesting(repo: Repo, base_commit: str, commits: List[str]) -> return code_diff -def get_all_doctest_files() -> List[str]: +def get_all_doctest_files() -> list[str]: """ Return the complete list of python and Markdown files on which we run doctest. @@ -525,7 +525,7 @@ def get_all_doctest_files() -> List[str]: return sorted(test_files_to_run) -def get_new_doctest_files(repo, base_commit, branching_commit) -> List[str]: +def get_new_doctest_files(repo, base_commit, branching_commit) -> list[str]: """ Get the list of files that were removed from "utils/not_doctested.txt", between `base_commit` and `branching_commit`. @@ -552,7 +552,7 @@ def get_new_doctest_files(repo, base_commit, branching_commit) -> List[str]: return [] -def get_doctest_files(diff_with_last_commit: bool = False) -> List[str]: +def get_doctest_files(diff_with_last_commit: bool = False) -> list[str]: """ Return a list of python and Markdown files where doc example have been modified between: @@ -621,7 +621,7 @@ _re_single_line_direct_imports = re.compile(r"(?:^|\n)\s*from\s+transformers(\S* _re_multi_line_direct_imports = re.compile(r"(?:^|\n)\s*from\s+transformers(\S*)\s+import\s+\(([^\)]+)\)") -def extract_imports(module_fname: str, cache: Optional[Dict[str, List[str]]] = None) -> List[str]: +def extract_imports(module_fname: str, cache: Optional[dict[str, list[str]]] = None) -> list[str]: """ Get the imports a given module makes. @@ -703,7 +703,7 @@ def extract_imports(module_fname: str, cache: Optional[Dict[str, List[str]]] = N return result -def get_module_dependencies(module_fname: str, cache: Optional[Dict[str, List[str]]] = None) -> List[str]: +def get_module_dependencies(module_fname: str, cache: Optional[dict[str, list[str]]] = None) -> list[str]: """ Refines the result of `extract_imports` to remove subfolders and get a proper list of module filenames: if a file as an import `from utils import Foo, Bar`, with `utils` being a subfolder containing many files, this will traverse @@ -786,7 +786,7 @@ def get_module_dependencies(module_fname: str, cache: Optional[Dict[str, List[st return dependencies -def create_reverse_dependency_tree() -> List[Tuple[str, str]]: +def create_reverse_dependency_tree() -> list[tuple[str, str]]: """ Create a list of all edges (a, b) which mean that modifying a impacts b with a going over all module and test files. """ @@ -800,7 +800,7 @@ def create_reverse_dependency_tree() -> List[Tuple[str, str]]: return list(set(edges)) -def get_tree_starting_at(module: str, edges: List[Tuple[str, str]]) -> List[Union[str, List[str]]]: +def get_tree_starting_at(module: str, edges: list[tuple[str, str]]) -> list[Union[str, list[str]]]: """ Returns the tree starting at a given module following all edges. @@ -861,7 +861,7 @@ def print_tree_deps_of(module, all_edges=None): print(line[0]) -def init_test_examples_dependencies() -> Tuple[Dict[str, List[str]], List[str]]: +def init_test_examples_dependencies() -> tuple[dict[str, list[str]], list[str]]: """ The test examples do not import from the examples (which are just scripts, not modules) so we need some extra care initializing the dependency map, which is the goal of this function. It initializes the dependency map for @@ -897,7 +897,7 @@ def init_test_examples_dependencies() -> Tuple[Dict[str, List[str]], List[str]]: return test_example_deps, all_examples -def create_reverse_dependency_map() -> Dict[str, List[str]]: +def create_reverse_dependency_map() -> dict[str, list[str]]: """ Create the dependency map from module/test filename to the list of modules/tests that depend on it recursively. @@ -953,8 +953,8 @@ def create_reverse_dependency_map() -> Dict[str, List[str]]: def create_module_to_test_map( - reverse_map: Optional[Dict[str, List[str]]] = None, filter_models: bool = False -) -> Dict[str, List[str]]: + reverse_map: Optional[dict[str, list[str]]] = None, filter_models: bool = False +) -> dict[str, list[str]]: """ Extract the tests from the reverse_dependency_map and potentially filters the model tests. @@ -1108,7 +1108,7 @@ def infer_tests_to_run( f.write(" ".join(doctest_list)) -def filter_tests(output_file: str, filters: List[str]): +def filter_tests(output_file: str, filters: list[str]): """ Reads the content of the output file and filters out all the tests in a list of given folders. @@ -1135,7 +1135,7 @@ def filter_tests(output_file: str, filters: List[str]): f.write(" ".join(test_files)) -def parse_commit_message(commit_message: str) -> Dict[str, bool]: +def parse_commit_message(commit_message: str) -> dict[str, bool]: """ Parses the commit message to detect if a command is there to skip, force all or part of the CI. diff --git a/utils/update_metadata.py b/utils/update_metadata.py index d8df28c2a32..41b3c9a7add 100755 --- a/utils/update_metadata.py +++ b/utils/update_metadata.py @@ -34,7 +34,6 @@ import collections import os import re import tempfile -from typing import Dict, List, Tuple import pandas as pd from datasets import Dataset @@ -124,7 +123,7 @@ PIPELINE_TAGS_AND_AUTO_MODELS = [ ] -def camel_case_split(identifier: str) -> List[str]: +def camel_case_split(identifier: str) -> list[str]: """ Split a camel-cased name into words. @@ -213,7 +212,7 @@ def get_frameworks_table() -> pd.DataFrame: return pd.DataFrame(data) -def update_pipeline_and_auto_class_table(table: Dict[str, Tuple[str, str]]) -> Dict[str, Tuple[str, str]]: +def update_pipeline_and_auto_class_table(table: dict[str, tuple[str, str]]) -> dict[str, tuple[str, str]]: """ Update the table mapping models to pipelines and auto classes without removing old keys if they don't exist anymore.