Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ line-length = 119
ignore = ["C901", "E501", "E741", "F402", "F823", "SIM1", "SIM300", "SIM212", "SIM905", "UP009", "UP015", "UP031", "UP028", "UP004", "UP045", "UP007"]
# 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", "PERF102", "PLC1802", "PLC0208", "SIM", "UP", "PIE794", "FURB"]
select = ["C", "E", "F", "I", "W", "RUF013", "PERF102", "PLC1802", "PLC0208", "SIM", "UP", "PIE", "FURB"]
extend-safe-fixes = ["UP006"]

# Ignore import violations in all `__init__.py` files.
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def load_audio_librosa(audio: Union[str, np.ndarray], sampling_rate=16000, timeo
requires_backends(load_audio_librosa, ["librosa"])

# Load audio from URL (e.g https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen2-Audio/audio/translate_to_chinese.wav)
if audio.startswith("http://") or audio.startswith("https://"):
if audio.startswith(("http://", "https://")):
audio = librosa.load(
BytesIO(httpx.get(audio, follow_redirects=True, timeout=timeout).content), sr=sampling_rate
)[0]
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def load_image(image: Union[str, "PIL.Image.Image"], timeout: Optional[float] =
"""
requires_backends(load_image, ["vision"])
if isinstance(image, str):
if image.startswith("http://") or image.startswith("https://"):
if image.startswith(("http://", "https://")):
# We need to actually check for a real protocol, otherwise it's impossible to use a local file
# like http_huggingface_co.png
image = PIL.Image.open(BytesIO(httpx.get(image, timeout=timeout, follow_redirects=True).content))
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/integrations/finegrained_fp8.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _w8a8_block_fp8_matmul(
Bs_ptrs = Bs + offs_bsn * stride_Bs_n

accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
for k in range(0, tl.cdiv(K, BLOCK_SIZE_K)):
for k in range(tl.cdiv(K, BLOCK_SIZE_K)):
a = tl.load(a_ptrs, mask=offs_k[None, :] < K - k * BLOCK_SIZE_K, other=0.0)
b = tl.load(b_ptrs, mask=offs_k[:, None] < K - k * BLOCK_SIZE_K, other=0.0)

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/modeling_gguf_pytorch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _split_moe_expert_tensor(
# https://github.com/ggerganov/llama.cpp/blob/master/convert_hf_to_gguf.py#L1994-L2022
name = tensor_key_mapping[name]
w_counter = self.config.get("num_experts", 60)
for i in range(0, w_counter):
for i in range(w_counter):
temp_name = name.replace("mlp.experts.", f"mlp.experts.{i}.")
exp_weight = weights[i]
parsed_parameters["tensors"][temp_name] = torch.from_numpy(np.copy(exp_weight))
Expand Down Expand Up @@ -338,7 +338,7 @@ def get_gguf_hf_weights_map(
hf_name = re.sub(r"mlp.experts.\d+.", "mlp.experts.", hf_name)

name, suffix = hf_name, ""
if hf_name.endswith(".weight") or hf_name.endswith(".bias"):
if hf_name.endswith((".weight", ".bias")):
name, suffix = hf_name.rsplit(".", 1)
suffix = "." + suffix

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def get_encoder_attention_layer_array(layer_index: int, name: str, original_shap
model = BertForMaskedLM(config)

# Layers
for layer_index in range(0, config.num_hidden_layers):
for layer_index in range(config.num_hidden_layers):
layer: BertLayer = model.bert.encoder.layer[layer_index]

# Self-attention
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/bertweet/tokenization_bertweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def normalizeToken(self, token):
lowercased_token = token.lower()
if token.startswith("@"):
return "@USER"
elif lowercased_token.startswith("http") or lowercased_token.startswith("www"):
elif lowercased_token.startswith(("http", "www")):
return "HTTPURL"
elif len(token) == 1:
if token in self.special_puncts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ def load_tf_weights_trivia_qa(init_vars):
try:
if len(array.shape) > len(pointer.shape) and math.prod(array.shape) == math.prod(pointer.shape):
# print(txt_name, array.shape)
if (
txt_name.endswith("attention/self/key/kernel")
or txt_name.endswith("attention/self/query/kernel")
or txt_name.endswith("attention/self/value/kernel")
if txt_name.endswith(
("attention/self/key/kernel", "attention/self/query/kernel", "attention/self/value/kernel")
):
array = array.transpose(1, 0, 2).reshape(pointer.shape)
elif txt_name.endswith("attention/output/dense/kernel"):
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/clap/feature_extraction_clap.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def _np_extract_fbank_features(self, waveform: np.ndarray, mel_filters: Optional
return log_mel_spectrogram.T

def _random_mel_fusion(self, mel, total_frames, chunk_frames):
ranges = np.array_split(list(range(0, total_frames - chunk_frames + 1)), 3)
ranges = np.array_split(list(range(total_frames - chunk_frames + 1)), 3)
if len(ranges[1]) == 0:
# if the audio is too short, we just use the first chunk
ranges[1] = [0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def convert_conditional_detr_checkpoint(model_name, pytorch_dump_folder_path):
elif "class_labels_classifier" in key or "bbox_predictor" in key:
val = state_dict.pop(key)
state_dict["conditional_detr." + key] = val
elif key.startswith("bbox_attention") or key.startswith("mask_head"):
elif key.startswith(("bbox_attention", "mask_head")):
continue
else:
val = state_dict.pop(key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def convert_tf_gptsan_to_pt(args):
shapes = reader.get_variable_to_shape_map()
for key_name in shapes:
vnp = reader.get_tensor(key_name).astype(np.float16)
if key_name.endswith("/adam_m") or key_name.endswith("/adam_v"):
if key_name.endswith(("/adam_m", "/adam_v")):
continue
if key_name.startswith("pasts/"):
if key_name.startswith("pasts/mlp"):
Expand All @@ -60,7 +60,7 @@ def convert_tf_gptsan_to_pt(args):
name = "model.blocks.%d.feed_forward.soft_bypass_mlp.weight" % player
state = vnp.transpose([1, 0]).copy() # Mesh-Tensorflow is a diagonal matrix
new_state[name] = torch.tensor(state)
elif key_name.endswith("/wo/kernel") or key_name.endswith("/wi/kernel"):
elif key_name.endswith(("/wo/kernel", "/wi/kernel")):
nlayer = key_name[-9:-7]
for i in range(16):
name = "model.blocks.%d.feed_forward.mlp.experts.expert_%d.%s.weight" % (player, i, nlayer)
Expand Down Expand Up @@ -140,11 +140,7 @@ def convert_tf_gptsan_to_pt(args):
name = "model.blocks.%d.self_attn.norm.weight" % player
state = vnp.copy() # same because it is one dimensional
new_state[name] = torch.tensor(state)
elif (
key_name.startswith("model/wte")
or key_name.startswith("model/wpe")
or key_name.startswith("model/ete")
):
elif key_name.startswith(("model/wte", "model/wpe", "model/ete")):
nlayer = {"wte": "embed_tokens", "wpe": "position_embeddings", "ete": "extra_position_embeddings"}[
key_name[-3:]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_relevant_lyric_tokens(full_tokens, max_n_lyric_tokens, total_length, off
tokens = torch.cat(
[torch.zeros(max_n_lyric_tokens - len(full_tokens), dtype=torch.long).to(full_tokens.device), full_tokens]
)
indices = [-1] * (max_n_lyric_tokens - len(full_tokens)) + list(range(0, len(full_tokens)))
indices = [-1] * (max_n_lyric_tokens - len(full_tokens)) + list(range(len(full_tokens)))
else:
midpoint = int(len(full_tokens) * (offset + duration / 2.0) / total_length)
midpoint = min(max(midpoint, max_n_lyric_tokens // 2), len(full_tokens) - max_n_lyric_tokens // 2)
Expand Down Expand Up @@ -1464,7 +1464,7 @@ def sample(
if get_preds:
preds = []

iter = tqdm(range(0, sample_tokens), leave=False)
iter = tqdm(range(sample_tokens), leave=False)
for sample_t in iter:
iter.set_description(f"Ancestral sampling {sample_tokens} music tokens", refresh=True)
hidden_states, cond = self.get_emb(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def convert_detr_checkpoint(model_name, pytorch_dump_folder_path):
elif "class_labels_classifier" in key or "bbox_predictor" in key:
val = state_dict.pop(key)
state_dict["detr." + key] = val
elif key.startswith("bbox_attention") or key.startswith("mask_head"):
elif key.startswith(("bbox_attention", "mask_head")):
continue
else:
val = state_dict.pop(key)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/detr/convert_detr_to_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def convert_detr_checkpoint(model_name, pytorch_dump_folder_path=None, push_to_h
elif "class_labels_classifier" in key or "bbox_predictor" in key:
val = state_dict.pop(key)
state_dict["detr." + key] = val
elif key.startswith("bbox_attention") or key.startswith("mask_head"):
elif key.startswith(("bbox_attention", "mask_head")):
continue
else:
val = state_dict.pop(key)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/funnel/modeling_funnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def get_position_embeds(
pos = torch.arange(0, seq_len, dtype=torch.int64, device=device).to(dtype)
pooled_pos = pos
position_embeds_list = []
for block_index in range(0, self.config.num_blocks):
for block_index in range(self.config.num_blocks):
# For each block with block_index > 0, we need two types position embeddings:
# - Attention(pooled-q, unpooled-kv)
# - Attention(pooled-q, pooled-kv)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DummyClass:
def __init__(self, *args, **kwargs):
pass

if mod_name.startswith("megatron") or mod_name.startswith("glm") or mod_name.startswith("__main__"):
if mod_name.startswith(("megatron", "glm", "__main__")):
return DummyClass
return super().find_class(mod_name, name)

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/glm4v/modular_glm4v.py
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ def __call__(
timestamps = metadata.timestamps[::2] # mrope

unique_timestamps = []
for idx in range(0, len(timestamps)):
for idx in range(len(timestamps)):
unique_timestamps.append(timestamps[idx])

selected_timestamps = unique_timestamps[:num_frames]
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/glm4v/processing_glm4v.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __call__(
timestamps = metadata.timestamps[::2] # mrope

unique_timestamps = []
for idx in range(0, len(timestamps)):
for idx in range(len(timestamps)):
unique_timestamps.append(timestamps[idx])

selected_timestamps = unique_timestamps[:num_frames]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DummyClass:
def __init__(self, *args, **kwargs):
pass

if mod_name.startswith("megatron") or mod_name.startswith("glm") or mod_name.startswith("__main__"):
if mod_name.startswith(("megatron", "glm", "__main__")):
return DummyClass
return super().find_class(mod_name, name)

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/gpt_neo/configuration_gpt_neo.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def custom_unfold(input, dimension, size, step):
s[dimension] = indices
sliced = input[s]

perm = list(range(0, rank + 1))
perm = list(range(rank + 1))
perm.append(perm.pop(dimension + 1))

return sliced.permute(perm)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/gpt_sw3/tokenization_gpt_sw3.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def __init__(

# Regular expression to remove non-printing characters (e.g. some unicode control chars) in preprocessing
self.non_printing_characters_re = re.compile(
f"[{''.join(map(chr, list(range(0, 9)) + list(range(11, 32)) + list(range(127, 160)) + [160, 173, 8203]))}]"
f"[{''.join(map(chr, list(range(9)) + list(range(11, 32)) + list(range(127, 160)) + [160, 173, 8203]))}]"
)

super().__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,6 @@ def get_image_features(
pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
The tensors corresponding to the input images.
"""
pass

def get_placeholder_mask(self, input_ids: torch.LongTensor, inputs_embeds: torch.FloatTensor):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,7 @@ def convert_old_keys_to_new_keys(state_dict_keys: Optional[dict] = None, path: O
new_text = re.sub(pattern, replacement, new_text)
output_dict.update(dict(zip(old_text_language.split("\n"), new_text.split("\n"))))
old_text_multi = "\n".join(
[
key
for key in state_dict_keys
if not (key.startswith("language_model") or key.startswith("vision_model"))
]
[key for key in state_dict_keys if not (key.startswith(("language_model", "vision_model")))]
)
new_text = old_text_multi
for pattern, replacement in ORIGINAL_TO_CONVERTED_KEY_MAPPING_MULTI.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def convert_luke_checkpoint(checkpoint_path, metadata_path, entity_vocab_path, p
missing_keys, unexpected_keys = model.load_state_dict(state_dict, strict=False)
if not (len(missing_keys) == 1 and missing_keys[0] == "embeddings.position_ids"):
raise ValueError(f"Missing keys {', '.join(missing_keys)}. Expected only missing embeddings.position_ids")
if not (all(key.startswith("entity_predictions") or key.startswith("lm_head") for key in unexpected_keys)):
if not (all(key.startswith(("entity_predictions", "lm_head")) for key in unexpected_keys)):
raise ValueError(
"Unexpected keys"
f" {', '.join([key for key in unexpected_keys if not (key.startswith('entity_predictions') or key.startswith('lm_head'))])}"
f" {', '.join([key for key in unexpected_keys if not (key.startswith(('entity_predictions', 'lm_head')))])}"
)

# Check outputs
Expand Down
13 changes: 8 additions & 5 deletions src/transformers/models/marian/convert_marian_to_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,11 +574,14 @@ def _check_layer_entries(self):
def extra_keys(self):
extra = []
for k in self.state_keys:
if (
k.startswith("encoder_l")
or k.startswith("decoder_l")
or k in [CONFIG_KEY, "Wemb", "encoder_Wemb", "decoder_Wemb", "Wpos", "decoder_ff_logit_out_b"]
):
if k.startswith(("encoder_l", "decoder_l")) or k in [
CONFIG_KEY,
"Wemb",
"encoder_Wemb",
"decoder_Wemb",
"Wpos",
"decoder_ff_logit_out_b",
]:
continue
else:
extra.append(k)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def rename_keys_for_conv(detectron_conv: str, mine_conv: str):
renamed_keys.extend(rename_keys_for_conv(f"{src_prefix}.layer_4", f"{dst_prefix}.fpn.stem"))

# add all the fpn layers (here we need some config parameters to know the size in advance)
for src_i, dst_i in zip(range(3, 0, -1), range(0, 3)):
for src_i, dst_i in zip(range(3, 0, -1), range(3)):
renamed_keys.extend(
rename_keys_for_conv(f"{src_prefix}.adapter_{src_i}", f"{dst_prefix}.fpn.layers.{dst_i}.proj")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def create_rename_keys(config):
rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight"))
rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight"))
rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias"))
for source_index, target_index in zip(range(3, 0, -1), range(0, 3)):
for source_index, target_index in zip(range(3, 0, -1), range(3)):
rename_keys.append((f"sem_seg_head.adapter_{source_index}.weight", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight"))
rename_keys.append((f"sem_seg_head.adapter_{source_index}.norm.weight", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight"))
rename_keys.append((f"sem_seg_head.adapter_{source_index}.norm.bias", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def create_rename_keys(config):
rename_keys.append(("sem_seg_head.layer_4.weight", "model.pixel_level_module.decoder.fpn.stem.0.weight"))
rename_keys.append(("sem_seg_head.layer_4.norm.weight", "model.pixel_level_module.decoder.fpn.stem.1.weight"))
rename_keys.append(("sem_seg_head.layer_4.norm.bias", "model.pixel_level_module.decoder.fpn.stem.1.bias"))
for source_index, target_index in zip(range(3, 0, -1), range(0, 3)):
for source_index, target_index in zip(range(3, 0, -1), range(3)):
rename_keys.append((f"sem_seg_head.adapter_{source_index}.weight", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.0.weight"))
rename_keys.append((f"sem_seg_head.adapter_{source_index}.norm.weight", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.weight"))
rename_keys.append((f"sem_seg_head.adapter_{source_index}.norm.bias", f"model.pixel_level_module.decoder.fpn.layers.{target_index}.proj.1.bias"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def convert_luke_checkpoint(checkpoint_path, metadata_path, entity_vocab_path, p
state_dict.pop("lm_head.decoder.bias")
state_dict_for_hugging_face = OrderedDict()
for key in state_dict:
if not (key.startswith("lm_head") or key.startswith("entity_predictions")):
if not (key.startswith(("lm_head", "entity_predictions"))):
state_dict_for_hugging_face[f"luke.{key}"] = state_dict[key]
else:
state_dict_for_hugging_face[key] = state_dict[key]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,7 @@ def preprocess_old_state(state_dict: dict, config: MMGroundingDinoConfig) -> dic
k == "dn_query_generator.label_embedding.weight"
or k == "language_model.language_backbone.body.model.embeddings.position_ids"
or k == "image_seperate.weight"
or k.startswith("lmm")
or k.startswith("connector")
or k.startswith("region_connector")
or k.startswith("ref_point_head")
or k.startswith(("lmm", "connector", "region_connector", "ref_point_head"))
):
new_state_dict.pop(k)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def rename_key(name, base_model=False):
if ".conv_proj." in name:
name = name.replace(".conv_proj.", ".conv_projection.")

for i in range(0, 2):
for j in range(0, 4):
for i in range(2):
for j in range(4):
if f".{i}.{j}." in name:
name = name.replace(f".{i}.{j}.", f".{i}.layer.{j}.")

for i in range(2, 6):
for j in range(0, 4):
for j in range(4):
if f".{i}.{j}." in name:
name = name.replace(f".{i}.{j}.", f".{i}.")
if "expand_1x1" in name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def convert_mobilevitv2_checkpoint(task_name, checkpoint_path, orig_config_path,
checkpoint = torch.load(checkpoint_path, map_location="cpu", weights_only=True)

# load huggingface model
if task_name.startswith("ade20k_") or task_name.startswith("voc_"):
if task_name.startswith(("ade20k_", "voc_")):
model = MobileViTV2ForSemanticSegmentation(config).eval()
base_model = False
else:
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/nougat/tokenization_nougat_fast.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def truncate_repetitions(text: str, min_len: int = 30) -> str:
for repetition_length in range(min_len, int(text_length / 2)):
# check if there is a repetition at the end
same = True
for i in range(0, repetition_length):
for i in range(repetition_length):
if text_lower[text_length - repetition_length - i - 1] != text_lower[text_length - i - 1]:
same = False
break
Expand Down
Loading