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 examples/pytorch/transformers_serve_cb_eval_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def main():
print("=" * 70)

# Start server with output going directly to stdout/stderr
server_process = subprocess.Popen(serve_cmd, stdout=None, stderr=None)
server_process = subprocess.Popen(serve_cmd, stdout=None, stderr=None) # noqa: S603

wait_for_server_up(server_process, port=args.port, timeout=600)

Expand Down
17 changes: 16 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,19 @@ select = [
"UP", # pyupgrade
"FURB", # refurb
"SIM", # flake8-simplify
"S110", # bandit's try-except-pass rule
"S102", # Use of `exec` detected
"S103", # `os.chmod` setting a permissive mask on file or directory
"S104", # Possible binding to all interfaces
"S108", # Probable insecure usage of temporary file or directory
"S306", # Use of `tempfile.mktemp` (use `tempfile.mkstemp` instead)
"S307", # Use of `eval` detected
"S603", # `subprocess` call: check for execution of untrusted input
"S604", # Function call with `shell=True` parameter identified
"S605", # Starting a process with a shell, possible injection
"S606", # Starting a process without a shell
"S607", # Starting a process with a partial executable path
"S110", # `try`-`except`-`pass` detected, consider logging the exception
"S112", # `try`-`except`-`continue` detected, consider logging the exception
"C4", # flake8-comprehensions
"RUF013", # Checks for the use of implicit Optional in type annotations when the default parameter value is None.
"PERF102", # Checks for uses of dict.items() that discard either the key or the value when iterating over the dictionary.
Expand All @@ -51,6 +63,9 @@ extend-safe-fixes = [
"__init__.py" = ["E402", "F401", "F403", "F811"]
"src/transformers/file_utils.py" = ["F401"]
"src/transformers/utils/dummy_*.py" = ["F401"]
"tests/**/*.py" = ["S104", "S603", "S607"]
"benchmark/**/*.py" = ["S603", "S607"]
"benchmark_v2/**/*.py" = ["S603", "S607"]

[tool.ruff.lint.isort]
lines-after-imports = 2
Expand Down
22 changes: 13 additions & 9 deletions src/transformers/cli/add_new_model_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,24 +585,28 @@ def _add_new_model_like_internal(

# 9. Run linters
model_init_file = repo_path / "src" / "transformers" / "models" / "__init__.py"
subprocess.run(
["ruff", "check", new_module_folder, tests_folder, model_init_file, "--fix"],
subprocess.run( # noqa: S603
["ruff", "check", new_module_folder, tests_folder, model_init_file, "--fix"], # noqa: S607
cwd=repo_path,
stdout=subprocess.DEVNULL,
)
subprocess.run(
["ruff", "format", new_module_folder, tests_folder, model_init_file],
subprocess.run( # noqa: S603
["ruff", "format", new_module_folder, tests_folder, model_init_file], # noqa: S607
cwd=repo_path,
stdout=subprocess.DEVNULL,
)
subprocess.run(
["python", "utils/check_doc_toc.py", "--fix_and_overwrite"], cwd=repo_path, stdout=subprocess.DEVNULL
subprocess.run( # noqa: S603
["python", "utils/check_doc_toc.py", "--fix_and_overwrite"], # noqa: S607
cwd=repo_path,
stdout=subprocess.DEVNULL,
)
subprocess.run(["python", "utils/sort_auto_mappings.py"], cwd=repo_path, stdout=subprocess.DEVNULL)
subprocess.run(["python", "utils/sort_auto_mappings.py"], cwd=repo_path, stdout=subprocess.DEVNULL) # noqa: S603, S607

# 10. Run the modular conversion
subprocess.run(
["python", "utils/modular_model_converter.py", new_lowercase_name], cwd=repo_path, stdout=subprocess.DEVNULL
subprocess.run( # noqa: S603
["python", "utils/modular_model_converter.py", new_lowercase_name], # noqa: S607
cwd=repo_path,
stdout=subprocess.DEVNULL,
)


Expand Down
3 changes: 3 additions & 0 deletions src/transformers/models/auto/processing_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ def from_pretrained(cls, pretrained_model_name_or_path, **kwargs):
pretrained_model_name_or_path, trust_remote_code=trust_remote_code, **kwargs
)
except Exception:
logger.debug(
"Failed to load %s with %s, trying next class.", pretrained_model_name_or_path, klass.__name__
)
continue

raise ValueError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import argparse
import tempfile

from transformers import LlamaTokenizer, LlamaTokenizerFast

Expand Down Expand Up @@ -67,6 +68,7 @@
hf_tok.add_tokens([token], special_tokens=True)

# save slow model and convert on load time
hf_tok.save_pretrained("/tmp/ernie4_5_tokenizer")
hf_tok_fast = LlamaTokenizerFast.from_pretrained("/tmp/ernie4_5_tokenizer", from_slow=True)
with tempfile.TemporaryDirectory() as tmp_dir:
hf_tok.save_pretrained(tmp_dir)
hf_tok_fast = LlamaTokenizerFast.from_pretrained(tmp_dir, from_slow=True)
hf_tok_fast.save_pretrained(args.output_dir, push_to_hub=args.push_to_hub)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import argparse
import json
import os
import tempfile
from pathlib import Path
from shutil import copyfile

Expand Down Expand Up @@ -72,7 +73,7 @@
"rope_parameters",
]

TMP_TOKENIZER_DIR = "/tmp/ernie_vl_tokenizer"
TMP_TOKENIZER_DIR = tempfile.mkdtemp(prefix="ernie_vl_tokenizer_")
TOKENIZER_CONFIG_FILE = "tokenizer_config.json"
DEFAULT_CHAT_TEMPLATE = """
{%- set image_count = namespace(value=0) -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Convert Hubert checkpoint."""

import argparse
import ast

import torch
from s3prl.hub import distilhubert
Expand Down Expand Up @@ -158,7 +159,7 @@ def convert_config(model):
config.apply_spec_augment = False
config.attention_dropout = fs_config.attention_dropout
config.conv_bias = False
conv_layers = eval(fs_config.extractor_conv_feature_layers)
conv_layers = ast.literal_eval(fs_config.extractor_conv_feature_layers)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would work, so ok

config.conv_dim = [x[0] for x in conv_layers]
config.conv_kernel = [x[1] for x in conv_layers]
config.conv_stride = [x[2] for x in conv_layers]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Convert SEW checkpoint."""

import argparse
import ast
import json
import os

Expand Down Expand Up @@ -177,7 +178,7 @@ def convert_config(model, is_finetuned):
fs_config = model.cfg

config.conv_bias = fs_config.conv_bias
conv_layers = eval(fs_config.conv_feature_layers)
conv_layers = ast.literal_eval(fs_config.conv_feature_layers)
config.conv_dim = [x[0] for x in conv_layers]
config.conv_kernel = [x[1] for x in conv_layers]
config.conv_stride = [x[2] for x in conv_layers]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Convert SEW checkpoint."""

import argparse
import ast
import json
import os

Expand Down Expand Up @@ -181,7 +182,7 @@ def convert_config(model, is_finetuned):
fs_config = model.cfg

config.conv_bias = fs_config.conv_bias
conv_layers = eval(fs_config.conv_feature_layers)
conv_layers = ast.literal_eval(fs_config.conv_feature_layers)
config.conv_dim = [x[0] for x in conv_layers]
config.conv_kernel = [x[1] for x in conv_layers]
config.conv_stride = [x[2] for x in conv_layers]
Expand Down
4 changes: 2 additions & 2 deletions src/transformers/models/videomt/convert_videomt_to_hf.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ def verify_conversion_against_github_reference(
with tempfile.TemporaryDirectory(prefix="videomt_ref_") as tmp_dir:
repo_path = Path(reference_repo_path) if reference_repo_path is not None else Path(tmp_dir) / "videomt"
if reference_repo_path is None:
subprocess.run(
["git", "clone", "--depth", "1", "https://github.com/tue-mps/videomt", str(repo_path)],
subprocess.run( # noqa: S603
["git", "clone", "--depth", "1", "https://github.com/tue-mps/videomt", str(repo_path)], # noqa: S607
check=True,
capture_output=True,
text=True,
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/pipelines/audio_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def ffmpeg_read(bpayload: bytes, sampling_rate: int) -> np.ndarray:
]

try:
ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
ffmpeg_process = subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) # noqa: S603
except FileNotFoundError:
raise ValueError("ffmpeg was not found but is required to load audio files from filename")
output_stream = ffmpeg_process.communicate(bpayload)
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/pipelines/audio_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def ffmpeg_read(bpayload: bytes, sampling_rate: int) -> np.ndarray:
]

try:
with subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as ffmpeg_process:
with subprocess.Popen(ffmpeg_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE) as ffmpeg_process: # noqa: S603
output_stream = ffmpeg_process.communicate(bpayload)
except FileNotFoundError as error:
raise ValueError("ffmpeg was not found but is required to load audio files from filename") from error
Expand Down Expand Up @@ -265,7 +265,7 @@ def _ffmpeg_stream(ffmpeg_command, buflen: int):
"""
bufsize = 2**24 # 16Mo
try:
with subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE, bufsize=bufsize) as ffmpeg_process:
with subprocess.Popen(ffmpeg_command, stdout=subprocess.PIPE, bufsize=bufsize) as ffmpeg_process: # noqa: S603
while True:
raw = ffmpeg_process.stdout.read(buflen)
if raw == b"":
Expand All @@ -282,7 +282,7 @@ def _get_microphone_name():
command = ["ffmpeg", "-list_devices", "true", "-f", "dshow", "-i", ""]

try:
ffmpeg_devices = subprocess.run(command, text=True, stderr=subprocess.PIPE, encoding="utf-8")
ffmpeg_devices = subprocess.run(command, text=True, stderr=subprocess.PIPE, encoding="utf-8") # noqa: S603
microphone_lines = [line for line in ffmpeg_devices.stderr.splitlines() if "(audio)" in line]

if microphone_lines:
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ def run_command(command: list[str], return_stdout=False):
if an error occurred while running `command`
"""
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
output = subprocess.check_output(command, stderr=subprocess.STDOUT) # noqa: S603
if return_stdout:
if hasattr(output, "decode"):
output = output.decode("utf-8")
Expand Down Expand Up @@ -2774,7 +2774,7 @@ def wrapper(*args, **kwargs):
else:
command = [f"{sys.executable}", "-m", "pytest", f"{test}"]

subprocess.run(command, env=env, check=True, capture_output=True)
subprocess.run(command, env=env, check=True, capture_output=True) # noqa: S603
except subprocess.CalledProcessError as e:
exception_message = e.stdout.decode()
lines = exception_message.split("\n")
Expand Down Expand Up @@ -3799,7 +3799,7 @@ def torchrun(script: str, nproc_per_node: int, is_torchrun: bool = True, env: di

# Note that the subprocess will be waited for here, and raise an error if not successful
try:
_ = subprocess.run(cmd, capture_output=True, env=env, text=True, check=True)
_ = subprocess.run(cmd, capture_output=True, env=env, text=True, check=True) # noqa: S603
except subprocess.CalledProcessError as e:
raise Exception(f"The following error was captured: {e.stderr}")

Expand Down
1 change: 1 addition & 0 deletions src/transformers/tokenization_utils_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3402,6 +3402,7 @@ def find_sentencepiece_model_file(pretrained_model_name_or_path, **kwargs):
return candidate
except Exception:
# TODO: tighten to OSError / ProxyError
logger.debug("Failed to check for %s in %s, skipping.", candidate, pretrained_model_name_or_path)
continue

subfolder = kwargs.get("subfolder", "")
Expand Down
10 changes: 9 additions & 1 deletion src/transformers/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def is_ninja_available() -> bool:
[ninja](https://ninja-build.org/) build system is available on the system, `False` otherwise.
"""
try:
subprocess.check_output(["ninja", "--version"])
subprocess.check_output(["ninja", "--version"]) # noqa: S603, S607
except Exception:
return False
else:
Expand Down Expand Up @@ -2407,6 +2407,9 @@ def call(self, *args, **kwargs):
break
except Exception:
# If this candidate fails, try the next one
logger.debug(
"Failed to resolve %s via candidate %s, trying next.", name, candidate_name
)
continue
else:
# Candidate not in _class_to_module - might need recursive resolution
Expand All @@ -2423,6 +2426,11 @@ def call(self, *args, **kwargs):
setattr(self, name, value)
break
except Exception:
logger.debug(
"Failed to resolve %s via recursive lookup of %s, trying next.",
name,
candidate_name,
)
continue
except (ImportError, AttributeError):
pass
Expand Down
11 changes: 6 additions & 5 deletions tests/models/dia/test_modeling_dia.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"""Testing suite for the PyTorch Dia model."""

import copy
import pathlib
import os
import shutil
import tempfile
import unittest

Expand Down Expand Up @@ -546,14 +547,14 @@ def setUp(self):
audio_sample_2 = librispeech_dummy[-2]["audio"]["array"]
# 10 and 5 codebooks as prefix - saved as files as we need wav files for the original Dia
dac_chunk_len = 512
self.audio_prompt_1_path = "/tmp/dia_test_sample_1.mp3"
self.audio_prompt_2_path = "/tmp/dia_test_sample_2.mp3"
self._tmp_dir = tempfile.mkdtemp(prefix="dia_test_")
self.audio_prompt_1_path = os.path.join(self._tmp_dir, "dia_test_sample_1.mp3")
self.audio_prompt_2_path = os.path.join(self._tmp_dir, "dia_test_sample_2.mp3")
Comment on lines +550 to +552

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is integration test (decoreated @slow) - make sure we run this before merge, although the change is small and clear.

sf.write(self.audio_prompt_1_path, audio_sample_1[: (dac_chunk_len * 10)], self.sampling_rate)
sf.write(self.audio_prompt_2_path, audio_sample_2[: (dac_chunk_len * 5)], self.sampling_rate)

def tearDown(self):
pathlib.Path(self.audio_prompt_1_path).unlink()
pathlib.Path(self.audio_prompt_2_path).unlink()
shutil.rmtree(self._tmp_dir, ignore_errors=True)
cleanup(torch_device, gc_collect=True)

@slow
Expand Down
7 changes: 4 additions & 3 deletions tests/tokenization/test_tokenization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ def test_special_tokens_overwrite(self):
== text_with_nonspecial_tokens
)

# Checking if this carries over even after saving and relaoding
tokenizer.save_pretrained("/tmp/ernie_tokenizer")
new_tokenizer = AutoTokenizer.from_pretrained("/tmp/ernie_tokenizer")
# Checking if this carries over even after saving and reloading
with tempfile.TemporaryDirectory() as tmp_dir:
tokenizer.save_pretrained(tmp_dir)
new_tokenizer = AutoTokenizer.from_pretrained(tmp_dir)
self.assertTrue(
new_tokenizer.decode(new_tokenizer.encode(text_with_nonspecial_tokens), skip_special_tokens=True)
== text_with_nonspecial_tokens
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/test_auto_docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""

import importlib
import logging
import os
import statistics
import sys
Expand Down Expand Up @@ -724,6 +725,7 @@ def _import_all():
try:
importlib.import_module(mod)
except Exception:
logging.debug("Failed to import module %s, skipping.", mod)
continue

_import_all() # warmup
Expand Down
14 changes: 8 additions & 6 deletions utils/add_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def check_file_exists_on_github(file_path: str) -> bool:
def get_modified_cards() -> list[str]:
"""Get the list of model names from modified files in docs/source/en/model_doc/"""

current_branch = subprocess.check_output(["git", "branch", "--show-current"], text=True).strip()
current_branch = subprocess.check_output(["git", "branch", "--show-current"], text=True).strip() # noqa: S607
if current_branch == "main":
# On main branch, only uncommitted changes detected
result = subprocess.check_output(["git", "diff", "--name-only", "HEAD"], text=True)
result = subprocess.check_output(["git", "diff", "--name-only", "HEAD"], text=True) # noqa: S607
else:
fork_point_sha = subprocess.check_output("git merge-base main HEAD".split()).decode("utf-8")
result = subprocess.check_output(f"git diff --name-only {fork_point_sha}".split()).decode("utf-8")
fork_point_sha = subprocess.check_output("git merge-base main HEAD".split()).decode("utf-8") # noqa: S603
result = subprocess.check_output(f"git diff --name-only {fork_point_sha}".split()).decode("utf-8") # noqa: S603

model_names = []
for line in result.strip().split("\n"):
Expand Down Expand Up @@ -159,8 +159,9 @@ def get_first_commit_date(model_name: str | None) -> str:
final_date = date.today().isoformat()
else:
# File exists on GitHub main branch, get the first commit date from local git history
final_date = subprocess.check_output(
["git", "log", "--reverse", "--pretty=format:%ad", "--date=iso", file_path], text=True
final_date = subprocess.check_output( # noqa: S603
["git", "log", "--reverse", "--pretty=format:%ad", "--date=iso", file_path], # noqa: S607
text=True,
)
return final_date.strip().split("\n")[0][:10]

Expand Down Expand Up @@ -208,6 +209,7 @@ def replace_paper_links(file_path: str) -> bool:

except Exception:
# Paper not available on huggingface, keep arxiv link
logger.debug("Paper %s not available on huggingface, keeping arxiv link.", paper_id)
continue

# Write back only if content changed
Expand Down
Loading
Loading