From 67c2dbdb54cd1d52d01ecdb1968f9ffafb79ee83 Mon Sep 17 00:00:00 2001 From: Yih-Dar <2521628+ydshieh@users.noreply.github.com> Date: Tue, 21 Mar 2023 19:22:01 +0100 Subject: [PATCH] Time to Say Goodbye, torch 1.7 and 1.8 (#22291) * time to say goodbye, torch 1.7 and 1.8 * clean up torch_int_div * clean up is_torch_less_than_1_8-9 * update --------- Co-authored-by: ydshieh --- setup.py | 2 +- src/transformers/dependency_versions_table.py | 2 +- src/transformers/file_utils.py | 1 - src/transformers/generation/utils.py | 11 ++++++----- src/transformers/modeling_utils.py | 5 ----- .../models/big_bird/modeling_big_bird.py | 4 ++-- .../modeling_bigbird_pegasus.py | 3 +-- .../image_processing_conditional_detr.py | 6 ++---- .../modeling_conditional_detr.py | 5 ++--- .../data2vec/modeling_data2vec_audio.py | 3 +-- .../image_processing_deformable_detr.py | 6 ++---- .../modeling_deformable_detr.py | 6 +++--- .../models/deta/image_processing_deta.py | 3 +-- src/transformers/models/deta/modeling_deta.py | 6 +++--- src/transformers/models/detr/modeling_detr.py | 3 +-- .../models/hubert/modeling_hubert.py | 3 +-- .../models/layoutlmv2/modeling_layoutlmv2.py | 8 +++++--- .../image_processing_mask2former.py | 4 +--- .../maskformer/image_processing_maskformer.py | 4 +--- .../models/mctct/modeling_mctct.py | 7 ------- .../oneformer/image_processing_oneformer.py | 4 +--- src/transformers/models/sew/modeling_sew.py | 3 +-- .../models/sew_d/modeling_sew_d.py | 4 ++-- .../models/speecht5/modeling_speecht5.py | 3 +-- .../modeling_table_transformer.py | 3 +-- .../models/tapas/modeling_tapas.py | 3 +-- .../models/unispeech/modeling_unispeech.py | 3 +-- .../unispeech_sat/modeling_unispeech_sat.py | 3 +-- .../models/wav2vec2/modeling_wav2vec2.py | 3 +-- .../modeling_wav2vec2_conformer.py | 3 +-- .../models/wavlm/modeling_wavlm.py | 3 +-- src/transformers/onnx/convert.py | 4 ---- src/transformers/pytorch_utils.py | 12 ------------ src/transformers/utils/__init__.py | 1 - src/transformers/utils/import_utils.py | 9 +-------- tests/generation/test_beam_search.py | 9 ++++++--- tests/models/bloom/test_modeling_bloom.py | 6 +----- tests/models/mctct/test_modeling_mctct.py | 4 ---- tests/models/swin/test_modeling_swin.py | 5 +---- .../test_modeling_trajectory_transformer.py | 4 ---- .../models/wav2vec2/test_modeling_wav2vec2.py | 19 ++++++------------- tests/onnx/test_onnx_v2.py | 10 ---------- 42 files changed, 61 insertions(+), 149 deletions(-) diff --git a/setup.py b/setup.py index c28387a3d4..70197c209f 100644 --- a/setup.py +++ b/setup.py @@ -171,7 +171,7 @@ _deps = [ "timeout-decorator", "timm", "tokenizers>=0.11.1,!=0.11.3,<0.14", - "torch>=1.7,!=1.12.0", + "torch>=1.9,!=1.12.0", "torchaudio", "torchvision", "pyctcdecode>=0.4.0", diff --git a/src/transformers/dependency_versions_table.py b/src/transformers/dependency_versions_table.py index aa638a6a9f..aa23c1ad06 100644 --- a/src/transformers/dependency_versions_table.py +++ b/src/transformers/dependency_versions_table.py @@ -77,7 +77,7 @@ deps = { "timeout-decorator": "timeout-decorator", "timm": "timm", "tokenizers": "tokenizers>=0.11.1,!=0.11.3,<0.14", - "torch": "torch>=1.7,!=1.12.0", + "torch": "torch>=1.9,!=1.12.0", "torchaudio": "torchaudio", "torchvision": "torchvision", "pyctcdecode": "pyctcdecode>=0.4.0", diff --git a/src/transformers/file_utils.py b/src/transformers/file_utils.py index 263ebc94e3..da24760118 100644 --- a/src/transformers/file_utils.py +++ b/src/transformers/file_utils.py @@ -115,7 +115,6 @@ from .utils import ( is_torch_cuda_available, is_torch_fx_available, is_torch_fx_proxy, - is_torch_onnx_dict_inputs_support_available, is_torch_tf32_available, is_torch_tpu_available, is_torchaudio_available, diff --git a/src/transformers/generation/utils.py b/src/transformers/generation/utils.py index b0b4e577d3..eb0e0ed4c2 100644 --- a/src/transformers/generation/utils.py +++ b/src/transformers/generation/utils.py @@ -32,7 +32,6 @@ from ..models.auto import ( MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING, MODEL_FOR_VISION_2_SEQ_MAPPING, ) -from ..pytorch_utils import torch_int_div from ..utils import ModelOutput, logging from .beam_constraints import DisjunctiveConstraint, PhrasalConstraint from .beam_search import BeamScorer, BeamSearchScorer, ConstrainedBeamSearchScorer @@ -2795,7 +2794,7 @@ class GenerationMixin: next_token_scores, 2 * num_beams, dim=1, largest=True, sorted=True ) - next_indices = torch_int_div(next_tokens, vocab_size) + next_indices = torch.div(next_tokens, vocab_size, rounding_mode="floor") next_tokens = next_tokens % vocab_size # stateless @@ -3129,7 +3128,7 @@ class GenerationMixin: next_token_scores, _indices = torch.sort(next_token_scores, descending=True, dim=1) next_tokens = torch.gather(next_tokens, -1, _indices) - next_indices = torch_int_div(next_tokens, vocab_size) + next_indices = torch.div(next_tokens, vocab_size, rounding_mode="floor") next_tokens = next_tokens % vocab_size # stateless @@ -3473,7 +3472,7 @@ class GenerationMixin: next_token_scores, 2 * group_size, dim=1, largest=True, sorted=True ) - next_indices = torch_int_div(next_tokens, vocab_size) + next_indices = torch.div(next_tokens, vocab_size, rounding_mode="floor") next_tokens = next_tokens % vocab_size # stateless @@ -3503,7 +3502,9 @@ class GenerationMixin: # (beam_idx // group_size) -> batch_idx # (beam_idx % group_size) -> offset of idx inside the group reordering_indices[batch_group_indices] = ( - num_beams * torch_int_div(beam_idx, group_size) + group_start_idx + (beam_idx % group_size) + num_beams * torch.div(beam_idx, group_size, rounding_mode="floor") + + group_start_idx + + (beam_idx % group_size) ) # Store scores, attentions and hidden_states when required diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 901677a604..94bbe4a9d5 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -539,9 +539,6 @@ def _move_model_to_meta(model, loaded_state_dict_keys, start_prefix): """ - # meta device was added in pt=1.9 - require_version_core("torch>=1.9") - # dematerialize param storage for keys that are going to be replaced by state_dict, by # putting those on the meta device for k in loaded_state_dict_keys: @@ -2100,8 +2097,6 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix raise ValueError("Passing along a `device_map` requires `low_cpu_mem_usage=True`") if low_cpu_mem_usage: - # low_cpu_mem_usage requires PyTorch >= 1.9 to have the meta device. - require_version_core("torch>=1.9") if device_map is not None: # The max memory utils require PyTorch >= 1.10 to have torch.cuda.mem_get_info. require_version_core("torch>=1.10") diff --git a/src/transformers/models/big_bird/modeling_big_bird.py b/src/transformers/models/big_bird/modeling_big_bird.py index ba3b82555f..4f90deb506 100755 --- a/src/transformers/models/big_bird/modeling_big_bird.py +++ b/src/transformers/models/big_bird/modeling_big_bird.py @@ -37,7 +37,7 @@ from ...modeling_outputs import ( TokenClassifierOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import apply_chunking_to_forward, torch_int_div +from ...pytorch_utils import apply_chunking_to_forward from ...utils import ( ModelOutput, add_code_sample_docstrings, @@ -972,7 +972,7 @@ class BigBirdBlockSparseAttention(nn.Module): num_indices_to_pick_from = params.shape[2] shift = torch.arange(indices.shape[0] * indices.shape[1] * num_indices_to_gather, device=indices.device) - indices_shift = torch_int_div(shift, num_indices_to_gather) * num_indices_to_pick_from + indices_shift = torch.div(shift, num_indices_to_gather, rounding_mode="floor") * num_indices_to_pick_from flattened_indices = indices.view(-1) + indices_shift flattened_params = params.reshape(-1, params.shape[-2], params.shape[-1]) diff --git a/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py b/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py index 1def588a51..ad22199e66 100755 --- a/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py +++ b/src/transformers/models/bigbird_pegasus/modeling_bigbird_pegasus.py @@ -36,7 +36,6 @@ from ...modeling_outputs import ( Seq2SeqSequenceClassifierOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( add_code_sample_docstrings, add_end_docstrings, @@ -791,7 +790,7 @@ class BigBirdPegasusBlockSparseAttention(nn.Module): num_indices_to_pick_from = params.shape[2] shift = torch.arange(indices.shape[0] * indices.shape[1] * num_indices_to_gather, device=indices.device) - indices_shift = torch_int_div(shift, num_indices_to_gather) * num_indices_to_pick_from + indices_shift = torch.div(shift, num_indices_to_gather, rounding_mode="floor") * num_indices_to_pick_from flattened_indices = indices.view(-1) + indices_shift flattened_params = params.reshape(-1, params.shape[-2], params.shape[-1]) 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 a496f7787f..8a146ccea0 100644 --- a/src/transformers/models/conditional_detr/image_processing_conditional_detr.py +++ b/src/transformers/models/conditional_detr/image_processing_conditional_detr.py @@ -68,8 +68,6 @@ if is_torch_available(): import torch from torch import nn - from transformers.pytorch_utils import torch_int_div - if is_vision_available(): import PIL @@ -1314,7 +1312,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): prob = out_logits.sigmoid() topk_values, topk_indexes = torch.topk(prob.view(out_logits.shape[0], -1), 300, dim=1) scores = topk_values - topk_boxes = torch_int_div(topk_indexes, out_logits.shape[2]) + topk_boxes = torch.div(topk_indexes, out_logits.shape[2], rounding_mode="floor") labels = topk_indexes % out_logits.shape[2] boxes = center_to_corners_format(out_bbox) boxes = torch.gather(boxes, 1, topk_boxes.unsqueeze(-1).repeat(1, 1, 4)) @@ -1360,7 +1358,7 @@ class ConditionalDetrImageProcessor(BaseImageProcessor): prob = out_logits.sigmoid() topk_values, topk_indexes = torch.topk(prob.view(out_logits.shape[0], -1), 100, dim=1) scores = topk_values - topk_boxes = torch_int_div(topk_indexes, out_logits.shape[2]) + topk_boxes = torch.div(topk_indexes, out_logits.shape[2], rounding_mode="floor") labels = topk_indexes % out_logits.shape[2] boxes = center_to_corners_format(out_bbox) boxes = torch.gather(boxes, 1, topk_boxes.unsqueeze(-1).repeat(1, 1, 4)) diff --git a/src/transformers/models/conditional_detr/modeling_conditional_detr.py b/src/transformers/models/conditional_detr/modeling_conditional_detr.py index eb67b1e25b..6110fb4491 100644 --- a/src/transformers/models/conditional_detr/modeling_conditional_detr.py +++ b/src/transformers/models/conditional_detr/modeling_conditional_detr.py @@ -26,7 +26,6 @@ from torch import Tensor, nn from ...activations import ACT2FN from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithCrossAttentions, Seq2SeqModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_start_docstrings, @@ -452,7 +451,7 @@ class ConditionalDetrSinePositionEmbedding(nn.Module): x_embed = x_embed / (x_embed[:, :, -1:] + 1e-6) * self.scale dim_t = torch.arange(self.embedding_dim, dtype=torch.float32, device=pixel_values.device) - dim_t = self.temperature ** (2 * torch_int_div(dim_t, 2) / self.embedding_dim) + dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / self.embedding_dim) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t @@ -504,7 +503,7 @@ def build_position_encoding(config): def gen_sine_position_embeddings(pos_tensor): scale = 2 * math.pi dim_t = torch.arange(128, dtype=torch.float32, device=pos_tensor.device) - dim_t = 10000 ** (2 * torch_int_div(dim_t, 2) / 128) + dim_t = 10000 ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / 128) x_embed = pos_tensor[:, :, 0] * scale y_embed = pos_tensor[:, :, 1] * scale pos_x = x_embed[:, :, None] / dim_t diff --git a/src/transformers/models/data2vec/modeling_data2vec_audio.py b/src/transformers/models/data2vec/modeling_data2vec_audio.py index e659d30781..168f342acd 100755 --- a/src/transformers/models/data2vec/modeling_data2vec_audio.py +++ b/src/transformers/models/data2vec/modeling_data2vec_audio.py @@ -35,7 +35,6 @@ from ...modeling_outputs import ( XVectorOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_data2vec_audio import Data2VecAudioConfig @@ -731,7 +730,7 @@ class Data2VecAudioPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) 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 01ff544719..07cafe149e 100644 --- a/src/transformers/models/deformable_detr/image_processing_deformable_detr.py +++ b/src/transformers/models/deformable_detr/image_processing_deformable_detr.py @@ -68,8 +68,6 @@ if is_torch_available(): import torch from torch import nn - from ...pytorch_utils import torch_int_div - if is_vision_available(): import PIL @@ -1312,7 +1310,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): prob = out_logits.sigmoid() topk_values, topk_indexes = torch.topk(prob.view(out_logits.shape[0], -1), 100, dim=1) scores = topk_values - topk_boxes = torch_int_div(topk_indexes, out_logits.shape[2]) + topk_boxes = torch.div(topk_indexes, out_logits.shape[2], rounding_mode="floor") labels = topk_indexes % out_logits.shape[2] boxes = center_to_corners_format(out_bbox) boxes = torch.gather(boxes, 1, topk_boxes.unsqueeze(-1).repeat(1, 1, 4)) @@ -1357,7 +1355,7 @@ class DeformableDetrImageProcessor(BaseImageProcessor): prob = out_logits.sigmoid() topk_values, topk_indexes = torch.topk(prob.view(out_logits.shape[0], -1), 100, dim=1) scores = topk_values - topk_boxes = torch_int_div(topk_indexes, out_logits.shape[2]) + topk_boxes = torch.div(topk_indexes, out_logits.shape[2], rounding_mode="floor") labels = topk_indexes % out_logits.shape[2] boxes = center_to_corners_format(out_bbox) boxes = torch.gather(boxes, 1, topk_boxes.unsqueeze(-1).repeat(1, 1, 4)) diff --git a/src/transformers/models/deformable_detr/modeling_deformable_detr.py b/src/transformers/models/deformable_detr/modeling_deformable_detr.py index edb1b8349e..bc6959ddcc 100755 --- a/src/transformers/models/deformable_detr/modeling_deformable_detr.py +++ b/src/transformers/models/deformable_detr/modeling_deformable_detr.py @@ -41,7 +41,7 @@ from ...file_utils import ( ) from ...modeling_outputs import BaseModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import meshgrid, torch_int_div +from ...pytorch_utils import meshgrid from ...utils import is_ninja_available, logging from ..auto import AutoBackbone from .configuration_deformable_detr import DeformableDetrConfig @@ -497,7 +497,7 @@ class DeformableDetrSinePositionEmbedding(nn.Module): x_embed = (x_embed - 0.5) / (x_embed[:, :, -1:] + eps) * self.scale dim_t = torch.arange(self.embedding_dim, dtype=torch.float32, device=pixel_values.device) - dim_t = self.temperature ** (2 * torch_int_div(dim_t, 2) / self.embedding_dim) + dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / self.embedding_dim) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t @@ -1552,7 +1552,7 @@ class DeformableDetrModel(DeformableDetrPreTrainedModel): scale = 2 * math.pi dim_t = torch.arange(num_pos_feats, dtype=torch.float32, device=proposals.device) - dim_t = temperature ** (2 * torch_int_div(dim_t, 2) / num_pos_feats) + dim_t = temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / num_pos_feats) # batch_size, num_queries, 4 proposals = proposals.sigmoid() * scale # batch_size, num_queries, 4, 128 diff --git a/src/transformers/models/deta/image_processing_deta.py b/src/transformers/models/deta/image_processing_deta.py index 8afc69fd31..eda4fdff16 100644 --- a/src/transformers/models/deta/image_processing_deta.py +++ b/src/transformers/models/deta/image_processing_deta.py @@ -63,7 +63,6 @@ from ...utils.generic import ExplicitEnum, TensorType if is_torch_available(): import torch - from ...pytorch_utils import torch_int_div if is_torchvision_available(): from torchvision.ops.boxes import batched_nms @@ -967,7 +966,7 @@ class DetaImageProcessor(BaseImageProcessor): all_scores = prob.view(batch_size, num_queries * num_labels).to(out_logits.device) all_indexes = torch.arange(num_queries * num_labels)[None].repeat(batch_size, 1).to(out_logits.device) - all_boxes = torch_int_div(all_indexes, out_logits.shape[2]) + all_boxes = torch.div(all_indexes, out_logits.shape[2], rounding_mode="floor") all_labels = all_indexes % out_logits.shape[2] boxes = center_to_corners_format(out_bbox) diff --git a/src/transformers/models/deta/modeling_deta.py b/src/transformers/models/deta/modeling_deta.py index 33706069c4..6fd2e8fdd1 100644 --- a/src/transformers/models/deta/modeling_deta.py +++ b/src/transformers/models/deta/modeling_deta.py @@ -36,7 +36,7 @@ from ...file_utils import ( ) from ...modeling_outputs import BaseModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import meshgrid, torch_int_div +from ...pytorch_utils import meshgrid from ...utils import is_torchvision_available, logging, requires_backends from ..auto import AutoBackbone from .configuration_deta import DetaConfig @@ -399,7 +399,7 @@ class DetaSinePositionEmbedding(nn.Module): x_embed = (x_embed - 0.5) / (x_embed[:, :, -1:] + eps) * self.scale dim_t = torch.arange(self.embedding_dim, dtype=torch.float32, device=pixel_values.device) - dim_t = self.temperature ** (2 * torch_int_div(dim_t, 2) / self.embedding_dim) + dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / self.embedding_dim) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t @@ -1463,7 +1463,7 @@ class DetaModel(DetaPreTrainedModel): scale = 2 * math.pi dim_t = torch.arange(num_pos_feats, dtype=torch.float32, device=proposals.device) - dim_t = temperature ** (2 * torch_int_div(dim_t, 2) / num_pos_feats) + dim_t = temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / num_pos_feats) # batch_size, num_queries, 4 proposals = proposals.sigmoid() * scale # batch_size, num_queries, 4, 128 diff --git a/src/transformers/models/detr/modeling_detr.py b/src/transformers/models/detr/modeling_detr.py index 814a09c37b..bd4eae1283 100644 --- a/src/transformers/models/detr/modeling_detr.py +++ b/src/transformers/models/detr/modeling_detr.py @@ -26,7 +26,6 @@ from torch import Tensor, nn from ...activations import ACT2FN from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithCrossAttentions, Seq2SeqModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_start_docstrings, @@ -442,7 +441,7 @@ class DetrSinePositionEmbedding(nn.Module): x_embed = x_embed / (x_embed[:, :, -1:] + 1e-6) * self.scale dim_t = torch.arange(self.embedding_dim, dtype=torch.float32, device=pixel_values.device) - dim_t = self.temperature ** (2 * torch_int_div(dim_t, 2) / self.embedding_dim) + dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / self.embedding_dim) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t diff --git a/src/transformers/models/hubert/modeling_hubert.py b/src/transformers/models/hubert/modeling_hubert.py index 8c7a1a1516..1c24938256 100755 --- a/src/transformers/models/hubert/modeling_hubert.py +++ b/src/transformers/models/hubert/modeling_hubert.py @@ -27,7 +27,6 @@ from ...activations import ACT2FN from ...deepspeed import is_deepspeed_zero3_enabled from ...modeling_outputs import BaseModelOutput, CausalLMOutput, SequenceClassifierOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( add_code_sample_docstrings, add_start_docstrings, @@ -871,7 +870,7 @@ class HubertPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py b/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py index 9e9c7033f1..5a6f39ce31 100755 --- a/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py +++ b/src/transformers/models/layoutlmv2/modeling_layoutlmv2.py @@ -31,7 +31,7 @@ from ...modeling_outputs import ( TokenClassifierOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import apply_chunking_to_forward, torch_int_div +from ...pytorch_utils import apply_chunking_to_forward from ...utils import ( add_start_docstrings, add_start_docstrings_to_model_forward, @@ -770,7 +770,7 @@ class LayoutLMv2Model(LayoutLMv2PreTrainedModel): return embeddings def _calc_visual_bbox(self, image_feature_pool_shape, bbox, device, final_shape): - visual_bbox_x = torch_int_div( + visual_bbox_x = torch.div( torch.arange( 0, 1000 * (image_feature_pool_shape[1] + 1), @@ -779,8 +779,9 @@ class LayoutLMv2Model(LayoutLMv2PreTrainedModel): dtype=bbox.dtype, ), self.config.image_feature_pool_shape[1], + rounding_mode="floor", ) - visual_bbox_y = torch_int_div( + visual_bbox_y = torch.div( torch.arange( 0, 1000 * (self.config.image_feature_pool_shape[0] + 1), @@ -789,6 +790,7 @@ class LayoutLMv2Model(LayoutLMv2PreTrainedModel): dtype=bbox.dtype, ), self.config.image_feature_pool_shape[0], + rounding_mode="floor", ) visual_bbox = torch.stack( [ diff --git a/src/transformers/models/mask2former/image_processing_mask2former.py b/src/transformers/models/mask2former/image_processing_mask2former.py index 07642faf24..234e784bd6 100644 --- a/src/transformers/models/mask2former/image_processing_mask2former.py +++ b/src/transformers/models/mask2former/image_processing_mask2former.py @@ -57,8 +57,6 @@ if is_torch_available(): import torch from torch import nn - from ...pytorch_utils import torch_int_div - # Copied from transformers.models.detr.image_processing_detr.max_across_indices def max_across_indices(values: Iterable[Any]) -> List[Any]: @@ -1009,7 +1007,7 @@ class Mask2FormerImageProcessor(BaseImageProcessor): scores_per_image, topk_indices = scores.flatten(0, 1).topk(num_queries, sorted=False) labels_per_image = labels[topk_indices] - topk_indices = torch_int_div(topk_indices, num_classes) + topk_indices = torch.div(topk_indices, num_classes, rounding_mode="floor") mask_pred = mask_pred[topk_indices] pred_masks = (mask_pred > 0).float() diff --git a/src/transformers/models/maskformer/image_processing_maskformer.py b/src/transformers/models/maskformer/image_processing_maskformer.py index ef4314869b..bfdc07431e 100644 --- a/src/transformers/models/maskformer/image_processing_maskformer.py +++ b/src/transformers/models/maskformer/image_processing_maskformer.py @@ -61,8 +61,6 @@ if is_torch_available(): import torch from torch import nn - from ...pytorch_utils import torch_int_div - # Copied from transformers.models.detr.image_processing_detr.max_across_indices def max_across_indices(values: Iterable[Any]) -> List[Any]: @@ -1077,7 +1075,7 @@ class MaskFormerImageProcessor(BaseImageProcessor): scores_per_image, topk_indices = scores.flatten(0, 1).topk(num_queries, sorted=False) labels_per_image = labels[topk_indices] - topk_indices = torch_int_div(topk_indices, num_classes) + topk_indices = torch.div(topk_indices, num_classes, rounding_mode="floor") mask_pred = mask_pred[topk_indices] pred_masks = (mask_pred > 0).float() diff --git a/src/transformers/models/mctct/modeling_mctct.py b/src/transformers/models/mctct/modeling_mctct.py index 3effb52de5..08e280b3cc 100755 --- a/src/transformers/models/mctct/modeling_mctct.py +++ b/src/transformers/models/mctct/modeling_mctct.py @@ -33,19 +33,12 @@ from ...modeling_utils import ( find_pruneable_heads_and_indices, prune_linear_layer, ) -from ...pytorch_utils import is_torch_less_than_1_9 from ...utils import logging from .configuration_mctct import MCTCTConfig logger = logging.get_logger(__name__) -if is_torch_less_than_1_9: - logger.warning( - f"You are using torch=={torch.__version__}, but torch>=1.9.0 is required to use MCTCTModel. Please upgrade" - " torch." - ) - _HIDDEN_STATES_START_POSITION = 1 _CONFIG_FOR_DOC = "MCTCTConfig" diff --git a/src/transformers/models/oneformer/image_processing_oneformer.py b/src/transformers/models/oneformer/image_processing_oneformer.py index 67eadd4e84..a99a7182b8 100644 --- a/src/transformers/models/oneformer/image_processing_oneformer.py +++ b/src/transformers/models/oneformer/image_processing_oneformer.py @@ -58,8 +58,6 @@ if is_torch_available(): import torch from torch import nn - from ...pytorch_utils import torch_int_div - # Copied from transformers.models.detr.image_processing_detr.max_across_indices def max_across_indices(values: Iterable[Any]) -> List[Any]: @@ -1122,7 +1120,7 @@ class OneFormerImageProcessor(BaseImageProcessor): scores_per_image, topk_indices = scores.flatten(0, 1).topk(num_queries, sorted=False) labels_per_image = labels[topk_indices] - topk_indices = torch_int_div(topk_indices, num_classes) + topk_indices = torch.div(topk_indices, num_classes, rounding_mode="floor") # mask_pred = mask_pred.unsqueeze(1).repeat(1, self.sem_seg_head.num_classes, 1).flatten(0, 1) mask_pred = masks_queries_logits[i][topk_indices] diff --git a/src/transformers/models/sew/modeling_sew.py b/src/transformers/models/sew/modeling_sew.py index 836c38f8f4..3e41496ea1 100644 --- a/src/transformers/models/sew/modeling_sew.py +++ b/src/transformers/models/sew/modeling_sew.py @@ -28,7 +28,6 @@ from ...activations import ACT2FN from ...deepspeed import is_deepspeed_zero3_enabled from ...modeling_outputs import BaseModelOutput, CausalLMOutput, SequenceClassifierOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_sew import SEWConfig @@ -770,7 +769,7 @@ class SEWPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/sew_d/modeling_sew_d.py b/src/transformers/models/sew_d/modeling_sew_d.py index 02a8477d14..7cdce062ee 100644 --- a/src/transformers/models/sew_d/modeling_sew_d.py +++ b/src/transformers/models/sew_d/modeling_sew_d.py @@ -29,7 +29,7 @@ from ...activations import ACT2FN from ...deepspeed import is_deepspeed_zero3_enabled from ...modeling_outputs import BaseModelOutput, CausalLMOutput, SequenceClassifierOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import softmax_backward_data, torch_int_div +from ...pytorch_utils import softmax_backward_data from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_sew_d import SEWDConfig @@ -1305,7 +1305,7 @@ class SEWDPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/speecht5/modeling_speecht5.py b/src/transformers/models/speecht5/modeling_speecht5.py index 975f483395..01ac79af47 100644 --- a/src/transformers/models/speecht5/modeling_speecht5.py +++ b/src/transformers/models/speecht5/modeling_speecht5.py @@ -34,7 +34,6 @@ from ...modeling_outputs import ( Seq2SeqSpectrogramOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings from .configuration_speecht5 import SpeechT5Config, SpeechT5HifiGanConfig @@ -620,7 +619,7 @@ class SpeechT5SpeechEncoderPrenet(nn.Module): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/table_transformer/modeling_table_transformer.py b/src/transformers/models/table_transformer/modeling_table_transformer.py index ff4bca7a5a..6716a78be3 100644 --- a/src/transformers/models/table_transformer/modeling_table_transformer.py +++ b/src/transformers/models/table_transformer/modeling_table_transformer.py @@ -26,7 +26,6 @@ from torch import Tensor, nn from ...activations import ACT2FN from ...modeling_outputs import BaseModelOutput, BaseModelOutputWithCrossAttentions, Seq2SeqModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_start_docstrings, @@ -380,7 +379,7 @@ class TableTransformerSinePositionEmbedding(nn.Module): x_embed = x_embed / (x_embed[:, :, -1:] + 1e-6) * self.scale dim_t = torch.arange(self.embedding_dim, dtype=torch.float32, device=pixel_values.device) - dim_t = self.temperature ** (2 * torch_int_div(dim_t, 2) / self.embedding_dim) + dim_t = self.temperature ** (2 * torch.div(dim_t, 2, rounding_mode="floor") / self.embedding_dim) pos_x = x_embed[:, :, :, None] / dim_t pos_y = y_embed[:, :, :, None] / dim_t diff --git a/src/transformers/models/tapas/modeling_tapas.py b/src/transformers/models/tapas/modeling_tapas.py index 2bb4e2baae..c215f0e0d3 100644 --- a/src/transformers/models/tapas/modeling_tapas.py +++ b/src/transformers/models/tapas/modeling_tapas.py @@ -33,7 +33,6 @@ from ...pytorch_utils import ( apply_chunking_to_forward, find_pruneable_heads_and_indices, prune_linear_layer, - torch_int_div, ) from ...utils import ( ModelOutput, @@ -1637,7 +1636,7 @@ class ProductIndexMap(IndexMap): def project_outer(self, index): """Projects an index with the same index set onto the outer components.""" - indices = torch_int_div(index.indices, self.inner_index.num_segments).type(torch.long) + indices = torch.div(index.indices, self.inner_index.num_segments, rounding_mode="floor").type(torch.long) return IndexMap(indices=indices, num_segments=self.outer_index.num_segments, batch_dims=index.batch_dims) def project_inner(self, index): diff --git a/src/transformers/models/unispeech/modeling_unispeech.py b/src/transformers/models/unispeech/modeling_unispeech.py index c55a90bb6a..71dcaad119 100755 --- a/src/transformers/models/unispeech/modeling_unispeech.py +++ b/src/transformers/models/unispeech/modeling_unispeech.py @@ -29,7 +29,6 @@ from ...activations import ACT2FN from ...deepspeed import is_deepspeed_zero3_enabled from ...modeling_outputs import BaseModelOutput, CausalLMOutput, SequenceClassifierOutput, Wav2Vec2BaseModelOutput from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_code_sample_docstrings, @@ -981,7 +980,7 @@ class UniSpeechPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py index baf0f93a09..ce3f5b80ca 100755 --- a/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py +++ b/src/transformers/models/unispeech_sat/modeling_unispeech_sat.py @@ -36,7 +36,6 @@ from ...modeling_outputs import ( XVectorOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_code_sample_docstrings, @@ -995,7 +994,7 @@ class UniSpeechSatPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/wav2vec2/modeling_wav2vec2.py b/src/transformers/models/wav2vec2/modeling_wav2vec2.py index 856ce56560..bcf7b7a3ad 100755 --- a/src/transformers/models/wav2vec2/modeling_wav2vec2.py +++ b/src/transformers/models/wav2vec2/modeling_wav2vec2.py @@ -37,7 +37,6 @@ from ...modeling_outputs import ( XVectorOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_code_sample_docstrings, @@ -1098,7 +1097,7 @@ class Wav2Vec2PreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py b/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py index 0dfac20c06..94ce66e4f3 100644 --- a/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py +++ b/src/transformers/models/wav2vec2_conformer/modeling_wav2vec2_conformer.py @@ -35,7 +35,6 @@ from ...modeling_outputs import ( XVectorOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import ( ModelOutput, add_code_sample_docstrings, @@ -1143,7 +1142,7 @@ class Wav2Vec2ConformerPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/models/wavlm/modeling_wavlm.py b/src/transformers/models/wavlm/modeling_wavlm.py index 6347aafab7..718c4d61dd 100755 --- a/src/transformers/models/wavlm/modeling_wavlm.py +++ b/src/transformers/models/wavlm/modeling_wavlm.py @@ -36,7 +36,6 @@ from ...modeling_outputs import ( XVectorOutput, ) from ...modeling_utils import PreTrainedModel -from ...pytorch_utils import torch_int_div from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging from .configuration_wavlm import WavLMConfig @@ -1019,7 +1018,7 @@ class WavLMPreTrainedModel(PreTrainedModel): def _conv_out_length(input_length, kernel_size, stride): # 1D convolutional layer output length formula taken # from https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html - return torch_int_div(input_length - kernel_size, stride) + 1 + return torch.div(input_length - kernel_size, stride, rounding_mode="floor") + 1 for kernel_size, stride in zip(self.config.conv_kernel, self.config.conv_stride): input_lengths = _conv_out_length(input_lengths, kernel_size, stride) diff --git a/src/transformers/onnx/convert.py b/src/transformers/onnx/convert.py index 918134d311..288c2574e1 100644 --- a/src/transformers/onnx/convert.py +++ b/src/transformers/onnx/convert.py @@ -26,7 +26,6 @@ from ..utils import ( TensorType, is_tf_available, is_torch_available, - is_torch_onnx_dict_inputs_support_available, logging, ) from .config import OnnxConfig @@ -339,9 +338,6 @@ def export( if is_torch_available(): from ..utils import torch_version - if not is_torch_onnx_dict_inputs_support_available(): - raise AssertionError(f"Unsupported PyTorch version, minimum required is 1.8.0, got: {torch_version}") - if not config.is_torch_support_available: logger.warning( f"Unsupported PyTorch version for this model. Minimum required is {config.torch_onnx_minimum_version}," diff --git a/src/transformers/pytorch_utils.py b/src/transformers/pytorch_utils.py index 15549f2233..4e477afd9e 100644 --- a/src/transformers/pytorch_utils.py +++ b/src/transformers/pytorch_utils.py @@ -27,22 +27,10 @@ logger = logging.get_logger(__name__) parsed_torch_version_base = version.parse(version.parse(torch.__version__).base_version) -is_torch_less_than_1_8 = parsed_torch_version_base < version.parse("1.8.0") -is_torch_less_than_1_9 = parsed_torch_version_base < version.parse("1.9.0") is_torch_greater_or_equal_than_1_10 = parsed_torch_version_base >= version.parse("1.10") is_torch_less_than_1_11 = parsed_torch_version_base < version.parse("1.11") -def torch_int_div(tensor1, tensor2): - """ - A function that performs integer division across different versions of PyTorch. - """ - if is_torch_less_than_1_8: - return tensor1 // tensor2 - else: - return torch.div(tensor1, tensor2, rounding_mode="floor") - - def softmax_backward_data(parent, grad_output, output, dim, self): """ A function that calls the internal `_softmax_backward_data` PyTorch method and that adjusts the arguments according diff --git a/src/transformers/utils/__init__.py b/src/transformers/utils/__init__.py index 26371782f8..b9b1771e68 100644 --- a/src/transformers/utils/__init__.py +++ b/src/transformers/utils/__init__.py @@ -151,7 +151,6 @@ from .import_utils import ( is_torch_fx_available, is_torch_fx_proxy, is_torch_neuroncore_available, - is_torch_onnx_dict_inputs_support_available, is_torch_tensorrt_fx_available, is_torch_tf32_available, is_torch_tpu_available, diff --git a/src/transformers/utils/import_utils.py b/src/transformers/utils/import_utils.py index cfa7f44d98..ceb876040f 100644 --- a/src/transformers/utils/import_utils.py +++ b/src/transformers/utils/import_utils.py @@ -276,7 +276,6 @@ except importlib_metadata.PackageNotFoundError: # This is the version of torch required to run torch.fx features and torch.onnx with dictionary inputs. TORCH_FX_REQUIRED_VERSION = version.parse("1.10") -TORCH_ONNX_DICT_INPUTS_MINIMUM_VERSION = version.parse("1.8") def is_kenlm_available(): @@ -388,7 +387,7 @@ def is_torch_tf32_available(): torch_version = None -_torch_fx_available = _torch_onnx_dict_inputs_support_available = False +_torch_fx_available = False if _torch_available: torch_version = version.parse(importlib_metadata.version("torch")) _torch_fx_available = (torch_version.major, torch_version.minor) >= ( @@ -396,8 +395,6 @@ if _torch_available: TORCH_FX_REQUIRED_VERSION.minor, ) - _torch_onnx_dict_inputs_support_available = torch_version >= TORCH_ONNX_DICT_INPUTS_MINIMUM_VERSION - def is_torch_fx_available(): return _torch_fx_available @@ -407,10 +404,6 @@ def is_bs4_available(): return importlib.util.find_spec("bs4") is not None -def is_torch_onnx_dict_inputs_support_available(): - return _torch_onnx_dict_inputs_support_available - - def is_tf_available(): return _tf_available diff --git a/tests/generation/test_beam_search.py b/tests/generation/test_beam_search.py index e35e8d9b81..47d3b4b38a 100644 --- a/tests/generation/test_beam_search.py +++ b/tests/generation/test_beam_search.py @@ -32,7 +32,6 @@ if is_torch_available(): DisjunctiveConstraint, PhrasalConstraint, ) - from transformers.pytorch_utils import torch_int_div class BeamSearchTester: @@ -161,7 +160,9 @@ class BeamSearchTester: expected_output_scores = cut_expected_tensor(next_scores) # add num_beams * batch_idx - offset = torch_int_div(torch.arange(self.num_beams * self.batch_size, device=torch_device), self.num_beams) + offset = torch.div( + torch.arange(self.num_beams * self.batch_size, device=torch_device), self.num_beams, rounding_mode="floor" + ) expected_output_indices = cut_expected_tensor(next_indices) + offset * self.num_beams self.parent.assertListEqual(expected_output_tokens.tolist(), output_tokens.tolist()) @@ -398,7 +399,9 @@ class ConstrainedBeamSearchTester: expected_output_scores = cut_expected_tensor(next_scores) # add num_beams * batch_idx - offset = torch_int_div(torch.arange(self.num_beams * self.batch_size, device=torch_device), self.num_beams) + offset = torch.div( + torch.arange(self.num_beams * self.batch_size, device=torch_device), self.num_beams, rounding_mode="floor" + ) expected_output_indices = cut_expected_tensor(next_indices) + offset * self.num_beams self.parent.assertListEqual(expected_output_tokens.tolist(), output_tokens.tolist()) diff --git a/tests/models/bloom/test_modeling_bloom.py b/tests/models/bloom/test_modeling_bloom.py index 9dc803a3ba..617998cc61 100644 --- a/tests/models/bloom/test_modeling_bloom.py +++ b/tests/models/bloom/test_modeling_bloom.py @@ -38,10 +38,9 @@ if is_torch_available(): BloomModel, BloomTokenizerFast, ) - from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_10, is_torch_less_than_1_9 + from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_10 else: is_torch_greater_or_equal_than_1_10 = False - is_torch_less_than_1_9 = True @require_torch @@ -751,9 +750,6 @@ class BloomEmbeddingTest(unittest.TestCase): self.assertAlmostEqual(EMBEDDINGS_DS_AFTER_LN[key][idx], output_dict_norm[key][idx], places=1) @require_torch - @unittest.skipIf( - is_torch_less_than_1_9, reason="Test failed with torch < 1.9 (`min_cuda` not implemented for `BFloat16`)" - ) def test_hidden_states_transformers(self): cuda_available = torch.cuda.is_available() model = BloomModel.from_pretrained(self.path_bigscience_model, use_cache=False, torch_dtype="auto").to( diff --git a/tests/models/mctct/test_modeling_mctct.py b/tests/models/mctct/test_modeling_mctct.py index c488c3e75d..7fa9d99a3f 100644 --- a/tests/models/mctct/test_modeling_mctct.py +++ b/tests/models/mctct/test_modeling_mctct.py @@ -32,9 +32,6 @@ if is_torch_available(): import torch from transformers import MCTCTForCTC, MCTCTModel, MCTCTProcessor - from transformers.pytorch_utils import is_torch_less_than_1_9 -else: - is_torch_less_than_1_9 = True class MCTCTModelTester: @@ -265,7 +262,6 @@ class MCTCTModelTester: @require_torch -@unittest.skipIf(is_torch_less_than_1_9, "MCTCT is only available in torch v1.9+") class MCTCTModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase): all_model_classes = (MCTCTForCTC, MCTCTModel) if is_torch_available() else () pipeline_model_mapping = ( diff --git a/tests/models/swin/test_modeling_swin.py b/tests/models/swin/test_modeling_swin.py index 804bffd31f..f519a0204f 100644 --- a/tests/models/swin/test_modeling_swin.py +++ b/tests/models/swin/test_modeling_swin.py @@ -33,9 +33,7 @@ if is_torch_available(): from transformers import SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST - from transformers.pytorch_utils import is_torch_less_than_1_9 -else: - is_torch_less_than_1_9 = True + if is_vision_available(): from PIL import Image @@ -266,7 +264,6 @@ class SwinModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase): def test_multi_gpu_data_parallel_forward(self): pass - @unittest.skipIf(is_torch_less_than_1_9, reason="This test fails for SwinModel when torch < 1.9") def test_training_gradient_checkpointing(self): super().test_training_gradient_checkpointing() diff --git a/tests/models/trajectory_transformer/test_modeling_trajectory_transformer.py b/tests/models/trajectory_transformer/test_modeling_trajectory_transformer.py index 0b9be4918c..cf553bdf75 100644 --- a/tests/models/trajectory_transformer/test_modeling_trajectory_transformer.py +++ b/tests/models/trajectory_transformer/test_modeling_trajectory_transformer.py @@ -36,9 +36,6 @@ if is_torch_available(): from transformers.models.trajectory_transformer.modeling_trajectory_transformer import ( TRAJECTORY_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST, ) - from transformers.pytorch_utils import is_torch_less_than_1_9 -else: - is_torch_less_than_1_9 = True class TrajectoryTransformerModelTester: @@ -199,7 +196,6 @@ class TrajectoryTransformerModelTest(ModelTesterMixin, GenerationTesterMixin, Pi ).loss loss.backward() - @unittest.skipIf(is_torch_less_than_1_9, reason="This test fails for TrajectoryTransformerModel when torch < 1.9") def test_training_gradient_checkpointing(self): if not self.model_tester.is_training: return diff --git a/tests/models/wav2vec2/test_modeling_wav2vec2.py b/tests/models/wav2vec2/test_modeling_wav2vec2.py index cd98d41034..f3e93b670c 100644 --- a/tests/models/wav2vec2/test_modeling_wav2vec2.py +++ b/tests/models/wav2vec2/test_modeling_wav2vec2.py @@ -71,9 +71,6 @@ if is_torch_available(): _compute_mask_indices, _sample_negative_indices, ) - from transformers.pytorch_utils import is_torch_less_than_1_9, torch_int_div -else: - is_torch_less_than_1_9 = True if is_torchaudio_available(): @@ -1217,7 +1214,9 @@ class Wav2Vec2UtilsTest(unittest.TestCase): sequence_length = 10 hidden_size = 4 num_negatives = 3 - sequence = torch_int_div(torch.arange(sequence_length * hidden_size, device=torch_device), hidden_size) + sequence = torch.div( + torch.arange(sequence_length * hidden_size, device=torch_device), hidden_size, rounding_mode="floor" + ) features = sequence.view(sequence_length, hidden_size) # each value in vector consits of same value features = features[None, :].expand(batch_size, sequence_length, hidden_size).contiguous() @@ -1245,7 +1244,9 @@ class Wav2Vec2UtilsTest(unittest.TestCase): mask = torch.ones((batch_size, sequence_length), dtype=torch.long, device=torch_device) mask[-1, sequence_length // 2 :] = 0 - sequence = torch_int_div(torch.arange(sequence_length * hidden_size, device=torch_device), hidden_size) + sequence = torch.div( + torch.arange(sequence_length * hidden_size, device=torch_device), hidden_size, rounding_mode="floor" + ) features = sequence.view(sequence_length, hidden_size) # each value in vector consits of same value features = features[None, :].expand(batch_size, sequence_length, hidden_size).contiguous() @@ -1651,10 +1652,6 @@ class Wav2Vec2ModelIntegrationTest(unittest.TestCase): @require_pyctcdecode @require_torchaudio - @unittest.skipIf( - is_torch_less_than_1_9, - reason="`torchaudio.functional.resample` needs torchaudio >= 0.9 which requires torch >= 0.9", - ) def test_wav2vec2_with_lm(self): ds = load_dataset("common_voice", "es", split="test", streaming=True) sample = next(iter(ds)) @@ -1679,10 +1676,6 @@ class Wav2Vec2ModelIntegrationTest(unittest.TestCase): @require_pyctcdecode @require_torchaudio - @unittest.skipIf( - is_torch_less_than_1_9, - reason="`torchaudio.functional.resample` needs torchaudio >= 0.9 which requires torch >= 0.9", - ) def test_wav2vec2_with_lm_pool(self): ds = load_dataset("common_voice", "es", split="test", streaming=True) sample = next(iter(ds)) diff --git a/tests/onnx/test_onnx_v2.py b/tests/onnx/test_onnx_v2.py index 51d9e2cb18..81e28a3796 100644 --- a/tests/onnx/test_onnx_v2.py +++ b/tests/onnx/test_onnx_v2.py @@ -14,7 +14,6 @@ from transformers.onnx import ( OnnxConfig, OnnxConfigWithPast, ParameterFormat, - export, validate_model_outputs, ) from transformers.onnx.utils import ( @@ -40,15 +39,6 @@ class OnnxUtilsTestCaseV2(TestCase): Cover all the utilities involved to export ONNX models """ - @require_torch - @patch("transformers.onnx.convert.is_torch_onnx_dict_inputs_support_available", return_value=False) - def test_ensure_pytorch_version_ge_1_8_0(self, mock_is_torch_onnx_dict_inputs_support_available): - """ - Ensure we raise an Exception if the pytorch version is unsupported (< 1.8.0) - """ - self.assertRaises(AssertionError, export, None, None, None, None, None) - mock_is_torch_onnx_dict_inputs_support_available.assert_called() - def test_compute_effective_axis_dimension(self): """ When exporting ONNX model with dynamic axis (batch or sequence) we set batch_size and/or sequence_length = -1.