Fix unused Python variables outside torch/ and test/ (#136359)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/136359
Approved by: https://github.com/albanD
This commit is contained in:
Tom Ritchford
2024-12-11 14:00:52 +00:00
committed by PyTorch MergeBot
parent 241bf047b3
commit 498a7808ff
49 changed files with 39 additions and 86 deletions

View File

@ -332,7 +332,7 @@ def process_jobs(
# The job name from github is in the PLATFORM / JOB (CONFIG) format, so breaking
# it into its two components first
current_platform, _ = (n.strip() for n in job_name.split(JOB_NAME_SEP, 1) if n)
except ValueError as error:
except ValueError:
warnings.warn(f"Invalid job name {job_name}, returning")
return test_matrix

View File

@ -258,7 +258,7 @@ def load_yaml(yaml_text: str) -> Any:
try:
data = yaml.safe_load(yaml_text)
return data
except yaml.YAMLError as exc:
except yaml.YAMLError:
log.exception("Error loading YAML")
raise

View File

@ -898,7 +898,7 @@ class TestBypassFailures(TestCase):
repo = DummyGitRepo()
# Check that failure is classified as flaky but still raises exception
with warnings.catch_warnings(record=True) as w, self.assertRaises(RuntimeError):
rule = find_matching_merge_rule(pr, repo)
find_matching_merge_rule(pr, repo)
self.assertEqual(len(w), 1)
self.assertIn(
"1 checks failed but were likely due flakiness or broken trunk",

View File

@ -1747,7 +1747,7 @@ def get_classifications(
try:
print(f"From Dr.CI checkrun summary: {drci_summary}")
drci_classifications = json.loads(str(drci_summary))
except json.JSONDecodeError as error:
except json.JSONDecodeError:
warn("Invalid Dr.CI checkrun summary")
drci_classifications = {}
@ -1918,7 +1918,6 @@ def do_revert_prs(
dry_run: bool = False,
) -> None:
# Prepare and push revert commits
commit_shas: List[str] = []
for commit_sha, pr in shas_and_prs:
revert_msg = f"\nReverted {pr.get_pr_url()} on behalf of {prefix_with_github_url(author_login)}"
revert_msg += extra_msg

View File

@ -326,7 +326,7 @@ jobs:
try:
data = yaml.safe_load(yaml_text)
return data
except yaml.YAMLError as exc:
except yaml.YAMLError:
log.exception("Error loading YAML")
raise

View File

@ -72,7 +72,6 @@ class CoordinatorBase:
print(f"Episode {ep} - ", end="")
n_steps = episode_steps
agent_start_time = time.time()
futs = []
for ob_rref in self.ob_rrefs:

View File

@ -19,7 +19,7 @@ flaky_models = {
def get_field(csv, model_name: str, field: str):
try:
return csv.loc[csv["name"] == model_name][field].item()
except Exception as e:
except Exception:
return None

View File

@ -9,7 +9,7 @@ import pandas as pd
def get_field(csv, model_name: str, field: str):
try:
return csv.loc[csv["name"] == model_name][field].item()
except Exception as e:
except Exception:
return None

View File

@ -671,7 +671,7 @@ def print_summary_table(data, print_dataframe=False):
col.ljust(width),
f"gmean={gmean(cdata):.2f}x mean={cdata.mean():.3f}x",
)
except Exception as e:
except Exception:
pass
@ -3018,7 +3018,7 @@ class BenchmarkRunner:
)
):
is_same = False
except Exception as e:
except Exception:
# Sometimes torch.allclose may throw RuntimeError
is_same = False
@ -3110,7 +3110,7 @@ class BenchmarkRunner:
tol=tolerance,
):
is_same = False
except Exception as e:
except Exception:
# Sometimes torch.allclose may throw RuntimeError
is_same = False
@ -3157,7 +3157,7 @@ class BenchmarkRunner:
self.init_optimizer(name, current_device, model.parameters())
optimized_model_iter_fn = optimize_ctx(self.run_n_iterations)
new_result = optimized_model_iter_fn(model, example_inputs)
except Exception as e:
except Exception:
log.exception("")
print(
"TorchDynamo optimized model failed to run because of following error"
@ -3542,7 +3542,7 @@ class BenchmarkRunner:
try:
shutil.move("repro.py", f"{repro_dir}/{name}_repro.py")
except OSError as e:
except OSError:
logging.error("Could not find repro script for model %s", name)
else:
logging.info(
@ -4369,9 +4369,6 @@ def run(runner, args, original_dir=None):
# Set translation validation on by default on CI accuracy runs.
torch.fx.experimental._config.translation_validation = True
ci = functools.partial(
CI, args.backend, training=args.training, dynamic=args.dynamic_shapes
)
if args.ddp:
assert args.training, "DDP benchmark requires --training mode"
torch._dynamo.config.optimize_ddp = args.optimize_ddp_mode

View File

@ -90,7 +90,7 @@ def model_iter_fn(model, example_inputs, collect_outputs=False):
def get_model(args):
if args.torchbench_model:
old_cwd = setup_torchbench_cwd()
setup_torchbench_cwd()
module = importlib.import_module(
f"torchbenchmark.models.{args.torchbench_model}"
)

View File

@ -1451,7 +1451,7 @@ class DashboardUpdater:
RegressionDetector(self.args).generate_comment()
try:
RegressionTracker(self.args).diff()
except Exception as e:
except Exception:
logging.exception("")
with open(f"{self.args.output_dir}/gh_regression.txt", "w") as gh_fh:
gh_fh.write("")

View File

@ -236,7 +236,6 @@ class TorchBenchmarkRunner(BenchmarkRunner):
)
is_training = self.args.training
use_eval_mode = self.args.use_eval_mode
dynamic_shapes = self.args.dynamic_shapes
candidates = [
f"torchbenchmark.models.{model_name}",
f"torchbenchmark.canary_models.{model_name}",

View File

@ -205,7 +205,7 @@ def bench(rnn_runners, group_name, print_json=False, sep=" ", **params):
result_with_no_info = result._replace(info_fwd="None", info_bwd="None")
print_stderr(pretty_print(result_with_no_info, sep=sep))
results[name] = result
except Exception as e:
except Exception:
if not print_json:
raise

View File

@ -338,8 +338,8 @@ def layernorm_pytorch_lstm_creator(**kwargs):
seq_len = len(input.unbind(0))
hy, cy = new_hidden
for i in range(seq_len):
ln_i_output = ln_i(ln_input1)
ln_h_output = ln_h(ln_input1)
ln_i(ln_input1)
ln_h(ln_input1)
cy = ln_c(cy)
return out, (hy, cy)

View File

@ -40,7 +40,7 @@ def cuda_sync(func, *args, **kwargs):
class TestBenchNetwork:
# See 'modeldef' fixture, which provides the things to benchmark
def test_forward(self, modeldef, benchmark):
forward_output = benchmark(cuda_sync, modeldef.forward, *modeldef.inputs)
benchmark(cuda_sync, modeldef.forward, *modeldef.inputs)
def test_backward(self, modeldef, benchmark):
backward_input = modeldef.forward(*modeldef.inputs)

View File

@ -25,7 +25,7 @@ SUPPORTED_OPS = {"add_op"}
def parse_op_args(op):
op_list = op.split(",")
op_list = op.split(",") # noqa: F841
def print_results(result):

View File

@ -190,9 +190,9 @@ def run_once(model: Callable, inp: InputsType, task: str, v: VType, **kwargs) ->
func = get_task_func(task)
if v is not None:
res = func(model, inp, v=v, strict=True)
func(model, inp, v=v, strict=True)
else:
res = func(model, inp, strict=True)
func(model, inp, strict=True)
def run_once_functorch(

View File

@ -284,7 +284,6 @@ def run_benchmarks(operators, shapes):
shapes = [globals()[k] for k in shapes.split(",")]
print("fuser,device,operator,shape,time")
results = []
for shape, operator in itertools.product(shapes, operators):
nargs = len(inspect.signature(operator).parameters)
args = shape()

View File

@ -132,7 +132,6 @@ class WeightOnlyInt8Linear(torch.nn.Module):
target_dtype=None,
) -> None:
assert target_dtype is not None
factory_kwargs = {"device": device, "dtype": dtype}
super().__init__()
self.in_features = in_features
self.out_features = out_features

View File

@ -93,7 +93,6 @@ class WeightOnlyInt8Linear(torch.nn.Module):
device=None,
dtype=None,
) -> None:
factory_kwargs = {"device": device, "dtype": dtype}
super().__init__()
self.in_features = in_features
self.out_features = out_features

View File

@ -74,7 +74,7 @@ def parse_stmts(stmts: str) -> Tuple[str, str]:
assert len(lines) >= 3, f"Invalid string:\n{stmts}"
column_header_pattern = r"^Python\s{35}\| C\+\+(\s*)$"
signature_pattern = r"^: f\((.*)\)( -> (.+))?\s*$"
signature_pattern = r"^: f\((.*)\)( -> (.+))?\s*$" # noqa: F841
separation_pattern = r"^[-]{40} | [-]{40}$"
code_pattern = r"^(.{40}) \|($| (.*)$)"

View File

@ -6,14 +6,14 @@ import torch
def bench(nt_a, nt_b, niter):
# Warmup
nt_c = nt_a.bmm(nt_b)
nt_a.bmm(nt_b)
torch.cuda.synchronize()
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
for iter in range(niter):
nt_c = nt_a.bmm(nt_b)
nt_a.bmm(nt_b)
end_event.record()
torch.cuda.synchronize()
runtime = (start_event.elapsed_time(end_event)) / niter

View File

@ -111,10 +111,9 @@ def _build_test(
if tags is None:
raise ValueError("Missing tags in configs")
input_config = str(test_attrs)[1:-1].replace("'", "")
op = bench_op()
assert op is not None, "Can't create test"
tensor_error_info = None
# op_name_function is a dictionary which has op_name and op_function.
# an example of op_name_function is:
# {'op_name' : 'abs', 'op_function' : torch.abs}

View File

@ -31,8 +31,6 @@ class LSTMBenchmark(op_bench.TorchBenchmarkBase):
# The quantized.dynamic.LSTM has a bug. That's why we create a regular
# LSTM, and quantize it later. See issue #31192.
scale = 1.0 / 256
zero_point = 0
cell_nn = nn.LSTM(
input_size=I,
hidden_size=H,

View File

@ -97,7 +97,7 @@ if __name__ == "__main__":
with_stack=args.with_stack,
use_kineto=args.use_kineto,
use_cpu=not args.cuda_only,
) as prof:
):
x = workload(input_x)
return x

View File

@ -13,14 +13,14 @@ class Basic(Benchmark):
torch.save(x, "big_tensor.zip", _use_new_zipfile_serialization=use_new)
with Timer() as big2:
v = torch.load("big_tensor.zip")
torch.load("big_tensor.zip")
x = [torch.ones(10, 10) for i in range(200)]
with Timer() as small1:
torch.save(x, "small_tensor.zip", _use_new_zipfile_serialization=use_new)
with Timer() as small2:
v = torch.load("small_tensor.zip")
torch.load("small_tensor.zip")
return {
"Big Tensors Save": big1.ms_duration,

View File

@ -56,16 +56,12 @@ def load_sparse_matrix(path, device):
def gen_vector(path, device):
with open(path) as file:
nrows, ncols, nnz = (int(el) for el in file.readline().split(", "))
index_pointers = (int(el) for el in file.readline().split())
indices = (int(el) for el in file.readline().split())
return torch.randn(nrows, dtype=torch.double, device=device)
def gen_matrix(path, device):
with open(path) as file:
nrows, ncols, nnz = (int(el) for el in file.readline().split(", "))
index_pointers = (int(el) for el in file.readline().split())
indices = (int(el) for el in file.readline().split())
return torch.randn(nrows, ncols, dtype=torch.double, device=device)

View File

@ -374,7 +374,7 @@ if __name__ == "__main__":
for r in range(args.repeat):
try:
time_ms, performance_tflops = test_func(x, y, **meta)
except triton.compiler.OutOfResources as msg:
except triton.compiler.OutOfResources:
print(
f"op={op}[{meta_str}]({bsr_size},{k}x{n}) dtype={args.dtype} {sparsity=}(nnz={x._nnz()})"
f" blocksize={bm}x{bk} OutOfResources",

View File

@ -208,7 +208,6 @@ class SimpleElementBench(benchmark.Benchmark):
return "simple_element"
def memory_workload(self):
input_count = len(self.inputs)
if self.mode == "fwd":
sol_count = 2
algorithmic_count = 2

View File

@ -136,7 +136,7 @@ def run(
torch.testing.assert_close(
y_native_mha_fast, y_native_mha_slow, atol=1e-3, rtol=1e-3
)
except AssertionError as e:
except AssertionError:
error_dict[entry_name] += 1
pprint(error_dict)

View File

@ -98,8 +98,6 @@ def generate_inputs(
assert q_heads % kv_heads == 0
num_h_groups = q_heads // kv_heads
make_q = partial(
torch.rand, q_shape, device=device, dtype=dtype, requires_grad=requires_grad
)

View File

@ -211,7 +211,7 @@ def run_single_experiment(config: ExperimentConfig) -> ExperimentResults:
enable_flash=config.enable_flash,
enable_mem_efficient=config.enable_mem_efficient,
enable_cudnn=config.enable_cudnn,
) as kernel_choice, torch.inference_mode() as inference_mode:
):
dropout_p = 0.0
mask = None

View File

@ -62,7 +62,6 @@ class DelayedMulTensor(_Tensor):
plhs, levelslhs = self._lhs._tensor, self._lhs._levels
prhs, levelsrhs = self._rhs._tensor, self._rhs._levels
new_dims = tuple(d for d in self.dims if d not in dims)
new_levels = [l for l in self._levels if l not in dims]
fmt = "".join(
[

View File

@ -198,7 +198,6 @@ def __torch_function__(self, orig, cls, args, kwargs=empty_dict):
if orig in pointwise:
result_levels = llist()
arg_levels = llist()
to_expand = []
for i, f in enumerate(flat_args):
if isinstance(f, TensorLike):
@ -268,7 +267,6 @@ def positional(self, *dims):
needs_view = True
permute = list(range(len(levels)))
nflat = len(flat_dims)
for i, d in enumerate(flat_dims):
try:
idx = levels.index(d)

View File

@ -214,10 +214,6 @@ def main():
else:
generator = None
augmentations = [
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
]
normalize = [
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),

View File

@ -145,8 +145,6 @@ def loss_for_task(net, n_inner_iter, x_spt, y_spt, x_qry, y_qry):
def train(db, net, device, meta_opt, epoch, log):
params = dict(net.named_parameters())
buffers = dict(net.named_buffers())
n_train_iter = db.x_train.shape[0] // db.batchsz
for batch_idx in range(n_train_iter):

View File

@ -653,7 +653,6 @@ def gen_inplace_or_view_type(
) -> None:
# NOTE: see Note [Sharded File] at the top of the VariableType.cpp
# template regarding sharding of the generated files.
num_shards = 2
fm = FileManager(install_dir=out, template_dir=template_path, dry_run=False)
fm.write_sharded(

View File

@ -1319,8 +1319,6 @@ def emit_single_dispatch(
else:
schema_comment = f"// aten::{f.func}"
deprecated = "[deprecated] " if ps.deprecated else ""
# dispatch lambda signature
name = cpp.name(f.func)
lambda_formals = ", ".join(

View File

@ -46,7 +46,6 @@ from torchgen.api.types import (
BaseCppType,
BaseCType,
Binding,
DispatcherSignature,
intArrayRefT,
iTensorListRefT,
ListCType,
@ -1535,9 +1534,6 @@ def emit_body(
f: NativeFunction, input_base: str, unpacked_args: Sequence[str]
) -> str:
"""Dispatch call via function in a namespace or method on Tensor."""
dispatcher_sig = DispatcherSignature.from_schema(f.func)
dispatcher_exprs = dispatcher_sig.exprs()
# code-generated autograd kernels plumb and recompute dispatch keys directly through the kernel for performance.
# Ops also always have a function variant of the redispatch API.
# See Note [Plumbing Keys Through The Dispatcher] for details.

View File

@ -269,7 +269,7 @@ def main(args: list[str]) -> None:
native_yaml_path = os.path.join(options.source_path, "native/native_functions.yaml")
tags_yaml_path = os.path.join(options.source_path, "native/tags.yaml")
parsed_yaml = parse_native_yaml(native_yaml_path, tags_yaml_path)
native_functions, backend_indices = (
native_functions, _backend_indices = (
parsed_yaml.native_functions,
parsed_yaml.backend_indices,
)

View File

@ -95,7 +95,7 @@ class CMake:
print(" ".join(command))
try:
check_call(command, cwd=self.build_dir, env=env)
except (CalledProcessError, KeyboardInterrupt) as e:
except (CalledProcessError, KeyboardInterrupt):
# This error indicates that there was a problem with cmake, the
# Python backtrace adds no signal here so skip over it by catching
# the error and exiting manually

View File

@ -235,12 +235,12 @@ class UsageLogger:
if self._has_amdsmi:
try:
amdsmi.amdsmi_shut_down()
except amdsmi.AmdSmiException as e:
except amdsmi.AmdSmiException:
pass
if self._has_pynvml:
try:
pynvml.nvmlShutdown()
except pynvml.NVMLError as e:
except pynvml.NVMLError:
pass
def _get_per_process_gpu_info(self, handle: Any) -> list[dict[str, Any]]:
@ -306,7 +306,7 @@ class UsageLogger:
if "pss" in memory_full_info:
# only availiable in linux
info["pss_memory"] = f"{memory_full_info.pss / (1024 * 1024):.2f}"
except psutil.AccessDenied as e:
except psutil.AccessDenied:
# It's ok to skip this
pass
per_process_info.append(info)

View File

@ -50,7 +50,7 @@ class MentionedInPR(HeuristicInterface):
) + self._search_for_linked_issues(pr_body):
try:
linked_issue_bodies.append(get_issue_or_pr_body(int(issue)))
except Exception as e:
except Exception:
pass
mentioned = []

View File

@ -42,7 +42,7 @@ if IS_ROCM and not IS_MEM_LEAK_CHECK:
assert count > 0 # there must be at least 1 GPU
# Limiting to 8 GPUs(PROCS)
NUM_PROCS = min(count, 8)
except subprocess.CalledProcessError as e:
except subprocess.CalledProcessError:
# The safe default for ROCm GHA runners is to run tests serially.
NUM_PROCS = 1

View File

@ -64,7 +64,6 @@ class AHTrainDecisionTree(AHTrain):
Given a trained decision tree, and a dataframe containing the training data, returns a list of unsafe leaves.
"""
X = df[feature_columns]
y = df["winner"]
leaf_ids = model.apply(X)
unique_leaves = np.unique(leaf_ids)
@ -136,7 +135,6 @@ class AHTrainDecisionTree(AHTrain):
best_model = None
best_model_safe_proba = 0
best_model_num_correct = 0
best_model_num_wrong = 0
best_model_unsafe_leaves = []
columns = ["set", "crit", "max_depth", "min_samples_leaf"]
metrics_columns = []
@ -223,7 +221,6 @@ class AHTrainDecisionTree(AHTrain):
)
best_model = model
best_model_num_correct = num_correct
best_model_num_wrong = num_wrong
best_model_safe_proba = safe_proba
best_model_unsafe_leaves = unsafe_leaves
@ -786,7 +783,6 @@ class DecisionEvaluator:
def top_k_classes(self, model, probas, k, avail_choices):
# Get classes and their corresponding probabilities
classes = model.classes_
class_proba_pairs = list(zip(classes, probas))
# Sort by probability (descending) and filter out zero probabilities
sorted_classes = [

View File

@ -399,7 +399,6 @@ class AHTrainRegressionTree(AHTrain):
def dt_to_python(node, depth):
indent = " " * (depth + 1)
false_predicate = ""
if tree_.feature[node] != -2:
name = feature_name[node]
threshold = tree_.threshold[node]

View File

@ -488,9 +488,6 @@ extern "C" {{
"""
else:
c_shim_include = (
f"#include <torch/csrc/inductor/aoti_torch/generated/c_shim_{device}.h>"
)
return f"""
{warning}

View File

@ -693,7 +693,7 @@ def emit_inplace_functionalization_body(
]
)
else:
mutable_input_post_processing = "\n".join(
mutable_input_post_processing = "\n".join( # noqa: F841
[
f"""
at::functionalization::impl::replace_({a.name}, tmp_output);

View File

@ -317,7 +317,6 @@ def get_upgrader_bytecode_function_to_index_map(
def write_cpp(cpp_path: str, upgrader_dict: list[dict[str, Any]]) -> None:
body_parts = []
upgrader_bytecode_function_to_index_map = (
get_upgrader_bytecode_function_to_index_map(upgrader_dict)
)
@ -335,7 +334,6 @@ def write_cpp(cpp_path: str, upgrader_dict: list[dict[str, Any]]) -> None:
operator_list_str = ""
for table_name, contents in bytecode.items():
element = ByteCode[table_name]
body_string = ""
if element is ByteCode.instructions:
instruction_list_str = construct_instruction(contents)
elif element is ByteCode.constants:
@ -364,10 +362,8 @@ def write_cpp(cpp_path: str, upgrader_dict: list[dict[str, Any]]) -> None:
operator_version_map=version_map_src,
upgrader_bytecode="".join(all_upgrader_src_string).lstrip("\n"),
)
body_parts.append(upgrader_file_content)
print("writing file to : ", cpp_path + "/" + UPGRADER_MOBILE_FILE_NAME)
with open(os.path.join(cpp_path, UPGRADER_MOBILE_FILE_NAME), "wb") as out_file:
final_output = "".join(body_parts)
out_file.write(upgrader_file_content.encode("utf-8"))